# Results in terms of a function

Let's assume I have a function $f(x)=\exp(10x^2)$, i.e. f[x_]:= Exp[10*x^2]. When I differentiate $f$ by evaluating the expression D[f[x],x], I get the result:

20 E^(10 x^2) x


which is OK, but I would like to see

20 f[x] x


Is there any way to force Mathematica to do this substitution?

I do not necessarily expect Mathematica to output this right away, but at least is there any command to replace expressions with predefined functions where possible?

This is just a simple example, but clearly you get the point. If then one works with much more complicated expressions, this can save a lot of space and supports nice analytical expressions.

f[x_]:= Exp[10*x^2]

D[f[x], x] /. f[x] :> HoldForm@f[x]


Update

B[t_, T_] := 1/a*(1 - E^(-a*(T - t)));

A[t_, T_] :=
1/a^2*(a*b - 1/2*sigma^2)*(B[t, T] - T + t) - (sigma^2*(B[t, T])^2)/(4*a);

P[t_, T_, r_] := Exp[A[t, T] - B[t, T]*r];

D[P[t, T, r], {r, 1}] /. B[t, T] -> HoldForm[B[t, T]] /. P[t, T, r] -> HoldForm[P[t, T, r]]


gives the desired

• Alternatively, ReplaceRepeated can be used for nested functions, for instance, D[P[t, T, r], {r, 1}] //. {B[t, T] -> HoldForm[B[t, T]], P[t, T, r] -> HoldForm[P[t, T, r]]}. – bbgodfrey Jan 10 '16 at 16:15
• Thanks both, this is wonderful! – user2743931 Jan 10 '16 at 16:58
• @user2743931 try it with f[x_]:= 2 Exp[10*x^2] and you'll see that this method works with the examples given through sheer coincidence. I'm afraid it is not an easy problem to solve properly. – Oleksandr R. Jan 10 '16 at 19:01
• @user2743931 it is nothing to do with HoldForm. It is because the approach seeks to find instances where the function in question appears verbatim in the output. For most possible functions and outputs this will never happen. The answer/comments of AccidentalFourierTransform illustrate very well the problems with this idea, and I think make a strong case that at the very least you need to supply an ansatz. – Oleksandr R. Jan 10 '16 at 19:24
• @user2743931 it should be obvious according to the counterexample I mentioned. f[x_]:= 2 Exp[10*x^2]; D[f[x], x] produces 40 Exp[10 x^2] x. There is no 2 Exp[10 x^2] to be found in that, and so the literal search for the value of f[x] fails. – Oleksandr R. Jan 10 '16 at 19:53

Here's a pretty snazzy method:

DifferentialRootReduce[Exp[10 x^2], {x, 0}]
(* -> DifferentialRoot[Function[{y, x}, {-20 x y[x] + y'[x] == 0, y[0] == 1}]][x] *)


(The actual output uses \[FormalY] and \[FormalX], which I replaced with the corresponding normal letters for clarity.)

Though as noted in another answer, one can conjure any number of ODEs that have a given analytic function as a solution, one can usually derive a unique linear ODE; DifferentialRootReduce[] is the function intended for finding that linear ODE, if there is one.

• thank you for introducing me to DifferentialRoot[] :) – ubpdqn Apr 18 '16 at 12:07

Note that any function satisfies an infinite number of ODE's, so the answer to your question is non-unique. The following code will do what you want for some specific cases (it works for your example):

f[x_] := Exp[10 x^2]
ansatz = Inactivate[D[f[x], x] + a x f[x] + b f[x] + c, f];
Solve[Activate[#==0&/@(D[ansatz,{x,#}]&/@Range[0,2]/.x->0),f],{a,b,c}]
ansatz == 0/.%


If you want to use a more general ansatz, with $n$ unknowns (in my code, $n=3$ for $a,b,c$), you just have to modify ansatz, and change Range[0,2] to Range[0,n-1].

Of course, any suggestion to my code will be highly well-recieved!

• It doesn't necessarily need to be related to differenation. I can just simply have an expression: 20 E^(10 x^2) x and replace it with 20 f[x] x, where f[x_]:= Exp[10*x^2] – user2743931 Jan 10 '16 at 14:49
• in that case its not easy to see how should we handle other examples: if $f(x)=x$, then what is the expected output for $3x^2$? is it $3xf(x)$? or $3f(x)^2$? – AccidentalFourierTransform Jan 10 '16 at 15:01
• The third option, is what I would expect. Replace all $x$ by $f(x)$... – user2743931 Jan 10 '16 at 15:12
• I still think the answer is highly non-unique. Maybe the example $f(x)=x$ was too simple to illustrate my point. Take $f(x)=\sqrt{1-x^2}$. Then $x^3\to x(1-f(x)^2)$? or $x^3\to (1-f(x)^2)^{3/2}$? both seem very valid to me... If you dont expect any $x$ in the output, then $20x\mathrm e^{10x^2}\to 2\sqrt{10\log(f(x))}f(x)$, and not what you wrote.(I hope Im not being annoying: if eldo's answer address your concerns then its pointless for us to keep on arguing abot this; if his answer is not enough for you, then I wont be able to help unless I know precisely what the expected output is) – AccidentalFourierTransform Jan 10 '16 at 15:30
• I got your point, but maybe that is overly complicated. Any of these expressions would do. The same problem could occur if I set, say, $f(x) = 2x$ and $g(x)= 10x$. Then an expression $z(x)=20x$ could be any combination of the two (e.g. $z(x)=2g(x)$ or $z(x)=g(x)+5f(x)$, etc...). eldo's suggestion works great for me. – user2743931 Jan 10 '16 at 17:12

You can achieve this by giving a definition for the derivative only. For example:

f' ^= 20 # f[#]&


Then:

f[x]
f'[x]
f''[x]
f'''[x]


f[x]

20 x f[x]

20 f[x] + 400 x^2 f[x]

1200 x f[x] + 8000 x^3 f[x]

Not really an answer, but some thoughts:

• For something like this, you don't need :=. You can do f[x_] = Exp[10*x^2]

• This probably isn't the answer you're looking for, but:

D[f[x],x] /. f[x] -> "f[x]"

gives you what you want. Of course, the second f[x] here is a string, not a function.