From 7622dfd80d5a0374148c6b87d11f87a524c7096f Mon Sep 17 00:00:00 2001 From: devendermishra Date: Sat, 26 Jul 2014 21:12:25 +0530 Subject: [PATCH 1/4] Implementing the solution --- cachematrix.R | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..f6c31841536 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,40 @@ ## Put comments here that give an overall description of what your ## functions do -## Write a short comment describing this function +##makeCacheMatrix creates a special matrix with list of the following +##function +##1. Set the matrix +##2. Get the matrix +##3. Set the Inverse of the matrix +##4. Get the Inverse of the matrix makeCacheMatrix <- function(x = matrix()) { + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinv <- function(solve) m <<- solve + getinv <- function() m + list(set = set, get = get, + setinv = setinv, + getinv = getinv) } - -## Write a short comment describing this function - +##cacheSolve find the inverse of special matrix created by makeCacheMatrix +##If the inverse is already calculated, it gives the precalculated inverse. +##Else, it computes the inverse and stores it in special matrix. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + m <- x$getinv() + if(!is.null(m)) { + message("getting cached matrix inverse") + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setinv(m) + m } From a6e7c0c0b0c80e78a0da3dd443f602958c776e05 Mon Sep 17 00:00:00 2001 From: devendermishra Date: Sat, 26 Jul 2014 21:16:16 +0530 Subject: [PATCH 2/4] Adding commet how to test or use? --- cachematrix.R | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cachematrix.R b/cachematrix.R index f6c31841536..3c68f030edd 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -38,3 +38,9 @@ cacheSolve <- function(x, ...) { x$setinv(m) m } + +###Example: How to test or use? +### c<-rbind(c(1,-1),c(-1,1)) +### y<-makeCacheMatrix(c) +### cacheSolve(y) +### Next time, it will give the matrix inverse from cache. From b3254b0df245dba272ec991e72446f819a1e32fe Mon Sep 17 00:00:00 2001 From: devendermishra Date: Sat, 26 Jul 2014 21:32:06 +0530 Subject: [PATCH 3/4] Adding comment in the function --- cachematrix.R | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 3c68f030edd..13dae7ad5f7 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -10,14 +10,28 @@ ##4. Get the Inverse of the matrix makeCacheMatrix <- function(x = matrix()) { + #m is the cached result of solve operation m <- NULL - set <- function(y) { + + #set and get the matrix respectively + #matrix + + set <- function(y) { x <<- y m <<- NULL } + get <- function() x - setinv <- function(solve) m <<- solve + + #setinv and getinv are to set/get the inverse of the matrix + # or any other result which caller wants to store respectively + + #Assign the calculated solution + setinv <- function(solution) m <<- solution + #Return the calculated solution getinv <- function() m + + list(set = set, get = get, setinv = setinv, getinv = getinv) @@ -29,10 +43,14 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' m <- x$getinv() + + #It founds hte precomputed inverse if(!is.null(m)) { message("getting cached matrix inverse") return(m) } + + #Else, compute the inverse data <- x$get() m <- solve(data, ...) x$setinv(m) From a47d5698554e2c46c5b9970502385c49a8986406 Mon Sep 17 00:00:00 2001 From: devendermishra Date: Sat, 26 Jul 2014 21:34:30 +0530 Subject: [PATCH 4/4] Adding comment in the function --- cachematrix.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 13dae7ad5f7..35d7e14e331 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -8,6 +8,7 @@ ##2. Get the matrix ##3. Set the Inverse of the matrix ##4. Get the Inverse of the matrix + makeCacheMatrix <- function(x = matrix()) { #m is the cached result of solve operation @@ -40,11 +41,12 @@ makeCacheMatrix <- function(x = matrix()) { ##cacheSolve find the inverse of special matrix created by makeCacheMatrix ##If the inverse is already calculated, it gives the precalculated inverse. ##Else, it computes the inverse and stores it in special matrix. + cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' m <- x$getinv() - #It founds hte precomputed inverse + #It founds the precomputed inverse if(!is.null(m)) { message("getting cached matrix inverse") return(m) @@ -61,4 +63,4 @@ cacheSolve <- function(x, ...) { ### c<-rbind(c(1,-1),c(-1,1)) ### y<-makeCacheMatrix(c) ### cacheSolve(y) -### Next time, it will give the matrix inverse from cache. +### On the next call to cacheSolve(y), it will give the matrix inverse from cache.