This repository was archived by the owner on Oct 16, 2020. It is now read-only.
forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
62 lines (55 loc) · 1.68 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
## Put comments here that give an overall description of what your
## functions do
## TSC 2014-05-18
## create a matrix type object that can get and set
## its own inverse (using the solve function)
## If the inverse has alread been computed, then getsolve() will return
## the inverse; if not, use setsolve() to compute the inverse and cache
## it inside the object
## TSC 2014-05-24
## Adding some testing
## Set up the matrices using the magic package
# m3 <- makeCacheMatrix(magic(3))
# m5 <- makeCacheMatrix(magic(5))
# m2001 <- makeCacheMatrix(magic(2001))
## Solve and check for the cache hit
# cacheSolve(m3)
# cacheSolve(m3)
## Timing on a small matrix
# system.time(cacheSolve(m5))
# system.time(cacheSolve(m5))
## Timing on a bigger matrix.
# system.time(cacheSolve(m2001))
# system.time(cacheSolve(m2001))
## 20 seconds on first; 0 seconds on the second
makeCacheMatrix <- function(x = matrix()) {
s <- NULL
set <- function(y) {
x <<- y
s <<- NULL
}
get <- function() x
setsolve <- function(solve) s <<- solve
getsolve <- function() s
list(set = set, get = get,
setsolve = setsolve,
getsolve = getsolve)
}
## TSC 2014-05-18
## use makeCacheMatrix to get the (possibly cached) inverse
## of a matrix.
## If the inverse has alread been computed, then getsolve() will return
## the inverse; if not, use setsolve() to compute the inverse and cache
## it inside the object
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
s <- x$getsolve()
if(!is.null(s)) {
message("getting cached data")
return(s)
}
data <- x$get()
s <- solve(data, ...)
x$setsolve(s)
s
}