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.Tue, 23 Oct 2012 07:35:57 +0200Filtering an expression: keeping only term with even powerhttps://ask.sagemath.org/question/9456/filtering-an-expression-keeping-only-term-with-even-power/In the expression
$$
x^2 y^2+x y^2+x^3y + 5 x^4 y^4
$$
I would like to keep only the term where the variables have even power
$$
x^2y^2+5 x^4 y^4
$$
Is there a way to do it?
I look into this [post](http://ask.sagemath.org/question/1747/extract-terms-from-a-sum?answer=2598#2598).
But once I get the operands, I don't see how to analyze the variable exponent.Mon, 22 Oct 2012 19:26:25 +0200https://ask.sagemath.org/question/9456/filtering-an-expression-keeping-only-term-with-even-power/Answer by calc314 for <p>In the expression
$$
x^2 y^2+x y^2+x^3y + 5 x^4 y^4
$$
I would like to keep only the term where the variables have even power
$$
x^2y^2+5 x^4 y^4
$$</p>
<p>Is there a way to do it?</p>
<p>I look into this <a href="http://ask.sagemath.org/question/1747/extract-terms-from-a-sum?answer=2598#2598">post</a>. <br/>
But once I get the operands, I don't see how to analyze the variable exponent.</p>
https://ask.sagemath.org/question/9456/filtering-an-expression-keeping-only-term-with-even-power/?answer=14172#post-id-14172The following also works, although it might not be as flexible as the answer by @achrzesz, which handles things nicely.
x, y = var('x, y')
f(x,y)=x^2*y^2-4*x^2*y+2*y^2+4*x*y^4-3*x^4*y^6
g(x,y)=(f(x,y)+f(-x,y))/2
h(x,y)=(g(x,y)+g(x,-y))/2
hTue, 23 Oct 2012 04:27:21 +0200https://ask.sagemath.org/question/9456/filtering-an-expression-keeping-only-term-with-even-power/?answer=14172#post-id-14172Answer by calc314 for <p>In the expression
$$
x^2 y^2+x y^2+x^3y + 5 x^4 y^4
$$
I would like to keep only the term where the variables have even power
$$
x^2y^2+5 x^4 y^4
$$</p>
<p>Is there a way to do it?</p>
<p>I look into this <a href="http://ask.sagemath.org/question/1747/extract-terms-from-a-sum?answer=2598#2598">post</a>. <br/>
But once I get the operands, I don't see how to analyze the variable exponent.</p>
https://ask.sagemath.org/question/9456/filtering-an-expression-keeping-only-term-with-even-power/?answer=14171#post-id-14171Here is an answer using the `list` command and some list comprehensions.
var('x y')
R.<x,y>=PolynomialRing(RR)
q=x^2*y^2+x+x^2*y+y^2
terms=list(q)
ans=[t[0]*t[1] for t in terms if (t[1].degree(x) % 2)==0 and (t[1].degree(y) % 2==0)]
sum(ans)
This will also keep the coefficients.Mon, 22 Oct 2012 20:46:14 +0200https://ask.sagemath.org/question/9456/filtering-an-expression-keeping-only-term-with-even-power/?answer=14171#post-id-14171Answer by achrzesz for <p>In the expression
$$
x^2 y^2+x y^2+x^3y + 5 x^4 y^4
$$
I would like to keep only the term where the variables have even power
$$
x^2y^2+5 x^4 y^4
$$</p>
<p>Is there a way to do it?</p>
<p>I look into this <a href="http://ask.sagemath.org/question/1747/extract-terms-from-a-sum?answer=2598#2598">post</a>. <br/>
But once I get the operands, I don't see how to analyze the variable exponent.</p>
https://ask.sagemath.org/question/9456/filtering-an-expression-keeping-only-term-with-even-power/?answer=14170#post-id-14170Is that what you need?
var('x y');
p=(5*x^2*y^2+x*y^2+x^3*y+x^4*y^4).polynomial(QQ)
sum([v[0]*v[1] for v in p if forall(v[1].exponents()[0],lambda x:is_even(x))[0]])
#x^4*y^4 + 5*x^2*y^2
Mon, 22 Oct 2012 20:30:05 +0200https://ask.sagemath.org/question/9456/filtering-an-expression-keeping-only-term-with-even-power/?answer=14170#post-id-14170Comment by Nicolas Essis-Breton for <p>Is that what you need?</p>
<pre><code>var('x y');
p=(5*x^2*y^2+x*y^2+x^3*y+x^4*y^4).polynomial(QQ)
sum([v[0]*v[1] for v in p if forall(v[1].exponents()[0],lambda x:is_even(x))[0]])
#x^4*y^4 + 5*x^2*y^2
</code></pre>
https://ask.sagemath.org/question/9456/filtering-an-expression-keeping-only-term-with-even-power/?comment=18832#post-id-18832@achrzesz Very flexible approach. Thank you.Tue, 23 Oct 2012 07:35:57 +0200https://ask.sagemath.org/question/9456/filtering-an-expression-keeping-only-term-with-even-power/?comment=18832#post-id-18832Comment by Nicolas Essis-Breton for <p>Is that what you need?</p>
<pre><code>var('x y');
p=(5*x^2*y^2+x*y^2+x^3*y+x^4*y^4).polynomial(QQ)
sum([v[0]*v[1] for v in p if forall(v[1].exponents()[0],lambda x:is_even(x))[0]])
#x^4*y^4 + 5*x^2*y^2
</code></pre>
https://ask.sagemath.org/question/9456/filtering-an-expression-keeping-only-term-with-even-power/?comment=18837#post-id-18837@archrzesz Exactly what I want, thank you. Is there a way to keep the coefficient of each monomial? For example, return $5 x^4 y^4$.Mon, 22 Oct 2012 20:42:54 +0200https://ask.sagemath.org/question/9456/filtering-an-expression-keeping-only-term-with-even-power/?comment=18837#post-id-18837Comment by achrzesz for <p>Is that what you need?</p>
<pre><code>var('x y');
p=(5*x^2*y^2+x*y^2+x^3*y+x^4*y^4).polynomial(QQ)
sum([v[0]*v[1] for v in p if forall(v[1].exponents()[0],lambda x:is_even(x))[0]])
#x^4*y^4 + 5*x^2*y^2
</code></pre>
https://ask.sagemath.org/question/9456/filtering-an-expression-keeping-only-term-with-even-power/?comment=18834#post-id-18834I've edited my answer according to the Nicolas request.
Notice that my answer works for many variables.
Just change var and p.Tue, 23 Oct 2012 03:14:50 +0200https://ask.sagemath.org/question/9456/filtering-an-expression-keeping-only-term-with-even-power/?comment=18834#post-id-18834