From 8ca4a15c2d0296e25234c03bdc842a03875ea568 Mon Sep 17 00:00:00 2001 From: bjegithub Date: Tue, 22 Apr 2014 19:49:19 -0400 Subject: [PATCH 1/2] First attempt at functions --- cachematrix.R | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..ed3e818c439 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,12 +4,31 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + inv <- NULL + set <- function(y) { + x <<- y + inv <<- NULL + } + get <- function() x + setinverse <- function(solve) inv <<- solve + getinverse <- function() inv + list(set = set, get = get, setinverse = setinverse, + getinverse = getinverse ) } ## Write a short comment describing this function cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + + inv <- x$getinverse() + if(!is.null(inv)) { + message("getting cached data") + return(inv) + } + data <- x$get() + inv <- solve(data, ...) + x$setinv(inv) + inv } From aad5ec97adc8750c40b6259edb4edb0e0b46644d Mon Sep 17 00:00:00 2001 From: bjegithub Date: Tue, 22 Apr 2014 20:38:05 -0400 Subject: [PATCH 2/2] Final coding changes & added comments --- cachematrix.R | 60 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index ed3e818c439..2f44540df80 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,34 +1,70 @@ -## Put comments here that give an overall description of what your -## functions do +## Brian J. Engelhardt +## R Programming Assignment #2 +## 04/22/2014 +## +## This file contains two functions that allow the user to construct a "special" matrix that can +## be solved using the cacheSolve() function, which will cache the time consuming inverse +## calculations, so repeated calls to cacheSolve (for the same matrix data) will not result in +## re-calculating the inverse of the matrix. -## Write a short comment describing this function + +## makeCacheMatrix +## Creates a special "matrix", which is really a list of 4 functions allowing access to the data +## +## Args: +## x : a matrix +## +## Returns: +## A list of 4 functions that allow you to get/set the matrix, and get/set the invere of the matrix makeCacheMatrix <- function(x = matrix()) { inv <- NULL + + ## set the matrix data, and set the inverse to NULL, since the matrix data has changed set <- function(y) { x <<- y inv <<- NULL } + get <- function() x - setinverse <- function(solve) inv <<- solve - getinverse <- function() inv - list(set = set, get = get, setinverse = setinverse, - getinverse = getinverse ) + + setInverse <- function(inverse) inv <<- inverse + + getInverse <- function() inv + + list(set = set, get = get, setInverse = setInverse, + getInverse = getInverse ) } -## Write a short comment describing this function +## cacheSolve +## Calculate the inverse of a matrix, given a "special" matrix. If the inverse has already been +## calculated, then the inverse is just retrieved from memory +## +## Args: +## x : a special matrix created from "makeCacheMatrix" +## +## Returns: +## The inverse of the matrix contained in the special matrix x. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' - inv <- x$getinverse() + inv <- x$getInverse() + if(!is.null(inv)) { - message("getting cached data") + ## The inverse has already been calculated, just return it + message("Retrieving cached inverse") return(inv) } + + ## in this case, the inverse has not been calculated, so retrieve the original + ## matrix data from x, and then solve the inverse data <- x$get() inv <- solve(data, ...) - x$setinv(inv) + + ## store the inverse back in the "special" matrix x, so that if we need it again + ## it doesn't have to be solved again + x$setInverse(inv) + inv }