diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..e230ae95d61 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,39 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - makeCacheMatrix <- function(x = matrix()) { - + inv <- NULL # this will store the inverse later + + # 1. set() - set a new matrix and reset cached inverse + set <- function(y) { + x <<- y + inv <<- NULL + } + + # 2. get() - get the current matrix + get <- function() x + + # 3. setinverse() - store (cache) the inverse + setinverse <- function(inverse) inv <<- inverse + + # 4. getinverse() - get the cached inverse + getinverse <- function() inv + + # return a list of all 4 functions + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } - -## Write a short comment describing this function - cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + inv <- x$getinverse() # check if inverse already cached + + if (!is.null(inv)) { # if yes, just return it + message("getting cached data") + return(inv) + } + + # if not cached, calculate it + data <- x$get() + inv <- solve(data, ...) # compute inverse + x$setinverse(inv) # store inverse in cache + + inv # return the result }