From fac1c72d59614f4bd5bfd5b03c43aad533e538ef Mon Sep 17 00:00:00 2001 From: "Craig L. Ching" Date: Mon, 21 Apr 2014 22:46:36 -0500 Subject: [PATCH 1/3] * Initial implementation of makeCacheMatrix and cacheSolve for R Programming programming assignment 2/Peer Assessment project --- cachematrix.R | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..59348922c45 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -5,6 +5,14 @@ makeCacheMatrix <- function(x = matrix()) { + m <- NULL + get <- function() x + setinverse <- function(inverse) m <<- inverse + getinverse <- function() m + + list(get = get, + setinverse = setinverse, + getinverse = getinverse) } @@ -12,4 +20,13 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + m <- x$getinverse() + if (!is.null(m)) { + message("Getting cached data") + return(m) + } + data <- x$get() + m <- solve(data) + x$setinverse(m) + m } From 65de45bb43679c5b44d3fffd75145290d51ff9a0 Mon Sep 17 00:00:00 2001 From: "Craig L. Ching" Date: Fri, 25 Apr 2014 20:49:12 -0500 Subject: [PATCH 2/3] * Comment the solution --- cachematrix.R | 80 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 23 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 59348922c45..9f94fb9d3fe 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,32 +1,66 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +# cachematrix.R implements a special type of data structure (implemented as a list) +# that caches the results of the solve() R function. Implemented as two +# functions, makeCacheMatrix() acts as a constructor for this data structure +# and cacheSolve() checks the data structure returning the cached +# solution if available and solving and caching if not. +# makeCacheMatrix() constructs a caching data structure for a matrix. Intended +# to be used with the cacheSolve() function defined below. +# +# Arguments: +# x -- a matrix that is assumed to be invertible +# +# Returns: +# A list that contains a placeholder for the cached value (initialized +# to NULL) and functions that can set the cached value and get the +# cached value. makeCacheMatrix <- function(x = matrix()) { - m <- NULL - get <- function() x - setinverse <- function(inverse) m <<- inverse - getinverse <- function() m + # Intialize the cached value + m <- NULL - list(get = get, - setinverse = setinverse, - getinverse = getinverse) -} + # A function to get the given matrix + get <- function() x + + # A function that caches the inverse matrix (result from solve()) + setinverse <- function(inverse) m <<- inverse + # A function to return the cached value + getinverse <- function() m -## Write a short comment describing this function + # Return our data structure as a list, binding the + # above functions to it + list(get = get, + setinverse = setinverse, + getinverse = getinverse) +} +# cacheSolve() computes and caches the inverse of the given +# matrix 'x'. If the cached value is available, that is +# returned otherwise solve() is called on the matrix, the +# return from resolve() cached and then returned. +# +# Arguments: +# x -- a data structure returned from makeCacheMatrix() +# +# Returns: +# The inversion of the matrix contained in 'x'. The +# matrix contained in 'x' is assumed to be invertible. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' - m <- x$getinverse() - if (!is.null(m)) { - message("Getting cached data") - return(m) - } - data <- x$get() - m <- solve(data) - x$setinverse(m) - m + + # Get the cached value and return it + # if it's been set (not NULL) + m <- x$getinverse() + if (!is.null(m)) { + message("Getting cached data") + return(m) + } + + # The result has not been cached, get + # the original matrix, call solve() on it + # cache the result and return the result + data <- x$get() + m <- solve(data) + x$setinverse(m) + m } From a2ddd75abbcb56c0ba227b50092718eedf43887d Mon Sep 17 00:00:00 2001 From: "Craig L. Ching" Date: Fri, 25 Apr 2014 21:09:25 -0500 Subject: [PATCH 3/3] * Add a set function on the data structure. --- cachematrix.R | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index 9f94fb9d3fe..3edd3f87776 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -19,6 +19,13 @@ makeCacheMatrix <- function(x = matrix()) { # Intialize the cached value m <- NULL + # A function to reset the data + # structure to a new matrix value + set <- function(y) { + x <<- y + m <<- NULL + } + # A function to get the given matrix get <- function() x @@ -30,7 +37,7 @@ makeCacheMatrix <- function(x = matrix()) { # Return our data structure as a list, binding the # above functions to it - list(get = get, + list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) }