# Split matrix expression into multiple lines

Given a matrix expression

testm = {a + b, c + d, e};

{# , testm[[ # ]] // Simplify} & /@ Range[ Length[testm] ] // MatrixForm


$$$$\begin{pmatrix} 1 & a+b \\ 2 & c+d \\ 3 & e \\\end{pmatrix}$$$$ I want to rewrite this as $$$$\begin{pmatrix}1.1 & a \\1.2 & b \\2.1 & c \\2.2 & d \\3 & e \\\end{pmatrix}$$$$

How do I do this? Eventually, I want to extend this to a more complicated case where I have $$$$(a+b) = A exp[I(k + l)m(n + o + p\ q)] Cos(r \ s)+A exp(I (k + l) m (n + o + p \ q)) Sin(t \ u)$$$$ displaying the cosine term next to 1.1 and the sine term next to 1.2 .

• Is 1.1 the real number 1.1? – Αλέξανδρος Ζεγγ Aug 17 '20 at 11:16
• no, just a subindex; meaning the first summand of (a+b) – MrDerDart Aug 17 '20 at 11:20

List @@@ {a + b, c + d}
testm = {Cos[a] + Sin[a], 5 + Sin[a]};
List @@@ testm


Whether is the following you want?

testm = {Cos[a] + Sin[a], 5 + Sin[a]};
m = List @@@ testm
Flatten[Table[{i.j, m[[i, j]]}, {i, 2}, {j, 2}], 1]


Updated

Maybe MapIndexed should work

 Clear["Global*"];
testm = {a + b, c + d, e};
mat = List @@@ testm
Flatten[MapIndexed[f @@ {Dot @@ #2, #1} &, mat, {-1}]] /.
f -> List // MatrixForm

testm = {a + b, c + d, e};
index = StringTemplate[If[Length[#] == 2, ".", ""]] @@ # &;
{index @ Position[testm, #, {-1}][[1]], #} & /@ Cases[testm, _Symbol, -1] // MatrixForm


Update

testm = {A Exp[I (k + l) m (n + o + p q)] Cos[r s] + A Exp[I (k + l) m (n + o + p q)] Sin[t u], c + d, e};
{index @ Position[testm, #, 2][[1]], #} & /@ Cases[testm, (_Symbol | _Times), 2] // MatrixForm

• Thank you. This seems to work for the simple example that I posted. However it does not work if I substitute for (a+b) a more complicated expression such as A exp(I(k+l)m(n+o+pq)) Cos(rs)+A exp(I(k+l)m(n+o+pq)) Sin(tu). I want this to be split up into the Cos and Sin terms. – MrDerDart Aug 17 '20 at 12:09
• @MrDerDart That means there are essential differences between your two cases and I just worked for your posted example. – Αλέξανδρος Ζεγγ Aug 17 '20 at 12:10
• I have edited the post. – MrDerDart Aug 17 '20 at 12:19
• @MrDerDart Plz see my update. – Αλέξανδρος Ζεγγ Aug 17 '20 at 14:42
ClearAll[indexedMonomials]
indexedMonomials = Join @@
(MapIndexed[{Dot @@ #2, #} &, MonomialList@#, {2}] /. {{a_, b_}} :> {{First@a, b}}) &;


Example:

testm2 = {A E^(I (k + l) m (n + o + p q)) Cos[r s] +
A E^(I (k + l) m (n + o + p q)) Sin[t u], a + b + x, c + d, e};

MapIndexed[{#2[[1]], #} &, testm2] // MatrixForm // TeXForm


$$\left( \begin{array}{cc} 1 & A \cos (r s) e^{i m (k+l) (n+o+p q)}+A \sin (t u) e^{i m (k+l) (n+o+p q)} \\ 2 & a+b+x \\ 3 & c+d \\ 4 & e \\ \end{array} \right)$$

indexedMonomials @ testm2 // MatrixForm // TeXForm
`

$$\left( \begin{array}{cc} 1.1 & A \cos (r s) e^{i m (k+l) (n+o+p q)} \\ 1.2 & A \sin (t u) e^{i m (k+l) (n+o+p q)} \\ 2.1 & a \\ 2.2 & b \\ 2.3 & x \\ 3.1 & c \\ 3.2 & d \\ 4 & e \\ \end{array} \right)$$