1
0

Minor code cleanup

This commit is contained in:
Niklas Birk 2024-02-22 02:28:26 +01:00
parent 3ec62d99f7
commit 3a4c0ed75d
3 changed files with 19 additions and 22 deletions

View File

@ -1,7 +1,6 @@
import math import math
import numpy import numpy
from numpy import linalg
class Matrix: class Matrix:
@ -76,8 +75,7 @@ class Matrix:
self.__shape__ = (len(data), len(data[0])) self.__shape__ = (len(data), len(data[0]))
self.__data__ = data self.__data__ = data
else: else:
raise ValueError( raise ValueError("Only following signatures are allowed: "
"Only following signatures are allowed: "
"(list), (numpy.ndarray), (list, tuple), (list, str, int), (list, str, int, int), (str, int)") "(list), (numpy.ndarray), (list, tuple), (list, str, int), (list, str, int, int), (str, int)")
def get_data(self): def get_data(self):
@ -243,7 +241,7 @@ class Matrix:
abs_sum = 0 abs_sum = 0
for i in range(rows): for i in range(rows):
for j in range(cols): for j in range(cols):
abs_sum += abs(self.__data__[i][j])**2 abs_sum += abs(self.__data__[i][j]) ** 2
norm = math.sqrt(abs_sum) norm = math.sqrt(abs_sum)
elif f == "col sum": elif f == "col sum":
row_sum = [0 for _ in range(cols)] row_sum = [0 for _ in range(cols)]

View File

@ -34,9 +34,9 @@ print(f"vec_x2.T() has shape {vec_x2.T().shape()} | must be (1,10000)")
print(f"vec_x2.T().T() has shape {vec_x2.T().T().shape()} | must be (10000,1)") print(f"vec_x2.T().T() has shape {vec_x2.T().T().shape()} | must be (10000,1)")
print("End 1c\n") print("End 1c\n")
### 1d addition and substraction ### 1d addition and subtraction
print("Start 1d addition and substraction") print("Start 1d addition and subtraction")
# intitialization # initialization
a = Vector([1, 3, 5, 7, 9]) a = Vector([1, 3, 5, 7, 9])
b = Vector([-2, 5, 1, 0, -3]) b = Vector([-2, 5, 1, 0, -3])
# computation # computation
@ -60,7 +60,7 @@ print("End 1d\n")
### 1e multiplication ### 1e multiplication
print("Start 1e multiplication") print("Start 1e multiplication")
# intitialization # initialization
a = Vector([1, 3, 5, 7, 9]) a = Vector([1, 3, 5, 7, 9])
b = Vector([-2, 5, 1, 0, -3]) b = Vector([-2, 5, 1, 0, -3])
# computation with vectors # computation with vectors
@ -77,9 +77,9 @@ y = 0.1 * b.T()
print(f"0.1 * b.T()= {str(y)} | must be {0.1 * np.array([-2, 5, 1, 0, -3])}") print(f"0.1 * b.T()= {str(y)} | must be {0.1 * np.array([-2, 5, 1, 0, -3])}")
print("End 1e\n") print("End 1e\n")
### 1f divison ### 1f division
print("Start 1f divison") print("Start 1f division")
# intitialization # initialization
a = Vector([1, 3, 5, 7, 9]) a = Vector([1, 3, 5, 7, 9])
b = Vector([-2, 5, 1, 7, -3]) b = Vector([-2, 5, 1, 7, -3])
# computation with vectors # computation with vectors
@ -91,7 +91,7 @@ print("End 1f\n")
### 1g norm ### 1g norm
print("Start 1g norm") print("Start 1g norm")
# intitialization # initialization
a = Vector([1, 3, 5, 7, 9]) a = Vector([1, 3, 5, 7, 9])
a_norm = a.norm() a_norm = a.norm()
a_normalized = a.normalize() a_normalized = a.normalize()
@ -101,7 +101,7 @@ print("End 1g\n")
### 1h negation ### 1h negation
print("Start 1h negation") print("Start 1h negation")
# intitialization # initialization
a = Vector([1, 3, 5, 7, 9]) a = Vector([1, 3, 5, 7, 9])
x = -a x = -a
print(f"-a = {str(x)} | must be {-np.array([1, 3, 5, 7, 9])}") print(f"-a = {str(x)} | must be {-np.array([1, 3, 5, 7, 9])}")
@ -109,7 +109,7 @@ print("End 1h\n")
### 1i manipulation ### 1i manipulation
print("Start 1i manipulation") print("Start 1i manipulation")
# intitialization # initialization
a = Vector([1, 3, 5, 7, 9]) a = Vector([1, 3, 5, 7, 9])
print( print(
f"a[{str([1, 2, 4])}] = {str(np.array(a[1, 2, 4]).reshape(3, ))} | must be {np.array([1, 3, 5, 7, 9]).reshape(5, 1)[np.array([1, 2, 4])].reshape(3, )}") f"a[{str([1, 2, 4])}] = {str(np.array(a[1, 2, 4]).reshape(3, ))} | must be {np.array([1, 3, 5, 7, 9]).reshape(5, 1)[np.array([1, 2, 4])].reshape(3, )}")
@ -148,8 +148,8 @@ print(f"A.T() has shape {A.T().shape()} | must be (3,4)")
print(f"A.T().T() has shape {A.T().T().shape()} | must be (4,3)") print(f"A.T().T() has shape {A.T().T().shape()} | must be (4,3)")
print("End 2c\n") print("End 2c\n")
### 1d addition and substraction ### 1d addition and subtraction
print("Start 2d addition and substraction") print("Start 2d addition and subtraction")
# Initialization # Initialization
A = Matrix(structure="diagonal", data=[3], offset=0, n=10) A = Matrix(structure="diagonal", data=[3], offset=0, n=10)
print(str(A)) print(str(A))
@ -186,8 +186,8 @@ print(
f"Norm of (B.T()*A.T() - np.(B.T()*A.T())) is {(B.T() * A.T() - Matrix(np.array(b_mat).T @ np.array(a_mat).T)).norm()} | must be < 1e-8") f"Norm of (B.T()*A.T() - np.(B.T()*A.T())) is {(B.T() * A.T() - Matrix(np.array(b_mat).T @ np.array(a_mat).T)).norm()} | must be < 1e-8")
print("End 2e\n") print("End 2e\n")
### 1f divison ### 1f division
print("Start 2f divison") print("Start 2f division")
# initialization # initialization
A = Matrix(a_mat) A = Matrix(a_mat)
# computation # computation

View File

@ -110,7 +110,7 @@ class TestVector(TestCase):
v = Vector([1, 2]) v = Vector([1, 2])
s = 5 s = 5
expected = Vector([1/5, 2/5]) expected = Vector([1 / 5, 2 / 5])
actual = v / s actual = v / s
self.assertEqual(expected, actual) self.assertEqual(expected, actual)
@ -119,7 +119,7 @@ class TestVector(TestCase):
v1 = Vector([1, 2]) v1 = Vector([1, 2])
v2 = Vector([3, 4]) v2 = Vector([3, 4])
expected = Vector([1/3, 1/2]) expected = Vector([1 / 3, 1 / 2])
actual = v1 / v2 actual = v1 / v2
self.assertEqual(expected, actual) self.assertEqual(expected, actual)
@ -274,4 +274,3 @@ class TestVector(TestCase):
expected = Vector([10, 2, 30, 4, 50, 6, 70, 8, 9]) expected = Vector([10, 2, 30, 4, 50, 6, 70, 8, 9])
self.assertEqual(expected, actual) self.assertEqual(expected, actual)