0
$\begingroup$

Let's say I have a table in Mathematica like so: Table

Where the first row is the header, and first column is also a header (similar to a multiplication table). ie S_AS_A = Es; S_BS_A = ss_sigma;

The layout for this list is like so:

{
{
{header row},
{"row name1",row item 1, row item 2, etc...}
{"row name 2", row item 1, row item 2, etc...}
}
}

How do I flatten this table into an association thread like so: Desired list

$\endgroup$
1
  • $\begingroup$ Btw.: S_A, ss_sigma etc. are not valid variable names in Mathematica. Underscores are used to define patterns (look up Blank in the documentation) $\endgroup$ Commented Apr 4, 2018 at 20:06

1 Answer 1

1
$\begingroup$

Maybe it works with

energies = {{{"Column1", "SA", "SB", "SC", "SD", "PxA", "PxB", "PxC", "PxD", "PyA", "PyB", "PyC", "PyD", "PzA", "PzB", "PzC", "PzD"}, {"SA", "Es", "sssigma", 0., "sssigma", 0., "-spsigma", 0., 0., 0., 0., 0., "spsigma", 0., 0., 0., 0.}}}[[1]];
Normal@AssociationThread[
 Flatten@Outer[
   Times,
   energies[[1, 2 ;;]],
   energies[[2 ;;, 1]],
   1
   ],
 Flatten@energies[[2 ;;, 2 ;;]]
 ]

{("SA")^2 -> "Es", "SA" "SB" -> "sssigma", "SA" "SC" -> 0., "SA" "SD" -> "sssigma", "PxA" "SA" -> 0., "PxB" "SA" -> "-spsigma", "PxC" "SA" -> 0., "PxD" "SA" -> 0., "PyA" "SA" -> 0., "PyB" "SA" -> 0., "PyC" "SA" -> 0., "PyD" "SA" -> "spsigma", "PzA" "SA" -> 0., "PzB" "SA" -> 0., "PzC" "SA" -> 0., "PzD" "SA" -> 0.}

Alternatively, you may use Thread@Rule instead of Normal@AssociationThread (this should be also a bit faster).

$\endgroup$
8
  • $\begingroup$ Definitely has to do with association thread, so thanks for that! I'll see if I can find more with that hint. $\endgroup$
    – MHS
    Commented Apr 4, 2018 at 20:02
  • $\begingroup$ Tried that code, so far all I get in return is an empty list: {}. I should mention that my original table is within 2 lists: { { {row 1},{row2},...,{row 16} } } $\endgroup$
    – MHS
    Commented Apr 4, 2018 at 20:06
  • $\begingroup$ Would you be so kind and post copyable code in your question. Then I could have another try. $\endgroup$ Commented Apr 4, 2018 at 20:07
  • $\begingroup$ {{{"Column1", "SA", "SB", "SC", "SD", "PxA", "PxB", "PxC", "PxD", "PyA", "PyB", "PyC", "PyD", "PzA", "PzB", "PzC", "PzD"}, {"SA", "Es", "sssigma", 0., "sssigma", 0., "-spsigma", 0., 0., 0., 0., 0., "spsigma", 0., 0., 0., 0.}}} $\endgroup$
    – MHS
    Commented Apr 4, 2018 at 20:08
  • $\begingroup$ Ah, and remove TableForm. It is just a wrapper for display. $\endgroup$ Commented Apr 4, 2018 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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