|
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 | +## |
| 2 | +# Combination of functions that provide caching strategy for obtaining |
| 3 | +# the results of 'solve' to a specified Vector. |
| 4 | +# |
| 5 | +# Usage: |
| 6 | +# makeCacheMatrix - use this function to create a special object |
| 7 | +# that encapsulates a specified matrix and a cached |
| 8 | +# result. |
| 9 | +# cacheSolve - this provides the 'solve' restuls for the specified |
| 10 | +# matrix - providing the cached value if executed twice. |
5 | 11 |
|
| 12 | +# Creates an object that encapsulates a cached result for |
| 13 | +# calculating the inverse of a matrix. |
| 14 | +# |
| 15 | +# Args: |
| 16 | +# x: Specified matrix that the inverse function will be applied to. |
| 17 | +# |
| 18 | +# Returns: |
| 19 | +# An object encapsulating the cached resulting value that has the |
| 20 | +# following functions: |
| 21 | +# result$get - Get the specified matrix value |
| 22 | +# result$set - Set the specified amatrix value |
| 23 | +# result$getinverse - The cached value or NULL |
| 24 | +# result$setinverse - Set the cached value |
6 | 25 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 26 | + m <- NULL |
| 27 | + set <- function(y) { |
| 28 | + x <<- y |
| 29 | + m <<- NULL |
| 30 | + } |
| 31 | + get <- function() x |
| 32 | + setsolve <- function(solve) m <<- solve |
| 33 | + getsolve <- function() m |
| 34 | + list(set = set, get = get, |
| 35 | + getsolve = getsolve, |
| 36 | + setsolve = setsolve) |
8 | 37 | }
|
9 | 38 |
|
10 | 39 |
|
11 |
| -## Write a short comment describing this function |
12 |
| - |
| 40 | +# Calculates the mean value using the 'mean' value of the value specified |
| 41 | +# in the specified 'special vector'. Returns the cache value if there is |
| 42 | +# one. |
| 43 | +# |
| 44 | +# Args: |
| 45 | +# x - 'special object' returned by the 'makeCacheMatrix'. |
| 46 | +# |
| 47 | +# Returns: |
| 48 | +# An object encapsulating the cached resulting value that has the |
| 49 | +# following functions: |
| 50 | +# result$get - Get the specified matrix value |
| 51 | +# result$set - Set the specified amatrix value |
| 52 | +# result$getinverse - The cached value or NULL |
| 53 | +# result$setinverse - Set the cached value |
13 | 54 | cacheSolve <- function(x, ...) {
|
14 |
| - ## Return a matrix that is the inverse of 'x' |
| 55 | + m <- x$getsolve() |
| 56 | + if(!is.null(m)) { |
| 57 | + message("getting cached data") |
| 58 | + return(m) |
| 59 | + } |
| 60 | + data <- x$get() |
| 61 | + m <- solve(data, ...) |
| 62 | + x$setsolve(m) |
| 63 | + m |
15 | 64 | }
|
0 commit comments