Skip to content

Commit ce7e05b

Browse files
author
Marcus Teixeira
committed
Comments added in.
1 parent ccf3086 commit ce7e05b

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

cachematrix.R

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,70 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## This program calculates the inverse of a Matrix. For efficiency' sake, it also leverages
2+
## a cache memory in order to avoid recalculating an inverse matrix previously done.
3+
##
4+
## Usage:
5+
## 1. Assign the makeCacheMatrix' returned Matrix to a variable
6+
## 2. Calculate its inverse Matrix using cacheSolve
7+
## 3. Call cacheSolve one more time to verify it is using a cache
8+
## 4. Clear cache redoing step 1 and repeat step 2 to check the cache is not used
9+
##
10+
## Example:
11+
## 1. matrixTest = makeCacheMatrix( matrix( c(2,0,1,3,0,0,5,1,1), nrow=3, ncol=3, byrow=TRUE ) )
12+
## 2. cacheSolve( matrixTest )
13+
## 3. cacheSolve( matrixTest )
14+
## 4. matrixTest = makeCacheMatrix( matrix( c(2,0,1,3,0,0,5,1,1), nrow=3, ncol=3, byrow=TRUE ) )
15+
## cacheSolve( matrixTest )
16+
##
317

4-
## Write a short comment describing this function
18+
## makeCacheMatrix sets up an special Matrix object, which contains 2 variables
19+
## getters and setters functions for each one of them.
20+
## By default, the Matrix is initialized empty. Another matrix can be passed as parameter.
21+
## 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.
22+
##
523

624
makeCacheMatrix <- function( dataMatrix = matrix() ) {
725
inverseMatrix <- NULL
826

27+
## Set the matrix
928
set <- function( newDataMatrix ) {
10-
dataMatrix <<- newDataMatrix
29+
dataMatrix <<- newDataMatrix
1130
inverseMatrix <<- NULL
1231
}
1332

33+
## Get the matrix
1434
get <- function() dataMatrix
1535

36+
## Set the inverse of the matrix
1637
setInverse <- function( inverse ) inverseMatrix <<- inverse
1738

39+
## Get the inverse of the matrix
1840
getInverse <- function() inverseMatrix
1941

20-
list(set = set, get = get,
42+
## Return a list with all setter and getter functions
43+
list(set = set,
44+
get = get,
2145
setInverse = setInverse,
2246
getInverse = getInverse)
2347
}
2448

2549

26-
## Write a short comment describing this function
50+
## cacheSolve returns the inverse of a makeCacheMatrix object passed as parameter.
51+
## In case the inverse is stored in a cache, it returns the value stored there,
52+
## otherwise the inverse is calculated.
53+
##
2754

28-
cacheSolve <- function(x, ...) {
29-
## Return a matrix that is the inverse of 'x'
30-
inverse <- x$getInverse()
55+
cacheSolve <- function( matrix, ... ) {
56+
## Verify the inverse matrix is cached
57+
inverse <- matrix$getInverse()
58+
3159
if ( !is.null( inverse ) ) {
3260
message( "Getting cached data..." )
3361
return( inverse )
3462
}
35-
data <- x$get()
63+
64+
## In case it is not cached, calculate it
65+
data <- matrix$get()
3666
inverse <- solve( data, ... )
37-
x$setInverse( inverse )
67+
matrix$setInverse( inverse )
68+
3869
inverse
39-
}
70+
}

0 commit comments

Comments
 (0)