|
1 | 1 | # ------------------------------------------------------------------------------
|
2 | 2 | # Helper functions to compute and cache the inverse of a matrix (i.e. "solve" a
|
3 |
| -# matrix). |
| 3 | +# matrix). Computing the inverse of a matrix can be a time-consuming operation, |
| 4 | +# therefore it's useful to be able to cache the computation so the value can be |
| 5 | +# used multiple times without running the computation again. |
4 | 6 | #
|
5 | 7 | # Note: we assume a square matrix (i.e. one that is invertible) is always
|
6 |
| -# provided when using these functions. Therefore there is no erro handling for |
| 8 | +# provided when using these functions. Therefore there is no error handling for |
7 | 9 | # cases where this is not true.
|
8 | 10 | #-------------------------------------------------------------------------------
|
9 | 11 |
|
10 | 12 |
|
11 | 13 | # Given an initial matrix, this method returns a special wrapper, in the form of
|
12 |
| -# a list with functions that can be used to: |
| 14 | +# a list, with functions that can be used to: |
13 | 15 | # - set the underlying matrix again
|
14 | 16 | # - get the underlying matrix
|
15 |
| -# - set the inverse of the underlying matrix |
16 |
| -# - get th inverse of the underlying matrix |
| 17 | +# - set the inverse of the underlying matrix (which caches this value internally) |
| 18 | +# - get the inverse of the underlying matrix (which uses the cached value) |
17 | 19 | makeCacheMatrix <- function(x = matrix()) {
|
18 |
| - i <- NULL # Cache value for the inverse matrix of `x` |
19 |
| - |
| 20 | + i <- NULL # Cache for the inverse matrix of `x` |
| 21 | + |
20 | 22 | # Setter method, if we want to overwrite the underlying matrix `x`
|
21 | 23 | set <- function(y) {
|
22 | 24 | x <<- y
|
23 |
| - i <<- NULL |
| 25 | + i <<- NULL # Reset the cache |
24 | 26 | }
|
25 |
| - |
| 27 | + |
26 | 28 | # Getter method, to access the underlying matrix `x`
|
27 | 29 | get <- function() {
|
28 | 30 | x
|
29 | 31 | }
|
30 |
| - |
| 32 | + |
31 | 33 | # Set the inverse of the underlying matrix `x`, into the cache
|
32 | 34 | setinverse <- function(inverse) {
|
33 | 35 | i <<- inverse
|
34 | 36 | }
|
35 |
| - |
| 37 | + |
36 | 38 | # Get the inverse of the underlying matrix `x`, from the cache
|
37 | 39 | # Note: this will return `NULL` if the cache hasn't been set yet
|
38 | 40 | getinverse <- function() {
|
39 | 41 | i
|
40 | 42 | }
|
41 |
| - |
| 43 | + |
42 | 44 | list(set = set, get = get,
|
43 | 45 | setinverse = setinverse,
|
44 | 46 | getinverse = getinverse)
|
45 | 47 | }
|
46 | 48 |
|
47 | 49 |
|
48 | 50 | # Use this method to "solve" (i.e. get the inverse) of a matrix, doing so in a
|
49 |
| -# way that caches the very first computation of the inverse and uses the cache |
50 |
| -# subsequently. |
| 51 | +# way that caches the very first computation of the inverse computation, and |
| 52 | +# uses the cache for subsequent access. |
51 | 53 | #
|
52 |
| -# The argument `x` MUST be the special wrapper obtained from `makeCacheMatrix`. |
| 54 | +# The argument `x` MUST be a special wrapper obtained from `makeCacheMatrix`. |
53 | 55 | cacheSolve <- function(x, ...) {
|
54 | 56 | ## Return a matrix that is the inverse of 'x'
|
55 |
| - |
| 57 | + |
56 | 58 | # Try to get the cached value
|
57 | 59 | i <- x$getinverse()
|
58 | 60 | if(!is.null(i)) {
|
59 | 61 | message("getting cached data")
|
60 | 62 | return(i)
|
61 | 63 | }
|
62 |
| - |
63 |
| - # … otherwise compute the inverse and cache |
| 64 | + |
| 65 | + # … otherwise compute the inverse and cache it |
64 | 66 | data <- x$get()
|
65 | 67 | i <- solve(data, ...)
|
66 | 68 | x$setinverse(i)
|
|
0 commit comments