forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
112 lines (93 loc) · 3.28 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
## `makeCacheMatrix` and `cacheSolve`
##
## This file contains the `makeCacheMatrix` function, which is used
## to construct a wrapper object. This wrapper object holds a matrix,
## which can be retrieved using the `get` method and updated with
## the `set` method.
##
## The wrapper also contains the functions `getInv` and `setInv`,
## which exist to support use of the wrapper with the `cacheSolve`
## function.
## Calling `cacheSolve` on a wrapper instance will extract and return
## the inverse of the wrapped matrix if it has been previously calculated.
## Otherwise it will calculate the inverse and store it in the wrapper
## for future use.
##
## Sample Usage:
## -------------
## > m <- matrix(c(-1, -2, 1, 1), 2, 2)
## > cm <- makeCacheMatrix(m)
## > cacheSolve(cm)
## [,1] [,2]
## [1,] 1 -1
## [2,] 2 -1
## > cacheSolve(cm)
## getting cached data
## [,1] [,2]
## [1,] 1 -1
## [2,] 2 -1
## >
## ==================================================================
## `makeCacheMatrix`
## ==================================================================
##
## Create & return a matrix wrapper object.
##
## May be invoked with an initial matrix to wrap, otherwise will
## initially wrap an empty matrix.
##
## Note that updating the wrapped matrix via `set` will clear the
## cached inverse.
##
## The cached inverse of the wrapped matrix can be retrieved (or
## computed, set, and retrieved, if it has been cleared or not
## yet set) using the `cacheInverse` function.
makeCacheMatrix <- function(x = matrix()) {
# Matrix inverse is initially unset.
inv <- NULL
# Wrapped matrix can be updated.
# Doing so will clear the cached inverse.
set <- function(m) {
x <<- m
inv <<- NULL
}
# Wrapped matrix can be retrieved.
get <- function() x
# Inverse of the wrapped matrix can be get and
# set using these methods.
setInv <- function(inv) inv <<- inv
getInv <- function() inv
# Return a list of methods (functions). These functions
# all share the same environment, which consists of the
# matrix `x` and it's cached inverse `inv`.
list(set = set, get = get,
setInv = setInv,
getInv = getInv)
}
## ==================================================================
## `cacheSolve`
## ==================================================================
##
## This function can be invoked with an instance of the matrix wrapper
## object created using `makeCacheMatrix`. Invoking it will return the
## inverse of the wrapped matrix.
##
## The inverse will be retrieved from the wrapper's cache if the cache
## has been set, otherwise `cacheSolve` will compute the inverse, store
## it using the wrapper's `setInv` method, and then return it.
cacheSolve <- function(x, ...) {
# `x` should be a matrix wrapper created with `makeCacheMatrix`.
# Attempt to read the cached inverse of the wrapped matrix.
inv <- x$getInv()
# If the cached inverse is not null, return it.
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
# Otherwise compute the inverse of the wrapped matrix...
inv <- solve(x$get())
# ... set the inverse in wrapper ...
x$setInv(inv)
# ... and then return the computed inverse.
inv
}