forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcachematrix.R
More file actions
130 lines (107 loc) · 3.64 KB
/
cachematrix.R
File metadata and controls
130 lines (107 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
## This file provides two functions that perform functions to calculate
## the inverse of a user supplied matrix. The result is cached so that
## calls after the first will quickly return the cached value
##
## NOTE: Per the assignment rules, these functions only support a square
## invertable matix (N by N).
##
## A unit test program is provided by the 'cacheSolve.UnitTest' function in
## this file.
##
#####################################################################
## Calculate the inverse of a matix and caches the result in case
## it is requested again
##
## 'x' is a matrix
##
## Return a special "vector", which is really a list containing a function to:
## - 'set' -- set the the matrix
## - 'get' -- get the the matrix
## - 'setInverse' -- set the value of the inverse matrix
## - 'getInverse' -- get the inverse of the matrix
##
makeCacheMatrix <- function(x = matrix())
{
# Initialize the cached matrix
cachedMatrix <- NULL
set <- function(y)
{
x <<- y
cachedMatrix <<- NULL
}
get <- function() x
setInverse <- function(inverse) cachedMatrix <<- inverse
getInverse <- function() cachedMatrix
list(set = set, get = get,
setinverse = setInverse,
getinverse = getInverse)
}
#####################################################################
## Get the inverse of a matrix
##
## 'x' is vector of functions calculated by the "makeCacheMatrix" routine
## Return a matrix that is the inverse of 'x'
cacheSolve <- function(x, ...)
{
# See if an inverse has already been calculated
inverse <- x$getinverse()
if (!is.null(inverse))
{
# Yes, return cached result
message("getting cached data")
return(inverse)
}
# Retrieve the matrix
theMatrix <- x$get()
# Calculate the inverse of the matrix
inverse <- solve(theMatrix) %*% theMatrix
# Save off/cache the inverse
x$setinverse(inverse)
# Return the inverse as a result
inverse
}
#####################################################################
## Unit Test
##
## Simple unit test of the 'cacheSolve' function. The default is
## a normalized distribution of numbers in a 7x7 matrix. You can change
## this size with the 'matrixSize' parameter
##
## Returns 'TRUE' if successful
##
cacheSolve.UnitTest <- function(matrixSize = 7)
{
# Create a 7x7 matrix to operate on. Random numbers....
testMatrix <- matrix(as.numeric(runif(matrixSize*matrixSize, 0, 100)),
nrow = matrixSize, ncol = matrixSize)
# Output for comparison later
print("The matrix is:")
print(testMatrix)
# Compute the inverse here for test comparison later
testInverse <- solve(testMatrix) %*% testMatrix
############################################################
# Create the object that caches the mean of the above vector
invCacher <- makeCacheMatrix(testMatrix)
############################################################
# Now ask for and print out the inverse
theInverse <- cacheSolve(invCacher)
print("The inverse is")
print(theInverse)
# Verify
if (!identical(theInverse, testInverse))
{
warning("The matrix inverse returned was not correct")
return(FALSE)
}
# Ask for it again. It should print out the 'getting cached data'
# message...
secondInverse <- cacheSolve(invCacher)
if (!identical(secondInverse, testInverse))
{
warning("The cached matrix inverse returned was not correct")
return(FALSE)
}
print("Success if you see the 'getting cached data' message above this line")
# Success
TRUE
}