diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..b6d82b38586 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,36 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - +# makeCacheMatrix:This function creates a special "matrix" object +# that can cache its inverse makeCacheMatrix <- function(x = matrix()) { - + iv <- NULL + ## Set the value of the matrix + set <- function(y) { + x <<- y + iv <<- NULL + } + ## Get the value of the matrix + get <- function() x + ## Set the value of the inverse + setiv <- function(inverse) iv <<- inverse + ## Get the value of the inverse + getiv <- function() iv + list(set = set, get = get, setiv = setiv, getiv = getiv) } -## Write a short comment describing this function - +# cacheSolve: This function computes the inverse of the special "matrix" +# returned by makeCacheMatrix above cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + iv <- x$getiv() + ## Check if the inverse has already been calculated (and the matrix has not changed) + if (!is.null(iv)) { + message("getting cached data") + ### retrieve the inverse from the cache + return(iv) + } + ## Calculate the inverse + data <- x$get() + iv <- solve(data, ...) + # Cache and return the inverse + x$setiv(iv) + iv }