コード例 #1
0
    def __new__(self, *a):
        I = isinstance
        # Order elements based on type and order to insure the order is always
        # deterministic.
        rank_type = lambda a: (2*I(a,Power) + 2*I(a,Product) + 3*I(a,Symbol)
            + 4*I(a,Number))
        rank = lambda a: 10000 * rank_type(a)\
            - (a.order() if hasattr(a,'order') else 100)

        terms = list(dedup(add, sorted(a, key=rank), ht(0)))
        if len(terms) > 1:
            b = Algebra.__new__(self); b.__terms = terms
            return b
        else:
            # For the sum of one element, just return that element
            return terms[0]
コード例 #2
0
    def __new__(self, *a):
        I = isinstance
        # Order elements based on type and order to insure the order is always
        # deterministic.
        rank_type = lambda a: (2 * I(a, Power) + 2 * I(a, Product) + 3 * I(
            a, Symbol) + 4 * I(a, Number))
        rank = lambda a: 10000 * rank_type(a)\
            - (a.order() if hasattr(a,'order') else 100)

        terms = list(dedup(add, sorted(a, key=rank), ht(0)))
        if len(terms) > 1:
            b = Algebra.__new__(self)
            b.__terms = terms
            return b
        else:
            # For the sum of one element, just return that element
            return terms[0]
コード例 #3
0
    def __new__(self, *a):
        # Order the elements based upon their type and position within the
        # alphabet, to ensure the order is always deterministic
        I = isinstance
        rank_type = lambda a: (1*I(a, Number) + 2*I(a, Symbol) + 3*I(a, Power)
            + 4*I(a, Product) + 5*I(a, Sum) + 6*I(a, Function))
        rank = lambda a: 10000*rank_type(a) + (ord(str(a)) if I(a, Symbol)
            else ord(str(a.a())) if I(a, Power) and I(a.b(), Symbol)
            else 100)

        terms = list(dedup(mul, sorted(a, key=rank), 1))
        if len(terms) > 1 and terms[0] != 0:
            b = Algebra.__new__(self); b.__terms = terms
            return b
        else:
            # For the product of 1 element, just return that element
            return terms[0]
コード例 #4
0
    def __new__(self, *a):
        # Order the elements based upon their type and position within the
        # alphabet, to ensure the order is always deterministic
        I = isinstance
        rank_type = lambda a: (1 * I(a, Number) + 2 * I(a, Symbol) + 3 * I(
            a, Power) + 4 * I(a, Product) + 5 * I(a, Sum) + 6 * I(a, Function))
        rank = lambda a: 10000 * rank_type(a) + (
            ord(str(a)) if I(a, Symbol) else ord(str(a.a()))
            if I(a, Power) and I(a.b(), Symbol) else 100)

        terms = list(dedup(mul, sorted(a, key=rank), 1))
        if len(terms) > 1 and terms[0] != 0:
            b = Algebra.__new__(self)
            b.__terms = terms
            return b
        else:
            # For the product of 1 element, just return that element
            return terms[0]