Vektorübungen, Serie 1 Übungen 26 und 27
This commit is contained in:
parent
987e01c6c1
commit
d190458950
17
vektoren/serie_1_26.py
Normal file
17
vektoren/serie_1_26.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# %%
|
||||||
|
# Determinante mit Python berechnen
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
a = [1, 1, 1, 0] # a Vektor
|
||||||
|
b = [1, 0, -1, 1] # b Vektor
|
||||||
|
c = [2, 1, 1, 0] # c Vektor
|
||||||
|
d = [0, 0, -1, 1] # d Vektor
|
||||||
|
e = [-3, -2, -3, -1] # e Vektor
|
||||||
|
|
||||||
|
abcd = [a, b, c, d]
|
||||||
|
abde = [a, b, d, e]
|
||||||
|
abce = [a, b, c, e]
|
||||||
|
|
||||||
|
print(f"Determinante abcd: {np.linalg.det(abcd)}") # Determinante abcd
|
||||||
|
print(f"Determinante abde: {np.linalg.det(abde)}") # Determinante abde
|
||||||
|
print(f"Determinante abce: {np.linalg.det(abce)}") # Determinante abce
|
||||||
66
vektoren/serie_1_27.py
Normal file
66
vektoren/serie_1_27.py
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
# %%
|
||||||
|
# Ausdrücke berechnen
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
# Vector Klasse um fehler abzufangen wie Vektor + Skalar. Numpy führt das aus, ist aber mathematisch nicht definiert.
|
||||||
|
class Vector:
|
||||||
|
def __init__(self, values):
|
||||||
|
self._values = np.asarray(values)
|
||||||
|
|
||||||
|
def __add__(self, other):
|
||||||
|
if not isinstance(other, Vector):
|
||||||
|
raise TypeError("Can only add vectors")
|
||||||
|
if self._values.shape != other._values.shape:
|
||||||
|
raise ValueError("Vectors must have the same dimension")
|
||||||
|
return Vector(self._values + other._values)
|
||||||
|
|
||||||
|
def __sub__(self, other):
|
||||||
|
if not isinstance(other, Vector):
|
||||||
|
raise TypeError("Can only subtract vectors")
|
||||||
|
return Vector(self._values - other._values)
|
||||||
|
|
||||||
|
def __mul__(self, other):
|
||||||
|
if isinstance(other, Vector):
|
||||||
|
# Vector * Vector = dot product
|
||||||
|
return np.dot(self._values, other._values)
|
||||||
|
|
||||||
|
if np.isscalar(other):
|
||||||
|
# Vector * scalar = scaled vector
|
||||||
|
return Vector(self._values * other)
|
||||||
|
|
||||||
|
return NotImplemented
|
||||||
|
|
||||||
|
def __rmul__(self, scalar):
|
||||||
|
return self * scalar
|
||||||
|
|
||||||
|
def cross(self, other):
|
||||||
|
if not isinstance(other, Vector):
|
||||||
|
raise TypeError("Cross product requires two vectors")
|
||||||
|
return Vector(np.cross(self._values, other._values))
|
||||||
|
|
||||||
|
def __abs__(self):
|
||||||
|
return np.linalg.norm(self._values)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"Vector({self._values})"
|
||||||
|
|
||||||
|
|
||||||
|
a = Vector([3.8, 2.12, -4.01])
|
||||||
|
b = Vector([0.01, -0.02, 2.341])
|
||||||
|
c = Vector([-5.1, 1.0, 4.5])
|
||||||
|
d = Vector([1.0, 0.32])
|
||||||
|
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
lambda: 4 * a + 2.1 * (b * c),
|
||||||
|
lambda: -8.35 * (a.cross(c) * b - abs(d) ** 2),
|
||||||
|
lambda: (3.1 * d).cross(a) - (a * 2 * c) * b,
|
||||||
|
lambda: a * (1 / abs(d)) - (2 * b - 9.33 * c),
|
||||||
|
]
|
||||||
|
|
||||||
|
for operation in operations:
|
||||||
|
try:
|
||||||
|
print(operation())
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error: {e}")
|
||||||
Loading…
x
Reference in New Issue
Block a user