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 (42 loc) · 1.23 KB
/
Copy pathcachematrix.R
File metadata and controls
47 lines (42 loc) · 1.23 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
## Put comments here that give an overall description of what your
## functions do
## This object factory creates objects which take a matrix as input and additionally store the inverse
## calculated by the cacheSolve function
makeCacheMatrix <- function(x = matrix()) {
internal_matrix <- x
cached_inv <- NULL
list(
get = function() {
internal_matrix
},
set = function(input_matrix = matrix() ){
internal_matrix <<- input_matrix
cached_inv <<- NULL #since we're modifying the stored matrix, invalidate the cache
},
getcache = function() {
cached_inv
},
setcache = function(inv_input) {
if(is.null(cached_inv)) {
print("Setting Cache")
} else {
print("Overwriting Cache")
}
cached_inv <<- inv_input
}
)
}
## This function takes a makeCacheMatrix object as it's input to caluculate the inverse matrix
## storing the result in the original object
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv_mat <- x$getcache()
if(!is.null(inv_mat)){
print("Returning inverse matrix from cache")
} else {
print("Calculating Inverse of Matrix")
inv_mat=solve(x$get())
x$setcache(inv_mat)
}
inv_mat
}