forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
49 lines (38 loc) · 1.46 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
## These two functions together save calculation cost and reduce response time
## by caching the inverse of a matrix. The cached copy will be returned if found,
## otherwise the calculation is performed and the result is cached for future reuse
## makeCacheMatrix "decorates" a matrix
## with a list of functions that support get/set of matrix
## and get/set of its inverse
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setinverse <- function(inverse) inv <<- inverse
getinverse <- function() inv
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## cacheSolve takes a decorated matrix produced
## by makeCacheMatrix and returns the inverse
## of that matrix. If the inverse is null,
## this function computes the inverse, saves
## it in the cache, and returns the inverse
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inverse <- x$getinverse()
if (!is.null(inverse)) {
message("Getting cached data")
return(inverse)
}
## Cache miss. Calculate inverse and update cache
data <- x$get()
inverse <- solve(data)
x$setinverse(inverse)
## return inverse
inverse
}