diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..807ea251739 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.Rproj.user +.Rhistory +.RData diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..6d7199421e5 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,68 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +# cachematrix.R +# +# 2 functions to assist in caching the inverse of a matrix +# +# Usage: +# You first create a makeCacheMatrix object from a matrix +# then you solve its inverse using cacheSolve makeCacheMatrix <- function(x = matrix()) { - + # Wrap the matrix object provided and allow the caching of it's inverse + # + # Args: + # x: a matrix object whose inverse can be calculated + # + # Returns: + # A list of four functions (set, get, setInverse, getInverse) + + inverse <- NULL + + # replace the matrix with a new one + set <- function(y) { + x <<- y + inverse <<- NULL + } + + # get the matrix object + get <- function() x + + # set the inverse of the matrix + setInverse <- function(inverseMatrix) inverse <<- inverseMatrix + + # get the saved inverse or NULL + getInverse <- function() inverse + + # return object + list(set = set, get = get, + setInverse = setInverse, + getInverse = getInverse) } - -## Write a short comment describing this function - cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' -} + # Compute the inverse of the matrix provided by the makeCacheMatrix object + # + # Args: + # x: makeCacheMatrix object + # + # Returns: + # The inverse of the provided matrix + # + # Todo: + # write guard clause against x != matrix + + inverse <- x$getInverse() + + # Returns the cached inverse if it is available + if(!is.null(inverse)) { + message("getting cached inverse matrix") + return(inverse) + } + + # calculate the inverse + data <- x$get() + inverse <- solve(data, ...) + x$setInverse(inverse) + + # return the inverse + inverse +} \ No newline at end of file diff --git a/peerAssignment.Rproj b/peerAssignment.Rproj new file mode 100644 index 00000000000..3715d52d2dc --- /dev/null +++ b/peerAssignment.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 4 +Encoding: UTF-8 + +RnwWeave: knitr +LaTeX: pdfLaTeX