|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
3 |
| - |
4 |
| -## Write a short comment describing this function |
| 1 | +## This function creates and stores a special "matrix" object that can cache its inverse. |
| 2 | +## the object is a list of four functions |
5 | 3 |
|
6 | 4 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 5 | + # store the value of the matrix |
| 6 | + m <- NULL |
| 7 | + set <- function(y) { |
| 8 | + x <<- y |
| 9 | + m <<- NULL |
| 10 | + } |
| 11 | + |
| 12 | + # retrieve the value of the matrix |
| 13 | + get <- function() x |
| 14 | + |
| 15 | + # store the value of the mean |
| 16 | + setInverse <- function(inverse) m <<- inverse |
| 17 | + |
| 18 | + # retrieve the value of the mean |
| 19 | + getInverse <- function() m |
| 20 | + |
| 21 | + # create and store the object: a list containing the 4 functions defined above |
| 22 | + list(set = set, get = get, |
| 23 | + setInverse = setInverse, |
| 24 | + getInverse = getInverse) |
| 25 | + |
8 | 26 | }
|
9 | 27 |
|
10 | 28 |
|
11 |
| -## Write a short comment describing this function |
| 29 | +## This function computes the inverse of a "matrix" |
| 30 | +## If the inverse has already been calculated (and the matrix has not changed), then the cachesolve retrieves the inverse from the cache. |
| 31 | +## Return a matrix that is the inverse of 'x' |
12 | 32 |
|
13 | 33 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 34 | + # first check to see if the inverse matrix has already been calculated and is cached |
| 35 | + inv <- x$getInverse() |
| 36 | + print(inv) |
| 37 | + if(!is.null(inv)) { |
| 38 | + message("Found in cache.") |
| 39 | + return(inv) # retrieve and return immediately |
| 40 | + } else |
| 41 | + { |
| 42 | + # not yet cached: calculate the inverse of the input matrix |
| 43 | + message("Not found in cache. Calculating it") |
| 44 | + data <- x$get() |
| 45 | + inv <- solve(data) |
| 46 | + # and set the value of the inverse matrix in the cache via the setInverse function. |
| 47 | + x$setInverse(inv) |
| 48 | + # return the calculated inverse matrix |
| 49 | + return(inv) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +## ========================================================================================= |
| 54 | +## This function is used only to test that the above code is working as supposed |
| 55 | +## |
| 56 | +## Test basic caching |
| 57 | +## |
| 58 | + |
| 59 | +unitTestCacheMatrix <- function() { |
| 60 | + print("Start ... test matrix is: ") |
| 61 | + testMatrix <- matrix(c(1, 2, 3, 4), nrow=2, ncol=2) |
| 62 | + print(testMatrix) |
| 63 | + |
| 64 | + # Testing set() and get(): create the special matrix object with caching functions |
| 65 | + matrixCached <- makeCacheMatrix(testMatrix) |
| 66 | + print("Get. This is what has been cached: ") |
| 67 | + print (matrixCached$get()) |
| 68 | + |
| 69 | + # Test cacheSolve(); now invert the test matrix |
| 70 | + inverseMatrix <- cacheSolve(matrixCached) |
| 71 | + print("The matrix inversed is: ") |
| 72 | + print (inverseMatrix) |
| 73 | + print("multiplying the test matrix with its inverse should return the identity matrix: ") |
| 74 | + print(testMatrix %*% inverseMatrix) |
| 75 | + |
| 76 | + # Test caching mechanism. Now calculating again. It should found it in the cache |
| 77 | + print("Now calculating again. It should found it in the cache") |
| 78 | + againMatrix <- cacheSolve(matrixCached) |
| 79 | + |
| 80 | + if (!identical(inverseMatrix, againMatrix)) |
| 81 | + message("Cached version does not match solved version") |
15 | 82 | }
|
| 83 | + |
0 commit comments