feat: task3 Matrix Berechnungen
This commit is contained in:
parent
d6b1626761
commit
fcfabfe170
85
src/lineare_algebra/task3.py
Normal file
85
src/lineare_algebra/task3.py
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
#%%
|
||||||
|
import numpy as np
|
||||||
|
import sympy as sp
|
||||||
|
from IPython.display import display, Math
|
||||||
|
|
||||||
|
#%%
|
||||||
|
# Parameter
|
||||||
|
A = np.array([[1,3,-1],[4,-2,8]])
|
||||||
|
B = np.array([[-3,9,3],[-6,6,3]])
|
||||||
|
|
||||||
|
# Layout
|
||||||
|
print(60 * "-")
|
||||||
|
print("Aufgabe 3. Matrizen berechnen (Numpy)")
|
||||||
|
print(__file__)
|
||||||
|
print(60 * "-")
|
||||||
|
|
||||||
|
#Berechnung
|
||||||
|
C=A+B
|
||||||
|
D=-2*A
|
||||||
|
E=(1/3)*B
|
||||||
|
F=2*B-A
|
||||||
|
|
||||||
|
#Ausgabe
|
||||||
|
print(f"C = {C}")
|
||||||
|
print(f"D = {D}")
|
||||||
|
print(f"E = {E}")
|
||||||
|
print(f"F = {F}")
|
||||||
|
print(60 * "-")
|
||||||
|
|
||||||
|
#%%
|
||||||
|
# Parameters
|
||||||
|
A = sp.Matrix([[1, 3, -1], [4, -2, 8]])
|
||||||
|
B = sp.Matrix([[-3, 9, 3], [-6, 6, 3]])
|
||||||
|
|
||||||
|
# Layout
|
||||||
|
print("-" * 60)
|
||||||
|
print("Aufgabe 3. Matrizen berechnen (Sympy)")
|
||||||
|
print(__file__)
|
||||||
|
print("-" * 60)
|
||||||
|
|
||||||
|
# Berechnung
|
||||||
|
C = A + B
|
||||||
|
D = -2 * A
|
||||||
|
E = sp.Rational(1, 3) * B # ← keeps fractions exact, e.g. 1 not 0.333...
|
||||||
|
F = 2 * B - A
|
||||||
|
|
||||||
|
# Ausgabe – symbolic, paper-style
|
||||||
|
for name, matrix in [("C", C), ("D", D), ("E", E), ("F", F)]:
|
||||||
|
display(Math(rf"{name} = {sp.latex(matrix)}"))
|
||||||
|
|
||||||
|
print("-" * 60)
|
||||||
|
|
||||||
|
# %%
|
||||||
|
# Parameters
|
||||||
|
A = sp.Matrix([[4,-3,2],[6,2,5],[-1,-2,3]])
|
||||||
|
B = sp.Matrix([[3,4],[1,2],[5,6]])
|
||||||
|
u = sp.Matrix([0,2,-4])
|
||||||
|
v = sp.Matrix([1,3,-3])
|
||||||
|
|
||||||
|
# Layout
|
||||||
|
print("-" * 60)
|
||||||
|
print("Aufgabe 6. Produkte mit Matrizen")
|
||||||
|
print(__file__)
|
||||||
|
print("-" * 60)
|
||||||
|
|
||||||
|
# Berechnung
|
||||||
|
berechnungen = [
|
||||||
|
("a", lambda: A * B),
|
||||||
|
("b", lambda: B * A),
|
||||||
|
("c", lambda: A * u),
|
||||||
|
("d", lambda: A ** 2),
|
||||||
|
("e", lambda: B * B),
|
||||||
|
("f", lambda: v.T * u),
|
||||||
|
("g", lambda: v * u),
|
||||||
|
("h", lambda:u * v.T),
|
||||||
|
("i", lambda: B.T * v),
|
||||||
|
("j", lambda: v.T * B)
|
||||||
|
]
|
||||||
|
|
||||||
|
for name, berechnung in berechnungen:
|
||||||
|
try:
|
||||||
|
result = berechnung()
|
||||||
|
display(Math(rf"{name} = {sp.latex(result)}"))
|
||||||
|
except Exception as e:
|
||||||
|
display(Math(rf"{name} = \text{{Fehler: }} \text{{{e}}}"))
|
||||||
Loading…
x
Reference in New Issue
Block a user