ef39742cf0c225ab4242d0b2d0b892d570848c73
2 from fractions
import Fraction
4 from bigint_presentation_code
.matrix
import Matrix
7 class TestMatrix(unittest
.TestCase
):
9 self
.assertEqual(repr(Matrix(2, 3, [0, 1, 2,
11 'Matrix(height=2, width=3, data=[\n'
15 self
.assertEqual(repr(Matrix(2, 3, [0, 1, Fraction(2) / 3,
17 'Matrix(height=2, width=3, data=[\n'
18 ' 0, 1, Fraction(2, 3),\n'
21 self
.assertEqual(repr(Matrix(0, 3)), 'Matrix(height=0, width=3)')
22 self
.assertEqual(repr(Matrix(2, 0)), 'Matrix(height=2, width=0)')
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]))
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]))
38 def test_identity(self
):
39 self
.assertEqual(Matrix
.identity(2, 2),
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,
47 self
.assertEqual(Matrix
.identity(3),
48 Matrix(3, 3, [1, 0, 0,
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]))
58 self
.assertEqual(-Matrix(2, 2, [40, 30, 20, 10]),
59 Matrix(2, 2, [-40, -30, -20, -10]))
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]))
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]))
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(),
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,
86 Matrix(3, 3, [0, 1, 0,
92 self
.assertEqual(Matrix(5, 5, [1, 0, 0, 0, 0,
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,
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()
112 if __name__
== "__main__":