ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 27 Jul 2017 18:18:33 +0200collect variables buried in an expressionhttps://ask.sagemath.org/question/38417/collect-variables-buried-in-an-expression/Suppose we have
var('Pmean,alpha,M,Nb,D,H,c');
sol3= [M == 1/4*(2*D*Nb*Pmean - (D*H*alpha + D*H)*c)/((alpha + 1)*c)]
How would you collect the coefficients of D*H in the second term?
Specifically, once the collection is done, how could the factored version of the expression be returned?
I know from using other computer algebra systems that it can be taken all the way to this:
M==1/4*D*(-H+(2*Nb*Pmean)/((1+alpha)*c))Chris ChiassonThu, 27 Jul 2017 18:18:33 +0200https://ask.sagemath.org/question/38417/Collecting coefficients of derivativeshttps://ask.sagemath.org/question/38214/collecting-coefficients-of-derivatives/ I have a complicated expression involving derivatives of a function, and I'd like to collect all of the first, second, third, etc. derivatives. For example, my expression starts:
a1*a2*a3*x*f(x) + a1*a2*x^2*diff(f(x), x) + a1*a3*x^2*diff(f(x), x) + a2*a3*x^2*diff(f(x), x)
and I would like to rewrite that part as
a1*a2*a3*x*f(x) + (a1*a2*x^2*+ a1*a3*x^2+ a2*a3*x^2)*diff(f(x), x)
How do I tell Sage to do this?UrsulaMon, 10 Jul 2017 16:41:23 +0200https://ask.sagemath.org/question/38214/collect is not ordering termshttps://ask.sagemath.org/question/9542/collect-is-not-ordering-terms/I was a bit surprised by the following behaviour:
sage: var('c0 c1 c2 x0 x1 x')
(c0, c1, c2, x0, x1, x)
sage: (c0+(x-x0)*(c1+(x-x1)*c2)).collect(x)
c2*x^2 - c2*x*x0 - c2*x*x1 + (c2*x1 - c1)*x0 + c1*x + c0
I assumed that `collect` would order my terms into coefficients for powers of `x`. At the very least, I'd have expected
c2*x^2 - c2*x*x0 - c2*x*x1 + c1*x + (c2*x1 - c1)*x0 + c0
| x^2 | x x x | 1 |
Although my real goal would have been
c2*x^2 - (c2*x0 - c2*x1 + c1)*x + (c2*x1*x0 - c1*x0 + c0)
I see from [the documentation](http://www.sagemath.org/doc/reference/sage/symbolic/expression.html#sage.symbolic.expression.Expression.collect) that there is no description at all what `collect` does, except returning a symbolic expression. Am I missing the point of that method?
I know I can get at the coefficients as a list using the `coeffs` method, but I'd prefer the form as a sum. It seems that even turning that back into a sum, my terms get reordered:
sage: sum([a*x^p for a, p in (c0+(x-x0)*(c1+(x-x1)*c2)).coeffs(x)])
c2*x^2 + (c2*x1 - c1)*x0 - (c2*x0 + c2*x1 - c1)*x + c0
This indicates that the problem might not be in `collect` itself, but rather in the way symbolic expressions are stored and printed.
My best solution currently is a manually computed string:
sage: print(' + '.join(['({})*{}'.format(a.expand(), x^p) for a, p in
(c0+(x-x0)*(c1+(x-x1)*c2)).coeffs(x)]))
(c2*x0*x1 - c1*x0 + c0)*1 + (-c2*x0 - c2*x1 + c1)*x + (c2)*x^2
Is there a better way to achieve this result?
MvGSat, 17 Nov 2012 18:02:50 +0100https://ask.sagemath.org/question/9542/collect multiple variables and simplify their coefficientshttps://ask.sagemath.org/question/8369/collect-multiple-variables-and-simplify-their-coefficients/Hi there!
In Mathematica you can do the following: Collect[expr, {var1, var2, var3}, Simplify]
This transforms
expr = var1*expr11 + var2*expr22 + var1*var2*expr12 + ...
to
var1*Simplify[expr11] + var2*Simplify[expr22] + var1*var2*Simplify[expr12] + ...
I was wondering if there is an easy way to do this in Sage.
So my questions are:
1) How can you collect multiple variables?
2) How can you manipulate their coefficients? (each coefficient is independent)
3*) How can you print the result in a pretty way? (e.g. collecting the expression by coefficients and printing each term in a new line)
Thanks for your help!
nablaoperatorSun, 09 Oct 2011 16:50:44 +0200https://ask.sagemath.org/question/8369/