forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
63 lines (53 loc) · 1.77 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
## This set of functions allows the use of a matrix that caches its inverse,
## allowing efficient access without needing to recompute it every time.
##
## We use a silly two-method approach to accomplish this task:
## * makeCacheMatrix: This isolates the data (matrix and its inverse)
## * cacheSolve: This retrieves the inverse from cache if available,
## and computes/stores it if not.
## Create a wrapper around a matrix and its inverse.
##
## Methods:
## - get: Returns the matrix
## - set: Sets the value to a new matrix
## - getInverse: Retrieves the stored inverse matrix (can be NULL)
## - setInverse: Sets the value of the inverse matrix
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
# Set the matrix to value y. We assign it to 'x', as shown by the example,
# even though that's a silly way to go about it. :-)
# Also reset the stored inverse, for obvious reasons.
set <- function(y) {
x <<- y
inv <<- NULL
}
# Get the matrix.
get <- function() {
x
}
# Set the inverse of the matrix.
setInverse <- function(inverse) {
inv <<- inverse
}
# Get the inverse value.
getInverse <- function() {
inv
}
# Expose all these functions
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
}
## Given a matrix constructed through makeCacheMatrix, retrieve the inverse.
## If the inverse is not already cached, compute it and store it for future use.
cacheSolve <- function(x, ...) {
# Retrieve from cache if possible.
inv <- x$getInverse()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
# Compute from scratch.
data <- x$get()
inv <- solve(data, ...)
x$setInverse(inv)
inv
}