2023-12-12 14:31:09 +01:00
|
|
|
from unittest import TestCase
|
|
|
|
|
|
|
|
from vector import Vector
|
|
|
|
|
|
|
|
|
|
|
|
class TestVector(TestCase):
|
|
|
|
def test_should_create_zero_vector(self):
|
2024-01-13 13:53:21 +01:00
|
|
|
actual = Vector(5)
|
|
|
|
expected = Vector([0, 0, 0, 0, 0])
|
2023-12-12 14:31:09 +01:00
|
|
|
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_should_create_vector(self):
|
2024-01-13 13:53:21 +01:00
|
|
|
actual = Vector(list(range(5)))
|
|
|
|
expected = Vector([0, 1, 2, 3, 4])
|
2023-12-12 14:31:09 +01:00
|
|
|
|
2023-12-12 16:34:54 +01:00
|
|
|
self.assertEqual(expected, actual)
|
2023-12-12 14:31:09 +01:00
|
|
|
|
2024-02-22 02:21:05 +01:00
|
|
|
def test_should_transpose_col_vector(self):
|
2024-01-18 16:02:20 +01:00
|
|
|
data = [1, 2, 3, 4, 5, 6]
|
|
|
|
actual = Vector(data)
|
|
|
|
|
|
|
|
actual = actual.transpose()
|
|
|
|
expected = [[1, 2, 3, 4, 5, 6]]
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2024-02-22 02:21:05 +01:00
|
|
|
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)
|
|
|
|
|
2023-12-12 16:34:54 +01:00
|
|
|
def test_should_neg_vector(self):
|
|
|
|
v = Vector([1, 2])
|
|
|
|
|
|
|
|
expected = Vector([-1, -2])
|
|
|
|
actual = -v
|
2024-01-13 13:53:21 +01:00
|
|
|
|
2023-12-12 14:31:09 +01:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_should_add_vectors(self):
|
|
|
|
v1 = Vector([1, 2])
|
|
|
|
v2 = Vector([3, 4])
|
|
|
|
|
|
|
|
expected = Vector([4, 6])
|
|
|
|
actual = v1 + v2
|
2024-01-13 13:53:21 +01:00
|
|
|
|
2023-12-12 16:34:54 +01:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_should_add_scalar_to_vector(self):
|
|
|
|
v = Vector([1, 2])
|
|
|
|
s = 2
|
|
|
|
|
|
|
|
expected = Vector([3, 4])
|
|
|
|
actual = v + s
|
2024-01-13 13:53:21 +01:00
|
|
|
|
2023-12-12 16:34:54 +01:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_should_radd_scalar_to_vector(self):
|
|
|
|
v = Vector([1, 2])
|
|
|
|
s = 2
|
2023-12-12 14:31:09 +01:00
|
|
|
|
2023-12-12 16:34:54 +01:00
|
|
|
expected = Vector([3, 4])
|
|
|
|
actual = s + v
|
2024-01-13 13:53:21 +01:00
|
|
|
|
2023-12-12 14:31:09 +01:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2023-12-12 16:34:54 +01:00
|
|
|
def test_should_raise_value_missmatch_error_while_adding_vectors(self):
|
2023-12-12 14:31:09 +01:00
|
|
|
v1 = Vector(1)
|
|
|
|
v2 = Vector(2)
|
2024-01-13 13:53:21 +01:00
|
|
|
|
2023-12-12 16:34:54 +01:00
|
|
|
self.assertRaises(ValueError, lambda: v1 + v2)
|
2023-12-12 14:31:09 +01:00
|
|
|
|
2023-12-12 16:34:54 +01:00
|
|
|
def test_should_raise_dimension_error_while_adding_vectors(self):
|
|
|
|
v1 = Vector(1)
|
|
|
|
v2 = '0'
|
2024-01-13 13:53:21 +01:00
|
|
|
|
2023-12-12 14:31:09 +01:00
|
|
|
self.assertRaises(ValueError, lambda: v1 + v2)
|
2023-12-12 16:34:54 +01:00
|
|
|
|
2024-01-13 13:53:21 +01:00
|
|
|
def test_should_sub_vectors(self):
|
2023-12-12 16:34:54 +01:00
|
|
|
v1 = Vector([1, 2])
|
|
|
|
v2 = Vector([3, 4])
|
|
|
|
|
|
|
|
expected = Vector([-2, -2])
|
|
|
|
actual = v1 - v2
|
2024-01-13 13:53:21 +01:00
|
|
|
|
2023-12-12 16:34:54 +01:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2024-01-13 13:53:21 +01:00
|
|
|
def test_should_sub_scalar_of_vector(self):
|
2023-12-12 16:34:54 +01:00
|
|
|
v = Vector([1, 2])
|
|
|
|
s = 2
|
|
|
|
|
|
|
|
expected = Vector([-1, 0])
|
|
|
|
actual = v - s
|
2024-01-13 13:53:21 +01:00
|
|
|
|
2023-12-12 16:34:54 +01:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2024-01-13 13:53:21 +01:00
|
|
|
def test_should_rsub_scalar_of_vector(self):
|
2023-12-12 16:34:54 +01:00
|
|
|
v = Vector([1, 2])
|
|
|
|
s = 2
|
|
|
|
|
|
|
|
expected = Vector([1, 0])
|
|
|
|
actual = s - v
|
2024-01-13 13:53:21 +01:00
|
|
|
|
2023-12-12 16:34:54 +01:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2024-02-22 02:21:05 +01:00
|
|
|
def test_should_truediv_scalar(self):
|
|
|
|
v = Vector([1, 2])
|
|
|
|
s = 5
|
|
|
|
|
2024-02-22 02:28:26 +01:00
|
|
|
expected = Vector([1 / 5, 2 / 5])
|
2024-02-22 02:21:05 +01:00
|
|
|
actual = v / s
|
|
|
|
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_should_truediv_same_shape_vectors(self):
|
|
|
|
v1 = Vector([1, 2])
|
|
|
|
v2 = Vector([3, 4])
|
|
|
|
|
2024-02-22 02:28:26 +01:00
|
|
|
expected = Vector([1 / 3, 1 / 2])
|
2024-02-22 02:21:05 +01:00
|
|
|
actual = v1 / v2
|
|
|
|
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2024-01-18 16:02:20 +01:00
|
|
|
def test_should_mul_same_shape_vectors(self):
|
2023-12-12 16:34:54 +01:00
|
|
|
v1 = Vector([1, 2])
|
|
|
|
v2 = Vector([3, 4])
|
|
|
|
|
2024-01-18 16:02:20 +01:00
|
|
|
expected = Vector([3, 8])
|
2023-12-12 16:34:54 +01:00
|
|
|
actual = v1 * v2
|
2024-01-13 13:53:21 +01:00
|
|
|
|
2023-12-12 16:34:54 +01:00
|
|
|
self.assertEqual(expected, actual)
|
2024-01-18 16:02:20 +01:00
|
|
|
|
|
|
|
def test_should_mul_vectors_dot(self):
|
|
|
|
v1 = Vector([1, 2])
|
|
|
|
v2 = Vector([3, 4])
|
|
|
|
|
|
|
|
expected = 11
|
|
|
|
actual = v1.T() * v2
|
|
|
|
|
|
|
|
self.assertEqual(expected, actual)
|
2023-12-12 16:34:54 +01:00
|
|
|
|
2024-02-22 02:21:05 +01:00
|
|
|
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)
|
|
|
|
|
2024-01-13 13:53:21 +01:00
|
|
|
def test_should_mul_scalar_with_vector(self):
|
2023-12-12 16:34:54 +01:00
|
|
|
v = Vector([1, 2])
|
|
|
|
s = 2
|
|
|
|
|
|
|
|
expected = Vector([2, 4])
|
|
|
|
actual = v * s
|
2024-01-13 13:53:21 +01:00
|
|
|
|
2023-12-12 16:34:54 +01:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2024-01-13 13:53:21 +01:00
|
|
|
def test_should_rmul_scalar_with_vector(self):
|
2023-12-12 16:34:54 +01:00
|
|
|
v = Vector([1, 2])
|
|
|
|
s = 2
|
|
|
|
|
|
|
|
expected = Vector([2, 4])
|
|
|
|
actual = s * v
|
2024-01-13 13:53:21 +01:00
|
|
|
|
2023-12-12 16:34:54 +01:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2024-01-13 13:53:21 +01:00
|
|
|
def test_should_raise_value_missmatch_error_while_mul_vectors(self):
|
2023-12-12 16:34:54 +01:00
|
|
|
v1 = Vector([1, 2])
|
|
|
|
v2 = '0'
|
2024-01-13 13:53:21 +01:00
|
|
|
|
2023-12-12 16:34:54 +01:00
|
|
|
self.assertRaises(ValueError, lambda: v1 * v2)
|
2024-01-13 13:53:21 +01:00
|
|
|
|
|
|
|
def test_should_return_vector_norm(self):
|
|
|
|
v = Vector([1, 2])
|
|
|
|
|
|
|
|
actual = v.norm()
|
|
|
|
expected = 2.236
|
|
|
|
|
|
|
|
self.assertAlmostEqual(expected, actual, 3)
|
|
|
|
|
|
|
|
def test_should_return_normalized_vector(self):
|
|
|
|
v = Vector([1, 2])
|
|
|
|
|
|
|
|
actual = v.normalize()
|
|
|
|
expected = [1 / 2.236, 2 / 2.236]
|
|
|
|
|
2024-02-22 02:21:05 +01:00
|
|
|
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)
|