示例#1
0
    def test_0_Mr_Howes_code(self):
        Mat_A = Matrix(A)
        Mat_Z = Matrix.zeros((2, 5))
        self.assertTrue(
            Mat_Z.equals(Matrix(((0, 0, 0, 0, 0), (0, 0, 0, 0, 0)))),
            "Zeros method not working correctly.")

        with self.assertRaises(AssertionError):
            Mat_Z2 = Matrix.zeros((0, 2))

        Mat_U = Matrix.ones((4, 4))
        self.assertTrue(
            Mat_U.equals(
                Matrix(
                    ((1, 1, 1, 1), (1, 1, 1, 1), (1, 1, 1, 1), (1, 1, 1, 1)))),
            "Ones method not working correctly.")
        with self.assertRaises(AssertionError):
            Mat_U2 = Matrix.ones((-1, 0))
        self.assertTupleEqual((4, 3), Mat_A.shape(
        ), f"Shape of A should be (4,3). You got a different shape: {Mat_A.shape()}."
                              )
        self.assertTupleEqual((2, 5), Mat_Z.shape(
        ), f"Shape of Z should be (2,5). You got a different shape: {Mat_Z.shape()}."
                              )
        self.assertTupleEqual((4, 4), Mat_U.shape(
        ), f"Shape of U should be (4,4). You got a different shape: {Mat_U.shape()}."
                              )
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)}")
示例#3
0
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()