From 55c071d88e5d4522c5de419ca5d37dd2fea3c577 Mon Sep 17 00:00:00 2001 From: sshikari Date: Sun, 12 Jun 2016 22:21:48 -0400 Subject: [PATCH 1/2] Tested successfully --- cachematrix.R | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..406e51b335f 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 are used in tandom to create a matrix along with calculating and +## caching its inverse (a time consuming operation). +## +## Example usage +## a<-makeCacheMatrix() +## a$set(matrix(1:4,2,2)) +## cacheSolve(a) -## Write a short comment describing this function +## makeCacheMatrix takes a matrix as an argument, calls cacheSolve() to calculate and +## store the inverse of the matrix in cache. The method also contains functions to get the matrix and +## to get and set the inverse of the matrix. makeCacheMatrix <- function(x = matrix()) { - + inverseMatrix <- NULL + set <- function(y) { + x <<- y + inverseMatrix <<- NULL + } + get <- function() x + setInverseMatrix <- function(solve) inverseMatrix <<- solve + getInverseMatrix <- function() inverseMatrix + list(set = set, get = get, + setInverseMatrix = setInverseMatrix, + getInverseMatrix = getInverseMatrix) } -## Write a short comment describing this function +## cacheSolve takes in a matrix as an argument and returns the cached inverse if it exists, +## otherwise it calculates the inverse, stores it in cache and then returns it. -cacheSolve <- function(x, ...) { +cacheSolve <- function(x =, ...) { ## Return a matrix that is the inverse of 'x' + + inverseMatrix <- x$getInverseMatrix() + if(!is.null(inverseMatrix)) { + message("getting cached data") + return(inverseMatrix) + } + data <- x$get() + inverseMatrix <- solve(data, ...) + x$setInverseMatrix(inverseMatrix) + inverseMatrix } From ce8019a426816b32e4f25d459fc170130ac0afa7 Mon Sep 17 00:00:00 2001 From: sshikari Date: Tue, 14 Jun 2016 03:33:12 -0400 Subject: [PATCH 2/2] modified comments --- cachematrix.R | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 406e51b335f..96a591f2976 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -6,9 +6,8 @@ ## a$set(matrix(1:4,2,2)) ## cacheSolve(a) -## makeCacheMatrix takes a matrix as an argument, calls cacheSolve() to calculate and -## store the inverse of the matrix in cache. The method also contains functions to get the matrix and -## to get and set the inverse of the matrix. +## makeCacheMatrix constructs an empty matrix and stores it in a variable and contains methods to get / set a matrix +## to that variable. It also initializes a variable that can store the inverse of the matrix. makeCacheMatrix <- function(x = matrix()) { inverseMatrix <- NULL