diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..f7eddb0ddcb 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,43 @@ -## Put comments here that give an overall description of what your -## functions do -## Write a short comment describing this function +## Author: Devon Smith +## Date Created: 2014-11-23 -makeCacheMatrix <- function(x = matrix()) { +## This is a pair of functions -- "makeCacheMatrix" and "cacheSolve" -- which +## are used to create a matrix 'object' that can calculate and cache its +## inverse. The object returned by "makeCacheMatrix" can be passed to +## "cacheSolve" which will either calculate and cache the inverse, or simply +## return a previously cached inverse. The cached inverse is erased when a new +## value is supplied with the "set" function, so that a new inverse will be +## calculated the next time it's called. +## Input: A matrix +## Output: A list/object that stores the input matrix and its inverse +## Description: Creates a list with functions to set and get a matrix and its inverse +makeCacheMatrix <- function(x = matrix()) { + i <- NULL + set <- function(y) { + x <<- y + i <<- NULL + } + get <- function() x + setinverse <- function(inverse) i <<- inverse + getinverse <- function() i + list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) } - -## Write a short comment describing this function - +## Input: A matrix created by "makeCacheMatrix" +## Output: The inverse of the the input matrix +## Description: If matrix inverse has been cached, the cached version will be returned, +## otherwise the inverse will be calculated, cached in the input object, then returned cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + i <- x$getinverse() + if(!is.null(i)) { + message("getting cached data") + return(i) + } + data <- x$get() + i <- solve(data, ...) + x$setinverse(i) + i }