|
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 | +## cachematrix() contains convenience functions for |
| 2 | +## matrix storage and inversion. The matirx 'x' will have |
| 3 | +## its inverse calculated once. If the inverse has already |
| 4 | +## been calculated (and the matrix has not changed), then |
| 5 | +## the cachesolve() should retrieve the inverse from the cache. |
5 | 6 |
|
| 7 | +## This function creates a special "matrix" object |
| 8 | +## that can cache its inverse |
6 | 9 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 10 | + im <- NULL |
| 11 | + |
| 12 | + set <- function(y){ |
| 13 | + x <- y |
| 14 | + im <<- NULL |
| 15 | + } |
| 16 | + |
| 17 | + get <- function() x |
| 18 | + setmatrix <- function(solve) im <<- solve |
| 19 | + getmatrix <- function() im |
| 20 | + |
| 21 | + list( set=set, get=get, |
| 22 | + setmatrix=setmatrix, |
| 23 | + getmatrix=getmatrix) |
8 | 24 | }
|
9 | 25 |
|
10 | 26 |
|
11 |
| -## Write a short comment describing this function |
12 |
| - |
| 27 | +## This function computes the inverse of the |
| 28 | +## special "matrix" returned by makeCacheMatrix above. |
| 29 | +## If the inverse has already been calculated (and the matrix |
| 30 | +## has not changed), then the cachesolve should retrieve the |
| 31 | +## inverse from the cache. |
13 | 32 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 33 | + ## This function assume that the matrix supplied is |
| 34 | + ## always invertible. |
| 35 | + im <- x$getmatrix() |
| 36 | + if(!is.null(im)){ |
| 37 | + message("getting cached data...") |
| 38 | + return (im) |
| 39 | + } |
| 40 | + m <- x$get() |
| 41 | + im <- solve(m, ...) |
| 42 | + x$setmatrix(im) |
| 43 | + ## Return a matrix that is the inverse of 'x' |
| 44 | + im |
15 | 45 | }
|
0 commit comments