|
1 | 1 | ## Put comments here that give an overall description of what your
|
2 | 2 | ## functions do
|
3 | 3 |
|
4 |
| -## Write a short comment describing this function |
5 |
| - |
| 4 | +## This function returns a list of functions used to manage the matrix |
| 5 | +## content and the memorized inverse of the matrix |
6 | 6 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 7 | + m <- NULL # Contains the memorized inverse matrix |
| 8 | + set <- function(y){ # Reassign matrix |
| 9 | + x <<- y |
| 10 | + m <<- NULL # Memorized value is not valid anymore |
| 11 | + } |
| 12 | + get <- function() x # Return matrix content |
| 13 | + setinv <- function(inv) m <<- inv # Set a new memorized value |
| 14 | + getinv <- function() m # Return memorized inverse |
| 15 | + list(set = set, |
| 16 | + get = get, |
| 17 | + setinv = setinv, |
| 18 | + getinv = getinv) # Provides the interface for the functions |
8 | 19 | }
|
9 | 20 |
|
10 |
| - |
11 |
| -## Write a short comment describing this function |
12 |
| - |
| 21 | +## Returns the inverse of the matrix x (created with makeCacheMatrix). |
| 22 | +## If the inverse has already been calculated before for the same |
| 23 | +## matrix, the computation will not be repeated, but a memorized |
| 24 | +## version of the inverse will be returned |
13 | 25 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 26 | + m <- x$getinv() # Get currently memorized value |
| 27 | + ## If a memorized value exists, return it |
| 28 | + if(!is.null(m)){ |
| 29 | + message("returning memorized data") |
| 30 | + return(m) |
| 31 | + } |
| 32 | + ## If no memorized value exists |
| 33 | + data <- x$get() # Get current matrix represented by x |
| 34 | + m <- solve(data, ...) # Get the inverse |
| 35 | + x$setinv(m) # Memorize this inverse |
| 36 | + m # Return the inverse |
15 | 37 | }
|
0 commit comments