Skip to content

Commit cc5aede

Browse files
author
Frank Hecker
committed
Write working makeCacheMatrix and cacheSolve functions
1 parent 7f657dd commit cc5aede

File tree

1 file changed

+106
-6
lines changed

1 file changed

+106
-6
lines changed

cachematrix.R

Lines changed: 106 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,115 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## cachematrix.R: Cache a matrix and its inverse
32

4-
## Write a short comment describing this function
3+
## These functions support caching of a matrix and its inverse in order to
4+
## avoid potentially-lenghty computations: A given matrix's inverse is
5+
## calculated once and then cached for future use.
6+
##
7+
## How to use:
8+
##
9+
## m <- matrix(c(1, 4, 5, 2, 0, 8, 3, 2, 5), 3, 3) # The original matrix
10+
## mcached <- makeCacheMatrix(m) # Cache the matrix
11+
## minv <- cacheSolve(mcached) # Find inverse and cache
12+
## minv2 <- cacheSolve(mcached) # Return cached inverse
13+
##
14+
## How it works:
15+
##
16+
## The makeCacheMatrix() and cacheSolve() functions use closures, defined
17+
## as "a function or reference to a function together with a referencing
18+
## environment--a table storing a reference to each of the non-local variables
19+
## (also called free variables or upvalues) of that function" (Wikipedia).
20+
##
21+
## When invoked makeCacheMatrix() defines four "getter" and "setter" functions
22+
## and then returns a list containing references to each of the four functions.
23+
## Each of the functions is bound to the environment associated with that
24+
## particular invocation of makeCacheMatrix(). The environment in turn contains
25+
## two variables x and inv used for storing the cached matrix and its inverse.
26+
## (These variables are defined in makeCacheMatrix() itself, and thus are
27+
## free variables with respect to the four getter and setter functions.)
28+
##
29+
## The variable x is set to the value of the matrix originally passed to
30+
## makeCacheMatrix(), and the variable inv is set to the inverse of the matrix
31+
## as calculated in the cacheSolve() function.
532

33+
34+
## Create an object to hold a matrix and its (cached) inverse.
635
makeCacheMatrix <- function(x = matrix()) {
736

8-
}
37+
# x and inv are variables in the (new) environment that is
38+
# instantiated each time makeCacheMatrix() is called.
39+
inv <- NULL
40+
41+
# Define getter and setter functions referencing x and inv.
42+
43+
# Set the object's matrix value and clear the cached inverse.
44+
set <- function(y) {
45+
# NOTE: We use the <<- assignment operator because we want to
46+
# set the variables x and inv in the environment associated
47+
# with this particular invocation of makeCacheMatrix(), the one
48+
# in which this and the other getter and setter functions are
49+
# being defined.
50+
#
51+
# (In other words, we want to treat x and inv as free variables
52+
# as far as this function is concerned. If the <- assignment
53+
# operator were used instead then x and inv would be created as
54+
# local variables in this function, and their values would not
55+
# be visible in the other getter and setter functions.)
56+
x <<- y
57+
inv <<- NULL
58+
}
59+
60+
# Retrieve the matrix value of the object.
61+
get <- function() {
62+
# Since x was not previously defined in this function it is
63+
# considered a free variable, and hence is looked up in and its
64+
# value retrieved from the environment associated with the
65+
# invocation of makeCacheMatrix() in which this function was
66+
# defined.
67+
x
68+
}
969

70+
# Cache the inverse of the object's matrix value.
71+
set_inverse <- function(inverse) {
72+
# See the above note for the set() function re use of <<-.
73+
inv <<- inverse
74+
}
1075

11-
## Write a short comment describing this function
76+
# Retrieve the cached inverse of the object's matrix value.
77+
get_inverse <- function() {
78+
# See the above note for the get() function re inv as a free
79+
# variable.
80+
inv
81+
}
1282

83+
# Return the new object as a list of getter and setter functions
84+
# bound to the environment associated with this particular invocation
85+
# of makeCacheMatrix().
86+
#
87+
# For convenience give each of the list elements a name that matches
88+
# that of the underlying function. If v <- makeCacheMatrix(x) we can
89+
# then reference (e.g.) the set() and get() functions as v$set() and
90+
# v$get() respectively.
91+
list(set = set,
92+
get = get,
93+
set_inverse = set_inverse,
94+
get_inverse = get_inverse)
95+
}
96+
97+
98+
## Return the inverse of a CacheMatrix object's matrix value.
1399
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
100+
# Return the cached inverse if one is available.
101+
inv <- x$get_inverse()
102+
if(!is.null(inv)) {
103+
message("Getting cached inverse")
104+
return(inv)
105+
}
106+
107+
# There is no inverse cached, so we compute it from scratch. Any extra
108+
# arguments passed to cacheSolve() are passed on to solve().
109+
y <- x$get()
110+
inv <- solve(y, ...)
111+
112+
# Cache the resulting inverse and then return it.
113+
x$set_inverse(inv)
114+
inv
15115
}

0 commit comments

Comments
 (0)