Skip to content

Commit 018a0b2

Browse files
committed
note about numpy in-place ops
1 parent b75d40b commit 018a0b2

4 files changed

Lines changed: 61 additions & 32 deletions

File tree

Images/literature.png

6.53 KB
Loading

Images/literature_small.png

4.38 KB
Loading

tutorials/matrix_cheatsheet.md

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ Bezanson, J., Karpinski, S., Shah, V.B. and Edelman, A. (2012), “Julia: A fast
235235
<br>
236236
<br>
237237

238+
239+
238240
<table cellpadding="2" cellspacing="0" style="page-break-before: always">
239241
<col width="383">
240242
<col width="387">
@@ -243,7 +245,7 @@ Bezanson, J., Karpinski, S., Shah, V.B. and Edelman, A. (2012), “Julia: A fast
243245
<col width="511">
244246
<col width="690">
245247
<tr>
246-
<td height="57" bgcolor="#0066cc" style="border: none; padding: 0in">
248+
<td height="59" bgcolor="#0066cc" style="border: none; padding: 0in">
247249
<p align="center"><font color="#ffffff"><font face="Arial"><font size="5" style="font-size: 18pt"><b>Task</b></font></font></font></p>
248250
</td>
249251
<td bgcolor="#0066cc" style="border: none; padding: 0in">
@@ -264,9 +266,9 @@ Bezanson, J., Karpinski, S., Shah, V.B. and Edelman, A. (2012), “Julia: A fast
264266
</td>
265267
</tr>
266268
<tr>
267-
<td colspan="6" height="70" bgcolor="#ffffff" style="border: none; padding: 0in" id="creating">
269+
<td colspan="6" height="70" bgcolor="#ffffff" style="border: none; padding: 0in">
268270
<p align="center"><font face="Arial"><font size="4" style="font-size: 14pt"><b>CREATING
269-
MATRICES </b></font></font><br>[<a href='#cheat_overview'>back to cheat sheet overview</a>]</p>
271+
MATRICES</b></font></font></p>
270272
</td>
271273
</tr>
272274
<tr valign="top">
@@ -313,10 +315,15 @@ Bezanson, J., Karpinski, S., Shah, V.B. and Edelman, A. (2012), “Julia: A fast
313315
3</font></font></p>
314316
</td>
315317
<td bgcolor="#cfe7f5" style="border-top: none; border-bottom: none; border-left: 1px dotted #000000; border-right: 1px dotted #000000; padding: 0in 0.02in">
316-
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
317-
a = np.array([[1],[2],[3]])<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
318-
a<br>array([[1],<br>&nbsp;&nbsp; &nbsp; &nbsp; [2],<br>&nbsp;&nbsp;
319-
&nbsp; &nbsp; [3]])</font></font></p>
318+
<p align="left"><font color="#ff420e"><font face="Andale Mono"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font></font>
319+
<font face="Andale Mono"><font size="1" style="font-size: 8pt">a</font></font><font color="#2fff12">
320+
</font><font color="#000000"><font face="Andale Mono"><font size="1" style="font-size: 8pt">=
321+
np.array([1,2,3]).reshape(1,3)</font></font></font></p>
322+
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><br></font></font><font color="#ff420e"><font face="Andale Mono"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font></font>
323+
<font color="#000000"><font face="Andale Mono"><font size="1" style="font-size: 8pt">b.shape<br>(1,
324+
3)</font></font></font></p>
325+
<p align="left"><br>
326+
</p>
320327
</td>
321328
<td bgcolor="#cfe7f5" style="border-top: none; border-bottom: none; border-left: 1px dotted #000000; border-right: 1px dotted #000000; padding: 0in 0.02in">
322329
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><font color="#0000ff"><font size="2" style="font-size: 10pt"><b>R&gt;</b></font></font>
@@ -345,6 +352,14 @@ Bezanson, J., Karpinski, S., Shah, V.B. and Edelman, A. (2012), “Julia: A fast
345352
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
346353
b = np.array([1,2,3])<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
347354
b<br>array([1, 2, 3])</font></font></p>
355+
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt">#
356+
note that numpy doesn't have <br># explicit “row-vectors”, but
357+
1-D <br># arrays</font></font></p>
358+
<p style="margin-bottom: 0in"><font face="Liberation Serif, serif"><font size="3" style="font-size: 12pt"><font color="#ff420e"><font face="Andale Mono"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font></font><font color="#2fff12">
359+
</font><font color="#000000"><font face="Andale Mono"><font size="1" style="font-size: 8pt">b.shape</font></font></font></font></font></p>
360+
<p style="margin-bottom: 0in"><font face="Andale Mono"><font size="1" style="font-size: 8pt">(3,)</font></font></p>
361+
<p align="left"><br>
362+
</p>
348363
</td>
349364
<td bgcolor="#ffffff" style="border-top: none; border-bottom: none; border-left: 1px dotted #000000; border-right: 1px dotted #000000; padding: 0in 0.02in">
350365
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><font color="#0000ff"><font size="2" style="font-size: 10pt"><b>R&gt;</b></font></font>
@@ -434,7 +449,7 @@ Bezanson, J., Karpinski, S., Shah, V.B. and Edelman, A. (2012), “Julia: A fast
434449
</td>
435450
<td bgcolor="#cfe7f5" style="border-top: none; border-bottom: none; border-left: 1px dotted #000000; border-right: 1px dotted #000000; padding: 0in 0.02in">
436451
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
437-
np.ones([3,2])<br>array([[ 1.,&nbsp; 1.],<br>&nbsp;&nbsp; &nbsp; &nbsp;
452+
np.ones((3,2))<br>array([[ 1.,&nbsp; 1.],<br>&nbsp;&nbsp; &nbsp; &nbsp;
438453
[ 1.,&nbsp; 1.],<br>&nbsp;&nbsp; &nbsp; &nbsp; [ 1.,&nbsp; 1.]])</font></font></p>
439454
</td>
440455
<td bgcolor="#cfe7f5" style="border-top: none; border-bottom: none; border-left: 1px dotted #000000; border-right: 1px dotted #000000; padding: 0in 0.02in">
@@ -519,9 +534,9 @@ Bezanson, J., Karpinski, S., Shah, V.B. and Edelman, A. (2012), “Julia: A fast
519534
</td>
520535
</tr>
521536
<tr>
522-
<td colspan="6" height="128" bgcolor="#ffffff" style="border: none; padding: 0in" id='accessing'>
537+
<td colspan="6" height="128" bgcolor="#ffffff" style="border: none; padding: 0in">
523538
<p align="center"><font face="Arial"><font size="4" style="font-size: 14pt"><b>ACCESSING
524-
MATRIX ELEMENTS</b></font></font><br>[<a href='#cheat_overview'>back to cheat sheet overview</a>]</p>
539+
MATRIX ELEMENTS</b></font></font></p>
525540
</td>
526541
</tr>
527542
<tr valign="top">
@@ -714,9 +729,9 @@ Bezanson, J., Karpinski, S., Shah, V.B. and Edelman, A. (2012), “Julia: A fast
714729
</td>
715730
</tr>
716731
<tr>
717-
<td colspan="6" height="128" bgcolor="#ffffff" style="border: none; padding: 0in" id="manipulating">
732+
<td colspan="6" height="128" bgcolor="#ffffff" style="border: none; padding: 0in">
718733
<p align="center"><font face="Arial"><font size="4" style="font-size: 14pt"><b>MANIPULATING
719-
SHAPE AND DIMENSIONS</b></font></font><br>[<a href='#cheat_overview'>back to cheat sheet overview</a>]</p>
734+
SHAPE AND DIMENSIONS</b></font></font></p>
720735
</td>
721736
</tr>
722737
<tr valign="top">
@@ -772,10 +787,10 @@ Bezanson, J., Karpinski, S., Shah, V.B. and Edelman, A. (2012), “Julia: A fast
772787
A = np.array([[1,2,3],[4,5,6],[7,8,9]])<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
773788
A<br>array([[1, 2, 3],<br>&nbsp;&nbsp; &nbsp; &nbsp; [4, 5, 9],<br>&nbsp;&nbsp;
774789
&nbsp; &nbsp; [7, 8, 9]])<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
775-
total_elements = A.shape[0] * A.shape[1]<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
776-
B = A.reshape(1, total_elements)&nbsp;<br><br># or
777-
A.reshape(1,9)<br># Alternative: A.shape = (1,9)&nbsp;<br>#
778-
to&nbsp;change the array in place<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
790+
total_elements = np.prod(A.shape)</font></font></p>
791+
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
792+
B = A.reshape(1, total_elements)&nbsp;<br><br># alternative
793+
shortcut:<br># A.reshape(1,-1)<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
779794
B<br>array([[1, 2, 3, 4, 5, 6, 7, 8, 9]])</font></font></p>
780795
</td>
781796
<td bgcolor="#ffffff" style="border-top: none; border-bottom: none; border-left: 1px dotted #000000; border-right: 1px dotted #000000; padding: 0in 0.02in">
@@ -885,9 +900,9 @@ Bezanson, J., Karpinski, S., Shah, V.B. and Edelman, A. (2012), “Julia: A fast
885900
</td>
886901
</tr>
887902
<tr>
888-
<td colspan="6" height="114" bgcolor="#ffffff" style="border: none; padding: 0in" id='basic_ops'>
903+
<td colspan="6" height="114" bgcolor="#ffffff" style="border: none; padding: 0in">
889904
<p align="center"><font face="Arial"><font size="4" style="font-size: 14pt"><b>BASIC
890-
MATRIX OPERATIONS</b></font></font><br>[<a href='#cheat_overview'>back to cheat sheet overview</a>]</p>
905+
MATRIX OPERATIONS</b></font></font></p>
891906
</td>
892907
</tr>
893908
<tr valign="top">
@@ -912,6 +927,9 @@ Bezanson, J., Karpinski, S., Shah, V.B. and Edelman, A. (2012), “Julia: A fast
912927
A + 2<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
913928
A - 2<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
914929
A / 2</font></font></p>
930+
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt">#
931+
Note that NumPy was optimized for<br># in-place assignments<br>#
932+
e.g., A += A instead of <br># A = A + A</font></font></p>
915933
</td>
916934
<td bgcolor="#cfe7f5" style="border-top: none; border-bottom: none; border-left: 1px dotted #000000; border-right: 1px dotted #000000; padding: 0in 0.02in">
917935
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><font color="#0000ff"><font size="2" style="font-size: 10pt"><b>R&gt;</b></font></font>
@@ -1025,6 +1043,9 @@ Bezanson, J., Karpinski, S., Shah, V.B. and Edelman, A. (2012), “Julia: A fast
10251043
A + A<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
10261044
A - A<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
10271045
A / A</font></font></p>
1046+
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt">#
1047+
Note that NumPy was optimized for<br># in-place assignments<br>#
1048+
e.g., A += A instead of <br># A = A + A</font></font></p>
10281049
</td>
10291050
<td bgcolor="#ffffff" style="border-top: none; border-bottom: none; border-left: 1px dotted #000000; border-right: 1px dotted #000000; padding: 0in 0.02in">
10301051
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><font color="#0000ff"><font size="2" style="font-size: 10pt"><b>R&gt;</b></font></font>
@@ -1232,9 +1253,9 @@ Bezanson, J., Karpinski, S., Shah, V.B. and Edelman, A. (2012), “Julia: A fast
12321253
</td>
12331254
</tr>
12341255
<tr>
1235-
<td colspan="6" height="202" bgcolor="#ffffff" style="border: none; padding: 0in" id='advanced_ops'>
1256+
<td colspan="6" height="202" bgcolor="#ffffff" style="border: none; padding: 0in">
12361257
<p align="center"><font face="Arial"><font size="4" style="font-size: 14pt"><b>ADVANCED
1237-
MATRIX OPERATIONS</b></font></font><br>[<a href='#cheat_overview'>back to cheat sheet overview</a>]</p>
1258+
MATRIX OPERATIONS</b></font></font></p>
12381259
</td>
12391260
</tr>
12401261
<tr valign="top">
@@ -1328,7 +1349,7 @@ Bezanson, J., Karpinski, S., Shah, V.B. and Edelman, A. (2012), “Julia: A fast
13281349
</td>
13291350
</tr>
13301351
<tr valign="top">
1331-
<td height="539" bgcolor="#ffffff" style="border-top: none; border-bottom: none; border-left: none; border-right: 1px solid #000000; padding-top: 0in; padding-bottom: 0in; padding-left: 0in; padding-right: 0.02in">
1352+
<td height="541" bgcolor="#ffffff" style="border-top: none; border-bottom: none; border-left: none; border-right: 1px solid #000000; padding-top: 0in; padding-bottom: 0in; padding-left: 0in; padding-right: 0.02in">
13321353
<p align="left"><font face="Arial"><font size="2" style="font-size: 10pt"><b>Generating
13331354
a Gaussian dataset:<br><br>creating random vectors from the
13341355
multivariate normal<br>distribution given mean and covariance
@@ -1395,6 +1416,8 @@ Bezanson, J., Karpinski, S., Shah, V.B. and Edelman, A. (2012), “Julia: A fast
13951416
</td>
13961417
</tr>
13971418
</table>
1419+
1420+
13981421
<br>
13991422

14001423
<br><br><font size=0.6>

tutorials/matrix_cheatsheet_only.html

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<title></title>
66
<meta name="generator" content="LibreOffice 4.2.4.2 (MacOSX)">
77
<meta name="created" content="0;0">
8-
<meta name="changed" content="20140616;230046743144000">
8+
<meta name="changed" content="20140622;123733939958000">
99
<style type="text/css">
1010
<!--
1111
td p { color: #000000; font-family: "Verdana"; font-size: 12pt }
@@ -22,7 +22,7 @@
2222
<col width="511">
2323
<col width="690">
2424
<tr>
25-
<td height="58" bgcolor="#0066cc" style="border: none; padding: 0in">
25+
<td height="59" bgcolor="#0066cc" style="border: none; padding: 0in">
2626
<p align="center"><font color="#ffffff"><font face="Arial"><font size="5" style="font-size: 18pt"><b>Task</b></font></font></font></p>
2727
</td>
2828
<td bgcolor="#0066cc" style="border: none; padding: 0in">
@@ -92,12 +92,12 @@
9292
3</font></font></p>
9393
</td>
9494
<td bgcolor="#cfe7f5" style="border-top: none; border-bottom: none; border-left: 1px dotted #000000; border-right: 1px dotted #000000; padding: 0in 0.02in">
95-
<p align="left"><font color="#ff420e"><font face="Andale Mono"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font></font><font face="Andale Mono"><font size="1" style="font-size: 8pt">
96-
</font></font><font face="Andale Mono"><font size="1" style="font-size: 8pt">a</font></font><font color="#2fff12"><font face="AndaleMono, serif"><font size="4" style="font-size: 14pt">
97-
</font></font></font><font color="#000000"><font face="Andale Mono"><font size="1" style="font-size: 8pt">=
95+
<p align="left"><font color="#ff420e"><font face="Andale Mono"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font></font>
96+
<font face="Andale Mono"><font size="1" style="font-size: 8pt">a</font></font><font color="#2fff12">
97+
</font><font color="#000000"><font face="Andale Mono"><font size="1" style="font-size: 8pt">=
9898
np.array([1,2,3]).reshape(1,3)</font></font></font></p>
99-
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><br></font></font><font color="#ff420e"><font face="Andale Mono"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font></font><font face="Andale Mono"><font size="1" style="font-size: 8pt">
100-
</font></font><font color="#000000"><font face="Andale Mono"><font size="1" style="font-size: 8pt">b.shape<br>(1,
99+
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><br></font></font><font color="#ff420e"><font face="Andale Mono"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font></font>
100+
<font color="#000000"><font face="Andale Mono"><font size="1" style="font-size: 8pt">b.shape<br>(1,
101101
3)</font></font></font></p>
102102
<p align="left"><br>
103103
</p>
@@ -132,10 +132,10 @@
132132
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt">#
133133
note that numpy doesn't have <br># explicit “row-vectors”, but
134134
1-D <br># arrays</font></font></p>
135-
<p style="margin-bottom: 0in"><font face="Liberation Serif, serif"><font size="3" style="font-size: 12pt"><font color="#ff420e"><font face="Andale Mono"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font></font><font color="#2fff12"><font face="AndaleMono, serif"><font size="4" style="font-size: 14pt"><b>
136-
</b></font></font></font><font color="#000000"><font face="Andale Mono"><font size="1" style="font-size: 8pt">b.shape</font></font></font></font></font></p>
135+
<p style="margin-bottom: 0in"><font face="Liberation Serif, serif"><font size="3" style="font-size: 12pt"><font color="#ff420e"><font face="Andale Mono"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font></font><font color="#2fff12">
136+
</font><font color="#000000"><font face="Andale Mono"><font size="1" style="font-size: 8pt">b.shape</font></font></font></font></font></p>
137137
<p style="margin-bottom: 0in"><font face="Andale Mono"><font size="1" style="font-size: 8pt">(3,)</font></font></p>
138-
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><br></font></font><br>
138+
<p align="left"><br>
139139
</p>
140140
</td>
141141
<td bgcolor="#ffffff" style="border-top: none; border-bottom: none; border-left: 1px dotted #000000; border-right: 1px dotted #000000; padding: 0in 0.02in">
@@ -704,6 +704,9 @@
704704
A + 2<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
705705
A - 2<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
706706
A / 2</font></font></p>
707+
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt">#
708+
Note that NumPy was optimized for<br># in-place assignments<br>#
709+
e.g., A += A instead of <br># A = A + A</font></font></p>
707710
</td>
708711
<td bgcolor="#cfe7f5" style="border-top: none; border-bottom: none; border-left: 1px dotted #000000; border-right: 1px dotted #000000; padding: 0in 0.02in">
709712
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><font color="#0000ff"><font size="2" style="font-size: 10pt"><b>R&gt;</b></font></font>
@@ -817,6 +820,9 @@
817820
A + A<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
818821
A - A<br><br><font color="#ff420e"><font size="2" style="font-size: 10pt"><b>P&gt;</b></font></font>
819822
A / A</font></font></p>
823+
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt">#
824+
Note that NumPy was optimized for<br># in-place assignments<br>#
825+
e.g., A += A instead of <br># A = A + A</font></font></p>
820826
</td>
821827
<td bgcolor="#ffffff" style="border-top: none; border-bottom: none; border-left: 1px dotted #000000; border-right: 1px dotted #000000; padding: 0in 0.02in">
822828
<p align="left"><font face="Andale Mono"><font size="1" style="font-size: 8pt"><font color="#0000ff"><font size="2" style="font-size: 10pt"><b>R&gt;</b></font></font>
@@ -1120,7 +1126,7 @@
11201126
</td>
11211127
</tr>
11221128
<tr valign="top">
1123-
<td height="540" bgcolor="#ffffff" style="border-top: none; border-bottom: none; border-left: none; border-right: 1px solid #000000; padding-top: 0in; padding-bottom: 0in; padding-left: 0in; padding-right: 0.02in">
1129+
<td height="541" bgcolor="#ffffff" style="border-top: none; border-bottom: none; border-left: none; border-right: 1px solid #000000; padding-top: 0in; padding-bottom: 0in; padding-left: 0in; padding-right: 0.02in">
11241130
<p align="left"><font face="Arial"><font size="2" style="font-size: 10pt"><b>Generating
11251131
a Gaussian dataset:<br><br>creating random vectors from the
11261132
multivariate normal<br>distribution given mean and covariance

0 commit comments

Comments
 (0)