Rewrite matrix.py to match test_serial.py and to be less dependent on numpy
This commit is contained in:
@ -28,17 +28,6 @@ class TestMatrix(TestCase):
|
||||
expected = [[0, 1, 2]]
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_create_vectorlike_matrix_from_numpy_array_with_shape_3_1(self):
|
||||
data = numpy.array([0, 1, 2])
|
||||
actual = Matrix(data)
|
||||
|
||||
actual_shape = actual.shape()
|
||||
expected_shape = (3, 1)
|
||||
self.assertEqual(expected_shape, actual_shape)
|
||||
|
||||
expected = [0, 1, 2]
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_create_matrix_from_list_with_shape_2_2(self):
|
||||
data = [0, 1, 2, 3]
|
||||
actual = Matrix(data, shape=(2, 2))
|
||||
@ -50,9 +39,20 @@ class TestMatrix(TestCase):
|
||||
expected = [[0, 1], [2, 3]]
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_create_matrix_from_matrixlike_list_with_shape_2_3(self):
|
||||
data = [[0, 1, 2], [3, 4, 5]]
|
||||
actual = Matrix(data, shape=(2, 3))
|
||||
|
||||
actual_shape = actual.shape()
|
||||
expected_shape = (2, 3)
|
||||
self.assertEqual(expected_shape, actual_shape)
|
||||
|
||||
expected = [[0, 1, 2], [3, 4, 5]]
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_create_diagonal_matrix_from_list(self):
|
||||
data = [1, 1, 1]
|
||||
actual = Matrix(data, structure="diagonal", n=0)
|
||||
data = [1]
|
||||
actual = Matrix(data, structure="diagonal", offset=0, n=3)
|
||||
|
||||
actual_shape = actual.shape()
|
||||
expected_shape = (3, 3)
|
||||
@ -62,8 +62,8 @@ class TestMatrix(TestCase):
|
||||
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)
|
||||
data = [1]
|
||||
actual = Matrix(data, structure="diagonal", offset=1, n=4)
|
||||
|
||||
actual_shape = actual.shape()
|
||||
expected_shape = (4, 4)
|
||||
@ -188,6 +188,15 @@ class TestMatrix(TestCase):
|
||||
|
||||
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 = ""
|
||||
@ -195,20 +204,29 @@ class TestMatrix(TestCase):
|
||||
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))
|
||||
m1 = Matrix([1, 2, 3, 4], (2, 2))
|
||||
m2 = Matrix([4, 3, 2, 1], (2, 2))
|
||||
|
||||
actual = m1 * m2
|
||||
expected = Matrix([3, 4, 6, 8], (2, 2))
|
||||
expected = Matrix([8, 5, 20, 13], (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))
|
||||
m1 = Matrix([1, 2, 3, 4, 5, 6], (2, 3))
|
||||
m2 = Matrix([6, 5, 4, 3, 2, 1], (3, 2))
|
||||
|
||||
actual = m1 * m2
|
||||
expected = Matrix([11], (1, 1))
|
||||
expected = Matrix([20, 14, 56, 41], (2, 2))
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_should_mul_matrices_3(self):
|
||||
m1 = Matrix([1, 2, 3, 4, 5, 6], (3, 2))
|
||||
m2 = Matrix([6, 5, 4, 3, 2, 1], (2, 3))
|
||||
|
||||
actual = m1 * m2
|
||||
expected = Matrix([12, 9, 6, 30, 23, 16, 48, 37, 26], (3, 3))
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
@ -236,15 +254,6 @@ class TestMatrix(TestCase):
|
||||
|
||||
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_return_frobenius_norm(self):
|
||||
m = Matrix([1, 2, 3, 4], (2, 2))
|
||||
|
||||
@ -256,7 +265,7 @@ class TestMatrix(TestCase):
|
||||
def test_should_return_colsum_norm(self):
|
||||
m = Matrix([1, 2, 3, 4], (2, 2))
|
||||
|
||||
actual = m.norm("colsum")
|
||||
actual = m.norm("col sum")
|
||||
expected = 6
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
@ -264,7 +273,7 @@ class TestMatrix(TestCase):
|
||||
def test_should_return_rowsum_norm(self):
|
||||
m = Matrix([1, 2, 3, 4], (2, 2))
|
||||
|
||||
actual = m.norm("rowsum")
|
||||
actual = m.norm("row sum")
|
||||
expected = 7
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
@ -289,7 +298,7 @@ class TestMatrix(TestCase):
|
||||
m = Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9], (3, 3))
|
||||
|
||||
actual = m[0]
|
||||
expected = Matrix([1, 2, 3], (1, 3))
|
||||
expected = [1, 2, 3]
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
@ -297,7 +306,7 @@ class TestMatrix(TestCase):
|
||||
m = Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9], (3, 3))
|
||||
|
||||
actual = m[2, 0:2]
|
||||
expected = Matrix([7, 8], (1, 2))
|
||||
expected = [7, 8]
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
@ -305,7 +314,7 @@ class TestMatrix(TestCase):
|
||||
m = Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9], (3, 3))
|
||||
|
||||
actual = m[:, 1]
|
||||
expected = Matrix([2, 5, 8], (1, 3))
|
||||
expected = [2, 5, 8]
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
@ -313,7 +322,7 @@ class TestMatrix(TestCase):
|
||||
m = Matrix([1, 2, 3, 4, 5, 6, 7, 8, 9], (3, 3))
|
||||
|
||||
actual = m[[0, 2], 0]
|
||||
expected = Matrix([1, 7], (1, 2))
|
||||
expected = [1, 7]
|
||||
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
|
@ -125,9 +125,9 @@ print("\n\nTesting the matrix class -----------------------------------\n\n")
|
||||
print("Start 2a initialization")
|
||||
a_list = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 2 * np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])])
|
||||
A = Matrix(a_list)
|
||||
B = Matrix(structure="tridiagonal", given_size=11)
|
||||
B = Matrix([-1, 2, -1], structure="tridiagonal", n=11)
|
||||
c_list = [[(i + 1) / (index + 1) for index in range(10)] for i in range(10)]
|
||||
C = Matrix(c_list, given_shape=(10, 10))
|
||||
C = Matrix(c_list, shape=(10, 10))
|
||||
print("End 2a\n")
|
||||
|
||||
### 1b __str__ function, string representation
|
||||
@ -141,7 +141,7 @@ print("End 2b\n")
|
||||
### 1c shape and transpose
|
||||
print("Start 2c shape and transpose")
|
||||
# Initialization
|
||||
A = Matrix(np.array([i for i in range(12)]).reshape(-1, 1), given_shape=(4, 3))
|
||||
A = Matrix(np.array([i for i in range(12)]).reshape(-1, 1), shape=(4, 3))
|
||||
print(f"A has shape {A.shape()} | must be (4,3)")
|
||||
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)")
|
||||
@ -150,17 +150,17 @@ print("End 2c\n")
|
||||
### 1d addition and substraction
|
||||
print("Start 2d addition and substraction")
|
||||
# Initialization
|
||||
A = Matrix(structure="diagonal", given_values=[3], offset=0, given_size=10)
|
||||
A = Matrix(structure="diagonal", data=[3], offset=0, n=10)
|
||||
print(str(A))
|
||||
A21 = Matrix(structure="diagonal", given_values=[-1], offset=-1, given_size=10)
|
||||
A21 = Matrix(structure="diagonal", data=[-1], offset=-1, n=10)
|
||||
print(str(A21))
|
||||
A12 = Matrix(structure="diagonal", given_values=[-1], offset=+1, given_size=10)
|
||||
A12 = Matrix(structure="diagonal", data=[-1], offset=+1, n=10)
|
||||
print(str(A12))
|
||||
B = Matrix(structure="diagonal", given_values=[1], offset=0, given_size=10)
|
||||
B = Matrix(structure="diagonal", data=[1], offset=0, n=10)
|
||||
print(str(B))
|
||||
# computation
|
||||
C = A + A21 + A12 - B
|
||||
print(str(C) + f"must be\n{Matrix(structure='tridiagonal', given_values=[-1, 2, -1], given_size=10)}")
|
||||
print(str(C) + f"must be\n{Matrix(structure='tridiagonal', data=[-1, 2, -1], n=10)}")
|
||||
print(str(5 + A - 3))
|
||||
print("End 2d\n")
|
||||
|
||||
@ -195,21 +195,21 @@ print("End 2f\n")
|
||||
|
||||
### 1g norm
|
||||
print("Start 2g norm")
|
||||
A = Matrix(structure="tridiagonal", given_size=50, given_values=[-1, 2, -1])
|
||||
A = Matrix(structure="tridiagonal", n=50, data=[-1, 2, -1])
|
||||
print(f"Frobenius norm of tridiagonal matrix: {A.norm('frobenius')} | must be 17.263")
|
||||
print(f"Row sum norm of tridiagonal matrix: {A.norm('row sum')} | must be 2")
|
||||
print(f"Col sum norm of tridiagonal matrix: {A.norm('col sum')} | must be 2")
|
||||
print(f"Row sum norm of tridiagonal matrix: {A.norm('row sum')} | must be 4")
|
||||
print(f"Col sum norm of tridiagonal matrix: {A.norm('col sum')} | must be 4")
|
||||
print("End 2g\n")
|
||||
|
||||
### 1h negation
|
||||
print("Start 2h negation")
|
||||
A = Matrix(structure="tridiagonal", given_size=50, given_values=[-1, 2, 1])
|
||||
A = Matrix(structure="tridiagonal", n=50, data=[-1, 2, 1])
|
||||
print(f"Norm of (A + (-A)) is {(A + (-A)).norm('frobenius')} | must be < 1e-8")
|
||||
print("End 2h\n")
|
||||
|
||||
### 1i manipulation
|
||||
print("Start 2i manipulation")
|
||||
A = Matrix(structure="tridiagonal", given_size=10, given_values=[-1, 2, 1])
|
||||
A = Matrix(structure="tridiagonal", n=10, data=[-1, 2, 1])
|
||||
A[1, 1] = 4
|
||||
A[[1, 2, 3], 2] = [-5, -10, 100]
|
||||
print(str(A))
|
||||
|
Reference in New Issue
Block a user