diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..91c0ded0129 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -5,6 +5,21 @@ makeCacheMatrix <- function(x = matrix()) { + ainv <- NULL + + set <- function(newMatrix) + { + x <<- newMatrix + ainv <<- NULL + } + + get <- function() x + setInv <- function(inv) ainv <<- inv + getInv <- function() ainv + + list (set=set,get=get,setInv=setInv,getInv=getInv) + + } @@ -12,4 +27,15 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + + if (is.null(x$getInv())) + { + message("calculating Inverse ::") + inv <- solve(x$get(),...) + x$setInv(inv) + } + else + message("Getting Inv from cache:") + x$getInv() + }