From 8ca98ace0a036a395d59225d2e4c9a25539b81ac Mon Sep 17 00:00:00 2001 From: Christopher Roach Date: Mon, 19 Jan 2015 15:03:39 -0800 Subject: [PATCH 1/3] Making git repo an RStudio project --- .gitignore | 3 +++ ProgrammingAssignment2.Rproj | 13 +++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 .gitignore create mode 100644 ProgrammingAssignment2.Rproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..807ea251739 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.Rproj.user +.Rhistory +.RData diff --git a/ProgrammingAssignment2.Rproj b/ProgrammingAssignment2.Rproj new file mode 100644 index 00000000000..8e3c2ebc99e --- /dev/null +++ b/ProgrammingAssignment2.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX From 1eb4341b4f0b3d2b4d88916e742c063f0d1738fc Mon Sep 17 00:00:00 2001 From: Christopher Roach Date: Mon, 19 Jan 2015 15:04:03 -0800 Subject: [PATCH 2/3] Changes for Assignment 2 --- cachematrix.R | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..93211575dcd 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,12 +4,31 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + i <- NULL + + set <- function(y) { + message("clearing the cached inverse") + i <<- NULL + x <<- y + } + get <- function() x + setinverse <- function(inverse) i <<- inverse + getinverse <- function() i + 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' + i <- x$getinverse() + if (!is.null(i)) { + message("getting cached data") + return(i) + } + data <- x$get() + i <- solve(data, ...) + x$setinverse(i) + i } From 4facc0fd3fdc14de38ef07904e4d830303e402d4 Mon Sep 17 00:00:00 2001 From: Christopher Roach Date: Mon, 19 Jan 2015 15:18:18 -0800 Subject: [PATCH 3/3] Added some comments explaining the code --- cachematrix.R | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 93211575dcd..c4cf826b08a 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,12 +1,16 @@ -## Put comments here that give an overall description of what your -## functions do +## The makeCacheMatrix and cacheSolve functions work together to create a +## special matrix wrapper object that can cache its own inverse value. -## Write a short comment describing this function +## The makeCacheMatrix function creates a special object that wraps a matrix +## object and provides accessor functions to the matrix object and the cached +## inverse of the matrix. makeCacheMatrix <- function(x = matrix()) { i <- NULL set <- function(y) { + # Clear the cached inverse whenever we update the matrix object to make + # sure we don't return an incorrect inverse. message("clearing the cached inverse") i <<- NULL x <<- y @@ -18,7 +22,9 @@ makeCacheMatrix <- function(x = matrix()) { } -## Write a short comment describing this function +## The cacheSolve function calculates the inverse of the given matrix and +## caches it on that object. If the inverse of the give matrix is already +## cached it returns instead of calculating it anew. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x'