Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #15268 > unrolled thread

Re: Pattern with powers

Started byAlexei Boulbitch <Alexei.Boulbitch@iee.lu>
First post2013-07-31 08:49 +0000
Last post2013-08-08 04:02 +0000
Articles 4 — 4 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Re: Pattern with powers Alexei Boulbitch <Alexei.Boulbitch@iee.lu> - 2013-07-31 08:49 +0000
    Re: Pattern with powers "Dr. Wolfgang Hintze" <weh@snafu.de> - 2013-08-01 04:14 +0000
      Re: Pattern with powers danl@wolfram.com - 2013-08-02 06:49 +0000
      Cases or Select? W Craig Carter <ccarter@MIT.EDU> - 2013-08-08 04:02 +0000

#15268 — Re: Pattern with powers

FromAlexei Boulbitch <Alexei.Boulbitch@iee.lu>
Date2013-07-31 08:49 +0000
SubjectRe: Pattern with powers
Message-ID<ktaj20$llc$1@smc.vnet.net>
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

Example 2: this would be the form I would like most
Cases[ls, (_.)*x^_?(#1 >= a & )*y^_?(#1 < a & )]
gives
{}
Here even I didn't get the dot behind the blank before the test, so it misses first powers.

Thanks in advance for any help.

Best regards,
Wolfgang



Hi, Wolfgang,

Your explanation is not quite clear. Have a look:

Clear[a, u, v, b];

a = 2;
Cases[ls, (Times[_, x, Power[y, v_]] /;
    v <= a) | (Times[_, Power[x, u_]] /; u >= a)]

{5 x^3, 15 x^2 y, 15 x y^2}

Is it, what you are after? Or this:

Clear[a, u, v, b];
a = 2;
Cases[ls, (Times[_, x, Power[y, v_]] /;
    v < a) | (Times[_, Power[x, u_]] /; u >= a)]

{5 x^3, 15 x^2 y}

??

Have fun, Alexei

Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone :  +352-2454-2566
Office fax:       +352-2454-3566
mobile phone:  +49 151 52 40 66 44

e-mail: alexei.boulbitch@iee.lu



[toc] | [next] | [standalone]


#15272

From"Dr. Wolfgang Hintze" <weh@snafu.de>
Date2013-08-01 04:14 +0000
Message-ID<ktcnaf$r2b$1@smc.vnet.net>
In reply to#15268
Am Mittwoch, 31. Juli 2013 10:49:04 UTC+2 schrieb Alexei Boulbitch:
> 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
> 
> 
> 
> Example 2: this would be the form I would like most
> 
> Cases[ls, (_.)*x^_?(#1 >= a & )*y^_?(#1 < a & )]
> 
> gives
> 
> {}
> 
> Here even I didn't get the dot behind the blank before the test, so it misses first powers.
> 
> 
> 
> Thanks in advance for any help.
> 
> 
> 
> Best regards,
> 
> Wolfgang
> 
> 
> 
> 
> 
> 
> 
> Hi, Wolfgang,
> 
> 
> 
> Your explanation is not quite clear. Have a look:
> 
> 
> 
> Clear[a, u, v, b];
> 
> 
> 
> a = 2;
> 
> Cases[ls, (Times[_, x, Power[y, v_]] /;
> 
>     v <= a) | (Times[_, Power[x, u_]] /; u >= a)]
> 
> 
> 
> {5 x^3, 15 x^2 y, 15 x y^2}
> 
> 
> 
> Is it, what you are after? Or this:
> 
> 
> 
> Clear[a, u, v, b];
> 
> a = 2;
> 
> Cases[ls, (Times[_, x, Power[y, v_]] /;
> 
>     v < a) | (Times[_, Power[x, u_]] /; u >= a)]
> 
> 
> 
> {5 x^3, 15 x^2 y}
> 
> 
> 
> ??
> 
> 
> 
> Have fun, Alexei
> 
> 
> 
> Alexei BOULBITCH, Dr., habil.
> 
> IEE S.A.
> 
> ZAE Weiergewan,
> 
> 11, rue Edmond Reuter,
> 
> L-5326 Contern, LUXEMBOURG
> 
> 
> 
> Office phone :  +352-2454-2566
> 
> Office fax:       +352-2454-3566
> 
> mobile phone:  +49 151 52 40 66 44
> 
> 
> 
> e-mail: alexei.boulbitch@iee.lu

Alexei,

thanks for your message. 

As I tried to explain, I wish to extract from the list ls all terms of the form 
c x^u y^v, with c a numerical factor, u and v integers subject to the conditions u>=a and v<a with integer a>0.

I have no problem as long as all terms in the list are "true" powers, i.e. as long as u>=2, v>=2.
Therefore I asked for a solution which also covers the values 0 and 1 for the powers.

Your first solution contains the wrong conditions, and the second one fails for a term  x^2 y^3 which is selected by your proposal but it shouldn't be selected because v>2.

Best regards,
Wolfgang

[toc] | [prev] | [next] | [standalone]


#15275

Fromdanl@wolfram.com
Date2013-08-02 06:49 +0000
Message-ID<ktfkqj$4ps$1@smc.vnet.net>
In reply to#15272
On Wednesday, July 31, 2013 11:14:07 PM UTC-5, Dr. Wolfgang Hintze wrote:
> Am Mittwoch, 31. Juli 2013 10:49:04 UTC+2 schrieb Alexei Boulbitch:
> 
> > 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
> 
> > Example 2: this would be the form I would like most
> 
> > Cases[ls, (_.)*x^_?(#1 >= a & )*y^_?(#1 < a & )]
> 
> > gives 
> 
> > {}
> 
> > Here even I didn't get the dot behind the blank before the test, so it misses first powers.
> 
> > Thanks in advance for any help. 
> 
> > Best regards,
> 
> > 
> 
> > Wolfgang 
> 
> > 
> 
> > Hi, Wolfgang,
> 
> > Your explanation is not quite clear. Have a look: 
> 
> > 
> 
> > Clear[a, u, v, b];
> 
> > a = 2; 
> 
> > Cases[ls, (Times[_, x, Power[y, v_]] /;
> 
> >     v <= a) | (Times[_, Power[x, u_]] /; u >= a)]
> 
> > {5 x^3, 15 x^2 y, 15 x y^2}
> 
> > Is it, what you are after? Or this:
> 
> > Clear[a, u, v, b];
> 
> > a = 2; 
> 
> > Cases[ls, (Times[_, x, Power[y, v_]] /; 
> 
> >     v < a) | (Times[_, Power[x, u_]] /; u >= a)] 
> 
> > {5 x^3, 15 x^2 y}
> 
> > ??
> 
> > Have fun, Alexei
> 
> > Alexei BOULBITCH, Dr., habil.
> 
> 
> Alexei,
> 
> 
> 
> thanks for your message.  
> 
> As I tried to explain, I wish to extract from the list ls all terms of the form 
> 
> c x^u y^v, with c a numerical factor, u and v integers subject to the conditions u>=a and v<a with integer a>0. 
> 
> I have no problem as long as all terms in the list are "true" powers, i.e. as long as u>=2, v>=2.
> 
> Therefore I asked for a solution which also covers the values 0 and 1 for the powers.
> 
> Your first solution contains the wrong conditions, and the second one fails for a term  x^2 y^3 which is selected by your proposal but it shouldn't be selected because v>2.
> 
> Best regards,
> 
> Wolfgang

Powers of zero, that is, missing factors, will be difficult to handle via patterns. A more natural choice, in my view, would be to define predicate functions. Here is an example.

In[19]:= s = List @@ Expand[5 (x + y)^3]

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

In[20]:= a = 1;

In[21]:= Select[s, Exponent[#, x] >= a && Exponent[#, y] <= a &]

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

Obviously this can also be done with Cases, but that seems a bit more awkward to me.

In[22]:= Cases[s, aa_ /; Exponent[aa, x] >= a && Exponent[aa, y] <= a]

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

Since "natural" and "awkward" are in the mind of the beholder, one might well hold an opinion the reverse of my own.

Daniel Lichtblau
Wolfram Research

[toc] | [prev] | [next] | [standalone]


#15288 — Cases or Select?

FromW Craig Carter <ccarter@MIT.EDU>
Date2013-08-08 04:02 +0000
SubjectCases or Select?
Message-ID<ktv58t$agp$1@smc.vnet.net>
In reply to#15272
Hello Danny,
I have the same subjective opinion, Select seems more natural than Cases in this case.
I can never remember which of the two goes with patterns and which with functions in any case--I think I've looked up the syntax on these functions more than any others.

So, I decided to run a little test:

s = List @@ Expand[5 (x + y)^100000];

ts = First@
  Timing[Do[
    Select[s, Exponent[#, x] >= 1 && Exponent[#, y] <= 1 &], {100}]]

(*roughly 75*)

tc = First@
  Timing[Do[
    Cases[s,
     aa_ /; Exponent[aa, x] >= a && Exponent[aa, y] <= a], {100}]]

(* roughly 73*)

tc/ts
 
(*roughly 1.03*)

I had guessed that the function call would have been faster... but I guessed wrong.  I don't believe there is any caching of results here, so it seems a fair comparison.

I suppose that if the function in Select had only numeric arguments, then using Compile might make Select a better choice.


Craig



On Aug 2, 13, at 2:50 AM, danl@wolfram.com wrote:

> In[20]:= a = 1;
>
> In[21]:= Select[s, Exponent[#, x] >= a && Exponent[#, y] <= a &]
>
> Out[21]= {5 x^3, 15 x^2 y}
>
> Obviously this can also be done with Cases, but that seems a bit more awkward to me.
>
> In[22]:= Cases[s, aa_ /; Exponent[aa, x] >= a && Exponent[aa, y] <= a]
>
> Out[22]= {5 x^3, 15 x^2 y}
>
> Since "natural" and "awkward" are in the mind of the beholder, one might well hold an opinion the reverse of my own.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web