コード例 #1
0
ファイル: Part1Test.py プロジェクト: Harlan-Howe/Matrices2021
    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()}."
                              )
コード例 #2
0
ファイル: Demo.py プロジェクト: ZhongxuanWang/Python-Cluster
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))