ef39742cf0c225ab4242d0b2d0b892d570848c73
[bigint-presentation-code.git] / src / bigint_presentation_code / test_matrix.py
1 import unittest
2 from fractions import Fraction
3
4 from bigint_presentation_code.matrix import Matrix
5
6
7 class TestMatrix(unittest.TestCase):
8 def test_repr(self):
9 self.assertEqual(repr(Matrix(2, 3, [0, 1, 2,
10 3, 4, 5])),
11 'Matrix(height=2, width=3, data=[\n'
12 ' 0, 1, 2,\n'
13 ' 3, 4, 5,\n'
14 '])')
15 self.assertEqual(repr(Matrix(2, 3, [0, 1, Fraction(2) / 3,
16 3, 4, 5])),
17 'Matrix(height=2, width=3, data=[\n'
18 ' 0, 1, Fraction(2, 3),\n'
19 ' 3, 4, 5,\n'
20 '])')
21 self.assertEqual(repr(Matrix(0, 3)), 'Matrix(height=0, width=3)')
22 self.assertEqual(repr(Matrix(2, 0)), 'Matrix(height=2, width=0)')
23
24 def test_eq(self):
25 self.assertFalse(Matrix(1, 1) == 5)
26 self.assertFalse(5 == Matrix(1, 1))
27 self.assertFalse(Matrix(2, 1) == Matrix(1, 1))
28 self.assertFalse(Matrix(1, 2) == Matrix(1, 1))
29 self.assertTrue(Matrix(1, 1) == Matrix(1, 1))
30 self.assertTrue(Matrix(1, 1, [1]) == Matrix(1, 1, [1]))
31 self.assertFalse(Matrix(1, 1, [2]) == Matrix(1, 1, [1]))
32
33 def test_add(self):
34 self.assertEqual(Matrix(2, 2, [1, 2, 3, 4])
35 + Matrix(2, 2, [40, 30, 20, 10]),
36 Matrix(2, 2, [41, 32, 23, 14]))
37
38 def test_identity(self):
39 self.assertEqual(Matrix.identity(2, 2),
40 Matrix(2, 2, [1, 0,
41 0, 1]))
42 self.assertEqual(Matrix.identity(1, 3),
43 Matrix(1, 3, [1, 0, 0]))
44 self.assertEqual(Matrix.identity(2, 3),
45 Matrix(2, 3, [1, 0, 0,
46 0, 1, 0]))
47 self.assertEqual(Matrix.identity(3),
48 Matrix(3, 3, [1, 0, 0,
49 0, 1, 0,
50 0, 0, 1]))
51
52 def test_sub(self):
53 self.assertEqual(Matrix(2, 2, [40, 30, 20, 10])
54 - Matrix(2, 2, [-1, -2, -3, -4]),
55 Matrix(2, 2, [41, 32, 23, 14]))
56
57 def test_neg(self):
58 self.assertEqual(-Matrix(2, 2, [40, 30, 20, 10]),
59 Matrix(2, 2, [-40, -30, -20, -10]))
60
61 def test_mul(self):
62 self.assertEqual(Matrix(2, 2, [1, 2, 3, 4]) * Fraction(3, 2),
63 Matrix(2, 2, [Fraction(3, 2), 3, Fraction(9, 2), 6]))
64 self.assertEqual(Fraction(3, 2) * Matrix(2, 2, [1, 2, 3, 4]),
65 Matrix(2, 2, [Fraction(3, 2), 3, Fraction(9, 2), 6]))
66
67 def test_matmul(self):
68 self.assertEqual(Matrix(2, 2, [1, 2, 3, 4])
69 @ Matrix(2, 2, [4, 3, 2, 1]),
70 Matrix(2, 2, [8, 5, 20, 13]))
71 self.assertEqual(Matrix(3, 2, [6, 5, 4, 3, 2, 1])
72 @ Matrix(2, 1, [1, 2]),
73 Matrix(3, 1, [16, 10, 4]))
74
75 def test_inverse(self):
76 self.assertEqual(Matrix(0, 0).inverse(), Matrix(0, 0))
77 self.assertEqual(Matrix(1, 1, [2]).inverse(),
78 Matrix(1, 1, [Fraction(1, 2)]))
79 self.assertEqual(Matrix(1, 1, [1]).inverse(),
80 Matrix(1, 1, [1]))
81 self.assertEqual(Matrix(2, 2, [1, 0, 1, 1]).inverse(),
82 Matrix(2, 2, [1, 0, -1, 1]))
83 self.assertEqual(Matrix(3, 3, [0, 1, 0,
84 1, 0, 0,
85 0, 0, 1]).inverse(),
86 Matrix(3, 3, [0, 1, 0,
87 1, 0, 0,
88 0, 0, 1]))
89 _1_2 = Fraction(1, 2)
90 _1_3 = Fraction(1, 3)
91 _1_6 = Fraction(1, 6)
92 self.assertEqual(Matrix(5, 5, [1, 0, 0, 0, 0,
93 1, 1, 1, 1, 1,
94 1, -1, 1, -1, 1,
95 1, -2, 4, -8, 16,
96 0, 0, 0, 0, 1]).inverse(),
97 Matrix(5, 5, [1, 0, 0, 0, 0,
98 _1_2, _1_3, -1, _1_6, -2,
99 -1, _1_2, _1_2, 0, -1,
100 -_1_2, _1_6, _1_2, -_1_6, 2,
101 0, 0, 0, 0, 1]))
102 with self.assertRaisesRegex(ZeroDivisionError, "Matrix is singular"):
103 Matrix(1, 1, [0]).inverse()
104 with self.assertRaisesRegex(ZeroDivisionError, "Matrix is singular"):
105 Matrix(2, 2, [0, 0, 1, 1]).inverse()
106 with self.assertRaisesRegex(ZeroDivisionError, "Matrix is singular"):
107 Matrix(2, 2, [1, 0, 1, 0]).inverse()
108 with self.assertRaisesRegex(ZeroDivisionError, "Matrix is singular"):
109 Matrix(2, 2, [1, 1, 1, 1]).inverse()
110
111
112 if __name__ == "__main__":
113 unittest.main()