Beispiel #1
0
def test_bc_dist_diag():
    A = MatrixSymbol('A', n, n)
    B = MatrixSymbol('B', m, m)
    C = MatrixSymbol('C', l, l)
    X = BlockDiagMatrix(A, B, C)

    assert bc_dist(X + X).equals(BlockDiagMatrix(2 * A, 2 * B, 2 * C))
Beispiel #2
0
def test_BlockDiagMatrix():
    A = MatrixSymbol('A', n, n)
    B = MatrixSymbol('B', m, m)
    C = MatrixSymbol('C', l, l)
    M = MatrixSymbol('M', n + m + l, n + m + l)

    X = BlockDiagMatrix(A, B, C)
    Y = BlockDiagMatrix(A, 2 * B, 3 * C)

    assert X.blocks[1, 1] == B
    assert X.shape == (n + m + l, n + m + l)
    assert all(
        X.blocks[i, j].is_ZeroMatrix if i != j else X.blocks[i,
                                                             j] in [A, B, C]
        for i in range(3) for j in range(3))
    assert X.__class__(*X.args) == X

    assert isinstance(block_collapse(X.I * X), Identity)

    assert bc_matmul(X * X) == BlockDiagMatrix(A * A, B * B, C * C)
    assert block_collapse(X * X) == BlockDiagMatrix(A * A, B * B, C * C)
    # XXX: should be == ??
    assert block_collapse(X + X).equals(BlockDiagMatrix(2 * A, 2 * B, 2 * C))
    assert block_collapse(X * Y) == BlockDiagMatrix(A * A, 2 * B * B,
                                                    3 * C * C)
    assert block_collapse(X + Y) == BlockDiagMatrix(2 * A, 3 * B, 4 * C)

    # Ensure that BlockDiagMatrices can still interact with normal MatrixExprs
    assert (X * (2 * M)).is_MatMul
    assert (X + (2 * M)).is_MatAdd

    assert (X._blockmul(M)).is_MatMul
    assert (X._blockadd(M)).is_MatAdd
Beispiel #3
0
def test_block_plus_ident():
    A = MatrixSymbol('A', n, n)
    B = MatrixSymbol('B', n, m)
    C = MatrixSymbol('C', m, n)
    D = MatrixSymbol('D', m, m)
    X = BlockMatrix([[A, B], [C, D]])
    assert bc_block_plus_ident(X+Identity(m+n)) == \
            BlockDiagMatrix(Identity(n), Identity(m)) + X
Beispiel #4
0
def test_squareBlockMatrix():
    A = MatrixSymbol('A', n, n)
    B = MatrixSymbol('B', n, m)
    C = MatrixSymbol('C', m, n)
    D = MatrixSymbol('D', m, m)
    X = BlockMatrix([[A, B], [C, D]])
    Y = BlockMatrix([[A]])

    assert X.is_square

    assert (block_collapse(X + Identity(m + n)) ==
            BlockMatrix([[A + Identity(n), B], [C, D + Identity(m)]]))
    X + Identity(m + n)

    assert (X + MatrixSymbol('Q', n + m, n + m)).is_MatAdd
    assert (X * MatrixSymbol('Q', n + m, n + m)).is_MatMul

    assert block_collapse(Y.inverse()) == A.inverse()
    assert block_collapse(X.inverse()) == BlockMatrix([
        [(-B*D.inverse()*C + A).inverse(), -A.inverse()*B*(D + -C*A.inverse()*B).inverse()],
        [-(D - C*A.inverse()*B).inverse()*C*A.inverse(), (D - C*A.inverse()*B).inverse()]])

    assert isinstance(X.inverse(), Inverse)

    assert not X.is_Identity

    Z = BlockMatrix([[Identity(n), B], [C, D]])
    assert not Z.is_Identity
Beispiel #5
0
def test_Trace_MatAdd_doit():
    # See issue #9028
    X = ImmutableMatrix([[1, 2, 3]] * 3)
    Y = MatrixSymbol('Y', 3, 3)
    q = MatAdd(X, 2 * X, Y, -3 * Y)
    assert Trace(q).arg == q
    assert Trace(q).doit() == 18 - 2 * Trace(Y)
Beispiel #6
0
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(eye(3)).doit(deep=False) == eye(3)

    assert Transpose(Integer(5)).doit() == Integer(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)
Beispiel #7
0
def test_reblock_2x2():
    B = BlockMatrix([[MatrixSymbol('A_%d%d' % (i, j), 2, 2) for j in range(3)]
                     for i in range(3)])
    assert B.blocks.shape == (3, 3)

    BB = reblock_2x2(B)
    assert BB.blocks.shape == (2, 2)

    assert B.shape == BB.shape
    assert B.as_explicit() == BB.as_explicit()
Beispiel #8
0
def test_squareBlockMatrix():
    A = MatrixSymbol('A', n, n)
    B = MatrixSymbol('B', n, m)
    C = MatrixSymbol('C', m, n)
    D = MatrixSymbol('D', m, m)
    X = BlockMatrix([[A, B], [C, D]])
    Y = BlockMatrix([[A]])

    assert X.is_square

    assert (block_collapse(X + Identity(m + n)) == BlockMatrix(
        [[A + Identity(n), B], [C, D + Identity(m)]]))
    Q = X + Identity(m + n)

    assert (X + MatrixSymbol('Q', n + m, n + m)).is_MatAdd
    assert (X * MatrixSymbol('Q', n + m, n + m)).is_MatMul

    assert block_collapse(Y.I) == A.I
    assert block_collapse(X.inverse()) == BlockMatrix([[
        (-B * D.I * C + A).I, -A.I * B * (D + -C * A.I * B).I
    ], [-(D - C * A.I * B).I * C * A.I, (D - C * A.I * B).I]])

    assert isinstance(X.inverse(), Inverse)

    assert not X.is_Identity

    Z = BlockMatrix([[Identity(n), B], [C, D]])
    assert not Z.is_Identity
Beispiel #9
0
def test_blockcut():
    A = MatrixSymbol('A', n, m)
    B = blockcut(A, (n / 2, n / 2), (m / 2, m / 2))
    assert A[i, j] == B[i, j]
    assert B == BlockMatrix([[A[:n / 2, :m / 2], A[:n / 2, m / 2:]],
                             [A[n / 2:, :m / 2], A[n / 2:, m / 2:]]])

    M = ImmutableMatrix(4, 4, range(16))
    B = blockcut(M, (2, 2), (2, 2))
    assert M == ImmutableMatrix(B)

    B = blockcut(M, (1, 3), (2, 2))
    assert ImmutableMatrix(B.blocks[0, 1]) == ImmutableMatrix([[2, 3]])
Beispiel #10
0
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(Integer(5)).doit() == Integer(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)
Beispiel #11
0
from diofant.matrices.expressions.blockmatrix import (
    block_collapse, bc_matmul, bc_block_plus_ident, BlockDiagMatrix,
    BlockMatrix, bc_dist, bc_matadd, bc_transpose, blockcut, reblock_2x2,
    deblock)
from diofant.matrices.expressions import (MatrixSymbol, Identity, Inverse,
                                          trace, Transpose, det)
from diofant.matrices import Matrix, ImmutableMatrix
from diofant.core import Tuple, symbols, Expr
from diofant.functions import transpose

__all__ = ()

i, j, k, l, m, n, p = symbols('i:n, p', integer=True)
A = MatrixSymbol('A', n, n)
B = MatrixSymbol('B', n, n)
C = MatrixSymbol('C', n, n)
D = MatrixSymbol('D', n, n)
G = MatrixSymbol('G', n, n)
H = MatrixSymbol('H', n, n)
b1 = BlockMatrix([[G, H]])
b2 = BlockMatrix([[G], [H]])


def test_bc_matmul():
    assert bc_matmul(H * b1 * b2 * G) == BlockMatrix([[
        (H * G * G + H * H * H) * G
    ]])


def test_bc_matadd():
    assert bc_matadd(BlockMatrix([[G, H]]) + BlockMatrix([[H, H]])) == \
Beispiel #12
0
def test_BlockMatrix_equals():
    A, B, C, D = [MatrixSymbol(s, 3, 3) for s in 'ABCD']
    X = BlockMatrix([[A, B], [C, D]])
    assert X.equals(X) is True
    assert X.equals(Matrix([1, 2])) is False
Beispiel #13
0
def test_deblock():
    B = BlockMatrix([[MatrixSymbol('A_%d%d' % (i, j), n, n) for j in range(4)]
                     for i in range(4)])

    assert deblock(reblock_2x2(B)) == B
Beispiel #14
0
import pytest

from diofant.core import Lambda, S, symbols
from diofant.concrete import Sum
from diofant.functions import adjoint, conjugate, transpose
from diofant.matrices import eye, Matrix, ShapeError, ImmutableMatrix
from diofant.matrices.expressions import (Adjoint, Identity, FunctionMatrix,
                                          MatrixExpr, MatrixSymbol, Trace,
                                          ZeroMatrix, trace, MatPow, MatAdd,
                                          MatMul)

n = symbols('n', integer=True)
A = MatrixSymbol('A', n, n)
B = MatrixSymbol('B', n, n)
C = MatrixSymbol('C', 3, 4)


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)

    A / Trace(A)  # Make sure this is possible

    # Some easy simplifications
Beispiel #15
0
import pytest

from diofant import Basic
from diofant.matrices import ImmutableMatrix, ShapeError, eye
from diofant.matrices.expressions import MatAdd, MatMul, MatPow, MatrixSymbol


__all__ = ()

X = MatrixSymbol('X', 2, 2)
Y = MatrixSymbol('Y', 2, 2)


def test_sort_key():
    assert MatAdd(Y, X).doit().args == (X, Y)


def test_matadd():
    pytest.raises(ShapeError, lambda: X + eye(1))
    MatAdd(X, eye(1), check=False)  # not raises


def test_matadd_sympify():
    assert isinstance(MatAdd(eye(1), eye(1)).args[0], Basic)


def test_matadd_of_matrices():
    assert MatAdd(eye(2), 4*eye(2), eye(2)).doit() == ImmutableMatrix(6*eye(2))


def test_doit_args():
Beispiel #16
0
def test_is_commutative():
    A = MatrixSymbol('A', 2, 2, commutative=True)
    B = MatrixSymbol('B', 2, 2, commutative=True)
    assert (A + B).is_commutative
    assert (A + X).is_commutative is False
Beispiel #17
0
def test_negative_index():
    X = MatrixSymbol('x', 10, 10)
    assert X[-1, :] == X[9, :]
Beispiel #18
0
def test_exceptions():
    X = MatrixSymbol('x', 10, 20)
    pytest.raises(IndexError, lambda: X[0:12, 2])
    pytest.raises(IndexError, lambda: X[0:9, 22])
    pytest.raises(IndexError, lambda: X[-1:5, 2])
Beispiel #19
0
def test_BlockMatrix():
    A = MatrixSymbol('A', n, m)
    B = MatrixSymbol('B', n, k)
    C = MatrixSymbol('C', l, m)
    D = MatrixSymbol('D', l, k)
    M = MatrixSymbol('M', m + k, p)
    N = MatrixSymbol('N', l + n, k + m)
    X = BlockMatrix(Matrix([[A, B], [C, D]]))

    assert X.__class__(*X.args) == X

    # block_collapse does nothing on normal inputs
    E = MatrixSymbol('E', n, m)
    assert block_collapse(A + 2 * E) == A + 2 * E
    F = MatrixSymbol('F', m, m)
    assert block_collapse(E.T * A * F) == E.T * A * F

    assert X.shape == (l + n, k + m)
    assert X.blockshape == (2, 2)
    assert transpose(X) == BlockMatrix(Matrix([[A.T, C.T], [B.T, D.T]]))
    assert transpose(X).shape == X.shape[::-1]

    # Test that BlockMatrices and MatrixSymbols can still mix
    assert (X * M).is_MatMul
    assert X._blockmul(M).is_MatMul
    assert (X * M).shape == (n + l, p)
    assert (X + N).is_MatAdd
    assert X._blockadd(N).is_MatAdd
    assert (X + N).shape == X.shape

    E = MatrixSymbol('E', m, 1)
    F = MatrixSymbol('F', k, 1)

    Y = BlockMatrix(Matrix([[E], [F]]))

    assert (X * Y).shape == (l + n, 1)
    assert block_collapse(X * Y).blocks[0, 0] == A * E + B * F
    assert block_collapse(X * Y).blocks[1, 0] == C * E + D * F

    # block_collapse passes down into container objects, transposes, and inverse
    assert block_collapse(transpose(X * Y)) == transpose(block_collapse(X * Y))
    assert block_collapse(Tuple(X * Y, 2 * X)) == (block_collapse(X * Y),
                                                   block_collapse(2 * X))

    # Make sure that MatrixSymbols will enter 1x1 BlockMatrix if it simplifies
    Ab = BlockMatrix([[A]])
    Z = MatrixSymbol('Z', *A.shape)
    assert block_collapse(Ab + Z) == A + Z
Beispiel #20
0
from diofant.core import symbols, Integer
from diofant.functions import adjoint, conjugate, transpose
from diofant.matrices.expressions import MatrixSymbol, Adjoint, trace, Transpose
from diofant.matrices import eye, Matrix

__all__ = ()

n, m, l, k, p = symbols('n m l k p', integer=True)
A = MatrixSymbol('A', n, m)
B = MatrixSymbol('B', m, l)
C = MatrixSymbol('C', n, n)


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(Integer(5)).doit() == Integer(5)

    assert Adjoint(Matrix([[1, 2], [3, 4]])).doit() == Matrix([[1, 3], [2, 4]])

    assert adjoint(trace(Sq)) == conjugate(trace(Sq))
Beispiel #21
0
import pytest

from diofant.matrices.expressions.slice import MatrixSlice
from diofant.matrices.expressions import MatrixSymbol
from diofant.functions.elementary.integers import floor

from diofant.abc import a, b, c, d, k, l, m, n

X = MatrixSymbol('X', n, m)
Y = MatrixSymbol('Y', m, k)


def test_shape():
    B = MatrixSlice(X, (a, b), (c, d))
    assert B.shape == (b - a, d - c)


def test_entry():
    B = MatrixSlice(X, (a, b), (c, d))
    assert B[0, 0] == X[a, c]
    assert B[k, l] == X[a + k, c + l]
    pytest.raises(IndexError, lambda: MatrixSlice(X, 1, (2, 5))[1, 0])

    assert X[1::2, :][1, 3] == X[1 + 2, 3]
    assert X[:, 1::2][3, 1] == X[3, 1 + 2]


def test_on_diag():
    assert not MatrixSlice(X, (a, b), (c, d)).on_diag
    assert MatrixSlice(X, (a, b), (a, b)).on_diag
Beispiel #22
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)
Beispiel #23
0
def test_slice_of_slice():
    X = MatrixSymbol('x', 10, 10)
    assert X[2, :][:, 3][0, 0] == X[2, 3]
    assert X[:5, :5][:4, :4] == X[:4, :4]
    assert X[1:5, 2:6][1:3, 2] == X[2:4, 4]
    assert X[1:9:2, 2:6][1:3, 2] == X[3:7:2, 4]
Beispiel #24
0
def test_BlockMatrix_Determinant():
    A, B, C, D = [MatrixSymbol(s, 3, 3) for s in 'ABCD']
    X = BlockMatrix([[A, B], [C, D]])

    assert isinstance(det(X), Expr)
Beispiel #25
0
from diofant.abc import n
from diofant.matrices.expressions import MatrixSymbol
from diofant.matrices.expressions.diagonal import DiagonalMatrix, DiagonalOf

__all__ = ()

x = MatrixSymbol('x', n, 1)
X = MatrixSymbol('X', n, n)
D = DiagonalMatrix(x)
d = DiagonalOf(X)


def test_DiagonalMatrix():
    assert D.shape == (n, n)
    assert D[1, 2] == 0
    assert D[1, 1] == x[1, 0]


def test_DiagonalOf():
    assert d.shape == (n, 1)
    assert d[2, 0] == X[2, 2]
Beispiel #26
0
import pytest

from diofant.core import symbols
from diofant.matrices.expressions import MatrixSymbol, Inverse
from diofant.matrices import eye, Identity, ShapeError

n, m, l = symbols('n m l', integer=True)
A = MatrixSymbol('A', n, m)
B = MatrixSymbol('B', m, l)
C = MatrixSymbol('C', n, n)
D = MatrixSymbol('D', n, n)
E = MatrixSymbol('E', m, n)


def test_inverse():
    pytest.raises(ShapeError, lambda: Inverse(A))
    pytest.raises(ShapeError, lambda: Inverse(A * B))

    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