From ccf30862a20266830493ddfec14204028b2c8ba4 Mon Sep 17 00:00:00 2001 From: Marcus Teixeira Date: Sat, 24 May 2014 21:34:28 -0400 Subject: [PATCH 1/3] Functions makeCacheMatrix and cacheSolve completed. --- cachematrix.R | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..985000963e0 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -3,8 +3,23 @@ ## Write a short comment describing this function -makeCacheMatrix <- function(x = matrix()) { - +makeCacheMatrix <- function( dataMatrix = matrix() ) { + inverseMatrix <- NULL + + set <- function( newDataMatrix ) { + dataMatrix <<- newDataMatrix + inverseMatrix <<- NULL + } + + get <- function() dataMatrix + + setInverse <- function( inverse ) inverseMatrix <<- inverse + + getInverse <- function() inverseMatrix + + list(set = set, get = get, + setInverse = setInverse, + getInverse = getInverse) } @@ -12,4 +27,13 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + inverse <- x$getInverse() + if ( !is.null( inverse ) ) { + message( "Getting cached data..." ) + return( inverse ) + } + data <- x$get() + inverse <- solve( data, ... ) + x$setInverse( inverse ) + inverse } From ce7e05bc12f504b583ec538e3a234fe9f669f7f5 Mon Sep 17 00:00:00 2001 From: Marcus Teixeira Date: Sat, 24 May 2014 22:11:41 -0400 Subject: [PATCH 2/3] Comments added in. --- cachematrix.R | 55 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 985000963e0..b3d776b81ca 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,39 +1,70 @@ -## Put comments here that give an overall description of what your -## functions do +## This program calculates the inverse of a Matrix. For efficiency' sake, it also leverages +## a cache memory in order to avoid recalculating an inverse matrix previously done. +## +## Usage: +## 1. Assign the makeCacheMatrix' returned Matrix to a variable +## 2. Calculate its inverse Matrix using cacheSolve +## 3. Call cacheSolve one more time to verify it is using a cache +## 4. Clear cache redoing step 1 and repeat step 2 to check the cache is not used +## +## Example: +## 1. matrixTest = makeCacheMatrix( matrix( c(2,0,1,3,0,0,5,1,1), nrow=3, ncol=3, byrow=TRUE ) ) +## 2. cacheSolve( matrixTest ) +## 3. cacheSolve( matrixTest ) +## 4. matrixTest = makeCacheMatrix( matrix( c(2,0,1,3,0,0,5,1,1), nrow=3, ncol=3, byrow=TRUE ) ) +## cacheSolve( matrixTest ) +## -## Write a short comment describing this function +## makeCacheMatrix sets up an special Matrix object, which contains 2 variables +## getters and setters functions for each one of them. +## By default, the Matrix is initialized empty. Another matrix can be passed as parameter. +## The inverse of a Matrix can only be calculated for a squared matrix, which means it has to have the same number of rows and columns. +## makeCacheMatrix <- function( dataMatrix = matrix() ) { inverseMatrix <- NULL + ## Set the matrix set <- function( newDataMatrix ) { - dataMatrix <<- newDataMatrix + dataMatrix <<- newDataMatrix inverseMatrix <<- NULL } + ## Get the matrix get <- function() dataMatrix + ## Set the inverse of the matrix setInverse <- function( inverse ) inverseMatrix <<- inverse + ## Get the inverse of the matrix getInverse <- function() inverseMatrix - list(set = set, get = get, + ## Return a list with all setter and getter functions + list(set = set, + get = get, setInverse = setInverse, getInverse = getInverse) } -## Write a short comment describing this function +## cacheSolve returns the inverse of a makeCacheMatrix object passed as parameter. +## In case the inverse is stored in a cache, it returns the value stored there, +## otherwise the inverse is calculated. +## -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' - inverse <- x$getInverse() +cacheSolve <- function( matrix, ... ) { + ## Verify the inverse matrix is cached + inverse <- matrix$getInverse() + if ( !is.null( inverse ) ) { message( "Getting cached data..." ) return( inverse ) } - data <- x$get() + + ## In case it is not cached, calculate it + data <- matrix$get() inverse <- solve( data, ... ) - x$setInverse( inverse ) + matrix$setInverse( inverse ) + inverse -} +} \ No newline at end of file From 9bd17aa4bb4fd5674692dfd33e8505d1436e431d Mon Sep 17 00:00:00 2001 From: Marcus Teixeira Date: Wed, 3 Sep 2014 00:27:01 -0400 Subject: [PATCH 3/3] Comments added in --- cachematrix.R | 1 + 1 file changed, 1 insertion(+) diff --git a/cachematrix.R b/cachematrix.R index b3d776b81ca..973cf3752c3 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -17,6 +17,7 @@ ## makeCacheMatrix sets up an special Matrix object, which contains 2 variables ## getters and setters functions for each one of them. +## ## By default, the Matrix is initialized empty. Another matrix can be passed as parameter. ## The inverse of a Matrix can only be calculated for a squared matrix, which means it has to have the same number of rows and columns. ##