Пример #1
0
 def ident_remove(expr):
     """ Remove identities """
     ids = map(isid, expr.args)
     if sum(ids) == 0:  # No identities. Common case
         return expr
     elif sum(ids) != len(ids):  # there is at least one non-identity
         return new(expr.__class__,
                    *[arg for arg, x in zip(expr.args, ids) if not x])
     else:
         return new(expr.__class__, expr.args[0])
Пример #2
0
 def ident_remove(expr):
     """ Remove identities """
     ids = map(isid, expr.args)
     if sum(ids) == 0:           # No identities. Common case
         return expr
     elif sum(ids) != len(ids):  # there is at least one non-identity
         return new(expr.__class__,
                    *[arg for arg, x in zip(expr.args, ids) if not x])
     else:
         return new(expr.__class__, expr.args[0])
Пример #3
0
 def conglomerate(expr):
     """ Conglomerate together identical args x + x -> 2x """
     groups = sift(expr.args, key)
     counts = dict((k, sum(map(count, args))) for k, args in groups.items())
     newargs = [combine(cnt, mat) for mat, cnt in counts.items()]
     if set(newargs) != set(expr.args):
         return new(type(expr), *newargs)
     else:
         return expr
Пример #4
0
 def conglomerate(expr):
     """ Conglomerate together identical args x + x -> 2x """
     groups = sift(expr.args, key)
     counts = dict((k, sum(map(count, args))) for k, args in groups.items())
     newargs = [combine(cnt, mat) for mat, cnt in counts.items()]
     if set(newargs) != set(expr.args):
         return new(type(expr), *newargs)
     else:
         return expr
Пример #5
0
def flatten(expr, new=new):
    """ Flatten T(a, b, T(c, d), T2(e)) to T(a, b, c, d, T2(e)) """
    cls = expr.__class__
    args = []
    for arg in expr.args:
        if arg.__class__ == cls:
            args.extend(arg.args)
        else:
            args.append(arg)
    return new(expr.__class__, *args)
Пример #6
0
def flatten(expr):
    """ Flatten T(a, b, T(c, d), T2(e)) to T(a, b, c, d, T2(e)) """
    cls = expr.__class__
    args = []
    for arg in expr.args:
        if arg.__class__ == cls:
            args.extend(arg.args)
        else:
            args.append(arg)
    return new(expr.__class__, *args)
Пример #7
0
 def __add__(self, item):
   return new(self, [*self, item])
Пример #8
0
 def all_rl(expr):
     if is_leaf(expr):
         return expr
     else:
         args = map(rule, expr.args)
         return new(type(expr), *args)
Пример #9
0
 def top_down_rl(expr):
     newexpr = rule(expr)
     if newexpr.is_Atom:
         return newexpr
     return new(newexpr.__class__, *map(top_down_rl, newexpr.args))
Пример #10
0
 def bottom_up_rl(expr):
     if expr.is_Atom:
         return rule(expr)
     else:
         return rule(new(expr.__class__, *map(bottom_up_rl, expr.args)))
Пример #11
0
 def bottom_up_rl(expr):
     if is_leaf(expr):
         return rule(expr)
     else:
         return rule(new(type(expr), *map(bottom_up_rl, expr.args)))
Пример #12
0
 def __mul__(self, other):
   return new(self, zip(self, other))
Пример #13
0
 def __rmul__(self, other):
   return new(self, zip(other, self))
Пример #14
0
 def __rand__(self, other):
   return new(self, [*other, *self])
Пример #15
0
 def __and__(self, other):
   return new(self, [*self, *other])
Пример #16
0
 def top_down_rl(expr):
     newexpr = rule(expr)
     if is_leaf(newexpr):
         return newexpr
     return new(type(newexpr), *map(top_down_rl, newexpr.args))
Пример #17
0
 def bottom_up_rl(expr):
     if is_leaf(expr):
         return rule(expr)
     else:
         return rule(new(type(expr), *map(bottom_up_rl, expr.args)))
Пример #18
0
 def top_down_rl(expr):
     newexpr = rule(expr)
     if is_leaf(newexpr):
         return newexpr
     return new(type(newexpr), *map(top_down_rl, newexpr.args))
Пример #19
0
 def sort_rl(expr):
     return new(expr.__class__, *sorted(expr.args, key=key))
Пример #20
0
 def __radd__(self, item):
   return new(self, [item, *self])
Пример #21
0
 def bottom_up_rl(expr):
     if not isinstance(expr, Basic) or expr.is_Atom:
         return rule(expr)
     else:
         return rule(new(expr.__class__, *map(bottom_up_rl, expr.args)))
Пример #22
0
 def top_down_rl(expr):
     newexpr = rule(expr)
     if not isinstance(newexpr, Basic) or newexpr.is_Atom:
         return newexpr
     return new(newexpr.__class__, *map(top_down_rl, newexpr.args))
Пример #23
0
 def sort_rl(expr):
     return new(expr.__class__, *sorted(expr.args, key=key))
Пример #24
0
 def all_rl(expr):
     if is_leaf(expr):
         return expr
     else:
         args = map(rule, expr.args)
         return new(type(expr), *args)