From e32725e2d13c47b0ecc4f2abc07b14aeb48869a6 Mon Sep 17 00:00:00 2001 From: Rajwinder Singh Date: Thu, 22 May 2014 01:25:10 -0700 Subject: [PATCH 1/3] matrix solution --- cachematrix.R | 55 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..d60024cc11c 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,40 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { - -} - - -## Write a short comment describing this function - -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' -} +## Put comments here that give an overall description of what your +## functions do + +## Write a short comment describing this function + +makeCacheMatrix <- function(x = matrix()) { + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinverse <- function(solve) m <<- solve + getinverse <- function() m + list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) + +} + + +## Write a short comment describing this function + +cacheSolve <- function(x, ...) { + m <- x$getinverse() + if(!is.null(m)) { + message ("getting cached inverse matrix") + return(m) + } + message("Don't have computation cached. Calculating inverse of input matrix...") + data <- x$get() + m <- solve(data) + x$setinverse(m) + m +} + +a <- matrix(c(1:9), nrow= 3, ncol = 3) +a[[2,2]] <- 0 +a +b <- makeCacheMatrix(a) +cacheSolve(b) + From 2bcf8c57efa6ed3dc43cdaf9d1c5f86814cca1b3 Mon Sep 17 00:00:00 2001 From: Rajwinder Singh Date: Sun, 25 May 2014 11:13:50 -0700 Subject: [PATCH 2/3] Final version, tested. --- cachematrix.R | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index d60024cc11c..270124e67aa 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,7 +1,15 @@ -## Put comments here that give an overall description of what your -## functions do +## This pair of functions gives a computationally efficient way of cacluating the +## inverse of a matrix. It employs the lexical scoping feature of R. Effectively, +## this code reduces time complexity at the cost of space complexity. -## Write a short comment describing this function +## The computational efficiency comes from never calculating the inverse of +## a given input matrix more than once. This is achieved by caching the inverse of each +## new (distinct) input matrix as a list object. The cache is implemented +## outside the current environment using <<- (the superassignment operato)r. + + +# makeCacheMatrix creates a new list object that has the following methods: +# a getter, a setter, inverse calculator/setter, inverse retriever/getter. makeCacheMatrix <- function(x = matrix()) { m <- NULL @@ -17,24 +25,35 @@ makeCacheMatrix <- function(x = matrix()) { } -## Write a short comment describing this function +# cacheSolve checks if the given matrix has already been claculated in previous runs. +# If so, it retrieves the inverser matrix from the cache. If not, it calculates +# inverse using the "solve" library function, stores it in the cache and also +# return the newly calculated inverse of the input matrix. cacheSolve <- function(x, ...) { - m <- x$getinverse() + m <- x$getinverse() # check if already encountered this matrix if(!is.null(m)) { message ("getting cached inverse matrix") return(m) } message("Don't have computation cached. Calculating inverse of input matrix...") data <- x$get() - m <- solve(data) + m <- solve(data) # calculate the inverse of the given matrix x$setinverse(m) m } -a <- matrix(c(1:9), nrow= 3, ncol = 3) -a[[2,2]] <- 0 -a -b <- makeCacheMatrix(a) -cacheSolve(b) +# Test the code for functionality & correctness. +# a <- matrix(c(6, 9, 0, -9, 66, 2, -0.5, 25, 37), nrow= 3, ncol = 3) +# a[[2,2]] <- 0 +# a +# b <- makeCacheMatrix(a) +# cacheSolve(b) +# a <- cacheSolve(b) +# b <- makeCacheMatrix(a) +# cacheSolve(b) +# round(cacheSolve(b), 3) +# a <- matrix(c(1,0,0,0,1,0,0,0,1), nrow = 3, ncol = 3) +# b <- makeCacheMatrix(a) +# cacheSolve(b) From 8a6ed2e031dd389e318c8ea0b339a5cdcf20680a Mon Sep 17 00:00:00 2001 From: Rajwinder Singh Date: Sun, 25 May 2014 11:43:11 -0700 Subject: [PATCH 3/3] Final version, tested. -v2 --- cachematrix.R | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 270124e67aa..0c0e0b82a3a 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -10,12 +10,13 @@ # makeCacheMatrix creates a new list object that has the following methods: # a getter, a setter, inverse calculator/setter, inverse retriever/getter. +# This list object is a wrapper around the input matrix makeCacheMatrix <- function(x = matrix()) { m <- NULL set <- function(y) { - x <<- y - m <<- NULL + x <<- y # store the input matrix + m <<- NULL # placeholder for inverse matrix, to be filled with the setter } get <- function() x setinverse <- function(solve) m <<- solve @@ -37,9 +38,9 @@ cacheSolve <- function(x, ...) { return(m) } message("Don't have computation cached. Calculating inverse of input matrix...") - data <- x$get() + data <- x$get() # pull out the actual input matrix from the input (list object) m <- solve(data) # calculate the inverse of the given matrix - x$setinverse(m) + x$setinverse(m) # store the inverse in the cache m }