Exemple #1
0
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)
Exemple #2
0
def test_MatPow():
    A = MatrixSymbol('A', n, n)

    AA = MatPow(A, 2)
    assert AA.exp == 2
    assert AA.base == A
    assert (A**n).exp == n

    assert A**0 == Identity(n)
    assert A**1 == A
    assert A**2 == AA
    assert A**-1 == Inverse(A)
    assert A**Rational(1, 2) == sqrt(A)
    pytest.raises(ShapeError, lambda: MatrixSymbol('B', 3, 2)**2)
Exemple #3
0
def test_xxinv():
    assert xxinv(MatMul(D, Inverse(D), D, evaluate=False)) == \
        MatMul(Identity(n), D, evaluate=False)
Exemple #4
0
def test_inverse():
    pytest.raises(ShapeError, lambda: Inverse(A))
    pytest.raises(ShapeError, lambda: Inverse(A * B))
    pytest.raises(TypeError, lambda: Inverse(1))

    assert Inverse(C).shape == (n, n)
    assert Inverse(A * E).shape == (n, n)
    assert Inverse(E * A).shape == (m, m)
    assert Inverse(C).inverse() == C
    assert isinstance(Inverse(Inverse(C)), Inverse)

    assert C.inverse().inverse() == C

    assert C.inverse() * C == Identity(C.rows)

    assert Identity(n).inverse() == Identity(n)
    assert (3 * Identity(n)).inverse() == Identity(n) / 3

    # Simplifies Muls if possible (i.e. submatrices are square)
    assert (C * D).inverse() == D.inverse() * C.inverse()
    # But still works when not possible
    assert isinstance((A * E).inverse(), Inverse)

    assert Inverse(eye(3)).doit() == eye(3)
    assert Inverse(eye(3)).doit(deep=False) == eye(3)

    assert det(Inverse(C)) == 1 / det(C)