From 78b020d35d8921a5d9386b19bb33d4d21abf175d Mon Sep 17 00:00:00 2001 From: chriskopec Date: Sun, 22 Nov 2015 16:52:17 -0500 Subject: [PATCH] Implementation of inverse matrix caching --- cachematrix.R | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..7f99e2f48f0 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,48 @@ -## Put comments here that give an overall description of what your -## functions do +## Matrix inversion is usually a costly computation and there +## may be some benefit to caching the inverse of a matrix rather +## than compute it repeatedly. These functions compute and cache +## the matrix inverse. +## +## These functions assume that the matrix supplied is invertible. -## Write a short comment describing this function +## makeCacheMatrix: +## This function creates a special "matrix" with functions to +## get, set, getsolve, and setsolve. +## +## 'x' is a matrix that is wrapped with additional functionality. +## +## Return a "special" matrix that is able to cache its inverse. makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setsolve <- function(solve) m <<- solve + getsolve <- function() m + list(set = set, get = get, + setsolve = setsolve, + getsolve = getsolve) } -## Write a short comment describing this function - +## cacheSolve: +## This function computes the inverse of the special "matrix" +## returned by makeCacheMatrix above. +## +## 'x' is a special "matrix" created by calling makeCacheMatrix +## +## Return the inverse of the special "matrix" cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + m <- x$getsolve() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setsolve(m) + m }