Data Programming Course  Check-in [6d9e084ded]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:aggiornate le slide delle lezioni 4 e 5 con i commenti
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:6d9e084ded9f21cbf3e257853aeca63b6e53dd96
User & Date: EnricoGiampieri 2017-03-16 15:36:18
Context
2017-03-16
15:37
inserito lo stub della seconda lezione extra su xarray check-in: 33ae3729a3 user: EnricoGiampieri tags: trunk
15:36
aggiornate le slide delle lezioni 4 e 5 con i commenti check-in: 6d9e084ded user: EnricoGiampieri tags: trunk
15:28
aggiunte le slide dell'esercitazione finale check-in: a5c76a0189 user: EnricoGiampieri tags: trunk
Changes

Changes to Esercitazione finale.html.

11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
.....
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
<p>Provate a fare il parsing del file degli autori ed estrarre il numero di autori per paper, correlandolo con il successo del paper in questione!</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%</span><span class="k">pwd</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt output_prompt">Out[1]:</div>



<div class="output_text output_subarea output_execute_result">
<pre>&#39;/home/enrico/lavoro/DataProgrammingCourse&#39;</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%</span><span class="k">mkdir</span> esercitazione
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[41]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%</span><span class="k">cd</span> esercitazione
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">!</span>wget http://snap.stanford.edu/data/cit-HepTh-abstracts.tar.gz
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[48]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># questo comando creerà diverse cartelle con dentro i singoli file degli abstract</span>
<span class="o">!</span>tar -xzf cit-HepTh-abstracts.tar.gz
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span> 
</pre></div>

</div>
</div>
</div>

</div>
    </div>
  </div>
</body>

 


</html>







|


<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<

<
<
<
<
<
<
<
<
<
<
<
<
|







 







<
<
<
<
<
<
<
<
<
<
<
<
|



<
<
<
<
<
<
<
<
<
<
<
<
<













11843
11844
11845
11846
11847
11848
11849
11850
11851
11852































11853












11854
11855
11856
11857
11858
11859
11860
11861
.....
12037
12038
12039
12040
12041
12042
12043












12044
12045
12046
12047













12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
<p>Provate a fare il parsing del file degli autori ed estrarre il numero di autori per paper, correlandolo con il successo del paper in questione!</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[41]:</div>
<div class="inner_cell">
    <div class="input_area">































<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%</span><span class="k">mkdir</span> esercitazione












<span class="o">%</span><span class="k">cd</span> esercitazione
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">!</span>wget http://snap.stanford.edu/data/cit-HepTh-abstracts.tar.gz












<span class="c1"># questo comando creerà diverse cartelle con dentro i singoli file degli abstract</span>
<span class="o">!</span>tar -xzf cit-HepTh-abstracts.tar.gz
</pre></div>














</div>
</div>
</div>

</div>
    </div>
  </div>
</body>

 


</html>

Changes to Esercitazione finale.slides.html.

11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
.....
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
.....
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
.....
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
.....
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
.....
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
.....
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
.....
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
<ul>
<li>la coppia di paper citante-citato</li>
<li>la data di pubblicazione di ciascun paper</li>
</ul>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Passi dell'esercitazione:</p>
<ol>
................................................................................
<li>fate una (o più) delle analisi possibili</li>
<li>graficate i risultati e salvate la figura</li>
<li>rendete la pipeline automatica con Snakemake</li>
</ol>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>domande possibili da rispondere:</p>
<ul>
................................................................................
<li>qual è il numero di citazioni nel tempo di ciascun paper?</li>
</ul>
</li>
</ul>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Per i più audaci: esiste la rete di citazioni dei paper teorici</p>
<h2 id="http://snap.stanford.edu/data/cit-HepTh.html"><a href="http://snap.stanford.edu/data/cit-HepTh.html">http://snap.stanford.edu/data/cit-HepTh.html</a><a class="anchor-link" href="#http://snap.stanford.edu/data/cit-HepTh.html">&#182;</a></h2><p>che contiene anche i metadati sugli articoli, inclusi i nomi degli autori.</p>
<p>Provate a fare il parsing del file degli autori ed estrarre il numero di autori per paper, correlandolo con il successo del paper in questione!</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%</span><span class="k">pwd</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt output_prompt">Out[1]:</div>



<div class="output_text output_subarea output_execute_result">
<pre>&#39;/home/enrico/lavoro/DataProgrammingCourse&#39;</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%</span><span class="k">mkdir</span> esercitazione
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[41]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%</span><span class="k">cd</span> esercitazione
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[42]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">!</span>wget http://snap.stanford.edu/data/cit-HepPh.txt.gz
<span class="o">!</span>gunzip -k cit-HepPh.txt.gz
................................................................................
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[43]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">!</span>wget http://snap.stanford.edu/data/cit-HepPh-dates.txt.gz
<span class="o">!</span>gunzip -k cit-HepPh-dates.txt.gz
................................................................................
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[44]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">!</span>wget http://snap.stanford.edu/data/cit-HepTh-dates.txt.gz
<span class="o">!</span>gunzip -k cit-HepTh-dates.txt.gz
................................................................................
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[45]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">!</span>wget http://snap.stanford.edu/data/cit-HepTh.txt.gz
<span class="o">!</span>gunzip -k cit-HepTh.txt.gz
................................................................................
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">!</span>wget http://snap.stanford.edu/data/cit-HepTh-abstracts.tar.gz
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[48]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># questo comando creerà diverse cartelle con dentro i singoli file degli abstract</span>
<span class="o">!</span>tar -xzf cit-HepTh-abstracts.tar.gz
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span> 
</pre></div>

</div>
</div>
</div>

</div></section></section>
</div>
</div>







|







 







|







 







|











|


|


<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<

<
<
<
<
<
<
<
<
<
<
<
<
|







 







|







 







|







 







|







 







|







 







|






<
<
<
<
<
<
<
<
<
<
<
<
|



<
<
<
<
<
<
<
<
<
<
<
<
<







11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
.....
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
.....
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933































11934












11935
11936
11937
11938
11939
11940
11941
11942
.....
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
.....
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
.....
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
.....
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
.....
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124












12125
12126
12127
12128













12129
12130
12131
12132
12133
12134
12135
<ul>
<li>la coppia di paper citante-citato</li>
<li>la data di pubblicazione di ciascun paper</li>
</ul>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Passi dell'esercitazione:</p>
<ol>
................................................................................
<li>fate una (o più) delle analisi possibili</li>
<li>graficate i risultati e salvate la figura</li>
<li>rendete la pipeline automatica con Snakemake</li>
</ol>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>domande possibili da rispondere:</p>
<ul>
................................................................................
<li>qual è il numero di citazioni nel tempo di ciascun paper?</li>
</ul>
</li>
</ul>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Per i più audaci: esiste la rete di citazioni dei paper teorici</p>
<h2 id="http://snap.stanford.edu/data/cit-HepTh.html"><a href="http://snap.stanford.edu/data/cit-HepTh.html">http://snap.stanford.edu/data/cit-HepTh.html</a><a class="anchor-link" href="#http://snap.stanford.edu/data/cit-HepTh.html">&#182;</a></h2><p>che contiene anche i metadati sugli articoli, inclusi i nomi degli autori.</p>
<p>Provate a fare il parsing del file degli autori ed estrarre il numero di autori per paper, correlandolo con il successo del paper in questione!</p>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[41]:</div>
<div class="inner_cell">
    <div class="input_area">































<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%</span><span class="k">mkdir</span> esercitazione












<span class="o">%</span><span class="k">cd</span> esercitazione
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
</pre>
</div>
</div>

</div>
</div>

</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[42]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">!</span>wget http://snap.stanford.edu/data/cit-HepPh.txt.gz
<span class="o">!</span>gunzip -k cit-HepPh.txt.gz
................................................................................
</pre>
</div>
</div>

</div>
</div>

</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[43]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">!</span>wget http://snap.stanford.edu/data/cit-HepPh-dates.txt.gz
<span class="o">!</span>gunzip -k cit-HepPh-dates.txt.gz
................................................................................
</pre>
</div>
</div>

</div>
</div>

</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[44]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">!</span>wget http://snap.stanford.edu/data/cit-HepTh-dates.txt.gz
<span class="o">!</span>gunzip -k cit-HepTh-dates.txt.gz
................................................................................
</pre>
</div>
</div>

</div>
</div>

</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[45]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">!</span>wget http://snap.stanford.edu/data/cit-HepTh.txt.gz
<span class="o">!</span>gunzip -k cit-HepTh.txt.gz
................................................................................
</pre>
</div>
</div>

</div>
</div>

</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">!</span>wget http://snap.stanford.edu/data/cit-HepTh-abstracts.tar.gz












<span class="c1"># questo comando creerà diverse cartelle con dentro i singoli file degli abstract</span>
<span class="o">!</span>tar -xzf cit-HepTh-abstracts.tar.gz
</pre></div>














</div>
</div>
</div>

</div></section></section>
</div>
</div>

Changes to Lezione 4 - Vettorizzazione.html.

11769
11770
11771
11772
11773
11774
11775













11776
11777
11778
11779
11780
11781
11782
.....
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
.....
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
.....
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
.....
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
.....
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
.....
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
.....
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
.....
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
.....
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
.....
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
.....
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
.....
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
.....
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
.....
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
.....
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
.....
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
.....
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
.....
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
.....
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
.....
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
.....
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
.....
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
.....
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
.....
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
.....
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
.....
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
.....
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
.....
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
.....
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194































13195
13196
13197
13198
13199
13200
13201
13202
.....
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
    });
    </script>
    <!-- End of mathjax configuration --></head>
<body>
  <div tabindex="-1" id="notebook" class="border-box-sizing">
    <div class="container" id="notebook-container">














<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h1 id="Precisione,-Accuratezza-e-Velocit&#224;">Precisione, Accuratezza e Velocit&#224;<a class="anchor-link" href="#Precisione,-Accuratezza-e-Velocit&#224;">&#182;</a></h1><h1 id="per-il-calcolo-scientifico">per il calcolo scientifico<a class="anchor-link" href="#per-il-calcolo-scientifico">&#182;</a></h1>
</div>
................................................................................
<p>Ad esempio?</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="si">%f</span><span class="s2"> = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">))</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="si">%.17f</span><span class="s2"> + </span><span class="si">%.17f</span><span class="s2"> = </span><span class="si">%.17f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="mf">0.1</span><span class="p">,</span><span class="mf">0.2</span><span class="p">,</span><span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.2</span><span class="p">))</span>

<span class="nb">print</span> <span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
</ul>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="mf">0.1</span>
<span class="n">b</span> <span class="o">=</span> <span class="mf">0.2</span>
<span class="n">c</span> <span class="o">=</span> <span class="mf">0.3</span>
<span class="nb">print</span> <span class="p">((</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> <span class="o">+</span> <span class="n">c</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="p">(</span><span class="n">b</span> <span class="o">+</span> <span class="n">c</span><span class="p">))</span>
<span class="k">assert</span><span class="p">((</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> <span class="o">+</span> <span class="n">c</span> <span class="o">==</span> <span class="n">a</span> <span class="o">+</span> <span class="p">(</span><span class="n">b</span> <span class="o">+</span> <span class="n">c</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>0.6000000000000001 0.6
</pre>
</div>
</div>

<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_text output_error">
<pre>
<span class="ansi-red-fg">---------------------------------------------------------------------------</span>
<span class="ansi-red-fg">AssertionError</span>                            Traceback (most recent call last)
<span class="ansi-green-fg">&lt;ipython-input-2-65fc2234e42f&gt;</span> in <span class="ansi-cyan-fg">&lt;module&gt;</span><span class="ansi-blue-fg">()</span>
<span class="ansi-green-intense-fg ansi-bold">      3</span> c <span class="ansi-blue-fg">=</span> <span class="ansi-cyan-fg">0.3</span>
<span class="ansi-green-intense-fg ansi-bold">      4</span> print <span class="ansi-blue-fg">(</span><span class="ansi-blue-fg">(</span>a <span class="ansi-blue-fg">+</span> b<span class="ansi-blue-fg">)</span> <span class="ansi-blue-fg">+</span> c<span class="ansi-blue-fg">,</span> a <span class="ansi-blue-fg">+</span> <span class="ansi-blue-fg">(</span>b <span class="ansi-blue-fg">+</span> c<span class="ansi-blue-fg">)</span><span class="ansi-blue-fg">)</span>
<span class="ansi-green-fg">----&gt; 5</span><span class="ansi-red-fg"> </span><span class="ansi-green-fg">assert</span><span class="ansi-blue-fg">(</span><span class="ansi-blue-fg">(</span>a <span class="ansi-blue-fg">+</span> b<span class="ansi-blue-fg">)</span> <span class="ansi-blue-fg">+</span> c <span class="ansi-blue-fg">==</span> a <span class="ansi-blue-fg">+</span> <span class="ansi-blue-fg">(</span>b <span class="ansi-blue-fg">+</span> c<span class="ansi-blue-fg">)</span><span class="ansi-blue-fg">)</span>

<span class="ansi-red-fg">AssertionError</span>: </pre>
</div>
</div>

</div>
</div>
................................................................................
<p>Chiunque (sfido il contrario!) utilizzerebbe la formula esattamente com'è scritta!</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="nb">print</span> <span class="p">(</span><span class="mf">1.</span><span class="o">/</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<h2 id="MA...">MA...<a class="anchor-link" href="#MA...">&#182;</a></h2><p>Qualcuno (non so davvero chi...decisamente me incluso) potrebbe pensare di calcolarla sviluppando il rapporto come:</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">isqrt</span><span class="p">(</span><span class="n">number</span><span class="p">):</span>
    <span class="k">assert</span> <span class="n">number</span> <span class="o">&gt;</span> <span class="mi">0</span>
    <span class="n">threehalfs</span> <span class="o">=</span> <span class="mf">1.5</span>
    <span class="n">x2</span> <span class="o">=</span> <span class="n">number</span> <span class="o">*</span> <span class="mf">0.5</span>
    <span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">(</span><span class="n">number</span><span class="p">)</span>

    <span class="n">i</span> <span class="o">=</span> <span class="n">y</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
    <span class="n">i</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">(</span><span class="mh">0x5f3759df</span><span class="p">)</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">(</span><span class="n">i</span> <span class="o">&gt;&gt;</span> <span class="mi">1</span><span class="p">)</span>
    <span class="n">y</span> <span class="o">=</span> <span class="n">i</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>

    <span class="n">y</span> <span class="o">=</span> <span class="n">y</span> <span class="o">*</span> <span class="p">(</span><span class="n">threehalfs</span> <span class="o">-</span> <span class="p">(</span><span class="n">x2</span> <span class="o">*</span> <span class="n">y</span> <span class="o">*</span> <span class="n">y</span><span class="p">))</span>
    <span class="k">return</span> <span class="nb">float</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>

<span class="nb">print</span> <span class="p">(</span><span class="n">isqrt</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>0.49915357479239103
</pre>
</div>
</div>

</div>
</div>

................................................................................
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Un-esempio-pi&#249;-comune">Un esempio pi&#249; comune<a class="anchor-link" href="#Un-esempio-pi&#249;-comune">&#182;</a></h2><p>Supponiamo di voler calcolare il valore medio di un vettore $x$. Saremo tutti d'accordo che questo sia calcolabile come:</p>
<p>$E[x] = \sum_{i=0}^N \frac{x_i}{N}$</p>
<p>che è certamente equivalente a</p>
<p>$E[x] = \frac{1}{N}\sum_{i=0}^N x_i$</p>
<p>Bene...proviamo allora</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="n">N</span> <span class="o">=</span> <span class="mi">10000</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="n">N</span><span class="p">)</span>
<span class="n">mean_1</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="n">mean_2</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="k">for</span> <span class="n">x_i</span> <span class="ow">in</span> <span class="n">x</span><span class="p">:</span>
    <span class="n">mean_1</span> <span class="o">+=</span> <span class="n">x_i</span> <span class="o">/</span> <span class="n">N</span>
    <span class="n">mean_2</span> <span class="o">+=</span> <span class="n">x_i</span>
<span class="n">mean_2</span> <span class="o">/=</span> <span class="n">N</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;Media 1 = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_1</span><span class="p">))</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;Media 2 = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_2</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>Media 1 = 0.50372520461176450989881914210855
Media 2 = 0.50372520461176406580960929204593
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Cost-of-Operations">Cost of Operations<a class="anchor-link" href="#Cost-of-Operations">&#182;</a></h2><p><img src="./immagini/time.png" alt="&quot;## Cost of Operations&quot;"></p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Cost-of-Functions">Cost of Functions<a class="anchor-link" href="#Cost-of-Functions">&#182;</a></h2><p><img src="./immagini/time2.png" alt="&quot;## Cost of Operations&quot;"></p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
................................................................................
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Ad-esempio">Ad esempio<a class="anchor-link" href="#Ad-esempio">&#182;</a></h2><p><img src="./immagini/vec.png" alt="&quot;## Cost of Operations&quot;"></p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
................................................................................
<p><strong>NOTA</strong>: la vettorizzazione di un loop è uno dei metodi più rapidi da implementare e con il miglior guadagno in tempi di calcolo...soprattutto nei linguaggi ad alto livello!</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span> <span class="p">(</span><span class="s1">&#39;true&#39;</span> <span class="k">if</span> <span class="kc">True</span> <span class="k">else</span> <span class="s1">&#39;false&#39;</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s1">&#39;false&#39;</span> <span class="k">if</span> <span class="kc">False</span> <span class="k">else</span> <span class="s1">&#39;false&#39;</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s1">&#39;odd&#39;</span> <span class="k">if</span> <span class="mi">5</span><span class="o">%</span><span class="k">2</span> else &#39;even&#39;)
<span class="nb">print</span> <span class="p">(</span><span class="s1">&#39;odd&#39;</span> <span class="k">if</span> <span class="mi">4</span><span class="o">%</span><span class="k">2</span> else &#39;even&#39;)
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<h2 id="I-NumPy-array-e-ndarray">I NumPy array e ndarray<a class="anchor-link" href="#I-NumPy-array-e-ndarray">&#182;</a></h2><p>Proprio come gli std::vector<type> del C++, NumPy mette a disposizione i suoi contenitori per i vettori, chiamati np.ndarray (N-dimensional array object), contenitori <strong>veloci</strong>, <strong>flessibili</strong> per <strong>grandi data sets</strong>.</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">])</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">],[</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">8</span><span class="p">]])</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;b = &quot;</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
</ul>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">]</span>
<span class="n">a_array</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">b_array</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a_array</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">b_array</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a_array</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">a_array</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>(4,)
(4,)
int64
4
</pre>
</div>
</div>

</div>
</div>
................................................................................
<h2 id="Creare-array">Creare array<a class="anchor-link" href="#Creare-array">&#182;</a></h2><p>Ci sono 3 funzioni importanti per la creazione di array e l'inizializzazione di array.</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">array_zeros</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;array_zeros = &quot;</span><span class="p">,</span> <span class="n">array_zeros</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;array_zeros ha dimensioni : &quot;</span><span class="p">,</span> <span class="n">array_zeros</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="n">a_matzeros</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="mi">10</span><span class="p">))</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a_matzeros ha dimensioni : &quot;</span><span class="p">,</span> <span class="n">a_matzeros</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="n">array_ones</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;aarray_ones = &quot;</span><span class="p">,</span> <span class="n">array_ones</span><span class="p">)</span>
<span class="n">a_empty</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a_empty = &quot;</span><span class="p">,</span> <span class="n">a_empty</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a_empty contiene oggetti di tipo : &quot;</span><span class="p">,</span> <span class="n">a_empty</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>array_zeros =  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
array_zeros ha dimensioni :  (10,)
a_matzeros ha dimensioni :  (2, 10)
aarray_ones =  [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
a_empty =  [  6.91255410e-310   2.14713104e-316   6.91253074e-310   4.33854919e-274
   6.91253076e-310   6.91253074e-310  -1.74801968e-218   6.91253076e-310
   2.12335699e-316  -2.65620379e-021   6.91253076e-310   2.12340363e-316
  -4.39613327e+118   6.91253076e-310   2.12345027e-316   2.02461812e+083
   6.91253076e-310   2.12349691e-316  -9.23758036e-155   6.91253076e-310]
a_empty contiene oggetti di tipo :  float64
</pre>
</div>
</div>

</div>
</div>
................................................................................
<p>Le principali funzioni sono 2: np.arange e np.linspace</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[10]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[11]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a contiene elementi di tipo : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">uint32</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a contiene elementi di tipo : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">&#39;1.21&#39;</span><span class="p">,</span> <span class="s1">&#39;.2&#39;</span><span class="p">,</span> <span class="s1">&#39;-.4&#39;</span><span class="p">],</span> <span class="n">dtype</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">string_</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a di stringhe = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a cast 2 float = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>a =  [ 1.  2.  3.  4.]
a contiene elementi di tipo :  float64
a =  [1 2 3 4]
a contiene elementi di tipo :  uint32
a di stringhe =  [b&#39;1.21&#39; b&#39;.2&#39; b&#39;-.4&#39;]
a cast 2 float =  [ 1.21  0.2  -0.4 ]
</pre>
</div>
</div>

</div>
</div>
................................................................................
<p>...ma soprattutto hanno tutta una serie di operazioni <strong>già vettorizzate</strong>!!</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[12]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">])</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">])</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> OPERAZIONI ARITMETICHE</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;b = &quot;</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a * 10 : &quot;</span><span class="p">,</span> <span class="n">a</span> <span class="o">*</span> <span class="mi">10</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a + 2 : &quot;</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a^2 : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> OPERAZIONI TRA ARRAY</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a * b : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a + b : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a - b : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">-</span><span class="n">b</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;1. / a : &quot;</span><span class="p">,</span> <span class="mf">1.</span><span class="o">/</span><span class="n">a</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;1 / a : &quot;</span><span class="p">,</span> <span class="mi">1</span><span class="o">/</span><span class="n">a</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................

 OPERAZIONI TRA ARRAY

a * b :  [ 2  6 12 20]
a + b :  [3 5 7 9]
a - b :  [-1 -1 -1 -1]
1. / a :  [ 1.          0.5         0.33333333  0.25      ]
1 / a :  [ 1.          0.5         0.33333333  0.25      ]
</pre>
</div>
</div>

</div>
</div>

................................................................................
<p>Posso anche prendere pezzi dei vettori e combinarli insieme.</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[13]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="mi">8</span><span class="p">])</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[:</span><span class="mi">8</span><span class="p">])</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="mi">8</span><span class="p">:</span><span class="mi">12</span><span class="p">])</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="mi">12</span><span class="p">:</span><span class="o">-</span><span class="mi">4</span><span class="p">])</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="o">-</span><span class="mi">4</span><span class="p">:])</span>
<span class="k">assert</span><span class="p">(</span> <span class="n">np</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="o">-</span><span class="mi">15</span><span class="p">:</span><span class="o">-</span><span class="mi">10</span><span class="p">]</span> <span class="o">==</span> <span class="n">a</span><span class="p">[</span><span class="mi">5</span><span class="p">:</span><span class="mi">10</span><span class="p">]))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="ATTENZIONE!">ATTENZIONE!<a class="anchor-link" href="#ATTENZIONE!">&#182;</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[14]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">arr_slice</span> <span class="o">=</span> <span class="n">a</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">5</span><span class="p">]</span>
<span class="n">arr_slice</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">10000</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<pre><code>                arr[2:5].copy()</code></pre>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[15]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">arr3d</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]],</span> <span class="p">[[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">],</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">]]])</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot; a = &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;primo elemento : &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;prima riga : &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;prima matrice : &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;profondità : &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">])</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<pre><code>                    np.mean(array)</code></pre>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[16]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">time</span>
<span class="n">start_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
<span class="n">N</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="mf">1e7</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="n">N</span><span class="p">)</span>
<span class="n">mean_for_1</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="n">mean_for_2</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="k">for</span> <span class="n">x_i</span> <span class="ow">in</span> <span class="n">x</span><span class="p">:</span>
    <span class="n">mean_for_1</span> <span class="o">+=</span> <span class="n">x_i</span><span class="o">/</span><span class="n">N</span>
    <span class="n">mean_for_2</span> <span class="o">+=</span> <span class="n">x_i</span>
<span class="n">mean_for_2</span> <span class="o">/=</span> <span class="n">N</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;Mean 1 = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_for_1</span><span class="p">))</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;Mean 2 = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_for_2</span><span class="p">))</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;Calcolate in </span><span class="si">%.32f</span><span class="s2"> sec&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span><span class="o">-</span><span class="n">start_time</span><span class="p">))</span>

<span class="n">start_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
<span class="n">mean_vec</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;Mean vettorizzata = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_vec</span><span class="p">))</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;Calcolata in </span><span class="si">%.32f</span><span class="s2"> sec&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span><span class="o">-</span><span class="n">start_time</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>Mean 1 = 0.50013625298338160174438371541328
Mean 2 = 0.50013625298346542358274291473208
Calcolate in 4.40506458282470703125000000000000 sec
Mean vettorizzata = 0.50013625298339459135377182974480
Calcolata in 0.00512361526489257812500000000000 sec
</pre>
</div>
</div>

</div>
</div>

................................................................................
<p>Numpy predispone già un'ampia gamma di generatori!</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[17]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">numpy</span> <span class="k">import</span> <span class="n">random</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">())</span> <span class="c1"># uniform distribution</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">())</span> <span class="c1"># normal distribution</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">exponential</span><span class="p">())</span> <span class="c1"># exponential distribution</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span> <span class="c1"># random matrix</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>0.8360009805434964
0.9891605650843475
4.412141950043221
[[ 0.84408589  0.07277303  0.57363996]
 [ 0.61573595  0.31707604  0.06528632]]
</pre>
</div>
</div>

</div>
</div>

................................................................................
<h2 id="Una-carrellata-di-altre-funzioni-utili...">Una carrellata di altre funzioni utili...<a class="anchor-link" href="#Una-carrellata-di-altre-funzioni-utili...">&#182;</a></h2><p><strong>NOTA</strong> In Matlab la nomenclatura è <strong>quasi sempre</strong> la stessa, ovviamente senza l'np davanti!</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[19]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mf">3.444</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mf">4.29</span><span class="p">,</span> <span class="mf">6.98</span><span class="p">])</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mf">3.44</span><span class="p">,</span> <span class="o">-</span><span class="mf">2.</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">])</span>

<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> ALCUNE altre operazioni </span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>

<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># absolute value </span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">fabs</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># absolute value </span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># square root</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># the largest integer value less than or equal to x</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># smallest integer value greater than or equal to x</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
</div>
</div>

<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stderr output_text">
<pre>/usr/local/lib/python3.6/site-packages/ipykernel/__main__.py:8: RuntimeWarning: invalid value encountered in sqrt
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[20]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;b = &quot;</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> ALCUNE operazioni logiche </span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>

<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">logical_and</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span> <span class="c1"># AND</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">logical_or</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span> <span class="c1"># OR</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">greater</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span> <span class="c1"># &gt;</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">equal</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span> <span class="c1"># ==</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[21]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;b = &quot;</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>

<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> ALCUNE operazioni sul posizionamento </span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># SORTING ELEMENTS</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">argsort</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># SORTING INDICES</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">a</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span> <span class="c1"># INDICES WHERE CONDITION</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">a</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span> <span class="c1"># (CONDITION, IF(CONDITION), ELSE)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">median</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># MEDIAN</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<h2 id="Basta-teoria!-E'-ora-di-sporcarsi-un-po'-le-mani-scrivendo-del-codice!">Basta teoria! E' ora di sporcarsi un po' le mani scrivendo del codice!<a class="anchor-link" href="#Basta-teoria!-E'-ora-di-sporcarsi-un-po'-le-mani-scrivendo-del-codice!">&#182;</a></h2><p>Da quello che abbiamo imparato dovreste essere in grado di vettorizzare facilmente il codice seguente per il calcolo del $\pi$. Controllate anche il tempo di esecuzione delle due versioni dell'algoritmo!</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[16]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">time</span>
































<span class="n">N</span> <span class="o">=</span> <span class="mi">100000</span> <span class="c1"># number of MC events</span>
<span class="n">N_run</span> <span class="o">=</span> <span class="mi">100</span> <span class="c1"># number of runs</span>
<span class="n">Nhits</span> <span class="o">=</span> <span class="mf">0.0</span> <span class="c1"># number of points accepted</span>
<span class="n">pi</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">N_run</span><span class="p">)</span> <span class="c1"># values of pi</span>

<span class="n">start_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="c1"># start clock </span>
<span class="k">for</span> <span class="n">I</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">N_run</span><span class="p">):</span>
    <span class="n">Nhits</span> <span class="o">=</span> <span class="mf">0.0</span>
................................................................................
        <span class="n">res</span> <span class="o">=</span> <span class="n">x</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="o">*</span><span class="n">y</span>
        <span class="k">if</span> <span class="n">res</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">Nhits</span> <span class="o">+=</span> <span class="mf">1.0</span>
    <span class="n">pi</span><span class="p">[</span><span class="n">I</span><span class="p">]</span> <span class="o">+=</span> <span class="mf">4.</span> <span class="o">*</span> <span class="n">Nhits</span><span class="o">/</span><span class="n">N</span>

<span class="n">run_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>

<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;pi with &quot;</span><span class="p">,</span> <span class="n">N</span><span class="p">,</span> <span class="s2">&quot; steps for &quot;</span><span class="p">,</span> <span class="n">N_run</span><span class="p">,</span> <span class="s2">&quot; runs is &quot;</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">pi</span><span class="p">),</span> <span class="s2">&quot; in &quot;</span><span class="p">,</span> <span class="n">run_time</span><span class="o">-</span><span class="n">start_time</span><span class="p">,</span> <span class="s2">&quot; sec&quot;</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;Precision computation : &quot;</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">pi</span><span class="p">)</span><span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">







>
>
>
>
>
>
>
>
>
>
>
>
>







 







|


|
|

|







 







|


|


|
|







 







|











|
|
|
|







 







|


|
|







 







|


|



|

|
|





|







 







|







 







|

|







|


|

|






|
|














|
|













|









|







 







|







 







|


|
|
|
|







 







|


|
|
|
|
|







 







|


|


|
|
|
|














|
|
|







 







|


|
|
|
|
|
|
|
|
|
|







 







|
|

|
|
|
|
|







 







|


|
|
|
|
|
|







 







|


|
|
|

|
|

|
|







 







|







 







|


|

|
|
|
|
|
|
|
|
|
|
|
|







 







|







 







|


|
|
|
|
|
|
|
|







 







|


|
|
|
|
|







 







|


|
|
|
|
|
|







 







|


|
|








|
|
|



|
|







 







|
|
|
|
|







 







|


|
|
|
|
|







 







|
|
|
|
|







 







|


|


|

|
|
|
|
|







 







|










|


|
|
|

|
|
|
|







 







|


|
|

|
|
|
|
|
|







 







|


|


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|







 







|
|







11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
.....
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
.....
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
.....
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
.....
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
.....
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
.....
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
.....
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
.....
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
.....
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
.....
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
.....
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
.....
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
.....
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
.....
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
.....
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
.....
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
.....
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
.....
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
.....
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
.....
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
.....
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
.....
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
.....
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
.....
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
.....
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
.....
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
.....
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
.....
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
.....
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
.....
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
    });
    </script>
    <!-- End of mathjax configuration --></head>
<body>
  <div tabindex="-1" id="notebook" class="border-box-sizing">
    <div class="container" id="notebook-container">

<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[12]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h1 id="Precisione,-Accuratezza-e-Velocit&#224;">Precisione, Accuratezza e Velocit&#224;<a class="anchor-link" href="#Precisione,-Accuratezza-e-Velocit&#224;">&#182;</a></h1><h1 id="per-il-calcolo-scientifico">per il calcolo scientifico<a class="anchor-link" href="#per-il-calcolo-scientifico">&#182;</a></h1>
</div>
................................................................................
<p>Ad esempio?</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[13]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="si">%f</span><span class="s2"> = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">))</span> <span class="c1">#scrittura del numero 0.1 con 32 cifre decimali</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="si">%.17f</span><span class="s2"> + </span><span class="si">%.17f</span><span class="s2"> = </span><span class="si">%.17f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="mf">0.1</span><span class="p">,</span><span class="mf">0.2</span><span class="p">,</span><span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.2</span><span class="p">))</span>

<span class="k">print</span> <span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
</ul>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[14]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="mf">0.1</span>
<span class="n">b</span> <span class="o">=</span> <span class="mf">0.2</span>
<span class="n">c</span> <span class="o">=</span> <span class="mf">0.3</span>
<span class="k">print</span> <span class="p">((</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> <span class="o">+</span> <span class="n">c</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="p">(</span><span class="n">b</span> <span class="o">+</span> <span class="n">c</span><span class="p">))</span>
<span class="k">assert</span><span class="p">((</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> <span class="o">+</span> <span class="n">c</span> <span class="o">==</span> <span class="n">a</span> <span class="o">+</span> <span class="p">(</span><span class="n">b</span> <span class="o">+</span> <span class="n">c</span><span class="p">))</span> <span class="c1">#verifica della proprietà associativa(+) considerando tutti i bit</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>0.6 0.6
</pre>
</div>
</div>

<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_text output_error">
<pre>
<span class="ansi-red-fg">---------------------------------------------------------------------------</span>
<span class="ansi-red-fg">AssertionError</span>                            Traceback (most recent call last)
<span class="ansi-green-fg">&lt;ipython-input-14-65fc2234e42f&gt;</span> in <span class="ansi-cyan-fg">&lt;module&gt;</span><span class="ansi-blue-fg">()</span>
<span class="ansi-green-intense-fg ansi-bold">      3</span> c <span class="ansi-yellow-intense-fg ansi-bold">=</span> <span class="ansi-cyan-intense-fg ansi-bold">0.3</span>
<span class="ansi-green-intense-fg ansi-bold">      4</span> <span class="ansi-green-intense-fg ansi-bold">print</span> <span class="ansi-yellow-intense-fg ansi-bold">(</span><span class="ansi-yellow-intense-fg ansi-bold">(</span>a <span class="ansi-yellow-intense-fg ansi-bold">+</span> b<span class="ansi-yellow-intense-fg ansi-bold">)</span> <span class="ansi-yellow-intense-fg ansi-bold">+</span> c<span class="ansi-yellow-intense-fg ansi-bold">,</span> a <span class="ansi-yellow-intense-fg ansi-bold">+</span> <span class="ansi-yellow-intense-fg ansi-bold">(</span>b <span class="ansi-yellow-intense-fg ansi-bold">+</span> c<span class="ansi-yellow-intense-fg ansi-bold">)</span><span class="ansi-yellow-intense-fg ansi-bold">)</span>
<span class="ansi-green-fg">----&gt; 5</span><span class="ansi-red-fg"> </span><span class="ansi-green-intense-fg ansi-bold">assert</span><span class="ansi-yellow-intense-fg ansi-bold">(</span><span class="ansi-yellow-intense-fg ansi-bold">(</span>a <span class="ansi-yellow-intense-fg ansi-bold">+</span> b<span class="ansi-yellow-intense-fg ansi-bold">)</span> <span class="ansi-yellow-intense-fg ansi-bold">+</span> c <span class="ansi-yellow-intense-fg ansi-bold">==</span> a <span class="ansi-yellow-intense-fg ansi-bold">+</span> <span class="ansi-yellow-intense-fg ansi-bold">(</span>b <span class="ansi-yellow-intense-fg ansi-bold">+</span> c<span class="ansi-yellow-intense-fg ansi-bold">)</span><span class="ansi-yellow-intense-fg ansi-bold">)</span>

<span class="ansi-red-fg">AssertionError</span>: </pre>
</div>
</div>

</div>
</div>
................................................................................
<p>Chiunque (sfido il contrario!) utilizzerebbe la formula esattamente com'è scritta!</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[15]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="k">print</span> <span class="p">(</span><span class="mf">1.</span><span class="o">/</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<h2 id="MA...">MA...<a class="anchor-link" href="#MA...">&#182;</a></h2><p>Qualcuno (non so davvero chi...decisamente me incluso) potrebbe pensare di calcolarla sviluppando il rapporto come:</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[16]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">def</span> <span class="nf">isqrt</span><span class="p">(</span><span class="n">number</span><span class="p">):</span>
    <span class="k">assert</span> <span class="n">number</span> <span class="o">&gt;</span> <span class="mi">0</span>
    <span class="n">threehalfs</span> <span class="o">=</span> <span class="mf">1.5</span>
    <span class="n">x2</span> <span class="o">=</span> <span class="n">number</span> <span class="o">*</span> <span class="mf">0.5</span>
    <span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">(</span><span class="n">number</span><span class="p">)</span> <span class="c1">#conversione a float32 del numero</span>

    <span class="n">i</span> <span class="o">=</span> <span class="n">y</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span> <span class="c1">#&quot;vedi&quot; y come una variabile int32</span>
    <span class="n">i</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">(</span><span class="mh">0x5f3759df</span><span class="p">)</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">(</span><span class="n">i</span> <span class="o">&gt;&gt;</span> <span class="mi">1</span><span class="p">)</span> <span class="c1">#differenza tra numeri in bit-format</span>
    <span class="n">y</span> <span class="o">=</span> <span class="n">i</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>

    <span class="n">y</span> <span class="o">=</span> <span class="n">y</span> <span class="o">*</span> <span class="p">(</span><span class="n">threehalfs</span> <span class="o">-</span> <span class="p">(</span><span class="n">x2</span> <span class="o">*</span> <span class="n">y</span> <span class="o">*</span> <span class="n">y</span><span class="p">))</span>
    <span class="k">return</span> <span class="nb">float</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>

<span class="k">print</span> <span class="p">(</span><span class="n">isqrt</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>0.499153574792
</pre>
</div>
</div>

</div>
</div>

................................................................................
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Un-esempio-pi&#249;-comune">Un esempio pi&#249; comune<a class="anchor-link" href="#Un-esempio-pi&#249;-comune">&#182;</a></h2><p>Supponiamo di voler calcolare il valore medio di un vettore $x$. Saremo tutti d'accordo che questo sia calcolabile come:</p>
<p>$E[x] = \sum_{i=1}^N \frac{x_i}{N}$</p>
<p>che è certamente equivalente a</p>
<p>$E[x] = \frac{1}{N}\sum_{i=1}^N x_i$</p>
<p>Bene...proviamo allora</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[17]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="n">N</span> <span class="o">=</span> <span class="mi">10000</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="n">N</span><span class="p">)</span> <span class="c1">#vettore contenente N numeri random estratti uniformemente in [0,1]</span>
<span class="n">mean_1</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="n">mean_2</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="k">for</span> <span class="n">x_i</span> <span class="ow">in</span> <span class="n">x</span><span class="p">:</span>
    <span class="n">mean_1</span> <span class="o">+=</span> <span class="n">x_i</span> <span class="o">/</span> <span class="n">N</span>
    <span class="n">mean_2</span> <span class="o">+=</span> <span class="n">x_i</span>
<span class="n">mean_2</span> <span class="o">/=</span> <span class="n">N</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;Media 1 = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_1</span><span class="p">))</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;Media 2 = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_2</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>Media 1 = 0.49977160834157097202279373959755
Media 2 = 0.49977160834156963975516418940970
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Cost-of-Operations">Cost of Operations<a class="anchor-link" href="#Cost-of-Operations">&#182;</a></h2><p><img src="./immagini/time.png" alt="alt text" title="## Cost of Operations"></p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Cost-of-Functions">Cost of Functions<a class="anchor-link" href="#Cost-of-Functions">&#182;</a></h2><p><img src="./immagini/time2.png" alt="alt text" title="## Cost of Operations"></p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
................................................................................
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Ad-esempio">Ad esempio<a class="anchor-link" href="#Ad-esempio">&#182;</a></h2><p><img src="./immagini/vec.png" alt="alt text" title="## Cost of Operations"></p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
................................................................................
<p><strong>NOTA</strong>: la vettorizzazione di un loop è uno dei metodi più rapidi da implementare e con il miglior guadagno in tempi di calcolo...soprattutto nei linguaggi ad alto livello!</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[18]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">print</span> <span class="p">(</span><span class="s1">&#39;true&#39;</span> <span class="k">if</span> <span class="bp">True</span> <span class="k">else</span> <span class="s1">&#39;false&#39;</span><span class="p">)</span> <span class="c1"># operatore ternario del C++ in Python: la sintassi è &quot;output1 se condizione, altrimenti output2</span>
<span class="k">print</span> <span class="p">(</span><span class="s1">&#39;false&#39;</span> <span class="k">if</span> <span class="bp">False</span> <span class="k">else</span> <span class="s1">&#39;false&#39;</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s1">&#39;odd&#39;</span> <span class="k">if</span> <span class="mi">5</span><span class="o">%</span><span class="k">2</span> else &#39;even&#39;)
<span class="k">print</span> <span class="p">(</span><span class="s1">&#39;odd&#39;</span> <span class="k">if</span> <span class="mi">4</span><span class="o">%</span><span class="k">2</span> else &#39;even&#39;)
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<h2 id="I-NumPy-array-e-ndarray">I NumPy array e ndarray<a class="anchor-link" href="#I-NumPy-array-e-ndarray">&#182;</a></h2><p>Proprio come gli std::vector<type> del C++, NumPy mette a disposizione i suoi contenitori per i vettori, chiamati np.ndarray (N-dimensional array object), contenitori <strong>veloci</strong>, <strong>flessibili</strong> per <strong>grandi data sets</strong>.</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[19]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">])</span> <span class="c1"># vettore 1x4</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">],[</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">8</span><span class="p">]])</span> <span class="c1"># matrice 2x4</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;b = &quot;</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
</ul>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[20]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">]</span>
<span class="n">a_array</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">b_array</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a_array</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="n">b_array</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a_array</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">a_array</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>(4L,)
(4L,)
int32
4
</pre>
</div>
</div>

</div>
</div>
................................................................................
<h2 id="Creare-array">Creare array<a class="anchor-link" href="#Creare-array">&#182;</a></h2><p>Ci sono 3 funzioni importanti per la creazione di array e l'inizializzazione di array.</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[21]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">array_zeros</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="c1"># vettore 1x10 di zeri</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;array_zeros = &quot;</span><span class="p">,</span> <span class="n">array_zeros</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;array_zeros ha dimensioni : &quot;</span><span class="p">,</span> <span class="n">array_zeros</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="n">a_matzeros</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="mi">10</span><span class="p">))</span> <span class="c1"># matrice 2x10 di zeri</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a_matzeros ha dimensioni : &quot;</span><span class="p">,</span> <span class="n">a_matzeros</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="n">array_ones</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="c1"># vettore 1x10 di uno</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;aarray_ones = &quot;</span><span class="p">,</span> <span class="n">array_ones</span><span class="p">)</span>
<span class="n">a_empty</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span> <span class="c1"># vettore 1x20 di valori &quot;vuoti&quot;=locazioni di memoria libere</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a_empty = &quot;</span><span class="p">,</span> <span class="n">a_empty</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a_empty contiene oggetti di tipo : &quot;</span><span class="p">,</span> <span class="n">a_empty</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>array_zeros =  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
array_zeros ha dimensioni :  (10L,)
a_matzeros ha dimensioni :  (2L, 10L)
aarray_ones =  [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
a_empty =  [  2.85808557e-316   6.22522714e-322   0.00000000e+000   0.00000000e+000
   1.90979621e-313   5.30276956e+180   7.70748458e-043   2.99877042e-066
   3.65961324e-086   4.25757156e-096   2.64936654e+180   3.80884741e+180
   8.03704417e-095   9.97328607e-143   1.47278628e+179   8.37170573e-144
   6.53456787e-042   3.21430743e-057   4.46920458e-090   1.72171244e+184]
a_empty contiene oggetti di tipo :  float64
</pre>
</div>
</div>

</div>
</div>
................................................................................
<p>Le principali funzioni sono 2: np.arange e np.linspace</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[22]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="c1"># vettore di 10 elementi con numeri crescenti da 0(default) e 10</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="c1"># (valore iniziale, valore finale, step)</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span> <span class="c1"># (valore iniziale, valore finale, numero di punti)</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[23]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a contiene elementi di tipo : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">uint32</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a contiene elementi di tipo : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">&#39;1.21&#39;</span><span class="p">,</span> <span class="s1">&#39;.2&#39;</span><span class="p">,</span> <span class="s1">&#39;-.4&#39;</span><span class="p">],</span> <span class="n">dtype</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">string_</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a di stringhe = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a cast 2 float = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>a =  [ 1.  2.  3.  4.]
a contiene elementi di tipo :  float64
a =  [1 2 3 4]
a contiene elementi di tipo :  uint32
a di stringhe =  [&#39;1.21&#39; &#39;.2&#39; &#39;-.4&#39;]
a cast 2 float =  [ 1.21  0.2  -0.4 ]
</pre>
</div>
</div>

</div>
</div>
................................................................................
<p>...ma soprattutto hanno tutta una serie di operazioni <strong>già vettorizzate</strong>!!</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[24]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">])</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">])</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> OPERAZIONI ARITMETICHE</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;b = &quot;</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a * 10 : &quot;</span><span class="p">,</span> <span class="n">a</span> <span class="o">*</span> <span class="mi">10</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a + 2 : &quot;</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a^2 : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> OPERAZIONI TRA ARRAY</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a * b : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a + b : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a - b : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">-</span><span class="n">b</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;1. / a : &quot;</span><span class="p">,</span> <span class="mf">1.</span><span class="o">/</span><span class="n">a</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;1 / a : &quot;</span><span class="p">,</span> <span class="mi">1</span><span class="o">/</span><span class="n">a</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................

 OPERAZIONI TRA ARRAY

a * b :  [ 2  6 12 20]
a + b :  [3 5 7 9]
a - b :  [-1 -1 -1 -1]
1. / a :  [ 1.          0.5         0.33333333  0.25      ]
1 / a :  [1 0 0 0]
</pre>
</div>
</div>

</div>
</div>

................................................................................
<p>Posso anche prendere pezzi dei vettori e combinarli insieme.</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[162]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="mi">8</span><span class="p">])</span> <span class="c1"># ottavo elemento del vettore</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[:</span><span class="mi">8</span><span class="p">])</span> <span class="c1"># primi 8 elementi</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="mi">8</span><span class="p">:</span><span class="mi">12</span><span class="p">])</span> <span class="c1"># elementi compresi tra l&#39;ottavo e il dodicesimo</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="mi">12</span><span class="p">:</span><span class="o">-</span><span class="mi">4</span><span class="p">])</span> <span class="c1"># elementi compresi tra il 12esimo e il quart&#39;ultimo</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="o">-</span><span class="mi">4</span><span class="p">:])</span> <span class="c1"># elementi dal quart&#39;ultimo e la fine</span>
<span class="k">assert</span><span class="p">(</span> <span class="n">np</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="o">-</span><span class="mi">15</span><span class="p">:</span><span class="o">-</span><span class="mi">10</span><span class="p">]</span> <span class="o">==</span> <span class="n">a</span><span class="p">[</span><span class="mi">5</span><span class="p">:</span><span class="mi">10</span><span class="p">]))</span> 
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="ATTENZIONE!">ATTENZIONE!<a class="anchor-link" href="#ATTENZIONE!">&#182;</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[165]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">arr_slice</span> <span class="o">=</span> <span class="n">a</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">5</span><span class="p">]</span> <span class="c1"># pezzo del vettore a</span>
<span class="n">arr_slice</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">10000</span> <span class="c1"># modifica del secondo elemento del pezzo di a</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<pre><code>                arr[2:5].copy()</code></pre>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[176]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">arr3d</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]],</span> <span class="p">[[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">],</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">]]])</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot; a = &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;primo elemento : &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;prima riga : &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;prima matrice : &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;profondità : &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">])</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<pre><code>                    np.mean(array)</code></pre>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[15]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">time</span>
<span class="n">start_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="c1"># inizia a contare il tempo</span>
<span class="n">N</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="mf">1e7</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="n">N</span><span class="p">)</span>
<span class="n">mean_for_1</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="n">mean_for_2</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="k">for</span> <span class="n">x_i</span> <span class="ow">in</span> <span class="n">x</span><span class="p">:</span>
    <span class="n">mean_for_1</span> <span class="o">+=</span> <span class="n">x_i</span><span class="o">/</span><span class="n">N</span>
    <span class="n">mean_for_2</span> <span class="o">+=</span> <span class="n">x_i</span>
<span class="n">mean_for_2</span> <span class="o">/=</span> <span class="n">N</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;Mean 1 = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_for_1</span><span class="p">))</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;Mean 2 = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_for_2</span><span class="p">))</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;Calcolate in </span><span class="si">%.32f</span><span class="s2"> sec&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span><span class="o">-</span><span class="n">start_time</span><span class="p">))</span>

<span class="n">start_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
<span class="n">mean_vec</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;Mean vettorizzata = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_vec</span><span class="p">))</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;Calcolata in </span><span class="si">%.32f</span><span class="s2"> sec&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span><span class="o">-</span><span class="n">start_time</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>Mean 1 = 0.50003546594170067418616554277833
Mean 2 = 0.50003546594162207039602208169526
Calcolate in 33.26500010490417480468750000000000 sec
Mean vettorizzata = 0.50003546594165892980043963689241
Calcolata in 0.04699993133544921875000000000000 sec
</pre>
</div>
</div>

</div>
</div>

................................................................................
<p>Numpy predispone già un'ampia gamma di generatori!</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[193]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="n">random</span>
<span class="k">print</span> <span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">())</span> <span class="c1"># uniform distribution</span>
<span class="k">print</span> <span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">())</span> <span class="c1"># normal distribution</span>
<span class="k">print</span> <span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">exponential</span><span class="p">())</span> <span class="c1"># exponential distribution</span>
<span class="k">print</span> <span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span> <span class="c1"># random matrix</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>0.0803586638819
-1.11673488584
0.466184685074
[[ 0.73152502  0.59559674  0.73434003]
 [ 0.18037221  0.05359538  0.28565275]]
</pre>
</div>
</div>

</div>
</div>

................................................................................
<h2 id="Una-carrellata-di-altre-funzioni-utili...">Una carrellata di altre funzioni utili...<a class="anchor-link" href="#Una-carrellata-di-altre-funzioni-utili...">&#182;</a></h2><p><strong>NOTA</strong> In Matlab la nomenclatura è <strong>quasi sempre</strong> la stessa, ovviamente senza l'np davanti!</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[30]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mf">3.444</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mf">4.29</span><span class="p">,</span> <span class="mf">6.98</span><span class="p">])</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mf">3.44</span><span class="p">,</span> <span class="o">-</span><span class="mf">2.</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">])</span>

<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> ALCUNE altre operazioni </span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>

<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># absolute value </span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">fabs</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># absolute value </span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># square root</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># the largest integer value less than or equal to x</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># smallest integer value greater than or equal to x</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
</div>
</div>

<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stderr output_text">
<pre>C:\Users\NICO\Anaconda2\lib\site-packages\ipykernel\__main__.py:8: RuntimeWarning: invalid value encountered in sqrt
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[27]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;b = &quot;</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> ALCUNE operazioni logiche </span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>

<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">logical_and</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span> <span class="c1"># AND</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">logical_or</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span> <span class="c1"># OR</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">greater</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span> <span class="c1"># &gt;</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">equal</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span> <span class="c1"># ==</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[29]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;b = &quot;</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>

<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> ALCUNE operazioni sul posizionamento </span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># SORTING ELEMENTS</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">argsort</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># SORTING INDICES</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">a</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span> <span class="c1"># INDICES WHERE CONDITION</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">a</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span> <span class="c1"># (CONDITION, IF(CONDITION), ELSE)</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">median</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># MEDIAN</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<h2 id="Basta-teoria!-E'-ora-di-sporcarsi-un-po'-le-mani-scrivendo-del-codice!">Basta teoria! E' ora di sporcarsi un po' le mani scrivendo del codice!<a class="anchor-link" href="#Basta-teoria!-E'-ora-di-sporcarsi-un-po'-le-mani-scrivendo-del-codice!">&#182;</a></h2><p>Da quello che abbiamo imparato dovreste essere in grado di vettorizzare facilmente il codice seguente per il calcolo del $\pi$. Controllate anche il tempo di esecuzione delle due versioni dell'algoritmo!</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">time</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Metropolis Algorithm</span>

<span class="sd">Algoritmo per il calcolo dell&#39;integrale di una funzione attraverso un campionamento Monte Carlo. </span>
<span class="sd">In questo esempio viene utilizzato per il calcolo del valore del pi-greco. Si consideri al proposito un cerchio di</span>
<span class="sd">raggio unitario (r = 1) centrato nell&#39;origine, inscritto in un quadrato. In questo modo il quadrato avrà lato pari a 2</span>
<span class="sd">e la sua area sarà uguale a 4. Andando ad estrarre in modo uniforme nell&#39;intervallo [-1,1] una coppia di numeri (x, y),</span>
<span class="sd">qualora le coordinate risultino appartenenti al cerchio verrà conteggiato un Hit. La condizione da imporre sarà quindi:</span>

<span class="sd">                                            x^2 + y^2 &lt; 1</span>
<span class="sd">                                </span>
<span class="sd">Secondo la teoria dei metodi Monte Carlo, il numero di Hit rispetto al totale sarà equivalente (per un numero tendente</span>
<span class="sd">all&#39;infinito di estrazioni) al rapporto tra le aree. In questo caso quindi</span>

<span class="sd">                                            Hit/Tot = pi/4</span>
<span class="sd">                                            </span>
<span class="sd">Volendo estrarre un risultato più robusto si consiglia di campionare il valore di pi un numero N_runs di volte, in modo</span>
<span class="sd">che il corretto valore di pi sia dato dalla media della distribuzione di più campionamenti Monte Carlo.</span>
<span class="sd">&quot;&quot;&quot;</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[16]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">N</span> <span class="o">=</span> <span class="mi">100000</span> <span class="c1"># number of MC events</span>
<span class="n">N_run</span> <span class="o">=</span> <span class="mi">100</span> <span class="c1"># number of runs</span>
<span class="n">Nhits</span> <span class="o">=</span> <span class="mf">0.0</span> <span class="c1"># number of points accepted</span>
<span class="n">pi</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">N_run</span><span class="p">)</span> <span class="c1"># values of pi</span>

<span class="n">start_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="c1"># start clock </span>
<span class="k">for</span> <span class="n">I</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">N_run</span><span class="p">):</span>
    <span class="n">Nhits</span> <span class="o">=</span> <span class="mf">0.0</span>
................................................................................
        <span class="n">res</span> <span class="o">=</span> <span class="n">x</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="o">*</span><span class="n">y</span>
        <span class="k">if</span> <span class="n">res</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">Nhits</span> <span class="o">+=</span> <span class="mf">1.0</span>
    <span class="n">pi</span><span class="p">[</span><span class="n">I</span><span class="p">]</span> <span class="o">+=</span> <span class="mf">4.</span> <span class="o">*</span> <span class="n">Nhits</span><span class="o">/</span><span class="n">N</span>

<span class="n">run_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>

<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;pi with &quot;</span><span class="p">,</span> <span class="n">N</span><span class="p">,</span> <span class="s2">&quot; steps for &quot;</span><span class="p">,</span> <span class="n">N_run</span><span class="p">,</span> <span class="s2">&quot; runs is &quot;</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">pi</span><span class="p">),</span> <span class="s2">&quot; in &quot;</span><span class="p">,</span> <span class="n">run_time</span><span class="o">-</span><span class="n">start_time</span><span class="p">,</span> <span class="s2">&quot; sec&quot;</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;Precision computation : &quot;</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">pi</span><span class="p">)</span><span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">

Changes to Lezione 4 - Vettorizzazione.ipynb.

1
2
















3
4
5
6
7
8
9
...
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
...
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
...
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
...
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
...
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
...
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
...
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
...
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
...
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
...
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
...
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
...
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
...
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
...
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
...
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
...
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
...
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
...
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
....
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
....
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
....
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
....
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
....
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
....
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
....
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
....
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
....
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
....
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
....
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
....
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
....
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
....
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
....
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
....
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
....
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
....
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
....
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
....
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
....
1711
1712
1713
1714
1715
1716
1717





































1718
1719
1720
1721
1722
1723
1724
....
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
....
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
{
 "cells": [
















  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "dfcf1b08-a43b-482c-9bff-ef4e3352bef7"
................................................................................
    "Si va bene ma cosa vuol dire?\n",
    "\n",
    "Ad esempio?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "6ab53abb-7acd-4f5b-9d45-0e25d37d7f33"
    },
................................................................................
      "0.100000 = 0.10000000000000000555111512312578\n",
      "0.10000000000000001 + 0.20000000000000001 = 0.30000000000000004\n",
      "0.1\n"
     ]
    }
   ],
   "source": [
    "print (\"%f = %.32f\"%(0.1, 0.1))\n",
    "print (\"%.17f + %.17f = %.17f\"%(0.1,0.2,0.1 + 0.2))\n",
    "\n",
    "print (0.1)"
   ]
  },
  {
   "cell_type": "markdown",
................................................................................
    "* division : $a/b \\neq a \\times (1/b)$\n",
    "* algorithms : $(a+b) \\times (a-b) \\neq a^2-b^2$\n",
    "* ecc."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "e92059d4-9755-4743-92da-155a3caaec9d"
    },
................................................................................
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.6000000000000001 0.6\n"
     ]
    },
    {
     "ename": "AssertionError",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAssertionError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-2-65fc2234e42f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0.3\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32massert\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mc\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mb\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAssertionError\u001b[0m: "
     ]
    }
   ],
   "source": [
    "a = 0.1\n",
    "b = 0.2\n",
    "c = 0.3\n",
    "print ((a + b) + c, a + (b + c))\n",
    "assert((a + b) + c == a + (b + c))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true,
................................................................................
   },
   "source": [
    "Chiunque (sfido il contrario!) utilizzerebbe la formula esattamente com'è scritta!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "d6e6e52e-4bac-492e-89f3-98221a86b91c"
    },
................................................................................
    "## MA...\n",
    "\n",
    "Qualcuno (non so davvero chi...decisamente me incluso) potrebbe pensare di calcolarla sviluppando il rapporto come:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "2f57d0cf-dbb0-433c-833a-7db5f70cebac"
    },
................................................................................
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.49915357479239103\n"
     ]
    }
   ],
   "source": [
    "def isqrt(number):\n",
    "    assert number > 0\n",
    "    threehalfs = 1.5\n",
    "    x2 = number * 0.5\n",
    "    y = np.float32(number)\n",
    "\n",
    "    i = y.view(np.int32)\n",
    "    i = np.int32(0x5f3759df) - np.int32(i >> 1)\n",
    "    y = i.view(np.float32)\n",
    "\n",
    "    y = y * (threehalfs - (x2 * y * y))\n",
    "    return float(y)\n",
    "\n",
    "print (isqrt(4))"
   ]
................................................................................
    }
   },
   "source": [
    "## Un esempio più comune\n",
    "\n",
    "Supponiamo di voler calcolare il valore medio di un vettore $x$. Saremo tutti d'accordo che questo sia calcolabile come:\n",
    "\n",
    "$E[x] = \\sum_{i=0}^N \\frac{x_i}{N}$\n",
    "\n",
    "che è certamente equivalente a \n",
    "\n",
    "$E[x] = \\frac{1}{N}\\sum_{i=0}^N x_i$\n",
    "\n",
    "Bene...proviamo allora"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "635b05e7-dbb3-4249-adf2-e650fc01baa7"
    },
................................................................................
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Media 1 = 0.50372520461176450989881914210855\n",
      "Media 2 = 0.50372520461176406580960929204593\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "N = 10000\n",
    "x = np.random.rand(N)\n",
    "mean_1 = 0.0\n",
    "mean_2 = 0.0\n",
    "for x_i in x:\n",
    "    mean_1 += x_i / N\n",
    "    mean_2 += x_i\n",
    "mean_2 /= N\n",
    "print (\"Media 1 = %.32f\"%(mean_1))\n",
................................................................................
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Cost of Operations\n",
    "\n",
    "![\"## Cost of Operations\"](./immagini/time.png )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true,
................................................................................
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Cost of Functions\n",
    "\n",
    "![\"## Cost of Operations\"](./immagini/time2.png )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true,
................................................................................
    },
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Ad esempio\n",
    "![\"## Cost of Operations\"](./immagini/vec.png )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true,
................................................................................
    "* Ogni iterazione del loop deve essere eseguita!!(*)\n",
    "\n",
    "**NOTA**: la vettorizzazione di un loop è uno dei metodi più rapidi da implementare e con il miglior guadagno in tempi di calcolo...soprattutto nei linguaggi ad alto livello!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "ac1da7a6-1d51-4350-b103-869c18e77204"
    },
................................................................................
      "false\n",
      "odd\n",
      "even\n"
     ]
    }
   ],
   "source": [
    "print ('true' if True else 'false')\n",
    "print ('false' if False else 'false')\n",
    "print ('odd' if 5%2 else 'even')\n",
    "print ('odd' if 4%2 else 'even')"
   ]
  },
  {
   "cell_type": "markdown",
................................................................................
    "## I NumPy array e ndarray\n",
    "\n",
    "Proprio come gli std::vector<type> del C++, NumPy mette a disposizione i suoi contenitori per i vettori, chiamati np.ndarray (N-dimensional array object), contenitori **veloci**, **flessibili** per **grandi data sets**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "6cbf22b1-059c-46ed-a42c-d4f758c0e953"
    },
................................................................................
      "b =  [[1 2 3 4]\n",
      " [5 6 7 8]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([1,2,3,4])\n",
    "print (\"a = \", a)\n",
    "b = np.array([[1,2,3,4],[5,6,7,8]])\n",
    "print (\"b = \", b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
................................................................................
    "* array.dtype : ritorna il tipo di dato contenuto\n",
    "* len(array) : ritorna la dimensione delle **righe** di una matrice o più in generale la **prima** dimensione dell'ndarray.\n",
    "* np.array(lista) o np.array(tupla) : converte in un numpy array l'oggetto."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "68f36037-aebc-447f-9ed6-6756ae3175c0"
    },
................................................................................
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4,)\n",
      "(4,)\n",
      "int64\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "a_array = np.array(a)\n",
................................................................................
    "## Creare array\n",
    "\n",
    "Ci sono 3 funzioni importanti per la creazione di array e l'inizializzazione di array."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "575117fd-1d32-4d36-b62b-610f5ed99ef1"
    },
................................................................................
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "array_zeros =  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]\n",
      "array_zeros ha dimensioni :  (10,)\n",
      "a_matzeros ha dimensioni :  (2, 10)\n",
      "aarray_ones =  [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]\n",
      "a_empty =  [  6.91255410e-310   2.14713104e-316   6.91253074e-310   4.33854919e-274\n",
      "   6.91253076e-310   6.91253074e-310  -1.74801968e-218   6.91253076e-310\n",
      "   2.12335699e-316  -2.65620379e-021   6.91253076e-310   2.12340363e-316\n",
      "  -4.39613327e+118   6.91253076e-310   2.12345027e-316   2.02461812e+083\n",
      "   6.91253076e-310   2.12349691e-316  -9.23758036e-155   6.91253076e-310]\n",
      "a_empty contiene oggetti di tipo :  float64\n"
     ]
    }
   ],
   "source": [
    "array_zeros = np.zeros(10)\n",
    "print (\"array_zeros = \", array_zeros)\n",
    "print (\"array_zeros ha dimensioni : \", array_zeros.shape)\n",
    "a_matzeros = np.zeros((2,10))\n",
    "print (\"a_matzeros ha dimensioni : \", a_matzeros.shape)\n",
    "array_ones = np.ones(10)\n",
    "print (\"aarray_ones = \", array_ones)\n",
    "a_empty = np.empty(20)\n",
    "print (\"a_empty = \", a_empty)\n",
    "print (\"a_empty contiene oggetti di tipo : \", a_empty.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
................................................................................
    "Posso anche creare array che contengano numeri appartenenti ad un range numerico crescente o decrescente.\n",
    "\n",
    "Le principali funzioni sono 2: np.arange e np.linspace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "7238ab2b-ae7b-4dea-bb79-df6b613f330d"
    },
................................................................................
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[1 3 5 7 9]\n",
      "[ 0.          0.66666667  1.33333333  2.        ]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(10)\n",
    "print (a)\n",
    "a = np.arange(1, 10, 2)\n",
    "print (a)\n",
    "a = np.linspace(0, 2, 4)\n",
    "print (a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "8996df45-220a-4a74-8254-4a6e9082e7b4"
    },
................................................................................
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a =  [ 1.  2.  3.  4.]\n",
      "a contiene elementi di tipo :  float64\n",
      "a =  [1 2 3 4]\n",
      "a contiene elementi di tipo :  uint32\n",
      "a di stringhe =  [b'1.21' b'.2' b'-.4']\n",
      "a cast 2 float =  [ 1.21  0.2  -0.4 ]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1,2,3,4], dtype=np.float64)\n",
    "print (\"a = \", a)\n",
................................................................................
    "I NumPy array non sono solo belli da vedersi ma ci tolgono anche tanti pensieri sul calcolo numerico e algebrico...\n",
    "\n",
    "...ma soprattutto hanno tutta una serie di operazioni **già vettorizzate**!!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "177728b4-06e1-4d59-9ad4-058a334e9775"
    },
................................................................................
      "\n",
      " OPERAZIONI TRA ARRAY\n",
      "\n",
      "a * b :  [ 2  6 12 20]\n",
      "a + b :  [3 5 7 9]\n",
      "a - b :  [-1 -1 -1 -1]\n",
      "1. / a :  [ 1.          0.5         0.33333333  0.25      ]\n",
      "1 / a :  [ 1.          0.5         0.33333333  0.25      ]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1,2,3,4])\n",
    "b = np.array([2,3,4,5])\n",
    "print (\"\\n OPERAZIONI ARITMETICHE\\n\")\n",
................................................................................
   },
   "source": [
    "Posso anche prendere pezzi dei vettori e combinarli insieme."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "183db7a5-f898-4085-a130-2ac7465a1bf0"
    },
................................................................................
      "[16 17 18 19]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(20)\n",
    "print (a)\n",
    "print (a[8])\n",
    "print (a[:8])\n",
    "print (a[8:12])\n",
    "print (a[12:-4])\n",
    "print (a[-4:])\n",
    "assert( np.all(a[-15:-10] == a[5:10]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true,
................................................................................
   },
   "source": [
    "## ATTENZIONE!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "e004ef40-362e-4b6d-9f3c-2826e6b41675"
    },
................................................................................
      "[    0     1     2 10000     4     5     6     7     8     9]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(10)\n",
    "print (a)\n",
    "arr_slice = a[2:5]\n",
    "arr_slice[1] = 10000\n",
    "print (a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
................................................................................
    "Quando volete **copiare** una porzione di un array senza che le modifiche si ripercuotano sull'array originale dovete usare il comando appropriato...che con grande fantasia sarà\n",
    "\n",
    "                    arr[2:5].copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "d50d09f6-0c49-4de9-9a81-25834375c865"
    },
................................................................................
    "NumPy mette a disposizione una sua versione vettorizzata del calcolo della media di un vettore\n",
    "\n",
    "                        np.mean(array)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "73422ad3-02b5-4013-947f-326eae035105"
    },
................................................................................
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mean 1 = 0.50013625298338160174438371541328\n",
      "Mean 2 = 0.50013625298346542358274291473208\n",
      "Calcolate in 4.40506458282470703125000000000000 sec\n",
      "Mean vettorizzata = 0.50013625298339459135377182974480\n",
      "Calcolata in 0.00512361526489257812500000000000 sec\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "start_time = time.time()\n",
    "N = int(1e7)\n",
    "x = np.random.rand(N)\n",
    "mean_for_1 = 0.0\n",
    "mean_for_2 = 0.0\n",
    "for x_i in x:\n",
    "    mean_for_1 += x_i/N\n",
    "    mean_for_2 += x_i\n",
................................................................................
    "Una classe di funzioni molto importante per la programmazione è la generazione di numeri random.\n",
    "\n",
    "Numpy predispone già un'ampia gamma di generatori!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "c47b0755-85fb-4121-b2cd-d616f8d97fd5"
    },
................................................................................
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.8360009805434964\n",
      "0.9891605650843475\n",
      "4.412141950043221\n",
      "[[ 0.84408589  0.07277303  0.57363996]\n",
      " [ 0.61573595  0.31707604  0.06528632]]\n"
     ]
    }
   ],
   "source": [
    "from numpy import random\n",
    "print (random.rand()) # uniform distribution\n",
    "print (random.randn()) # normal distribution\n",
................................................................................
    "## Una carrellata di altre funzioni utili...\n",
    "\n",
    "**NOTA** In Matlab la nomenclatura è **quasi sempre** la stessa, ovviamente senza l'np davanti!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "a0cd31f2-223a-4514-bbce-6259d0229faf"
    },
................................................................................
      "[ 1. -2.  4. -2.  5.  7.]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.6/site-packages/ipykernel/__main__.py:8: RuntimeWarning: invalid value encountered in sqrt\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1, -2, 3.444, -2, 4.29, 6.98])\n",
    "b = np.array([2, -2, 3.44, -2., 5, 7])\n",
    "\n",
................................................................................
    "print (np.sqrt(a)) # square root\n",
    "print (np.floor(a)) # the largest integer value less than or equal to x\n",
    "print (np.ceil(a)) # smallest integer value greater than or equal to x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    }
................................................................................
    "print (np.logical_or(a, b)) # OR\n",
    "print (np.greater(a, b)) # >\n",
    "print (np.equal(a, b)) # =="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    }
................................................................................
   },
   "source": [
    "## Basta teoria! E' ora di sporcarsi un po' le mani scrivendo del codice!\n",
    "\n",
    "Da quello che abbiamo imparato dovreste essere in grado di vettorizzare facilmente il codice seguente per il calcolo del $\\pi$. Controllate anche il tempo di esecuzione delle due versioni dell'algoritmo!"
   ]
  },





































  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
................................................................................
     "text": [
      "pi with  100000  steps for  100  runs is  3.1418768  in  48.2949998379  sec\n",
      "Precision computation :  0.000284146410207\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import time\n",
    "\n",
    "N = 100000 # number of MC events\n",
    "N_run = 100 # number of runs\n",
    "Nhits = 0.0 # number of points accepted\n",
    "pi = np.zeros(N_run) # values of pi\n",
    "\n",
    "start_time = time.time() # start clock \n",
    "for I in range(N_run):\n",
................................................................................
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  },
  "nbpresent": {
   "slides": {
    "0003d80d-8617-4f6c-a1ba-5ed01cb56f2b": {
     "id": "0003d80d-8617-4f6c-a1ba-5ed01cb56f2b",
     "prev": "93eeb58b-fd7f-4329-823e-9c33af7a0c24",
     "regions": {


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|







 







|







 







|







 







|









|









|







 







|







 







|







 







|








|

|
|







 







|



|






|







 







|
|






|







 







|







 







|







 







|







 







|







 







|







 







|







 







|

|







 







|







 







|
|
|







 







|







 







|
|

|
|
|
|
|





|


|

|

|







 







|







 







|

|

|





|







 







|







 







|







 







|







 







|







 







|
|
|
|
|
|







 







|







 







|
|







 







|







 







|







 







|
|
|
|
|





|







 







|







 







|
|
|
|
|







 







|







 







|







 







|







 







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







<
<
<







 







|

|




|





|
|







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
...
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
...
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
...
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
...
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
...
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
...
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
...
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
...
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
...
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
...
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
...
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
...
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
...
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
...
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
...
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
...
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
...
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
...
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
....
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
....
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
....
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
....
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
....
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
....
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
....
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
....
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
....
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
....
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
....
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
....
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
....
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
....
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
....
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
....
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
....
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
....
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
....
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
....
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
....
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
....
1789
1790
1791
1792
1793
1794
1795



1796
1797
1798
1799
1800
1801
1802
....
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "dfcf1b08-a43b-482c-9bff-ef4e3352bef7"
................................................................................
    "Si va bene ma cosa vuol dire?\n",
    "\n",
    "Ad esempio?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "6ab53abb-7acd-4f5b-9d45-0e25d37d7f33"
    },
................................................................................
      "0.100000 = 0.10000000000000000555111512312578\n",
      "0.10000000000000001 + 0.20000000000000001 = 0.30000000000000004\n",
      "0.1\n"
     ]
    }
   ],
   "source": [
    "print (\"%f = %.32f\"%(0.1, 0.1)) #scrittura del numero 0.1 con 32 cifre decimali\n",
    "print (\"%.17f + %.17f = %.17f\"%(0.1,0.2,0.1 + 0.2))\n",
    "\n",
    "print (0.1)"
   ]
  },
  {
   "cell_type": "markdown",
................................................................................
    "* division : $a/b \\neq a \\times (1/b)$\n",
    "* algorithms : $(a+b) \\times (a-b) \\neq a^2-b^2$\n",
    "* ecc."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "e92059d4-9755-4743-92da-155a3caaec9d"
    },
................................................................................
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.6 0.6\n"
     ]
    },
    {
     "ename": "AssertionError",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAssertionError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-14-65fc2234e42f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mc\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0.3\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[1;32massert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mc\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAssertionError\u001b[0m: "
     ]
    }
   ],
   "source": [
    "a = 0.1\n",
    "b = 0.2\n",
    "c = 0.3\n",
    "print ((a + b) + c, a + (b + c))\n",
    "assert((a + b) + c == a + (b + c)) #verifica della proprietà associativa(+) considerando tutti i bit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true,
................................................................................
   },
   "source": [
    "Chiunque (sfido il contrario!) utilizzerebbe la formula esattamente com'è scritta!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "d6e6e52e-4bac-492e-89f3-98221a86b91c"
    },
................................................................................
    "## MA...\n",
    "\n",
    "Qualcuno (non so davvero chi...decisamente me incluso) potrebbe pensare di calcolarla sviluppando il rapporto come:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "2f57d0cf-dbb0-433c-833a-7db5f70cebac"
    },
................................................................................
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.499153574792\n"
     ]
    }
   ],
   "source": [
    "def isqrt(number):\n",
    "    assert number > 0\n",
    "    threehalfs = 1.5\n",
    "    x2 = number * 0.5\n",
    "    y = np.float32(number) #conversione a float32 del numero\n",
    "\n",
    "    i = y.view(np.int32) #\"vedi\" y come una variabile int32\n",
    "    i = np.int32(0x5f3759df) - np.int32(i >> 1) #differenza tra numeri in bit-format\n",
    "    y = i.view(np.float32)\n",
    "\n",
    "    y = y * (threehalfs - (x2 * y * y))\n",
    "    return float(y)\n",
    "\n",
    "print (isqrt(4))"
   ]
................................................................................
    }
   },
   "source": [
    "## Un esempio più comune\n",
    "\n",
    "Supponiamo di voler calcolare il valore medio di un vettore $x$. Saremo tutti d'accordo che questo sia calcolabile come:\n",
    "\n",
    "$E[x] = \\sum_{i=1}^N \\frac{x_i}{N}$\n",
    "\n",
    "che è certamente equivalente a \n",
    "\n",
    "$E[x] = \\frac{1}{N}\\sum_{i=1}^N x_i$\n",
    "\n",
    "Bene...proviamo allora"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "635b05e7-dbb3-4249-adf2-e650fc01baa7"
    },
................................................................................
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Media 1 = 0.49977160834157097202279373959755\n",
      "Media 2 = 0.49977160834156963975516418940970\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "N = 10000\n",
    "x = np.random.rand(N) #vettore contenente N numeri random estratti uniformemente in [0,1]\n",
    "mean_1 = 0.0\n",
    "mean_2 = 0.0\n",
    "for x_i in x:\n",
    "    mean_1 += x_i / N\n",
    "    mean_2 += x_i\n",
    "mean_2 /= N\n",
    "print (\"Media 1 = %.32f\"%(mean_1))\n",
................................................................................
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Cost of Operations\n",
    "\n",
    "![alt text](./immagini/time.png \"## Cost of Operations\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true,
................................................................................
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Cost of Functions\n",
    "\n",
    "![alt text](./immagini/time2.png \"## Cost of Operations\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true,
................................................................................
    },
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Ad esempio\n",
    "![alt text](./immagini/vec.png \"## Cost of Operations\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true,
................................................................................
    "* Ogni iterazione del loop deve essere eseguita!!(*)\n",
    "\n",
    "**NOTA**: la vettorizzazione di un loop è uno dei metodi più rapidi da implementare e con il miglior guadagno in tempi di calcolo...soprattutto nei linguaggi ad alto livello!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "ac1da7a6-1d51-4350-b103-869c18e77204"
    },
................................................................................
      "false\n",
      "odd\n",
      "even\n"
     ]
    }
   ],
   "source": [
    "print ('true' if True else 'false') # operatore ternario del C++ in Python: la sintassi è \"output1 se condizione, altrimenti output2\n",
    "print ('false' if False else 'false')\n",
    "print ('odd' if 5%2 else 'even')\n",
    "print ('odd' if 4%2 else 'even')"
   ]
  },
  {
   "cell_type": "markdown",
................................................................................
    "## I NumPy array e ndarray\n",
    "\n",
    "Proprio come gli std::vector<type> del C++, NumPy mette a disposizione i suoi contenitori per i vettori, chiamati np.ndarray (N-dimensional array object), contenitori **veloci**, **flessibili** per **grandi data sets**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "6cbf22b1-059c-46ed-a42c-d4f758c0e953"
    },
................................................................................
      "b =  [[1 2 3 4]\n",
      " [5 6 7 8]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([1,2,3,4]) # vettore 1x4\n",
    "print (\"a = \", a)\n",
    "b = np.array([[1,2,3,4],[5,6,7,8]]) # matrice 2x4\n",
    "print (\"b = \", b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
................................................................................
    "* array.dtype : ritorna il tipo di dato contenuto\n",
    "* len(array) : ritorna la dimensione delle **righe** di una matrice o più in generale la **prima** dimensione dell'ndarray.\n",
    "* np.array(lista) o np.array(tupla) : converte in un numpy array l'oggetto."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "68f36037-aebc-447f-9ed6-6756ae3175c0"
    },
................................................................................
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4L,)\n",
      "(4L,)\n",
      "int32\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "a_array = np.array(a)\n",
................................................................................
    "## Creare array\n",
    "\n",
    "Ci sono 3 funzioni importanti per la creazione di array e l'inizializzazione di array."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "575117fd-1d32-4d36-b62b-610f5ed99ef1"
    },
................................................................................
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "array_zeros =  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]\n",
      "array_zeros ha dimensioni :  (10L,)\n",
      "a_matzeros ha dimensioni :  (2L, 10L)\n",
      "aarray_ones =  [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]\n",
      "a_empty =  [  2.85808557e-316   6.22522714e-322   0.00000000e+000   0.00000000e+000\n",
      "   1.90979621e-313   5.30276956e+180   7.70748458e-043   2.99877042e-066\n",
      "   3.65961324e-086   4.25757156e-096   2.64936654e+180   3.80884741e+180\n",
      "   8.03704417e-095   9.97328607e-143   1.47278628e+179   8.37170573e-144\n",
      "   6.53456787e-042   3.21430743e-057   4.46920458e-090   1.72171244e+184]\n",
      "a_empty contiene oggetti di tipo :  float64\n"
     ]
    }
   ],
   "source": [
    "array_zeros = np.zeros(10) # vettore 1x10 di zeri\n",
    "print (\"array_zeros = \", array_zeros)\n",
    "print (\"array_zeros ha dimensioni : \", array_zeros.shape)\n",
    "a_matzeros = np.zeros((2,10)) # matrice 2x10 di zeri\n",
    "print (\"a_matzeros ha dimensioni : \", a_matzeros.shape)\n",
    "array_ones = np.ones(10) # vettore 1x10 di uno\n",
    "print (\"aarray_ones = \", array_ones)\n",
    "a_empty = np.empty(20) # vettore 1x20 di valori \"vuoti\"=locazioni di memoria libere\n",
    "print (\"a_empty = \", a_empty)\n",
    "print (\"a_empty contiene oggetti di tipo : \", a_empty.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
................................................................................
    "Posso anche creare array che contengano numeri appartenenti ad un range numerico crescente o decrescente.\n",
    "\n",
    "Le principali funzioni sono 2: np.arange e np.linspace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "7238ab2b-ae7b-4dea-bb79-df6b613f330d"
    },
................................................................................
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[1 3 5 7 9]\n",
      "[ 0.          0.66666667  1.33333333  2.        ]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(10) # vettore di 10 elementi con numeri crescenti da 0(default) e 10\n",
    "print (a)\n",
    "a = np.arange(1, 10, 2) # (valore iniziale, valore finale, step)\n",
    "print (a)\n",
    "a = np.linspace(0, 2, 4) # (valore iniziale, valore finale, numero di punti)\n",
    "print (a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "8996df45-220a-4a74-8254-4a6e9082e7b4"
    },
................................................................................
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a =  [ 1.  2.  3.  4.]\n",
      "a contiene elementi di tipo :  float64\n",
      "a =  [1 2 3 4]\n",
      "a contiene elementi di tipo :  uint32\n",
      "a di stringhe =  ['1.21' '.2' '-.4']\n",
      "a cast 2 float =  [ 1.21  0.2  -0.4 ]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1,2,3,4], dtype=np.float64)\n",
    "print (\"a = \", a)\n",
................................................................................
    "I NumPy array non sono solo belli da vedersi ma ci tolgono anche tanti pensieri sul calcolo numerico e algebrico...\n",
    "\n",
    "...ma soprattutto hanno tutta una serie di operazioni **già vettorizzate**!!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "177728b4-06e1-4d59-9ad4-058a334e9775"
    },
................................................................................
      "\n",
      " OPERAZIONI TRA ARRAY\n",
      "\n",
      "a * b :  [ 2  6 12 20]\n",
      "a + b :  [3 5 7 9]\n",
      "a - b :  [-1 -1 -1 -1]\n",
      "1. / a :  [ 1.          0.5         0.33333333  0.25      ]\n",
      "1 / a :  [1 0 0 0]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1,2,3,4])\n",
    "b = np.array([2,3,4,5])\n",
    "print (\"\\n OPERAZIONI ARITMETICHE\\n\")\n",
................................................................................
   },
   "source": [
    "Posso anche prendere pezzi dei vettori e combinarli insieme."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "183db7a5-f898-4085-a130-2ac7465a1bf0"
    },
................................................................................
      "[16 17 18 19]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(20)\n",
    "print (a)\n",
    "print (a[8]) # ottavo elemento del vettore\n",
    "print (a[:8]) # primi 8 elementi\n",
    "print (a[8:12]) # elementi compresi tra l'ottavo e il dodicesimo\n",
    "print (a[12:-4]) # elementi compresi tra il 12esimo e il quart'ultimo\n",
    "print (a[-4:]) # elementi dal quart'ultimo e la fine\n",
    "assert( np.all(a[-15:-10] == a[5:10])) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true,
................................................................................
   },
   "source": [
    "## ATTENZIONE!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "e004ef40-362e-4b6d-9f3c-2826e6b41675"
    },
................................................................................
      "[    0     1     2 10000     4     5     6     7     8     9]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(10)\n",
    "print (a)\n",
    "arr_slice = a[2:5] # pezzo del vettore a\n",
    "arr_slice[1] = 10000 # modifica del secondo elemento del pezzo di a\n",
    "print (a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
................................................................................
    "Quando volete **copiare** una porzione di un array senza che le modifiche si ripercuotano sull'array originale dovete usare il comando appropriato...che con grande fantasia sarà\n",
    "\n",
    "                    arr[2:5].copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "d50d09f6-0c49-4de9-9a81-25834375c865"
    },
................................................................................
    "NumPy mette a disposizione una sua versione vettorizzata del calcolo della media di un vettore\n",
    "\n",
    "                        np.mean(array)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "73422ad3-02b5-4013-947f-326eae035105"
    },
................................................................................
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mean 1 = 0.50003546594170067418616554277833\n",
      "Mean 2 = 0.50003546594162207039602208169526\n",
      "Calcolate in 33.26500010490417480468750000000000 sec\n",
      "Mean vettorizzata = 0.50003546594165892980043963689241\n",
      "Calcolata in 0.04699993133544921875000000000000 sec\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "start_time = time.time() # inizia a contare il tempo\n",
    "N = int(1e7)\n",
    "x = np.random.rand(N)\n",
    "mean_for_1 = 0.0\n",
    "mean_for_2 = 0.0\n",
    "for x_i in x:\n",
    "    mean_for_1 += x_i/N\n",
    "    mean_for_2 += x_i\n",
................................................................................
    "Una classe di funzioni molto importante per la programmazione è la generazione di numeri random.\n",
    "\n",
    "Numpy predispone già un'ampia gamma di generatori!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 193,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "c47b0755-85fb-4121-b2cd-d616f8d97fd5"
    },
................................................................................
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0803586638819\n",
      "-1.11673488584\n",
      "0.466184685074\n",
      "[[ 0.73152502  0.59559674  0.73434003]\n",
      " [ 0.18037221  0.05359538  0.28565275]]\n"
     ]
    }
   ],
   "source": [
    "from numpy import random\n",
    "print (random.rand()) # uniform distribution\n",
    "print (random.randn()) # normal distribution\n",
................................................................................
    "## Una carrellata di altre funzioni utili...\n",
    "\n",
    "**NOTA** In Matlab la nomenclatura è **quasi sempre** la stessa, ovviamente senza l'np davanti!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "nbpresent": {
     "id": "a0cd31f2-223a-4514-bbce-6259d0229faf"
    },
................................................................................
      "[ 1. -2.  4. -2.  5.  7.]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\NICO\\Anaconda2\\lib\\site-packages\\ipykernel\\__main__.py:8: RuntimeWarning: invalid value encountered in sqrt\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1, -2, 3.444, -2, 4.29, 6.98])\n",
    "b = np.array([2, -2, 3.44, -2., 5, 7])\n",
    "\n",
................................................................................
    "print (np.sqrt(a)) # square root\n",
    "print (np.floor(a)) # the largest integer value less than or equal to x\n",
    "print (np.ceil(a)) # smallest integer value greater than or equal to x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    }
................................................................................
    "print (np.logical_or(a, b)) # OR\n",
    "print (np.greater(a, b)) # >\n",
    "print (np.equal(a, b)) # =="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    }
................................................................................
   },
   "source": [
    "## Basta teoria! E' ora di sporcarsi un po' le mani scrivendo del codice!\n",
    "\n",
    "Da quello che abbiamo imparato dovreste essere in grado di vettorizzare facilmente il codice seguente per il calcolo del $\\pi$. Controllate anche il tempo di esecuzione delle due versioni dell'algoritmo!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import time\n",
    "\n",
    "\"\"\"\n",
    "Metropolis Algorithm\n",
    "\n",
    "Algoritmo per il calcolo dell'integrale di una funzione attraverso un campionamento Monte Carlo. \n",
    "In questo esempio viene utilizzato per il calcolo del valore del pi-greco. Si consideri al proposito un cerchio di\n",
    "raggio unitario (r = 1) centrato nell'origine, inscritto in un quadrato. In questo modo il quadrato avrà lato pari a 2\n",
    "e la sua area sarà uguale a 4. Andando ad estrarre in modo uniforme nell'intervallo [-1,1] una coppia di numeri (x, y),\n",
    "qualora le coordinate risultino appartenenti al cerchio verrà conteggiato un Hit. La condizione da imporre sarà quindi:\n",
    "\n",
    "                                            x^2 + y^2 < 1\n",
    "                                \n",
    "Secondo la teoria dei metodi Monte Carlo, il numero di Hit rispetto al totale sarà equivalente (per un numero tendente\n",
    "all'infinito di estrazioni) al rapporto tra le aree. In questo caso quindi\n",
    "\n",
    "                                            Hit/Tot = pi/4\n",
    "                                            \n",
    "Volendo estrarre un risultato più robusto si consiglia di campionare il valore di pi un numero N_runs di volte, in modo\n",
    "che il corretto valore di pi sia dato dalla media della distribuzione di più campionamenti Monte Carlo.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
................................................................................
     "text": [
      "pi with  100000  steps for  100  runs is  3.1418768  in  48.2949998379  sec\n",
      "Precision computation :  0.000284146410207\n"
     ]
    }
   ],
   "source": [



    "N = 100000 # number of MC events\n",
    "N_run = 100 # number of runs\n",
    "Nhits = 0.0 # number of points accepted\n",
    "pi = np.zeros(N_run) # values of pi\n",
    "\n",
    "start_time = time.time() # start clock \n",
    "for I in range(N_run):\n",
................................................................................
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  },
  "nbpresent": {
   "slides": {
    "0003d80d-8617-4f6c-a1ba-5ed01cb56f2b": {
     "id": "0003d80d-8617-4f6c-a1ba-5ed01cb56f2b",
     "prev": "93eeb58b-fd7f-4329-823e-9c33af7a0c24",
     "regions": {

Changes to Lezione 4 - Vettorizzazione.slides.html.

12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
.....
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
.....
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
.....
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
.....
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
.....
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
.....
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
.....
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
.....
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
.....
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
.....
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
.....
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
.....
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
.....
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
.....
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
.....
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
.....
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
.....
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
.....
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
.....
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
.....
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
.....
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
.....
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
.....
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
.....
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
.....
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
.....
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
.....
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
.....
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275































13276
13277
13278
13279
13280
13281
13282
13283
.....
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
<p>Ad esempio?</p>

</div>
</div>
</div></div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="si">%f</span><span class="s2"> = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">))</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="si">%.17f</span><span class="s2"> + </span><span class="si">%.17f</span><span class="s2"> = </span><span class="si">%.17f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="mf">0.1</span><span class="p">,</span><span class="mf">0.2</span><span class="p">,</span><span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.2</span><span class="p">))</span>

<span class="nb">print</span> <span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
</ul>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="mf">0.1</span>
<span class="n">b</span> <span class="o">=</span> <span class="mf">0.2</span>
<span class="n">c</span> <span class="o">=</span> <span class="mf">0.3</span>
<span class="nb">print</span> <span class="p">((</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> <span class="o">+</span> <span class="n">c</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="p">(</span><span class="n">b</span> <span class="o">+</span> <span class="n">c</span><span class="p">))</span>
<span class="k">assert</span><span class="p">((</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> <span class="o">+</span> <span class="n">c</span> <span class="o">==</span> <span class="n">a</span> <span class="o">+</span> <span class="p">(</span><span class="n">b</span> <span class="o">+</span> <span class="n">c</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>0.6000000000000001 0.6
</pre>
</div>
</div>

<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_text output_error">
<pre>
<span class="ansi-red-fg">---------------------------------------------------------------------------</span>
<span class="ansi-red-fg">AssertionError</span>                            Traceback (most recent call last)
<span class="ansi-green-fg">&lt;ipython-input-2-65fc2234e42f&gt;</span> in <span class="ansi-cyan-fg">&lt;module&gt;</span><span class="ansi-blue-fg">()</span>
<span class="ansi-green-intense-fg ansi-bold">      3</span> c <span class="ansi-blue-fg">=</span> <span class="ansi-cyan-fg">0.3</span>
<span class="ansi-green-intense-fg ansi-bold">      4</span> print <span class="ansi-blue-fg">(</span><span class="ansi-blue-fg">(</span>a <span class="ansi-blue-fg">+</span> b<span class="ansi-blue-fg">)</span> <span class="ansi-blue-fg">+</span> c<span class="ansi-blue-fg">,</span> a <span class="ansi-blue-fg">+</span> <span class="ansi-blue-fg">(</span>b <span class="ansi-blue-fg">+</span> c<span class="ansi-blue-fg">)</span><span class="ansi-blue-fg">)</span>
<span class="ansi-green-fg">----&gt; 5</span><span class="ansi-red-fg"> </span><span class="ansi-green-fg">assert</span><span class="ansi-blue-fg">(</span><span class="ansi-blue-fg">(</span>a <span class="ansi-blue-fg">+</span> b<span class="ansi-blue-fg">)</span> <span class="ansi-blue-fg">+</span> c <span class="ansi-blue-fg">==</span> a <span class="ansi-blue-fg">+</span> <span class="ansi-blue-fg">(</span>b <span class="ansi-blue-fg">+</span> c<span class="ansi-blue-fg">)</span><span class="ansi-blue-fg">)</span>

<span class="ansi-red-fg">AssertionError</span>: </pre>
</div>
</div>

</div>
</div>
................................................................................
<p>Chiunque (sfido il contrario!) utilizzerebbe la formula esattamente com'è scritta!</p>

</div>
</div>
</div></div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="nb">print</span> <span class="p">(</span><span class="mf">1.</span><span class="o">/</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<h2 id="MA...">MA...<a class="anchor-link" href="#MA...">&#182;</a></h2><p>Qualcuno (non so davvero chi...decisamente me incluso) potrebbe pensare di calcolarla sviluppando il rapporto come:</p>

</div>
</div>
</div></div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">isqrt</span><span class="p">(</span><span class="n">number</span><span class="p">):</span>
    <span class="k">assert</span> <span class="n">number</span> <span class="o">&gt;</span> <span class="mi">0</span>
    <span class="n">threehalfs</span> <span class="o">=</span> <span class="mf">1.5</span>
    <span class="n">x2</span> <span class="o">=</span> <span class="n">number</span> <span class="o">*</span> <span class="mf">0.5</span>
    <span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">(</span><span class="n">number</span><span class="p">)</span>

    <span class="n">i</span> <span class="o">=</span> <span class="n">y</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
    <span class="n">i</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">(</span><span class="mh">0x5f3759df</span><span class="p">)</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">(</span><span class="n">i</span> <span class="o">&gt;&gt;</span> <span class="mi">1</span><span class="p">)</span>
    <span class="n">y</span> <span class="o">=</span> <span class="n">i</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>

    <span class="n">y</span> <span class="o">=</span> <span class="n">y</span> <span class="o">*</span> <span class="p">(</span><span class="n">threehalfs</span> <span class="o">-</span> <span class="p">(</span><span class="n">x2</span> <span class="o">*</span> <span class="n">y</span> <span class="o">*</span> <span class="n">y</span><span class="p">))</span>
    <span class="k">return</span> <span class="nb">float</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>

<span class="nb">print</span> <span class="p">(</span><span class="n">isqrt</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>0.49915357479239103
</pre>
</div>
</div>

</div>
</div>

................................................................................
</div></section></section><section><section>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Un-esempio-pi&#249;-comune">Un esempio pi&#249; comune<a class="anchor-link" href="#Un-esempio-pi&#249;-comune">&#182;</a></h2><p>Supponiamo di voler calcolare il valore medio di un vettore $x$. Saremo tutti d'accordo che questo sia calcolabile come:</p>
<p>$E[x] = \sum_{i=0}^N \frac{x_i}{N}$</p>
<p>che è certamente equivalente a</p>
<p>$E[x] = \frac{1}{N}\sum_{i=0}^N x_i$</p>
<p>Bene...proviamo allora</p>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="n">N</span> <span class="o">=</span> <span class="mi">10000</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="n">N</span><span class="p">)</span>
<span class="n">mean_1</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="n">mean_2</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="k">for</span> <span class="n">x_i</span> <span class="ow">in</span> <span class="n">x</span><span class="p">:</span>
    <span class="n">mean_1</span> <span class="o">+=</span> <span class="n">x_i</span> <span class="o">/</span> <span class="n">N</span>
    <span class="n">mean_2</span> <span class="o">+=</span> <span class="n">x_i</span>
<span class="n">mean_2</span> <span class="o">/=</span> <span class="n">N</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;Media 1 = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_1</span><span class="p">))</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;Media 2 = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_2</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>Media 1 = 0.50372520461176450989881914210855
Media 2 = 0.50372520461176406580960929204593
</pre>
</div>
</div>

</div>
</div>

</div></section></section><section><section>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Cost-of-Operations">Cost of Operations<a class="anchor-link" href="#Cost-of-Operations">&#182;</a></h2><p><img src="./immagini/time.png" alt="&quot;## Cost of Operations&quot;"></p>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Cost-of-Functions">Cost of Functions<a class="anchor-link" href="#Cost-of-Functions">&#182;</a></h2><p><img src="./immagini/time2.png" alt="&quot;## Cost of Operations&quot;"></p>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
................................................................................
</div>
</div></div></section></section><section><section>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Ad-esempio">Ad esempio<a class="anchor-link" href="#Ad-esempio">&#182;</a></h2><p><img src="./immagini/vec.png" alt="&quot;## Cost of Operations&quot;"></p>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
................................................................................
<p><strong>NOTA</strong>: la vettorizzazione di un loop è uno dei metodi più rapidi da implementare e con il miglior guadagno in tempi di calcolo...soprattutto nei linguaggi ad alto livello!</p>

</div>
</div>
</div><div class="fragment">
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span> <span class="p">(</span><span class="s1">&#39;true&#39;</span> <span class="k">if</span> <span class="kc">True</span> <span class="k">else</span> <span class="s1">&#39;false&#39;</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s1">&#39;false&#39;</span> <span class="k">if</span> <span class="kc">False</span> <span class="k">else</span> <span class="s1">&#39;false&#39;</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s1">&#39;odd&#39;</span> <span class="k">if</span> <span class="mi">5</span><span class="o">%</span><span class="k">2</span> else &#39;even&#39;)
<span class="nb">print</span> <span class="p">(</span><span class="s1">&#39;odd&#39;</span> <span class="k">if</span> <span class="mi">4</span><span class="o">%</span><span class="k">2</span> else &#39;even&#39;)
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<h2 id="I-NumPy-array-e-ndarray">I NumPy array e ndarray<a class="anchor-link" href="#I-NumPy-array-e-ndarray">&#182;</a></h2><p>Proprio come gli std::vector<type> del C++, NumPy mette a disposizione i suoi contenitori per i vettori, chiamati np.ndarray (N-dimensional array object), contenitori <strong>veloci</strong>, <strong>flessibili</strong> per <strong>grandi data sets</strong>.</p>

</div>
</div>
</div><div class="fragment">
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">])</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">],[</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">8</span><span class="p">]])</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;b = &quot;</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
</ul>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">]</span>
<span class="n">a_array</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">b_array</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a_array</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">b_array</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a_array</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">a_array</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>(4,)
(4,)
int64
4
</pre>
</div>
</div>

</div>
</div>
................................................................................
<h2 id="Creare-array">Creare array<a class="anchor-link" href="#Creare-array">&#182;</a></h2><p>Ci sono 3 funzioni importanti per la creazione di array e l'inizializzazione di array.</p>

</div>
</div>
</div><div class="fragment">
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">array_zeros</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;array_zeros = &quot;</span><span class="p">,</span> <span class="n">array_zeros</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;array_zeros ha dimensioni : &quot;</span><span class="p">,</span> <span class="n">array_zeros</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="n">a_matzeros</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="mi">10</span><span class="p">))</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a_matzeros ha dimensioni : &quot;</span><span class="p">,</span> <span class="n">a_matzeros</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="n">array_ones</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;aarray_ones = &quot;</span><span class="p">,</span> <span class="n">array_ones</span><span class="p">)</span>
<span class="n">a_empty</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a_empty = &quot;</span><span class="p">,</span> <span class="n">a_empty</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a_empty contiene oggetti di tipo : &quot;</span><span class="p">,</span> <span class="n">a_empty</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>array_zeros =  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
array_zeros ha dimensioni :  (10,)
a_matzeros ha dimensioni :  (2, 10)
aarray_ones =  [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
a_empty =  [  6.91255410e-310   2.14713104e-316   6.91253074e-310   4.33854919e-274
   6.91253076e-310   6.91253074e-310  -1.74801968e-218   6.91253076e-310
   2.12335699e-316  -2.65620379e-021   6.91253076e-310   2.12340363e-316
  -4.39613327e+118   6.91253076e-310   2.12345027e-316   2.02461812e+083
   6.91253076e-310   2.12349691e-316  -9.23758036e-155   6.91253076e-310]
a_empty contiene oggetti di tipo :  float64
</pre>
</div>
</div>

</div>
</div>
................................................................................
<p>Le principali funzioni sono 2: np.arange e np.linspace</p>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[10]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................

</div>
</div>

</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[11]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a contiene elementi di tipo : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">uint32</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a contiene elementi di tipo : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">&#39;1.21&#39;</span><span class="p">,</span> <span class="s1">&#39;.2&#39;</span><span class="p">,</span> <span class="s1">&#39;-.4&#39;</span><span class="p">],</span> <span class="n">dtype</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">string_</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a di stringhe = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a cast 2 float = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>a =  [ 1.  2.  3.  4.]
a contiene elementi di tipo :  float64
a =  [1 2 3 4]
a contiene elementi di tipo :  uint32
a di stringhe =  [b&#39;1.21&#39; b&#39;.2&#39; b&#39;-.4&#39;]
a cast 2 float =  [ 1.21  0.2  -0.4 ]
</pre>
</div>
</div>

</div>
</div>
................................................................................
<p>...ma soprattutto hanno tutta una serie di operazioni <strong>già vettorizzate</strong>!!</p>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[12]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">])</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">])</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> OPERAZIONI ARITMETICHE</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;b = &quot;</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a * 10 : &quot;</span><span class="p">,</span> <span class="n">a</span> <span class="o">*</span> <span class="mi">10</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a + 2 : &quot;</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a^2 : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> OPERAZIONI TRA ARRAY</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a * b : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a + b : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a - b : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">-</span><span class="n">b</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;1. / a : &quot;</span><span class="p">,</span> <span class="mf">1.</span><span class="o">/</span><span class="n">a</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;1 / a : &quot;</span><span class="p">,</span> <span class="mi">1</span><span class="o">/</span><span class="n">a</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................

 OPERAZIONI TRA ARRAY

a * b :  [ 2  6 12 20]
a + b :  [3 5 7 9]
a - b :  [-1 -1 -1 -1]
1. / a :  [ 1.          0.5         0.33333333  0.25      ]
1 / a :  [ 1.          0.5         0.33333333  0.25      ]
</pre>
</div>
</div>

</div>
</div>

................................................................................
<p>Posso anche prendere pezzi dei vettori e combinarli insieme.</p>

</div>
</div>
</div><div class="fragment">
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[13]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="mi">8</span><span class="p">])</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[:</span><span class="mi">8</span><span class="p">])</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="mi">8</span><span class="p">:</span><span class="mi">12</span><span class="p">])</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="mi">12</span><span class="p">:</span><span class="o">-</span><span class="mi">4</span><span class="p">])</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="o">-</span><span class="mi">4</span><span class="p">:])</span>
<span class="k">assert</span><span class="p">(</span> <span class="n">np</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="o">-</span><span class="mi">15</span><span class="p">:</span><span class="o">-</span><span class="mi">10</span><span class="p">]</span> <span class="o">==</span> <span class="n">a</span><span class="p">[</span><span class="mi">5</span><span class="p">:</span><span class="mi">10</span><span class="p">]))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="ATTENZIONE!">ATTENZIONE!<a class="anchor-link" href="#ATTENZIONE!">&#182;</a></h2>
</div>
</div>
</div><div class="fragment">
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[14]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">arr_slice</span> <span class="o">=</span> <span class="n">a</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">5</span><span class="p">]</span>
<span class="n">arr_slice</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">10000</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<pre><code>                arr[2:5].copy()</code></pre>

</div>
</div>
</div></div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[15]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">arr3d</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]],</span> <span class="p">[[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">],</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">]]])</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot; a = &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;primo elemento : &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;prima riga : &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;prima matrice : &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;profondità : &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">])</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<pre><code>                    np.mean(array)</code></pre>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[16]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">time</span>
<span class="n">start_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
<span class="n">N</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="mf">1e7</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="n">N</span><span class="p">)</span>
<span class="n">mean_for_1</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="n">mean_for_2</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="k">for</span> <span class="n">x_i</span> <span class="ow">in</span> <span class="n">x</span><span class="p">:</span>
    <span class="n">mean_for_1</span> <span class="o">+=</span> <span class="n">x_i</span><span class="o">/</span><span class="n">N</span>
    <span class="n">mean_for_2</span> <span class="o">+=</span> <span class="n">x_i</span>
<span class="n">mean_for_2</span> <span class="o">/=</span> <span class="n">N</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;Mean 1 = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_for_1</span><span class="p">))</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;Mean 2 = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_for_2</span><span class="p">))</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;Calcolate in </span><span class="si">%.32f</span><span class="s2"> sec&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span><span class="o">-</span><span class="n">start_time</span><span class="p">))</span>

<span class="n">start_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
<span class="n">mean_vec</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;Mean vettorizzata = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_vec</span><span class="p">))</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;Calcolata in </span><span class="si">%.32f</span><span class="s2"> sec&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span><span class="o">-</span><span class="n">start_time</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>Mean 1 = 0.50013625298338160174438371541328
Mean 2 = 0.50013625298346542358274291473208
Calcolate in 4.40506458282470703125000000000000 sec
Mean vettorizzata = 0.50013625298339459135377182974480
Calcolata in 0.00512361526489257812500000000000 sec
</pre>
</div>
</div>

</div>
</div>

................................................................................
<p>Numpy predispone già un'ampia gamma di generatori!</p>

</div>
</div>
</div><div class="fragment">
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[17]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">numpy</span> <span class="k">import</span> <span class="n">random</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">())</span> <span class="c1"># uniform distribution</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">())</span> <span class="c1"># normal distribution</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">exponential</span><span class="p">())</span> <span class="c1"># exponential distribution</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span> <span class="c1"># random matrix</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>0.8360009805434964
0.9891605650843475
4.412141950043221
[[ 0.84408589  0.07277303  0.57363996]
 [ 0.61573595  0.31707604  0.06528632]]
</pre>
</div>
</div>

</div>
</div>

................................................................................
<h2 id="Una-carrellata-di-altre-funzioni-utili...">Una carrellata di altre funzioni utili...<a class="anchor-link" href="#Una-carrellata-di-altre-funzioni-utili...">&#182;</a></h2><p><strong>NOTA</strong> In Matlab la nomenclatura è <strong>quasi sempre</strong> la stessa, ovviamente senza l'np davanti!</p>

</div>
</div>
</div><div class="fragment">
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[19]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mf">3.444</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mf">4.29</span><span class="p">,</span> <span class="mf">6.98</span><span class="p">])</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mf">3.44</span><span class="p">,</span> <span class="o">-</span><span class="mf">2.</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">])</span>

<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> ALCUNE altre operazioni </span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>

<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># absolute value </span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">fabs</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># absolute value </span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># square root</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># the largest integer value less than or equal to x</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># smallest integer value greater than or equal to x</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
</div>
</div>

<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stderr output_text">
<pre>/usr/local/lib/python3.6/site-packages/ipykernel/__main__.py:8: RuntimeWarning: invalid value encountered in sqrt
</pre>
</div>
</div>

</div>
</div>

</div></div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[20]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;b = &quot;</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> ALCUNE operazioni logiche </span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>

<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">logical_and</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span> <span class="c1"># AND</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">logical_or</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span> <span class="c1"># OR</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">greater</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span> <span class="c1"># &gt;</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">equal</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span> <span class="c1"># ==</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................

</div>
</div>

</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[21]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;b = &quot;</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>

<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> ALCUNE operazioni sul posizionamento </span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># SORTING ELEMENTS</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">argsort</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># SORTING INDICES</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">a</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span> <span class="c1"># INDICES WHERE CONDITION</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">a</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span> <span class="c1"># (CONDITION, IF(CONDITION), ELSE)</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">median</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># MEDIAN</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<h2 id="Basta-teoria!-E'-ora-di-sporcarsi-un-po'-le-mani-scrivendo-del-codice!">Basta teoria! E' ora di sporcarsi un po' le mani scrivendo del codice!<a class="anchor-link" href="#Basta-teoria!-E'-ora-di-sporcarsi-un-po'-le-mani-scrivendo-del-codice!">&#182;</a></h2><p>Da quello che abbiamo imparato dovreste essere in grado di vettorizzare facilmente il codice seguente per il calcolo del $\pi$. Controllate anche il tempo di esecuzione delle due versioni dell'algoritmo!</p>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[16]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">time</span>
































<span class="n">N</span> <span class="o">=</span> <span class="mi">100000</span> <span class="c1"># number of MC events</span>
<span class="n">N_run</span> <span class="o">=</span> <span class="mi">100</span> <span class="c1"># number of runs</span>
<span class="n">Nhits</span> <span class="o">=</span> <span class="mf">0.0</span> <span class="c1"># number of points accepted</span>
<span class="n">pi</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">N_run</span><span class="p">)</span> <span class="c1"># values of pi</span>

<span class="n">start_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="c1"># start clock </span>
<span class="k">for</span> <span class="n">I</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">N_run</span><span class="p">):</span>
    <span class="n">Nhits</span> <span class="o">=</span> <span class="mf">0.0</span>
................................................................................
        <span class="n">res</span> <span class="o">=</span> <span class="n">x</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="o">*</span><span class="n">y</span>
        <span class="k">if</span> <span class="n">res</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">Nhits</span> <span class="o">+=</span> <span class="mf">1.0</span>
    <span class="n">pi</span><span class="p">[</span><span class="n">I</span><span class="p">]</span> <span class="o">+=</span> <span class="mf">4.</span> <span class="o">*</span> <span class="n">Nhits</span><span class="o">/</span><span class="n">N</span>

<span class="n">run_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>

<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;pi with &quot;</span><span class="p">,</span> <span class="n">N</span><span class="p">,</span> <span class="s2">&quot; steps for &quot;</span><span class="p">,</span> <span class="n">N_run</span><span class="p">,</span> <span class="s2">&quot; runs is &quot;</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">pi</span><span class="p">),</span> <span class="s2">&quot; in &quot;</span><span class="p">,</span> <span class="n">run_time</span><span class="o">-</span><span class="n">start_time</span><span class="p">,</span> <span class="s2">&quot; sec&quot;</span><span class="p">)</span>
<span class="nb">print</span> <span class="p">(</span><span class="s2">&quot;Precision computation : &quot;</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">pi</span><span class="p">)</span><span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">







|


|
|

|







 







|


|


|
|







 







|











|
|
|
|







 







|


|
|







 







|


|



|

|
|





|







 







|







 







|

|







|


|

|






|
|














|
|













|









|







 







|







 







|


|
|
|
|







 







|


|
|
|
|
|







 







|


|


|
|
|
|














|
|
|







 







|


|
|
|
|
|
|
|
|
|
|







 







|
|

|
|
|
|
|







 







|


|
|
|
|
|
|







 







|


|
|
|

|
|

|
|







 







|







 







|


|

|
|
|
|
|
|
|
|
|
|
|
|







 







|







 







|


|
|
|
|
|
|
|
|







 







|


|
|
|
|
|







 







|


|
|
|
|
|
|







 







|


|
|








|
|
|



|
|







 







|
|
|
|
|







 







|


|
|
|
|
|







 







|
|
|
|
|







 







|


|


|

|
|
|
|
|







 







|










|


|
|
|

|
|
|
|







 







|


|
|

|
|
|
|
|
|







 







|


|


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|







 







|
|







12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
.....
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
.....
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
.....
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
.....
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
.....
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
.....
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
.....
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
.....
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
.....
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
.....
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
.....
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
.....
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
.....
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
.....
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
.....
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
.....
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
.....
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
.....
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
.....
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
.....
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
.....
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
.....
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
.....
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
.....
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
.....
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
.....
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
.....
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
.....
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
.....
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
<p>Ad esempio?</p>

</div>
</div>
</div></div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[13]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="si">%f</span><span class="s2"> = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">))</span> <span class="c1">#scrittura del numero 0.1 con 32 cifre decimali</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="si">%.17f</span><span class="s2"> + </span><span class="si">%.17f</span><span class="s2"> = </span><span class="si">%.17f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="mf">0.1</span><span class="p">,</span><span class="mf">0.2</span><span class="p">,</span><span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.2</span><span class="p">))</span>

<span class="k">print</span> <span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
</ul>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[14]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="mf">0.1</span>
<span class="n">b</span> <span class="o">=</span> <span class="mf">0.2</span>
<span class="n">c</span> <span class="o">=</span> <span class="mf">0.3</span>
<span class="k">print</span> <span class="p">((</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> <span class="o">+</span> <span class="n">c</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="p">(</span><span class="n">b</span> <span class="o">+</span> <span class="n">c</span><span class="p">))</span>
<span class="k">assert</span><span class="p">((</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span> <span class="o">+</span> <span class="n">c</span> <span class="o">==</span> <span class="n">a</span> <span class="o">+</span> <span class="p">(</span><span class="n">b</span> <span class="o">+</span> <span class="n">c</span><span class="p">))</span> <span class="c1">#verifica della proprietà associativa(+) considerando tutti i bit</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>0.6 0.6
</pre>
</div>
</div>

<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_text output_error">
<pre>
<span class="ansi-red-fg">---------------------------------------------------------------------------</span>
<span class="ansi-red-fg">AssertionError</span>                            Traceback (most recent call last)
<span class="ansi-green-fg">&lt;ipython-input-14-65fc2234e42f&gt;</span> in <span class="ansi-cyan-fg">&lt;module&gt;</span><span class="ansi-blue-fg">()</span>
<span class="ansi-green-intense-fg ansi-bold">      3</span> c <span class="ansi-yellow-intense-fg ansi-bold">=</span> <span class="ansi-cyan-intense-fg ansi-bold">0.3</span>
<span class="ansi-green-intense-fg ansi-bold">      4</span> <span class="ansi-green-intense-fg ansi-bold">print</span> <span class="ansi-yellow-intense-fg ansi-bold">(</span><span class="ansi-yellow-intense-fg ansi-bold">(</span>a <span class="ansi-yellow-intense-fg ansi-bold">+</span> b<span class="ansi-yellow-intense-fg ansi-bold">)</span> <span class="ansi-yellow-intense-fg ansi-bold">+</span> c<span class="ansi-yellow-intense-fg ansi-bold">,</span> a <span class="ansi-yellow-intense-fg ansi-bold">+</span> <span class="ansi-yellow-intense-fg ansi-bold">(</span>b <span class="ansi-yellow-intense-fg ansi-bold">+</span> c<span class="ansi-yellow-intense-fg ansi-bold">)</span><span class="ansi-yellow-intense-fg ansi-bold">)</span>
<span class="ansi-green-fg">----&gt; 5</span><span class="ansi-red-fg"> </span><span class="ansi-green-intense-fg ansi-bold">assert</span><span class="ansi-yellow-intense-fg ansi-bold">(</span><span class="ansi-yellow-intense-fg ansi-bold">(</span>a <span class="ansi-yellow-intense-fg ansi-bold">+</span> b<span class="ansi-yellow-intense-fg ansi-bold">)</span> <span class="ansi-yellow-intense-fg ansi-bold">+</span> c <span class="ansi-yellow-intense-fg ansi-bold">==</span> a <span class="ansi-yellow-intense-fg ansi-bold">+</span> <span class="ansi-yellow-intense-fg ansi-bold">(</span>b <span class="ansi-yellow-intense-fg ansi-bold">+</span> c<span class="ansi-yellow-intense-fg ansi-bold">)</span><span class="ansi-yellow-intense-fg ansi-bold">)</span>

<span class="ansi-red-fg">AssertionError</span>: </pre>
</div>
</div>

</div>
</div>
................................................................................
<p>Chiunque (sfido il contrario!) utilizzerebbe la formula esattamente com'è scritta!</p>

</div>
</div>
</div></div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[15]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="k">print</span> <span class="p">(</span><span class="mf">1.</span><span class="o">/</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<h2 id="MA...">MA...<a class="anchor-link" href="#MA...">&#182;</a></h2><p>Qualcuno (non so davvero chi...decisamente me incluso) potrebbe pensare di calcolarla sviluppando il rapporto come:</p>

</div>
</div>
</div></div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[16]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">def</span> <span class="nf">isqrt</span><span class="p">(</span><span class="n">number</span><span class="p">):</span>
    <span class="k">assert</span> <span class="n">number</span> <span class="o">&gt;</span> <span class="mi">0</span>
    <span class="n">threehalfs</span> <span class="o">=</span> <span class="mf">1.5</span>
    <span class="n">x2</span> <span class="o">=</span> <span class="n">number</span> <span class="o">*</span> <span class="mf">0.5</span>
    <span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">(</span><span class="n">number</span><span class="p">)</span> <span class="c1">#conversione a float32 del numero</span>

    <span class="n">i</span> <span class="o">=</span> <span class="n">y</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span> <span class="c1">#&quot;vedi&quot; y come una variabile int32</span>
    <span class="n">i</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">(</span><span class="mh">0x5f3759df</span><span class="p">)</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">(</span><span class="n">i</span> <span class="o">&gt;&gt;</span> <span class="mi">1</span><span class="p">)</span> <span class="c1">#differenza tra numeri in bit-format</span>
    <span class="n">y</span> <span class="o">=</span> <span class="n">i</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>

    <span class="n">y</span> <span class="o">=</span> <span class="n">y</span> <span class="o">*</span> <span class="p">(</span><span class="n">threehalfs</span> <span class="o">-</span> <span class="p">(</span><span class="n">x2</span> <span class="o">*</span> <span class="n">y</span> <span class="o">*</span> <span class="n">y</span><span class="p">))</span>
    <span class="k">return</span> <span class="nb">float</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>

<span class="k">print</span> <span class="p">(</span><span class="n">isqrt</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>0.499153574792
</pre>
</div>
</div>

</div>
</div>

................................................................................
</div></section></section><section><section>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Un-esempio-pi&#249;-comune">Un esempio pi&#249; comune<a class="anchor-link" href="#Un-esempio-pi&#249;-comune">&#182;</a></h2><p>Supponiamo di voler calcolare il valore medio di un vettore $x$. Saremo tutti d'accordo che questo sia calcolabile come:</p>
<p>$E[x] = \sum_{i=1}^N \frac{x_i}{N}$</p>
<p>che è certamente equivalente a</p>
<p>$E[x] = \frac{1}{N}\sum_{i=1}^N x_i$</p>
<p>Bene...proviamo allora</p>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[17]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="n">N</span> <span class="o">=</span> <span class="mi">10000</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="n">N</span><span class="p">)</span> <span class="c1">#vettore contenente N numeri random estratti uniformemente in [0,1]</span>
<span class="n">mean_1</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="n">mean_2</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="k">for</span> <span class="n">x_i</span> <span class="ow">in</span> <span class="n">x</span><span class="p">:</span>
    <span class="n">mean_1</span> <span class="o">+=</span> <span class="n">x_i</span> <span class="o">/</span> <span class="n">N</span>
    <span class="n">mean_2</span> <span class="o">+=</span> <span class="n">x_i</span>
<span class="n">mean_2</span> <span class="o">/=</span> <span class="n">N</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;Media 1 = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_1</span><span class="p">))</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;Media 2 = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_2</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>Media 1 = 0.49977160834157097202279373959755
Media 2 = 0.49977160834156963975516418940970
</pre>
</div>
</div>

</div>
</div>

</div></section></section><section><section>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Cost-of-Operations">Cost of Operations<a class="anchor-link" href="#Cost-of-Operations">&#182;</a></h2><p><img src="./immagini/time.png" alt="alt text" title="## Cost of Operations"></p>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Cost-of-Functions">Cost of Functions<a class="anchor-link" href="#Cost-of-Functions">&#182;</a></h2><p><img src="./immagini/time2.png" alt="alt text" title="## Cost of Operations"></p>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
................................................................................
</div>
</div></div></section></section><section><section>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Ad-esempio">Ad esempio<a class="anchor-link" href="#Ad-esempio">&#182;</a></h2><p><img src="./immagini/vec.png" alt="alt text" title="## Cost of Operations"></p>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
................................................................................
<p><strong>NOTA</strong>: la vettorizzazione di un loop è uno dei metodi più rapidi da implementare e con il miglior guadagno in tempi di calcolo...soprattutto nei linguaggi ad alto livello!</p>

</div>
</div>
</div><div class="fragment">
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[18]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">print</span> <span class="p">(</span><span class="s1">&#39;true&#39;</span> <span class="k">if</span> <span class="bp">True</span> <span class="k">else</span> <span class="s1">&#39;false&#39;</span><span class="p">)</span> <span class="c1"># operatore ternario del C++ in Python: la sintassi è &quot;output1 se condizione, altrimenti output2</span>
<span class="k">print</span> <span class="p">(</span><span class="s1">&#39;false&#39;</span> <span class="k">if</span> <span class="bp">False</span> <span class="k">else</span> <span class="s1">&#39;false&#39;</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s1">&#39;odd&#39;</span> <span class="k">if</span> <span class="mi">5</span><span class="o">%</span><span class="k">2</span> else &#39;even&#39;)
<span class="k">print</span> <span class="p">(</span><span class="s1">&#39;odd&#39;</span> <span class="k">if</span> <span class="mi">4</span><span class="o">%</span><span class="k">2</span> else &#39;even&#39;)
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<h2 id="I-NumPy-array-e-ndarray">I NumPy array e ndarray<a class="anchor-link" href="#I-NumPy-array-e-ndarray">&#182;</a></h2><p>Proprio come gli std::vector<type> del C++, NumPy mette a disposizione i suoi contenitori per i vettori, chiamati np.ndarray (N-dimensional array object), contenitori <strong>veloci</strong>, <strong>flessibili</strong> per <strong>grandi data sets</strong>.</p>

</div>
</div>
</div><div class="fragment">
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[19]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">])</span> <span class="c1"># vettore 1x4</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">],[</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">8</span><span class="p">]])</span> <span class="c1"># matrice 2x4</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;b = &quot;</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
</ul>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[20]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">]</span>
<span class="n">a_array</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">b_array</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a_array</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="n">b_array</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a_array</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">a_array</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>(4L,)
(4L,)
int32
4
</pre>
</div>
</div>

</div>
</div>
................................................................................
<h2 id="Creare-array">Creare array<a class="anchor-link" href="#Creare-array">&#182;</a></h2><p>Ci sono 3 funzioni importanti per la creazione di array e l'inizializzazione di array.</p>

</div>
</div>
</div><div class="fragment">
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[21]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">array_zeros</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="c1"># vettore 1x10 di zeri</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;array_zeros = &quot;</span><span class="p">,</span> <span class="n">array_zeros</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;array_zeros ha dimensioni : &quot;</span><span class="p">,</span> <span class="n">array_zeros</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="n">a_matzeros</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="mi">10</span><span class="p">))</span> <span class="c1"># matrice 2x10 di zeri</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a_matzeros ha dimensioni : &quot;</span><span class="p">,</span> <span class="n">a_matzeros</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="n">array_ones</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="c1"># vettore 1x10 di uno</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;aarray_ones = &quot;</span><span class="p">,</span> <span class="n">array_ones</span><span class="p">)</span>
<span class="n">a_empty</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span> <span class="c1"># vettore 1x20 di valori &quot;vuoti&quot;=locazioni di memoria libere</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a_empty = &quot;</span><span class="p">,</span> <span class="n">a_empty</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a_empty contiene oggetti di tipo : &quot;</span><span class="p">,</span> <span class="n">a_empty</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>array_zeros =  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
array_zeros ha dimensioni :  (10L,)
a_matzeros ha dimensioni :  (2L, 10L)
aarray_ones =  [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
a_empty =  [  2.85808557e-316   6.22522714e-322   0.00000000e+000   0.00000000e+000
   1.90979621e-313   5.30276956e+180   7.70748458e-043   2.99877042e-066
   3.65961324e-086   4.25757156e-096   2.64936654e+180   3.80884741e+180
   8.03704417e-095   9.97328607e-143   1.47278628e+179   8.37170573e-144
   6.53456787e-042   3.21430743e-057   4.46920458e-090   1.72171244e+184]
a_empty contiene oggetti di tipo :  float64
</pre>
</div>
</div>

</div>
</div>
................................................................................
<p>Le principali funzioni sono 2: np.arange e np.linspace</p>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[22]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="c1"># vettore di 10 elementi con numeri crescenti da 0(default) e 10</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="c1"># (valore iniziale, valore finale, step)</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span> <span class="c1"># (valore iniziale, valore finale, numero di punti)</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................

</div>
</div>

</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[23]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a contiene elementi di tipo : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">uint32</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a contiene elementi di tipo : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">&#39;1.21&#39;</span><span class="p">,</span> <span class="s1">&#39;.2&#39;</span><span class="p">,</span> <span class="s1">&#39;-.4&#39;</span><span class="p">],</span> <span class="n">dtype</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">string_</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a di stringhe = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a cast 2 float = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>a =  [ 1.  2.  3.  4.]
a contiene elementi di tipo :  float64
a =  [1 2 3 4]
a contiene elementi di tipo :  uint32
a di stringhe =  [&#39;1.21&#39; &#39;.2&#39; &#39;-.4&#39;]
a cast 2 float =  [ 1.21  0.2  -0.4 ]
</pre>
</div>
</div>

</div>
</div>
................................................................................
<p>...ma soprattutto hanno tutta una serie di operazioni <strong>già vettorizzate</strong>!!</p>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[24]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">])</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">])</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> OPERAZIONI ARITMETICHE</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;b = &quot;</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a * 10 : &quot;</span><span class="p">,</span> <span class="n">a</span> <span class="o">*</span> <span class="mi">10</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a + 2 : &quot;</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a^2 : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> OPERAZIONI TRA ARRAY</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a * b : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">*</span><span class="n">b</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a + b : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a - b : &quot;</span><span class="p">,</span> <span class="n">a</span><span class="o">-</span><span class="n">b</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;1. / a : &quot;</span><span class="p">,</span> <span class="mf">1.</span><span class="o">/</span><span class="n">a</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;1 / a : &quot;</span><span class="p">,</span> <span class="mi">1</span><span class="o">/</span><span class="n">a</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................

 OPERAZIONI TRA ARRAY

a * b :  [ 2  6 12 20]
a + b :  [3 5 7 9]
a - b :  [-1 -1 -1 -1]
1. / a :  [ 1.          0.5         0.33333333  0.25      ]
1 / a :  [1 0 0 0]
</pre>
</div>
</div>

</div>
</div>

................................................................................
<p>Posso anche prendere pezzi dei vettori e combinarli insieme.</p>

</div>
</div>
</div><div class="fragment">
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[162]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="mi">8</span><span class="p">])</span> <span class="c1"># ottavo elemento del vettore</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[:</span><span class="mi">8</span><span class="p">])</span> <span class="c1"># primi 8 elementi</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="mi">8</span><span class="p">:</span><span class="mi">12</span><span class="p">])</span> <span class="c1"># elementi compresi tra l&#39;ottavo e il dodicesimo</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="mi">12</span><span class="p">:</span><span class="o">-</span><span class="mi">4</span><span class="p">])</span> <span class="c1"># elementi compresi tra il 12esimo e il quart&#39;ultimo</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="o">-</span><span class="mi">4</span><span class="p">:])</span> <span class="c1"># elementi dal quart&#39;ultimo e la fine</span>
<span class="k">assert</span><span class="p">(</span> <span class="n">np</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="o">-</span><span class="mi">15</span><span class="p">:</span><span class="o">-</span><span class="mi">10</span><span class="p">]</span> <span class="o">==</span> <span class="n">a</span><span class="p">[</span><span class="mi">5</span><span class="p">:</span><span class="mi">10</span><span class="p">]))</span> 
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="ATTENZIONE!">ATTENZIONE!<a class="anchor-link" href="#ATTENZIONE!">&#182;</a></h2>
</div>
</div>
</div><div class="fragment">
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[165]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">arr_slice</span> <span class="o">=</span> <span class="n">a</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">5</span><span class="p">]</span> <span class="c1"># pezzo del vettore a</span>
<span class="n">arr_slice</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">10000</span> <span class="c1"># modifica del secondo elemento del pezzo di a</span>
<span class="k">print</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<pre><code>                arr[2:5].copy()</code></pre>

</div>
</div>
</div></div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[176]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">arr3d</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]],</span> <span class="p">[[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">],</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">]]])</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot; a = &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;primo elemento : &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;prima riga : &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;prima matrice : &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;profondità : &quot;</span><span class="p">,</span> <span class="n">arr3d</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">])</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<pre><code>                    np.mean(array)</code></pre>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[15]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">time</span>
<span class="n">start_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="c1"># inizia a contare il tempo</span>
<span class="n">N</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="mf">1e7</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="n">N</span><span class="p">)</span>
<span class="n">mean_for_1</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="n">mean_for_2</span> <span class="o">=</span> <span class="mf">0.0</span>
<span class="k">for</span> <span class="n">x_i</span> <span class="ow">in</span> <span class="n">x</span><span class="p">:</span>
    <span class="n">mean_for_1</span> <span class="o">+=</span> <span class="n">x_i</span><span class="o">/</span><span class="n">N</span>
    <span class="n">mean_for_2</span> <span class="o">+=</span> <span class="n">x_i</span>
<span class="n">mean_for_2</span> <span class="o">/=</span> <span class="n">N</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;Mean 1 = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_for_1</span><span class="p">))</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;Mean 2 = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_for_2</span><span class="p">))</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;Calcolate in </span><span class="si">%.32f</span><span class="s2"> sec&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span><span class="o">-</span><span class="n">start_time</span><span class="p">))</span>

<span class="n">start_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
<span class="n">mean_vec</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;Mean vettorizzata = </span><span class="si">%.32f</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">mean_vec</span><span class="p">))</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;Calcolata in </span><span class="si">%.32f</span><span class="s2"> sec&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span><span class="o">-</span><span class="n">start_time</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>Mean 1 = 0.50003546594170067418616554277833
Mean 2 = 0.50003546594162207039602208169526
Calcolate in 33.26500010490417480468750000000000 sec
Mean vettorizzata = 0.50003546594165892980043963689241
Calcolata in 0.04699993133544921875000000000000 sec
</pre>
</div>
</div>

</div>
</div>

................................................................................
<p>Numpy predispone già un'ampia gamma di generatori!</p>

</div>
</div>
</div><div class="fragment">
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[193]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="n">random</span>
<span class="k">print</span> <span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">())</span> <span class="c1"># uniform distribution</span>
<span class="k">print</span> <span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">())</span> <span class="c1"># normal distribution</span>
<span class="k">print</span> <span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">exponential</span><span class="p">())</span> <span class="c1"># exponential distribution</span>
<span class="k">print</span> <span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span> <span class="c1"># random matrix</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<div class="output">


<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stdout output_text">
<pre>0.0803586638819
-1.11673488584
0.466184685074
[[ 0.73152502  0.59559674  0.73434003]
 [ 0.18037221  0.05359538  0.28565275]]
</pre>
</div>
</div>

</div>
</div>

................................................................................
<h2 id="Una-carrellata-di-altre-funzioni-utili...">Una carrellata di altre funzioni utili...<a class="anchor-link" href="#Una-carrellata-di-altre-funzioni-utili...">&#182;</a></h2><p><strong>NOTA</strong> In Matlab la nomenclatura è <strong>quasi sempre</strong> la stessa, ovviamente senza l'np davanti!</p>

</div>
</div>
</div><div class="fragment">
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[30]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mf">3.444</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mf">4.29</span><span class="p">,</span> <span class="mf">6.98</span><span class="p">])</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mf">3.44</span><span class="p">,</span> <span class="o">-</span><span class="mf">2.</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">])</span>

<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> ALCUNE altre operazioni </span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>

<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># absolute value </span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">fabs</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># absolute value </span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># square root</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># the largest integer value less than or equal to x</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># smallest integer value greater than or equal to x</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
</div>
</div>

<div class="output_area">
<div class="prompt"></div>

<div class="output_subarea output_stream output_stderr output_text">
<pre>C:\Users\NICO\Anaconda2\lib\site-packages\ipykernel\__main__.py:8: RuntimeWarning: invalid value encountered in sqrt
</pre>
</div>
</div>

</div>
</div>

</div></div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[27]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;b = &quot;</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> ALCUNE operazioni logiche </span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>

<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">logical_and</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span> <span class="c1"># AND</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">logical_or</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span> <span class="c1"># OR</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">greater</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span> <span class="c1"># &gt;</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">equal</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span> <span class="c1"># ==</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................

</div>
</div>

</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[29]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">print</span> <span class="p">(</span><span class="s2">&quot;a = &quot;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;b = &quot;</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>

<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2"> ALCUNE operazioni sul posizionamento </span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># SORTING ELEMENTS</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">argsort</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># SORTING INDICES</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">a</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span> <span class="c1"># INDICES WHERE CONDITION</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">a</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span> <span class="c1"># (CONDITION, IF(CONDITION), ELSE)</span>
<span class="k">print</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">median</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="c1"># MEDIAN</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
................................................................................
<h2 id="Basta-teoria!-E'-ora-di-sporcarsi-un-po'-le-mani-scrivendo-del-codice!">Basta teoria! E' ora di sporcarsi un po' le mani scrivendo del codice!<a class="anchor-link" href="#Basta-teoria!-E'-ora-di-sporcarsi-un-po'-le-mani-scrivendo-del-codice!">&#182;</a></h2><p>Da quello che abbiamo imparato dovreste essere in grado di vettorizzare facilmente il codice seguente per il calcolo del $\pi$. Controllate anche il tempo di esecuzione delle due versioni dell'algoritmo!</p>

</div>
</div>
</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">time</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Metropolis Algorithm</span>

<span class="sd">Algoritmo per il calcolo dell&#39;integrale di una funzione attraverso un campionamento Monte Carlo. </span>
<span class="sd">In questo esempio viene utilizzato per il calcolo del valore del pi-greco. Si consideri al proposito un cerchio di</span>
<span class="sd">raggio unitario (r = 1) centrato nell&#39;origine, inscritto in un quadrato. In questo modo il quadrato avrà lato pari a 2</span>
<span class="sd">e la sua area sarà uguale a 4. Andando ad estrarre in modo uniforme nell&#39;intervallo [-1,1] una coppia di numeri (x, y),</span>
<span class="sd">qualora le coordinate risultino appartenenti al cerchio verrà conteggiato un Hit. La condizione da imporre sarà quindi:</span>

<span class="sd">                                            x^2 + y^2 &lt; 1</span>
<span class="sd">                                </span>
<span class="sd">Secondo la teoria dei metodi Monte Carlo, il numero di Hit rispetto al totale sarà equivalente (per un numero tendente</span>
<span class="sd">all&#39;infinito di estrazioni) al rapporto tra le aree. In questo caso quindi</span>

<span class="sd">                                            Hit/Tot = pi/4</span>
<span class="sd">                                            </span>
<span class="sd">Volendo estrarre un risultato più robusto si consiglia di campionare il valore di pi un numero N_runs di volte, in modo</span>
<span class="sd">che il corretto valore di pi sia dato dalla media della distribuzione di più campionamenti Monte Carlo.</span>
<span class="sd">&quot;&quot;&quot;</span>
</pre></div>

</div>
</div>
</div>

</div></section></section><section><section>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[16]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">N</span> <span class="o">=</span> <span class="mi">100000</span> <span class="c1"># number of MC events</span>
<span class="n">N_run</span> <span class="o">=</span> <span class="mi">100</span> <span class="c1"># number of runs</span>
<span class="n">Nhits</span> <span class="o">=</span> <span class="mf">0.0</span> <span class="c1"># number of points accepted</span>
<span class="n">pi</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">N_run</span><span class="p">)</span> <span class="c1"># values of pi</span>

<span class="n">start_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="c1"># start clock </span>
<span class="k">for</span> <span class="n">I</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">N_run</span><span class="p">):</span>
    <span class="n">Nhits</span> <span class="o">=</span> <span class="mf">0.0</span>
................................................................................
        <span class="n">res</span> <span class="o">=</span> <span class="n">x</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="o">*</span><span class="n">y</span>
        <span class="k">if</span> <span class="n">res</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">Nhits</span> <span class="o">+=</span> <span class="mf">1.0</span>
    <span class="n">pi</span><span class="p">[</span><span class="n">I</span><span class="p">]</span> <span class="o">+=</span> <span class="mf">4.</span> <span class="o">*</span> <span class="n">Nhits</span><span class="o">/</span><span class="n">N</span>

<span class="n">run_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>

<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;pi with &quot;</span><span class="p">,</span> <span class="n">N</span><span class="p">,</span> <span class="s2">&quot; steps for &quot;</span><span class="p">,</span> <span class="n">N_run</span><span class="p">,</span> <span class="s2">&quot; runs is &quot;</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">pi</span><span class="p">),</span> <span class="s2">&quot; in &quot;</span><span class="p">,</span> <span class="n">run_time</span><span class="o">-</span><span class="n">start_time</span><span class="p">,</span> <span class="s2">&quot; sec&quot;</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="s2">&quot;Precision computation : &quot;</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">pi</span><span class="p">)</span><span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">

Changes to Lezione 5 - Librerie per il calcolo numerico.html.

more than 10,000 changes

Changes to Lezione 5 - Librerie per il calcolo numerico.ipynb.

cannot compute difference between binary files

Changes to Lezione 5 - Librerie per il calcolo numerico.slides.html.

more than 10,000 changes