2

So, I have my function, such as

f[x_,y_]:=(1-x/y)^(1/2);

This function enters a big expression that contains variables x and y, as well as the function f. Now I have to differentiate that expression with respect to either x or y (or both). Evaluating the result, Mathematica will substitute each occurrence of f[x,y] with the definition above. For this particular function, its derivative will involve the factor of

(1-x/y)^(-1/2),

i.e.

1/f[x,y].

When simplifying the result, how can I tell Mathematica to use this fact? I know there options to Simplify like TransformationFunctions, ExludedForms, ComplexityFunction, but the information and examples of the beginner-level on these options are rather scarce.

ADDENDUM

I think first I should have asked a simpler question.

In[1]:= Simplify[G m/r^2, G m == A]

Out[1]= A/r^2

But:

In[2]:= Simplify[G m/r^2, G m/r^2 == A]

Out[2]= G m/r^2

Why is output not simply A?

ThisGuy
  • 121
  • 3

2 Answers2

4

Instead of giving a DownValues for f, you could give definitions for it's derivative:

Clear[f];
f /: Derivative[1, 0][f] = -1/(2 f[#1, #2] y)&;
f /: Derivative[0, 1][f] = x / (2 f[#1, #2] y^2)&;

Then, the following derivatives use f[x, y] in the output:

D[f[x, y], x] //TeXForm

$-\frac{1}{2 y f(x,y)}$

D[f[x, y], y] //TeXForm

$\frac{x}{2 y^2 f(x,y)}$

D[f[x, y] y + f[x, z] x + f[y, x] z, x] //TeXForm

$\frac{x z}{2 y^2 f(y,x)}-\frac{x}{2 y f(x,z)}-\frac{1}{2 f(x,y)}+f(x,z)$

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
  • Interesting that you get the higher derivatives for free; D[f[x, y], {x, 2}]. – Edmund Nov 06 '17 at 21:57
  • Is there a more general way? I gave a particular example, but I need a general solution, when I have f[x_, y_]:=whatever function of x and y, and I want to replace in a given expression each combination of x and y that yields this function with the symbol f. – ThisGuy Nov 07 '17 at 03:54
3

You can define f this way

frule = {f -> (Sqrt[1 - #1/#2] &)}

Then use it in any expression, such as

D[f[x, y], x] + f[x, y] /. frule
(*Sqrt[1-x/y]-1/(2 y Sqrt[1-x/y])*)
Bill Watts
  • 8,217
  • 1
  • 11
  • 28