forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
53 lines (50 loc) · 1.93 KB
/
cachematrix.R
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
## This method creates a special matrix which allows caching the inverse matrix.
# It is is really a list containing a function to
# 1. set the value of the matrix
# 2. get the value of the matrix
# 3. set the inverse matrix
# 4. get the inverse matrix
makeCacheMatrix <- function(x = matrix()) {
xinv <- NULL # create a NULL inverse matrix
set <- function(y) { # set the matrix to be x, and its inverse to be NULL
x <<- y
xinv <<- NULL
}
get <- function () x # returns the matrix x
setinverse <- function(inv) xinv <<- inv # set the inverse matrix
getinverse <- function() xinv # returns the inverse matrix
list(set = set,
get = get,
setinverse = setinverse,
getinverse = getinverse) # list all functions, essential if we want to use them elsewhere
}
## This method searches if the inverse matrix is cached
# if not, the method computes the inverse and caches it
cacheSolve <- function(x, ...) {
xMatrix <- x$get() # gets the matrix x and its dimensions
nCol = ncol(xMatrix)
nRow = nrow(xMatrix)
if(nCol != nRow) { # if it is not square it returns an error
return(message("fail: matrix should be square"))
}
xInverse <- x$getinverse()
if(!is.null(xInverse)) { # searches for its inverse on the cache and it returns it
message("getting cached data: \n\tif you changed the parameters, you should clean the cache with cacheClean(matrix)")
return(xInverse)
}
#if it does not find it, it computes the inverse and caches it
xInverse <- solve(xMatrix, ...)
x$setinverse(xInverse)
xInverse
}
## Cleans the cache
# the inverse can be computed using different parameters, but those are not kept on cache,
# and therefore the method cacheSolve will return the inverse matrix with the old parameters
# If we clean the cache, this will not happen
cacheClean <- function(x) {
if(is.null(x$getinverse())) { # if it does not find an inverse there is nothing to do
return(message("already clean"))
}
x$setinverse(NULL)
message("cleaning cached data")
}