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
39 lines (37 loc) · 1.4 KB
/
cachematrix.R
File metadata and controls
39 lines (37 loc) · 1.4 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
## create a special matrix with makeCacheMatrix. This returns a list of functions.
## Then you may solve the cache matrix using cacheSolve, which will retrieve
# the inverse w/o recomputing if it was already been computed
## Creates a vector of functions on the given matrix.
## Provides inverse caching
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL #inverse starts empty
set <- function(y = matrix()) {
x <<- y #reassign matrix in parent env
inv <<- NULL #must calc inverse again if reset
}
get <- function() {
x #just return matrix
}
setinverse <- function(inverse) {
inv <<- inverse #set inverse
}
getinverse <- function() {
inv #return the inverse
}
list(set=set,get=get,setinverse=setinverse,getinverse=getinverse)
}
## solve the matrix provided from the first method, checking for a cached value
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getinverse() #call getinverse -> returns NULL if not cached
if(is.null(inv)) {
data <- x$get() #get the value of matrix
inv <- solve(data) #returns inverse
x$setinverse(inv) #save inverse to vector obj
inv #return inverse to user
}
else {
message("getting cached inverse")
inv
}
}