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
92 lines (80 loc) · 2.7 KB
/
cachematrix.R
File metadata and controls
92 lines (80 loc) · 2.7 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
## Put comments here that give an overall description of what your
## functions do
## makeCacheMatrix is a support function that creates a cache for a Matrix
# and its inverse. It returns a list of functions to
# support matrix inversion by caching the previous matrix and its inverse
# thus if called twice with the same matrix it will quickly return the
# previous calculation
# usage: fnList <- makeCacheMatrix()
# NB: create a different function list for each matrix you wish to cache
# The returned list of functions:
# fnList$set(m): sets the input matrix to m and clear the stored inverse
# fnList$get(): returns the input matrix
# fnList$setinverse(m): cache the inverse
# fnList$getinverse(m): return the cached inverse
makeCacheMatrix <- function()
{
inputMatrix <- NULL
set <- function(m)
{
inputMatrix <<- m
inverseMatrix <<- NULL
}
get <- function() inputMatrix
setinverse <- function(inv) inverseMatrix <<- inv
getinverse <- function() inverseMatrix
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Find the inverse of the matrix in the cache
# Given an invertable matrix A find and cache the inverse by:
# fnList <- makeCacheMatrix() # create the cache, save function list
# fnList$set(A) # save input matrix
# B <- cacheSolve(A) # calculate the inverse
## subsequent calls to cacheSolve will return the cached results if
# fnList$set() has not been called again
cacheSolve <- function(x, ...)
{
## Return a matrix that is the inverse of 'x'
m <- x$getinverse()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data, ...)
x$setinverse(m)
m
}
# test the cache system
test <- function(n=100, ...)
{
# initialize the function list
fn.list <- makeCacheMatrix()
# get a test matrix
a <- matrix(rnorm(n*n), nrow=n)
# set the matrix in the cache manager
fn.list$set(a)
# get the inverse, timing it
start.time <- Sys.time();
b=cacheSolve(fn.list)
end.time <- Sys.time()
nocache.time <- end.time - start.time
# now time it again, this time answer is in cache
start.time <- Sys.time();
b=cacheSolve(fn.list)
end.time <- Sys.time()
cache.time <- end.time - start.time
# show timing difference
speedup = as.numeric(nocache.time) / as.numeric(cache.time)
print(sprintf("For %dx%d. Without cache: %.5f %s, cache: %.5f %s, speed up: %.1fX",
n, n, nocache.time, units(nocache.time), cache.time, units(cache.time),
speedup))
c <- a %*% b
I <- diag(n)
if (!isTRUE(all.equal(I, c)))
{
print("Error: result is not the inverse of input")
}
}