|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## Matrix inversion is potentially time-consuming, particullarly when repeated |
| 2 | +## inversion must be performed on the same matrix. This function is to creates |
| 3 | +## a special "matrix" object that can cache its inverse If the inverse has |
| 4 | +## already been calculated (and the matrix has not changed), then this function |
| 5 | +##retrieve the inverse from the cache without repeating the operation. |
3 | 6 |
|
4 |
| -## Write a short comment describing this function |
| 7 | +## This function creates a special "matrix" object that can cache its inverse. |
5 | 8 |
|
6 | 9 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 10 | + # Creates a special "matrix" object that can cache its inverse. |
| 11 | + # |
| 12 | + # Args: |
| 13 | + # x: One matrix used to initialize the cacheMatrix object. |
| 14 | + # |
| 15 | + # Returns: |
| 16 | + # An object that can cache the inverse of the input matrix. |
| 17 | + invx <- NULL |
| 18 | + set <- function(y) { |
| 19 | + x <<- y |
| 20 | + invx <<- NULL |
| 21 | + } |
| 22 | + get <- function() x |
| 23 | + setMatrixInverse <- function(invMatrix) invx <<- invMatrix |
| 24 | + getMatrixInverse <- function() invx |
| 25 | + list(set = set, get = get, |
| 26 | + setMatrixInverse = setMatrixInverse, |
| 27 | + getMatrixInverse = getMatrixInverse) |
8 | 28 | }
|
9 | 29 |
|
10 |
| - |
11 |
| -## Write a short comment describing this function |
| 30 | +## This function computes the inverse of the special "matrix" returned by |
| 31 | +## makeCacheMatrix above. If the inverse has already been calculated |
| 32 | +## (and the matrix has not changed), then cacheSolve should retrieve the |
| 33 | +## inverse from the cache. |
12 | 34 |
|
13 | 35 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 36 | + # Creates a special "matrix" object that can cache its inverse. |
| 37 | + # |
| 38 | + # Args: |
| 39 | + # x: matrix object that can cache its inverse. |
| 40 | + # |
| 41 | + # Returns: |
| 42 | + # the inverse of the matrix. |
| 43 | + |
| 44 | + ## Return a matrix that is the inverse of 'x' |
| 45 | + invx <- x$getMatrixInverse() |
| 46 | + if(!is.null(invx)) { |
| 47 | + message("getting cached inversed matrix") |
| 48 | + return(invx) |
| 49 | + } |
| 50 | + data <- x$get() |
| 51 | + invx <- solve(data, ...) |
| 52 | + x$setMatrixInverse(invx) |
| 53 | + invx |
15 | 54 | }
|
0 commit comments