summaryrefslogtreecommitdiff
path: root/PVCM/cama/en/ma1 np01 Numpy Introduction.ipynb
blob: bf308355ca76a7c9ff81e3c6bb0d715b3a6588ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
{
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4,
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "# NumPy - N-dimensional Array manipulations library\n",
    "\n",
    "[NumPy](https://docs.scipy.org/doc/numpy/user/index.html) is a toolkit for scientific program development. It\n",
    "provides multidimensional arrays that are simple to manipulate and **efficient** (which Python lists are not).\n",
    "\n",
    "Numpy's syntax is also used in other languages like R or Julia. In AI, TensorFlow, PyTorch and JAX use also this syntax."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # np is the convention"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "Unlike lists, __tables have all their values of the same type__.\n",
    "\n",
    "## Creating an array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 7]]\n",
      "shape:  (2, 3)\n",
      "type:  <class 'numpy.int64'>\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1,2,3], [4,5,7]]) # 2D array built from a list\n",
    "print(a)\n",
    "print(\"shape: \",a.shape)\n",
    "print(\"type: \",type(a[0,0]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "#### dtype: the choice of weapons\n",
    "\n",
    "When doing scientific calculation it is important to choose the type of tables to optimize memory, errors, speed.\n",
    "\n",
    "NumPy offers the following types:\n",
    "\n",
    "<table border=\"1\" class=\"docutils\">\n",
    "<colgroup>\n",
    "<col width=\"15%\" />\n",
    "<col width=\"85%\" />\n",
    "</colgroup>\n",
    "<thead valign=\"bottom\">\n",
    "<tr class=\"row-odd\"><th class=\"head\">Data type</th>\n",
    "<th class=\"head\">Description</th>\n",
    "</tr>\n",
    "</thead>\n",
    "<tbody valign=\"top\">\n",
    "<tr class=\"row-even\"><td>bool</td>\n",
    "<td>Boolean (True or False) stored as a byte</td>\n",
    "</tr>\n",
    "<tr class=\"row-odd\"><td>int</td>\n",
    "<td>Platform integer (usually either <tt class=\"docutils literal\"><span class=\"pre\">int32</span></tt> or <tt class=\"docutils literal\"><span class=\"pre\">int64</span></tt>) </td>\n",
    "</tr>\n",
    "<tr class=\"row-even\"><td>int8</td>\n",
    "<td>Byte (-128 to 127) </td>\n",
    "</tr>\n",
    "<tr class=\"row-odd\"><td>int16</td>\n",
    "<td>Integer (-32768 to 32767) </td>\n",
    "</tr>\n",
    "<tr class=\"row-even\"><td>int32</td>\n",
    "<td>Integer (-2147483648 to 2147483647) </td>\n",
    "</tr>\n",
    "<tr class=\"row-odd\"><td>int64</td>\n",
    "<td>Integer (9223372036854775808 to 9223372036854775807) </td>\n",
    "</tr>\n",
    "<tr class=\"row-even\"><td>uint8</td>\n",
    "<td>Unsigned integer (0 to 255) </td>\n",
    "</tr>\n",
    "<tr class=\"row-odd\"><td>uint16</td>\n",
    "<td>Unsigned integer (0 to 65535) </td>\n",
    "</tr>\n",
    "<tr class=\"row-even\"><td>uint32</td>\n",
    "<td>Unsigned integer (0 to 4294967295) </td>\n",
    "</tr>\n",
    "<tr class=\"row-odd\"><td>uint64</td>\n",
    "<td>Unsigned integer (0 to 18446744073709551615) </td>\n",
    "</tr>\n",
    "<tr class=\"row-even\"><td>float</td>\n",
    "<td>Shorthand for <tt class=\"docutils literal\"><span class=\"pre\">float64</span></tt>.</td>\n",
    "</tr>\n",
    "<tr class=\"row-odd\"><td>float16</td>\n",
    "<td>Half precision float: sign bit, 5 bits exponent,\n",
    "10-bit mantissa</td>\n",
    "</tr>\n",
    "<tr class=\"row-even\"><td>float32</td>\n",
    "<td>Single precision float: sign bit, 8 bits exponent,\n",
    "23-bit mantissa</td>\n",
    "</tr>\n",
    "<tr class=\"row-odd\"><td>float64</td>\n",
    "<td>Double precision float: sign bit, 11 bits exponent,\n",
    "52-bit mantissa</td>\n",
    "</tr>\n",
    "<tr class=\"row-even\"><td>complex</td>\n",
    "<td>Shorthand for <tt class=\"docutils literal\"><span class=\"pre\">complex128</span></tt>.</td>\n",
    "</tr>\n",
    "<tr class=\"row-odd\"><td>complex64</td>\n",
    "<td>Complex number, represented by two 32-bit floats (real\n",
    "and imaginary components) </td>\n",
    "</tr>\n",
    "<tr class=\"row-even\"><td>complex128</td>\n",
    "<td>Complex number, represented by two 64-bit floats (real\n",
    "and imaginary components) </td>\n",
    "</tr>\n",
    "</tbody>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x = [0 1 2 3] uint8\n",
      "x = [254   1   2   3] uint8\n",
      "y = [254.   1.   2.   3.] float32\n"
     ]
    }
   ],
   "source": [
    "x = np.arange(4, dtype= np.uint8)  # arange is the numpy version of range to produce an array\n",
    "print(\"x =\", x, x.dtype)\n",
    "\n",
    "x[0] = -2                          # 0 - 2 = max -1 for unsigned int\n",
    "print(\"x =\", x, x.dtype)\n",
    "\n",
    "y = x.astype('float32')            # conversion\n",
    "print(\"y =\", y, y.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "We can know the memory size (in bytes) occupied by an element of the array:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "4\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(x[0].itemsize)\n",
    "print(y[0].itemsize)\n",
    "y.nbytes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "#### Predefined methods\n",
    "\n",
    "There is also the possibility to create an empty or predefined array of the dimension of your choice using\n",
    "[predefined creation methods](https://docs.scipy.org/doc/numpy/reference/routines.array-creation.html):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Empty float:\n",
      " [[2.42350504e-316 0.00000000e+000]\n",
      " [4.94065646e-324             nan]]\n",
      "Float zeros:\n",
      " [[0. 0.]\n",
      " [0. 0.]]\n",
      "Complex ones:\n",
      " [[1.+0.j 1.+0.j 1.+0.j]\n",
      " [1.+0.j 1.+0.j 1.+0.j]]\n",
      "Full of 3.2:\n",
      " [[3.2 3.2]\n",
      " [3.2 3.2]]\n",
      "La matrice suivante est affichée partiellement car trop grande : \n",
      "[[1. 0. 0. ... 0. 0. 0.]\n",
      " [0. 1. 0. ... 0. 0. 0.]\n",
      " [0. 0. 1. ... 0. 0. 0.]\n",
      " ...\n",
      " [0. 0. 0. ... 1. 0. 0.]\n",
      " [0. 0. 0. ... 0. 1. 0.]\n",
      " [0. 0. 0. ... 0. 0. 1.]]\n"
     ]
    }
   ],
   "source": [
    "a = np.empty((2,2), dtype=float)  # empty do not set any value, it is faster\n",
    "print(\"Empty float:\\n\", a)\n",
    "\n",
    "print(\"Float zeros:\\n\", np.zeros((2,2), dtype=float))   # matrix filled with 0\n",
    "\n",
    "print(\"Complex ones:\\n\", np.ones((2,3), dtype=complex))  # matrix filled with 1\n",
    "\n",
    "print(\"Full of 3.2:\\n\", np.full((2,2), 3.2))\n",
    "\n",
    "print(\"La matrice suivante est affichée partiellement car trop grande : \")\n",
    "print(np.identity(1000))              # identity matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "#### With random values\n",
    "\n",
    "The sub-library [`np.random`](https://docs.scipy.org/doc/numpy/reference/routines.random.html) provides methods for generating arrays of random values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Random integers < 10:\n",
      " [[7 5 7 2]\n",
      " [6 7 0 8]\n",
      " [6 3 8 3]]\n",
      "Random reals between 0 and 1 :\n",
      " [[0.45228105 0.39674551 0.02142443 0.14342572]\n",
      " [0.95223097 0.85798264 0.85777533 0.28784271]\n",
      " [0.5142878  0.36823048 0.87704756 0.44096786]]\n"
     ]
    }
   ],
   "source": [
    "print(\"Random integers < 10:\\n\", np.random.randint(10, size=(3,4))) # can also choose a min\n",
    "\n",
    "print(\"Random reals between 0 and 1 :\\n\", np.random.random(size=(3,4)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "We can choose the law of distribution (uniform law by default)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.44216124, 2.5586318 , 4.28770816],\n",
       "       [2.16814785, 1.82332334, 4.53031302]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loc = 3\n",
    "scale = 1.5\n",
    "np.random.normal(loc, scale, size=(2,3))  # Gauss distribution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "### By redefining its shape\n",
    "\n",
    "A classic case for testing is to create a small multidimensional array with different values.\n",
    "For this the simplest is to put 0,1,2,...,N in the boxes of our form table (3,4) for example.\n",
    "This is done with `arange` which we have already seen to generate the values and `reshape` to have the desired form:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.arange(3*4).reshape((3,4))\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[1, 0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "Attention, the numbering of the boxes of a table is carried out with nested loops.  It is always the last dimension which varies most quickly. In 3D this means that going from one element to the next varies the last index (along z). This does not correspond to the human way of filling a cube (we tend to stack 2D arrays). In \n",
    "regular use this is not a problem, it is\n",
    "only weird when printing tests to check.\n",
    "\n",
    "```\n",
    " human    Numpy\n",
    "   ┌─┬─┐    ┌─┬─┐\n",
    "┌─┬─┐│5│ ┌─┬─┐│3│\n",
    "│0│1│┼─┤ │0│2│┼─┤\n",
    "├─┼─┤│7│ ├─┼─┤│7│\n",
    "│2│3│┴─┘ │4│6│┴─┘\n",
    "└─┴─┘    └─┴─┘\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0, 1],\n",
       "        [2, 3]],\n",
       "\n",
       "       [[4, 5],\n",
       "        [6, 7]]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.arange(8).reshape(2,2,2) #  a[0,1,0] is 2 and a[0,1,1] is 3\n",
    "A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "The reverse of reshape is `flatten()` which reshape a multi-dimensional array into a 1-dimensional array. We can also use `flat` to have a 1D view on the array without reshaping it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "(2, 2, 2)\n"
     ]
    }
   ],
   "source": [
    "print(A.flat[5])\n",
    "print(A.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "### Mixing values\n",
    "\n",
    "If we want to work with the values of an array taken in a random order then we can mix\n",
    "the table with `np.random.permutation()`. Attention the permutation is carried out on the elements of the first level of the array, `A[:]`, i.e. arrays\n",
    "if the array is multi-dimensional (an array of arrays)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [ 0,  1,  2,  3]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.arange(12).reshape(3,4)\n",
    "np.random.permutation(data) # permutes lines only"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "If you want to mix all the values ​​you have to flatten the table and give it back its shape:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10,  3, 11,  8],\n",
       "       [ 4,  1,  2,  7],\n",
       "       [ 0,  9,  6,  5]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.permutation(data.flatten()).reshape(data.shape) # it works because flatten returns a copy\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "#### With its own function\n",
    "\n",
    "We can also create an array with a function that gives the value of the array for each index (i, j):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0, -1, -2, -3],\n",
       "       [ 2,  1,  0, -1],\n",
       "       [ 4,  3,  2,  1]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def f(i,j):\n",
    "    return 2*i - j\n",
    "\n",
    "np.fromfunction(f, shape=(3,4), dtype=int)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "## Basic Operations\n",
    "\n",
    "Numpy makes it possible to apply the usual operations to all the elements of the array:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A + 1:\n",
      " [[2 3]\n",
      " [4 5]] \n",
      "\n",
      "2 A + Id:\n",
      " [[3. 4.]\n",
      " [6. 9.]] \n",
      "\n",
      "A * A (element-wise product):\n",
      " [[ 1  4]\n",
      " [ 9 16]] \n",
      "\n",
      "A @ A (matrix or dot product):\n",
      " [[ 7 10]\n",
      " [15 22]]\n"
     ]
    }
   ],
   "source": [
    "A = np.array([[1,2], [3,4]])\n",
    "\n",
    "print(\"A + 1:\\n\", A + 1, '\\n')\n",
    "print(\"2 A + Id:\\n\", 2 * A + np.identity(2), '\\n')\n",
    "print(u\"A * A (element-wise product):\\n\", A * A, '\\n')  # or np.square(A)\n",
    "print(u\"A @ A (matrix or dot product):\\n\", A @ A)  # or A.dot(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "You can transpose an array with `.T`. It does nothing with a 1D array, to make a horizontal vector or a vertical vector, you have to write it in 2D:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 3 5]] \n",
      "\n",
      " [[1]\n",
      " [3]\n",
      " [5]] \n",
      "\n",
      "Guess what v + v.T means:\n",
      " [[ 2  4  6]\n",
      " [ 4  6  8]\n",
      " [ 6  8 10]]\n"
     ]
    }
   ],
   "source": [
    "v = np.array([[1,3,5]])\n",
    "print(v, '\\n\\n', v.T, '\\n')\n",
    "print(\"Guess what v + v.T means:\\n\", v + v.T)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "Trigonometric functions and other usual mathematical functions are also available."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.841,  0.909],\n",
       "       [ 0.141, -0.757]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.set_printoptions(precision=3)  # set printing precision for reals\n",
    "np.sin(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "Finally Numpy offers a set of methods to perform calculations on array elements:\n",
    "\n",
    "* `sum()` to sum all elements\n",
    "* `mean()` to get the average of the elements and `average()` to get the weighted average,\n",
    "* `prod()` to multiply all elements,\n",
    "* `min()` and `max()` to get the minimum value and the maximum value,\n",
    "* `argmin()` and `argmax()` to have the array indices of the minimum and maximum value,\n",
    "* `cumsum()` and `cumprod()` for cumulative addition and multiplication,\n",
    "* `diff()` to get the gap with the next element (useful to calculate a derivative).\n",
    "\n",
    "Each `A.sum()` method also exists as a `np.sum(A)` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.argmax()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 1])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.diff(A.flatten())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "## Browse an array\n",
    "\n",
    "The natural way to browse all the elements of a multi-dimensional array is to make a loop for each dimension:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  0  1\n",
      "  2  3\n",
      "  4  5\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(6).reshape(3,2)\n",
    "for ligne in a:\n",
    "    for element in ligne:\n",
    "        print(\" \", element, end=\"\")  # end=\"\" avoid the return after each print\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "We saw in the manipulation of the form of an array that we can make it flatter, but rather than using `flatten()` which makes an array in 1 dimension, we prefer to use `flat` which gives an [iterator](https://en.wikipedia.org/wiki/Iterator) to iterate through all the elements of the array."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "for v in a.flat:\n",
    "    print(v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "It is also possible to make a loop on the indices but it is clearly less powerful."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "for i in range(len(a)):\n",
    "    for j in range(len(a[i])):\n",
    "        print(a[i,j])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "lang": "en"
   },
   "source": [
    "## Think vector\n",
    "\n",
    "Making loops is often our way of thinking but it is not effective. It is better to\n",
    "to work directly on the whole array. So rather than making the loop\n",
    "\n",
    "```\n",
    "for i in range (len (x)):\n",
    "   z[i] = x[i] + y[i]\n",
    "```\n",
    "\n",
    "we will do\n",
    "\n",
    "```\n",
    "z = x + y\n",
    "```\n",
    "   \n",
    "Not only is it more readable but it is also faster."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "45.5 ms ± 494 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
      "35.3 ms ± 139 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n",
      "36.9 µs ± 156 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
     ]
    }
   ],
   "source": [
    "def double_loop(a):\n",
    "    for i in range(a.shape[0]):\n",
    "        for j in range(a.shape[1]):\n",
    "            a[i,j] = np.sqrt(a[i,j])  # change in-place\n",
    "\n",
    "def iterate(a):\n",
    "    for x in a.flat:\n",
    "        x = np.sqrt(x)                # modification not saved in a, x is a local var\n",
    "\n",
    "b = np.random.random(size=(200,200))\n",
    "a = b.copy()                      # we need a copy to be sure to use the same data each time\n",
    "%timeit double_loop(a)\n",
    "\n",
    "a = b.copy()       \n",
    "%timeit iterate(a)\n",
    "\n",
    "a = b.copy()\n",
    "%timeit np.sqrt(a)   # vectorial operation, 1000 times faster"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}