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: Apply a rule to an expression only once
Replies: 7   Last Post: Mar 9, 2011 7:00 AM

 Messages: [ Previous | Next ]
 Guido Walter Pettinari Posts: 29 From: Italy & UK Registered: 3/24/10
Re: Apply a rule to an expression only once
Posted: Mar 9, 2011 7:00 AM

Thank you very much for all the answer! All of them were very
interesting and taught me something. In the end I adopted Ray's
solution, with a slight modification in order to prevent an error if
the rule doesn't apply:

If [
Position[expr, rule[[1]]] === {},
expr,
MapAt[ReplaceAll[#, rule] &, expr, Position[expr, rule[[1]]][[1]]]]
]

Cheers,

Guido

On Feb 12, 10:18 am, Ray Koopman <koop...@sfu.ca> wrote:
> On Feb 11, 1:20 am, Guido Walter Pettinari <coccoinom...@gmail.com>
> wrote:
>
>
>
>
>
>
>
>
>

> > Dear Andrzej,
>
> > 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).

>
> replaceFirst[expr_, rule_] := MapAt[ Replace[#, rule]&,
> expr, Position[expr,rule[[1]]][[1]]]
>
> expr = f[x] g[x] h[x];
>
> rule[k_] = f_[Except[k1|k2|k3]] :> f[k];
>
> Fold[replaceFirst, expr, rule/@{k1,k2,k3}]
>
> f[k1] g[k2] h[k3]

Date Subject Author
2/10/11 Guido Walter Pettinari
2/11/11 lshifr@gmail.com
2/11/11 DrMajorBob
2/11/11 Andrzej Kozlowski
2/11/11 Bob Hanlon
2/11/11 Guido Walter Pettinari
2/12/11 Ray Koopman
3/9/11 Guido Walter Pettinari