|
1 | 1 | ## Put comments here that give an overall description of what your
|
2 |
| -## functions do |
| 2 | +## functions do.................. |
| 3 | +## The "makeCacheMatrix()" function creates and initializes a "matrix" cache for subsequent use. |
| 4 | +## The "cacheSolve()" function uses the cached "matrix" as input and updates the matrix inverse |
| 5 | +## value stored there (if it has been previously computed). |
| 6 | +## If there is not previously computed inverse matrix, it computes one and updates the cache. |
3 | 7 |
|
4 |
| -## Write a short comment describing this function |
| 8 | +## The "set" pieces of these functions provide INPUT in to the cache. |
| 9 | +## The "get" pieces of these functions get OUTPUT from the cache. |
5 | 10 |
|
6 |
| -makeCacheMatrix <- function(x = matrix()) { |
| 11 | +## TEST RESULTS ARE SHOWN AT END |
| 12 | + |
| 13 | +## Write a short comment describing this function................. |
| 14 | +## This function initializes a matrix value (caches it). |
| 15 | +## It includes a place for the computed inverse matrix, initated to NULL. |
7 | 16 |
|
| 17 | +makeCacheMatrix <- function(x = matrix()) { |
| 18 | + matinv <- NULL ## Initialize the cached matrix inverse value as NULL |
| 19 | + ## until it's calculated the first time. |
| 20 | + set <- function(y) { ## The "set" variables provide the INPUT to the cache. |
| 21 | + x <<- y |
| 22 | + matinv <<- NULL ## Sets the calculated matrix inverse to NULL |
| 23 | + ## (i.e., not yet calculated) |
| 24 | + } |
| 25 | + get <- function() x ## The "get" variables provide the OUTPUT from the cache. |
| 26 | + setmatinv <- function(matinvin) matinv <<- matinvin |
| 27 | + getmatinv <- function() matinv |
| 28 | + list(set = set, get = get, |
| 29 | + setmatinv = setmatinv, |
| 30 | + getmatinv = getmatinv) |
8 | 31 | }
|
9 | 32 |
|
10 | 33 |
|
11 |
| -## Write a short comment describing this function |
| 34 | +## Write a short comment describing this function.................. |
| 35 | +## The following function computes the matrix inverse from a cached input matrix (which includes |
| 36 | +## a previously calculated value when the matrix inverse was already calculated). |
| 37 | +## If a new matrix inverse computation is performed, it is added to this cache. That is, the |
| 38 | +## return value (the matrix inverse) from this function |
| 39 | +## (another matrix) is used as reference to subsequent computations, and is no longer the |
| 40 | +## initialized NULL value. |
| 41 | +## So long as this computed matrix inverse is not changed ("reinitialized"), |
| 42 | +## the cached value of the matrix inverse is used (instead of being recomputed). |
| 43 | + |
12 | 44 |
|
13 | 45 | cacheSolve <- function(x, ...) {
|
14 | 46 | ## Return a matrix that is the inverse of 'x'
|
| 47 | + matinv <- x$getmatinv() |
| 48 | + if(!is.null(matinv)) { |
| 49 | + message("getting cached data") |
| 50 | + return(matinv) |
| 51 | + } |
| 52 | + data <- x$get() |
| 53 | + matinv <- solve(data, ...) |
| 54 | + x$setmatinv(matinv) |
| 55 | + matinv |
15 | 56 | }
|
| 57 | +## TEST RESULTS FOLLOW ###################### |
| 58 | +## > testmatA |
| 59 | +## [,1] [,2] |
| 60 | +## [1,] 1 4 |
| 61 | +## [2,] 3 2 |
| 62 | +## > matAtst<-makeCacheMatrix(testmatA) |
| 63 | +## > matAinvtst<-cacheSolve(matAtst) |
| 64 | +## > matAinvtst |
| 65 | +## [,1] [,2] |
| 66 | +## [1,] -0.2 0.4 |
| 67 | +## [2,] 0.3 -0.1 |
| 68 | +## > matAinvtst<-cacheSolve(matAtst) |
| 69 | +## getting cached data |
| 70 | +## > matAinvtst |
| 71 | +## [,1] [,2] |
| 72 | +## [1,] -0.2 0.4 |
| 73 | +## [2,] 0.3 -0.1 |
| 74 | +## > matAinvtst %*% testmatA |
| 75 | +## [,1] [,2] |
| 76 | +## [1,] 1 0 |
| 77 | +## [2,] 0 1 |
| 78 | +## > ## TEST IS GOOD |
0 commit comments