forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
83 lines (63 loc) · 1.95 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
76
77
78
79
80
81
82
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
## This follows the same algorithm for cacheMean
makeCacheMatrix <- function(x = matrix()) {
## init data
m <- NULL
## sets the matrix and sets the cached inverse to null
set <- function(y) {
x <<- y
m <<- NULL
}
## get the current matrix
get <- function() x
## This actually caches the inverse matrix in a separate environment
setInverse <- function(inverse) m <<- inverse
## get the inverse matrix. If the inverse has not been calculated, then we return null
getInverse <- function() m
##
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## Write a short comment describing this function
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
m <- x$getInverse()
## If we've previously cached the inverse of the matrix
## m will NOT be null
if(!is.null(m)) {
## previously calculated and cached. Return the cached value.
message("getting cached data")
return(m)
}
## the inverse has not been calculated. Calculate it now.
data <- x$get()
m <- solve(data, ...)
x$setInverse(m)
m
}
testCode <- function() {
## build an invertable matrix
v<-makeCacheMatrix(rbind(c(1, -1/4), c(-1/4, 1)))
## make sure it built properly
print("matrix")
print(v$get())
cat("\n")
print("inverse is null")
print(v$getInverse())
cat("\n")
## init the cache
print("inverse is null. First time attempting. Inverse will be calculated.")
print(cacheSolve(v))
cat("\n")
## call it again to make sure it was retrieved from the cache
print("Retrieved from cache")
print(cacheSolve(v))
cat("\n")
## verify that we really did calculate the inverse by making sure we got an identity matrix
print("Should return identity matrix")
print(cacheSolve(v) %*% v$get())
cat("\n")
}