|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## The objective of this functions is to create a special structure which is |
| 2 | +## able to store a matrix and cache the value of its inverse the first time |
| 3 | +## it is needed. From then on as long as the values of the elements of the matrix |
| 4 | +## remain the same there is no need to recompute its inverse. |
3 | 5 |
|
4 |
| -## Write a short comment describing this function |
| 6 | +## "makeCacheMatrix" : creates a special matrix that includes the value of the original matrix |
| 7 | +## itself and the value of its inverse. The functions returns a list of functions |
| 8 | +## that set and get the value of the matrix and its inverse respectively. |
5 | 9 |
|
6 | 10 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 11 | + ## initiate inv with a NULL value |
| 12 | + inv <- NULL |
| 13 | + |
| 14 | + ## set functions sets value y to x and sets inv to NULL again, |
| 15 | + ## thus, the inverse will have to be recomputed and cached again |
| 16 | + set <- function(y) { |
| 17 | + x <<- y |
| 18 | + inv <<- NULL |
| 19 | + } |
| 20 | + |
| 21 | + # returns the matrix |
| 22 | + get <- function() x |
| 23 | + |
| 24 | + # sets the value of the inverse |
| 25 | + setInverse <- function(inverse) inv <<- inverse |
| 26 | + |
| 27 | + # gets the value of the inverse |
| 28 | + getInverse <- function() inv |
| 29 | + |
| 30 | + # #return a list with functions to get and set the matrix and its inverse |
| 31 | + list(set = set, get = get, |
| 32 | + setInverse = setInverse, |
| 33 | + getInverse = getInverse) |
8 | 34 | }
|
9 | 35 |
|
10 | 36 |
|
11 |
| -## Write a short comment describing this function |
| 37 | +## "cacheSolve" : checks if the invere is already cached in memory. If it is the case |
| 38 | +## this value is returned. If not, the inverse is calculated and stored in memory |
| 39 | +## for future use. |
12 | 40 |
|
13 | 41 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 42 | + ## retrieves the value of the inverse stored in the CacheMatrix object |
| 43 | + inv <- x$getInverse() |
| 44 | + |
| 45 | + ## First we check if the value is NOT null, meaning that it has been |
| 46 | + ## calculated previusly. If that is the case, we return the value |
| 47 | + ## and we are done. |
| 48 | + if(!is.null(inv)) { |
| 49 | + message("getting cached data") |
| 50 | + return(inv) |
| 51 | + } |
| 52 | + |
| 53 | + ## If we reached this point, it means that the inverse of the matrix |
| 54 | + ## has not been calculated yet. We need to retrieve the original matrix |
| 55 | + ## from the CaheMatrix object |
| 56 | + matrix <- x$get() |
| 57 | + |
| 58 | + ## The inverse is calculated. All additional parameters are passed to |
| 59 | + ## the function |
| 60 | + inv <- solve(matrix, ...) |
| 61 | + |
| 62 | + ## We store the inverse for future uses |
| 63 | + x$setInverse(inv) |
| 64 | + |
| 65 | + ## the calculated inverse is returned once it has been stored |
| 66 | + inv |
15 | 67 | }
|
0 commit comments