|
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 two functions here work together to allow cached values for the inversion |
| 2 | +# of a Matrix. The Matrix is created with makeCacheMatrix() which returns a List |
| 3 | +# of functions to use when manipulating the Matrix. The cacheSolve(matrix) |
| 4 | +# function will return the cached inversion or solve a new inversion if required. |
5 | 5 |
|
| 6 | +# The makeCacheMatrix() function creates a special matrix which is really a |
| 7 | +# list containing functions to: |
| 8 | +# * get the value of the matrix |
| 9 | +# * set the value of the matrix |
| 10 | +# * get the value of the solved matrix |
| 11 | +# * set the value of the solved matrix |
| 12 | +# |
| 13 | +# Returns: |
| 14 | +# A List of functions used to manipulate the matrix |
| 15 | +# |
| 16 | +# Sample Use: |
| 17 | +# x <- makeCacheMatrix() |
| 18 | +# x$set(matrix(c(1,2,11 ,12), ncol=2, nrow=2, byrow=TRUE)) sets the matrix. |
| 19 | +# x$get() returns the matrix data |
| 20 | +# x$getsolve() returns the inverted matrix |
| 21 | +# x$setsolve([inverted matrix]) sets, and caches, the inverted matrix. |
6 | 22 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 23 | + # Intialize the solved matrix inversion variable |
| 24 | + s <- NULL |
| 25 | + # define the set() function to set the matrix value and re-set the inversion |
| 26 | + set <- function(y) { |
| 27 | + x <<- y |
| 28 | + s <<- NULL |
| 29 | + } |
| 30 | + # define the get() function to return the matrix |
| 31 | + get <- function() x |
| 32 | + # define the setsolve() function to set the solved matrix value |
| 33 | + setsolve <- function(slv) s <<- slv |
| 34 | + # define the getsolve() function to get the solved matrix value |
| 35 | + getsolve <- function() s |
| 36 | + # return a List of functions to use when manipulating the matrix |
| 37 | + list(set = set, get = get, |
| 38 | + setsolve = setsolve, |
| 39 | + getsolve = getsolve) |
8 | 40 | }
|
9 | 41 |
|
10 |
| - |
11 |
| -## Write a short comment describing this function |
12 |
| - |
| 42 | +# cacheSolve() inverts the matrix created with makeCacheMatrix(). |
| 43 | +# It will first check to see if the inverted matrix has already been |
| 44 | +# solved. If so, it returns the previously inverted matrix. Otherwise, |
| 45 | +# it solves the inversion and saves it. |
| 46 | +# |
| 47 | +# Returns: |
| 48 | +# An inverted matrix. |
| 49 | +# |
| 50 | +# Sample Use (x is created with the above makeCacheMatrix()): |
| 51 | +# inversion <- cacheSolve(x) will return the cached inversion or solve new. |
13 | 52 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 53 | + # Get the previously solved value |
| 54 | + s <- x$getsolve() |
| 55 | + # return the solved value if we have one. Give a message too. |
| 56 | + if(!is.null(s)) { |
| 57 | + message("getting cached data") |
| 58 | + return(s) |
| 59 | + } |
| 60 | + # We need to solve the matrix inversion. So get the data.. |
| 61 | + data <- x$get() |
| 62 | + # Solve it.. |
| 63 | + s <- solve(data, ...) |
| 64 | + # and set the value |
| 65 | + x$setsolve(s) |
| 66 | + # Return a matrix that is the inverse of 'x' |
| 67 | + s |
15 | 68 | }
|
0 commit comments