From 2f2cc77829f2e6ade7abf4db688f87aed611bcfb Mon Sep 17 00:00:00 2001
From: King Chung Huang <kinghuang@mac.com>
Date: Sat, 21 Nov 2015 14:44:20 -0700
Subject: [PATCH 1/5] Implement makeCacheMatrix

makeCacheMatrix takes in a matrix x and returns a list containing
functions for getting and setting the matrix and its inverse.
---
 cachematrix.R | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/cachematrix.R b/cachematrix.R
index a50be65aa44..f58637533e6 100644
--- a/cachematrix.R
+++ b/cachematrix.R
@@ -1,10 +1,19 @@
 ## Put comments here that give an overall description of what your
 ## functions do
 
-## Write a short comment describing this function
+## makeCacheMatrix takes in a matrix x and returns a list containing
+## functions for getting and setting the matrix and its inverse.
 
 makeCacheMatrix <- function(x = matrix()) {
-
+	inv <- NULL
+	set <- function(y) {
+		x <<- y
+		inv <<- NULL
+	}
+	get <- function() x
+	setinv <- function(minv) inv <<- minv
+	getinv <- function() inv
+	list(set = set, get = get, setinv = setinv, getinv = getinv)
 }
 
 

From 8337a63ae64961ac3e1bd879814832be5533edd1 Mon Sep 17 00:00:00 2001
From: King Chung Huang <kinghuang@mac.com>
Date: Sat, 21 Nov 2015 14:48:41 -0700
Subject: [PATCH 2/5] Implement cacheSolve

cacheSolve takes in a cache matrix x and returns the matrix's inverse.
x is a list created by the makeCacheMatrix function. If x has a
previously calculated inverse, it is used as the return value.
Otherwise, the inverse is calculated, stored in x, and returned.
---
 cachematrix.R | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/cachematrix.R b/cachematrix.R
index f58637533e6..73bc601470f 100644
--- a/cachematrix.R
+++ b/cachematrix.R
@@ -17,8 +17,20 @@ makeCacheMatrix <- function(x = matrix()) {
 }
 
 
-## Write a short comment describing this function
+## cacheSolve takes in a cache matrix x and returns the matrix's
+## inverse. x is a list created by the makeCacheMatrix function.
+## If x has a previously calculated inverse, it is used as the
+## return value. Otherwise, the inverse is calculated, stored
+## in x, and returned.
 
 cacheSolve <- function(x, ...) {
-        ## Return a matrix that is the inverse of 'x'
+	inv <- x$getinv()
+	if (!is.null(inv)) {
+		message("getting cached data")
+		return(inv)
+	}
+	data <- x$get()
+	inv <- solve(data, ...)
+	x$setinv(inv)
+	inv
 }

From f103b1823039d30d82f444801123dcb453806398 Mon Sep 17 00:00:00 2001
From: King Chung Huang <kinghuang@mac.com>
Date: Sat, 21 Nov 2015 14:53:10 -0700
Subject: [PATCH 3/5] Describe cachematrix

cachematrix contains two utility functions for solving the inverse of
matrices and stored the results for reuse.
---
 cachematrix.R | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/cachematrix.R b/cachematrix.R
index 73bc601470f..7e97fd703f0 100644
--- a/cachematrix.R
+++ b/cachematrix.R
@@ -1,5 +1,5 @@
-## Put comments here that give an overall description of what your
-## functions do
+## cachematrix contains two utility functions for solving the inverse
+## of matrices and stored the results for reuse.
 
 ## makeCacheMatrix takes in a matrix x and returns a list containing
 ## functions for getting and setting the matrix and its inverse.

From 78c0ad7554d483c188d83c85157b8b8f1953b58f Mon Sep 17 00:00:00 2001
From: King Chung Huang <kinghuang@mac.com>
Date: Sat, 21 Nov 2015 15:00:19 -0700
Subject: [PATCH 4/5] Add brief comments in makeCacheMatrix

Briefly describe the implementation of makeCacheMatrix. It is a simple
object (list) containing get/set functions for a matrix and its inverse.
---
 cachematrix.R | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/cachematrix.R b/cachematrix.R
index 7e97fd703f0..7173e452ff4 100644
--- a/cachematrix.R
+++ b/cachematrix.R
@@ -5,14 +5,27 @@
 ## functions for getting and setting the matrix and its inverse.
 
 makeCacheMatrix <- function(x = matrix()) {
+	# Initialize the inverse of x to NULL.
 	inv <- NULL
+	
+	# get and set functions for the matrix x.
+	# The set function also sets the inverse of x
+	# to NULL, to invalidate any previously cached
+	# inverse.
 	set <- function(y) {
 		x <<- y
 		inv <<- NULL
 	}
 	get <- function() x
+	
+	# get and set functions for the inverse of
+	# the matrix x.
 	setinv <- function(minv) inv <<- minv
 	getinv <- function() inv
+	
+	# Return a simple object (list) containing the
+	# get and set functions for a matrix and its
+	# inverse.
 	list(set = set, get = get, setinv = setinv, getinv = getinv)
 }
 

From ce9a3c2db35666d9ee02fa048bb3a8a552093803 Mon Sep 17 00:00:00 2001
From: King Chung Huang <kinghuang@mac.com>
Date: Sat, 21 Nov 2015 15:03:26 -0700
Subject: [PATCH 5/5] Add brief comments in cacheSolve

Briefly describe the implementation of cacheSolve. It attempts to
return the cached inverse of the matrix in x, and otherwise solves the
inverse and storing it before returning the result to the caller.
---
 cachematrix.R | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/cachematrix.R b/cachematrix.R
index 7173e452ff4..b2fec728867 100644
--- a/cachematrix.R
+++ b/cachematrix.R
@@ -37,11 +37,20 @@ makeCacheMatrix <- function(x = matrix()) {
 ## in x, and returned.
 
 cacheSolve <- function(x, ...) {
+	# Get the cached inverse from x. If the
+	# returned value is not null, return
+	# that as the inverse of the matrix
+	# stored in x.
 	inv <- x$getinv()
 	if (!is.null(inv)) {
 		message("getting cached data")
 		return(inv)
 	}
+	
+	# Otherwise, get the matrix in x, and
+	# solve for its inverse. The result is
+	# cached in x for subsequent to cacheSolve,
+	# for this matrix, and returned.
 	data <- x$get()
 	inv <- solve(data, ...)
 	x$setinv(inv)
