|
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 below create a special "matrix" object that can cache its inverse. The first function, makeCacheMatrix, creates a special "matrix" object/list |
| 2 | +## that can be used to cache the inverse of the matrix. In addition, it defines several helper functions that can be used to query/access the input matrix and/or |
| 3 | +## the inverse of the input matrix. The second function, cacheSolve, checks within the special "matrix" object to see whether the inverse of the input matrix |
| 4 | +## stored exists. If the inverse of the input matrix exists, the function returns the value stored in the special "matrix" object; if the inverse of the input |
| 5 | +## matrix does not exist, the function calculates the inverse of the matrix and stores/caches its value in the special "matrix" object, and returns the value of |
| 6 | +## the inverse of the input matrix. |
5 | 7 |
|
6 | 8 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 9 | + ## This function takes a matrix and creates four functions, a) set, b) get, c) setinv, and d) getinv which can be used to a) store the input matrix or |
| 10 | + ## modify/replace the matrix, b) retrieve the input matrix, c) calculate and store the inverse of the input matrix, and d) retrieve the inverse of the |
| 11 | + ## input matrix. |
| 12 | + |
| 13 | + m <- NULL |
| 14 | + set <- function(y) { # This function stores the input matrix as "x" or allows one to reset/update the input matrix as specified |
| 15 | + x <<- y |
| 16 | + m <<- NULL |
| 17 | + } |
| 18 | + get <- function() x # This function retrieves "x" |
| 19 | + setinv <- function(ginv) m <<- ginv # This function stores the inverse of "x" as "m" or allows one to reset/update the inverse of "x" as specified |
| 20 | + getinv <- function() m # This function retrieves "m" |
| 21 | + list(set = set, get = get, setinv = setinv, getinv = getinv) # Creates a list of the functions created by the function "makeCacheMatrix" |
8 | 22 | }
|
9 | 23 |
|
10 |
| - |
11 |
| -## Write a short comment describing this function |
12 |
| - |
13 | 24 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 25 | + ## This function's input is a makeCacheMatrix list. The function will calculate the Moore-Penrose generalized inverse of the input matrix stored in the |
| 26 | + ## makeCacheMatrix list. If the inverse of the matrix already exists within the makeCacheMatrix list, this function will simply return that value. |
| 27 | + |
| 28 | + m <- x$getinv() # Checks the makeCacheMatrix list to see if the inverse of the input matrix has already been calculated |
| 29 | + if(!is.null(m)) { # This function checks if the inverse has already been calculated (i.e., is not NULL), it returns the stored value and exits the function |
| 30 | + message("getting cached inverse") |
| 31 | + return(m) |
| 32 | + } |
| 33 | + data <- x$get() # This function extracts the input matrix from the makeCacheMatrix list |
| 34 | + require(MASS) # Loads the package "MASS" where the function "ginv" exists |
| 35 | + m <- ginv(data, ...) # ginv is a function that calculates the Moore-Penrose generalized inverse of a matrix |
| 36 | + x$setinv(m) # Stores the inverse of the input matrix within the makeCacheMatrix list |
| 37 | + m # Returns the value of the inverse of the input matrix |
15 | 38 | }
|
0 commit comments