|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## Functions allowing the creation of a special 'matrix' type that can cache |
| 2 | +## its inverse in order to save computation resources. |
3 | 3 |
|
4 |
| -## Using a function call to create space to cache a matrix's inverse. |
5 |
| -## Functions to access that space are created: (get, set, getinv, setinv) |
| 4 | +## We use a function call to create space to cache a matrix's inverse. |
| 5 | +## Functions to access that environment are created: (get, set, getinv, setinv) |
6 | 6 |
|
7 | 7 | makeCacheMatrix <- function(x = matrix()) {
|
8 | 8 | i <- NULL #initialize variable to store inverse
|
9 | 9 | set <- function(y) {
|
10 |
| - #assign a matrix to this CacheMatrix |
| 10 | + #assigns a matrix to this CacheMatrix |
11 | 11 | x <<- y
|
12 | 12 | m <<- NULL
|
13 | 13 | }
|
14 |
| - get <- function() x #get the matrix currently in this CacheMatrix |
15 |
| - setinv <- function(inv) i <<- inv #assign an inverse to this CacheMatrix |
16 |
| - getinv <- function() i #get the inverse currently in this CacheMatrix |
17 |
| - list(set = set, get = get, setinv = setinv, getinv = getinv) |
| 14 | + get <- function() x #gets the matrix currently in this CacheMatrix |
| 15 | + setinv <- function(inv) i <<- inv #assigns an inverse to this CacheMatrix |
| 16 | + getinv <- function() i #gets the inverse currently in this CacheMatrix |
| 17 | + list(set = set, get = get, setinv = setinv, getinv = getinv) #return funcs |
18 | 18 | }
|
19 | 19 |
|
20 | 20 |
|
21 |
| -## Computes the inverse of a makeCacheMatrix 'matrix'. If the inverse has been |
| 21 | +## Computes the inverse of a CacheMatrix. If the inverse has been |
22 | 22 | ##cached, it retrieves it.
|
23 | 23 |
|
24 | 24 | cacheSolve <- function(x, ...) {
|
25 | 25 | ## Return a matrix that is the inverse of 'x'
|
| 26 | + i <- x$getinv() #get the inverse of a given CacheMatrix |
| 27 | + if(!is.null(i)) { |
| 28 | + message('getting cached data') |
| 29 | + return(i) |
| 30 | + } |
| 31 | + #unless it's null, in which case: |
| 32 | + data <- x$get() #get the matrix |
| 33 | + i <- solve(data) #solve for the inverse |
| 34 | + x$setinv(i) #store the inverse in the CacheMatrix |
| 35 | + i |
26 | 36 | }
|
0 commit comments