File tree Expand file tree Collapse file tree 1 file changed +32
-6
lines changed Expand file tree Collapse file tree 1 file changed +32
-6
lines changed Original file line number Diff line number Diff line change 1
- # # Put comments here that give an overall description of what your
2
- # # functions do
1
+ # # These functions allow the program to run more efficiently by
2
+ # # caching certain computations that maybe costly. In this case,
3
+ # # it caches the inverse of a matrix to avoid computing again.
3
4
4
- # # Write a short comment describing this function
5
+ # # This function creates a special "matrix" object that can cache
6
+ # # its inverse.
5
7
6
8
makeCacheMatrix <- function (x = matrix ()) {
7
-
9
+ invX <- NULL
10
+ set <- function (y ) {
11
+ x <<- y
12
+ invX <<- NULL
13
+ }
14
+ get <- function () {
15
+ x
16
+ }
17
+ setInv <- function (inv ) {
18
+ invX <<- inv
19
+ }
20
+ getInv <- function () {
21
+ invX
22
+ }
23
+ list (set = set , get = get , setInv = setInv , getInv = getInv )
8
24
}
9
25
10
26
11
- # # Write a short comment describing this function
27
+ # # This function computes the inverse of the special "matrix"
28
+ # # returned by makeCacheMatrix above. If the inverse has already
29
+ # # been calculated (and the matrix has not changed), then
30
+ # # cacheSolve should retrieve the inverse from the cache.
12
31
13
32
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
33
+ mat <- x $ getInv()
34
+ if (! is.null(mat )) {
35
+ mat
36
+ }
37
+ data <- x $ get()
38
+ mat <- solve(data )
39
+ x $ setInv(mat )
40
+ mat
15
41
}
You can’t perform that action at this time.
0 commit comments