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
37 lines (30 loc) · 1.24 KB
/
cachematrix.R
File metadata and controls
37 lines (30 loc) · 1.24 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
################################################
#Inverse of a matrix data computing and caching
################################################
makeCacheMatrix <- function(input_matrix = matrix()) {
# used to store & return via set and get sub functions list
# both matrix and inverse of matrix
# invert_mat used to cache data
invert_mat <<- NULL
set_matrix <- function(loaded_matrix) {
# loading new matrix data
input_matrix <<- loaded_matrix
invert_mat <<- NULL
}
get_matrix <- function() input_matrix
set_matrix_inverse <- function(inverse_val) invert_mat <<- inverse_val
get_matrix_inverse <- function() invert_mat
list(set_matrix = set_matrix, get_matrix = get_matrix, set_matrix_inverse = set_matrix_inverse, get_matrix_inverse = get_matrix_inverse)
}
cacheSolve <- function(func_obj, ...) {
# compute or fetching & storing inverse of matrix data cache
invert_mat <- func_obj$get_matrix_inverse()
if(!is.null(invert_mat)) {
message("getting cached data...")
return(invert_mat)
}
mat_data <- func_obj$get_matrix()
invert_mat <- solve(mat_data)
func_obj$set_matrix_inverse(invert_mat)
invert_mat
}