forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathcachematrix.R
More file actions
148 lines (132 loc) · 3.45 KB
/
cachematrix.R
File metadata and controls
148 lines (132 loc) · 3.45 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
## some instructions for testing your makeCacheMatrix and cacheSolve functions
This is from this post: "Simple test matrices for the lexical scoping programming assignment"
https://www.coursera.org/learn/r-programming/discussions/weeks/3/threads/ePlO1eMdEeahzg7_4P4Vvg
R session:
>makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
# Set the matrix
set <- function(y) {
x <<- y
inv <<- NULL
}
# Get the matrix
get <- function() x
# Set the inverse
setInverse <- function(inverse) inv <<- inverse
# Get the inverse
getInverse <- function() inv
# Return a list of methods
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
}
> m1 <- matrix(c(1/2, -1/4, -1, 3/4), nrow = 2, ncol = 2)
> m1
[,1] [,2]
[1,] 0.50 -1.00
[2,] -0.25 0.75
>
> # You can use m1 to test your
> # makeCacheMatrix and cacheSolve functions.
> # Since the grading is done on the correctness of your
> # makeCacheMatrix and cacheSolve functions and your
> # comments on how they work, using this (or some other)
> # test matrix to check your code
> # before submitting it
> # is OK relative to the Coursera Honor Code.
> # (Checking code with test cases is always a good idea.)
> #
> # m1 was constructed (using very simple linear algebra, so
> # no references are given, almost surely the examples
> # in this post have been given many times before)
> # to have a simple marrix inverse, call it n1.
> # This means m1 %*% n1 (%*% is matrix multiply in R)
> # is the 2 row by 2 column Identity matrix I2
> I2 <- matrix(c(1,0,0,1), nrow = 2, ncol = 2)
> I2
[,1] [,2]
[1,] 1 0
[2,] 0 1
>
> # And so (by linear algebra) n1 %*% m1 is also equal I2.
> # (If n1 is the inverse of m1 then m1 is the inverse of n1.)
> # With m1 defined as above, n1 ( the inverse of m1) is
> n1 <- matrix(c(6,2,8,4), nrow = 2, ncol = 2)
> n1
[,1] [,2]
[1,] 6 8
[2,] 2 4
>
> # Checks:
> m1 %*% n1
[,1] [,2]
[1,] 1 0
[2,] 0 1
>
> n1 %*% m1
[,1] [,2]
[1,] 1 0
[2,] 0 1
>
> solve(m1)
[,1] [,2]
[1,] 6 8
[2,] 2 4
>
> solve(n1)
[,1] [,2]
[1,] 0.50 -1.00
[2,] -0.25 0.75
>
> # So if you have programmed your functions
> # correctly (in the file cachematrix.R),
> # (that, and your comments-explanation of how they work
> # are what you are graded on)
> # and sourced cachematrix.R so they are
> # available in your R session workspace, then doing
> #
> myMatrix_object <- makeCacheMatrix(m1)
>
> cacheSolve <- function(x, ...) {
# Check if the inverse is already calculated
inv <- x$getInverse()
if (!is.null(inv)) {
message("getting cached data")
return(inv)
}
# Calculate the inverse
mat <- x$get()
inv <- solve(mat, ...)
# Store the inverse in the cache
x$setInverse(inv)
inv
}
>
> # should return exactly the matrix n1
> cacheSolve(myMatrix_object)
[,1] [,2]
[1,] 6 8
[2,] 2 4
>
> # calling cacheSolve again should retrieve (not recalculate)
> # n1
> cacheSolve(myMatrix_object)
getting cached data
[,1] [,2]
[1,] 6 8
[2,] 2 4
>
> # you can use the set function to "put in" a new matrix.
> # For example n2
> n2 <- matrix(c(5/8, -1/8, -7/8, 3/8), nrow = 2, ncol = 2)
> myMatrix_object$set(n2)
> # and obtain its matrix inverse by
> cacheSolve(myMatrix_object)
[,1] [,2]
[1,] 3 7
[2,] 1 5
>
> cacheSolve(myMatrix_object)
getting cached data
[,1] [,2]
[1,] 3 7
[2,] 1 5
>