From a26b25283758b20d161c870df0b7c3fe0e7331d3 Mon Sep 17 00:00:00 2001 From: Shane Dowling Date: Thu, 22 Jan 2015 21:53:19 +0000 Subject: [PATCH 1/3] cachematrix.R --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..9b8377756b3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.Rproj.user +.Rhistory +.RData +*.Rproj From 5f8e0409305c3a54e05b54c94adaa19064342298 Mon Sep 17 00:00:00 2001 From: Shane Dowling Date: Thu, 22 Jan 2015 21:55:50 +0000 Subject: [PATCH 2/3] Adding solution to the code --- cachematrix.R | 52 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..895a650306d 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,47 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { +## This code provides a cacheable way to +## invert matrices. +## This is a object that takes in a matrix +## and provides functions to return that matrix and also +## caches and return the inverse of the matrix +makeCacheMatrix <- function(mat = matrix()) { + inv = NULL + # Set the internal variable mat with supplied matrix + set <- function(y) { + mat <<- y + inv <<- NULL + } + # Return matrix + get <- function() mat + # Set the internal variable inv with the inverse + setinverse <- function(inverse) inv <<- inverse + # Return matrix inverse + getinverse <- function() inv + # Return the list of callable arguments + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } - -## Write a short comment describing this function - +## This function takes in a makeCacheMatrix object and returns it's inverse cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + # Tries to get a cached copy and return + inverse <- x$getinverse() + if(!is.null(inverse)) { + message("getting cached data") + return(inverse) + } + # If none gets the matrix + data <- x$get() + # Calculates it's inverse + inverse <- solve(data, ...) + # And sets the inverse on the makeCacheMatrix container + x$setinverse(inverse) + inverse } +mat = matrix(c(4,3,3,2), 2) +print(mat) +cacheMat = makeCacheMatrix(mat) +inverseMat = cacheSolve(cacheMat) +inverseMat = cacheSolve(cacheMat) +print(inverseMat) \ No newline at end of file From 0f5415bf8f2280597e498605c034ed0f9158eef7 Mon Sep 17 00:00:00 2001 From: Shane Dowling Date: Thu, 22 Jan 2015 21:56:44 +0000 Subject: [PATCH 3/3] Tidying and removing calling code --- cachematrix.R | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 895a650306d..e31e357548e 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -38,10 +38,4 @@ cacheSolve <- function(x, ...) { # And sets the inverse on the makeCacheMatrix container x$setinverse(inverse) inverse -} -mat = matrix(c(4,3,3,2), 2) -print(mat) -cacheMat = makeCacheMatrix(mat) -inverseMat = cacheSolve(cacheMat) -inverseMat = cacheSolve(cacheMat) -print(inverseMat) \ No newline at end of file +} \ No newline at end of file