From b677ce8caf15b1c598107fc8c0587ce9bc58398e Mon Sep 17 00:00:00 2001 From: Mathew Date: Sun, 26 Apr 2015 05:40:15 -0400 Subject: [PATCH 1/2] Programming Assignment 2 initial commit --- cachematrix.R | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..fc2d1721266 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,43 @@ -## Put comments here that give an overall description of what your -## functions do +## Provides functions to find inverse of a matrix. Finding inverse +## of a matrix is expensive. This optimized function computes the +## inverse and cache the result for subsequent calls. -## Write a short comment describing this function +## This function crates a special matrix object that can +## can cache it's inverse makeCacheMatrix <- function(x = matrix()) { - + ## variable to keep the inverse and initialized to NULL + i <- NULL + ## function to cache matrix + set <- function(y) { + x <<- y + i <<- NULL + } + ## function to return matrix + get <- function() x + ## function to cache inverse of the matrix + setinverse <- function(inverse) i <<- inverse + ## function to return the inverse + getinverse <- function() i + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } - -## Write a short comment describing this function +## This function computes the inverse of special matrix created by +## makeCacheMatrix above. If inverse is already created, this function +## returns the inverse from cahce. +## Return a matrix that is the inverse of 'x'. This method assumes +## matrix 'x' is squre invertible, otherwise throws error. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + i <- x$getinverse() + if(!is.null(i)){ + message("getting cached data") + return(i) + } + data <- x$get() + i <- solve(data, ...) + x$setinverse(i) + i } From e5f2fa4bffa27674bcd15fd5e81644a30bf550e5 Mon Sep 17 00:00:00 2001 From: Mathew Date: Sun, 26 Apr 2015 05:50:57 -0400 Subject: [PATCH 2/2] more comments --- cachematrix.R | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index fc2d1721266..a4ad915ae5b 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,7 +4,6 @@ ## This function crates a special matrix object that can ## can cache it's inverse - makeCacheMatrix <- function(x = matrix()) { ## variable to keep the inverse and initialized to NULL i <- NULL @@ -29,15 +28,22 @@ makeCacheMatrix <- function(x = matrix()) { ## returns the inverse from cahce. ## Return a matrix that is the inverse of 'x'. This method assumes ## matrix 'x' is squre invertible, otherwise throws error. - cacheSolve <- function(x, ...) { + ## First check if inverse of x is already computed i <- x$getinverse() if(!is.null(i)){ + ## Inverse of x is already compted. So, show a message + ## and return inverse from cache message("getting cached data") return(i) } + ## Inverse is not in cache. Get the matrix to compute inverse data <- x$get() + ## FInd the inverse by calling solve() + ## Solve computes inverse only for squre invertible matrix i <- solve(data, ...) + ## Cache the result x$setinverse(i) + ## return the inverse i }