Minor code cleanup
This commit is contained in:
parent
3ec62d99f7
commit
3a4c0ed75d
@ -1,7 +1,6 @@
|
||||
import math
|
||||
|
||||
import numpy
|
||||
from numpy import linalg
|
||||
|
||||
|
||||
class Matrix:
|
||||
@ -76,8 +75,7 @@ class Matrix:
|
||||
self.__shape__ = (len(data), len(data[0]))
|
||||
self.__data__ = data
|
||||
else:
|
||||
raise ValueError(
|
||||
"Only following signatures are allowed: "
|
||||
raise ValueError("Only following signatures are allowed: "
|
||||
"(list), (numpy.ndarray), (list, tuple), (list, str, int), (list, str, int, int), (str, int)")
|
||||
|
||||
def get_data(self):
|
||||
@ -243,7 +241,7 @@ class Matrix:
|
||||
abs_sum = 0
|
||||
for i in range(rows):
|
||||
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)
|
||||
elif f == "col sum":
|
||||
row_sum = [0 for _ in range(cols)]
|
||||
|
@ -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("End 1c\n")
|
||||
|
||||
### 1d addition and substraction
|
||||
print("Start 1d addition and substraction")
|
||||
# intitialization
|
||||
### 1d addition and subtraction
|
||||
print("Start 1d addition and subtraction")
|
||||
# initialization
|
||||
a = Vector([1, 3, 5, 7, 9])
|
||||
b = Vector([-2, 5, 1, 0, -3])
|
||||
# computation
|
||||
@ -60,7 +60,7 @@ print("End 1d\n")
|
||||
|
||||
### 1e multiplication
|
||||
print("Start 1e multiplication")
|
||||
# intitialization
|
||||
# initialization
|
||||
a = Vector([1, 3, 5, 7, 9])
|
||||
b = Vector([-2, 5, 1, 0, -3])
|
||||
# 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("End 1e\n")
|
||||
|
||||
### 1f divison
|
||||
print("Start 1f divison")
|
||||
# intitialization
|
||||
### 1f division
|
||||
print("Start 1f division")
|
||||
# initialization
|
||||
a = Vector([1, 3, 5, 7, 9])
|
||||
b = Vector([-2, 5, 1, 7, -3])
|
||||
# computation with vectors
|
||||
@ -91,7 +91,7 @@ print("End 1f\n")
|
||||
|
||||
### 1g norm
|
||||
print("Start 1g norm")
|
||||
# intitialization
|
||||
# initialization
|
||||
a = Vector([1, 3, 5, 7, 9])
|
||||
a_norm = a.norm()
|
||||
a_normalized = a.normalize()
|
||||
@ -101,7 +101,7 @@ print("End 1g\n")
|
||||
|
||||
### 1h negation
|
||||
print("Start 1h negation")
|
||||
# intitialization
|
||||
# initialization
|
||||
a = Vector([1, 3, 5, 7, 9])
|
||||
x = -a
|
||||
print(f"-a = {str(x)} | must be {-np.array([1, 3, 5, 7, 9])}")
|
||||
@ -109,7 +109,7 @@ print("End 1h\n")
|
||||
|
||||
### 1i manipulation
|
||||
print("Start 1i manipulation")
|
||||
# intitialization
|
||||
# initialization
|
||||
a = Vector([1, 3, 5, 7, 9])
|
||||
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, )}")
|
||||
@ -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("End 2c\n")
|
||||
|
||||
### 1d addition and substraction
|
||||
print("Start 2d addition and substraction")
|
||||
### 1d addition and subtraction
|
||||
print("Start 2d addition and subtraction")
|
||||
# Initialization
|
||||
A = Matrix(structure="diagonal", data=[3], offset=0, n=10)
|
||||
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")
|
||||
print("End 2e\n")
|
||||
|
||||
### 1f divison
|
||||
print("Start 2f divison")
|
||||
### 1f division
|
||||
print("Start 2f division")
|
||||
# initialization
|
||||
A = Matrix(a_mat)
|
||||
# computation
|
||||
|
@ -110,7 +110,7 @@ class TestVector(TestCase):
|
||||
v = Vector([1, 2])
|
||||
s = 5
|
||||
|
||||
expected = Vector([1/5, 2/5])
|
||||
expected = Vector([1 / 5, 2 / 5])
|
||||
actual = v / s
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
@ -119,7 +119,7 @@ class TestVector(TestCase):
|
||||
v1 = Vector([1, 2])
|
||||
v2 = Vector([3, 4])
|
||||
|
||||
expected = Vector([1/3, 1/2])
|
||||
expected = Vector([1 / 3, 1 / 2])
|
||||
actual = v1 / v2
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
@ -274,4 +274,3 @@ class TestVector(TestCase):
|
||||
expected = Vector([10, 2, 30, 4, 50, 6, 70, 8, 9])
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user