diff --git a/.RData b/.RData new file mode 100644 index 00000000000..180aeffd09f Binary files /dev/null and b/.RData differ diff --git a/.Rhistory b/.Rhistory new file mode 100644 index 00000000000..525d439f9a6 --- /dev/null +++ b/.Rhistory @@ -0,0 +1,13 @@ +setwd("C:/Users/Chandru/ProgrammingAssignment2") +source("cachematrix.R"") +"" +source("cachematrix.R") +mat <- matrix(rnorm(1:(16)), nrow=4, ncol=4) +a <- makeCacheMatrix(mat) +cacheSolve(a) +cacheSolve(a) +source("cachematrix.R") +mat <- matrix(rnorm(1:(16)), nrow=4, ncol=4) +a <- makeCacheMatrix(mat) +cacheSolve(a) +cacheSolve(a) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..01365e7627c 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,43 @@ -## Put comments here that give an overall description of what your -## functions do +## The following functions speed up to compute repeated matrix inversion +## for a single matrix using caching technique -## Write a short comment describing this function + +## This function creates a special "matrix" object that can cache its inverse. makeCacheMatrix <- function(x = matrix()) { + im <- NULL ## im for inversion matrix + + set <- function(y) { ## set the matrix + x <<- y + im <<- NULL + } + get <- function() x ## get the matrix + setinv <- function(solve) im <<- solve ## set inversion matrix + getinv <- function() im ## get inversion matrix + + list(set = set, ## store the functions + get = get, + setinv = setinv, + getinv = getinv) } -## Write a short comment describing this function +## This function computes the inverse of the special "matrix" returned by makeCacheMatrix above. +##If the inverse has already been calculated (and the matrix has not changed), +##then the cachesolve should retrieve the inverse from the cache. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + + im <- x$getinv() ## get inversion from cache + + if(!is.null(im)){ ## check if it is not NULL + message("getting cached data") + return(im) ## return the cache data and save some computation + } + data <- x$get() ## if we are here we need compute + im <- solve(data, ...) ## compute the inverse + x$setinv(im) ## save it to cache + im ## return the computed inverse matrix }