From 4e1e61b0dcd17edaa78588b2ea1d22dc95d00940 Mon Sep 17 00:00:00 2001 From: Evan Jehu Date: Thu, 17 Jul 2014 21:41:58 -0400 Subject: [PATCH 1/3] initial version --- cachematrix.R | 53 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..d9f7e648245 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,50 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { +## A pair of functions that combined can create an object wrapping a square +## matrix that can cache its inverse to avoid the expense of this operation +## executing more than once. +## 'matrix_val' is a square matrix to be contained in the cache enabled object. +## +## Return an object that contains the input martix_val with the ability to +## cache its inverse. +## +makeCacheMatrix <- function(matrix_val = matrix()) { + inverse_val <- NULL + set <- function(new_matrix) { + matrix_val <<- new_matrix + inverse_val <<- NULL + } + get <- function() { + matrix_val + } + setInverse <- function(updated) { + inverse_val <<- updated + } + getInverse <- function() { + inverse_val + } + list(set = set, + get = get, + setInverse = setInverse, + getInverse = getInverse) } -## Write a short comment describing this function - +## 'x' the cache matrix object created by a call to makeCacheMatrix. +## '...' any optional arguments that should be passed to the call to solve() +## used to invert the contained matrix. +## +## Return the inverted matrix value, this will be calculated on the first call +## but the result will be stored in the matrix object for later use. +## cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + m <- x$getInverse() + if(is.null(m)) { + data <- x$get() + m <- solve(data, ...) + x$setInverse(m) + } else { + message("getting cached data") + } + m } From eca794ad1a4dd25c9b7b5a91fdad7eeec8b5a63c Mon Sep 17 00:00:00 2001 From: Evan Jehu Date: Thu, 17 Jul 2014 21:55:37 -0400 Subject: [PATCH 2/3] increased tab sizes from 2 to 4 --- cachematrix.R | 56 +++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index d9f7e648245..4e0dbedb00e 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -8,24 +8,24 @@ ## cache its inverse. ## makeCacheMatrix <- function(matrix_val = matrix()) { - inverse_val <- NULL - set <- function(new_matrix) { - matrix_val <<- new_matrix - inverse_val <<- NULL - } - get <- function() { - matrix_val - } - setInverse <- function(updated) { - inverse_val <<- updated - } - getInverse <- function() { - inverse_val - } - list(set = set, - get = get, - setInverse = setInverse, - getInverse = getInverse) + inverse_val <- NULL + set <- function(new_matrix) { + matrix_val <<- new_matrix + inverse_val <<- NULL + } + get <- function() { + matrix_val + } + setInverse <- function(updated) { + inverse_val <<- updated + } + getInverse <- function() { + inverse_val + } + list(set = set, + get = get, + setInverse = setInverse, + getInverse = getInverse) } @@ -37,14 +37,14 @@ makeCacheMatrix <- function(matrix_val = matrix()) { ## but the result will be stored in the matrix object for later use. ## cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' - m <- x$getInverse() - if(is.null(m)) { - data <- x$get() - m <- solve(data, ...) - x$setInverse(m) - } else { - message("getting cached data") - } - m + ## Return a matrix that is the inverse of 'x' + m <- x$getInverse() + if(is.null(m)) { + data <- x$get() + m <- solve(data, ...) + x$setInverse(m) + } else { + message("getting cached data") + } + m } From d60b8beb4bc4d49a04e594444f0c2eb8b32e7232 Mon Sep 17 00:00:00 2001 From: Evan Jehu Date: Sun, 27 Jul 2014 09:51:39 -0400 Subject: [PATCH 3/3] comment improvement --- cachematrix.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index 4e0dbedb00e..e53777b2dc6 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -2,7 +2,8 @@ ## matrix that can cache its inverse to avoid the expense of this operation ## executing more than once. -## 'matrix_val' is a square matrix to be contained in the cache enabled object. +## 'matrix_val' is a square matrix to be contained in the cache enabled object, +## this function assumes that the matrix supplied is always invertible. ## ## Return an object that contains the input martix_val with the ability to ## cache its inverse.