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: [mg36291] Re: [mg36278] Using a Named Pattern in a Rule
  • From: Andrzej Kozlowski <andrzej at lineone.net>
  • Date: Fri, 30 Aug 2002 01:19:16 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

You are right, I realized what you want but was just too careless with 
my reply and did not look carefully at the output. Sorry.  Would not 
something like this be satisfactory:

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

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

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

and

In[4]:=
expr = f[a]*g[b]*h[c];

In[5]:=
expr /. p:(f[a]*g[b]*h[c]) :> First[p]*op[Take[p, -2]]

Out[5]=
f[a]*op[g[b]*h[c]]

Andrzej Kozlowski
Toyama International University
JAPAN




On Thursday, August 29, 2002, at 01:09  PM, David Park wrote:

> 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: DSolve for a system of 3 equations
  • Next by Date: Operating on Level Subparts of an Expression
  • Previous by thread: RE: Using a Named Pattern in a Rule
  • Next by thread: Re: Using a Named Pattern in a Rule