From d4e922e758f7ac40d816d54e8dca63ebae40b90a Mon Sep 17 00:00:00 2001 From: deb14 Date: Sun, 23 Nov 2014 00:24:00 +0530 Subject: [PATCH 1/3] cachematrix.R --- cachematrix.R | 57 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..db1d402d741 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,56 @@ -## Put comments here that give an overall description of what your -## functions do +## functions that cache the inverse of a matrix +## Creates a matrix object that can cache its inverse +makeCacheMatrix <- function( m = matrix() ) { + ## Initialize the inverse property + i <- NULL + ## Method to set the matrix + set <- function( matrix ) { + m <<- matrix + i <<- NULL + } + ## Method the get the matrix + get <- function() { + ## Return the matrix + m + } + ## Method to set the inverse of the matrix + setInverse <- function(inverse) { + i <<- inverse + } + ## Method to get the inverse of the matrix + getInverse <- function() { + ## Return the inverse property + i + } + ## Return a list of the methods + list(set = set, get = get, + setInverse = setInverse, + getInverse = getInverse) +} +## Compute 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, ...) { -## Write a short comment describing this function + ## Return a matrix that is the inverse of 'x' + m <- x$getInverse() -makeCacheMatrix <- function(x = matrix()) { + ## Just return the inverse if its already set + if( !is.null(m) ) { + message("getting cached data") + return(m) + } -} + ## Get the matrix from our object + data <- x$get() + ## Calculate the inverse using matrix multiplication + m <- solve(data) %*% data -## Write a short comment describing this function + ## Set the inverse to the object + x$setInverse(m) -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return the matrix + m } + From 64c81f4f5960cdc5219c366e91b4c1e12fd76441 Mon Sep 17 00:00:00 2001 From: deb14 Date: Sun, 23 Nov 2014 00:34:50 +0530 Subject: [PATCH 2/3] Update cachematrix.R --- cachematrix.R | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index db1d402d741..b8a08dce286 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -2,25 +2,25 @@ ## Creates a matrix object that can cache its inverse makeCacheMatrix <- function( m = matrix() ) { ## Initialize the inverse property - i <- NULL - ## Method to set the matrix + counter <- NULL + ## This is to set the matrix set <- function( matrix ) { m <<- matrix - i <<- NULL + counter <<- NULL } - ## Method the get the matrix + ## This is to get the matrix get <- function() { ## Return the matrix m } - ## Method to set the inverse of the matrix + ## This is to inverse of the matrix setInverse <- function(inverse) { - i <<- inverse + counter <<- inverse } - ## Method to get the inverse of the matrix + ## This is to get the inverse of the matrix getInverse <- function() { ## Return the inverse property - i + counter } ## Return a list of the methods list(set = set, get = get, @@ -28,7 +28,7 @@ makeCacheMatrix <- function( m = matrix() ) { getInverse = getInverse) } ## Compute the inverse of the special matrix returned by "makeCacheMatrix" -## above. If the inverse has already been calculated (and the matrix has not +## 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, ...) { From c7dd3300b80ff89bd2cd23e54cf98ed43927694c Mon Sep 17 00:00:00 2001 From: deb14 Date: Sun, 23 Nov 2014 00:39:29 +0530 Subject: [PATCH 3/3] cachematrix.R --- cachematrix.R | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index b8a08dce286..97a839a10d5 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,56 +1,56 @@ -## functions that cache the inverse of a matrix -## Creates a matrix object that can cache its inverse +# functions that cache the inverse of a matrix +# Creates a matrix object that can cache its inverse makeCacheMatrix <- function( m = matrix() ) { - ## Initialize the inverse property + # Initialize the inverse property counter <- NULL - ## This is to set the matrix + # This is to set the matrix set <- function( matrix ) { m <<- matrix counter <<- NULL } - ## This is to get the matrix + # This is to get the matrix get <- function() { - ## Return the matrix + # Return the matrix m } - ## This is to inverse of the matrix + # This is to inverse of the matrix setInverse <- function(inverse) { counter <<- inverse } - ## This is to get the inverse of the matrix + # This is to get the inverse of the matrix getInverse <- function() { - ## Return the inverse property + # Return the inverse property counter } - ## Return a list of the methods + # Return a list of the methods list(set = set, get = get, setInverse = setInverse, getInverse = getInverse) } -## Compute the inverse of the special matrix returned by "makeCacheMatrix" -## If the inverse has already been calculated (and the matrix has not -## changed), then the "cachesolve" should retrieve the inverse from the cache. +# Compute the inverse of the special matrix returned by "makeCacheMatrix" +# 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' + # Return a matrix that is the inverse of 'x' m <- x$getInverse() - ## Just return the inverse if its already set + # Just return the inverse if its already set if( !is.null(m) ) { message("getting cached data") return(m) } - ## Get the matrix from our object + # Get the matrix from our object data <- x$get() - ## Calculate the inverse using matrix multiplication + # Calculate the inverse using matrix multiplication m <- solve(data) %*% data - ## Set the inverse to the object + # Set the inverse to the object x$setInverse(m) - ## Return the matrix + # Return the matrix m }