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


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'}?

  • 2
    $\begingroup$ Not a sigle rule so doesn't count :) f'[x] + f[x] == 0 /. {f[___] -> y, Derivative[n__][f][___] :> Derivative[n][y]} $\endgroup$ – Kuba Sep 27 '15 at 6:42
  • $\begingroup$ Related: (32651) $\endgroup$ – Mr.Wizard Sep 27 '15 at 8:49
  • $\begingroup$ @halirutan - I agree and have deleted my comment. $\endgroup$ – 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.

(* {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)} *)
  • $\begingroup$ Your answer is what I need! Thank you. $\endgroup$ – 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!

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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