diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..425b4aacb74 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,48 @@ -## Put comments here that give an overall description of what your -## functions do +## these functions allow for calculating the inverse of a matrix and stores the final output +## this allows for data to be retrieved instead of recalculating when a matrix has already been previously calculated -## Write a short comment describing this function +## makeCacheMatrix() allows the user to create a matrix makeCacheMatrix <- function(x = matrix()) { + inv <- NULL + + # setter and getter for original matrix + + set <- function(y) { + x <<- y + inv <<- NULL + } + + get <- function() x + + # setter and getter for inverse + set_inverse <- function(solve) inv <<- solve + get_inverse <- function() inv + list(set = set, get = get, + set_inverse = set_inverse, + get_inverse = get_inverse) } -## Write a short comment describing this function +## cacheSolve() finds the inverse of the matrix +## it gives the cached result if previously available cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + + ## finds stored inverse and returns it if possible + + inv <- x$get_inverse() + if(!is.null(inv)) { + message("Getting cached inverse matrix...") + return(inv) + } + + ## calculates new matrix if unavailable + + data <- x$get() + inv <- solve(data, ...) + x$set_inverse(inv) + + inv }