From 52a82a67de78fe1f99d4db1148456d4af96ecdb2 Mon Sep 17 00:00:00 2001 From: Hiep Khuu Date: Sat, 23 Aug 2014 15:54:28 -0700 Subject: [PATCH 1/3] Added makeCacheMatrix() and cacheSolve() functions to find inverse of an invertible matrix input --- cachematrix.R | 57 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..2081b3339fd 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,60 @@ -## Put comments here that give an overall description of what your -## functions do +## The following functions below will help find the inverse of a matrix, if it is +## invertible, given a matrix object. +## A matrix object can be created using makeCacheMatrix() function. The created +## matrix object has capability to store both the matrix input and matrix inverse result. +## In addition, if the matrix object already had the cached inverse result, the cacheSolve() +## function will just return it without spending expensive resource to re-compute it. -## Write a short comment describing this function -makeCacheMatrix <- function(x = matrix()) { +## This function creates a special "matrix" object that can cache its inverse +makeCacheMatrix <- function(x = matrix()) { + invMatrix <- NULL + + ## Define a set() function to store matrix input data + set <- function(y){ + x <<- y + invMatrix <<- NULL + } + + ## Define a get() function to return matrix input data + get <- function() x + + ## Define a getInverse() function to return the cached + ## matrix inverse result, if available + getInverse <- function() invMatrix + + ## Define a setInverse() function to store cached result + ## of inverse matrix of X + setInverse <- function(invX){ + invMatrix <<- invX + } + + ## Define list of functions to be accessible + list(get = get, set = set, + getInverse = getInverse, setInverse = setInverse) + } -## 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 cacheSolve should retrieve the inverse from the cache. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Return a matrix that is the inverse of 'x' + invMatrix <- x$getInverse() + + if (!is.null(invMatrix)){ + message("Getting cached matrix inverse result") + return(invMatrix) + } + + ## Compute the matrix Inverse of X, assuming X is invertible + invMatrix <- solve(x$get()) + + ## Cache the inverse matrix of X for next retrieval + x$setInverse(invMatrix) + + invMatrix } From 821b85f933ef39762bc97f4445ecde0f1583699c Mon Sep 17 00:00:00 2001 From: Hiep Khuu Date: Sat, 23 Aug 2014 16:22:08 -0700 Subject: [PATCH 2/3] Clean up comments --- cachematrix.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index 2081b3339fd..712ae51e119 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -5,7 +5,6 @@ ## In addition, if the matrix object already had the cached inverse result, the cacheSolve() ## function will just return it without spending expensive resource to re-compute it. - ## This function creates a special "matrix" object that can cache its inverse makeCacheMatrix <- function(x = matrix()) { @@ -56,5 +55,6 @@ cacheSolve <- function(x, ...) { ## Cache the inverse matrix of X for next retrieval x$setInverse(invMatrix) + ## Display the inverse matrix on console output invMatrix } From 420d9188ec342acb67157c255a9ec5b035475367 Mon Sep 17 00:00:00 2001 From: Hiep Khuu Date: Sat, 23 Aug 2014 16:23:03 -0700 Subject: [PATCH 3/3] Clean up comments --- cachematrix.R | 1 - 1 file changed, 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index 712ae51e119..c8629ab1cd8 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -55,6 +55,5 @@ cacheSolve <- function(x, ...) { ## Cache the inverse matrix of X for next retrieval x$setInverse(invMatrix) - ## Display the inverse matrix on console output invMatrix }