forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
71 lines (58 loc) · 2.37 KB
/
cachematrix.R
File metadata and controls
71 lines (58 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
## 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 )
##
## 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
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
## Return a list with all setter and getter functions
list(set = set,
get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## 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( matrix, ... ) {
## Verify the inverse matrix is cached
inverse <- matrix$getInverse()
if ( !is.null( inverse ) ) {
message( "Getting cached data..." )
return( inverse )
}
## In case it is not cached, calculate it
data <- matrix$get()
inverse <- solve( data, ... )
matrix$setInverse( inverse )
inverse
}