1 | initial version |
A tad awkwardly :
sage: sum([v[0]*SR("u")^v[1] for v in (c0 * (x+1) + c1*(x+1) + c2*c1*x - c0*c1*
(x+1)).subs(x+1==SR("u")).coefficients(SR("u"))]).subs(SR("u")==x+1)
c1*c2*x - (c0*c1 - c0 - c1)*(x + 1)
This can be programmed as :
def mycollect(e1, e2):
"""
Collects e2 coefficients in e1, even when e2 is not a factor of
some of the terms of e1
"""
g = maxima_calculus.gensym()._sage_()
while g in e1.variables(): g = maxima_calculus.gensym()._sage_()
return sum([v[0]*g^v[1]
for v in e1.subs(e2==g).coefficients(g)]).subs(g==e2)
Test :
sage: mycollect(c0 * (x+1) + c1*(x+1) + c2*c1*x - c0*c1*(x+1),x+1)
c1*c2*x - (c0*c1 - c0 - c1)*(x + 1)
This (with some possible generalizations) might become a new method for symbolic expressions. Advice ?
HTH,