Loading [MathJax]/jax/output/HTML-CSS/jax.js

First time here? Check out the FAQ!

Ask Your Question
0

Filtering an expression: keeping only term with even power

asked 12 years ago

Nicolas Essis-Breton gravatar image

updated 12 years ago

In the expression x2y2+xy2+x3y+5x4y4 I would like to keep only the term where the variables have even power x2y2+5x4y4

Is there a way to do it?

I look into this post.
But once I get the operands, I don't see how to analyze the variable exponent.

Preview: (hide)

3 Answers

Sort by » oldest newest most voted
1

answered 12 years ago

achrzesz gravatar image

updated 12 years ago

Is 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
Preview: (hide)
link

Comments

@archrzesz Exactly what I want, thank you. Is there a way to keep the coefficient of each monomial? For example, return 5x4y4.

Nicolas Essis-Breton gravatar imageNicolas Essis-Breton ( 12 years ago )

I've edited my answer according to the Nicolas request. Notice that my answer works for many variables. Just change var and p.

achrzesz gravatar imageachrzesz ( 12 years ago )

@achrzesz Very flexible approach. Thank you.

Nicolas Essis-Breton gravatar imageNicolas Essis-Breton ( 12 years ago )
2

answered 12 years ago

calc314 gravatar image

The 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
h
Preview: (hide)
link
1

answered 12 years ago

calc314 gravatar image

updated 12 years ago

Here 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.

Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 12 years ago

Seen: 581 times

Last updated: Oct 23 '12