5
$\begingroup$

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.

$\endgroup$
  • 2
    $\begingroup$ For single valued functions you could try something like m /. _[t]->0 $\endgroup$ – Ulrich Neumann Jun 19 at 12:49
  • $\begingroup$ 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. $\endgroup$ – Fred Jun 19 at 12:58
7
$\begingroup$

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, 
   Replace[If[Head[c0] === Plus, 
     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]
| improve this answer | |
$\endgroup$
  • $\begingroup$ This breaks if I have a constant like Sqrt[a + Sqrt[b]] $\endgroup$ – flinty Jun 19 at 13:28
  • $\begingroup$ ^ I think I've fixed that by only applying the Plus->List / List->Plus replace at level 1. $\endgroup$ – flinty Jun 19 at 13:34
  • $\begingroup$ 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. $\endgroup$ – Fred Jun 19 at 13:40
  • 1
    $\begingroup$ @Fred I've fixed cases when expr == 0 $\endgroup$ – flinty Jun 19 at 13:43
  • $\begingroup$ 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. $\endgroup$ – Fred Jun 19 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.