Suppose I have the expression:
expr = U''[x] + U'[x]
I would like to be able to write an abstract replacement rule so that I can transform the above expression to:
u2 + u1
I was hoping that the replacement rule:
U''[x] + U'[x] /. Derivative[n_][U][x]->StringJoin["u",ToString[n]]
would work. However, this disappointingly uses a literal n
, and not a variable n
:
U''[x] + U'[x] /. Derivative[n_][U][x]->StringJoin["u",ToString[n]]
(* 2 un *)
I suspect I'm close with my solution and I need to use some kind of #
and &
method but I still do not fully understand those functions.
Any help would be appreciated.
(dexpr = U''[x] + U'[x]) // FullForm
and try:dexpr /. Derivative[x_][f_][v_] :> ToLowerCase@ToString[f] <> ToString[x]
$\endgroup$:>
in my example also makes the replacement works as desired. $\endgroup$