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
42 lines (36 loc) · 1.26 KB
/
cachematrix.R
File metadata and controls
42 lines (36 loc) · 1.26 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
## Fucntions to get the inverse of a matrix with a cached value
## Function that create an special object that contains the value of a matrix and expose
# the functionality to calculate the solve just if the value of the matrix has changed
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
## function that sets the value of the matrix
## if the value of the matrix change the inverse must be recalculated
## then the cache must be cleaned
set <- function(y) {
x <<- y
m <<- NULL
}
## get the value of the matrix
get <- function() x
## set the value of the inverse of the matrix
setsolve <- function(solve) m <<- solve
## get the value of the inverse of the matrix
getsolve <- function() m
list(set = set, get = get,
setsolve = setsolve,
getsolve = getsolve)
}
## Function that gets the inverse of a matrix from the cache version if possible
cacheSolve <- function(x, ...) {
## Returns the inverse of the matrix
m <- x$getsolve()
if(!is.null(m)) {
message("getting cached inverse matrix")
return(m)
}
data <- x$get()
m <- solve(data, ...)
## If the inverse is not cached then calculates the inverse and sets the cache value
x$setsolve(m)
m
}