flatten sum opts: avoid optimisations over constants for now (TODO!)
@@ -507,7 +507,8 @@ def normalized_numexpr(expr):
# pre-process sum, to fold in nested subtractions and const*Exprs, e.g. x - y + 2*(z+r)
if expr.name == "sum" and \
- any(isinstance(a, Operator) and (a.name == "-" or _wsum_should(a)) for a in expr.args):
+ all(isinstance(a, Operator) for a in expr.args) and \
+ any((a.name == "-" or _wsum_should(a)) for a in expr.args):
we = [_wsum_make(a) for a in expr.args]
w = [wi for w,_ in we for wi in w]
e = [ei for _,e in we for ei in e]
@@ -519,7 +520,8 @@ def normalized_numexpr(expr):
# while here, avoid creation of auxiliary variables for compatible operators -/sum/wsum
i = 0
while(i < len(sub_exprs)): # can dynamically change
- if sub_exprs[i].name in ['-', 'sum', 'wsum']:
+ if isinstance(sub_exprs[i], Operator) and sub_exprs[i].name in ['-', 'sum', 'wsum'] and \
+ all(isinstance(a, Expression) for a in sub_exprs[i].args): # TODO: avoid constants for now...
w,e = _wsum_make(sub_exprs[i])
# insert in place, and next iteration over same 'i' again
weights[i:i+1] = [weights[i]*wj for wj in w]
