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 diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..e31e357548e 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,41 @@ -## 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 +} \ No newline at end of file