vikasgupta1812
4/23/2017 - 5:00 PM

Caching Matrix in R

Caching Matrix in R

## These functions  build a simple caching system for inverse matrix computation


## This fuction creates a matrix caching object based on a closure
makeCacheMatrix <- function(x = matrix()) {
        xInv <- NULL
        set <- function(y) {
                x <<- y
                xInv <<- NULL
        }
        get <- function() x
        setinv <- function(inv) xInv <<- inv
        getinv <- function() xInv
        list(
                 set = set
                ,get = get
                ,setinv = setinv
                ,getinv = getinv
        )
}

## This function actually takes the previously-created caching object as an argument
## and returns the inverse of the underlying matrix. The first time it is called
## it will actually compute the inverse. All subsequent calls will return the 
## cached version (assuming the matrix hasn't been modified in the meantime)
cacheSolve <- function(x, ...) {
        xInv <- x$getinv()
        if(!is.null(xInv)) {
                message("getting cached data")
                return(xInv)
        }
        data <- x$get()
        xInv <- solve(data)
        x$setinv(xInv)
        xInv
}

Matrix Caching

The following demonstrates the use of the matrix caching functions.

source("cachematrix.R")
bla <- makeCacheMatrix()
bla$set( matrix( c(23,54,23,90), 2, 2 ) )

# for the first call, the inverse matrix is calculated
cacheSolve(bla)
##            [,1]        [,2]
## [1,]  0.10869565 -0.02777778
## [2,] -0.06521739  0.02777778
# for the second call, the inverse matrix is retrieved from cache
cacheSolve(bla)
## getting cached data
##            [,1]        [,2]
## [1,]  0.10869565 -0.02777778
## [2,] -0.06521739  0.02777778