From 41a84d6996b3e5ac70183bbb12dfb3e741803bc0 Mon Sep 17 00:00:00 2001 From: zimmersandro Date: Wed, 22 Apr 2026 20:34:07 +0200 Subject: [PATCH] feat: task 7 Matrix Spur und Determinante mit numpy und sympy berechnen --- src/lineare_algebra/task7.py | 114 +++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/lineare_algebra/task7.py diff --git a/src/lineare_algebra/task7.py b/src/lineare_algebra/task7.py new file mode 100644 index 0000000..64608bc --- /dev/null +++ b/src/lineare_algebra/task7.py @@ -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 * "-")