|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## The functions defined below enable the use of a wrapper for a matrix so that |
| 2 | +## the result of its inverse calculation can be cached. The makeCacheMatrix |
| 3 | +## function creates the wrapper itself, and the cacheSolve function will return |
| 4 | +## the inverse matrix for any such wrapper. If cacheSolve has already |
| 5 | +## calculated the inverse for an input and cached it, it will simply return |
| 6 | +## that inverse rather than recalculating. See the comments preceding the |
| 7 | +## makeCacheMatrix and solveCache functions for more detail. |
3 | 8 |
|
4 |
| -## Write a short comment describing this function |
| 9 | +## The makeCacheMatrix function returns a wrapped matrix so that its inverse |
| 10 | +## matrix can be cached after the first time it is calculated. The get and |
| 11 | +## set functions provide access to the wrapped matrix itself, while the |
| 12 | +## getinverse and setinverse provide access to the inverse matrix. |
| 13 | +## For example, the cacheSolve function uses those functions to check |
| 14 | +## whether a cached inverse exists already, and to provide the inverse |
| 15 | +## value to be cached after the initial calculation. |
5 | 16 |
|
6 | 17 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 18 | + i <- NULL |
| 19 | + set <- function(y) { |
| 20 | + x <<- y |
| 21 | + i <<- NULL |
| 22 | + } |
| 23 | + get <- function() x |
| 24 | + setinverse <- function(inverse) i <<- inverse |
| 25 | + getinverse <- function() i |
| 26 | + list(set = set, |
| 27 | + get = get, |
| 28 | + setinverse = setinverse, |
| 29 | + getinverse = getinverse) |
8 | 30 | }
|
9 | 31 |
|
10 | 32 |
|
11 |
| -## Write a short comment describing this function |
| 33 | +## The cacheSolve function takes a cache matrix as input and returns the |
| 34 | +## inverse matrix. If that inverse had already been calculated for that cache |
| 35 | +## matrix, the cached value will be returned, avoiding any need to recalculate. |
| 36 | +## If the inverse had not yet been calculated for that cache matrix instance, |
| 37 | +## it will be calculated and returned, but also cached for any subsequent calls |
| 38 | +## to this function for that same cache matrix. |
12 | 39 |
|
13 | 40 | cacheSolve <- function(x, ...) {
|
14 | 41 | ## Return a matrix that is the inverse of 'x'
|
| 42 | + i <- x$getinverse() |
| 43 | + ## Check if the inverse is available in the cache matrix |
| 44 | + if(!is.null(i)) { |
| 45 | + message("returning cached inverse") |
| 46 | + return(i) |
| 47 | + } |
| 48 | + ## The inverse is not available, need to calculate it |
| 49 | + ## Get the raw matrix that is wrapped in the cache matrix |
| 50 | + data <- x$get() |
| 51 | + ## Calculate the inverse of the matrix |
| 52 | + i <- solve(data, ...) |
| 53 | + ## Set the inverse result on the cache matrix |
| 54 | + x$setinverse(i) |
| 55 | + i |
15 | 56 | }
|
0 commit comments