diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..57e1ac2e102 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,32 @@ -## Put comments here that give an overall description of what your -## functions do +## Given an invertible matrix compute the inverse and cache the result. Subsequent requests to get the inverse +## of the same matrix will return the cache result instead of re-calculating. -## Write a short comment describing this function +## This function creates a special "matrix" object that can cache its inverse. makeCacheMatrix <- function(x = matrix()) { - + i <- NULL + set <- function(y) { + x <<- y + i <<- NULL + } + get <- function() x + setinv <- function(inv) i <<- inv + getinv <- function() i + list(set = set, get = get, setinv = setinv, getinv = getinv) } -## Write a short comment describing this function +## This function computes and caches the inverse of the special "matrix" returned by makeCacheMatrix above. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + i <- x$getinv() + if(!is.null(i)) { + message("getting cached data") + return(i) + } + data <- x$get() + i <- solve(data, ...) + x$setinv(i) + i }