2
$\begingroup$

Given a matrix expression

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

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

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

How do I do this? Eventually, I want to extend this to a more complicated case where I have \begin{equation}(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)\end{equation} displaying the cosine term next to 1.1 and the sine term next to 1.2 .

$\endgroup$
2
  • $\begingroup$ Is 1.1 the real number 1.1? $\endgroup$ – Αλέξανδρος Ζεγγ Aug 17 '20 at 11:16
  • $\begingroup$ no, just a subindex; meaning the first summand of (a+b) $\endgroup$ – MrDerDart Aug 17 '20 at 11:20
2
$\begingroup$
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
$\endgroup$
0
1
$\begingroup$
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
$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$ – MrDerDart Aug 17 '20 at 12:09
  • 1
    $\begingroup$ @MrDerDart That means there are essential differences between your two cases and I just worked for your posted example. $\endgroup$ – Αλέξανδρος Ζεγγ Aug 17 '20 at 12:10
  • $\begingroup$ I have edited the post. $\endgroup$ – MrDerDart Aug 17 '20 at 12:19
  • $\begingroup$ @MrDerDart Plz see my update. $\endgroup$ – Αλέξανδρος Ζεγγ Aug 17 '20 at 14:42
1
$\begingroup$
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)$

$\endgroup$

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.