forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
37 lines (27 loc) · 875 Bytes
/
cachematrix.R
File metadata and controls
37 lines (27 loc) · 875 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
## These functions create a matrix with a cacheable inverse
## Use the makeCacheMatrix to return a cacheable matrix and then use
## cacheSolve to compute the inverse and cache it or return the cached inverse
## This function takes a matrix and returns a list of functions for caching its inverse
makeCacheMatrix <- function(x = matrix()) {
s <- NULL
set <- function(y) {
x <<- y
s <<- NULL
}
get <- function() x
set_solve <- function(solve) s <<- solve
get_solve <- function() s
list(set = set, get = get, set_solve = set_solve, get_solve = get_solve)
}
## This function will compute the inverse and cache it or return the cached inverse
cacheSolve <- function(x, ...) {
s <- x$get_solve()
if(!is.null(s)) {
message("getting cached data.")
return(s)
}
data <- x$get()
s <- solve(data, ...)
x$set_solve(s)
s
}