コード例 #1
0
ファイル: test_strat.py プロジェクト: FireJade/sympy
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)
コード例 #2
0
ファイル: test_strat.py プロジェクト: skolwind/sympy
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)
コード例 #3
0
ファイル: matmul.py プロジェクト: lucasvinhas/sympy
def remove_ids(mul):
    """ Remove Identities from a MatMul

    This is a modified version of sympy.rules.rm_id.
    This is necesssary because MatMul may contain both MatrixExprs and Exprs
    as args.

    See Also
    --------
        sympy.rules.rm_id
    """
    # Separate Exprs from MatrixExprs in args
    factor, mmul = mul.as_coeff_mmul()
    # Apply standard rm_id for MatMuls
    result = rm_id(lambda x: x.is_Identity is True)(mmul)
    if result != mmul:
        return newmul(factor, *result.args)  # Recombine and return
    else:
        return mul
コード例 #4
0
ファイル: matmul.py プロジェクト: lucasvinhas/sympy
    """ Remove Identities from a MatMul

    This is a modified version of sympy.rules.rm_id.
    This is necesssary because MatMul may contain both MatrixExprs and Exprs
    as args.

    See Also
    --------
        sympy.rules.rm_id
    """
    # Separate Exprs from MatrixExprs in args
    factor, mmul = mul.as_coeff_mmul()
    # Apply standard rm_id for MatMuls
    result = rm_id(lambda x: x.is_Identity is True)(mmul)
    if result != mmul:
        return newmul(factor, *result.args)  # Recombine and return
    else:
        return mul

def factor_in_front(mul):
    factor, matrices = mul.as_coeff_matrices()
    if factor != 1:
        return newmul(factor, *matrices)
    return mul

rules = (any_zeros, remove_ids, xxinv, unpack, rm_id(lambda x: x == 1),
         factor_in_front, flatten)

canonicalize = exhaust(condition(lambda x: isinstance(x, MatMul),
                                 do_one(*rules)))
コード例 #5
0
ファイル: matadd.py プロジェクト: FireJade/sympy
        return MatAdd(*[Trace(arg) for arg in self.args])

    def canonicalize(self):
        return canonicalize(self)

def validate(*args):
    if not all(arg.is_Matrix for arg in args):
        raise TypeError("Mix of Matrix and Scalar symbols")
    A = args[0]
    for B in args[1:]:
        if A.shape != B.shape:
            raise ShapeError("Matrices %s and %s are not aligned"%(A, B))

factor_of = lambda arg: arg.as_coeff_mmul()[0]
matrix_of = lambda arg: unpack(arg.as_coeff_mmul()[1])
def combine(cnt, mat):
    from matmul import MatMul
    if cnt == 1:
        return mat
    else:
        return cnt * mat

rules = (rm_id(lambda x: x == 0 or isinstance(x, ZeroMatrix)),
         unpack,
         flatten,
         glom(matrix_of, factor_of, combine),
         sort(str))

canonicalize = exhaust(condition(lambda x: isinstance(x, MatAdd),
                                 do_one(*rules)))
コード例 #6
0
ファイル: matmul.py プロジェクト: nthorne/sympy
    """ Remove Identities from a MatMul

    This is a modified version of sympy.rules.rm_id.
    This is necesssary because MatMul may contain both MatrixExprs and Exprs
    as args.

    See Also
    --------
        sympy.rules.rm_id
    """
    # Separate Exprs from MatrixExprs in args
    factor, mmul = mul.as_coeff_mmul()
    # Apply standard rm_id for MatMuls
    result = rm_id(lambda x: x.is_Identity is True)(mmul)
    if result != mmul:
        return newmul(factor, *result.args)  # Recombine and return
    else:
        return mul


def factor_in_front(mul):
    factor, matrices = mul.as_coeff_matrices()
    if factor != 1:
        return newmul(factor, *matrices)
    return mul


rules = (any_zeros, remove_ids, xxinv, unpack, rm_id(lambda x: x == 1), factor_in_front, flatten)

canonicalize = exhaust(condition(lambda x: isinstance(x, MatMul), do_one(*rules)))
コード例 #7
0
ファイル: matadd.py プロジェクト: skolwind/sympy
    def canonicalize(self):
        return canonicalize(self)


def validate(*args):
    if not all(arg.is_Matrix for arg in args):
        raise TypeError("Mix of Matrix and Scalar symbols")
    A = args[0]
    for B in args[1:]:
        if A.shape != B.shape:
            raise ShapeError("Matrices %s and %s are not aligned" % (A, B))


factor_of = lambda arg: arg.as_coeff_mmul()[0]
matrix_of = lambda arg: unpack(arg.as_coeff_mmul()[1])


def combine(cnt, mat):
    from matmul import MatMul
    if cnt == 1:
        return mat
    else:
        return cnt * mat


rules = (rm_id(lambda x: x == 0 or isinstance(x, ZeroMatrix)), unpack, flatten,
         glom(matrix_of, factor_of, combine), sort(str))

canonicalize = exhaust(
    condition(lambda x: isinstance(x, MatAdd), do_one(*rules)))