Esempio n. 1
0
def block_collapse(expr):
    """Evaluates a block matrix expression

    >>> from sympy import MatrixSymbol, BlockMatrix, symbols, \
                          Identity, Matrix, ZeroMatrix, block_collapse
    >>> n,m,l = symbols('n m l')
    >>> X = MatrixSymbol('X', n, n)
    >>> Y = MatrixSymbol('Y', m ,m)
    >>> Z = MatrixSymbol('Z', n, m)
    >>> B = BlockMatrix([[X, Z], [ZeroMatrix(m, n), Y]])
    >>> print B
    [X, Z]
    [0, Y]

    >>> C = BlockMatrix([[Identity(n), Z]])
    >>> print C
    [I, Z]

    >>> print block_collapse(C*B)
    [X, Z + Z*Y]
    """
    rule = canon(typed({MatAdd: do_one(bc_matadd, bc_block_plus_ident),
                        MatMul: do_one(bc_matmul, bc_dist),
                        MatPow: bc_matpow,
                        BlockMatrix: bc_unpack}))
    result = rule(expr)
    try:
        return result.canonicalize()
    except AttributeError:
        return result
Esempio n. 2
0
def block_collapse(expr):
    """Evaluates a block matrix expression

    >>> from sympy import MatrixSymbol, BlockMatrix, symbols, \
                          Identity, Matrix, ZeroMatrix, block_collapse
    >>> n,m,l = symbols('n m l')
    >>> X = MatrixSymbol('X', n, n)
    >>> Y = MatrixSymbol('Y', m ,m)
    >>> Z = MatrixSymbol('Z', n, m)
    >>> B = BlockMatrix([[X, Z], [ZeroMatrix(m, n), Y]])
    >>> print B
    [X, Z]
    [0, Y]

    >>> C = BlockMatrix([[Identity(n), Z]])
    >>> print C
    [I, Z]

    >>> print block_collapse(C*B)
    [X, Z + Z*Y]
    """
    rule = canon(
        typed({
            MatAdd: do_one(bc_matadd, bc_block_plus_ident),
            MatMul: do_one(bc_matmul, bc_dist),
            BlockMatrix: bc_unpack
        }))
    result = rule(expr)
    try:
        return result.doit()
    except AttributeError:
        return result
Esempio n. 3
0
def test_typed():
    class A(Basic):
        pass
    class B(Basic):
        pass
    rmzeros = rm_id(lambda x: x == 0)
    rmones  = rm_id(lambda x: x == 1)
    remove_something = typed({A: rmzeros, B: rmones})

    assert remove_something(A(0, 1)) == A(1)
    assert remove_something(B(0, 1)) == B(0)
Esempio n. 4
0
def test_typed():
    class A(Basic):
        pass

    class B(Basic):
        pass

    rmzeros = rm_id(lambda x: x == 0)
    rmones = rm_id(lambda x: x == 1)
    remove_something = typed({A: rmzeros, B: rmones})

    assert remove_something(A(0, 1)) == A(1)
    assert remove_something(B(0, 1)) == B(0)