|
1 |
| -#'This module contains two functions which aid in caching the inverse of a matrix. |
2 |
| -#' \code makeCacheMatrix returns an object which can hold a cache. |
3 |
| -#' \code cachesolve is used to compute or retrieve the inverse. |
4 |
| - |
5 |
| -#' Create an object that contains a matrix and possibly a cached value of the inverse |
6 |
| -#' of the matrix. |
7 |
| -#' |
8 |
| -#' @param x the matrix to be contained and inverted |
9 |
| -#' @return a list containg the given matrix with functions to get, set the matrix and its inverse |
10 |
| -#' @seealso \code cacheSolve which actually computes the inverse and caches it |
11 |
| -makeCacheMatrix <- function(x = matrix()) { |
12 |
| - inverse <- NULL |
13 |
| - set <- function(y) { |
14 |
| - x <<- y |
15 |
| - m <<- NULL |
16 |
| - } |
17 |
| - get <- function() x |
18 |
| - setinverse <- function(inv) inverse <<-inv |
19 |
| - getinverse <- function() inverse |
20 |
| - list(set = set, get = get, |
21 |
| - setinverse = setinverse, |
22 |
| - getinverse = getinverse) |
23 |
| -} |
24 |
| - |
25 |
| - |
26 |
| -#' Compute or retrieve the inverse of a matrix |
27 |
| -#' |
28 |
| -#' @param x an object created by \code makeCacheMatrix |
29 |
| -#' @return the inverse of x |
30 |
| -#' Assumes that x is invertible, otherwise will cause an error. The matrix |
31 |
| -#' inverse is retrieved from x if a cached value is available, otherwise |
32 |
| -#' the inverse is computed and cached. |
33 |
| -cacheSolve <- function(x, ...) { |
34 |
| - i <- x$getinverse() |
35 |
| - if(!is.null(i)) { |
36 |
| - message("getting cached data") |
37 |
| - return(i) |
38 |
| - } |
39 |
| - data <- x$get() |
40 |
| - i <- solve(data, ...) |
41 |
| - x$setinverse(i) |
42 |
| - i |
43 |
| - |
44 |
| -} |
| 1 | +#'This module contains two functions which aid in caching the inverse of a matrix. |
| 2 | +#' \code makeCacheMatrix returns an object which can hold a cache. |
| 3 | +#' \code cachesolve is used to compute or retrieve the inverse. |
| 4 | + |
| 5 | +#' Create an object that contains a matrix and possibly a cached value of the inverse |
| 6 | +#' of the matrix. |
| 7 | +#' |
| 8 | +#' @param x the matrix to be contained and inverted |
| 9 | +#' @return a list containg the given matrix with functions to get, set the matrix and its inverse |
| 10 | +#' @seealso \code cacheSolve which actually computes the inverse and caches it |
| 11 | +makeCacheMatrix <- function(x = matrix()) { |
| 12 | + inverse <- NULL |
| 13 | + set <- function(y) { |
| 14 | + x <<- y |
| 15 | + m <<- NULL |
| 16 | + } |
| 17 | + get <- function() x |
| 18 | + setinverse <- function(inv) inverse <<-inv |
| 19 | + getinverse <- function() inverse |
| 20 | + list(set = set, get = get, |
| 21 | + setinverse = setinverse, |
| 22 | + getinverse = getinverse) |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +#' Compute or retrieve the inverse of a matrix |
| 27 | +#' |
| 28 | +#' @param x an object created by \code makeCacheMatrix |
| 29 | +#' @return the inverse of x |
| 30 | +#' Assumes that x is invertible, otherwise will cause an error. The matrix |
| 31 | +#' inverse is retrieved from x if a cached value is available, otherwise |
| 32 | +#' the inverse is computed and cached. |
| 33 | +cacheSolve <- function(x, ...) { |
| 34 | + i <- x$getinverse() |
| 35 | + if(!is.null(i)) { |
| 36 | + message("getting cached data") |
| 37 | + return(i) |
| 38 | + } |
| 39 | + data <- x$get() |
| 40 | + i <- solve(data, ...) |
| 41 | + x$setinverse(i) |
| 42 | + i |
| 43 | + |
| 44 | +} |
0 commit comments