diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..4a49c912439 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 +## Functions to solve for the inverse of a square matrix, and cache the result for reuse +## create a matrix object that can cache its inverse makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + ## set: store the given matrix along with variable m to store inverse + set <- function(y) { + x <<- y + m <<- NULL + } + ## get: just return matrix x + get <- function() x + ## setmatrix: cache inverse for later reuse + setmatrix <- function(solve) m <<- solve + ## getmatrix: return cached inverse of x + getmatrix <- function() m + ## return list of above functions for use by cacheSolve + list(set = set, get = get, + setmatrix = setmatrix, + getmatrix = getmatrix) } - -## Write a short comment describing this function - -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' +## calculate the inverse of a given matrix, using cached solution if available +cacheSolve <- function(x=matrix(), ...) { + ## get matrix along with cached inverse, if available + m <- x$getmatrix() + ## if cached inverse exists, return that + if(!is.null(m)) { + message("getting cached data") + return(m) + } + ## otherwise, get matrix data and calculate inverse + data <- x$get() + m <- solve(data, ...) + ## cache the inverse for later reuse + x$setmatrix(m) + ## return the matrix's inverse + m }