def test_11_inverse(self): #Note: I'm adding a threshold here in equals, since we are multiplying floats and may encounter small rounding # errors The threshold is saying "the numbers are equal, give-or-take this amount." self.assertTrue(A_inv_expected.equals(A.inverse(),threshold= 1E-4),f"A_inv is incorrect.") self.assertTrue(Matrix.identity(3).equals(A.times(A.inverse()),threshold= 1E-4),f"A•A^-1 should be I.") self.assertTrue(Matrix.identity(3).equals(A.inverse().times(A),threshold= 1E-4),f"A•A^-1 should be I.") self.assertIsNone(C.inverse())
def main(): mat1 = Matrix(mat=np.array([[1, 2, -3], [4, 0, -2]])) mat2 = Matrix(mat=np.array([[5, -4, 2, 0], [-1, 6, 3, 1], [7, 0, 5, 8]])) mat3 = Matrix(mat=np.array([[-1, 3, 1], [2, 5, 0], [3, 1, -2]])) mat4 = Matrix(mat=np.array([[5, -4, 2], [-1, 3, 1], [7, 0, 8]])) mat5 = Matrix(mat=[[2, 0], [-7, 5]]) print("Printing out the content") print(mat1, "\n", mat2) print("Dot product - with real number directly") print(mat1 * 5) print("Dot product - with matrix directly") print(mat1 * mat2) print(mat3 * mat4) print("Determinant") print(abs(mat3)) print("Identities") print(Matrix.identity(3)) print("Zero") print(Matrix.zeros(3, 4))
def test_1_Identity(self): Mat_I2 = Matrix.identity(2) self.assertTrue( Mat_I2.equals(Matrix(((1, 0), (0, 1)))), f"identity of (2) incorrect. You got {Mat_I2.__repr__()}") Mat_I5 = Matrix.identity(5) self.assertTrue( Mat_I5.equals( Matrix(((1, 0, 0, 0, 0), (0, 1, 0, 0, 0), (0, 0, 1, 0, 0), (0, 0, 0, 1, 0), (0, 0, 0, 0, 1)))), f"identity of (5) incorrect. You got {Mat_I5.__repr__()}") Mat_I8 = Matrix.identity(8) self.assertTrue( Mat_I8.equals( Matrix(((1, 0, 0, 0, 0, 0, 0, 0), (0, 1, 0, 0, 0, 0, 0, 0), (0, 0, 1, 0, 0, 0, 0, 0), (0, 0, 0, 1, 0, 0, 0, 0), (0, 0, 0, 0, 1, 0, 0, 0), (0, 0, 0, 0, 0, 1, 0, 0), (0, 0, 0, 0, 0, 0, 1, 0), (0, 0, 0, 0, 0, 0, 0, 1)))), f"identity of (8) incorrect. You got {Mat_I8.__repr__()}")
import unittest from Matrices import Matrix mat_A = Matrix(((3,4),(6,7))) mat_B = Matrix.ones((2,2)) mat_A_plus_B_expected = Matrix(((4,5),(7,8))) mat_4B_expected = Matrix(((4,4),(4,4))) mat_C = Matrix(((3,5,7),(11,13,17))) mat_A_times_B_expected = Matrix(((7,7),(13,13))) mat_A_times_C_expected = Matrix(((53,67,89),(95,121,161))) mat_D = Matrix(((3,4),)) mat_E = Matrix(((5,9),)) mat_F = Matrix(((1,2,6),)) mat_G = Matrix(((5,-2,8),)) mat_H = Matrix(((28,22,-12),)) mat_I = Matrix.identity(4) mat_J = Matrix(((2,3,4,5),(12,14,16,18),(-4, 8, 0, -2))) mat_K = Matrix(((3,4),(5,6),(12,0),(-3,8))) mat_JK_expected = Matrix(((54,66),(244,276),(34,16))) class MyTestCase(unittest.TestCase): def test_4_sum(self): self.assertTrue(mat_A_plus_B_expected.equals(mat_A.add(mat_B)),"A + B was incorrect.") self.assertTrue(mat_4B_expected.equals(mat_B.add(mat_B).add(mat_B).add(mat_B)),"B + B + B + B incorrect.") with self.assertRaises(AssertionError): mat_A.add(mat_C) def test_5_scalar_multiply(self): self.assertTrue(mat_4B_expected.equals(mat_B.times(4)), f"4B is incorrect. You got {mat_B.times(4)}")
import unittest from Matrices import Matrix A = Matrix(((2, 5, 8), (3, 5, 6), (9, 12, 15))) B = Matrix.identity(3) C = Matrix(((1, 2, 3), (4, 5, 6), (7, 8, 9))) D = Matrix.ones((3, 5)) class MyTestCase(unittest.TestCase): def test_10_determinant(self): self.assertEqual(-21, A.determinant(), f"|A| should be -21. You got {A.determinant()}.") self.assertEqual(1, B.determinant(), f"|B| should be 1. You got {B.determinant()}.") self.assertEqual(0, C.determinant(), f"|C| should be 0. You got {C.determinant()}.") with self.assertRaises(AssertionError): d = D.determinant() if __name__ == '__main__': unittest.main()