Skip to article frontmatterSkip to article content
import numpy as np

contenu de ce notebook (notebook à sauter)

IMPORTANT
ce notebook est utile aux enseignements de mathématiques
sachez qu’il existe pour vous y référer
sans l’étudier en cours d’informatique

liste des fonctions présentées (succinctement)

fonctionscomportement
np.dotproduit de matrice
np.linalg.normnormes de matrice
np.transposetransposition de matrice
np.linalg.detdéterminant
np.linalg.invinversion
np.linalg.eigvaleurs propres
np.linalg.solverésolution de système linéaire
np.eyematrice identité
np.diagmatrice diagonale

introduction et contexte


terminologie

A = np.array([[2, 3, -7], [6, -4, 5]])
print(A.dtype)
A[0] = np.pi
A
int64
array([[ 3, 3, 3], [ 6, -4, 5]])

les matrices et vecteurs

# le code
m, n = 4, 3
A = np.arange(12).reshape(m, n)
print(A)
print(A.ndim)
print(A.shape)
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
2
(4, 3)
# le code
V = np.array([1, -3, 8])
print(V)
print(V.ndim)
print(V.shape)
[ 1 -3  8]
1
(3,)

le produit d’une matrice et d’un vecteur numpy.dot

m, n = 4, 3
A = np.arange(12).reshape(m, n)
print(A)
V = np.arange(3).reshape(n)
print(V)
print(np.dot(A, V))
print(A.dot(V))
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
[0 1 2]
[ 5 14 23 32]
[ 5 14 23 32]

le produit de deux matrices numpy.dot

# le code
m, n = 3, 4
A = np.arange(12).reshape(m, n)
print(A)
B = np.arange(10, 22).reshape(n, m)
print(B)
np.dot(A, B)
A.dot(B)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[[10 11 12]
 [13 14 15]
 [16 17 18]
 [19 20 21]]
array([[102, 108, 114], [334, 356, 378], [566, 604, 642]])

produit matriciel avec @ et numpy.matmul

# le code
np.matmul(A, B)
A @ B
array([[102, 108, 114], [334, 356, 378], [566, 604, 642]])

le produit scalaire

#le code
V1 = np.array([1, -3, 8])
V1.shape # (3,)
V1
array([ 1, -3, 8])
#le code
V2 = np.array([-6, 4, -7])
V2.shape # (3,)
V2
array([-6, 4, -7])
#le code
np.dot(V1, V2)
np.int64(-74)

la norme de vecteur

# le code
V = np.array([1, -3, 8])
np.linalg.norm(V)
np.sqrt(np.dot(V, V))
np.sqrt(np.sum(V*V))
np.float64(8.602325267042627)

la transposée d’une matrice

# le code
A = np.arange(1, 13).reshape(4, 3)
print(A)
print(np.transpose(A))
print(A.T)
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
[[ 1  4  7 10]
 [ 2  5  8 11]
 [ 3  6  9 12]]
[[ 1  4  7 10]
 [ 2  5  8 11]
 [ 3  6  9 12]]

les matrices identité

# le code
I = np.eye(5)
print(I.shape)
print(I.dtype)
(5, 5)
float64

le déterminant d’une matrice

# le code
A = 2*np.eye(3)
np.linalg.det(A)
np.float64(7.999999999999998)
#le code
B = np.array([[2, 3, 4], [5, 6, 7]])
try:
    np.linalg.det(B)
except np.linalg.LinAlgError as e:
    print(e)
Last 2 dimensions of the array must be square

les matrices diagonales

# le code
A = np.random.randint(-100, 100, size=(3, 4))
print(A)
np.diag(A)
[[ 90  37  61  56]
 [ 82 -73  35  23]
 [ 75  51  25   3]]
array([ 90, -73, 25])
# le code
l = np.array([9, -45, 6])
np.diag(l)
array([[ 9, 0, 0], [ 0, -45, 0], [ 0, 0, 6]])

la trace d’une matrice

# le code
A = np.arange(9).reshape(3, 3)
print(A)
print(np.trace(A))
print(np.sum(np.diag(A)))
[[0 1 2]
 [3 4 5]
 [6 7 8]]
12
12

l’inversion d’une matrice

# le code
R = np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]], dtype=float)
print(R)
IR = np.linalg.inv(R)
print(IR)
print(np.all(np.dot(R, IR) == np.eye(3)))
[[0. 1. 0.]
 [0. 0. 1.]
 [1. 0. 0.]]
[[0. 0. 1.]
 [1. 0. 0.]
 [0. 1. 0.]]
True

calculs approchés (avec tolérance)

A = np.random.random(size=(3, 3))
I = np.eye(len(A))
A_1 = np.linalg.inv(A)
np.all(I == np.dot(A_1, A))
print(np.dot(A_1, A))
np.all(np.isclose(np.dot(A_1, A), I))
[[ 1.00000000e+00  3.78036062e-16  9.65892074e-16]
 [-3.43292892e-15  1.00000000e+00 -3.08064778e-15]
 [ 2.00663367e-16  1.09428647e-16  1.00000000e+00]]
np.True_

les valeurs propres d’une matrice (eigen values)

# le code
M = np.random.random(size=(3, 3))
alpha, v  = np.linalg.eig(M)
alpha.shape, v.shape


v0 = v[:, 0] # le premier vecteur propre
alpha0 = alpha[0] # la première valeur propre

np.all(np.isclose(np.dot(M, v0),  alpha0*v0))
np.True_
# votre code ici

résolution d’un système linéaire

# le code
A = np.random.random(size=(3, 3))
b = np.array([1, 2, 3])
x = np.linalg.solve(A, b)
np.all(np.isclose(np.dot(A, x), b))
np.True_