2023-12-12 15:34:54 +00:00
|
|
|
from unittest import TestCase
|
|
|
|
|
|
|
|
import numpy
|
|
|
|
|
|
|
|
from matrix import Matrix
|
|
|
|
|
|
|
|
|
|
|
|
class TestMatrix(TestCase):
|
|
|
|
def test_should_create_matrix_from_numpy_array_with_shape_3_2(self):
|
|
|
|
data = numpy.array([[0, 1], [2, 3], [4, 5]])
|
2023-12-15 00:47:16 +00:00
|
|
|
actual = Matrix(data)
|
2023-12-12 15:34:54 +00:00
|
|
|
|
2023-12-15 00:47:16 +00:00
|
|
|
actual_shape = actual.shape()
|
2023-12-12 15:34:54 +00:00
|
|
|
expected_shape = (3, 2)
|
|
|
|
self.assertEqual(expected_shape, actual_shape)
|
|
|
|
|
2023-12-15 00:47:16 +00:00
|
|
|
expected = [[0, 1], [2, 3], [4, 5]]
|
|
|
|
self.assertEqual(expected, actual)
|
2023-12-12 15:34:54 +00:00
|
|
|
|
|
|
|
def test_should_create_matrix_from_numpy_array_with_shape_1_3(self):
|
|
|
|
data = numpy.array([[0, 1, 2]])
|
2023-12-15 00:47:16 +00:00
|
|
|
actual = Matrix(data)
|
2023-12-12 15:34:54 +00:00
|
|
|
|
2023-12-15 00:47:16 +00:00
|
|
|
actual_shape = actual.shape()
|
2023-12-12 15:34:54 +00:00
|
|
|
expected_shape = (1, 3)
|
|
|
|
self.assertEqual(expected_shape, actual_shape)
|
|
|
|
|
2023-12-15 00:47:16 +00:00
|
|
|
expected = [[0, 1, 2]]
|
|
|
|
self.assertEqual(expected, actual)
|
2023-12-12 15:34:54 +00:00
|
|
|
|
|
|
|
def test_should_create_vectorlike_matrix_from_numpy_array_with_shape_3_1(self):
|
|
|
|
data = numpy.array([0, 1, 2])
|
2023-12-15 00:47:16 +00:00
|
|
|
actual = Matrix(data)
|
2023-12-12 15:34:54 +00:00
|
|
|
|
2023-12-15 00:47:16 +00:00
|
|
|
actual_shape = actual.shape()
|
2023-12-12 15:34:54 +00:00
|
|
|
expected_shape = (3, 1)
|
|
|
|
self.assertEqual(expected_shape, actual_shape)
|
|
|
|
|
2023-12-15 00:47:16 +00:00
|
|
|
expected = [0, 1, 2]
|
|
|
|
self.assertEqual(expected, actual)
|
2023-12-12 15:34:54 +00:00
|
|
|
|
|
|
|
def test_should_create_matrix_from_list_with_shape_2_2(self):
|
|
|
|
data = [0, 1, 2, 3]
|
2023-12-15 00:47:16 +00:00
|
|
|
actual = Matrix(data, shape=(2, 2))
|
2023-12-12 15:34:54 +00:00
|
|
|
|
2023-12-15 00:47:16 +00:00
|
|
|
actual_shape = actual.shape()
|
2023-12-12 15:34:54 +00:00
|
|
|
expected_shape = (2, 2)
|
|
|
|
self.assertEqual(expected_shape, actual_shape)
|
|
|
|
|
2023-12-15 00:47:16 +00:00
|
|
|
expected = [[0, 1], [2, 3]]
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_should_create_diagonal_matrix_from_list(self):
|
|
|
|
data = [1, 1, 1]
|
|
|
|
actual = Matrix(data, structure="diagonal", n=0)
|
|
|
|
|
|
|
|
actual_shape = actual.shape()
|
|
|
|
expected_shape = (3, 3)
|
|
|
|
self.assertEqual(expected_shape, actual_shape)
|
|
|
|
|
|
|
|
expected = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_should_create_diagonal_matrix_from_list_with_offset_1(self):
|
|
|
|
data = [1, 1, 1]
|
|
|
|
actual = Matrix(data, structure="diagonal", n=1)
|
|
|
|
|
|
|
|
actual_shape = actual.shape()
|
|
|
|
expected_shape = (4, 4)
|
|
|
|
self.assertEqual(expected_shape, actual_shape)
|
|
|
|
|
|
|
|
expected = [[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]]
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_should_raise_value_error_while_creating_tridiagonal_matrix(self):
|
|
|
|
self.assertRaises(ValueError, lambda: Matrix([-1, 1, -1, 0], structure="tridiagonal", n=1))
|
|
|
|
|
|
|
|
def test_should_create_tridiagonal_matrix_from_list_with_size_4(self):
|
|
|
|
data = [-1, 1, -1]
|
|
|
|
actual = Matrix(data, structure="tridiagonal", n=4)
|
|
|
|
|
|
|
|
actual_shape = actual.shape()
|
|
|
|
expected_shape = (4, 4)
|
|
|
|
self.assertEqual(expected_shape, actual_shape)
|
|
|
|
|
|
|
|
expected = [[1, -1, 0, 0], [-1, 1, -1, 0], [0, -1, 1, -1], [0, 0, -1, 1]]
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2023-12-25 18:25:33 +00:00
|
|
|
def test_matrices_should_be_equal(self):
|
|
|
|
m1 = Matrix([1, 2, 3, 4], (2, 2))
|
|
|
|
m2 = Matrix([1, 2, 3, 4], (2, 2))
|
|
|
|
|
|
|
|
self.assertEqual(m1, m2)
|
|
|
|
|
|
|
|
def test_matrices_should_not_be_equal(self):
|
|
|
|
m1 = Matrix([1, 2, 3, 4], (2, 2))
|
|
|
|
m2 = Matrix([1, 2, 3, 3], (2, 2))
|
|
|
|
|
|
|
|
self.assertNotEqual(m1, m2)
|
|
|
|
|
2023-12-15 00:47:16 +00:00
|
|
|
def test_should_transpose_matrix(self):
|
|
|
|
data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
|
|
actual = Matrix(data, (3, 3))
|
|
|
|
|
|
|
|
actual = actual.transpose()
|
|
|
|
expected = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
|
|
|
|
self.assertEqual(expected, actual)
|
2023-12-25 18:25:33 +00:00
|
|
|
|
|
|
|
def test_should_negate_matrix(self):
|
|
|
|
m = Matrix([1, 2, 3, 4], (2, 2))
|
|
|
|
|
|
|
|
actual = -m
|
|
|
|
expected = Matrix([-1, -2, -3, -4], (2, 2))
|
|
|
|
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_should_add_two_matrices(self):
|
|
|
|
m1 = Matrix([1, 2, 3, 4], (2, 2))
|
|
|
|
m2 = Matrix([4, 3, 2, 1], (2, 2))
|
|
|
|
|
|
|
|
actual = m1 + m2
|
|
|
|
expected = Matrix([5, 5, 5, 5], (2, 2))
|
|
|
|
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_should_add_scalar_to_matrix(self):
|
|
|
|
m = Matrix([1, 2, 3, 4], (2, 2))
|
|
|
|
s = 5
|
|
|
|
|
|
|
|
actual = m + s
|
|
|
|
expected = Matrix([6, 7, 8, 9], (2, 2))
|
|
|
|
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_should_radd_scalar_to_matrix(self):
|
|
|
|
m = Matrix([1, 2, 3, 4], (2, 2))
|
|
|
|
s = 5
|
|
|
|
|
|
|
|
actual = s + m
|
|
|
|
expected = Matrix([6, 7, 8, 9], (2, 2))
|
|
|
|
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_should_raise_shape_missmatch_error_while_adding_vectors(self):
|
|
|
|
m1 = Matrix([1, 2, 3, 4], (2, 2))
|
|
|
|
m2 = Matrix([1, 2], (2, 1))
|
|
|
|
|
|
|
|
self.assertRaises(ValueError, lambda: m1 + m2)
|
|
|
|
|
|
|
|
def test_should_raise_value_missmatch_error_while_adding_vectors(self):
|
|
|
|
m = Matrix([1, 2, 3, 4], (2, 2))
|
|
|
|
o = ""
|
|
|
|
|
|
|
|
self.assertRaises(ValueError, lambda: m + o)
|
|
|
|
|
|
|
|
def test_should_sub_two_matrices(self):
|
|
|
|
m1 = Matrix([1, 2, 3, 4], (2, 2))
|
|
|
|
m2 = Matrix([4, 3, 2, 1], (2, 2))
|
|
|
|
|
|
|
|
actual = m1 - m2
|
|
|
|
expected = Matrix([-3, -1, 1, 3], (2, 2))
|
|
|
|
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_should_sub_scalar_of_matrix(self):
|
|
|
|
m = Matrix([1, 2, 3, 4], (2, 2))
|
|
|
|
s = 5
|
|
|
|
|
|
|
|
actual = m - s
|
|
|
|
expected = Matrix([-4, -3, -2, -1], (2, 2))
|
|
|
|
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_should_rsub_scalar_of_matrix(self):
|
|
|
|
m = Matrix([1, 2, 3, 4], (2, 2))
|
|
|
|
s = 5
|
|
|
|
|
|
|
|
actual = s - m
|
|
|
|
expected = Matrix([4, 3, 2, 1], (2, 2))
|
|
|
|
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_should_div_matrix_by_scalar(self):
|
|
|
|
m = Matrix([5, 10, 15, 20], (2, 2))
|
|
|
|
s = 5
|
|
|
|
|
|
|
|
actual = m / s
|
|
|
|
expected = Matrix([1, 2, 3, 4], (2, 2))
|
|
|
|
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_should_raise_value_missmatch_error_while_dividing_with_other_than_scalar(self):
|
|
|
|
m = Matrix([1, 2, 3, 4], (2, 2))
|
|
|
|
o = ""
|
|
|
|
|
|
|
|
self.assertRaises(ValueError, lambda: m / o)
|
|
|
|
|
|
|
|
def test_should_mul_matrices_1(self):
|
|
|
|
m1 = Matrix([1, 2], (2, 1))
|
|
|
|
m2 = Matrix([3, 4], (1, 2))
|
|
|
|
|
|
|
|
actual = m1 * m2
|
|
|
|
expected = Matrix([3, 4, 6, 8], (2, 2))
|
|
|
|
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_should_mul_matrices_2(self):
|
|
|
|
m1 = Matrix([1, 2], (1, 2))
|
|
|
|
m2 = Matrix([3, 4], (2, 1))
|
|
|
|
|
|
|
|
actual = m1 * m2
|
|
|
|
expected = Matrix([11], (1, 1))
|
|
|
|
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_should_raise_shape_missmatch_error_while_multiplying_matrices(self):
|
|
|
|
m1 = Matrix([1, 2], (2, 1))
|
|
|
|
m2 = Matrix([3, 4], (2, 1))
|
|
|
|
|
|
|
|
self.assertRaises(ValueError, lambda: m1 * m2)
|