diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..c7c267557c7 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,49 @@ -## Put comments here that give an overall description of what your -## functions do +## These two functions allow you to create a special object which we can use to +## cache an inverse matrix. MakeCache creates the object. Solvecache will check the +## object to see if it's already been solved and saved. If so, then it retrives the value +## if not, it solves the inverse and stores it for future. -## Write a short comment describing this function -makeCacheMatrix <- function(x = matrix()) { +## This function creates a list where we can store the inverse of the matrix +makeCacheMatrix <- function(x = matrix()) { + m <- NULL + + set <- function (y) { + x <<- y + m <<- NULL + } + + get <- function () x + + setInverse <- function (solve) m <<- solve + + getInverse <- function () m + + list (set=set, + get=get, + setInverse=setInverse, + getInverse=getInverse) } -## Write a short comment describing this function + +## This function checks to see if we've already solved the inversion and retreives it if we have cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + m <- x$getInverse() + + if (!is.null (m)) { + message ("getting cached inverse matrix") + return (m) + } + + data <- x$get() + m <- solve (data, ...) + x$setInverse (m) + m } + + +