From 7b1d4e4bafd1a7bcafc64e10c6dd5378b711f6ec Mon Sep 17 00:00:00 2001 From: Drew Sudell Date: Thu, 24 Jul 2014 00:54:07 -0400 Subject: [PATCH 1/7] Compute the inverse of a matrix while caching the results. Also add a simple script to exercise the code. --- cachematrix.R | 107 ++++++++++++++++++++++++++++++++++++++++++--- testcachedmatrix.R | 37 ++++++++++++++++ 2 files changed, 139 insertions(+), 5 deletions(-) create mode 100644 testcachedmatrix.R diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..1c8b2126bf2 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,112 @@ -## Put comments here that give an overall description of what your -## functions do +## R Programming Assignment 2 +## Cacheing the inverse of a matrix -## Write a short comment describing this function +## Create a functtion that wraps a in closure with +## a list of functions that mutate both the matrix +## and a cached inverse of the matrix makeCacheMatrix <- function(x = matrix()) { + # Wrap a matrix in order to cache the value of its inverse + # + # Args: + # x: the matrix to wrap, defaults to the empty matrix + # + # Returns: + # A list of accessor and mutator functions for the + # matrix and its inverse + # initialize the inverse as NULL, aka yet unknown + inv <- NULL + + set <- function(y) { + # (Re)set the matrix + # + # Args: + # y: the new matrix to hold + # Returns: + # NULL + + # Reset both the matrix x and its inverse inv + x <<- y + inv <<- NULL + } + + get <- function() { + # Get the current matrix + # + # Args: + # None + # + # Returns: + # the current matrix + x + } + + setinverse <- function(inverse) { + # Sets the inverse of the matrix + # + # Note this caches a value computed elsewhere + # + # Args: + # inverse: the inverse matrix of x + # + # Returns: + # the value of inverse argument + + # store away a copy of the inverse + inv <<- inverse + } + + getinverse <- function() { + # Get the cached inverse matrix + # + # Args: + # None + # + # Returns: + # The cached value of the inverse matrix + + # fetch the inverse + inv + } + + # return a list of the above functions + list(set = set, + get = get, + setinverse = setinverse, + getinverse = getinverse) } -## Write a short comment describing this function +# Solve for the inverse of a matrix, but +# using a makeCacheMatrix wrapper to cache the results cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + # Return a matrix that is the inverse of x + # + # Args: + # x: matrix to invert, wrapped in a makeCacheMatrix + # ...: Additional argument to pass to solve() + # + # Returns: + # invese matrix of x + + # Try just pulling out a cached result + inv <- x$getinverse() + # if we got it just return it + if (!is.null(inv)) { + message("getting cached data") + return(inv) + } + + # otherwise, really do the work + + # grab the actual matrix + m <- x$get() + # compute its inverse + inv <- solve(m, ...) + # cache the result + x$setinverse(inv) + # and return the value we computed + inv } diff --git a/testcachedmatrix.R b/testcachedmatrix.R new file mode 100644 index 00000000000..2ff2cde5f72 --- /dev/null +++ b/testcachedmatrix.R @@ -0,0 +1,37 @@ +# +# Exercise makeCachedMatrix and cacheSolve +# + +source("cachematrix.R") +m1 <- matrix(c(1,1,1,3,4,3,3,3,4), nrow=3, ncol=3) +m2 <- matrix(c(1,0,5,2,1,6,3,4,0), nrow=3, ncol=3) + +cm <- makeCacheMatrix(m1) +message("original m1 matrix") +m1 +message("wrapped matrix should be the same") +cm$get() + +message("inverse using solve") +solve(m1) +message("inverse using cacheMatrix should be the same") +cacheSolve(cm) +message("running cacheSolve again should yeid cached results") +cacheSolve(cm) + +message("reset matrix to m2") +cm$set(m2) + +message("original m2") +m2 +message("wrapped matrix should be the same") +cm$get() +message("and cached inverse should be null") +cm$getinverse() + +message("inverse using solve") +solve(m2) +message("inverse using cacheMatrix should be the same") +cacheSolve(cm) +message("running cacheSolve again should yeid cached results") +cacheSolve(cm) From 935b5e8fc6b657d0ee15d13087b3a246beb4d686 Mon Sep 17 00:00:00 2001 From: Drew Sudell Date: Thu, 24 Jul 2014 00:56:06 -0400 Subject: [PATCH 2/7] Rename the original readme, since it's not so much my readme, as the instructions for the problem. --- README.md => Instructions.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename README.md => Instructions.md (100%) diff --git a/README.md b/Instructions.md similarity index 100% rename from README.md rename to Instructions.md From a6b2f59df30c7d46b3b0e0e166152826bb21da7a Mon Sep 17 00:00:00 2001 From: Drew Sudell Date: Thu, 24 Jul 2014 01:04:23 -0400 Subject: [PATCH 3/7] Add my own readme. --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 00000000000..a62b3decb66 --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +### README + +This is my solution to Assignment 2, +caching the results of inverting a matrix, +for [R Programming](https://www.coursera.org/course/rprog). + +For the original README, see [Instructions](Instructions.md) + +### Manifest + + * README.md : this file + * Instructions.md : the original README, i.e. instructions + * cachematrix.R : my solution to the assignment + * testacachedmatrix.R : a simple test script for cachematrix.R From cfedb4bf3073e411f3d46e8cd3cd417194f19373 Mon Sep 17 00:00:00 2001 From: Drew Sudell Date: Thu, 24 Jul 2014 01:12:45 -0400 Subject: [PATCH 4/7] Add a link to a good sha/revision to the readme. That may make it easier for peer review. Add a gitignore while in the neighborhood. --- .gitignore | 1 + README.md | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..b25c15b81fa --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/README.md b/README.md index a62b3decb66..13f48a6ed2b 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,11 @@ For the original README, see [Instructions](Instructions.md) * Instructions.md : the original README, i.e. instructions * cachematrix.R : my solution to the assignment * testacachedmatrix.R : a simple test script for cachematrix.R + + +### Peer Assessment + +For Peer Assessment and Grading purposes, +you may look at a6b2f59df30c7d46b3b0e0e166152826bb21da7a +however, once I commit this change to the README, +it will no longer be the head. From b74e0e79f0dbcb6f9e1466de52c806f8a497988b Mon Sep 17 00:00:00 2001 From: Drew Sudell Date: Thu, 24 Jul 2014 01:15:44 -0400 Subject: [PATCH 5/7] Try to get the sha to be a link. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 13f48a6ed2b..6baac839f0a 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,6 @@ For the original README, see [Instructions](Instructions.md) ### Peer Assessment For Peer Assessment and Grading purposes, -you may look at a6b2f59df30c7d46b3b0e0e166152826bb21da7a +you may look at SHA: a6b2f59df30c7d46b3b0e0e166152826bb21da7a however, once I commit this change to the README, it will no longer be the head. From d65009b51c90273bf5f9776397c46ff962d4874d Mon Sep 17 00:00:00 2001 From: Drew Sudell Date: Thu, 24 Jul 2014 01:17:35 -0400 Subject: [PATCH 6/7] Try again to make sha a link --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6baac839f0a..0abb760f5ce 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,9 @@ For the original README, see [Instructions](Instructions.md) ### Peer Assessment For Peer Assessment and Grading purposes, -you may look at SHA: a6b2f59df30c7d46b3b0e0e166152826bb21da7a -however, once I commit this change to the README, +you may look at the following commit. +However, once I commit this change to the README, it will no longer be the head. + +* SHA: a6b2f59df30c7d46b3b0e0e166152826bb21da7a + From 7cafd3907012fe0092ef9ebd2b4cce8bd98fb39b Mon Sep 17 00:00:00 2001 From: Drew Sudell Date: Thu, 24 Jul 2014 01:25:11 -0400 Subject: [PATCH 7/7] Kill the attempt to link to the commit. That works in pull requsts, but apparently readme is plain-old, not git-flavored markdown. So it will probably confuses things. We'll just use the head. --- README.md | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/README.md b/README.md index 0abb760f5ce..9896f904550 100644 --- a/README.md +++ b/README.md @@ -13,13 +13,3 @@ For the original README, see [Instructions](Instructions.md) * cachematrix.R : my solution to the assignment * testacachedmatrix.R : a simple test script for cachematrix.R - -### Peer Assessment - -For Peer Assessment and Grading purposes, -you may look at the following commit. -However, once I commit this change to the README, -it will no longer be the head. - -* SHA: a6b2f59df30c7d46b3b0e0e166152826bb21da7a -