diff --git a/cachematrix.R b/cachematrix.R old mode 100644 new mode 100755 index a50be65aa44..2de70597521 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,45 @@ -## Put comments here that give an overall description of what your -## functions do +library("MASS") -## Write a short comment describing this function +## These functions are used to calculates the inverse of a matrix +## stores the result to avoid computations if the result is needed +## again by some other function to make the function more efficient -makeCacheMatrix <- function(x = matrix()) { +## The function, `makeCacheMatrix` creates a list containing a function to +## 1. set the value of the matrix +## 2. get the value of the matrix +## 3. set the value of the inverse +## 4. get the value of the inverse +makeCacheMatrix <- function(x = matrix()) { + inv <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinv <- function(inverse) inv <<- inverse + getinv <- function() inv + list(set = set, get = get, + setinv = setinv, + getinv = getinv) } -## Write a short comment describing this function +## Reads a list created by `makeCacheMatrix` function, +## Checks whether Inverse is calculated or not, +## If calculated , avoids computation and returns the value +## else computes inverse and stores it to avoid recomputing. + +## NOTE: Uses `ginv` function in the package MASS for inverse calculation cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + i <- x$getinv() + if(!is.null(i)) { + message("getting cached data...") + return(i) + } + data <- x$get() + i <- ginv(data, ...) + x$setinv(i) + i }