|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## Data structure to store a matrix and its inverse, cached. |
| 2 | +# |
| 3 | +# Arguments: matrix - a matrix |
| 4 | +# Returns: a list of functions to set and get a matrix and its (cached) inverse |
3 | 5 |
|
4 |
| -## Write a short comment describing this function |
| 6 | +makeCacheMatrix <- function(matrix = matrix()) { |
| 7 | + # Create empty object to store inversed matrix |
| 8 | + cachedInverse = NULL |
| 9 | + |
| 10 | + # Function to get the stored matrix |
| 11 | + getMatrix <- function() matrix; |
| 12 | + |
| 13 | + # Function to update the given matrix. |
| 14 | + # It also resets the cached value |
| 15 | + setMatrix <- function(newMatrix) { |
| 16 | + matrix <<- newMatrix |
| 17 | + cachedInverse <<- NULL |
| 18 | + } |
| 19 | + |
| 20 | + # Function to get cached inverse |
| 21 | + getInverse <- function() cachedInverse |
| 22 | + |
| 23 | + # Function to update the cached inverse |
| 24 | + setInverse <- function(newInversed) cachedInverse <<- newInversed |
| 25 | + |
| 26 | + # Return a list of functions |
| 27 | + # Those functions will have the environment in which they |
| 28 | + # were defined associated with them, thus exposing the correct |
| 29 | + # objects "matrix" and "cachedInverse". |
| 30 | + # |
| 31 | + # Only possible due to lexical scoping in R. If R used dynamic |
| 32 | + # scoping, getInverse() would look for object cachedInverse in |
| 33 | + # environment in which it is called, and not in the one where |
| 34 | + # it was defined. |
| 35 | + list(getMatrix = getMatrix, setMatrix = setMatrix, |
| 36 | + getInverse = getInverse, setInverse = setInverse) |
| 37 | +} |
5 | 38 |
|
6 |
| -makeCacheMatrix <- function(x = matrix()) { |
7 | 39 |
|
8 |
| -} |
| 40 | +## Function to calculate the inverse of the matrix special |
| 41 | +## data structure defined on the previous function. |
| 42 | +## First it looks on its cache to see if it has already |
| 43 | +## been calculated. Otherwise it calculates and stores it there. |
| 44 | +# |
| 45 | +# Arguments: matrix - list returned from makeCacheMatrix() |
| 46 | +# Returns: inverse of the matrix |
| 47 | +# Side effects: update cached version on input data structure if |
| 48 | +# it was empty. |
9 | 49 |
|
| 50 | +cacheSolve <- function(matrix, ...) { |
| 51 | + ## Return a matrix that is the inverse of 'x' |
| 52 | + inverse = matrix$getInverse(); |
| 53 | + |
| 54 | + # Check if we got something |
| 55 | + if(!is.null(inverse)) { |
| 56 | + # If we did, return it |
| 57 | + message("Retrieving cached version") |
| 58 | + return(inverse) |
| 59 | + } |
| 60 | + |
| 61 | + # Otherwise, we got nothing. We have to: |
| 62 | + message("Cache not found. Calculating inverse") |
| 63 | + |
| 64 | + # Get the matrix |
| 65 | + data <- matrix$getMatrix() |
| 66 | + |
| 67 | + # Calculate the inverse |
| 68 | + inverse <- solve(data) |
| 69 | + |
| 70 | + # Store it on cache |
| 71 | + matrix$setInverse(inverse) |
| 72 | + |
| 73 | + # Return it |
| 74 | + inverse |
| 75 | +} |
10 | 76 |
|
11 |
| -## Write a short comment describing this function |
| 77 | +## Function to test cacheMatrix() function |
| 78 | +# Expected to print messages: |
| 79 | +# Cache not found. Calculating inverse |
| 80 | +# [1] TRUE |
| 81 | +# Retrieving cached version |
| 82 | +# [1] TRUE |
12 | 83 |
|
13 |
| -cacheSolve <- function(x, ...) { |
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 84 | +testCacheMatrix <- function() { |
| 85 | + # Create matrix: 1 3 |
| 86 | + # 2 4 |
| 87 | + matrix <- makeCacheMatrix(matrix(1:4, 2, 2)) |
| 88 | + |
| 89 | + # Use our function to calculates its inverse |
| 90 | + inverse <- cacheSolve(matrix) |
| 91 | + |
| 92 | + # Check if inverse is correct by using the property that |
| 93 | + # states that: matrix * matrix_inverse = identity matrix |
| 94 | + print(identical(matrix$getMatrix() %*% inverse, diag(2))) |
| 95 | + |
| 96 | + # Use our function again. Now should print message |
| 97 | + # saying that there is a cached version |
| 98 | + inverse <- cacheSolve(matrix) |
| 99 | + |
| 100 | + # Perform check again |
| 101 | + print(identical(matrix$getMatrix() %*% inverse, diag(2))) |
15 | 102 | }
|
| 103 | + |
| 104 | +# Call function to test |
| 105 | +testCacheMatrix() |
0 commit comments