generated from psu-stat380/template
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathsol.html
1185 lines (1146 loc) · 58 KB
/
sol.html
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.2.313">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="Solutions">
<title>Homework 1 - Solutions</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<script src="sol_files/libs/clipboard/clipboard.min.js"></script>
<script src="sol_files/libs/quarto-html/quarto.js"></script>
<script src="sol_files/libs/quarto-html/popper.min.js"></script>
<script src="sol_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="sol_files/libs/quarto-html/anchor.min.js"></script>
<link href="sol_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="sol_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="sol_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="sol_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="sol_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script>
</head>
<body>
<header id="title-block-header" class="quarto-title-block default page-columns page-full">
<div class="quarto-title-banner page-columns page-full">
<div class="quarto-title column-body">
<h1 class="title">Homework 1 - Solutions</h1>
</div>
</div>
<div class="quarto-title-meta">
<div>
<div class="quarto-title-meta-heading">Author</div>
<div class="quarto-title-meta-contents">
<p><span style="background-color: yellow;">Solutions</span> </p>
</div>
</div>
</div>
</header><div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#question-1" id="toc-question-1" class="nav-link active" data-scroll-target="#question-1">Question 1</a></li>
<li><a href="#question-2" id="toc-question-2" class="nav-link" data-scroll-target="#question-2">Question 2</a></li>
<li><a href="#question-3" id="toc-question-3" class="nav-link" data-scroll-target="#question-3">Question 3</a></li>
<li><a href="#appendix" id="toc-appendix" class="nav-link" data-scroll-target="#appendix">Appendix</a></li>
</ul>
</nav>
</div>
<main class="content quarto-banner-title-block" id="quarto-document-content">
<p><a href="https://github.com/psu-stat380/hw-1">Link to the Github repository</a></p>
<hr>
<section id="libraries" class="level4">
<h4 class="anchored" data-anchor-id="libraries">Libraries</h4>
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-1_456b66efc2eae957f809e21aea95b738">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(dplyr)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(knitr)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p><br><br></p>
<hr>
</section>
<section id="question-1" class="level2">
<h2 class="anchored" data-anchor-id="question-1">Question 1</h2>
<div class="callout-tip callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
20 points
</div>
</div>
<div class="callout-body-container callout-body">
</div>
</div>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
20 points
</div>
</div>
<div class="callout-body-container callout-body">
<p>This has been shown in class.</p>
</div>
</div>
<hr>
</section>
<section id="question-2" class="level2">
<h2 class="anchored" data-anchor-id="question-2">Question 2</h2>
<div class="callout-tip callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
30 points
</div>
</div>
<div class="callout-body-container callout-body">
</div>
</div>
<p>Consider the following vector</p>
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-2_6d86d7d17ad5c31d0dae456e27da473b">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>my_vec <span class="ot"><-</span> <span class="fu">c</span>(</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> <span class="st">"+0.07"</span>,</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a> <span class="st">"-0.07"</span>,</span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a> <span class="st">"+0.25"</span>,</span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"-0.84"</span>,</span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a> <span class="st">"+0.32"</span>,</span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a> <span class="st">"-0.24"</span>,</span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a> <span class="st">"-0.97"</span>,</span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a> <span class="st">"-0.36"</span>,</span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a> <span class="st">"+1.76"</span>,</span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a> <span class="st">"-0.36"</span></span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>For the following questions, provide your answers in a code cell.</p>
<ol type="1">
<li>What data type does the vector contain?</li>
</ol>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
3 points
</div>
</div>
<div class="callout-body-container callout-body">
<p><code>my_vec</code> is a vector of <code>character</code>s</p>
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-3_0678a76bbf00355dd6ad437390d5e795">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">typeof</span>(my_vec)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "character"</code></pre>
</div>
</div>
</div>
</div>
<hr>
<ol start="2" type="1">
<li>Create two new vectors called <code>my_vec_double</code> and <code>my_vec_int</code> which converts <code>my_vec</code> to Double & Integer types, respectively.</li>
</ol>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
10 points
</div>
</div>
<div class="callout-body-container callout-body">
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-4_5e3e22a2d05ef8fc0c60a4bcd149e2ea">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>my_vec_int <span class="ot"><-</span> <span class="fu">as.integer</span>(my_vec)</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>my_vec_int</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] 0 0 0 0 0 0 0 0 1 0</code></pre>
</div>
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>my_vec_double <span class="ot"><-</span> <span class="fu">as.double</span>(my_vec)</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>my_vec_double</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] 0.07 -0.07 0.25 -0.84 0.32 -0.24 -0.97 -0.36 1.76 -0.36</code></pre>
</div>
</div>
</div>
</div>
<hr>
<ol start="3" type="1">
<li>Create a new vector <code>my_vec_bool</code> which comprises of:
<ul>
<li><code>TRUE</code>if an element in <code>my_vec_double</code> is <span class="math inline">\(\le 0\)</span></li>
<li><code>FALSE</code> if an element in <code>my_vec_double</code> is <span class="math inline">\(\ge 0\)</span></li>
</ul>
How many elements of <code>my_vec_double</code> are greater than zero?</li>
</ol>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
10 points
</div>
</div>
<div class="callout-body-container callout-body">
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-5_da3408487718c258133e94e34030aa9b">
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>my_vec_bool <span class="ot"><-</span> my_vec_double <span class="sc"><</span> <span class="dv">0</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a>my_vec_bool</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] FALSE TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sum</span>(<span class="sc">!</span>my_vec_bool)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 4</code></pre>
</div>
</div>
</div>
</div>
<hr>
<ol start="4" type="1">
<li>Sort the values of <code>my_vec_double</code> in ascending order.</li>
</ol>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
7 points
</div>
</div>
<div class="callout-body-container callout-body">
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-6_a5b36bf2b8adeceed572bbac642df952">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sort</span>(my_vec_double, <span class="at">decreasing=</span><span class="cn">FALSE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] -0.97 -0.84 -0.36 -0.36 -0.24 -0.07 0.07 0.25 0.32 1.76</code></pre>
</div>
</div>
</div>
</div>
<p><br><br><br><br> <br><br><br><br></p>
<hr>
</section>
<section id="question-3" class="level2">
<h2 class="anchored" data-anchor-id="question-3">Question 3</h2>
<div class="callout-tip callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
50 points
</div>
</div>
<div class="callout-body-container callout-body">
</div>
</div>
<p>In this question we will get a better understanding of how <code>R</code> handles large data structures in memory.</p>
<ol type="1">
<li>Provide <code>R</code> code to construct the following matrices: <span class="math display">\[
\begin{bmatrix}
1 & 2 & 3\\
4 & 5 & 6\\
7 & 8 & 9\\
\end{bmatrix}
\quad \text{ and } \quad
\begin{bmatrix}
1 & 2 & 3 & 4 & 5 & \dots & 100\\
1 & 4 & 9 & 16 & 25 & \dots & 10000\\
\end{bmatrix}
\]</span></li>
</ol>
<div class="callout-warning callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>Recall the discussion in class on how <code>R</code> fills in matrices</p>
</div>
</div>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
10 points
</div>
</div>
<div class="callout-body-container callout-body">
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-7_325f567d5614a3b3ffedcbb93da70d17">
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a>A <span class="ot"><-</span> <span class="fu">matrix</span>(</span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a> <span class="dv">1</span><span class="sc">:</span><span class="dv">9</span>, <span class="at">nrow=</span><span class="dv">3</span>, <span class="at">byrow=</span><span class="cn">TRUE</span></span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb15-4"><a href="#cb15-4" aria-hidden="true" tabindex="-1"></a>A <span class="sc">%>%</span> <span class="fu">kable</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<table class="table table-sm table-striped">
<tbody>
<tr class="odd">
<td style="text-align: right;">1</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">3</td>
</tr>
<tr class="even">
<td style="text-align: right;">4</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">6</td>
</tr>
<tr class="odd">
<td style="text-align: right;">7</td>
<td style="text-align: right;">8</td>
<td style="text-align: right;">9</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-8_b007b5285651d8a56aec902fa86ab439">
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a>vals <span class="ot"><-</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">100</span></span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a>B <span class="ot"><-</span> <span class="fu">matrix</span>(</span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">c</span>(vals, vals<span class="sc">^</span><span class="dv">2</span>), <span class="at">nrow=</span><span class="dv">2</span>, <span class="at">byrow=</span>T</span>
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb16-5"><a href="#cb16-5" aria-hidden="true" tabindex="-1"></a>B <span class="sc">%>%</span> <span class="fu">kable</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<table class="table table-sm table-striped">
<colgroup>
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 0%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
<col style="width: 1%">
</colgroup>
<tbody>
<tr class="odd">
<td style="text-align: right;">1</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">3</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">7</td>
<td style="text-align: right;">8</td>
<td style="text-align: right;">9</td>
<td style="text-align: right;">10</td>
<td style="text-align: right;">11</td>
<td style="text-align: right;">12</td>
<td style="text-align: right;">13</td>
<td style="text-align: right;">14</td>
<td style="text-align: right;">15</td>
<td style="text-align: right;">16</td>
<td style="text-align: right;">17</td>
<td style="text-align: right;">18</td>
<td style="text-align: right;">19</td>
<td style="text-align: right;">20</td>
<td style="text-align: right;">21</td>
<td style="text-align: right;">22</td>
<td style="text-align: right;">23</td>
<td style="text-align: right;">24</td>
<td style="text-align: right;">25</td>
<td style="text-align: right;">26</td>
<td style="text-align: right;">27</td>
<td style="text-align: right;">28</td>
<td style="text-align: right;">29</td>
<td style="text-align: right;">30</td>
<td style="text-align: right;">31</td>
<td style="text-align: right;">32</td>
<td style="text-align: right;">33</td>
<td style="text-align: right;">34</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">36</td>
<td style="text-align: right;">37</td>
<td style="text-align: right;">38</td>
<td style="text-align: right;">39</td>
<td style="text-align: right;">40</td>
<td style="text-align: right;">41</td>
<td style="text-align: right;">42</td>
<td style="text-align: right;">43</td>
<td style="text-align: right;">44</td>
<td style="text-align: right;">45</td>
<td style="text-align: right;">46</td>
<td style="text-align: right;">47</td>
<td style="text-align: right;">48</td>
<td style="text-align: right;">49</td>
<td style="text-align: right;">50</td>
<td style="text-align: right;">51</td>
<td style="text-align: right;">52</td>
<td style="text-align: right;">53</td>
<td style="text-align: right;">54</td>
<td style="text-align: right;">55</td>
<td style="text-align: right;">56</td>
<td style="text-align: right;">57</td>
<td style="text-align: right;">58</td>
<td style="text-align: right;">59</td>
<td style="text-align: right;">60</td>
<td style="text-align: right;">61</td>
<td style="text-align: right;">62</td>
<td style="text-align: right;">63</td>
<td style="text-align: right;">64</td>
<td style="text-align: right;">65</td>
<td style="text-align: right;">66</td>
<td style="text-align: right;">67</td>
<td style="text-align: right;">68</td>
<td style="text-align: right;">69</td>
<td style="text-align: right;">70</td>
<td style="text-align: right;">71</td>
<td style="text-align: right;">72</td>
<td style="text-align: right;">73</td>
<td style="text-align: right;">74</td>
<td style="text-align: right;">75</td>
<td style="text-align: right;">76</td>
<td style="text-align: right;">77</td>
<td style="text-align: right;">78</td>
<td style="text-align: right;">79</td>
<td style="text-align: right;">80</td>
<td style="text-align: right;">81</td>
<td style="text-align: right;">82</td>
<td style="text-align: right;">83</td>
<td style="text-align: right;">84</td>
<td style="text-align: right;">85</td>
<td style="text-align: right;">86</td>
<td style="text-align: right;">87</td>
<td style="text-align: right;">88</td>
<td style="text-align: right;">89</td>
<td style="text-align: right;">90</td>
<td style="text-align: right;">91</td>
<td style="text-align: right;">92</td>
<td style="text-align: right;">93</td>
<td style="text-align: right;">94</td>
<td style="text-align: right;">95</td>
<td style="text-align: right;">96</td>
<td style="text-align: right;">97</td>
<td style="text-align: right;">98</td>
<td style="text-align: right;">99</td>
<td style="text-align: right;">100</td>
</tr>
<tr class="even">
<td style="text-align: right;">1</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">9</td>
<td style="text-align: right;">16</td>
<td style="text-align: right;">25</td>
<td style="text-align: right;">36</td>
<td style="text-align: right;">49</td>
<td style="text-align: right;">64</td>
<td style="text-align: right;">81</td>
<td style="text-align: right;">100</td>
<td style="text-align: right;">121</td>
<td style="text-align: right;">144</td>
<td style="text-align: right;">169</td>
<td style="text-align: right;">196</td>
<td style="text-align: right;">225</td>
<td style="text-align: right;">256</td>
<td style="text-align: right;">289</td>
<td style="text-align: right;">324</td>
<td style="text-align: right;">361</td>
<td style="text-align: right;">400</td>
<td style="text-align: right;">441</td>
<td style="text-align: right;">484</td>
<td style="text-align: right;">529</td>
<td style="text-align: right;">576</td>
<td style="text-align: right;">625</td>
<td style="text-align: right;">676</td>
<td style="text-align: right;">729</td>
<td style="text-align: right;">784</td>
<td style="text-align: right;">841</td>
<td style="text-align: right;">900</td>
<td style="text-align: right;">961</td>
<td style="text-align: right;">1024</td>
<td style="text-align: right;">1089</td>
<td style="text-align: right;">1156</td>
<td style="text-align: right;">1225</td>
<td style="text-align: right;">1296</td>
<td style="text-align: right;">1369</td>
<td style="text-align: right;">1444</td>
<td style="text-align: right;">1521</td>
<td style="text-align: right;">1600</td>
<td style="text-align: right;">1681</td>
<td style="text-align: right;">1764</td>
<td style="text-align: right;">1849</td>
<td style="text-align: right;">1936</td>
<td style="text-align: right;">2025</td>
<td style="text-align: right;">2116</td>
<td style="text-align: right;">2209</td>
<td style="text-align: right;">2304</td>
<td style="text-align: right;">2401</td>
<td style="text-align: right;">2500</td>
<td style="text-align: right;">2601</td>
<td style="text-align: right;">2704</td>
<td style="text-align: right;">2809</td>
<td style="text-align: right;">2916</td>
<td style="text-align: right;">3025</td>
<td style="text-align: right;">3136</td>
<td style="text-align: right;">3249</td>
<td style="text-align: right;">3364</td>
<td style="text-align: right;">3481</td>
<td style="text-align: right;">3600</td>
<td style="text-align: right;">3721</td>
<td style="text-align: right;">3844</td>
<td style="text-align: right;">3969</td>
<td style="text-align: right;">4096</td>
<td style="text-align: right;">4225</td>
<td style="text-align: right;">4356</td>
<td style="text-align: right;">4489</td>
<td style="text-align: right;">4624</td>
<td style="text-align: right;">4761</td>
<td style="text-align: right;">4900</td>
<td style="text-align: right;">5041</td>
<td style="text-align: right;">5184</td>
<td style="text-align: right;">5329</td>
<td style="text-align: right;">5476</td>
<td style="text-align: right;">5625</td>
<td style="text-align: right;">5776</td>
<td style="text-align: right;">5929</td>
<td style="text-align: right;">6084</td>
<td style="text-align: right;">6241</td>
<td style="text-align: right;">6400</td>
<td style="text-align: right;">6561</td>
<td style="text-align: right;">6724</td>
<td style="text-align: right;">6889</td>
<td style="text-align: right;">7056</td>
<td style="text-align: right;">7225</td>
<td style="text-align: right;">7396</td>
<td style="text-align: right;">7569</td>
<td style="text-align: right;">7744</td>
<td style="text-align: right;">7921</td>
<td style="text-align: right;">8100</td>
<td style="text-align: right;">8281</td>
<td style="text-align: right;">8464</td>
<td style="text-align: right;">8649</td>
<td style="text-align: right;">8836</td>
<td style="text-align: right;">9025</td>
<td style="text-align: right;">9216</td>
<td style="text-align: right;">9409</td>
<td style="text-align: right;">9604</td>
<td style="text-align: right;">9801</td>
<td style="text-align: right;">10000</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<hr>
<p>In the next part, we will discover how knowledge of the way in which a matrix is stored in memory can inform better code choices. To this end, the following function takes an input <span class="math inline">\(n\)</span> and creates an <span class="math inline">\(n \times n\)</span> matrix with random entries.</p>
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-9_f81d4227614d50681ef0cfaad7ed1ea5">
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a>generate_matrix <span class="ot"><-</span> <span class="cf">function</span>(n){</span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span>(</span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">matrix</span>(</span>
<span id="cb17-4"><a href="#cb17-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">rnorm</span>(n<span class="sc">^</span><span class="dv">2</span>),</span>
<span id="cb17-5"><a href="#cb17-5" aria-hidden="true" tabindex="-1"></a> <span class="at">nrow=</span>n</span>
<span id="cb17-6"><a href="#cb17-6" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb17-7"><a href="#cb17-7" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb17-8"><a href="#cb17-8" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>For example:</p>
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-10_6a1c809e6f44fba97a46261fff553a52">
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="fu">generate_matrix</span>(<span class="dv">4</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [,1] [,2] [,3] [,4]
[1,] -1.43160647 -0.4556310 -0.5429311 0.6639006
[2,] -0.64468946 1.8242152 -0.3825676 1.0653859
[3,] 0.01427669 0.5192264 -0.4096663 1.8561722
[4,] -0.72349266 -1.0244723 0.6784307 -0.3826536</code></pre>
</div>
</div>
<p>Let <code>M</code> be a fixed <span class="math inline">\(50 \times 50\)</span> matrix</p>
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-11_01d36b16c2b84f2cc7c2487df2315326">
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>M <span class="ot"><-</span> <span class="fu">generate_matrix</span>(<span class="dv">50</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<hr>
<ol start="2" type="1">
<li>Write a function <code>row_wise_scan</code> which scans the entries of <code>M</code> one row after another and outputs the number of elements whose value is <span class="math inline">\(\ge 0\)</span>.</li>
</ol>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
15 points
</div>
</div>
<div class="callout-body-container callout-body">
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-12_b3452ebc9e824b28d6e1ca86b5a73246">
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a>row_wise_scan <span class="ot"><-</span> <span class="cf">function</span>(x){</span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a> n <span class="ot"><-</span> <span class="fu">nrow</span>(x)</span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a> m <span class="ot"><-</span> <span class="fu">ncol</span>(x)</span>
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a> count <span class="ot"><-</span> <span class="dv">0</span></span>
<span id="cb21-6"><a href="#cb21-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>n){</span>
<span id="cb21-7"><a href="#cb21-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span>(j <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>m){</span>
<span id="cb21-8"><a href="#cb21-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(x[i, j] <span class="sc">></span> <span class="dv">0</span>){</span>
<span id="cb21-9"><a href="#cb21-9" aria-hidden="true" tabindex="-1"></a> count <span class="ot"><-</span> count <span class="sc">+</span> <span class="dv">1</span> </span>
<span id="cb21-10"><a href="#cb21-10" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb21-11"><a href="#cb21-11" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb21-12"><a href="#cb21-12" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb21-13"><a href="#cb21-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span>(count)</span>
<span id="cb21-14"><a href="#cb21-14" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb21-15"><a href="#cb21-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-16"><a href="#cb21-16" aria-hidden="true" tabindex="-1"></a><span class="fu">row_wise_scan</span>(M)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1231</code></pre>
</div>
</div>
</div>
</div>
<hr>
<ol start="3" type="1">
<li>Similarly, write a function <code>col_wise_scan</code> which does exactly the same thing but scans the entries of <code>M</code> one column after another</li>
</ol>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
15 points
</div>
</div>
<div class="callout-body-container callout-body">
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-13_f71b64d43c98e4d0a59d311e4e98dffe">
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a>col_wise_scan <span class="ot"><-</span> <span class="cf">function</span>(x){</span>
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a> n <span class="ot"><-</span> <span class="fu">nrow</span>(x)</span>
<span id="cb23-3"><a href="#cb23-3" aria-hidden="true" tabindex="-1"></a> m <span class="ot"><-</span> <span class="fu">ncol</span>(x)</span>
<span id="cb23-4"><a href="#cb23-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-5"><a href="#cb23-5" aria-hidden="true" tabindex="-1"></a> count <span class="ot"><-</span> <span class="dv">0</span></span>
<span id="cb23-6"><a href="#cb23-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span>(j <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>m){</span>
<span id="cb23-7"><a href="#cb23-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>n){</span>
<span id="cb23-8"><a href="#cb23-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(x[i, j] <span class="sc">></span> <span class="dv">0</span>){</span>
<span id="cb23-9"><a href="#cb23-9" aria-hidden="true" tabindex="-1"></a> count <span class="ot"><-</span> count <span class="sc">+</span> <span class="dv">1</span> </span>
<span id="cb23-10"><a href="#cb23-10" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb23-11"><a href="#cb23-11" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb23-12"><a href="#cb23-12" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb23-13"><a href="#cb23-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span>(count)</span>
<span id="cb23-14"><a href="#cb23-14" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb23-15"><a href="#cb23-15" aria-hidden="true" tabindex="-1"></a><span class="fu">col_wise_scan</span>(M)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1231</code></pre>
</div>
</div>
</div>
</div>
<hr>
<p>You can check if your code is doing what it’s supposed to using the function here</p>
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-14_011d39e2e0e0ad87d6530ba3a6ea2f17">
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sapply</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">100</span>, <span class="cf">function</span>(i) {</span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a> x <span class="ot"><-</span> <span class="fu">generate_matrix</span>(<span class="dv">100</span>)</span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">row_wise_scan</span>(x) <span class="sc">==</span> <span class="fu">col_wise_scan</span>(x)</span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a>}) <span class="sc">%>%</span> sum <span class="sc">==</span> <span class="dv">100</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div>
<hr>
<ol start="4" type="1">
<li>Between <code>col_wise_scan</code> and <code>row_wise_scan</code>, which function do you expect to take shorter to run? Why?</li>
</ol>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
5 points
</div>
</div>
<div class="callout-body-container callout-body">
<p>As discussed in class, R stroes data in a column-wise fashion. Therefore, we should expect <code>col_wise_scan</code> to be faster than <code>row_wise_scan</code>.</p>
</div>
</div>
<hr>
<ol start="5" type="1">
<li>Write a function <code>time_scan</code> which takes in a method <code>f</code> and a matrix <code>M</code> and outputs the amount of time taken to run <code>f(M)</code></li>
</ol>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
5 points
</div>
</div>
<div class="callout-body-container callout-body">
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-15_eab464ca01f9dbf10418ca3302a5cf3c">
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a>time_scan <span class="ot"><-</span> <span class="cf">function</span>(f, M){</span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a> initial_time <span class="ot"><-</span> <span class="fu">Sys.time</span>()</span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">f</span>(M)</span>
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a> final_time <span class="ot"><-</span> <span class="fu">Sys.time</span>()</span>
<span id="cb27-5"><a href="#cb27-5" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb27-6"><a href="#cb27-6" aria-hidden="true" tabindex="-1"></a> total_time_taken <span class="ot"><-</span> final_time <span class="sc">-</span> initial_time</span>
<span id="cb27-7"><a href="#cb27-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span>(total_time_taken)</span>
<span id="cb27-8"><a href="#cb27-8" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Provide your output to</p>
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-16_6f4815bdee7bd13748ffe173e395393d">
<div class="sourceCode cell-code" id="cb28"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="fu">list</span>(</span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a> <span class="at">row_wise_time =</span> <span class="fu">time_scan</span>(row_wise_scan, M),</span>
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a> <span class="at">col_wise_time =</span> <span class="fu">time_scan</span>(col_wise_scan, M)</span>
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>$row_wise_time
Time difference of 0.001976013 secs
$col_wise_time
Time difference of 0.0009469986 secs</code></pre>
</div>
</div>
<p>Which took longer to run?</p>
<blockquote class="blockquote">
<p><code>row_wise_scan</code> took longer.</p>
</blockquote>
</div>
</div>
<hr>
<ol start="6" type="1">
<li>Repeat this experiment now when:
<ul>
<li><code>M</code> is a <span class="math inline">\(100 \times 100\)</span> matrix</li>
<li><code>M</code> is a <span class="math inline">\(1000 \times 1000\)</span> matrix</li>
<li><code>M</code> is a <span class="math inline">\(5000 \times 5000\)</span> matrix</li>
</ul></li>
</ol>
<p>What can you conclude?</p>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
5 points
</div>
</div>
<div class="callout-body-container callout-body">
<p>In all of the following illustrations, we can see that <code>col_wise_scan</code> is consistently faster than <code>row_wise_scan</code>.</p>
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-17_77992e00ed02ba42ec48ef16c31f705b">
<div class="sourceCode cell-code" id="cb30"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a>M <span class="ot"><-</span> <span class="fu">generate_matrix</span>(<span class="dv">100</span>)</span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a><span class="fu">list</span>(</span>
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a> <span class="at">row_wise_time =</span> <span class="fu">time_scan</span>(row_wise_scan, M),</span>
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a> <span class="at">col_wise_time =</span> <span class="fu">time_scan</span>(col_wise_scan, M)</span>
<span id="cb30-5"><a href="#cb30-5" aria-hidden="true" tabindex="-1"></a>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>$row_wise_time
Time difference of 0.0005249977 secs
$col_wise_time
Time difference of 0.0005090237 secs</code></pre>
</div>
</div>
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-18_88b39c214d183157c5d1e8ec5bff502c">
<div class="sourceCode cell-code" id="cb32"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a>M <span class="ot"><-</span> <span class="fu">generate_matrix</span>(<span class="dv">1000</span>)</span>
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a><span class="fu">list</span>(</span>
<span id="cb32-3"><a href="#cb32-3" aria-hidden="true" tabindex="-1"></a> <span class="at">row_wise_time =</span> <span class="fu">time_scan</span>(row_wise_scan, M),</span>
<span id="cb32-4"><a href="#cb32-4" aria-hidden="true" tabindex="-1"></a> <span class="at">col_wise_time =</span> <span class="fu">time_scan</span>(col_wise_scan, M)</span>
<span id="cb32-5"><a href="#cb32-5" aria-hidden="true" tabindex="-1"></a>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>$row_wise_time
Time difference of 0.3041039 secs
$col_wise_time
Time difference of 0.209291 secs</code></pre>
</div>
</div>
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-19_5c081aaa2603f15965fd999294c829aa">
<div class="sourceCode cell-code" id="cb34"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a>M <span class="ot"><-</span> <span class="fu">generate_matrix</span>(<span class="dv">5000</span>)</span>
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a><span class="fu">list</span>(</span>
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a> <span class="at">row_wise_time =</span> <span class="fu">time_scan</span>(row_wise_scan, M),</span>
<span id="cb34-4"><a href="#cb34-4" aria-hidden="true" tabindex="-1"></a> <span class="at">col_wise_time =</span> <span class="fu">time_scan</span>(col_wise_scan, M)</span>
<span id="cb34-5"><a href="#cb34-5" aria-hidden="true" tabindex="-1"></a>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>$row_wise_time
Time difference of 8.536247 secs
$col_wise_time
Time difference of 2.6855 secs</code></pre>
</div>
</div>
</div>
</div>
<p>In general, there are a lot of external factors which influence the execution time, e.g., any other applications open on your machine, available system resources, etc. A more principled way to test the execution time is as follows.</p>
<div class="cell" data-hash="sol_cache/html/unnamed-chunk-20_7ae4b86f2bbff117af688800b14a5acf">
<div class="sourceCode cell-code" id="cb36"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(microbenchmark)</span>
<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb36-3"><a href="#cb36-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-4"><a href="#cb36-4" aria-hidden="true" tabindex="-1"></a>mbm <span class="ot"><-</span> <span class="fu">microbenchmark</span>(</span>
<span id="cb36-5"><a href="#cb36-5" aria-hidden="true" tabindex="-1"></a> <span class="at">row_wise =</span> <span class="fu">row_wise_scan</span>(<span class="fu">generate_matrix</span>(<span class="dv">10</span>)),</span>
<span id="cb36-6"><a href="#cb36-6" aria-hidden="true" tabindex="-1"></a> <span class="at">col_wise =</span> <span class="fu">col_wise_scan</span>(<span class="fu">generate_matrix</span>(<span class="dv">10</span>)),</span>
<span id="cb36-7"><a href="#cb36-7" aria-hidden="true" tabindex="-1"></a> <span class="at">times =</span> <span class="dv">50</span></span>
<span id="cb36-8"><a href="#cb36-8" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb36-9"><a href="#cb36-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-10"><a href="#cb36-10" aria-hidden="true" tabindex="-1"></a>mbm</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Unit: microseconds
expr min lq mean median uq max neval
row_wise 19.996 20.334 1265.69628 20.7955 21.336 36779.738 50
col_wise 19.807 20.405 62.51914 20.8895 21.139 2097.653 50</code></pre>
</div>
<div class="sourceCode cell-code" id="cb38"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a><span class="fu">autoplot</span>(mbm)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Coordinate system already present. Adding new coordinate system, which will
replace the existing one.</code></pre>
</div>
<div class="cell-output-display">
<p><img src="sol_files/figure-html/unnamed-chunk-20-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p><br><br><br><br> <br><br><br><br></p>
<hr>
</section>
<section id="appendix" class="level1">
<h1>Appendix</h1>
<div class="callout callout callout-style-simple no-icon callout-captioned">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-16-contents" aria-controls="callout-16" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>