|
1 | | -## Put comments here that give an overall description of what your |
2 | | -## functions do |
| 1 | +# Programming assignment 2 is an opportunity to implement custom R functions that solve the common |
| 2 | +# programming problem of wasting time recalculating something your code has already determined. |
| 3 | +# Calculating means and matrix inversions are two examples. |
| 4 | +# At the same time, it gives the student a chance to review R's Lexical Scoping Rules and how they |
| 5 | +# are helpful when designing and coding the custom R functions. |
| 6 | +# The function makeCacheMatrix will use the matrix you specify and create a special matrix that can cache its inverse. |
| 7 | +# The function cacheSolve can calculate the inverse of the special matrix or retrieve it from a cache so that you save |
| 8 | +# time by retrieving the value and not recalculating it. |
| 9 | + |
| 10 | +# Our functions exist in the Global Environment, the Environment makeCacheMatrix was defined in, and |
| 11 | +# the Environment cacheSolve was defined in. |
| 12 | +# |
| 13 | +# makeCacheMatrix has formal argument m |
| 14 | +# and free variable i, and functions set, get, setinverse, and getinverse |
| 15 | +# the set function has formal argument y and free variables m and i |
| 16 | +# the set function uses the <<- operator to assign the value of the matrix from the calling environment, y to the formal |
| 17 | +# argument m in the Environment makeCacheMatrix was defined in. |
| 18 | +# the setinverse function has formal argument solve and uses the <<- operator to assign the value of solve from |
| 19 | +# the calling environment to the free variable i in the Environment makeCacheMatrix was defined in. |
| 20 | +# |
| 21 | +# cacheSolve has formal argument m |
| 22 | +# and free variables - i and data which get their values from the environment cacheSolve was defined in. |
| 23 | + |
| 24 | +# The following steps demonstrate how to create special matrices, calculate their inverses, and cache them. |
| 25 | +# 1. Create a special matrix that can cache its inverse! |
| 26 | +## sm <- makeCacheMatrix(matrix(1:4, nrow=2, ncol=2)) |
| 27 | + |
| 28 | +# 2. Calculate the inverse of the special matrix, sm and display |
| 29 | +## cacheSolve(sm) |
| 30 | + |
| 31 | +# 3. Create another special matrix that can cache its inverse |
| 32 | +## sm2 <- makeCacheMatrix(matrix(5:8, nrow=2, ncol=2)) |
| 33 | + |
| 34 | +# 4. When we try to recalculate the first matrix's inverse, cacheSolve finds it in the cache, |
| 35 | +# displays the getting cached data message, and the inverse of the matrix found in the cache |
| 36 | +## cacheSolve(sm) |
| 37 | + |
| 38 | +# 5. Calculate the inverse of the second special matrix (sm2) and display; it's not in cache yet |
| 39 | +## cacheSolve(sm2) |
| 40 | + |
| 41 | +# 6. When we try to recalculate sm2's inverse, cacheSolve finds it in the cache, |
| 42 | +# displays the getting cached data message, and the inverse of the matrix found in the cache |
| 43 | +## cacheSolve(sm2) |
| 44 | + |
| 45 | +# We can see what's in each function's environment with these commands |
| 46 | +# ls(environment(makeCacheMatrix)) |
| 47 | +# shows cacheSolve, makeCacheMatrix, sm, and sm2 |
| 48 | +# |
| 49 | +# ls(environment(cacheSolve)) |
| 50 | +# shows cacheSolve, makeCacheMatrix, sm, and sm2 |
| 51 | + |
| 52 | +# We can retrieve the value of a symbol in an environment with these commands |
| 53 | +# get("sm", environment(makeCacheMatrix)) |
| 54 | +# shows the 4 functions set, get, setinverse, and getinverse |
| 55 | +# get("sm2", environment(cacheSolve)) |
| 56 | +# shows the 4 functions set, get, setinverse, and getinverse |
| 57 | + |
| 58 | +# We can see what's in makeCacheMatrix individual function's environments with these commands |
| 59 | +# ls(environment(sm$set)) |
| 60 | +# ls(environment(sm$get)) |
| 61 | +# ls(environment(sm$setinverse)) |
| 62 | +# ls(environment(sm$getinverse)) |
| 63 | +# The output shows the symbols, get, getinverse, i, m, set, and setinverse for each function. |
3 | 64 |
|
4 | 65 | ## Write a short comment describing this function |
5 | | -makeCacheMatrix is a function that creates a special matrix that can be used to cache |
6 | | -its inverse, since matrix inversion can be time consuming. Instead of recalculating |
7 | | -the inverse of a matrix, it will be stored in a cache for retrieval. |
8 | | - |
9 | | -makeCacheMatrix returns a list with a function to |
10 | | -1. set the value of the matrix |
11 | | -2. get the value of the matrix |
12 | | -3. set the value of the matrix inverse |
13 | | -4. get the value of the matrix inverse |
14 | | - |
15 | | -makeCacheMatrix <- function(x = matrix()) { |
16 | | - m <- NULL |
| 66 | +## makeCacheMatrix uses a matrix you specify and creates a special matrix that can cache its inverse |
| 67 | +## |
| 68 | +## makeCacheMatrix returns a list with functions to |
| 69 | +## 1. set the value of the matrix |
| 70 | +## 2. get the value of the matrix |
| 71 | +## 3. set the value of the matrix inverse |
| 72 | +## 4. get the value of the matrix inverse |
| 73 | +makeCacheMatrix <- function(m = matrix()) { |
| 74 | + i <- NULL |
17 | 75 | set <- function(y) { |
18 | | - x <<- y |
19 | | - m <<- NULL |
| 76 | + m <<- y |
| 77 | + i <<- NULL |
20 | 78 | } |
21 | | - get <- function() x |
22 | | - setmatrixInverse <- function(calcMatrixInverse) m <<- matrixInverse |
23 | | - getmatrixInverse <- function() m |
| 79 | + get <- function() m |
| 80 | + setinverse <- function(solve) i <<- solve |
| 81 | + getinverse <- function() i |
24 | 82 | list(set = set, get = get, |
25 | | - setmatrixInverse = setmatrixInverse, |
26 | | - getmatrixInverse = getmatrixInverse) |
| 83 | + setinverse = setinverse, |
| 84 | + getinverse = getinverse) |
27 | 85 | } |
28 | | - |
29 | | - |
30 | 86 | ## Write a short comment describing this function |
31 | | - |
32 | | -The following function calculates the matrix inverse of the special "vector" |
33 | | -created with the above function. However, it first checks to see if the |
34 | | -matrix inverse has already been calculated. If so, it `get`s the matrix inverse from the |
35 | | -cache and skips the computation. Otherwise, it calculates the matrix inverse of |
36 | | -the data and sets the value of the matrix inverse in the cache via the `setMatrixInverse` |
37 | | -function. |
38 | | - |
39 | | -cacheSolve <- function(x, ...) { |
40 | | - ## Return a matrix that is the inverse of 'x' |
41 | | - m <- x$matrixInverse() |
42 | | - if(!is.null(m)) { |
| 87 | +## The cacheSolve function calculates the inverse of the special matrix returned by makeCacheMatrix. |
| 88 | +## If the inverse has been calculated before, it will be retrieved from a cache. Otherwise, the inverse |
| 89 | +## is calculated, cached, and returned. |
| 90 | +cacheSolve <- function(m, ...) { |
| 91 | + i <- m$getinverse() |
| 92 | + if(!is.null(i)) { |
43 | 93 | message("getting cached data") |
44 | | - return(m) |
| 94 | + return(i) |
45 | 95 | } |
46 | | - data <- x$get() |
47 | | - m <- calcMatrixInverse(data, ...) |
48 | | - x$setmatrixInverse(m) |
49 | | - m |
| 96 | + data <- m$get() |
| 97 | + i <- solve(data, ...) |
| 98 | + m$setinverse(i) |
| 99 | + i |
50 | 100 | } |
0 commit comments