|
1 | 1 | ## Functions for computing cached matrix inversions
|
2 | 2 |
|
3 | 3 | ## makeCacheMatrix creates a cached matrix object for storing cached inversion results
|
4 |
| -## Parameter x is the initial matrix stored in the cache |
| 4 | +## Parameter matrixValue is the initial matrix stored in the cache |
| 5 | +## Use cacheSolve() to compute its inverse |
5 | 6 |
|
6 |
| -makeCacheMatrix <- function(x = matrix()) { |
7 |
| - inv <- NULL |
| 7 | +makeCacheMatrix <- function(matrixValue = matrix()) { |
| 8 | + inverseValue <- NULL |
8 | 9 |
|
9 |
| - # Sets the matrix whose inverse is cached |
10 |
| - set <- function(y) { |
11 |
| - x <<- y |
12 |
| - inv <<- NULL |
| 10 | + # Sets the matrix whose inverse is cached (and resets the cached inverse) |
| 11 | + set <- function(newMatrixValue) { |
| 12 | + matrixValue <<- newMatrixValue |
| 13 | + inverseValue <<- NULL |
13 | 14 | }
|
14 | 15 |
|
15 | 16 | # Gets the matrix whose inverse is cached
|
16 | 17 | get <- function() {
|
17 |
| - x |
| 18 | + matrixValue |
18 | 19 | }
|
19 | 20 |
|
20 |
| - # Sets the inverse value of the matrix |
21 |
| - setinverse <- function(i) { |
22 |
| - inv <<- i |
| 21 | + # Sets the computed inverse value of the matrix |
| 22 | + setinverse <- function(newInverseValue) { |
| 23 | + inverseValue <<- newInverseValue |
23 | 24 | }
|
24 | 25 |
|
25 |
| - # Gets the inverse value of the matrix |
| 26 | + # Gets the computed inverse value of the matrix |
26 | 27 | getinverse <- function() {
|
27 |
| - inv |
| 28 | + inverseValue |
28 | 29 | }
|
29 | 30 |
|
30 |
| - # Return the object as a vector of functions |
| 31 | + # Return the cached matrix object as a vector of functions |
31 | 32 | list(set=set, get=get, setinverse=setinverse, getinverse=getinverse)
|
32 | 33 | }
|
33 | 34 |
|
34 | 35 |
|
35 |
| -## cacheSolve computes the inverse of the given matrix x, using cached results when available |
36 |
| -## Parameter x must be a cached matrix object created using makeCacheMatrix() |
| 36 | +## cacheSolve computes the inverse of the given matrix, using cached results when available |
| 37 | +## Parameter cacheMatrix must be a cached matrix object created using makeCacheMatrix() |
37 | 38 |
|
38 |
| -cacheSolve <- function(x, ...) { |
39 |
| - # Check if solution is cached |
40 |
| - inv <- x$getinverse() |
41 |
| - if (!is.null(inv)) { |
42 |
| - # Yes, it was cached, return it |
43 |
| - return(inv) |
| 39 | +cacheSolve <- function(cacheMatrix, ...) { |
| 40 | + # Check if solution is already cached |
| 41 | + inverseValue <- cacheMatrix$getinverse() |
| 42 | + if (!is.null(inverseValue)) { |
| 43 | + # Yes, solution was cached, return it |
| 44 | + return(inverseValue) |
44 | 45 | }
|
45 | 46 |
|
46 | 47 | # No, solution not yet cached, compute it now and cache it
|
47 |
| - data <- x$get() |
48 |
| - inv <- solve(data, ...) |
49 |
| - x$setinverse(inv) |
| 48 | + matrixValue <- cacheMatrix$get() |
| 49 | + inverseValue <- solve(matrixValue, ...) |
| 50 | + cacheMatrix$setinverse(inverseValue) |
50 | 51 |
|
51 |
| - # Return the inverse matrix |
52 |
| - inv |
| 52 | + # Return the computed inverse matrix |
| 53 | + inverseValue |
53 | 54 | }
|
0 commit comments