The list of coefficients of the "polynomial", which has the order of derivatives instead of degrees

I have some equation

eqn = (A + B).x''[t] + Transpose[x'[t]].(2 A - 3 B + 1).x'[t] +(СС - 5).y'[t]+ Sin[x[t]+y[t]]

I need to collect all the coefficients at x''[t],x'[t],y'[t] and free term Sin[x[t]+y[t]]

That is, to get something like:

X = A + B
Y = Transpose[x'[t]].(2 A - 3 B + 1)
Z = CC - 5
F = Sin[x[t]+y[t]]

How to do it with in Mathematica automatically?

• Perhaps this can be of use for the non-constant terms: Cases[eqn, Dot[a_, Derivative[i_][z_][t_]] :> a]. Sep 29 '21 at 19:02
• Or CoefficientList[eqn /. Dot[a_, Derivative[n_][z_][t]] :> a z^n, {x, y}] to get all of them. However, you have given just one example so I am not sure whether this is general enough for all your cases. Sep 29 '21 at 19:14
– dtn
Sep 29 '21 at 19:18

You can use Replace to convert derivatives to powers, wrapped in some arbitrary head, and then use any of the existing functions to get the coefficients (e.g. CoefficientList or CoefficientRules).

derivativeCoefficients[eqn_] :=
CoefficientRules[
eqn /. (a_) . Derivative[n_][z_][t] | a_*Derivative[n_][z_][t] :>
a d[z]^n, {d[x], d[y]}] /. d[z_]^n_ :> Derivative[n][z][t]

eqn = (A + B) . x''[t] +
Transpose[x'[t]] . (2 A - 3 B + 1) . x'[t] + (CC - 5) . y'[t] +
Sin[x[t] + y[t]]

derivativeCoefficients[eqn]

(* {{2, 0} -> A + B,
{1, 0} -> Transpose[x'[t]] . (1 + 2 A - 3 B),
{0, 1} -> -5 + CC,
{0, 0} -> Sin[x[t] + y[t]]} *)

{X, Y, Z, F} = {{2,0}, {1,0}, {0,1}, {0,0}} /. %

This should now also work for simple multiplicative coefficients.

eqn = a x''[t] + 5 x'[t] - 2 y'[t] + x[t] y[t]
derivativeCoefficients[eqn]

(* {{2, 0} -> a,
{1, 0} -> 5,
{0, 1} -> -2,
{0, 0} -> x[t] y[t]} *)
• Thanks for the answer. And what is this replacement for numbers? Is it possible to get the coefficients in the form in which they stand for the corresponding derivatives?
– dtn
Sep 29 '21 at 19:50
• @dtn, I have edited my answer to include this. Sep 29 '21 at 22:29
• Yes, it works. I will try your approach on other equations.
– dtn
Sep 30 '21 at 4:08