From 3f3eb8359f108610c3b905860ffc98c5fd737a48 Mon Sep 17 00:00:00 2001 From: Vishal Bamba Date: Sat, 25 Apr 2015 00:21:06 -0700 Subject: [PATCH 1/4] Initial commit --- cachematrix.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..2f4790b008a 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,14 +1,16 @@ ## Put comments here that give an overall description of what your ## functions do -## Write a short comment describing this function +## This function creates a special "matrix" object that can cache its inverse. makeCacheMatrix <- function(x = matrix()) { } -## Write a short comment describing this function +## This function computes the inverse of the special "matrix" returned by makeCacheMatrix above. +## If the inverse has already been calculated (and the matrix has not changed), then the cachesolve +## should retrieve the inverse from the cache. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' From ee97e076f9ffdacc62656e1152beaf55bb8658ea Mon Sep 17 00:00:00 2001 From: Vishal Bamba Date: Sat, 25 Apr 2015 12:10:14 -0700 Subject: [PATCH 2/4] Adding code for both functions --- cachematrix.R | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index 2f4790b008a..39b374e179d 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,7 +4,17 @@ ## This function creates a special "matrix" object that can cache its inverse. makeCacheMatrix <- function(x = matrix()) { - + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinverse <- function(inverse) m <<- inverse + getinverse <- function() m + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } @@ -14,4 +24,15 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + m <- x$getinverse() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- solve(data) + x$setinverse(m) + m } + + From c7853f7e24f10df5dd8043a984db20c33921c9cf Mon Sep 17 00:00:00 2001 From: Vishal Bamba Date: Sat, 25 Apr 2015 14:04:15 -0700 Subject: [PATCH 3/4] Added summary comments --- cachematrix.R | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 39b374e179d..14b4e906d1d 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,29 +1,37 @@ -## Put comments here that give an overall description of what your -## functions do +## cacheMatrix, provides two functions that compute the inverse of a matrix and cache it +# for future use. The first function makeCacheMatrix takes a matrix as a single argument. +# The matrix is assumed to be inversible. It returns a list of four helper functions to get & set a +# matrix, and to get and set its cached inverse +# The second function cacheSolve computes the inverse of a matrix and returns its value -## This function creates a special "matrix" object that can cache its inverse. + +## This function creates a special "matrix" object that can cache its inverse. The function +## is comprised of a list of four functions (set, get, setinverse, getinverse). get returns +## the current matrix, set is use to set the matrix to a new matrix, getinverse returns the +## cached copy of the inverse, setinverse sets the cache to the new value of the inverse makeCacheMatrix <- function(x = matrix()) { m <- NULL + get <- function() x set <- function(y) { x <<- y m <<- NULL } - get <- function() x setinverse <- function(inverse) m <<- inverse getinverse <- function() m - list(set = set, get = get, + list(get = get, + set = set, setinverse = setinverse, getinverse = getinverse) } ## This function computes the inverse of the special "matrix" returned by makeCacheMatrix above. -## If the inverse has already been calculated (and the matrix has not changed), then the cachesolve -## should retrieve the inverse from the cache. +## If the inverse has already been calculated (and the matrix has not changed), then the cached copy +## is returned. Otherwise we assume the current matrix is inversible and use solve(X) to inverse the +## matrix and return the result cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' m <- x$getinverse() if(!is.null(m)) { message("getting cached data") From d0ff8228bac5a181966e833bea18e29e2b8bc9db Mon Sep 17 00:00:00 2001 From: Vishal Bamba Date: Sun, 26 Apr 2015 01:13:13 -0700 Subject: [PATCH 4/4] Added more comments --- cachematrix.R | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cachematrix.R b/cachematrix.R index 14b4e906d1d..76e5ea27e63 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -3,6 +3,16 @@ # The matrix is assumed to be inversible. It returns a list of four helper functions to get & set a # matrix, and to get and set its cached inverse # The second function cacheSolve computes the inverse of a matrix and returns its value +# Sample code to test +# source("cacheMatrix.R") +# c=rbind(c(1, -1/4), c(-1/4, 1)) +# a <- makeCacheMatrix(c) +# a$get() +# s <- cacheSolve(a) +# s +# a$set(s) +# a$get() + ## This function creates a special "matrix" object that can cache its inverse. The function