forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
75 lines (64 loc) · 1.83 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
## Cached version of matrix which efficiently calculates its inverse
## when result is required multiple times.
## Create cached version of matrix.
## This is represented by a list with following 4 functions:
## - set : Set the underlying matrix.
## - get : Get the underlying matrix.
## - setInverse : Set an inverse of the underlying matrix.
## - getInverse : Get an inverse of the underlying matrix.
##
## Args:
## x: A underlying matrix represented by the result.
##
## Returns:
## A list which can be regarded as a cached version of matrix.
makeCacheMatrix <- function(x = matrix()) {
inverse <- NULL
set <- function(mat) {
x <<- mat?
# We have to erase answer since it is no longer right.
inverse <<- NULL
}
get <- function() {
x
}
# This function is intended to be used only by `cacheSolve`.
# Calling this directly may result in wrong answer.
setInverse <- function(inv) {
inverse <<- inv
}
getInverse <- function() {
inverse
}
list(set = set,
get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## Calculate an inverse matrix.
## This function uses answer if available from previous call
## for saving time.
##
## Args:
## x: Cached version of matrix created by `makeCacheMatrix`.
##
## Returns:
## An inverse matrix of x, non-cached version.
cacheSolve <- function(x, ...) {
inverse <- x$getInverse()
if (!is.null(inverse)) {
message("getting cached data")
inverse
} else {
inverse <- solve(x$get(), ...)
x$setInverse(inverse)
inverse
}
}
# Below is just for testing.
test <- makeCacheMatrix(matrix(1:4, 2, 2))
print(test$get())
print(test$getInverse())
testInverse <- cacheSolve(test)
testInverse <- cacheSolve(test)
print(test$get() %*% testInverse)