diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..764cfba6676 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,66 @@ -## Put comments here that give an overall description of what your -## functions do +## makeCacheMatrix create list of fuction to set matrix and get matrix and +## set solve for Matrix and get the solve matrix -## Write a short comment describing this function +## Create square Matix +## minv<-matrix(c(3,2,5,6),nrow=2,ncol=2) -makeCacheMatrix <- function(x = matrix()) { - -} +## cacheMat<-makeCacheMatrix(minv) +##cacheMat$setMat()------> Set Matrix value +##cacheMat$getMat()------> get Matrix value +##cacheMat$setSolve()----> set Solve Matrix +##cacheMat$getSolve()----> get Solve matrix -## Write a short comment describing this function +makeCacheMatrix <- function(x = matrix()) +{ + ## Set the value of the matrix which we pass through y, it will store + ## in x (lexical scoping) + + sol <- NULL + setMat <- function(y) + { + x <<- y + sol <<- NULL + } + + ## Get the value of the matrix x that store in setMat + getMat <- function() x + + ## Set the solve function + setSolve <- function(solve) sol <<- solve + + ## store solve matrix value + getSolve<- function() sol + list(setMat = setMat, getMat = getMat, setSolve = setSolve, + getSolve= getSolve) + +} +## CacheSolve returns the value of solve matrix +## cacheSolve(cacheMat) contains all the list function created in cacheMat +## cacheSolve(cacheMat) -------> returns inverse of matrix -cacheSolve <- function(x, ...) { +cacheSolve <- function(x, ...) +{ + sol <- x$getSolve() + + ## if the solve function empty it returns + ## function (a, b, ...) + ## UseMethod("solve") + ## + ## + + if(!is.null(sol)) { + message("getting cached data") + return(sol) + } + data <- x$getMat() + + ## solve(a, b, ...) is default. But we are going to compute for square matrix + ## solve(a=data) is used for inverse of square matrix + + sol <- solve(data, ...) ## Return a matrix that is the inverse of 'x' + x$setSolve(sol) + sol + }