|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
3 |
| - |
4 |
| -## Write a short comment describing this function |
| 1 | +## I did not follow the pattern establisedh by the makeVector/cachemean |
| 2 | +## examples. This implementation simplifies by pushing the cache check |
| 3 | +## down into the 'special' matrix object. |
| 4 | +## |
| 5 | +## Test it ithis way: |
| 6 | +## |
| 7 | +## x <- matrix(rnorm(100), 10, 10) # creates the original matrix |
| 8 | +## xinv <- solve(x) # actual inverse of x. |
| 9 | +## xspecial <- makeCacheMatrix(x) # creates the special version of x |
| 10 | +## xspecial$isCalculated() # should return FALSE because we haven't calculated the inverse |
| 11 | +## xspecialInv <- xspecial$getInverse() # calculates the inverse. |
| 12 | +## xspecial$isCalculated() # should return TRUE because we've calculated the inverse. |
| 13 | +## identical(xinv, xspecialInv) # should return TRUE because they are both the inverse of x |
5 | 14 |
|
| 15 | +## returns a collection of functions |
6 | 16 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 17 | + cachedInverse <- NULL |
| 18 | + |
| 19 | + # handy way to test of the inverse is already calculated. |
| 20 | + # used for testing. |
| 21 | + isCalculated <- function() { |
| 22 | + !is.null(cachedInverse) |
| 23 | + } |
| 24 | + |
| 25 | + ## do the 'solve' part here. if the cache is NULL compute the cache, |
| 26 | + ## then return the case. |
| 27 | + getInverse <- function() { |
| 28 | + if (is.null(cachedInverse)) { |
| 29 | + cachedInverse <<- solve(x) |
| 30 | + } |
| 31 | + return (cachedInverse) |
| 32 | + } |
| 33 | + |
| 34 | + # return getInverse and isCalculated |
| 35 | + list(getInverse = getInverse, |
| 36 | + isCalculated = isCalculated) |
8 | 37 | }
|
9 | 38 |
|
10 | 39 |
|
11 |
| -## Write a short comment describing this function |
12 |
| - |
| 40 | +## fetch the inverse from the object. |
13 | 41 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 42 | + ## Return a matrix that is the inverse of 'x' |
| 43 | + x$getInverse() |
15 | 44 | }
|
| 45 | + |
0 commit comments