This question came up in the Maple forum: Given a differential equation, how do we move all terms with the dependent variable and all its derivative to the left side of the equation and all terms with the independent variable to the right side?
Given a differential equation, typically the dependent variable is $y(x)$ and the independent variable is $x$.
Maple is not as strong as Mathematica in Pattern matching. So solutions are shown there using Maple's type system which is strong.
How would this be done in Mathematica? Below is my attempt.
Here is the function I wrote.
convert[odeIn_Equal, y_[x_]] :=
Module[{ode = odeIn, n, yd, yy, deps, indeps},
ode = ode[[1]] - ode[[2]];
If[Or[FreeQ[ode, y], Not[Head[ode] === Plus]], Return[odeIn, Module]];
ode = ode /. {Derivative[n_][y][x] :> yd^n, y[x] :> yy};
deps = Select[ode, Or[Not[FreeQ[#, yd]], Not[FreeQ[#, yy]]] &];
deps = deps /. {yd^n_. :> Derivative[n][y][x], yy^n_. :> y[x]^n};
indeps = Select[ode, (FreeQ[#, yd] && FreeQ[#, yy]) &];
deps == -indeps]
Here are tests cases and the output
testCases = {
y[x] + y'[x] + Cos[x] + g[y[x]] + f'[x] + 1/x == Sin[x],
f'[x] + 1/x == Sin[x],
y[x] + x == 0,
x*y'[x] + x == y[x] + y'''[x],
x^2 + 1/y[x] + y'[x] + Sin[x] == y[x]^2,
1 + x == 0,
y'[x] == 0,
y''[x] == y[x] - x};
Call it as
Map[{#, convert[#, y[x]]} &, testCases]
Grid[%, Frame -> All]
This is only for ordinary differential equation. Not partial differential equation. Separate function will have to be written for that.
Is there a better or more canonical way to do this? Does Mathematica have builtin function to do this?
V 14