|
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 is solution to Programming Assignment 2 of `R Programming` Coursera class |
| 2 | +## |
| 3 | +## Included function are used to cache computation of matrix inverse |
| 4 | +## by storing values in local environment of special cacheable matrix implementation |
| 5 | +## |
| 6 | +## Usage example: |
| 7 | +## |
| 8 | +## m <- makeCacheMatrix(matrix(1:4, nrow=2)) |
| 9 | +## all(m$get() %*% cacheSolve(m) == diag(2)) # gives TRUE |
| 10 | +## all(m$get() %*% cacheSolve(m) == diag(2)) # prints "getting cached data" and returns TRUE |
5 | 11 |
|
| 12 | +## `makeCacheMatrix` crates cacheable matrix of out specified R matrix, or |
| 13 | +## when specified with no arguments it creates 1-by-1 cacheable matrix with single NA value. |
| 14 | +## |
| 15 | +## Result of this function call is "nearly object" (not S3 or S4 object), |
| 16 | +## implemented by list of four methods: |
| 17 | +## |
| 18 | +## * get() - to get internal R matrix |
| 19 | +## * set(matrix) - to set internal R matrix and reset earlier calculated inverse |
| 20 | +## * getinv() - to get cached inverse, or NULL if no cached inverse is present |
| 21 | +## * setinv(matrix) - to set cached inverse |
6 | 22 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 23 | + i <- NULL # we create cache variable set to NULL |
| 24 | + set <- function(y) { # when we set new matrix |
| 25 | + x <<- y # we replace old x with new value |
| 26 | + i <<- NULL # and reset stored cache |
| 27 | + } |
| 28 | + get <- function() x # to get matrix, we just return x |
| 29 | + setinv <- function(inv) i <<- inv # here we store inverse in cache variable |
| 30 | + getinv <- function() i # and here we return it |
| 31 | + list(set = set, get = get, |
| 32 | + setinv = setinv, |
| 33 | + getinv = getinv) # what remains is return list of functions, so we can access it from outside world |
8 | 34 | }
|
9 | 35 |
|
10 |
| - |
11 |
| -## Write a short comment describing this function |
12 |
| - |
| 36 | +## `cacheSolve` function uses `solve` function to get inverse of cacheable matrix |
| 37 | +## returned by `makeCacheMatrix`, but also stores the value for later use. |
| 38 | +## If inverse was already calculated, it will return cached result instantly. |
| 39 | +## This function passes excess arguments directly to solve method. |
13 | 40 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 41 | + inv <- x$getinv() # get cached inverse |
| 42 | + if(!is.null(inv)) { # if it was calculated earlier |
| 43 | + message("getting cached data") |
| 44 | + return(inv) # we have it |
| 45 | + } # otherise |
| 46 | + m <- x$get() # we take R matrix out of cachable matrix |
| 47 | + inv <- solve(m, ...) # and calculate its inverse |
| 48 | + x$setinv(inv) # which we store in cachable matrix |
| 49 | + inv # and return |
15 | 50 | }
|
| 51 | + |
0 commit comments