diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..27f85f623eb 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,41 @@ -## Put comments here that give an overall description of what your -## functions do +## These functions will compute the inverse of a given matrix +## This is assuming the matrix is inversible +## Once the inverse has been calculated it is stored in a cache +## If the inverse is requested again, the cached value is returned -## Write a short comment describing this function -makeCacheMatrix <- function(x = matrix()) { +## This function will create a special list containing functions +## These functions can be used to set and get the matrix used +## or to set and get the inverse of the matrix + +makeCacheMatrix <- function(x = matrix()) { + i <- NULL + set <- function(y) { + x <<- y + i <<- NULL + } + get <- function() x + setinverse <- function(inverse) i <<- inverse + getinverse <- function() i + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } -## Write a short comment describing this function +## This function uses the special list created by 'makeCacheMatrix' +## It will check for a cached value of the inverse matrix and return it if found +## If no cached value is found, calculate, set and return the inverse matrix cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + i <- x$getinverse() + if(!is.null(i)) { + message("getting cached data") + return(i) + } + data <- x$get() + i <- solve(data, ...) + x$setinverse(i) + i }