Skip to content

Commit fb44511

Browse files
committed
My solution to R Programming Assignment 2
1 parent 7f657dd commit fb44511

File tree

1 file changed

+188
-15
lines changed

1 file changed

+188
-15
lines changed

cachematrix.R

Lines changed: 188 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,188 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
7-
8-
}
9-
10-
11-
## Write a short comment describing this function
12-
13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
15-
}
1+
## makeCacheMatrix and cacheSolve work together to provide a
2+
## cached matrix solution, where the matrix solution (inverse)
3+
## is calculated once, remembered and reused.
4+
##
5+
## Example usage:
6+
##
7+
## > x <- matrix(c(1,0,5, 2,1,6, 3,4,0), nrow=3, ncol=3)
8+
##
9+
## > x
10+
## [,1] [,2] [,3]
11+
## [1,] 1 2 3
12+
## [2,] 0 1 4
13+
## [3,] 5 6 0
14+
##
15+
## > cm <- makeCacheMatrix(x)
16+
##
17+
## > cacheSolve(cm)
18+
## [,1] [,2] [,3]
19+
## [1,] -24 18 5
20+
## [2,] 20 -15 -4
21+
## [3,] -5 4 1
22+
##
23+
## > cacheSolve(cm)
24+
## getting cached data
25+
## [,1] [,2] [,3]
26+
## [1,] -24 18 5
27+
## [2,] 20 -15 -4
28+
## [3,] -5 4 1
29+
##
30+
31+
32+
## makeCacheMatrix()
33+
##
34+
## Takes this argument:
35+
##
36+
## mat - the matrix
37+
##
38+
## And creates a closure:
39+
##
40+
## http://en.wikipedia.org/wiki/Closure_(computer_programming)
41+
##
42+
## With an environment that contains:
43+
##
44+
## mat - the matrix
45+
## solution - the solution (inverse) of mat or NULL
46+
##
47+
## And returns a list that contains these functions:
48+
##
49+
## set - sets the matrix
50+
## get - gets the matrix
51+
## setSolution - sets the solution
52+
## getSolution - gets the solution
53+
54+
makeCacheMatrix <- function(mat = matrix()) {
55+
solution <- NULL
56+
set <- function(m) {
57+
mat <<- m
58+
solution <<- NULL
59+
}
60+
get <- function() mat
61+
setSolution <- function(sol) solution <<- sol
62+
getSolution <- function() solution
63+
list(set = set,
64+
get = get,
65+
setSolution = setSolution,
66+
getSolution = getSolution)
67+
}
68+
69+
## cacheSolve()
70+
##
71+
## Takes this argument:
72+
##
73+
## cMat, a cached matrix as created by makeCacheMatrix()
74+
##
75+
## and returns:
76+
##
77+
## the solution (matrix inverse)
78+
##
79+
## If a cached value exists, that value is returned, otherwise
80+
## the solution is calculated, stored in the cache, and returned.
81+
82+
cacheSolve <- function(cMat, ...) {
83+
#### Return a matrix that is the inverse of 'cMat'
84+
solution <- cMat$getSolution()
85+
if(!is.null(solution)) {
86+
message("getting cached data")
87+
return(solution)
88+
}
89+
mat <- cMat$get()
90+
solution <- solve(mat, ...)
91+
cMat$setSolution(solution)
92+
solution
93+
}
94+
95+
96+
## Extra Credit ###############################################################
97+
98+
## Above I've done what I think was expected and used the given
99+
## cahced mean example as a template. Thank you for taking the
100+
## time to review my work. If you're too busy you don't need to
101+
## review this alternative implementation, but I want to share a
102+
## couple changes that I made.
103+
##
104+
## While working on this assignment few questions came up:
105+
##
106+
## 1. What if there wasn't a separate cacheSolve function?
107+
## Instead, what if we added "getSolution" to the list
108+
## of functions returned by makeCacheMatrix and had it
109+
## implement the cache logic?
110+
##
111+
## 2. What if function list was created directly
112+
## As it is we create the functions, assign them names,
113+
## and use those names in the returned list. Why not
114+
## create the functions directly in the list? While
115+
## it looks a little weird at first, I've see it done
116+
## this way unapologetically in JavaScript libraries.
117+
##
118+
## Below is my implementation of these changes (which I've
119+
## tested and verified). I think it's simpler (it's certainly
120+
## less code).
121+
122+
## makeLazyMatrix()
123+
##
124+
## Takes these arguments:
125+
##
126+
## mat - the matrix
127+
##
128+
## And creates a closure:
129+
##
130+
## http://en.wikipedia.org/wiki/Closure_(computer_programming)
131+
##
132+
## With an environment that contains:
133+
##
134+
## mat - the matrix
135+
## solution - the solution (inverse) of mat or NULL
136+
##
137+
## And returns a list that contains these functions:
138+
##
139+
## set - sets the matrix
140+
## get - gets the matrix
141+
## getSolution - gets the (possibly cached) solution
142+
## clearCache - clears cache
143+
##
144+
## Example usage:
145+
##
146+
## > x <- matrix(c(1,0,5, 2,1,6, 3,4,0), nrow=3, ncol=3)
147+
##
148+
## > x
149+
## [,1] [,2] [,3]
150+
## [1,] 1 2 3
151+
## [2,] 0 1 4
152+
## [3,] 5 6 0
153+
##
154+
## > lm <- makeLazyMatrix(x)
155+
##
156+
## > cacheSolve(cm)
157+
## [,1] [,2] [,3]
158+
## [1,] -24 18 5
159+
## [2,] 20 -15 -4
160+
## [3,] -5 4 1
161+
##
162+
## > lm$getSolution()
163+
## [,1] [,2] [,3]
164+
## [1,] -24 18 5
165+
## [2,] 20 -15 -4
166+
## [3,] -5 4 1
167+
168+
makeLazyMatrix <- function(mat = matrix()) {
169+
solution <- NULL
170+
list(
171+
set = function(m) {
172+
mat <<- m
173+
solution <<- NULL
174+
},
175+
176+
get = function() mat,
177+
178+
getSolution = function(...) {
179+
if(is.null(solution)) {
180+
solution <<- solve(mat, ...)
181+
}
182+
solution
183+
},
184+
185+
clearCache = function() solution <<- NULL
186+
)
187+
}
188+

0 commit comments

Comments
 (0)