|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## Couple of functions to optimize inversing matrixes as it could be fairly |
| 2 | +## computational work to find the inverse of a large matrix. Usage below: |
| 3 | +## |
| 4 | +## [Inspired by Johanna Tikanmäki COMMUNITY TA usage for cachemean on |
| 5 | +## https://class.coursera.org/rprog-014/forum/thread?thread_id=105] |
| 6 | +## |
| 7 | +## cMatrix <- makeCacheMatrix(m) #first a cache aware matrix where 'm' is the |
| 8 | +## #matrix you would like to use as input |
| 9 | +## cacheSolve(cMatrix) #calculate Inverse (solve) of the matrix |
| 10 | +## cacheSolve(cMatrix) #this will return the cached matrix inverse |
| 11 | +## cMatrix$set(new_m) #cMatrix can be set to a new input matrix ('new_m') |
| 12 | +## cacheSolve(cMatrix) #calculate Inverse of new_m for the first time |
| 13 | +## cacheSolve(cMatrix) #this will return the cached inverse |
| 14 | +## cacheSolve(cMatrix) #same result as above; still cached |
3 | 15 |
|
4 |
| -## Write a short comment describing this function |
5 | 16 |
|
6 |
| -makeCacheMatrix <- function(x = matrix()) { |
7 |
| - cachedMatrixInverse <- NULL |
| 17 | +## Create a cached aware matrix that that be reused with new matrixes as well |
| 18 | + |
| 19 | +makeCacheMatrix <- function(m = matrix()) { |
| 20 | + cachedMatrixInverse <- NULL #cached matrix inverse value if set |
8 | 21 | set <- function(y) {
|
9 |
| - x <<- y |
10 |
| - cachedMatrixInverse <<- NULL |
| 22 | + m <<- y #set new value for raw matrix 'm' set |
| 23 | + #at makeCacheMatrix function level using |
| 24 | + #<<- |
| 25 | + cachedMatrixInverse <<- NULL #clear cached as 'm' has changed at the |
| 26 | + #parent frame of this function |
11 | 27 | }
|
12 |
| - get <- function() x |
| 28 | + get <- function() m #get raw matrix 'm' |
| 29 | + |
| 30 | + #functions below: set & get cached matrix value for makeCacheMatrix's |
| 31 | + #cacheMatrixInverse variable |
13 | 32 | setMatrixInverse <- function(inv) cachedMatrixInverse <<- inv
|
14 | 33 | getMatrixInverse <- function() cachedMatrixInverse
|
| 34 | + |
| 35 | + #return a list containing various functions that can be used to: |
| 36 | + #get raw matrix 'm' |
| 37 | + #set raw matrix that will clear matrix inverse as well |
| 38 | + #getMatrixInverse to get cachedMatrixInverse |
| 39 | + #setMatrixInverse to set cachedMatrixInverse |
15 | 40 | list(set = set, get = get,
|
16 | 41 | setMatrixInverse = setMatrixInverse,
|
17 | 42 | getMatrixInverse = getMatrixInverse)
|
18 | 43 | }
|
19 | 44 |
|
20 | 45 |
|
21 |
| -## Write a short comment describing this function |
| 46 | +## Solve (Inverse) and cache matrix. Has to be used as shown in the usage |
| 47 | +## section. 'x' has to be an object returned by makeCacheMatrix function |
22 | 48 |
|
23 | 49 | cacheSolve <- function(x, ...) {
|
24 |
| - ## Return a matrix that is the inverse of 'x' |
25 |
| - matrixInverse <- x$getMatrixInverse() |
26 |
| - if(!is.null(matrixInverse)) { |
27 |
| - message("found cached matrix inverse") |
28 |
| - return(matrixInverse) |
| 50 | + matrixInverse <- x$getMatrixInverse() #try retrieving cached value |
| 51 | + if(!is.null(matrixInverse)) { #if we find the value |
| 52 | + message("found cached matrix inverse") #message we have cached value |
| 53 | + return(matrixInverse) #return from this function |
29 | 54 | }
|
30 |
| - matrix <- x$get() |
31 |
| - calculatedMatrixInverse <- solve(matrix) |
32 |
| - x$setMatrixInverse(calculatedMatrixInverse) |
| 55 | + matrix <- x$get() #get actual matrix data |
| 56 | + calculatedMatrixInverse <- solve(matrix) #use solve from {base} package |
| 57 | + x$setMatrixInverse(calculatedMatrixInverse) #set cached value for x |
| 58 | + ## Return a matrix that is the inverse of 'x' |
33 | 59 | calculatedMatrixInverse
|
34 | 60 | }
|
0 commit comments