Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente Prochaine révisionLes deux révisions suivantes | ||
teaching:progappchim:numpy_simple [2015/03/05 13:14] – villersd | teaching:progappchim:numpy_simple [2018/02/13 05:44] – [Références] villersd | ||
---|---|---|---|
Ligne 9: | Ligne 9: | ||
===== Tableaux numériques ===== | ===== Tableaux numériques ===== | ||
On convertit facilement des listes Python en tableau numpy. Essayez ceci : | On convertit facilement des listes Python en tableau numpy. Essayez ceci : | ||
- | <sxh python;> | + | <code python> |
import numpy as np | import numpy as np | ||
- | a=np.array([[1, | + | a = np.array([[1, |
- | print a | + | print(a) |
- | print a.dtype | + | print(a.dtype) |
- | </sxh> | + | </code> |
Sortie : | Sortie : | ||
< | < | ||
Ligne 27: | Ligne 27: | ||
Vous pouvez consulter [[http:// | Vous pouvez consulter [[http:// | ||
</ | </ | ||
- | <sxh python; title : arrays_01.py> | + | <code python arrays_01.py> |
#! / | #! / | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
Ligne 35: | Ligne 35: | ||
import numpy as np | import numpy as np | ||
- | a=np.array(((1, | + | a = np.array(((1, |
# afficher a, le nombre de dimensions, les dimensions, le type de donnée | # afficher a, le nombre de dimensions, les dimensions, le type de donnée | ||
- | print a, a.ndim, a.shape, a.dtype | + | print(a, a.ndim, a.shape, a.dtype) |
# avec des " | # avec des " | ||
- | b= np.array( | + | b = np.array([ |
- | | + | [1.1, 2.2, 3.3, 4.4], |
[5.5, 6.6, 7.7, 8.8], | [5.5, 6.6, 7.7, 8.8], | ||
- | [9.9, 0.2, 1.3, 2.4]]) | + | [9.9, 0.2, 1.3, 2.4], |
- | print b, b.ndim, b.shape, b.dtype | + | |
+ | print(b, b.ndim, b.shape, b.dtype) | ||
# un tableau de zéros | # un tableau de zéros | ||
- | c=np.zeros((4, | + | c = np.zeros((4, |
- | print c, c.ndim, c.shape, c.dtype | + | print(c, c.ndim, c.shape, c.dtype) |
# un tableau tridimensionnel de 1 " | # un tableau tridimensionnel de 1 " | ||
- | d=np.ones((2, | + | d = np.ones((2, |
- | print d, d.ndim, d.shape, d.dtype | + | print(d, d.ndim, d.shape, d.dtype) |
# un tableau avec arange, et ensuite reshape | # un tableau avec arange, et ensuite reshape | ||
- | e1= np.arange(1, | + | e1 = np.arange(1, |
- | e=np.reshape(e1, | + | e = np.reshape(e1, |
- | print e, e.ndim, e.shape, e.dtype | + | print(e, e.ndim, e.shape, e.dtype) |
- | f=np.random.rand(3, | + | f = np.random.rand(3, |
- | print f, f.ndim, f.shape, f.dtype | + | print(f, f.ndim, f.shape, f.dtype) |
# utilisation de linspace pour imposer le nombre d' | # utilisation de linspace pour imposer le nombre d' | ||
- | g=np.linspace(0., | + | g = np.linspace(0., |
- | print g, g.ndim, g.shape, g.dtype | + | print(g, g.ndim, g.shape, g.dtype) |
- | </sxh> | + | </code> |
Quelques manipulations élémentaires : | Quelques manipulations élémentaires : | ||
- | <sxh python; title : arrays_02.py> | + | <code python arrays_02.py> |
#! / | #! / | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
Ligne 70: | Ligne 71: | ||
import numpy as np | import numpy as np | ||
- | a=np.array([[1, | + | a = np.array([[1, |
- | b=np.array([[1, | + | b = np.array([[1, |
- | c=a+b # addition terme à terme | + | c = a + b # addition terme à terme |
- | print c, c.ndim, c.shape, c.dtype | + | print(c, c.ndim, c.shape, c.dtype) |
- | d=a*b # multiplication terme à terme | + | d =a * b # multiplication terme à terme |
- | print d, d.ndim, d.shape, d.dtype | + | print(d, d.ndim, d.shape, d.dtype) |
- | e=np.dot(a, | + | e = np.dot(a,b) # multiplication matricielle |
- | print e, e.ndim, e.shape, e.dtype | + | print(e, e.ndim, e.shape, e.dtype) |
- | f=np.sin(np.pi*0.5*a) # fonction mathématique et adaptation automatique du type | + | f = np.sin(np.pi*0.5*a) # fonction mathématique et adaptation automatique du type |
- | print f, f.ndim, f.shape, f.dtype | + | print(f, f.ndim, f.shape, f.dtype) |
- | g=np.transpose(a) # transposition | + | g = np.transpose(a) # transposition |
- | print g, g.ndim, g.shape, g.dtype | + | print(g, g.ndim, g.shape, g.dtype) |
- | print np.sum(a), | + | print(np.sum(a), |
- | </sxh> | + | </code> |
==== Fonctions mathématiques principales : ==== | ==== Fonctions mathématiques principales : ==== | ||
Ligne 99: | Ligne 100: | ||
* fonctions booléennes, | * fonctions booléennes, | ||
* // | * // | ||
+ | * // | ||
===== Algèbre linéaire ===== | ===== Algèbre linéaire ===== | ||
- | <sxh python; title : simple_linear_system.py> | + | <code python simple_linear_system.py> |
#! / | #! / | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
""" | """ | ||
Solve a system of simultaneous equation in two variables of the form | Solve a system of simultaneous equation in two variables of the form | ||
- | 2*x + 7*y=17. | + | 2 * x + 7 * y = 17. |
- | 3*x - 5*y=-21. | + | 3 * x - 5 * y = -21. |
reference : http:// | reference : http:// | ||
Ligne 116: | Ligne 118: | ||
a = np.array([[2., | a = np.array([[2., | ||
b = np.array([[17.], | b = np.array([[17.], | ||
- | print np.linalg.solve(a, | + | print(np.linalg.solve(a, |
- | </sxh> | + | </code> |
Quelques possibilités supplémentaires : | Quelques possibilités supplémentaires : | ||
- | <sxh python; title : arrays_linalg_03.py> | + | <code python arrays_linalg_03.py> |
#! / | #! / | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
Ligne 128: | Ligne 130: | ||
import numpy as np | import numpy as np | ||
- | a=np.array([[1, | + | a = np.array([[1, |
- | print a, a.ndim, a.shape, a.dtype | + | print(a, a.ndim, a.shape, a.dtype) |
- | b=np.linalg.inv(a) | + | b = np.linalg.inv(a) |
- | print b, b.ndim, b.shape, b.dtype | + | print(b, b.ndim, b.shape, b.dtype) |
unit = np.eye(2) | unit = np.eye(2) | ||
- | print unit, unit.ndim, unit.shape, unit.dtype | + | print(unit, unit.ndim, unit.shape, unit.dtype) |
v = np.array([[10.], | v = np.array([[10.], | ||
- | x1=np.dot(b, | + | x1 = np.dot(b, |
- | x2=np.linalg.solve(a, | + | x2 = np.linalg.solve(a, |
# des inconnues a et de coefficients indépendants b | # des inconnues a et de coefficients indépendants b | ||
# les deux techniques donnent évidemment le même résultat ! | # les deux techniques donnent évidemment le même résultat ! | ||
- | print x1, x1.ndim, x1.shape, x1.dtype | + | print(x1, x1.ndim, x1.shape, x1.dtype) |
- | print x2, x2.ndim, x2.shape, x2.dtype | + | print(x2, x2.ndim, x2.shape, x2.dtype) |
# valeurs propres et vecteurs propres de matrices : | # valeurs propres et vecteurs propres de matrices : | ||
- | d=np.array([[1, | + | d = np.array([[1, |
- | print np.linalg.eig(d) | + | print(np.linalg.eig(d)) |
- | </sxh> | + | </code> |
Numpy dispose aussi d'une classe particulière de " | Numpy dispose aussi d'une classe particulière de " | ||
Ligne 154: | Ligne 156: | ||
===== Statistiques élémentaires ===== | ===== Statistiques élémentaires ===== | ||
- | <sxh python; title : arrays_stats_elem_04.py> | + | <code python arrays_stats_elem_04.py> |
#! / | #! / | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
Ligne 162: | Ligne 164: | ||
import numpy as np | import numpy as np | ||
- | a=np.array([1., | + | a = np.array([1., |
- | print a, a.ndim, a.shape, a.dtype | + | print(a, a.ndim, a.shape, a.dtype) |
- | print " | + | print(" |
- | print " | + | print(" |
- | print " | + | print(" |
- | print " | + | print(" |
- | </sxh> | + | </code> |
+ | ==== Références complémentaires ==== | ||
+ | * [[http:// | ||
===== Itérations sur les tableaux ===== | ===== Itérations sur les tableaux ===== | ||
- | <sxh python; title : arrays_iteration_05.py> | + | <code python arrays_iteration_05.py> |
#! / | #! / | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
Ligne 179: | Ligne 183: | ||
import numpy as np | import numpy as np | ||
- | a=np.array([1., | + | a = np.array([1., |
for x in a: | for x in a: | ||
- | print x | + | print(x) |
# l' | # l' | ||
- | b= np.array( | + | b = np.array([ |
- | | + | [1.1, 2.2, 3.3, 4.4], |
[5.5, 6.6, 7.7, 8.8], | [5.5, 6.6, 7.7, 8.8], | ||
- | [9.9, 0.2, 1.3, 2.4]]) | + | [9.9, 0.2, 1.3, 2.4], |
+ | | ||
for x in b: | for x in b: | ||
- | print x | + | print(x) |
for y in x: | for y in x: | ||
- | print y,", ", | + | print(y,", ",) |
- | </sxh> | + | </code> |
===== Manipulation de polynômes ===== | ===== Manipulation de polynômes ===== | ||
Ligne 245: | Ligne 250: | ||
Le module de [[http:// | Le module de [[http:// | ||
- | <sxh python; title : fonctions-FT-04.py> | + | <code python fonctions-FT-04.py> |
# | # | ||
#-*- coding: utf-8 -*- | #-*- coding: utf-8 -*- | ||
Ligne 276: | Ligne 281: | ||
x = np.arange(0.0, | x = np.arange(0.0, | ||
y1 = f1(x) | y1 = f1(x) | ||
- | z1=fft.fft(y1) | + | z1 = fft.fft(y1) |
- | w1 = np.abs(z1[: | + | w1 = np.abs(z1[: |
y2 = f2(x) | y2 = f2(x) | ||
- | z2=fft.fft(y2) | + | z2 = fft.fft(y2) |
- | w2 = np.abs(z2[: | + | w2 = np.abs(z2[: |
y3 = f3(x) | y3 = f3(x) | ||
- | z3=fft.fft(y3) | + | z3 = fft.fft(y3) |
- | w3 = np.abs(z3[: | + | w3 = np.abs(z3[: |
# doc subplot : http:// | # doc subplot : http:// | ||
Ligne 320: | Ligne 325: | ||
plt.savefig(' | plt.savefig(' | ||
plt.show() | plt.show() | ||
- | </sxh> | + | </code> |
Figure obtenue : | Figure obtenue : | ||
Ligne 333: | Ligne 338: | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
- | * [[http:// | + | * [[http:// |
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||