forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
73 lines (58 loc) · 1.98 KB
/
cachematrix.R
File metadata and controls
73 lines (58 loc) · 1.98 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
# Programming assingment 2 for https://class.coursera.org/rprog-005
# Mitch Hendrickson
# Per the assignment, we write a couple of functions to demonstrate/duplicate
# the vector caching described in the assignemnt, but this time for matrices
## Create a "matrix" which is really a list of operation functions and has a place to
## stash a cache of the inverse. Based on makeVector(), and modified.
makeCacheMatrix <- function(x = matrix()) {
# we don't have an inverse cached at first
inv <- NULL
## learn/wrap the new matrix (y), toss any cached inverse
set <- function(y) {
x <<- y
inv <<- NULL
}
## return the wrapped matrix
get <- function() x
## stash a matrix assumed to be the inverse (i.e. "cache it")
setinverse <- function(newInv) inv <<- newInv
## retrieve the (cached inverse) matrix stored with setinverse
getinverse <- function() inv
## we really return a list of these accessors/mutators
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Inverse solver for our caching matrix which will use the cached version if it can.
## Based on cachemean from the assignment, and modified
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'.
# get and return the stashed inverse if we (x) has one
inv <- x$getinverse()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
# no stash; solve it and store the result in the cache; return that
m <- x$get()
inv <- solve(m, ...)
x$setinverse(inv)
inv
}
# testinverse <- function() {
# m <- matrix(c(1,0,5,2,1,6,3,4,0), nrow = 3, ncol = 3) # from http://www.purplemath.com/modules/mtrxinvr2.htm
# cm <- makeCacheMatrix(m)
# i <- cacheSolve(cm)
# print(i)
# i <- cacheSolve(cm)
# print(i)
# i <- cacheSolve(cm)
# print(i)
# cm <- makeCacheMatrix(i)
# i <- cacheSolve(cm)
# print(i)
# i <- cacheSolve(cm)
# print(i)
# i <- cacheSolve(cm)
# print(i)
# }