1
0

Finalize matrix_mpi.py; Add more vector_mpi.py

This commit is contained in:
2024-04-16 23:21:56 +02:00
parent 987de6acf7
commit 6848bc7b26
8 changed files with 291 additions and 502 deletions

View File

@@ -86,7 +86,7 @@ class Vector(Matrix):
return Vector(self.__mul_vector_same_shape_internal__(other))
elif self.__shape__ == tuple(reversed(other.__shape__)):
if self.__shape__[0] == 1: # Case (_ ... _) * (_\n...\n_) = scalar
return super().__mul_matrix_internal__(other)[0][0]
return super().__mul_matrix_internal__(other)[0]
else: # Case (_\n...\n_) * (_ ... _) = Matrix
new_data, shape = self.__mul_tensor_internal__(other)
return Matrix(new_data, shape)
@@ -95,10 +95,19 @@ class Vector(Matrix):
elif isinstance(other, int) or isinstance(other, float):
return Vector(super().__mul_scalar_internal__(other))
else:
raise ValueError("A ``Vector`` can only be multiplied with an ``Vector`` (dot product or tensor),"
raise ValueError("A ``Vector`` can only be multiplied with an ``Vector`` (dot product or tensor), "
"a compatible ``Matrix`` or a scalar")
def __mul_matrix_vector_internal__(self, other):
rows = other.__shape__[0]
new_data = [0] * rows
for i in range(rows):
new_data[i] = sum([other.__data__[i][j] * self.__data__[j][0] for j in range(self.__shape__[0])])
return new_data
def __rmul__(self, other):
if isinstance(other, Matrix):
return Vector(self.__mul_matrix_vector_internal__(other))
return self * other
def __truediv_vector_internal__(self, other):