# Split Expression Into Constant And Non-Constant Summands

I have a partially t-dependent matrix which I want to split into its t-dependent and its t-independent summand. My example for this matrix is

m = {{1 + Sin[t], 5}, {7 BesselJ[3, t], a + t + t^2 + t^3}}


which I want to split the following way:

In[1]:= Separate[m,t]

Out[1]:= {{{1, 5}, {0, a}}, {{Sin[t], 0}, {7 BesselJ[3, t], t + t^2 + t^3}}}

such that I get containing one constant and one completely non-constant matrix.

I don't want to use Integrate[] for this (so Fourier decomposition is not an option), but D[] is fine. I can use manual definitions like D[a, t]^=0 to tell Mathematica which parameters are constant.

The problem is that my actual functions are much more involved than what I presented here, and all solutions I could come up with are terrible workarounds which I'm afraid might fail occasionally without notice.

• For single valued functions you could try something like m /. _[t]->0 Jun 19, 2020 at 12:49
• Thank you, this is an interesting approach. Unfortunately, I also encounter problems when my functions have different levels of nesting of their t-dependence (e.g. Exp[I(1-Sin[w t])]). However, I will check whether it helps me find a solution.
– Fred
Jun 19, 2020 at 12:58

I hope this works. It was awkward to construct and I've tested it for a number of cases:

getconstants[expr_, var_] := Module[{c0},
If[expr == 0, Return[0]];
c0 = CoefficientList[expr, var][[1]];
If[FreeQ[c0, var], c0,
Select[Replace[c0, Plus -> List, 1], FreeQ[#, var] &], 0],
List -> Plus, 1]]]
extractConstantsMtx[mtx_, var_] :=
Map[getconstants[#, var] &, mtx, {2}]

m = {{Sqrt[a + Sqrt[b]] + Sin[t] Log[a + b], 5}, {7 BesselJ[3, t - a],
a Sin[x] Cos[x - 1 + Log[2 + b]] + t + t^2 + t^3}};

mc = extractConstantsMtx[m, t];

{m - mc, mc}

(* result:
{{{Log[a + b] Sin[t], 0}, {-7 BesselJ[3, a - t], t + t^2 + t^3}},
{{Sqrt[a + Sqrt[b]], 5}, {0, a Cos[1 - x - Log[2 + b]] Sin[x]}}} *)


(* test cases *)
extractConstantsMtx[{{0, 0}, {0, 0}}, t]
extractConstantsMtx[{{1, 2}, {3, 4}}, t]
extractConstantsMtx[{{1 t, 2 t}, {3 t, 4 t}}, t]
extractConstantsMtx[{{1 + t, 2 + t}, {3 + t, 4 + t}}, t]
extractConstantsMtx[{{1 + (1 + t)^2, 2 + (2 + t)^3}, {3 + (3 + t)^4, 4 + (5 + t)^5}}, t]
extractConstantsMtx[{{Sin[t], Cos[t + 1]}, {Tan[t + 2], Log[t - 3]}}, t]
getconstants[Sin[Sin[a + b] + c] + Cos[Sin[d + e] + t], t]
getconstants[BesselJ[y, s + b] + BesselJ[x, t + a], t]
getconstants[Mean[{a, b, Mean[{c, d, e}]}], t]

• This breaks if I have a constant like Sqrt[a + Sqrt[b]] Jun 19, 2020 at 13:28
• ^ I think I've fixed that by only applying the Plus->List / List->Plus replace at level 1. Jun 19, 2020 at 13:34
• Thank you very much. This really seems far from being an easy solution. For my matrix it still returns some expressions {}[[1]], probably because 0 as a matrix entry is somehow an exception, but apart from this it seems to work entirely correctly. I will do some more tests, too, and I will make sure I understand your code in its entirety.
– Fred
Jun 19, 2020 at 13:40
• @Fred I've fixed cases when expr == 0 Jun 19, 2020 at 13:43
• Thank you very much again! Now it really does seem to give me the correct solution, even for my quite complicated matrix. I will spend the following day working with it and later probably mark it as the answer.
– Fred
Jun 19, 2020 at 13:50