1
0

Add class matrix.py (wip)

This commit is contained in:
2023-12-12 16:34:54 +01:00
parent fadde30a06
commit 4aacfe8473
5 changed files with 200 additions and 22 deletions

View File

@@ -1,4 +1,7 @@
class Vector:
from matrix import Matrix
class Vector(Matrix):
__data__ = []
def __init__(self, data):
@@ -24,21 +27,33 @@ class Vector:
def __str__(self):
return f"{self.__data__}"
def __neg__(self):
return Vector([-x for x in self.__data__])
def __add__(self, other):
if self.get_dimension() != other.get_dimension():
raise ValueError("The vectors to be added must have the same dimension")
if isinstance(other, Vector):
if self.get_dimension() != other.get_dimension():
raise ValueError("The vectors to be added must have the same dimension")
return Vector([(x + y) for (x, y) in zip(self, other)])
elif isinstance(other, int) or isinstance(other, float):
return Vector([(x + other) for x in self.__data__])
else:
raise ValueError("A vector can only be multiplied with an vector (dot product) or a scalar")
data = []
for (i, j) in zip(self, other):
data.append(i + j)
def __radd__(self, other):
return self + other
return Vector(data)
def __sub__(self, other):
return self + (-other)
def __rsub__(self, other):
return -self + other
def __mul__(self, other):
if isinstance(other, Vector):
...
return sum([(x * y) for (x, y) in zip(self, other)])
elif isinstance(other, int) or isinstance(other, float):
...
return Vector([(other * x) for x in self.__data__])
else:
raise ValueError("A vector can only be multiplied with an vector (dot product) or a scalar")