forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
38 lines (33 loc) · 1.19 KB
/
cachematrix.R
File metadata and controls
38 lines (33 loc) · 1.19 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
## These two functions work in tandem to enable caching for a square matrix
## inversion calculation.
## This function takes a matrix, stores it in a cache variable
## and provides a list of setter and getter functions.
## It acts as a wrapper around the matrix, retaining the inverse in cache
makeCacheMatrix <- function(x = matrix()) {
inverseX <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setInverse <- function(inverse) inverseX <<- inverse
getInverse <- function() inverseX
list( set = set, get = get, setIntverse = setInverse, getInverse = getInverse )
}
## This function takes a list returned by makeCacheMatrix and
## returns matrix inverse, getting it from cache if possible.
## If the matrix inverse has not been previously computed (and hence not present in cache)
## then it computes the inverse, stores it in cache and returns the newly computed value
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inverseX <- x$getInverse
if ( !is.null(inverseX) ) {
message(inverseX)
message("getting cached data")
return(inverseX)
}
data <- x$get()
inverseX <- solve(X, ...)
x$setInverse(inverseX)
inverseX
}