Re: Replacement according to the pattern

• To: mathgroup at smc.vnet.net
• Subject: [mg77549] Re: [mg77526] Replacement according to the pattern
• From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
• Date: Wed, 13 Jun 2007 07:16:57 -0400 (EDT)
• References: <200706120518.BAA09356@smc.vnet.net>

```On 12 Jun 2007, at 14:18, tomuf at seznam.cz wrote:

> Hello,
> I wanted to parse a big expression and according to some rules
> integrate it term by term. For example, I wanted to replace symbol
>
> HPL[{1, 1, 0}, u]
>
> where HPL(list,variable) is function from package, with (note that
> this simplified example has no mathematical sense)
>
> HPL[{1, 1, 0}, u] - D[HPL[{1, 1, 0}, u], u]
>
> i.e. that function minus its derivative with respect to 'u'. I have
> loaded package which allows me to differentiate HPL. This works fine.
> But when I try to generalize this to handle all lists like {1,1,0} at
> the same time so that I don't have to type the rule for all
> combinations, the replacement doesn't work well. When I type:
>
> in = HPL[{1, 1, 0}, u]
> in /. HPL[{1, 1, 0}, u] -> HPL[{1, 1, 0}, u] - D[HPL[{1, 1, 0}, u], u]
> in /. HPL[a_, u] -> HPL[a, u] - D[HPL[a, u], u]
>
> the third line produces different result than the second line (the
> difference is in the term involving derivative). Does anyone know what
> could be wrong? Thanks in advance
>
> Best regards,
> Tomas Prochazka
>
>

Well, I think there are two and not just one relevant questions here.
One is why this happens and the second is :"what do you want to
happen?".
The first is easy. You can get the second answer to be the same as
the first by using RuleDelayed instead of Rule, i.e.

in /. HPL[a_, u] :> HPL[a, u] - D[HPL[a, u], u]

The difference between the two outputs reflects the difference
between the way Mathematica treats the function HPL[{1, 1, 0}, u]
when differentiation it with respect to u. When you use Rule as in:

in /. HPL[a_, u] -> HPL[a, u] - D[HPL[a, u], u]

D[HPL[a, u], u] is evaluated before a value is substituted for a, and
HPL is considered as a function of two variables a and u and
differentiated with respect to u. Only after that the list {1,1,0} is
substituted for a. The derivative is:

D[HPL[a, u], u] /. a -> {1, 1, 0}

Derivative[0, 1][HPL][{1, 1, 0}, u]

If you use RuleDelayed as in:

in /. HPL[a_, u] :> HPL[a, u] - D[HPL[a, u], u]

the differentiation is performed only after {1,1,0} is substituted
for a, that is, Mathematica differentiates D[HPL[{1,1,0}, u], u] a
function of two variables, the first of which is a 3-vector, with
respect to the second variable. The derivative is:

D[HPL[{1, 1, 0}, u], u]

Derivative[{0, 0, 0}, 1][HPL][{1, 1, 0}, u]

These are conceptually different and it is not clear to me which, if
any, of them corresponds to what you want. If you not not wish to
consider {1,1,0} as an argument of a function at all but only as an
index, it might be better to use the notation HPL[{1,1,0}][u]. In
this case:

in = HPL[{1, 1, 0}][u]
in /. HPL[{1, 1, 0}][u] -> HPL[{1, 1, 0}][u] - D[HPL[{1, 1, 0}][u], u]
in /. HPL[a_][u] -> HPL[a][u] - D[HPL[a][u], u]

will all give you the same answer (whether you use -> or :>).

Andrzej Kozlowski

```

• Prev by Date: Re: Slide presentation examples (v6)
• Next by Date: Re: Replacement according to the pattern
• Previous by thread: Re: Replacement according to the pattern
• Next by thread: Re: Replacement according to the pattern