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:
@ -58,7 +58,7 @@ else:
|
||||
print("It is required to raise a system error, e. g., ValueError, since dimensions mismatch!")
|
||||
print("End 1d\n")
|
||||
|
||||
### 1e multiplication
|
||||
### 1e multiplication
|
||||
print("Start 1e multiplication")
|
||||
# intitialization
|
||||
a = Vector([1, 3, 5, 7, 9])
|
||||
@ -77,7 +77,7 @@ 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
|
||||
### 1f divison
|
||||
print("Start 1f divison")
|
||||
# intitialization
|
||||
a = Vector([1, 3, 5, 7, 9])
|
||||
@ -93,7 +93,8 @@ print("End 1f\n")
|
||||
print("Start 1g norm")
|
||||
# intitialization
|
||||
a = Vector([1, 3, 5, 7, 9])
|
||||
a_norm, a_normalized = a.normalize()
|
||||
a_norm = a.norm()
|
||||
a_normalized = a.normalize()
|
||||
print(f"a_norm = {a_norm} | must be {np.linalg.norm([1, 3, 5, 7, 9])}")
|
||||
print(f"a_normalize = {str(a_normalized)} | must be {np.array([1, 3, 5, 7, 9]) / np.linalg.norm([1, 3, 5, 7, 9])}")
|
||||
print("End 1g\n")
|
||||
@ -111,7 +112,7 @@ print("Start 1i manipulation")
|
||||
# intitialization
|
||||
a = Vector([1, 3, 5, 7, 9])
|
||||
print(
|
||||
f"a[{str([1, 2, 4])}] = {str(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, )}")
|
||||
a[1, 2, 4] = [-1, -1, -1]
|
||||
print(f"a = {str(a)} | must be {np.array([1, -1, -1, 5, 7, -1])}")
|
||||
print("End 1i\n")
|
||||
|
@ -4,12 +4,6 @@ from vector import Vector
|
||||
|
||||
|
||||
class TestVector(TestCase):
|
||||
def test_should_create_vector_dim_5(self):
|
||||
actual = Vector(5).get_dimension()
|
||||
expected = 5
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_create_zero_vector(self):
|
||||
actual = Vector(5)
|
||||
expected = Vector([0, 0, 0, 0, 0])
|
||||
@ -22,7 +16,7 @@ class TestVector(TestCase):
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_transpose_vector(self):
|
||||
def test_should_transpose_col_vector(self):
|
||||
data = [1, 2, 3, 4, 5, 6]
|
||||
actual = Vector(data)
|
||||
|
||||
@ -30,6 +24,14 @@ class TestVector(TestCase):
|
||||
expected = [[1, 2, 3, 4, 5, 6]]
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_transpose_row_vector(self):
|
||||
data = [[1, 2, 3, 4, 5, 6]]
|
||||
actual = Vector(data)
|
||||
|
||||
actual = actual.transpose()
|
||||
expected = [[1], [2], [3], [4], [5], [6]]
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_neg_vector(self):
|
||||
v = Vector([1, 2])
|
||||
|
||||
@ -104,6 +106,24 @@ class TestVector(TestCase):
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_truediv_scalar(self):
|
||||
v = Vector([1, 2])
|
||||
s = 5
|
||||
|
||||
expected = Vector([1/5, 2/5])
|
||||
actual = v / s
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_truediv_same_shape_vectors(self):
|
||||
v1 = Vector([1, 2])
|
||||
v2 = Vector([3, 4])
|
||||
|
||||
expected = Vector([1/3, 1/2])
|
||||
actual = v1 / v2
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_mul_same_shape_vectors(self):
|
||||
v1 = Vector([1, 2])
|
||||
v2 = Vector([3, 4])
|
||||
@ -122,6 +142,15 @@ class TestVector(TestCase):
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_mul_vectors_tensor(self):
|
||||
v1 = Vector([1, 2])
|
||||
v2 = Vector([3, 4])
|
||||
|
||||
expected = [[3, 4], [6, 8]]
|
||||
actual = v1 * v2.T()
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_mul_scalar_with_vector(self):
|
||||
v = Vector([1, 2])
|
||||
s = 2
|
||||
@ -160,5 +189,89 @@ class TestVector(TestCase):
|
||||
actual = v.normalize()
|
||||
expected = [1 / 2.236, 2 / 2.236]
|
||||
|
||||
self.assertAlmostEqual(expected[0], actual[0][0], 3)
|
||||
self.assertAlmostEqual(expected[1], actual[1][0], 3)
|
||||
self.assertAlmostEqual(expected[0], actual[0], 3)
|
||||
self.assertAlmostEqual(expected[1], actual[1], 3)
|
||||
|
||||
def test_should_return_first_element_of_column_vector(self):
|
||||
m = Vector([1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
|
||||
actual = m[0]
|
||||
expected = 1
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_return_first_element_of_row_vector(self):
|
||||
m = Vector([[1, 2, 3, 4, 5, 6, 7, 8, 9]])
|
||||
|
||||
actual = m[0]
|
||||
expected = 1
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_return_last_element_of_column_vector(self):
|
||||
m = Vector([1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
|
||||
actual = m[8]
|
||||
expected = 9
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_return_last_element_of_row_vector(self):
|
||||
m = Vector([[1, 2, 3, 4, 5, 6, 7, 8, 9]])
|
||||
|
||||
actual = m[8]
|
||||
expected = 9
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_return_all_except_last_element(self):
|
||||
m = Vector([1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
|
||||
actual = m[0:8]
|
||||
expected = Vector([1, 2, 3, 4, 5, 6, 7, 8])
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_return_all_except_first_and_last_element(self):
|
||||
m = Vector([1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
|
||||
actual = m[1:8]
|
||||
expected = Vector([2, 3, 4, 5, 6, 7, 8])
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_return_some_element(self):
|
||||
m = Vector([1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
|
||||
actual = m[0, 2, 4, 6]
|
||||
expected = Vector([1, 3, 5, 7])
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_set_first_element(self):
|
||||
m = Vector([1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
|
||||
m[0] = 10
|
||||
actual = m
|
||||
expected = Vector([10, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_set_all_except_first_and_last_element(self):
|
||||
m = Vector([1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
|
||||
m[1:8] = [4, 4, 4, 4, 4, 4, 4]
|
||||
actual = m
|
||||
expected = Vector([1, 4, 4, 4, 4, 4, 4, 4, 9])
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_set_some_elements(self):
|
||||
m = Vector([1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
|
||||
m[0, 2, 4, 6] = [10, 30, 50, 70]
|
||||
actual = m
|
||||
expected = Vector([10, 2, 30, 4, 50, 6, 70, 8, 9])
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
|
Reference in New Issue
Block a user