Re: [Q] Implementing identities as rules
- To: mathgroup at smc.vnet.net
- Subject: [mg18666] Re: [mg18608] [Q] Implementing identities as rules
- From: "David Park" <djmp at earthlink.net>
- Date: Thu, 15 Jul 1999 01:45:52 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Kevin Jaffe wrote:
>
>During symbolic manipulations it often important to be able to use
>both sides of an identity or definition. For example, let f be a
>function defined by
>
>In[1]:= f = Function[{x, y}, Exp[x^2 + y^2]];
>
>Its partial derivative with respect to x is:
>
>In[2]:= D[f[x, y], x]
>
> 2 2
> x + y
>Out[2]= 2 E x
>
>Now, I want to recast this result in the form 2 x f[x, y], i.e. I want
>to revert to the "left-hand side" of the original definition of f[x,
>y]. How does one do this in Mathematica? I know that if I try the
>rule
>
>
>In[3]:= %2 /. Exp[a_^2 + b_^2] :> f[a, b]
>
> 2 2
> x + y
>Out[3]= 2 E x
>
>
>I get the original expression, because when the pattern is replace,
>f[a, b] is immediately evaluated to reproduce the original expression.
>(I know that the replacement occurs because if instead I use a rule
>whose right hand side cannot be evaluated further
>
>In[4]:= %2 /. Exp[a_^2 + b_^2] :> g[a, b]
>
>Out[4]= 2 x g[x, y]
>
>I get the desired result.)
>
>Is there a way to instruct Mathematica not to evaluate the expression
>after the replacement has been made?
>
>Thanks,
>
>kj0 at mailcity.com
>
There are two approaches that I know. Use HoldForm, or don't define f in a
definition but by a rule.
The first method:
f := Function[{x, y}, E^(x^2 + y^2)]
D[f[x, y], x]
% /. f[x, y] -> HoldForm[f[x, y]] giving
2*E^(x^2 + y^2)*x
2 x f[x, y]
The second method:
Clear[f];
frule = f[x, y] -> E^(x^2 + y^2);
D[f[x, y] /. frule, x]
% /. Reverse[frule] giving
2*E^(x^2 + y^2)*x
2 x f[x, y]
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/