import numpy as np
import numpy.linalg as LA #numpy linalg modul importieren
# oder mit
#from numpy import linalg as LA
Zuerst muss das "Lineare Algebra"-Modul von numpy importiert werden
A = np.array([[1, 2, 3], [0, 3, 7], [1, 1, 1]])
b = np.array([14, 28, 6])
# löse LGS Ax=b nach x
x = LA.solve(A, b)
x
Man kann auch die Inverse von A berechnen und "x" so berechnen (wird NICHT empfohlen)
Ainv = LA.inv(A)
x2 = Ainv@b
np.allclose(x, x2)
Wie man sieht, sind die Ergebnisse (fast) gleich.
A = np.array([[10, -7, 0], \
[-3, 2, 6], \
[5, -100, 5]])
A
# erste Zeile von A
A[0]
Zeilentausch:
$A[[i, j]] = A[[j, i]]$ vertauscht Zeile i mit Zeile j
A[[1, 2]] = A[[2, 1]]
A
x = y ist eine Kurzschreibweise für x = x y. Das gibt es auch für +, -, /
i = 3
i *= 2
i
# Addiere das k-Fache der 2. Zeile zur 3. Zeile
k = 2
A[2] += k*A[1]
A
# Achtung
A[2] += 1.2*A[1]
# Fehlerbehebung
A = A.astype(float)
A[2] += 1.2*A[1]
A
# oder von Anfang an als float
A = np.array([[10, -7, 0], \
[-3, 2, 6], \
[5, -100, 5]], dtype='float')
A