3
$\begingroup$

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

$\endgroup$
5
  • $\begingroup$ 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 $\endgroup$
    – Lukas Lang
    Sep 11, 2022 at 19:18
  • 1
    $\begingroup$ For instance this? imgur.com/a/WPL8B2c $\endgroup$
    – Johnny
    Sep 11, 2022 at 19:21
  • 1
    $\begingroup$ F should be replaced by a function of three variables, not a function of one, imo. $\endgroup$
    – Michael E2
    Sep 11, 2022 at 19:50
  • $\begingroup$ The UPDATE is what I had in mind. Of course, it's equivalent to @Lukas's answer, that answer being the anonymous version. $\endgroup$
    – Michael E2
    Sep 11, 2022 at 20:49
  • $\begingroup$ Yeah, in hindsight your comment says the same thing $\endgroup$
    – Johnny
    Sep 11, 2022 at 20:51

2 Answers 2

3
$\begingroup$

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*)
$\endgroup$
6
  • $\begingroup$ 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 ... $\endgroup$
    – Johnny
    Sep 11, 2022 at 19:32
  • $\begingroup$ 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. $\endgroup$
    – Lacia
    Sep 11, 2022 at 19:37
  • $\begingroup$ @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] $\endgroup$
    – Lacia
    Sep 11, 2022 at 19:38
  • $\begingroup$ @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. $\endgroup$
    – Lacia
    Sep 11, 2022 at 19:42
  • $\begingroup$ I see, I thought these completely symbolic derivatives were treated as Inert internally. $\endgroup$
    – Johnny
    Sep 11, 2022 at 19:43
5
$\begingroup$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.