# How to collect coefficients that has summation in ODE

I have the following equation,

 equation= x''[t]+a*x'[t]+b*x[t]+c*y[t]+d*u[t]+e*v[t]+Sum[f[i]*x[t]+g[i]*u[t]+h[i],{i,1,n}]+k


When I collect the coefficient of each x[t] derivatives, x[t],y[t], u[t] and v[t] the correct answer should be

 { 1,a,b+sum[f[i],{i,1,n}],c,d+sum[g[i],{i,1,n}],e,k+sum[h[i],{i,1,n}]}


However when I use Coefficient[] function for each variable, I can get answer mostly correct except that I am not getting b+sum, d+sum, and k+sum. This is because Collect function is unable to separate sum from x[t],u[t] and constant. Is it possible to separate sum from variable[t]? How to collect coefficient that has summation formula?

Clear[linearExpand];
e //. {op : head[arg_Plus, __] :> Distribute[op],
With[{dependencies =
InternalDependsOnQ[#, x] & /@ List @@ arg1},
Pick[arg1, dependencies, True], rest]]};

CoefficientList[
linearExpand[equation, x, Sum] /.
Derivative[n_][x][t] :> x[t]^(n + 1), {x[t]}]
(*
{k + n - HarmonicNumber[n] - StieltjesGamma[1] +
StieltjesGamma[1, 1 + n] + d u[t] + n u[t] -
HarmonicNumber[n] u[t] + e v[t] + c y[t],
b + n f[i],
a,
1}
*)


Note that when the sum is split up, Sum can evaluate the sum of each term.

You can tell Mathematica to expand sums before asking it to collect terms. Collect and Coefficient and friends are meant for explicit polynomials, but are nonetheless useful. Consider

    splitsumsofsums:=Sum[Plus[stuff__],{range__}]:>Plus@@(Sum[#,{range}]&/@{stuff})


and the less pretty

    splitsumsofproducts:=Sum[Times[a_,b_],{range__}]:>
If[{}==Cases[a,{range}[[1]],Infinity],a Sum[b,{range}],
If[{}==Cases[b,{range}[[1]],Infinity],b Sum[a,{range}],
Sum[Times[a,b],{range}]]]


The latter rule needs to check if one of the multiplicands of the expression being summed is independent of the summation variable (we can only factor out constants). It doesn't work for indefinite sums. It does work, however, in this case:

    Coefficient[Replace[equation/.splitsumsofsums,splitsumsofproducts,All],{x[t]}]


yields {b+Sum[f[i],{i,1,n}]}`.