forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.html
More file actions
262 lines (206 loc) · 6.43 KB
/
Copy pathREADME.html
File metadata and controls
262 lines (206 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<!DOCTYPE html>
<!-- saved from url=(0014)about:internet -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="x-ua-compatible" content="IE=9" >
<title>Introduction</title>
<style type="text/css">
body, td {
font-family: sans-serif;
background-color: white;
font-size: 12px;
margin: 8px;
}
tt, code, pre {
font-family: 'DejaVu Sans Mono', 'Droid Sans Mono', 'Lucida Console', Consolas, Monaco, monospace;
}
h1 {
font-size:2.2em;
}
h2 {
font-size:1.8em;
}
h3 {
font-size:1.4em;
}
h4 {
font-size:1.0em;
}
h5 {
font-size:0.9em;
}
h6 {
font-size:0.8em;
}
a:visited {
color: rgb(50%, 0%, 50%);
}
pre {
margin-top: 0;
max-width: 95%;
border: 1px solid #ccc;
white-space: pre-wrap;
}
pre code {
display: block; padding: 0.5em;
}
code.r, code.cpp {
background-color: #F8F8F8;
}
table, td, th {
border: none;
}
blockquote {
color:#666666;
margin:0;
padding-left: 1em;
border-left: 0.5em #EEE solid;
}
hr {
height: 0px;
border-bottom: none;
border-top-width: thin;
border-top-style: dotted;
border-top-color: #999999;
}
@media print {
* {
background: transparent !important;
color: black !important;
filter:none !important;
-ms-filter: none !important;
}
body {
font-size:12pt;
max-width:100%;
}
a, a:visited {
text-decoration: underline;
}
hr {
visibility: hidden;
page-break-before: always;
}
pre, blockquote {
padding-right: 1em;
page-break-inside: avoid;
}
tr, img {
page-break-inside: avoid;
}
img {
max-width: 100% !important;
}
@page :left {
margin: 15mm 20mm 15mm 10mm;
}
@page :right {
margin: 15mm 10mm 15mm 20mm;
}
p, h2, h3 {
orphans: 3; widows: 3;
}
h2, h3 {
page-break-after: avoid;
}
}
</style>
</head>
<body>
<h3>Introduction</h3>
<p>This second programming assignment will require you to write an R
function that is able to cache potentially time-consuming computations.
For example, taking the mean of a numeric vector is typically a fast
operation. However, for a very long vector, it may take too long to
compute the mean, especially if it has to be computed repeatedly (e.g.
in a loop). If the contents of a vector are not changing, it may make
sense to cache the value of the mean so that when we need it again, it
can be looked up in the cache rather than recomputed. In this
Programming Assignment you will take advantage of the scoping rules of
the R language and how they can be manipulated to preserve state inside
of an R object.</p>
<h3>Example: Caching the Mean of a Vector</h3>
<p>In this example we introduce the <code><<-</code> operator which can be used to
assign a value to an object in an environment that is different from the
current environment. Below are two functions that are used to create a
special object that stores a numeric vector and caches its mean.</p>
<p>The first function, <code>makeVector</code> creates a special “vector”, which is
really a list containing a function to</p>
<ol>
<li> set the value of the vector</li>
<li> get the value of the vector</li>
<li> set the value of the mean</li>
<li> get the value of the mean</li>
</ol>
<!-- -->
<pre><code>makeVector <- function(x = numeric()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setmean <- function(mean) m <<- mean
getmean <- function() m
list(set = set, get = get,
setmean = setmean,
getmean = getmean)
}
</code></pre>
<p>The following function calculates the mean of the special “vector”
created with the above function. However, it first checks to see if the
mean has already been calculated. If so, it <code>get</code>s the mean from the
cache and skips the computation. Otherwise, it calculates the mean of
the data and sets the value of the mean in the cache via the <code>setmean</code>
function.</p>
<pre><code>cachemean <- function(x, ...) {
m <- x$getmean()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- mean(data, ...)
x$setmean(m)
m
}
</code></pre>
<h3>Assignment: Caching the Inverse of a Matrix</h3>
<p>Matrix inversion is usually a costly computation and there may be some
benefit to caching the inverse of a matrix rather than computing it
repeatedly (there are also alternatives to matrix inversion that we will
not discuss here). Your assignment is to write a pair of functions that
cache the inverse of a matrix.</p>
<p>Write the following functions:</p>
<ol>
<li> <code>makeCacheMatrix</code>: This function creates a special “matrix” object
that can cache its inverse.</li>
<li> <code>cacheSolve</code>: This function computes the inverse of the special
“matrix” returned by <code>makeCacheMatrix</code> above. If the inverse has
already been calculated (and the matrix has not changed), then
<code>cacheSolve</code> should retrieve the inverse from the cache.</li>
</ol>
<p>Computing the inverse of a square matrix can be done with the <code>solve</code>
function in R. For example, if <code>X</code> is a square invertible matrix, then
<code>solve(X)</code> returns its inverse.</p>
<p>For this assignment, assume that the matrix supplied is always
invertible.</p>
<p>In order to complete this assignment, you must do the following:</p>
<ol>
<li> Fork the GitHub repository containing the stub R files at
<a href="https://github.com/rdpeng/ProgrammingAssignment2">https://github.com/rdpeng/ProgrammingAssignment2</a>
to create a copy under your own account.</li>
<li> Clone your forked GitHub repository to your computer so that you can
edit the files locally on your own machine.</li>
<li> Edit the R file contained in the git repository and place your
solution in that file (please do not rename the file).</li>
<li> Commit your completed R file into YOUR git repository and push your
git branch to the GitHub repository under your account.</li>
<li> Submit to Coursera the URL to your GitHub repository that contains
the completed R code for the assignment.</li>
</ol>
<h3>Grading</h3>
<p>This assignment will be graded via peer assessment.</p>
</body>
</html>