6
$\begingroup$

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

$\endgroup$
3
  • 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
    Commented Sep 27, 2015 at 6:42
  • $\begingroup$ Related: (32651) $\endgroup$
    – Mr.Wizard
    Commented Sep 27, 2015 at 8:49
  • $\begingroup$ @halirutan - I agree and have deleted my comment. $\endgroup$
    – Bob Hanlon
    Commented Sep 27, 2015 at 16:22

3 Answers 3

9
$\begingroup$

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)} *)
$\endgroup$
1
  • $\begingroup$ Your answer is what I need! Thank you. $\endgroup$
    – PureLine
    Commented Sep 28, 2015 at 8:16
2
$\begingroup$

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]
$\endgroup$
1
$\begingroup$

Try this:

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

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

Have fun!

$\endgroup$
4
  • $\begingroup$ It's tricky, however it works in this simple situation. $\endgroup$
    – PureLine
    Commented Sep 28, 2015 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$ Commented Sep 28, 2015 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
    Commented Sep 28, 2015 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$ Commented Sep 29, 2015 at 8:27

Your Answer

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

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