forked from lgreski/datasciencectacontent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestMakeCacheMatrix.R
56 lines (42 loc) · 1.66 KB
/
testMakeCacheMatrix.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
#
# rprog-testCacheMatrix.R
#
# test script for makeCacheMatrix.R
#
# approach 1: create a matrix object, then use it as input to cacheSolve()
a <- makeCacheMatrix(matrix(c(-1, -2, 1, 1), 2,2))
cacheSolve(a)
# call cacheSolve(a) a second time to trigger the "getting cached inverse" message
cacheSolve(a)
# multiply the matrix by inverse, resulting in identity matrix
a$get() %*% a$getsolve()
# reset a with another matrix to clear out cached value
a$set(matrix(c(2,3,2,2),2,2))
# confirm that a has new data and that cache is NULL
a$get()
a$getsolve()
# rerun cache solve, note that "getting cached inverse" does not print,
# and that we get a different result
cacheSolve(a)
# approach 2: use makeCacheMatrix() as the input argument to cacheSolve()
# note that the argument to cacheSolve() is a different object
# than the argument to the first call of cacheSolve()
cacheSolve(makeCacheMatrix(matrix(c(-1, -2, 1, 1), 2,2)))
# try a non-invertible matrix
b <- makeCacheMatrix(matrix(c(0,0,0,0),2,2))
cacheSolve(b)
# illustrate getting the memory locations
a <- makeCacheMatrix(matrix(c(-1, -2, 1, 1), 2,2))
tracemem(a)
tracemem(matrix(c(-1, -2, 1, 1), 2,2))
# approach 2: use makeCacheMatrix() as the input argument to cacheSolve()
# note that the argument to cacheSolve() is a different object
# than the argument to the first call of cacheSolve()
cacheSolve(makeCacheMatrix(matrix(c(-1, -2, 1, 1), 2,2)))
# illustrate getting the memory locations
a <- makeCacheMatrix(matrix(c(-1, -2, 1, 1), 2,2))
tracemem(a)
tracemem(matrix(c(-1, -2, 1, 1), 2,2))
# test non-matrix input: should return "not a matrix" error
a$set(1:5)
cacheSolve(a)