From 71cf924ec332001dfb5a3a50e981069377bf950b Mon Sep 17 00:00:00 2001 From: Robert Joseph Hagan Date: Mon, 21 Apr 2014 21:58:01 -0400 Subject: [PATCH 1/4] Update cachematrix.R --- cachematrix.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..7ba3d71f7fe 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,7 +1,8 @@ ## Put comments here that give an overall description of what your ## functions do -## Write a short comment describing this function +## This function creates a cache-able matrix and includes subfunctions to save it in the workspace +#Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { From 1d3e24eede395caf913360da4da7f7bb8f218460 Mon Sep 17 00:00:00 2001 From: Robert Joseph Hagan Date: Mon, 21 Apr 2014 22:57:59 -0400 Subject: [PATCH 2/4] Update cachematrix.R --- cachematrix.R | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 7ba3d71f7fe..0162263cd2d 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,16 +1,42 @@ -## Put comments here that give an overall description of what your +## Two functions to eliminate the need to repeately re-compute the inverse of a matrix if it has not changed +## The two functions work together to check if the inverse of a matrix has been computed and if +## it has already been computed, retrieve it from the parent workspace, otherwise compute the inverse +## and put it in the parent workspace for continuing use. + ## functions do ## This function creates a cache-able matrix and includes subfunctions to save it in the workspace -#Write a short comment describing this function +## makeCacheMatrix and cascheSolve are to be used together as a pair of functions +## to save time needed to re-compute the inverse of the matrix makeCacheMatrix <- function(x = matrix()) { - +m <- NULL #set up a null matrix + set <- function(y) { + x <- y + m <- NULL # set up an empty matrix to work with + } + get <- function() x + setinverse <- function(inverse) m <<- inverse #put the inverse in the parent workspace + getinverse <- function() m + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) #parameters for saving and getting back the inverse } -## Write a short comment describing this function +## R script for function to check if inverse is already computed +## if already computed, retrieve it. If first iteration, compute inverse with solve and return it +## to be saved in the cache cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' +m <- x$getinverse() + if(!is.null(m)) { + message("getting cached inverted matrix if it exist") + return(m) + } + data <- x$get() + m <- solve(data, ...) # invert the matrix and pass it to m + x$setinverse(m) + m } From 6f5cbfdc1b51e52f60530ff2fce5023186f38ba4 Mon Sep 17 00:00:00 2001 From: Robert Joseph Hagan Date: Mon, 21 Apr 2014 23:03:24 -0400 Subject: [PATCH 3/4] Update cachematrix.R --- cachematrix.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index 0162263cd2d..ba391df901a 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -33,7 +33,7 @@ cacheSolve <- function(x, ...) { m <- x$getinverse() if(!is.null(m)) { message("getting cached inverted matrix if it exist") - return(m) + return(m) #return the previously inverted matrix } data <- x$get() m <- solve(data, ...) # invert the matrix and pass it to m From a969209fde50034bb5723197f03a99d0d249da1f Mon Sep 17 00:00:00 2001 From: Robert Joseph Hagan Date: Mon, 21 Apr 2014 23:16:50 -0400 Subject: [PATCH 4/4] Update cachematrix.R --- cachematrix.R | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index ba391df901a..56bf16ba0f0 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -3,12 +3,16 @@ ## it has already been computed, retrieve it from the parent workspace, otherwise compute the inverse ## and put it in the parent workspace for continuing use. -## functions do +## Parameters should be a square matrix first and the second shuld be a vector giving the right hand +## sie of the linear system.. If b is missing, it is assumed to be an identity vector or matrix +## so the inverse of x is returned. To ensure the inverse of the matrix X is returned, no other parameters +## should be included. ## This function creates a cache-able matrix and includes subfunctions to save it in the workspace ## makeCacheMatrix and cascheSolve are to be used together as a pair of functions ## to save time needed to re-compute the inverse of the matrix + makeCacheMatrix <- function(x = matrix()) { m <- NULL #set up a null matrix set <- function(y) {