NB : le module numpy n'est pas présent dans toutes les distributions de Python.
from numpy import array,eye,zeros,ones,random,diag,dot
A = array([[1, 2],[3, 5]])
print(A)
I = eye(2)
print(I)
print( 2*A + 3*I)
Remarque :
print(2*A + 3)
print(zeros((2,3)))
print(diag(array([1,2,3])))
print(ones((1,4)))
print(random.rand(5))
B = array([[0,5],[2,-1]])
print(A , " = A")
print(B , " = B")
print(A+B, " = A+B")
print(A , " = A")
print(B , " = B")
print(dot(A,B), " = AxB")
Comparer avec :
print(A*B)
from numpy import linalg
linalg.inv(A)
vérification :
dot(A,linalg.inv(A))
linalg.matrix_power(A,3)
Le système $$\left\lbrace \begin{array}{l} 2x+y=7\\3x+4y=8 \end{array} \right. $$ s'écrit sous forme matricielle : $$AX=C$$ avec $A=\begin{pmatrix}2&1\\3&4 \end{pmatrix}$, $X=\begin{pmatrix} x\\y \end{pmatrix}$ et $C=\begin{pmatrix} 7\\8 \end{pmatrix}$.
En multipliant (à gauche !) les deux membres de $AX=C$ par $A^{-1}$, on obtient $$A^{-1} \times AX=A^{-1} \times C$$ soit $$I_2 X=A^{-1} \times C$$ $$X=A^{-1} \times C$$
Dans notre exemple, $$X=\begin{pmatrix}0,8&-0,2\\-0,6&0,4 \end{pmatrix} \times \begin{pmatrix} 7\\8 \end{pmatrix} = \begin{pmatrix} 4\\-1 \end{pmatrix}$$
A = array([[2,1],[3,4]])
C = array([[7],[8]])
dot(linalg.inv(A) , C)
Deux systèmes très semblables :
$$\left\lbrace \begin{array}{l} x-y=1\\x-1.00001*y=0 \end{array} \right. $$ et $$\left\lbrace \begin{array}{l} x-y=1\\x-0.99999*y=0 \end{array} \right. $$
Mais des solutions très différentes.
Solution du premier :
A1 = array([[1,-1],[1,-1.00001]])
C1 = array([[1],[0]])
dot(linalg.inv(A1) , C1)
solution du second :
A2 = array([[1,-1],[1,-0.99999]])
dot(linalg.inv(A2) , C1)