|
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 | +## The functions in this R source file make it possible to create a |
| 2 | +## special matrix that can cache its inverse, calculating it lazily |
| 3 | +## and only once |
5 | 4 |
|
6 | 5 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 6 | + ## Makes a matrix that can cache its inverse. |
| 7 | + ## |
| 8 | + ## This function creates a special matrix object |
| 9 | + ## that can cache its inverse. |
| 10 | + ## Re-setting the matrix data also resets the inverse, |
| 11 | + ## making it necessary to recalculate it. |
| 12 | + ## |
| 13 | + ## Args: |
| 14 | + ## x: The initial value for the matrix. Default is an |
| 15 | + ## empty matrix. |
| 16 | + |
| 17 | + inverse <- NULL |
| 18 | + |
| 19 | + set <- function(y) { |
| 20 | + ## Sets a new value for this matrix. |
| 21 | + ## |
| 22 | + ## Sets a new value for this matrix, |
| 23 | + ## and resets the cached inverse, if it was calculated. |
| 24 | + ## |
| 25 | + ## Args: |
| 26 | + ## y: The new value for this matrix. |
| 27 | + x <<- y |
| 28 | + inverse <<- NULL |
| 29 | + } |
| 30 | + |
| 31 | + get <- function() { |
| 32 | + ## Gets the actual matrix value. |
| 33 | + x |
| 34 | + } |
| 35 | + |
| 36 | + setinverse <- function(newinverse) { |
| 37 | + ## Sets the inverse for this matrix |
| 38 | + ## |
| 39 | + ## Args: |
| 40 | + ## newinverse: The new value for the |
| 41 | + ## inverse of this matrix. |
| 42 | + inverse <<- newinverse |
| 43 | + } |
| 44 | + |
| 45 | + getinverse <- function() { |
| 46 | + ## Gets the inverse for this matrix |
| 47 | + inverse |
| 48 | + } |
| 49 | + |
| 50 | + # Returns a list with the four functions defined |
| 51 | + # for this special matrix |
| 52 | + list(set = set, get = get, |
| 53 | + setinverse = setinverse, |
| 54 | + getinverse = getinverse) |
8 | 55 | }
|
9 | 56 |
|
10 | 57 |
|
11 |
| -## Write a short comment describing this function |
12 |
| - |
13 | 58 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 59 | + ## Calculates the inverse for the special matrix x, if needed. |
| 60 | + ## |
| 61 | + ## This function computes the inverse of the special matrix |
| 62 | + ## returned by the function makeCacheMatrix. |
| 63 | + ## If the inverse has already been calculated |
| 64 | + ## (and the matrix has not changed), then cachesolve retrieves |
| 65 | + ## the inverse from the cache. |
| 66 | + ## |
| 67 | + ## Args: |
| 68 | + ## x: The special matrix object. |
| 69 | + ## ...: Other arguments that will be passed to |
| 70 | + ## the solve function. |
| 71 | + |
| 72 | + inverse <- x$getinverse() |
| 73 | + |
| 74 | + # Since we have a cached inverse, we'll just return it |
| 75 | + if(!is.null(inverse)) { |
| 76 | + message("getting cached data") |
| 77 | + return(inverse) |
| 78 | + } |
| 79 | + |
| 80 | + # There is no cached inverse, so we calculate and set it. |
| 81 | + data <- x$get() |
| 82 | + inverse <- solve(data, ...) |
| 83 | + x$setinverse(inverse) |
| 84 | + |
| 85 | + # Returning the newly calculated inverse |
| 86 | + inverse |
15 | 87 | }
|
0 commit comments