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.Thu, 20 Sep 2012 13:21:38 -0500how to factorize an expression with constant variables ?http://ask.sagemath.org/question/9291/how-to-factorize-an-expression-with-constant-variables/Hello,
I would like to factorize an expression with sage that contains constant variables (i.e. parameters), but I cannot figure out how to do that.
Here is an example : `x`, `y` are variables and `A` is a parameter
var('A x y')
f = A*x + x + A^2*exp(y) + y
print (f.factor())
Sage returns `A^2*e^y + A*x + x + y`.
Instead, I would like that sage returns `(A+1)*x + (A^2+1)*exp(y)`.
How to do that ?
Thank you
Mon, 03 Sep 2012 01:35:03 -0500http://ask.sagemath.org/question/9291/how-to-factorize-an-expression-with-constant-variables/Answer by calc314 for <p>Hello,
I would like to factorize an expression with sage that contains constant variables (i.e. parameters), but I cannot figure out how to do that.</p>
<p>Here is an example : <code>x</code>, <code>y</code> are variables and <code>A</code> is a parameter</p>
<pre><code>var('A x y')
f = A*x + x + A^2*exp(y) + y
print (f.factor())
</code></pre>
<p>Sage returns <code>A^2*e^y + A*x + x + y</code>.
Instead, I would like that sage returns <code>(A+1)*x + (A^2+1)*exp(y)</code>.</p>
<p>How to do that ?
Thank you</p>
http://ask.sagemath.org/question/9291/how-to-factorize-an-expression-with-constant-variables/?answer=13976#post-id-13976To get the coefficient of each monomial, try using the `list` command.
R.<A> = QQ[]
R.<x,y> = PolynomialRing(QQ[A],order='deglex')
f3 = A*x^2 + x^2 + A^2*x + 2*x + x*y+3*x^2*y
list(f3)
This gives:
[(3, x^2*y), (A + 1, x^2), (1, x*y), (A^2 + 2, x)]
I'm not sure if this is exactly what you want, though.
Thu, 20 Sep 2012 13:21:38 -0500http://ask.sagemath.org/question/9291/how-to-factorize-an-expression-with-constant-variables/?answer=13976#post-id-13976Answer by mazkime for <p>Hello,
I would like to factorize an expression with sage that contains constant variables (i.e. parameters), but I cannot figure out how to do that.</p>
<p>Here is an example : <code>x</code>, <code>y</code> are variables and <code>A</code> is a parameter</p>
<pre><code>var('A x y')
f = A*x + x + A^2*exp(y) + y
print (f.factor())
</code></pre>
<p>Sage returns <code>A^2*e^y + A*x + x + y</code>.
Instead, I would like that sage returns <code>(A+1)*x + (A^2+1)*exp(y)</code>.</p>
<p>How to do that ?
Thank you</p>
http://ask.sagemath.org/question/9291/how-to-factorize-an-expression-with-constant-variables/?answer=14048#post-id-14048Thank you for your answer and sorry for my late answer.
'collect' works pretty well in the example I gave :
var('A x y')
f1 = A*x + x + A^2*exp(y) + exp(y)
print f1.collect(A).collect(x).collect(e^y)
--> (A + 1)*x + (A^2 + 1)*e^y
but unfortunately it also performs factorization with cross-products
var('A x y')
f2 = A*x^2 + x^2 + A^2*x + 2*x + x*y
print f2.collect(A).collect(x*y).collect(x)
--> (A + 1)*x^2 + (A^2 + y + 2)*x
whereas I would like to get the coefficient of each monomial x^i*y^j, as obtained with the following example
R.<A> = QQ[]
R.<x,y> = PolynomialRing(QQ[A],order='deglex')
f3 = A*x^2 + x^2 + A^2*x + 2*x + x*y
print f3
--> (A + 1)*x^2 + x*y + (A^2 + 2)*x
Is there a solution to do so ?
ThanksWed, 19 Sep 2012 20:45:55 -0500http://ask.sagemath.org/question/9291/how-to-factorize-an-expression-with-constant-variables/?answer=14048#post-id-14048Answer by calc314 for <p>Hello,
I would like to factorize an expression with sage that contains constant variables (i.e. parameters), but I cannot figure out how to do that.</p>
<p>Here is an example : <code>x</code>, <code>y</code> are variables and <code>A</code> is a parameter</p>
<pre><code>var('A x y')
f = A*x + x + A^2*exp(y) + y
print (f.factor())
</code></pre>
<p>Sage returns <code>A^2*e^y + A*x + x + y</code>.
Instead, I would like that sage returns <code>(A+1)*x + (A^2+1)*exp(y)</code>.</p>
<p>How to do that ?
Thank you</p>
http://ask.sagemath.org/question/9291/how-to-factorize-an-expression-with-constant-variables/?answer=13998#post-id-13998Try `(f.collect(A)).collect(x)`.
Tue, 04 Sep 2012 08:30:02 -0500http://ask.sagemath.org/question/9291/how-to-factorize-an-expression-with-constant-variables/?answer=13998#post-id-13998