Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
teaching:progappchim:matrices [2015/03/02 16:28] – créée villersd | teaching:progappchim:matrices [2018/03/06 14:49] (Version actuelle) – villersd | ||
---|---|---|---|
Ligne 5: | Ligne 5: | ||
===== Produit matriciel à l'aide de listes ===== | ===== Produit matriciel à l'aide de listes ===== | ||
- | Le [[http:// | + | Le [[wp>fr: |
Exemple de programme Python : | Exemple de programme Python : | ||
- | <sxh python; title : matrices_multiplication-list.py> | + | <code python matrices_multiplication-list.py> |
#! / | #! / | ||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||
Ligne 19: | Ligne 19: | ||
[2,-5], | [2,-5], | ||
[11,0], | [11,0], | ||
- | [6,-7]] | + | [6,-7], |
- | print mat1 # pour visualiser la matrice mat1 de 4 lignes de 2 colonnes | + | |
+ | print(mat1) # pour visualiser la matrice mat1 de 4 lignes de 2 colonnes | ||
mat2=[[1, | mat2=[[1, | ||
- | [2,4,6]] | + | [2,4,6], |
- | print mat2 # pour visualiser la matrice mat2 de 2 lignes de 3 colonnes | + | |
+ | print(mat2) # pour visualiser la matrice mat2 de 2 lignes de 3 colonnes | ||
- | if len(mat1[0]) == len(mat2): | + | if len(mat1[0]) == len(mat2): |
- | | + | mprod=[] |
- | mprod=[] | + | |
for i in range(len(mat1)): | for i in range(len(mat1)): | ||
- | li = [] # initialisation d'une ligne | + | li = [] |
for j in range(len(mat2[0])): | for j in range(len(mat2[0])): | ||
- | elem=0 | + | elem = 0 # initialisation d'un élément au sein de la ligne |
for k in range(len(mat1[0])): | for k in range(len(mat1[0])): | ||
elem = elem + mat1[i][k] * mat2[k][j] | elem = elem + mat1[i][k] * mat2[k][j] | ||
Ligne 37: | Ligne 38: | ||
mprod.append(li) | mprod.append(li) | ||
- | print mprod # pour visualiser la matrice produit | + | print(mprod) |
- | </sxh> | + | </code> |
+ | |||
+ | ===== Utiliser NumPy ===== | ||
+ | La librairie [[[[numpy_simple|Numpy]] permet d' | ||
+ | <code python matrices_multiplication-numpy.py> | ||
+ | #! / | ||
+ | # -*- coding: utf-8 -*- | ||
+ | """ | ||
+ | Calcul matriciel : multiplication | ||
+ | Utilisation de numpy (array,...) pour représenter les matrices et effectuer | ||
+ | les calculs d' | ||
+ | """ | ||
+ | import numpy as np | ||
+ | |||
+ | mat1 = np.array([[7, | ||
+ | print(mat1) | ||
+ | mat2=np.array([[1, | ||
+ | print(mat2) | ||
+ | |||
+ | print(np.dot(mat1, | ||
+ | </ |