ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 25 Jun 2012 03:44:01 -0500collect multiple variables and simplify their coefficientshttp://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!
Sun, 09 Oct 2011 09:50:44 -0500http://ask.sagemath.org/question/8369/collect-multiple-variables-and-simplify-their-coefficients/Answer by Jason Grout for <p>Hi there!</p>
<p>In Mathematica you can do the following: Collect[expr, {var1, var2, var3}, Simplify]</p>
<p>This transforms</p>
<p>expr = var1<em>expr11 + var2</em>expr22 + var1<em>var2</em>expr12 + ...</p>
<p>to</p>
<p>var1<em>Simplify[expr11] + var2</em>Simplify[expr22] + var1<em>var2</em>Simplify[expr12] + ...</p>
<p>I was wondering if there is an easy way to do this in Sage.</p>
<p>So my questions are:</p>
<p>1) How can you collect multiple variables?</p>
<p>2) How can you manipulate their coefficients? (each coefficient is independent)</p>
<p>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)</p>
<p>Thanks for your help!</p>
http://ask.sagemath.org/question/8369/collect-multiple-variables-and-simplify-their-coefficients/?answer=12743#post-id-12743Here are two helpful functions:
sage: var('x,y,z')
(x, y, z)
sage: expr=x^2*y+2*x*y*z+54*z-3*x*z
sage: expr.collect(x)
x^2*y + (2*y*z - 3*z)*x + 54*z
sage: expr.coefficients(x)
[[54*z, 0], [2*y*z - 3*z, 1], [y, 2]]
Mon, 10 Oct 2011 00:35:52 -0500http://ask.sagemath.org/question/8369/collect-multiple-variables-and-simplify-their-coefficients/?answer=12743#post-id-12743Comment by nablaoperator for <p>Here are two helpful functions:</p>
<pre><code>sage: var('x,y,z')
(x, y, z)
sage: expr=x^2*y+2*x*y*z+54*z-3*x*z
sage: expr.collect(x)
x^2*y + (2*y*z - 3*z)*x + 54*z
sage: expr.coefficients(x)
[[54*z, 0], [2*y*z - 3*z, 1], [y, 2]]
</code></pre>
http://ask.sagemath.org/question/8369/collect-multiple-variables-and-simplify-their-coefficients/?comment=21146#post-id-21146Okay, thank you! It seems I have to do it manually then.Mon, 10 Oct 2011 01:02:21 -0500http://ask.sagemath.org/question/8369/collect-multiple-variables-and-simplify-their-coefficients/?comment=21146#post-id-21146Answer by pang for <p>Hi there!</p>
<p>In Mathematica you can do the following: Collect[expr, {var1, var2, var3}, Simplify]</p>
<p>This transforms</p>
<p>expr = var1<em>expr11 + var2</em>expr22 + var1<em>var2</em>expr12 + ...</p>
<p>to</p>
<p>var1<em>Simplify[expr11] + var2</em>Simplify[expr22] + var1<em>var2</em>Simplify[expr12] + ...</p>
<p>I was wondering if there is an easy way to do this in Sage.</p>
<p>So my questions are:</p>
<p>1) How can you collect multiple variables?</p>
<p>2) How can you manipulate their coefficients? (each coefficient is independent)</p>
<p>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)</p>
<p>Thanks for your help!</p>
http://ask.sagemath.org/question/8369/collect-multiple-variables-and-simplify-their-coefficients/?answer=13756#post-id-13756maxima allows to collect multiple terms, so it is possible to do so in Sage using:
sage: f = 4*x*y + x*z + 20*y^2 + 21*y*z + 4*z^2 + x^2*y^2*z^2
sage: fm = f.maxima_methods()
sage: fm.collectterms(x,y)
x^2*y^2*z^2 + 4*x*y + x*z + 20*y^2 + 21*y*z + 4*z^2
sage: fm.collectterms(x)
x^2*y^2*z^2 + (4*y + z)*x + 20*y^2 + 21*y*z + 4*z^2
from there you could use the operands() method to simplify each term separately. Is this enough?
Can you assume that your expression is polynomial in x and y?Mon, 25 Jun 2012 03:44:01 -0500http://ask.sagemath.org/question/8369/collect-multiple-variables-and-simplify-their-coefficients/?answer=13756#post-id-13756