From 7443ea5afef2dc2aaaed3965202e77e84f6ac80c Mon Sep 17 00:00:00 2001 From: Jeff Slane Date: Mon, 1 Jan 2018 19:46:26 -0800 Subject: [PATCH] Added functionality to the methods to calculate, cache, and retrieve matrix inverse --- cachematrix.R | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..59c84c993fa 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,33 @@ -## Put comments here that give an overall description of what your -## functions do +## small library of functions to handle caching of matrix computations -## Write a short comment describing this function +## Makes a matrix that will cache its inverse to save computation time 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 +## If inverse is cached, return it... otherwise calculate it, cache it, and then return it cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + m <- x$getinverse() + if(!is.null(m)) { + message("getting cached inverse") + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setinverse(m) + m }