def test_transpose(): Sq = MatrixSymbol('Sq', n, n) assert transpose(A) == Transpose(A) assert Transpose(A).shape == (m, n) assert Transpose(A * B).shape == (l, n) assert transpose(Transpose(A)) == A assert isinstance(Transpose(Transpose(A)), Transpose) assert adjoint(Transpose(A)) == Adjoint(Transpose(A)) assert conjugate(Transpose(A)) == Adjoint(A) assert Transpose(eye(3)).doit() == eye(3) assert Transpose(S(5)).doit() == S(5) assert Transpose(Matrix([[1, 2], [3, 4]])).doit() == Matrix([[1, 3], [2, 4]]) assert transpose(trace(Sq)) == trace(Sq) assert trace(Transpose(Sq)) == trace(Sq) assert Transpose(Sq)[0, 1] == Sq[1, 0] assert Transpose(A * B).doit() == Transpose(B) * Transpose(A)
def test_BlockDiagMatrix_trace(): assert trace(BlockDiagMatrix()) == 0 assert trace(BlockDiagMatrix(ZeroMatrix(n, n))) == 0 A = MatrixSymbol('A', n, n) assert trace(BlockDiagMatrix(A)) == trace(A) B = MatrixSymbol('B', m, m) assert trace(BlockDiagMatrix(A, B)) == trace(A) + trace(B) # non-square blocks C = MatrixSymbol('C', m, n) D = MatrixSymbol('D', n, m) assert isinstance(trace(BlockDiagMatrix(C, D)), Trace)
def test_issue_9817_simplify(): # simplify on trace of substituted explicit quadratic form of matrix # expressions (a scalar) should return without errors (AttributeError) # See issue #9817 and #9190 for the original bug more discussion on this from sympy.matrices.expressions import Identity, trace v = MatrixSymbol('v', 3, 1) A = MatrixSymbol('A', 3, 3) x = Matrix([i + 1 for i in range(3)]) X = Identity(3) quadratic = v.T * A * v assert simplify((trace(quadratic.as_explicit())).xreplace({v:x, A:X})) == 14
def test_adjoint(): Sq = MatrixSymbol("Sq", n, n) assert Adjoint(A).shape == (m, n) assert Adjoint(A * B).shape == (l, n) assert adjoint(Adjoint(A)) == A assert isinstance(Adjoint(Adjoint(A)), Adjoint) assert conjugate(Adjoint(A)) == Transpose(A) assert transpose(Adjoint(A)) == Adjoint(Transpose(A)) assert Adjoint(eye(3)).doit() == eye(3) assert Adjoint(S(5)).doit() == S(5) assert Adjoint(Matrix([[1, 2], [3, 4]])).doit() == Matrix([[1, 3], [2, 4]]) assert adjoint(trace(Sq)) == conjugate(trace(Sq)) assert trace(adjoint(Sq)) == conjugate(trace(Sq)) assert Adjoint(Sq)[0, 1] == conjugate(Sq[1, 0]) assert Adjoint(A * B).doit() == Adjoint(B) * Adjoint(A)
def test_adjoint(): Sq = MatrixSymbol('Sq', n, n) assert Adjoint(A).shape == (m, n) assert Adjoint(A*B).shape == (l, n) assert adjoint(Adjoint(A)) == A assert isinstance(Adjoint(Adjoint(A)), Adjoint) assert conjugate(Adjoint(A)) == Transpose(A) assert transpose(Adjoint(A)) == Adjoint(Transpose(A)) assert Adjoint(eye(3)).doit() == eye(3) assert Adjoint(S(5)).doit() == S(5) assert Adjoint(Matrix([[1, 2], [3, 4]])).doit() == Matrix([[1, 3], [2, 4]]) assert adjoint(trace(Sq)) == conjugate(trace(Sq)) assert trace(adjoint(Sq)) == conjugate(trace(Sq)) assert Adjoint(Sq)[0, 1] == conjugate(Sq[1, 0]) assert Adjoint(A*B).doit() == Adjoint(B) * Adjoint(A)
def test_KroneckerProduct_trace(): kp = kronecker_product(W, Z) assert trace(kp) == trace(W)*trace(Z)
def test_rewrite(): assert isinstance(trace(A).rewrite(Sum), Sum)
def test_Trace(): assert isinstance(Trace(A), Trace) assert not isinstance(Trace(A), MatrixExpr) raises(ShapeError, lambda: Trace(C)) assert trace(eye(3)) == 3 assert trace(Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])) == 15 assert adjoint(Trace(A)) == trace(Adjoint(A)) assert conjugate(Trace(A)) == trace(Adjoint(A)) assert transpose(Trace(A)) == Trace(A) A / Trace(A) # Make sure this is possible # Some easy simplifications assert trace(Identity(5)) == 5 assert trace(ZeroMatrix(5, 5)) == 0 assert trace(2 * A * B) == 2 * trace(A * B) assert trace(A.T) == trace(A) i, j = symbols('i j') F = FunctionMatrix(3, 3, Lambda((i, j), i + j)) assert trace(F) == (0 + 0) + (1 + 1) + (2 + 2) raises(TypeError, lambda: Trace(S.One)) assert Trace(A).arg is A assert str(trace(A)) == str(Trace(A).doit())
def test_trace_constant_factor(): # Issue 9052: LHS gives Trace(MatMul(A)) assert trace(2 * A) == 2 * Trace(A) X = ImmutableMatrix([[1, 2], [3, 4]]) assert trace(MatMul(2, X)) == 10
def test_KroneckerProduct_trace(): kp = kronecker_product(W, Z) assert trace(kp) == trace(W) * trace(Z)
def test_trace_constant_factor(): # Issue 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_Trace(): assert isinstance(Trace(A), Trace) assert not isinstance(Trace(A), MatrixExpr) raises(ShapeError, lambda: Trace(C)) assert trace(eye(3)) == 3 assert trace(Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])) == 15 assert adjoint(Trace(A)) == trace(Adjoint(A)) assert conjugate(Trace(A)) == trace(Adjoint(A)) assert transpose(Trace(A)) == Trace(A) A / Trace(A) # Make sure this is possible # Some easy simplifications assert trace(Identity(5)) == 5 assert trace(ZeroMatrix(5, 5)) == 0 assert trace(OneMatrix(1, 1)) == 1 assert trace(OneMatrix(2, 2)) == 2 assert trace(OneMatrix(n, n)) == n assert trace(2*A*B) == 2*Trace(A*B) assert trace(A.T) == trace(A) i, j = symbols('i j') F = FunctionMatrix(3, 3, Lambda((i, j), i + j)) assert trace(F) == (0 + 0) + (1 + 1) + (2 + 2) raises(TypeError, lambda: Trace(S.One)) assert Trace(A).arg is A assert str(trace(A)) == str(Trace(A).doit()) assert Trace(A).is_commutative is True
def test_trace_rewrite(): assert trace(A).rewrite(Sum) == Sum(A[i, i], (i, 0, n - 1)) assert trace(eye(3)).rewrite(Sum) == 3
def test_BlockMatrix_trace(): A, B, C, D = [MatrixSymbol(s, 3, 3) for s in 'ABCD'] X = BlockMatrix([[A, B], [C, D]]) assert trace(X) == trace(A) + trace(D) assert trace(BlockMatrix([ZeroMatrix(n, n)])) == 0
def test_BlockMatrix_trace(): A, B, C, D = [MatrixSymbol(s, 3, 3) for s in "ABCD"] X = BlockMatrix([[A, B], [C, D]]) assert trace(X) == trace(A) + trace(D)
def test_BlockMatrix_trace(): A, B, C, D = map(lambda s: MatrixSymbol(s, 3, 3), "ABCD") X = BlockMatrix([[A, B], [C, D]]) assert trace(X) == trace(A) + trace(D)
def test_trace_constant_factor(): # Issue 9052: LHS gives Trace(MatMul(A)) assert trace(2*A) == 2*Trace(A) X = ImmutableMatrix([[1, 2], [3, 4]]) assert trace(MatMul(2, X)) == 10
def test_Trace_A_plus_B(): assert trace(A + B) == Trace(A) + Trace(B) assert Trace(A + B).arg == MatAdd(A, B) assert Trace(A + B).doit() == Trace(A) + Trace(B)
def test_BlockMatrix_trace(): A, B, C, D = map(lambda s: MatrixSymbol(s, 3, 3), 'ABCD') X = BlockMatrix([[A, B], [C, D]]) assert trace(X) == trace(A) + trace(D)
def test_trace_constant_factor(): # Issue 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_BlockMatrix_trace(): A, B, C, D = [MatrixSymbol(s, 3, 3) for s in 'ABCD'] X = BlockMatrix([[A, B], [C, D]]) assert trace(X) == trace(A) + trace(D)