From 0dad0042eb0ab997b9a753cd88dcef26fb7ee95a Mon Sep 17 00:00:00 2001
From: Ed Rogers <f5d2h4eeyw@snkmail.com>
Date: Thu, 23 Apr 2015 16:06:06 -0500
Subject: [PATCH 1/2] Completed code for Programming Assignment 2 in
 cachematrix.R

---
 cachematrix.R | 71 ++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 65 insertions(+), 6 deletions(-)

diff --git a/cachematrix.R b/cachematrix.R
index a50be65aa44..c400197619c 100644
--- a/cachematrix.R
+++ b/cachematrix.R
@@ -1,15 +1,74 @@
-## Put comments here that give an overall description of what your
-## functions do
+## The two functions below create a cached version of the "solve" function
+## as it is used to invert a square matrix. The first function creates an
+## object to hold both the original matrix as well as any previously 
+## computed (cached) copies of the inverse of that matrix. The second
+## function checks for previously cached copies of the inverse before
+## calling the standard "solve" function.
 
-## Write a short comment describing this function
+## "makeMatrix" takes a square matrix as argument. It will return a list 
+## that includes the functions to access and change both the original 
+## matrix and its inverse once it is calculated.
 
-makeCacheMatrix <- function(x = matrix()) {
+makeMatrix <- function(x = matrix()) {
+  xInverse <- NULL 
 
+  # "set" and "get" are accessors to the matrix "x":
+  set <- function(y) {
+    # In order to affect variables at the parent level, 
+    # the "superassignment" operator (<<-) must be used for "set"
+    x <<- y
+    xInverse <<- NULL
+  }
+  get <- function() x
+  
+  # "setInverse" and "getInverse" access the matrix "xInverse":
+  # Again, "superassignment" operator is necessary to affect parent scope
+  setInverse <- function(matrixInverse) xInverse <<- matrixInverse
+  getInverse <- function() xInverse
+
+  # A list of the four accessor functions is returned
+  list(set = set, get = get,
+       setInverse = setInverse,
+       getInverse = getInverse)
 }
 
 
-## Write a short comment describing this function
+## "cacheSolve" takes the result of "makeMatrix", a list of functions, 
+## and returns the inverse of the matrix to which those functions have
+## access. If the matrix inverse has been computed already, it will use
+## that value. Otherwise, it will compute it using "solve", and use the
+## "setInverse" accessor function to cache the result.
 
 cacheSolve <- function(x, ...) {
-        ## Return a matrix that is the inverse of 'x'
+  ## First, check to see if the inverse is cached already
+  xInverse <- x$getInverse()
+  if(!is.null(xInverse)) {
+    message("getting cached data")
+    return(xInverse)
+  }
+  
+  ## If the inverse was not yet computed, access the original matrix:
+  data <- x$get()
+
+  ## compute its inverse:
+  xInverse <- solve(data, ...)
+
+  ## cache the inverse:
+  x$setInverse(xInverse)
+  
+  ## and last, return the inverse:
+  xInverse
 }
+
+## code can be tested by sourcing and comparing to "solve":
+##
+## ## sourcing may vary based on architecture:
+## source('~/R/ProgrammingAssignment2/cachematrix.R') 
+## randomMatrix <- matrix(rnorm(1000000),nrow=1000,ncol=1000)
+## inverseRandomMatrix <- solve(randomMatrix)
+## cachedRandomMatrix <- makeMatrix(randomMatrix)
+## identical(inverseRandomMatrix,cacheSolve(cachedRandomMatrix))
+## identical(inverseRandomMatrix,cacheSolve(cachedRandomMatrix))
+## ## Both "identical" commands should return TRUE, 
+## ## second run should return much faster 
+## ## (and with printed message, "getting cached data")
\ No newline at end of file

From dd91b4876c8d6db6e6697682d2ac8e9bc89c88a4 Mon Sep 17 00:00:00 2001
From: Ed Rogers <f5d2h4eeyw@snkmail.com>
Date: Thu, 23 Apr 2015 16:12:27 -0500
Subject: [PATCH 2/2] Fixed a small typo. Returning name of one function to
 original makeCacheMatrix

---
 cachematrix.R | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/cachematrix.R b/cachematrix.R
index c400197619c..ded4cc07499 100644
--- a/cachematrix.R
+++ b/cachematrix.R
@@ -5,11 +5,11 @@
 ## function checks for previously cached copies of the inverse before
 ## calling the standard "solve" function.
 
-## "makeMatrix" takes a square matrix as argument. It will return a list 
-## that includes the functions to access and change both the original 
+## "makeCacheMatrix" takes a square matrix as argument. It will return a
+## list that includes the functions to access and change both the original 
 ## matrix and its inverse once it is calculated.
 
-makeMatrix <- function(x = matrix()) {
+makeCacheMatrix <- function(x = matrix()) {
   xInverse <- NULL 
 
   # "set" and "get" are accessors to the matrix "x":
@@ -33,7 +33,7 @@ makeMatrix <- function(x = matrix()) {
 }
 
 
-## "cacheSolve" takes the result of "makeMatrix", a list of functions, 
+## "cacheSolve" takes the result of "makeCacheMatrix", a list of functions, 
 ## and returns the inverse of the matrix to which those functions have
 ## access. If the matrix inverse has been computed already, it will use
 ## that value. Otherwise, it will compute it using "solve", and use the
@@ -66,7 +66,7 @@ cacheSolve <- function(x, ...) {
 ## source('~/R/ProgrammingAssignment2/cachematrix.R') 
 ## randomMatrix <- matrix(rnorm(1000000),nrow=1000,ncol=1000)
 ## inverseRandomMatrix <- solve(randomMatrix)
-## cachedRandomMatrix <- makeMatrix(randomMatrix)
+## cachedRandomMatrix <- makeCacheMatrix(randomMatrix)
 ## identical(inverseRandomMatrix,cacheSolve(cachedRandomMatrix))
 ## identical(inverseRandomMatrix,cacheSolve(cachedRandomMatrix))
 ## ## Both "identical" commands should return TRUE, 
