|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
3 |
| - |
4 |
| -## Write a short comment describing this function |
5 |
| - |
6 |
| -makeCacheMatrix <- function(x = matrix()) { |
| 1 | +## A pair of functions that combined can create an object wrapping a square |
| 2 | +## matrix that can cache its inverse to avoid the expense of this operation |
| 3 | +## executing more than once. |
7 | 4 |
|
| 5 | +## 'matrix_val' is a square matrix to be contained in the cache enabled object. |
| 6 | +## |
| 7 | +## Return an object that contains the input martix_val with the ability to |
| 8 | +## cache its inverse. |
| 9 | +## |
| 10 | +makeCacheMatrix <- function(matrix_val = matrix()) { |
| 11 | + inverse_val <- NULL |
| 12 | + set <- function(new_matrix) { |
| 13 | + matrix_val <<- new_matrix |
| 14 | + inverse_val <<- NULL |
| 15 | + } |
| 16 | + get <- function() { |
| 17 | + matrix_val |
| 18 | + } |
| 19 | + setInverse <- function(updated) { |
| 20 | + inverse_val <<- updated |
| 21 | + } |
| 22 | + getInverse <- function() { |
| 23 | + inverse_val |
| 24 | + } |
| 25 | + list(set = set, |
| 26 | + get = get, |
| 27 | + setInverse = setInverse, |
| 28 | + getInverse = getInverse) |
8 | 29 | }
|
9 | 30 |
|
10 | 31 |
|
11 |
| -## Write a short comment describing this function |
12 |
| - |
| 32 | +## 'x' the cache matrix object created by a call to makeCacheMatrix. |
| 33 | +## '...' any optional arguments that should be passed to the call to solve() |
| 34 | +## used to invert the contained matrix. |
| 35 | +## |
| 36 | +## Return the inverted matrix value, this will be calculated on the first call |
| 37 | +## but the result will be stored in the matrix object for later use. |
| 38 | +## |
13 | 39 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 40 | + ## Return a matrix that is the inverse of 'x' |
| 41 | + m <- x$getInverse() |
| 42 | + if(is.null(m)) { |
| 43 | + data <- x$get() |
| 44 | + m <- solve(data, ...) |
| 45 | + x$setInverse(m) |
| 46 | + } else { |
| 47 | + message("getting cached data") |
| 48 | + } |
| 49 | + m |
15 | 50 | }
|
0 commit comments