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
60 lines (52 loc) · 1.85 KB
/
cachematrix.R
File metadata and controls
60 lines (52 loc) · 1.85 KB
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
## Functions in this file are providing a wrapper around the matrix inversion
## operations with a caching functionality.
##
## Example usage:
##
## 1. wrap a 1000x1000 matrix:
## cm <- makeCacheMatrix(matrix(rnorm(1000000), nrow=1000, ncol=1000))
##
## 2. computation of the inverse, will take some time (cache is empty):
## s1 <- cacheSolve(cm)
##
## 3. try again, will use the cache, very fast:
## s2 <- cacheSolve(cm)
##
## Wrap a matrix (x parameter) in a structure that will also cache the value of
## its inverse after the first time it is computed.
makeCacheMatrix <- function(x = matrix()) {
cached <- NULL # cache for the inverse once it's computed
# use to update the matrix in the wrapper
set <- function(y) {
x <<- y
m <<- NULL
}
# get the original matrix that is stored in the wrapper
get <- function() x
# update the value of the cached inverse
setsolve <- function(solve) cached <<- solve
# obtain the value of the cached inverse
getsolve <- function() cached
# available functions
list(set = set, get = get,
setsolve = setsolve,
getsolve = getsolve)
}
## Get the inverse of a wrapped matrix, using its cached value if possible.
## X should be a wrapped matrix, and additional optional parameters are the
## same as for the solve() function.
cacheSolve <- function(x, ...) {
# get the cached value of the inverse
cached <- x$getsolve()
# if it is defined, use it
if(!is.null(cached)) {
message("getting cached data")
return(cached)
}
# if it is not yet cached, compute it...
data <- x$get()
cached <- solve(data, ...)
# ...and cache it
x$setsolve(cached)
cached
}