Skip to content

Commit 0376be4

Browse files
author
Sean Hill
committed
Merge branch 'make_cacheable_matrix'
* make_cacheable_matrix: Add function to compute solve (inverse) and cache it or return the cached value Add function to build matrix with cacheable solve (inverse) Add file description comments
2 parents 7f657dd + 62a19d3 commit 0376be4

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

cachematrix.R

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## These functions create a matrix with a cacheable inverse
2+
## Use the makeCacheMatrix to return a cacheable matrix and then use
3+
## cacheSolve to compute the inverse and cache it or return the cached inverse
34

4-
## Write a short comment describing this function
5+
## This function takes a matrix and returns a list of functions for caching its inverse
56

67
makeCacheMatrix <- function(x = matrix()) {
7-
8+
s <- NULL
9+
10+
set <- function(y) {
11+
x <<- y
12+
s <<- NULL
13+
}
14+
15+
get <- function() x
16+
17+
set_solve <- function(solve) s <<- solve
18+
get_solve <- function() s
19+
20+
list(set = set, get = get, set_solve = set_solve, get_solve = get_solve)
821
}
922

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

1325
cacheSolve <- function(x, ...) {
14-
## 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
1537
}

0 commit comments

Comments
 (0)