On Friday, June 6, 2014 1:34:04 PM UTC+2, Andrei Berceanu wrote: > > The unflatten_mul function factorized the 2, but not the g, i.e. it returns > > 2(g*|psi1|**2 + g*|psi2|**2) > > instead of > > 2g*(|psi1|**2 + |psi2|**2) >
Try to do so: def get_unflattener(m): m_args = list(m.args) def unflattener(node): new_args = [] sub_add_args = [] for arg in node.args: common = [i for i in arg.args if i in m_args] if set(common) == set(m_args): sub_add_args.append(arg) else: new_args.append(arg) sub_add = Add(*[i.func(*[j for j in i.args if j not in m_args]) for i in sub_add_args], evaluate=False) return Add(*(new_args + [Mul(*(m_args + [sub_add]), evaluate =False)]), evaluate=False) return unflattener get_unflattener(-2*g)(expr) -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To post to this group, send email to sympy@googlegroups.com. Visit this group at http://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/2e010cf4-7827-4d8c-9fe9-c485e0b77710%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.