Skip to content

Commit a8e1994

Browse files
procoder317procoder317
authored andcommitted
complete working functions cacheSolve and makeCacheMatrix with comments and proper naming
1 parent 7f657dd commit a8e1994

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

cachematrix.R

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
1+
################################################
2+
#Inverse of a matrix data computing and caching
3+
################################################
74

5+
makeCacheMatrix <- function(input_matrix = matrix()) {
6+
# used to store & return via set and get sub functions list
7+
# both matrix and inverse of matrix
8+
9+
# invert_mat used to cache data
10+
invert_mat <<- NULL
11+
set_matrix <- function(loaded_matrix) {
12+
# loading new matrix data
13+
14+
input_matrix <<- loaded_matrix
15+
invert_mat <<- NULL
16+
}
17+
18+
get_matrix <- function() input_matrix
19+
set_matrix_inverse <- function(inverse_val) invert_mat <<- inverse_val
20+
get_matrix_inverse <- function() invert_mat
21+
22+
list(set_matrix = set_matrix, get_matrix = get_matrix, set_matrix_inverse = set_matrix_inverse, get_matrix_inverse = get_matrix_inverse)
823
}
924

10-
11-
## Write a short comment describing this function
12-
13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
15-
}
25+
cacheSolve <- function(func_obj, ...) {
26+
# compute or fetching & storing inverse of matrix data cache
27+
28+
invert_mat <- func_obj$get_matrix_inverse()
29+
if(!is.null(invert_mat)) {
30+
message("getting cached data...")
31+
return(invert_mat)
32+
}
33+
mat_data <- func_obj$get_matrix()
34+
invert_mat <- solve(mat_data)
35+
func_obj$set_matrix_inverse(invert_mat)
36+
invert_mat
37+
}

0 commit comments

Comments
 (0)