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
41 lines (31 loc) · 1.27 KB
/
cachematrix.R
File metadata and controls
41 lines (31 loc) · 1.27 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
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
# makeCacheMatrix: create a matrix that can store itself and a cached copy of its inverse once its computed
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL # cache for inverse
set <- function(y) { # replace matrix and reset cache
x <<- y
inv <<- NULL
}
get <- function() x # return current matrix
setinverse <- function(inverse) # store computed inverse
inv <<- inverse
getinverse <- function() inv # return cached inverse (or NULL)
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Write a short comment describing this function
# cacheSolve: returns the cached inverse or computes & caches it
cacheSolve <- function(x, ...) {
inv <- x$getinverse() # try cache first
if (!is.null(inv)) {
message("getting the cached inverse")
return(inv)
}
mat <- x$get() # fetch matrix
inv <- solve(mat, ...) # compute inverse (assumes invertible)
x$setinverse(inv) # cache result
inv # return inverse
}