feat: task 7 Matrix Spur und Determinante mit numpy und sympy berechnen

This commit is contained in:
Sandro Zimmermann 2026-04-22 20:34:07 +02:00
parent 156ccd783b
commit 41a84d6996

View File

@ -0,0 +1,114 @@
# %%
# Import
import numpy as np
# Parameter
matrizen = [
np.array([[2, 3], [4, 5]]),
np.array([[2, 3], [4, 6]]),
np.array([[-1, 3, 0], [0, 2, 0], [1, 2, -1]]),
np.array([[-2, 4, 8], [1, -2, -4], [6, -3, 12]]),
np.array([[1, 0, 3, 0], [4, -2, 12, 6], [1, 2, 3, -4], [3, 3, 15, -2]]),
np.array(
[
[1, np.sqrt(3), 8, -np.sqrt(2)],
[-13, 3, np.sqrt(2), 0],
[np.sqrt(17), -1, 0, 0],
[2, 0, 0, 0],
]
),
np.array(
[
[2, -3, 5, 1, 4],
[2, -3, 1, -6, 18],
[4, -3, 9, 6, 10],
[-2, 4, -6, -1, -1],
[-6, 11, -23, -14, 9],
]
),
np.array(
[
[1, 0, 0, 1, 0],
[1, 0, 0, -1, 0],
[0, 1, 1, 0, 0],
[0, -1, 1, 0, 0],
[0, 0, 0, 0, np.sqrt(2)],
]
),
]
# Layout
print(60 * "-")
print("Aufgabe 3. Spur und Determinante berechnen (numpy)")
print(__file__)
print(60 * "-")
for matrix in matrizen:
# Berechnung
tr = np.trace(matrix)
det = np.linalg.det(matrix)
# Ausgabe
print(f"\n{matrix}\n")
print(f"Spur: {tr:.3f}")
print(f"Determinante: {det:.3f}")
print(60 * "-")
# %%
# Import
import sympy as sp
from IPython.display import display, Math
# Parameter
matrizen = [
sp.Matrix([[2, 3], [4, 5]]),
sp.Matrix([[2, 3], [4, 6]]),
sp.Matrix([[-1, 3, 0], [0, 2, 0], [1, 2, -1]]),
sp.Matrix([[-2, 4, 8], [1, -2, -4], [6, -3, 12]]),
sp.Matrix([[1, 0, 3, 0], [4, -2, 12, 6], [1, 2, 3, -4], [3, 3, 15, -2]]),
sp.Matrix(
[
[1, sp.sqrt(3), 8, -sp.sqrt(2)],
[-13, 3, sp.sqrt(2), 0],
[sp.sqrt(17), -1, 0, 0],
[2, 0, 0, 0],
]
),
sp.Matrix(
[
[2, -3, 5, 1, 4],
[2, -3, 1, -6, 18],
[4, -3, 9, 6, 10],
[-2, 4, -6, -1, -1],
[-6, 11, -23, -14, 9],
]
),
sp.Matrix(
[
[1, 0, 0, 1, 0],
[1, 0, 0, -1, 0],
[0, 1, 1, 0, 0],
[0, -1, 1, 0, 0],
[0, 0, 0, 0, sp.sqrt(2)],
]
),
]
# Layout
print(60 * "-")
print("Aufgabe 3. Spur und Determinante berechnen (sympy)")
print(__file__)
print(60 * "-")
for matrix in matrizen:
# Berechnung
tr = sp.trace(matrix)
det = sp.det(matrix)
# Ausgabe
display(Math(rf"{sp.latex(matrix)}"))
display(Math(rf"\text{{Spur: }}{sp.latex(tr)}"))
display(Math(rf"\text{{Determinante: }}{sp.latex(det)}"))
print(60 * "-")