Skip to content

Commit

Permalink
cachematrix.R - making it available on git
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravi S Mula committed Aug 18, 2015
1 parent 412d4dd commit 6c2607b
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions cachematrix.R
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 6c2607b

Please sign in to comment.