1
0

Finish vector.py;

Adjust matrix.py for use of vector.py as subclass;
Adjust vector.py and matrix.py to match test_serial.py
This commit is contained in:
2024-02-22 02:21:05 +01:00
parent 91f4191a65
commit 3ec62d99f7
4 changed files with 249 additions and 39 deletions

View File

@@ -92,20 +92,21 @@ class Matrix:
"""
return self.__shape__
def __transpose_internal__(self):
rows = self.__shape__[0]
cols = self.__shape__[1]
transposed_data = [[0 for _ in range(rows)] for _ in range(cols)]
for i in range(rows):
for j in range(cols):
transposed_data[j][i] = self.__data__[i][j]
return transposed_data, (cols, rows)
def transpose(self):
"""
:return: the transpose of the matrix
"""
rows = self.__shape__[0]
cols = self.__shape__[1]
transposed_data = [[0 for _ in range(rows)] for _ in range(cols)]
for i in range(rows):
for j in range(cols):
transposed_data[j][i] = self.__data__[i][j]
return Matrix(transposed_data, (cols, rows))
transposed_data, shape = self.__transpose_internal__()
return Matrix(transposed_data, shape)
def T(self):
"""
@@ -123,9 +124,9 @@ class Matrix:
:return: True if data in the matrix are equal to the given data in other for each component, otherwise False
"""
if isinstance(other, Matrix):
data_to_compare = other.__data__
if self.__shape__ != other.__shape__:
return False
data_to_compare = other.__data__
elif isinstance(other, list):
data_to_compare = other
if self.__shape__[0] != len(other) or self.__shape__[1] != len(other[0]):
@@ -144,10 +145,13 @@ class Matrix:
def __str__(self):
return str(numpy.array(self.__data__))
def __neg__(self):
def __neg_internal__(self):
rows = range(self.__shape__[0])
cols = range(self.__shape__[1])
return Matrix([[-(self.__data__[i][j]) for j in cols] for i in rows], self.__shape__)
return [[-(self.__data__[i][j]) for j in cols] for i in rows]
def __neg__(self):
return Matrix(self.__neg_internal__(), self.__shape__)
def __add_matrix_internal__(self, other):
rows = self.__shape__[0]
@@ -201,8 +205,8 @@ class Matrix:
return new_data
def __mul_scalar_internal__(self, other):
cols = range(self.__shape__[1])
rows = range(self.__shape__[0])
cols = range(self.__shape__[1])
return [[(self.__data__[i][j] * other) for j in cols] for i in rows]
def __mul__(self, other):