forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
49 lines (44 loc) · 1.22 KB
/
cachematrix.R
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
## Two utility functions to provide cache for matrix inverse operation.
## NOTE: the input matrix has to be invertible.
## Create a special matrix with four helper function with cache.
makeCacheMatrix <- function(x = matrix()) {
## Initilize cache to NULL
cache <- NULL
## Parsing parameter for setter,reset cache to NULL.
set <- function(y) {
x <<- y
cache <<- NULL
}
## return x for getter
get <- function() {
x
}
## cache setter to chain with invoked solve function.
setInverse <- function(solve) {
cache <<- solve
}
## cache getter for inverted matrix, if any.
getInverse <- function() {
cache
}
## Create list for funcation calls
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## Return a matrix that is the inverse of 'x', use cached result if possible.
cacheSolve <- function(x, ...) {
## Try evaluate the cache, return if is not NULL.
cache <- x$getInverse()
if(!is.null(cache)) {
message("getting cached data")
return(cache)
}
## Otherwise, retrive matrix via getter
data <- x$get()
## Save inverted matrix back to matrix
cache <- solve(data, ...)
x$setInverse(cache)
## Return invertied matrix
cache
}