diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..c5fb849c7de 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,52 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +#' A package supplying matrix closures with the purpose of caching their +#' inverse. +#' +#' @name cacheMatrix-package +#' @docType package +#' Create a cacheable matrix closure. +#' +#' This function creates a closure around a matrix with the ability to cache the +#' matrix inverse. +#' +#' @param x Existing matrix. +#' @return A CacheMatrix closure. +#' +#' @export +#' @examples +#' xc <- makeCacheMatrix(matrix(c(1, 0, 0, 2), nrow=2, ncol=2)) makeCacheMatrix <- function(x = matrix()) { - + inverse <- NULL + set <- function(y) { + x <<- y + inverse <<- NULL + } + get <- function() x + getInverse <- function() inverse + setInverse <- function(inverse) inverse <<- inverse + list(set = set, get = get, + getInverse = getInverse, + setInverse = setInverse) } - -## Write a short comment describing this function - +#' Get the solution for the specified cache matrix. +#' +#' If the solution has been cached, the cached value is returned, otherwise it +#' is calculated. +#' +#' @param x CacheMatrix to be solved (\code{\link{makeCacheMatrix}}). +#' @param ... Further arguments passed to solve. +#' @return The solution for the specified matrix. +#' +#' @export +#' @examples +#' xc <- makeCacheMatrix(matrix(c(1, 0, 0, 2), nrow=2, ncol=2)) +#' cacheSolve(xc) cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + i <- x$getInverse() + if (is.null(i)) { + i <- solve(x$get(), ...) + x$setInverse(i) + } + i }