MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Using a Named Pattern in a Rule

  • To: mathgroup at smc.vnet.net
  • Subject: [mg36287] RE: [mg36278] Using a Named Pattern in a Rule
  • From: "David Park" <djmp at earthlink.net>
  • Date: Fri, 30 Aug 2002 01:19:12 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Andrzej,

That doesn't work because I don't want to square the 3. Of course, I could
divide by p but not in my actual example. In my actual example I am not
squaring but doing a MetricSimplify tensor operation and the whole purpose
is to operate on only two of four factors. I can do it by giving a specific
pattern but not by using a general pattern.

Here is a better example.

expr = f[a]g[b]h[c];

This works...

expr /. g[b]h[c] :> op[g[a]h[c]]
f[a] op[g[a] h[c]]

This even works with a more general pattern but is a silly way to do it...

expr /. g[_]h[_] :> op[g[a]h[c]]
f[a] op[g[a] h[c]]

This is what I want to do, but again it doesn't work:

expr /. p_. (sub : g[_]h[_]) :> p op[sub]
f[a] g[b] h[c]

I have to be able to obtain a name for the match to the g[_]h[_] pattern.

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/



From: Andrzej Kozlowski [mailto:andrzej at lineone.net]
To: mathgroup at smc.vnet.net

The presence of 3* in your formula prevents the match pattern you want
form taking place since:

In[11]:=
FullForm[3*f[x]*g[y]]

Out[11]//FullForm=
Times[3,f[x],g[y]]

use instead:


In[12]:=
expr=3*f[x]*g[y]+2+x^2;

In[13]:=
expr /. a:((p_.)*f[_]*g[_]) :> p*a^2

Out[13]=
2 + x^2 + 27*f[x]^2*g[y]^2



Andrzej Kozlowski
Toyama International University
JAPAN



On Thursday, August 29, 2002, at 06:38  AM, David Park wrote:

> I would like to match a named pattern in an expression and then square
> the
> result. But my attempt fails.
>
> Clear[f, g, x, y, a]
> expr = 3*f[x]*g[y] + 2 + x^2;
>
> expr /. a:(f[_]*g[_]) :> a^2
> 2 + x^2 + 3*f[x]*g[y]
>
> If I drop the name on the pattern, it matches - but it doesn't do what
> I
> want.
>
> expr /. f[_]*g[_] :> a^2
> 2 + 3*a^2 + x^2
>
> How can I name such a pattern and use it on the rhs of a rule?
>
> David Park
> djmp at earthlink.net
> http://home.earthlink.net/~djmp/
>
>
>
>



  • Prev by Date: Re: Using a Named Pattern in a Rule
  • Next by Date: Re: matching nested heads
  • Previous by thread: Re: Using a Named Pattern in a Rule
  • Next by thread: RE: Using a Named Pattern in a Rule