def test_doit_args(): A = ImmutableMatrix([[1, 2], [3, 4]]) B = ImmutableMatrix([[2, 3], [4, 5]]) assert MatAdd(A, MatPow(B, 2)).doit() == A + B**2 assert MatAdd(A, MatMul(A, B)).doit() == A + A * B assert MatAdd(A, A).doit(deep=False) == 2 * A assert (MatAdd(A, X, MatMul(A, B), Y, MatAdd(2 * A, B)).doit() == MatAdd(X, Y, 3 * A + A * B + B))
def test_transpose(): assert transpose(A * B) == Transpose(B) * Transpose(A) assert transpose(2 * A * B) == 2 * Transpose(B) * Transpose(A) assert transpose(2 * I * C) == 2 * I * Transpose(C) M = Matrix(2, 2, [1, 2 + I, 3, 4]).as_immutable() MT = Matrix(2, 2, [1, 3, 2 + I, 4]) assert transpose(M) == MT assert transpose(2 * M) == 2 * MT assert transpose(MatMul(2, M)) == MatMul(2, MT).doit()
def test_adjoint(): assert adjoint(A * B) == Adjoint(B) * Adjoint(A) assert adjoint(2 * A * B) == 2 * Adjoint(B) * Adjoint(A) assert adjoint(2 * I * C) == -2 * I * Adjoint(C) M = Matrix(2, 2, [1, 2 + I, 3, 4]).as_immutable() MA = Matrix(2, 2, [1, 3, 2 - I, 4]) assert adjoint(M) == MA assert adjoint(2 * M) == 2 * MA assert adjoint(MatMul(2, M)) == MatMul(2, MA).doit()
def test_Trace_doit_deep_False(): X = Matrix([[1, 2], [3, 4]]) assert Trace(X).doit(deep=False) == 5 q = MatPow(X, 2) assert Trace(q).doit(deep=False).arg == q q = MatAdd(X, 2 * X) assert Trace(q).doit(deep=False).arg == q q = MatMul(X, 2 * X) assert Trace(q).doit(deep=False).arg == q
def test_invariants(): A = MatrixSymbol('A', n, m) B = MatrixSymbol('B', m, l) X = MatrixSymbol('X', n, n) objs = [Identity(n), ZeroMatrix(m, n), MatMul(A, B), MatAdd(A, A), Transpose(A), Adjoint(A), Inverse(X), MatPow(X, 2), MatPow(X, -1), MatPow(X, 0)] for obj in objs: assert obj == obj.__class__(*obj.args)
def test_doit_drills_down(): X = ImmutableMatrix([[1, 2], [3, 4]]) Y = ImmutableMatrix([[2, 3], [4, 5]]) assert MatMul(X, MatPow(Y, 2)).doit() == X * Y**2 assert MatMul(C, Transpose(D * C)).doit().args == (C, C.T, D.T)
def test_doit(): assert MatMul(C, 2, D).args == (C, 2, D) assert MatMul(C, 2, D).doit().args == (2, C, D) assert MatMul(C, Transpose(D * C)).args == (C, Transpose(D * C)) assert MatMul(C, Transpose(D * C)).doit(deep=True).args == (C, C.T, D.T)
def test_unpack(): assert unpack(MatMul(A, evaluate=False)) == A x = MatMul(A, B) assert unpack(x) == x
def test_any_zeros(): assert any_zeros(MatMul(A, ZeroMatrix(m, k), evaluate=False)) == \ ZeroMatrix(n, k)
def test_doit_deep_false_still_canonical(): assert (MatMul(C, Transpose(D * C), 2).doit(deep=False).args == (2, C, Transpose(D * C)))
def test_matmul_simplify(): A = MatrixSymbol('A', 1, 1) assert simplify(MatMul(A, ImmutableMatrix([[sin(x)**2 + cos(x)**2]]))) == \ MatMul(A, ImmutableMatrix([[1]]))
def test_factor_in_front(): assert factor_in_front(MatMul(A, 2, B, evaluate=False)) ==\ MatMul(2, A, B, evaluate=False)
def test_matmul_new(): pytest.raises(ShapeError, lambda: MatMul(A, C)) MatMul(A, C, check=False) # not raises
def test_collapse_MatrixBase(): A = Matrix([[1, 1], [1, 1]]) B = Matrix([[1, 2], [3, 4]]) assert MatMul(A, B).doit() == ImmutableMatrix([[4, 6], [4, 6]])
def test_matmul_sympify(): assert isinstance(MatMul(eye(1), eye(1)).args[0], Basic)
def test_matmul_scalar_Matrix_doit(): # Issue sympy/sympy#9053 X = Matrix([[1, 2], [3, 4]]) assert MatMul(2, X).doit() == 2 * X
def test_trace_constant_factor(): # Issue sympy/sympy#9052: gave 2*Trace(MatMul(A)) instead of 2*Trace(A) assert trace(2 * A) == 2 * Trace(A) X = ImmutableMatrix([[1, 2], [3, 4]]) assert trace(MatMul(2, X)) == 10
def test_remove_ids(): assert remove_ids(MatMul(A, Identity(m), B, evaluate=False)) == \ MatMul(A, B, evaluate=False) assert null_safe(remove_ids)(MatMul(Identity(n), evaluate=False)) == \ MatMul(Identity(n), evaluate=False)
def test_xxinv(): assert xxinv(MatMul(D, Inverse(D), D, evaluate=False)) == \ MatMul(Identity(n), D, evaluate=False)
def test_doit_nested_MatrixExpr(): X = ImmutableMatrix([[1, 2], [3, 4]]) Y = ImmutableMatrix([[2, 3], [4, 5]]) assert MatPow(MatMul(X, Y), 2).doit() == (X * Y)**2 assert MatPow(MatAdd(X, Y), 2).doit() == (X + Y)**2