Skip to content

Commit 62a19d3

Browse files
author
Sean Hill
committed
Add function to compute solve (inverse) and cache it or return the cached value
1 parent e4ff9fa commit 62a19d3

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

cachematrix.R

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,18 @@ makeCacheMatrix <- function(x = matrix()) {
2020
list(set = set, get = get, set_solve = set_solve, get_solve = get_solve)
2121
}
2222

23-
24-
## Write a short comment describing this function
23+
## This function will compute the inverse and cache it or return the cached inverse
2524

2625
cacheSolve <- function(x, ...) {
27-
## Return a matrix that is the inverse of 'x'
26+
s <- x$get_solve()
27+
28+
if(!is.null(s)) {
29+
message("getting cached data.")
30+
return(s)
31+
}
32+
33+
data <- x$get()
34+
s <- solve(data, ...)
35+
x$set_solve(s)
36+
s
2837
}

0 commit comments

Comments
 (0)