|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## A "matrix" that carries an embedded cached value of it's inverse |
| 2 | + |
| 3 | +# the matrix is implemented as a "poor man's object" i.e. a record of |
| 4 | +# closures with a shared mutable environment. |
| 5 | +# the two functions set() and get() allow accessing and mutating the |
| 6 | +# embedded matrix object, while set.inverse and get.inverse allow |
| 7 | +# manipulation of the inverse object in the shared environment. |
| 8 | +# |
| 9 | +# |
3 | 10 |
|
4 |
| -## Write a short comment describing this function |
5 | 11 |
|
6 | 12 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - cached.result <- NULL |
| 13 | + inverse <- NULL |
8 | 14 | set <- function(y) {
|
9 | 15 | x <<- y
|
10 |
| - cached.result <<- NULL |
| 16 | + inverse <<- NULL |
11 | 17 | }
|
12 | 18 | get <- function() x
|
13 |
| - set.cached.result <- function(result) cached.result <<- result |
14 |
| - get.cached.result <- function() cached.result |
| 19 | + set.inverse <- function(result) inverse <<- result |
| 20 | + get.inverse <- function() inverse |
15 | 21 | list(set = set, get = get,
|
16 |
| - set.cached.result = set.cached.result, |
17 |
| - get.cached.result = get.cached.result) |
| 22 | + set.inverse = set.inverse, |
| 23 | + get.inverse = get.inverse) |
18 | 24 | }
|
19 | 25 |
|
20 | 26 |
|
21 |
| -## Write a short comment describing this function |
| 27 | +# Cache solve can be used to get the inverse of a matrix |
| 28 | +# in a smart way by looking up the cached value and memoizing |
| 29 | +# it after a computation for future use. |
22 | 30 |
|
23 | 31 | cacheSolve <- function(matrix, ...) {
|
24 |
| - res <- matrix$get.cached.result() |
| 32 | + res <- matrix$get.inverse() |
25 | 33 | if(!is.null(res)) {
|
26 | 34 | message("getting cached data")
|
27 | 35 | return(res)
|
28 | 36 | }
|
29 | 37 | data <- matrix$get()
|
30 | 38 | res <- solve(data)
|
31 |
| - matrix$set.cached.result(res) |
| 39 | + matrix$set.inverse(res) |
32 | 40 | res
|
33 | 41 | }
|
34 | 42 |
|
|
0 commit comments