forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
41 lines (34 loc) · 1.41 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
## Put comments here that give an overall description of what your
## functions do
# makeCacheMatrix: creates a matrix object
# cacheSolve: computes the inv of a matrix or return the cached valued if it exists
## Write a short comment describing this function
# Constructs a matrix object and defines 4 methods
makeCacheMatrix <- function(x = matrix()) { # x is an NA matrix by default
invX = NULL
set = function(y) { # put the matrix in the cache
x <<- y # change the value of x defined in the parent function
invX <<- NULL # reset inv to null
}
get = function() x # get the matrix x from the cache
setInv = function(inv) invX <<- inv # put the inverse in the cache (defined in the parent function)
getInv = function() invX # get the inv from the cache
list(set = set, get = get, setInv = setInv, getInv = getInv) # return list of functions
}
## Write a short comment describing this function
# takes a cacheMatrix as argument
# computes the inverse if necesary
# returns the inverse
cacheSolve <- function(x, ...) { # x is a cacheMatrix
## Return a matrix that is the inverse of 'x'
inv = x$getInv()
if(!is.null(inv)) { # inv has been calculated yet
message("getting inverse from cached data")
return(inv) # exit the function
}
# inv need to be calculated
M = x$get() # get the matrix from the cach
inv = solve(M) # cal the inv
x$setInv(inv) # store the inv in the cache
return(inv)
}