From a8e19946dd9985ba45bf550385f0621021941b8d Mon Sep 17 00:00:00 2001 From: procoder317 Date: Thu, 19 Jun 2014 12:53:03 +0530 Subject: [PATCH] complete working functions cacheSolve and makeCacheMatrix with comments and proper naming --- cachematrix.R | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..27e71cfc9fe 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,37 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { +################################################ +#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) } - -## Write a short comment describing this function - -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' -} +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 +} \ No newline at end of file