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
47 lines (39 loc) · 1.3 KB
/
cachematrix.R
File metadata and controls
47 lines (39 loc) · 1.3 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
## The provided functions create a matrix which knows how to
## cache it's inverse when needed.
## Usage:
## m = makeCacheMatrix(mymatrix)
## cacheSolve(m) # computes and caches on the first call
## cacheSolve(m) # returns cache on subsequent calls
## Make a version of the matrix which is used to save the cache of the inverse
makeCacheMatrix <- function(x = matrix()) {
inverse.cache <- NULL
set <- function(y) {
x <<- y
inverse.cache <<- NULL
}
get <- function() x
setinverse <- function(inverse) inverse.cache <<- inverse
getinverse <- function() inverse.cache
# return the list of functions that allow the cache to be manipulated
list(set = set,
get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Return the cached inverse of a matrix or
## compute and cache the inverse if not cached.
cacheSolve <- function(x, ...) {
# see if we have the inverse cached
inverse <- x$getinverse()
if(!is.null(inverse)) {
message("using cached inverse")
} else {
message("computing and caching inverse")
# not cached, get the matrix and invert it
data <- x$get()
inverse <- solve(data, ...)
# save the inverse in the cache
x$setinverse(inverse)
}
inverse
}