File tree Expand file tree Collapse file tree 1 file changed +37
-7
lines changed Expand file tree Collapse file tree 1 file changed +37
-7
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
3
-
4
- # # Write a short comment describing this function
1
+ # # function makeCacheMatrix(): Returns a special matrix (list) that allows you
2
+ # # to cache it's inverse to expediantly fetch at a later time.
5
3
6
4
makeCacheMatrix <- function (x = matrix ()) {
5
+
6
+ set <- function (x ) {
7
+ MAT <<- x
8
+ INV_CACHE <<- NULL # empty cache with new instatiation
9
+ }
10
+
11
+ get <- function () MAT
12
+
13
+ setInverse <- function (inv ) INV_CACHE <<- inv
14
+
15
+ getInverse <- function () INV_CACHE
16
+
17
+ set(x ) # Initial instantiation
7
18
19
+ list (
20
+ set = set
21
+ , get = get
22
+ , setInverse = setInverse
23
+ , getInverse = getInverse
24
+ )
8
25
}
9
26
10
27
11
- # # Write a short comment describing this function
28
+ # # function cacheSolve(): Will pull value from cache if it exists else call
29
+ # # solve on the matrix and cache the results for future use. Finally, it returns
30
+ # # the inverse of the matrix.
12
31
13
- cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
32
+ cacheSolve <- function (x , ... ) { # # Return a matrix that is the inverse of 'x'
33
+
34
+ inv <- x $ getInverse()
35
+
36
+ if (! is.null(inv )) {
37
+ return (inv ) # return inverse from cache.
38
+ }
39
+
40
+ inv <- solve(x $ get()) # get inverse
41
+
42
+ x $ setInverse(inv ) # cache inverse for future use
43
+
44
+ inv
15
45
}
You can’t perform that action at this time.
0 commit comments