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 (36 loc) · 1.16 KB
/
Copy pathcachematrix.R
File metadata and controls
41 lines (36 loc) · 1.16 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
## Pair of functions to take a matrix and store it and its inverse
## in the parent/calling environment. If the inverse has already
## been calculated, it will be pulled from cache for efficiency.
## Example syntax: t <- makeCacheMatrix(m)
## cacheSolve(t)
## Returns a list of four functions in the parent/calling scope to
## set and get a matrix and its inverse
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set_m <- function(y) {
x <<- y
inv <<- NULL
}
get_m <- function() x
set_inv_m <- function(solve) inv <<- solve
get_inv_m <- function() inv
list(set_m = set_m, get_m = get_m,
set_inv_m = set_inv_m,
get_inv_m = get_inv_m)
}
## Using the matrix and functions defined in makeCacheMatrix, get the
## matrix inverse from cache if it exists, if not, calculate and set
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
invm <- x$get_inv_m()
## Pull from cache if inverse already exists
if(!is.null(invm)) {
message("pulling from cache")
return(invm)
}
## Compute inverse if cache does not exist
data <- x$get_m()
invm <- solve(data, ...)
x$set_inv_m(invm)
invm
}