diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..396d45ceef6 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,39 @@ -## Put comments here that give an overall description of what your -## functions do +## makeCacheMatrix takes an matrix and creates an object from which you +## can get the matrix back but also the inverse of the matrix if it has +## been computed and then by calling the cacheSolve you can compute the +## inverse of the matrix or take it from the cache if it was already +## computed -## Write a short comment describing this function +## This function creates the special caching object makeCacheMatrix <- function(x = matrix()) { - + inv <- NULL + setData <- function (y) + { + x <<- y + inv <<- NULL + } + getData <- function () x + setInv <- function (invMat) inv <<- invMat + getInv <- function () inv + list (setData = setData, getData = getData, setInv = setInv, getInv = getInv) } -## Write a short comment describing this function +## This function will get you the inverse of a matrix but if that +## was already computed then the computation is skipped and you get +## the cached result cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + inv <- x$getInv() + if (!is.null(inv)) + { + message("getting cached inverse") + return(inv) + } + # if nothing in cache then compute + data <- x$getData() + inv <- solve(data) + x$setInv(inv) + x$getInv() }