Skip to content

Commit d555c09

Browse files
committed
complete solution
1 parent 7f657dd commit d555c09

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

cachematrix.R

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,45 @@
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.
53

64
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
718

19+
list(
20+
set=set
21+
, get=get
22+
, setInverse=setInverse
23+
, getInverse=getInverse
24+
)
825
}
926

1027

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.
1231

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
1545
}

0 commit comments

Comments
 (0)