|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
3 | 1 |
|
4 |
| -## Write a short comment describing this function |
| 2 | +## This file is the solution to programming assignment 2 and contains 2 functions. |
| 3 | + |
| 4 | +## Make a list of functions that work on a captured varible to store a matrix and a cached version of the inverse of the same matrix. |
| 5 | +## No computation in this funxtion, only getter & setter functions that work on the captured variable. |
5 | 6 |
|
6 | 7 | makeCacheMatrix <- function(x = matrix()) {
|
7 | 8 |
|
| 9 | + ## Store the cached inverse matrix "inside" this closure |
| 10 | + cache <- NULL |
| 11 | + |
| 12 | + ## Getter / setter functions to return |
| 13 | + get <- function() x |
| 14 | + getInverse <- function() cache |
| 15 | + setInverse <- function(m) cache <<- m |
| 16 | + |
| 17 | + ## return a list of functions, like the example code |
| 18 | + list(get = get, getInverse = getInverse, setInverse = setInverse) |
8 | 19 | }
|
9 | 20 |
|
10 | 21 |
|
11 |
| -## Write a short comment describing this function |
| 22 | +## Solve the inverse of a matrix, using the closue we created with makeCacheMatrix |
| 23 | +## If the cache is set return that. If not, solve() for matrix and return the result after setting the cache |
12 | 24 |
|
13 | 25 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 26 | + |
| 27 | + inv <- x$getInverse() # # the varibale we will return after setting it to the correct matrix |
| 28 | + |
| 29 | + if(is.null(inv)){ |
| 30 | + # Nothing cached, solve and fill the cache |
| 31 | + matrix <- x$get() |
| 32 | + inv <- solve(matrix) |
| 33 | + x$setInverse(inv) |
| 34 | + } else { |
| 35 | + # Got cached result, nothing to do |
| 36 | + message("getting cached data") |
| 37 | + } |
| 38 | + |
| 39 | + inv |
| 40 | + |
15 | 41 | }
|
0 commit comments