How to change symbols in differential equation with a single rule?

Problem

Suppose I have a differential equation such as

$\qquad f'(x)+f(x)=0$

and I want to change it into

$\qquad y'+y=0$

My Method

Using rule = f[x] -> y, I got y + f'[x] == 0

Question

How do I get y' + y == 0 with a single rule. I don't want to use the literal and verbose rule1 = {f[x] -> y, f'[x] -> y'}?

• Not a sigle rule so doesn't count :) f'[x] + f[x] == 0 /. {f[___] -> y, Derivative[n__][f][___] :> Derivative[n][y]} – Kuba Sep 27 '15 at 6:42
• Related: (32651) – Mr.Wizard Sep 27 '15 at 8:49
• @halirutan - I agree and have deleted my comment. – Bob Hanlon Sep 27 '15 at 16:22

Your question suggests that you are not aware that f[x] and f'[x] have very different internal representations.

FullForm/@{f[x],f'[x]}
(* {f[x],Derivative[1][f][x]} *)


Matching those two expressions in a single rule would lead to nothing beautiful. The suggestion of Kuba is indeed a very good one because you only have to write two rules to match f and all its derivatives:

{f[x],f'[x],f'''[x]} /. {f[_]->y,Derivative[n__][f][_]:>Derivative[n][y]}
(* {y,y^′,y^(3)} *)

• Your answer is what I need! Thank you. – PureLine Sep 28 '15 at 8:16

I have to agree with halirutan that doing this with a single rule is not a great idea, because the result is likely to be gross and confusing. However, in order to prove that it's on the yucky side, I'm going to show the best way I could figure out how to do it. I make no promises about its robustness, but it at least seems to handle the most obvious wrinkles OK.

First, I match on heads applied to f, and use Alternatives to get either a Derivative of f or f itself. Once you have that match with Alternatives, you can do almost anything with it on the right-hand side. I don't think any of the choices are aesthetically pleasing, but here's one that I think is awful in a kind of fun way.

In[1]:= rule = h : (_Derivative[f] | f)[x] :>
FirstCase[h, _Derivative, Identity, {2}, Heads -> True]@y;

In[2]:= 2*f[x] + g'[z]*f''[x] /. rule
Out[2]= 2 y + y'' g'[z]


Try this:

  f'[x] + f[x] /. f -> (y) /. a_[_] -> a

(*  y + Derivative[1][y]  *)


Have fun!

• It's tricky, however it works in this simple situation. – PureLine Sep 28 '15 at 14:03
• @PureLine The only "tricky" thing here is that y stays in brackets. All the rest is trivial, is not it? – Alexei Boulbitch Sep 28 '15 at 14:08
• @AlexeiBoulbitch Very bad idea if there is anything more in the expression: Log[f'[x] + f[x]] /. f -> (y) /. a_[_] -> a – halirutan Sep 28 '15 at 17:40
• @halirutan Right, a very bad idea, this is much better Log[f'[x] + f[x] /. f -> (y) /. a_[_] -> a]  giving Log[y + Derivative[1][y]] . Have fun! – Alexei Boulbitch Sep 29 '15 at 8:27