forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
72 lines (69 loc) · 2.51 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
# General comments:
# Matrix inversion is usually a costly computation and their
# may be some benefit to caching the inverted matrix rather
# than compute it repeatedly. Below pair of functions are
# defined that cache the inverted matrix and access it without
# recomputing once input matrix is inverted.
# Example:
# 1.1. Define square matrix 2x2:
# > A = matrix(c(1, 2, 3, 4), nrow=2, ncol=2, byrow = TRUE)
# 1.2. Print matrix
# > A
# [,1] [,2]
# [1,] 1 2
# [2,] 3 4
# 2. Create special "matrix":
# > B<-makeCacheMatrix(A)
# 3.1. Invert special "matrix":
# > C<-cacheSolve(B)
# 3.2. Print inverted matrix:
# > C
# [,1] [,2]
# [1,] -2.0 1.0
# [2,] 1.5 -0.5
# 4. Test that A*C = E, where E is a unit matrix
# | 1 2 | | -2.0 1.0 | | 1 0 |
# | 3 4 | * | 1.5 -0.5 | = | 0 1 |
# The "makeCacheMatrix" function creates a special "matrix",
# which is really a list containing the following functions
# * "set" - set the "value" of the matrix
# * "get" - get the "value" of the matrix
# * "setInverse" - set the "value" of the inverse matrix
# * "getInverse" - get the "value" of the inverse matrix
makeCacheMatrix <- function(x = matrix()) {
inverse <- NULL
set <- function(y) {
x <<- y
inverse <<- NULL
}
get <- function() x
setInverse <- function(Inverse) inverse <<- Inverse
getInverse <- function() inverse
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
# The "cacheSolve" function inverses the special "matrix" objetc
# created with the "makeCacheMatrix" function (defined above).
# However, this function first checks if the inverted matrix has
# already been calculated and the matrix has not changed:
# * if so, it gets the inverted matrix from the cache and skips
# the computation. Note, the function prints out "getting
# cached inverted matrix" in this case
# * otherwise, it calculates the inverted matrix of the data and
# sets the "value" of the inverted matrix in the cache via the
# "setInverse" function. Note, the function prints out "calculate
# inverted matrix and store it in cache" in this case
cacheSolve <- function(x = matrix()) {
## Return a matrix that is the inverse of 'x'
inverse <- x$getInverse()
if(!is.null(inverse)) {
message("getting cached inverted matrix")
return(inverse)
}
message("calculate inverted matrix and store it in cache")
data <- x$get()
inverse <- solve(data)
x$setInverse(inverse)
inverse
}