MathGroup Archive 2011

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

Search the Archive

Re: Apply a rule to an expression only once

  • To: mathgroup at smc.vnet.net
  • Subject: [mg116362] Re: Apply a rule to an expression only once
  • From: Daniel Lichtblau <danl at wolfram.com>
  • Date: Sat, 12 Feb 2011 05:17:30 -0500 (EST)
  • References: <201102110920.EAA08165@smc.vnet.net>

Guido Walter Pettinari wrote:
> Dear Andrzej,
> 
> thank you for your answer.  Your example works indeed, but I would rather prefer not to modify my original rule, which is much more complex than the one I quoted in the example.  I want the rule to stay the most general as possible, since it is used elsewhere in my package and a redefinition will break it.
> 
> My package transforms mathematical equations from a real [x,y,z] space to a Fourier [kx,ky,kz] space.  The main ingredient is a rule that applies the necessary derivative & coordinate transformations on single terms (e.g. Derivative[1,0,0][ f [x,y,z] ] -> I kx f [kx,ky,kx] ).  This works perfectly fine on linear terms, but things get more involved when dealing with quadratic terms.   In fact, the Fourier transform of a quadratic term, say f [x,y,z] g [x,y,z], is a convolution term of the form C [ f [k1,k2,k3] g[q1,q2,q3] ], where C[] denotes a convolution integral over k1,k2,k3 and q1,q2,q3.
> 
> Aside from mathematical details, I would like to know a way to change the expression:
> 
> expr = f [ x ] g [ x ] h [ x ] .....
> 
> into
> 
> f [ k1 ] g [ k2 ] h [ k3 ] ....
> 
> by applying in a proper way the following rule:
> 
> rule[k_] := f_@x :> f@k .
> 
> No redefinitions of the rule are allowed :-)
> 
> I thought that a solution could be an option for Replace (say, OnlyFirst) that applies the rule only to the first occurrence of the pattern:
> 
> expr // Replace [ #, rule[k1], OnlyFirst->True ]& // Replace [ #, rule[k2], OnlyFirst->True ]& // Replace [ #, rule[k3], OnlyFirst->True ]& // ...
> 
> but I do not know how to implement the "OnlyFirst" bit.
> 
> Please note that (i) I do not care which function gets which dependence, e.g. f[k2]g[k3]h[k1] is equally good for me, and (ii) I do not know the names of the functions, I only recognize them by their coordinate dependence (usually x,y,z).
> 
> Thank you again for your consideration!
> 
> Best regards,
> 
> Guido
> [...]

Not sure if this is quite what you want. But it might give an idea about 
how to get the rule to alter itself as you go. Requires RuleDelayed 
rather than Rule.

In[59]:= Module[{a = 0},
  f[x] g[x] h[x] /. x :> (a++; ToExpression["k" ~~ ToString[a]])]

Out[59]= f[k1] g[k2] h[k3]

Daniel Lichtblau
Wolfram Research


  • Prev by Date: Re: changing variable in an equation
  • Next by Date: Re: Apply a rule to an expression only once
  • Previous by thread: Re: Apply a rule to an expression only once
  • Next by thread: Re: Apply a rule to an expression only once