From dd8abd394714c6344f64702fb3d73658fe9e16e4 Mon Sep 17 00:00:00 2001 From: Stephen Abrams Date: Sun, 21 Feb 2016 21:42:54 -0500 Subject: [PATCH 1/3] Completes assignment 2 --- cachematrix.R | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..a320a616e76 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,37 @@ ## Put comments here that give an overall description of what your ## functions do -## Write a short comment describing this function - +## returns a list of functions to get/set an 'internal' matrix and get/set its inverse. +## (get, set, getinv, setinv). +## If the matrix is 'set' via this list's set function, the inverse matrix will be cleared. makeCacheMatrix <- function(x = matrix()) { + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinv <- function(matinv) m <<- matinv + getinv <- function() m + list(set = set, get = get, + setinv = setinv, + getinv = getinv) } -## Write a short comment describing this function - +## Returns inverse of x, storing the computation of the inverse +## x is not actually a matrix, but rather a list generated by makeCacheMatrix cacheSolve <- function(x, ...) { + + m <- x$getinv() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- matrix(rev(c(data)), nrow=nrow(data), ncol=ncol(data)) + x$setinv(m) + m ## Return a matrix that is the inverse of 'x' } From 8d3d468b791d7477cdee450d95637a94dd48361d Mon Sep 17 00:00:00 2001 From: Stephen Abrams Date: Sun, 21 Feb 2016 21:50:02 -0500 Subject: [PATCH 2/3] add additional comments and include helper test script whose contents I used on console to verify --- cachematrix.R | 3 +++ test.R | 10 ++++++++++ 2 files changed, 13 insertions(+) create mode 100644 test.R diff --git a/cachematrix.R b/cachematrix.R index a320a616e76..2f747dc7362 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -22,6 +22,9 @@ makeCacheMatrix <- function(x = matrix()) { ## Returns inverse of x, storing the computation of the inverse ## x is not actually a matrix, but rather a list generated by makeCacheMatrix +## This will set the 'internal' inverse matrix for mackCacheMatrix context if +## it doesn't already exist, and will simply return that matrix if it exists w/o +## recomputing. cacheSolve <- function(x, ...) { m <- x$getinv() diff --git a/test.R b/test.R new file mode 100644 index 00000000000..7195dbb6fdb --- /dev/null +++ b/test.R @@ -0,0 +1,10 @@ +source('./cachematrix.R') +x <- matrix(1:20, nrow=3, ncol=2) +y <- matrix(100:106, nrow=3, ncol=2) +z <- matrix(c("a", "v", "b", "aa", "ab", "cc"), nrow=3, ncol=3) +cmx <- makeCacheMatrix(x) +cmy <- makeCacheMatrix(y) +cmz <- makeCacheMatrix(z) +invx <- cacheSolve(cmx) +invy <- cacheSolve(cmy) +invz <- cacheSolve(cmz) \ No newline at end of file From 244ef491e4c00d1ea6cc7efbf472b3c4613b7195 Mon Sep 17 00:00:00 2001 From: Stephen Abrams Date: Sun, 21 Feb 2016 22:38:00 -0500 Subject: [PATCH 3/3] fix after learning to get inverse by calling solve --- cachematrix.R | 2 +- test.R | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 2f747dc7362..1174ca5054b 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -33,7 +33,7 @@ cacheSolve <- function(x, ...) { return(m) } data <- x$get() - m <- matrix(rev(c(data)), nrow=nrow(data), ncol=ncol(data)) + m <- solve(data) x$setinv(m) m ## Return a matrix that is the inverse of 'x' diff --git a/test.R b/test.R index 7195dbb6fdb..8c1d7442748 100644 --- a/test.R +++ b/test.R @@ -1,10 +1,11 @@ source('./cachematrix.R') -x <- matrix(1:20, nrow=3, ncol=2) -y <- matrix(100:106, nrow=3, ncol=2) -z <- matrix(c("a", "v", "b", "aa", "ab", "cc"), nrow=3, ncol=3) +x <- matrix(c(2,2,3,2), nrow=2, ncol=2) +y <- matrix(c(1, -1, 1, 2), nrow=2, ncol=2) cmx <- makeCacheMatrix(x) cmy <- makeCacheMatrix(y) -cmz <- makeCacheMatrix(z) invx <- cacheSolve(cmx) invy <- cacheSolve(cmy) -invz <- cacheSolve(cmz) \ No newline at end of file +solve(cacheSolve(cmy)) +solve(cacheSolve(cmx)) +solve(cacheSolve(cmy)) +solve(cacheSolve(cmx))