|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## These two functions work in tandem to enable caching for a square matrix |
| 2 | +## inversion calculation. |
3 | 3 |
|
4 |
| -## Write a short comment describing this function |
| 4 | +## This function takes a matrix, stores it in a cache variable |
| 5 | +## and provides a list of setter and getter functions. |
| 6 | +## It acts as a wrapper around the matrix, retaining the inverse in cache |
5 | 7 |
|
6 | 8 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 9 | + inverseX <- NULL |
| 10 | + set <- function(y) { |
| 11 | + x <<- y |
| 12 | + m <<- NULL |
| 13 | + } |
| 14 | + get <- function() x |
| 15 | + setInverse <- function(inverse) inverseX <<- inverse |
| 16 | + getInverse <- function() inverseX |
| 17 | + list( set = set, get = get, setIntverse = setInverse, getInverse = getInverse ) |
8 | 18 | }
|
9 | 19 |
|
10 | 20 |
|
11 |
| -## Write a short comment describing this function |
| 21 | +## This function takes a list returned by makeCacheMatrix and |
| 22 | +## returns matrix inverse, getting it from cache if possible. |
| 23 | +## If the matrix inverse has not been previously computed (and hence not present in cache) |
| 24 | +## then it computes the inverse, stores it in cache and returns the newly computed value |
12 | 25 |
|
13 | 26 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 27 | + ## Return a matrix that is the inverse of 'x' |
| 28 | + inverseX <- x$getInverse |
| 29 | + if ( !is.null(inverseX) ) { |
| 30 | + message(inverseX) |
| 31 | + message("getting cached data") |
| 32 | + return(inverseX) |
| 33 | + } |
| 34 | + data <- x$get() |
| 35 | + inverseX <- solve(X, ...) |
| 36 | + x$setInverse(inverseX) |
| 37 | + inverseX |
15 | 38 | }
|
0 commit comments