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
+ # #
3
17
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
+ # #
5
23
6
24
makeCacheMatrix <- function ( dataMatrix = matrix () ) {
7
25
inverseMatrix <- NULL
8
26
27
+ # # Set the matrix
9
28
set <- function ( newDataMatrix ) {
10
- dataMatrix <<- newDataMatrix
29
+ dataMatrix <<- newDataMatrix
11
30
inverseMatrix <<- NULL
12
31
}
13
32
33
+ # # Get the matrix
14
34
get <- function () dataMatrix
15
35
36
+ # # Set the inverse of the matrix
16
37
setInverse <- function ( inverse ) inverseMatrix <<- inverse
17
38
39
+ # # Get the inverse of the matrix
18
40
getInverse <- function () inverseMatrix
19
41
20
- list (set = set , get = get ,
42
+ # # Return a list with all setter and getter functions
43
+ list (set = set ,
44
+ get = get ,
21
45
setInverse = setInverse ,
22
46
getInverse = getInverse )
23
47
}
24
48
25
49
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
+ # #
27
54
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
+
31
59
if ( ! is.null( inverse ) ) {
32
60
message( " Getting cached data..." )
33
61
return ( inverse )
34
62
}
35
- data <- x $ get()
63
+
64
+ # # In case it is not cached, calculate it
65
+ data <- matrix $ get()
36
66
inverse <- solve( data , ... )
37
- x $ setInverse( inverse )
67
+ matrix $ setInverse( inverse )
68
+
38
69
inverse
39
- }
70
+ }
0 commit comments