diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..5f9758dc35f 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,38 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - +# creates a list that can be used to cache matrix solving +# +# Args: +# x: an invertable matrix +# Returns: +# list containing methods to solve and cache a matrix makeCacheMatrix <- function(x = matrix()) { - + s <- NULL + set <- function(y) { + x <<- y + s <<- NULL + } + get <- function() x + setSolved <- function(solved) s <<- solved + getSolved <- function() s + list(set = set, get = get, + setSolved = setSolved, + getSolved = getSolved) } -## Write a short comment describing this function +# inverts a matrix, with cacheing +# Args: +# x: a 'matrix' returned by makeCacheMatrix +# Returns: +# Inverse of matrix x cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + s <- x$getSolved() + if(!is.null(s)) { + message("getting cached data") + return(s) + } + data <- x$get() + s <- solve(data, ...) + x$setSolved(s) + s }