forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
34 lines (30 loc) · 917 Bytes
/
cachematrix.R
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
## These functions provide memoized matrix inversion
## Returns the specified, invertible matrix in a wrapper (decorator)
## that provides a memoized (cached) access to its inverse
## via the solve() function.
##
## Additionally, the function is_cached() is exposed to allow testing
## the caching behaviour of the decorator.
##
makeCacheMatrix <- function(x = matrix()) {
cached_solution <- NULL
solve_memoized <- function() {
if (is.null(cached_solution)) {
cached_solution <<- solve(x)
}
cached_solution
}
list(
is_cached = function() !is.null(cached_solution),
solve = solve_memoized
)
}
## Takes a matrix decorated by the makeCacheMatrix function
## and returns its inverse, using the solve() function.
##
## Note: This function is redundant, and exists only to match
## the assignment. All the beef is in makeCacheMatrix().
##
cacheSolve <- function(x) {
x$solve()
}