Esempio n. 1
0
def test_matexpr():
    assert (x*A).shape == A.shape
    assert (x*A).__class__ == MatMul
    assert 2*A - A - A == ZeroMatrix(*A.shape)
    assert (A*B).shape == (n, l)
    assert A.equals(ZeroMatrix(3, 3)) is None

    # issue diofant/diofant#469
    expr = Eq(C, D)
    assert simplify(expr) == expr
Esempio n. 2
0
def test_zero_power():
    z1 = ZeroMatrix(n, n)
    assert MatPow(z1, 3).doit() == z1
    pytest.raises(ValueError, lambda: MatPow(z1, -1).doit())
    assert MatPow(z1, 0).doit() == Identity(n)
    assert MatPow(z1, n).doit() == z1
    pytest.raises(ValueError, lambda: MatPow(z1, -2).doit())
    z2 = ZeroMatrix(4, 4)
    assert MatPow(z2, n).doit() == z2
    pytest.raises(ValueError, lambda: MatPow(z2, -3).doit())
    assert MatPow(z2, 2).doit() == z2
    assert MatPow(z2, 0).doit() == Identity(4)
    pytest.raises(ValueError, lambda: MatPow(z2, -1).doit())
Esempio n. 3
0
def test_Zero_power():
    z1 = ZeroMatrix(n, n)
    assert z1**4 == z1
    pytest.raises(ValueError, lambda: z1**-2)
    assert z1**0 == Identity(n)
    assert MatPow(z1, 2).doit() == z1**2
    pytest.raises(ValueError, lambda: MatPow(z1, -2).doit())
    z2 = ZeroMatrix(3, 3)
    assert MatPow(z2, 4).doit() == z2**4
    pytest.raises(ValueError, lambda: z2**-3)
    assert z2**3 == MatPow(z2, 3).doit()
    assert z2**0 == Identity(3)
    pytest.raises(ValueError, lambda: MatPow(z2, -1).doit())
Esempio n. 4
0
def test_BlockMatrix():
    n, m = symbols('n m', integer=True)
    X = MatrixSymbol('X', n, n)
    Y = MatrixSymbol('Y', m, m)
    Z = MatrixSymbol('Z', n, m)
    B = BlockMatrix([[X, Z], [ZeroMatrix(m, n), Y]])
    assert str(B) == "Matrix([\n[X, Z],\n[0, Y]])"
Esempio n. 5
0
def test_addition():
    A = MatrixSymbol('A', n, m)
    B = MatrixSymbol('B', n, m)

    assert isinstance(A + B, MatAdd)
    assert (A + B).shape == A.shape
    assert isinstance(A - A + 2*B, MatMul)

    pytest.raises(ShapeError, lambda: A + B.T)
    pytest.raises(TypeError, lambda: A + 1)
    pytest.raises(TypeError, lambda: 5 + A)
    pytest.raises(TypeError, lambda: 5 - A)

    assert A + ZeroMatrix(n, m) - A == ZeroMatrix(n, m)
    with pytest.raises(TypeError):
        ZeroMatrix(n, m) + Integer(0)  # pylint: disable=expression-not-assigned
Esempio n. 6
0
def test_Trace():
    assert isinstance(Trace(A), Trace)
    assert not isinstance(Trace(A), MatrixExpr)
    pytest.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)

    assert isinstance(A / Trace(A), MatrixExpr)

    # 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)

    pytest.raises(TypeError, lambda: Trace(1))

    assert Trace(A).arg is A

    assert str(trace(A)) == str(Trace(A).doit())
Esempio n. 7
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)
Esempio n. 8
0
def test_multiplication():
    A = MatrixSymbol('A', n, m)
    B = MatrixSymbol('B', m, l)
    C = MatrixSymbol('C', n, n)

    assert (2*A*B).shape == (n, l)

    assert (A*0*B) == ZeroMatrix(n, l)

    pytest.raises(ShapeError, lambda: B*A)
    assert (2*A).shape == A.shape

    assert A * ZeroMatrix(m, m) * B == ZeroMatrix(n, l)

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

    assert B/2 == Rational(1, 2)*B
    pytest.raises(NotImplementedError, lambda: 2/B)

    A = MatrixSymbol('A', n, n)
    B = MatrixSymbol('B', n, n)
    assert Identity(n) * (A + B) == A + B
Esempio n. 9
0
def test_block_index():
    I = Identity(3)
    Z = ZeroMatrix(3, 3)
    B = BlockMatrix([[I, I], [I, I]])
    e3 = ImmutableMatrix(eye(3))
    BB = BlockMatrix([[e3, e3], [e3, e3]])
    assert B[0, 0] == B[3, 0] == B[0, 3] == B[3, 3] == 1
    assert B[4, 3] == B[5, 1] == 0

    BB = BlockMatrix([[e3, e3], [e3, e3]])
    assert B.as_explicit() == BB.as_explicit()

    BI = BlockMatrix([[I, Z], [Z, I]])

    assert BI.as_explicit().equals(eye(6))
Esempio n. 10
0
def test_ZeroMatrix():
    A = MatrixSymbol('A', n, m)
    Z = ZeroMatrix(n, m)

    assert A + Z == A
    assert A*Z.T == ZeroMatrix(n, n)
    assert Z*A.T == ZeroMatrix(n, n)
    assert A - A == ZeroMatrix(*A.shape)

    assert not Z

    assert transpose(Z) == ZeroMatrix(m, n)
    assert Z.conjugate() == Z

    assert ZeroMatrix(n, n)**0 == Identity(n)
    with pytest.raises(ShapeError):
        Z**0  # pylint: disable=pointless-statement
    with pytest.raises(ShapeError):
        Z**2  # pylint: disable=pointless-statement
Esempio n. 11
0
def test_eval_determinant():
    assert det(Matrix()) == 1
    assert det(Identity(n)) == 1
    assert det(ZeroMatrix(n, n)) == 0
    assert det(Transpose(A)) == det(A)
Esempio n. 12
0
def test_ZeroMatrix_doit():
    Znn = ZeroMatrix(Add(n, n, evaluate=False), n)
    assert isinstance(Znn.rows, Add)
    assert Znn.doit() == ZeroMatrix(2*n, n)
    assert isinstance(Znn.doit().rows, Mul)
Esempio n. 13
0
def test_any_zeros():
    assert any_zeros(MatMul(A, ZeroMatrix(m, k), evaluate=False)) == \
        ZeroMatrix(n, k)
Esempio n. 14
0
def test_matexpr():
    assert (x * A).shape == A.shape
    assert (x * A).__class__ == MatMul
    assert 2 * A - A - A == ZeroMatrix(*A.shape)
    assert (A * B).shape == (n, l)