# How to replace generic function with another one?

I have complicated expression in terms of derivatives of a multivariable function F[x,y,z] say

D[F[x,y,z],x]^2  + D[F[x,y,z],y]^2


I want to change the generic function F[x,y,z] to another completely generic, now single-variable function G[x] . How do I do this?

I have tried /.{F->G[x]} ,/.{F[x,y,z]->G[x]} ,/.{F->Function[x,G]} /.{F[x,y,z]->Function[x,G]} ...

Obviously I can’t afford to individually replace the various derivatives by hand...

UPDATE: While I got some good answers, I also managed to find what I was looking for! The rule /.{ F -> Function[{x,y,z},G[x]] } does the trick and (for me at least) is significantly more intuitive than @lilyric and @lukaslang ’s answer

• Can you show a concrete example of how you tries to do the replacement? Concretely, I would have expected your second attempt to (at least partially) work Sep 11, 2022 at 19:18
• For instance this? imgur.com/a/WPL8B2c Sep 11, 2022 at 19:21
• F should be replaced by a function of three variables, not a function of one, imo. Sep 11, 2022 at 19:50
• The UPDATE is what I had in mind. Of course, it's equivalent to @Lukas's answer, that answer being the anonymous version. Sep 11, 2022 at 20:49
• Yeah, in hindsight your comment says the same thing Sep 11, 2022 at 20:51

The FullForm of derivative is

D[F[x,y,z],x]//FullForm
(*Derivative[1,0,0][F][x,y,z]*)


so you can either prevent (say Inactivate the derivative

Inactivate[D[F[x,y,z],x]^2  + D[F[x,y,z],y]^2,D]/.{F[x,y,z]:>G[x]}//Activate
(*(G^\[Prime])[x]^2*)


or match the FullForm of derivative

D[F[x,y,z],x]^2  + D[F[x,y,z],y]^2/.{Derivative[a_,b_,c_][F][x,y,z]:>D[G[x],{x,a},{y,b},{z,c}]}
(*(G^\[Prime])[x]^2*)

• Thanks, this seems to work. Could you please explain how/why the second works? Coming from Maple this seems needlessly complicated, I would’ve expected a simple ‘ F[x,y,z]->G[x] ‘ does the trick ... Sep 11, 2022 at 19:32
• 1. when evaluating foo/.F[x,y,z]->G[x], MMA search all subparts of foo (from the outer level to inner) matching F[x,y,z] exactly. Sep 11, 2022 at 19:37
• @Johnny 2 in MMA, the order of evaluation matters. D[foo,x]/.F[x,y,z]->G[x] will first evaluate D and get stuffs like Derivative[1,0,0][F][x,y,z], which doesn't match with F[x,y,z] Sep 11, 2022 at 19:38
• @lukaslang has provided a simpler solution. For the order of evaluation, the documentation tutorial/Evaluation is a good start, see also the many nice answers on this forum. Sep 11, 2022 at 19:42
• I see, I thought these completely symbolic derivatives were treated as Inert internally. Sep 11, 2022 at 19:43

You can use F->(G[#]&):

D[F[x,y,z],x]+D[F[x,y,z],y] /. F->(G[#]&)
(* G'[x] *)


The reason this works (and your attempts don't) is that D[F[x,y,z],x] evaluates to Derivative[0,0,1][F][x,y,z]. So all the F[...]->... type rules don't even match. What you need is to replace the lonely F with G: this can in principle be done with just F->G, but of course that doesn't tell Mathematica about the number of arguments. G[#]& fixes this: This is a function that ignores all but the first argument, so G[#]&[x,y,z] results in G[x]. The rest is up to Mathematica, which manages to simplify the resulting derivates.

• I see! What If I wanted to replace it with G[y,z] tho? Sep 11, 2022 at 19:38
• F->(G[#2,#3]&) should work Sep 11, 2022 at 19:47
• Ah okay, that makes sense. Sep 11, 2022 at 19:55