Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Pattern with powers
Replies: 7   Last Post: Aug 8, 2013 12:02 AM

 Search Thread: Advanced Search

 Messages: [ Previous | Next ]
 Bill Rowe Posts: 1,647 Registered: 3/14/08
Re: Pattern with powers
Posted: Jul 31, 2013 4:48 AM
 Plain Text Reply

On 7/30/13 at 6:40 AM, weh@snafu.de (Dr. Wolfgang Hintze) wrote:

>I must admit that I am an absolute beginner in patterns, as I cannot
>cope with a little problem with patterns consisting of powers of
>variables x and y.

>Specifically, I would like to select from a list all terms of the
>form

>c x^u y^v (numerical coefficient c times x to the power u times y to
>the power v)

>where u and v are allowed to take the values 0 and 1.

>How can I do this using Cases?

>I have already accomplished the first non trivial step using _.
>(blank followed by a dot) in order to get first powers of the
>variables:

>ls = List@@Expand[5 (x + y)^3] {5*x^3, 15*x^2*y, 15*x*y^2, 5*y^3}

>Example 1 a = 2; Cases[ls, (_.)*x^(u_.)*y^(v_.) /; u >= a && v < a]
>gives {15*x^2*y} but misses the term 5*x^3

It is not necessary to convert the polynomial to a List since
Cases can be applied directly to the polynomial. That is:

In[1]:= ls = Expand[5 (x + y)^3];
a = 2; Cases[ls, (_.)*x^(u_.)*y^(v_.) /; u >= a && v < a]

Out[2]= {15*x^2*y}

yields the same result as you get below after making ls a List

Why use Cases? It appears you want an output with with all terms
of the form a x^u y^v with v either 1 or 0. Although this isn't
what you state initially, it does seem to accurately describe
what you indicate is your desired result. If this is what you
want then how about

In[3]:= CoefficientList[ls, y][[;; 2]] {1, y}

Out[3]= {5*x^3, 15*x^2*y}

Date Subject Author
7/30/13 Dr. Wolfgang Hintze
7/31/13 Bill Rowe
8/1/13 Dr. Wolfgang Hintze
7/31/13 Alexei Boulbitch
8/1/13 Dr. Wolfgang Hintze
8/2/13 Daniel Lichtblau
8/8/13 W. Craig Carter

© The Math Forum at NCTM 1994-2018. All Rights Reserved.