From 41e85801781770e5fedbdc81cbf1e1ae45d60fa5 Mon Sep 17 00:00:00 2001 From: Jonathan Taylor Date: Sun, 25 Jan 2015 14:29:54 +0000 Subject: [PATCH 1/3] completed assignment --- cachematrix.R | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..e9fa9b644b9 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -3,8 +3,21 @@ ## Write a short comment describing this function -makeCacheMatrix <- function(x = matrix()) { - +makeCacheMatrix <- function(our_matrix = matrix()) { + computed_inverse <- NULL + + set <- function(new_matrix) { + our_matrix <<- new_matrix #Update our matrix to the new one + computer_inverse <<- NULL #Clear the cached value as the matrix has changed (so we recompute it) + } + + get <- function() our_matrix + + setInverse <- function(inverted_matrix) computed_inverse <<- inverted_matrix + + getInverse <- function() computed_inverse + + list(set = set, get = get, setInverse = setInverse, getInverse = getInverse) } @@ -12,4 +25,14 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + inverse <- x$getInverse() + if(!is.null(inverse)) { + message("getting cached data") + return(inverse) + } + + data <- x$get() + inverse <- solve(data) #Not using ... here we need to force defaults otherwise we can get different answers for the same input matrix! + x$setInverse(inverse) + inverse } From ae86c37afb1e0006c08489d960a9bfb4fac68be3 Mon Sep 17 00:00:00 2001 From: Jonathan Taylor Date: Sun, 25 Jan 2015 14:33:06 +0000 Subject: [PATCH 2/3] Adding additional comments --- cachematrix.R | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index e9fa9b644b9..da79b70268f 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,7 +1,9 @@ -## Put comments here that give an overall description of what your -## functions do +## Provides two functions to create a special matrix object and another to perform work upon that +## special matrix object in order to cache its inverse -## Write a short comment describing this function +## Creates a clojure object containing the supplied matrix, has internal variables for managing state of the +## current matrix +## Note that changes to the matrix will reset the cached value, this handles changes to the matrix makeCacheMatrix <- function(our_matrix = matrix()) { computed_inverse <- NULL @@ -21,10 +23,13 @@ makeCacheMatrix <- function(our_matrix = matrix()) { } -## Write a short comment describing this function +## Takes the object returned from a makeCacheMatrix call and allows you to return either a cached inverse +## or to compute one if we haven't calculated it yet +## There is an important change here which is not to pass additional parameters to the solve function as its other +## paramters could modify output (see cacheSolve(t,b=M) as an example) cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' inverse <- x$getInverse() if(!is.null(inverse)) { message("getting cached data") From 04b9acd4b8c3dafa848004ad4cb7d547e26d1c00 Mon Sep 17 00:00:00 2001 From: Jonathan Taylor Date: Sun, 25 Jan 2015 14:37:28 +0000 Subject: [PATCH 3/3] Correcting typo --- cachematrix.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index da79b70268f..d5a7f877834 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -10,7 +10,7 @@ makeCacheMatrix <- function(our_matrix = matrix()) { set <- function(new_matrix) { our_matrix <<- new_matrix #Update our matrix to the new one - computer_inverse <<- NULL #Clear the cached value as the matrix has changed (so we recompute it) + computed_inverse <<- NULL #Clear the cached value as the matrix has changed (so we recompute it) } get <- function() our_matrix