1
$\begingroup$

I want to produce a table (or a grid) from my list of data, as follows:

eq01={0.01,1.02};
ev01={{1,3},{2,4}};
ev02={{5,5},{6,1}};
 data1 = {{"", "Equilibrium points", "Eigenvalues", 
   "Eigenvectors"}, {"A = 0",eq01,ev01,ev02}};

Grid[data1, Alignment -> Left, Spacings -> {2, 1}, Frame -> All, 
 ItemStyle -> "Text"]

And what I obtain is the following:

enter image description here

Is there a way to: 1) flatten the lists under "Eigenvalues" and "Eigenvectors" without using flatten for every definition of list in the beginning? Because I have many eigenvalues and eigenvectors and it would be very long; 2) substitute the curly brackets with round brackets, to have for example, in the second column and second row, (0.01,1.02)?

I hope the question is clear, thanks in advance!

$\endgroup$
3
$\begingroup$

Edit
this works exactly as OP asked in the comments below
also for many rows

eq01={0.01,1.02};
ev01={{1,3},{2,4}};
ev02={{5,5},{6,1}};
data1=StringReplace[#,{"{{"->"(","}}"->")","{"->"(","}"->")"}]&/@
ToString/@#&/@{{"","Equilibrium points","Eigenvalues","Eigenvectors"},{"A = 0",eq01,ev01,ev02}};

Grid[data1,Alignment->Left,Spacings->{2,1},Frame->All,ItemStyle->"Text"]

enter image description here

$\endgroup$
  • $\begingroup$ Almost, actually I did not mention clearly that in the eigenvalues and eigenvectors columns I would like to have: (1,3),(2,4) and (5,5),(6,1), so I would need to keep the internal brackets and substitute them with round brackets. Do you know how I could do? And is there a way to write the rule only once for the entire data (which is longer than just this one in reality) and not for every row? Thanks! $\endgroup$ – Esperanta Apr 12 at 13:02
  • 1
    $\begingroup$ Esperanta, you said "flatten the lists under "Eigenvalues" and "Eigenvectors"... but now you are changing both of your questions... let me see what I can do. You should also try yourself now that I showed you the basics ;-) $\endgroup$ – J42161217 Apr 12 at 13:10
  • 1
    $\begingroup$ @Esperanta I updated the answer. Please check it $\endgroup$ – J42161217 Apr 12 at 13:29
3
$\begingroup$

I think you have to use a custom formatting utility of some sort:

vectorForm[v_?VectorQ] := Row[{"(", Row[v, ","], ")"}];

Grid[data1 /. {
   m_?(MatrixQ[#, NumericQ] &) :> Row[vectorForm /@ m, ","], 
   v_?(VectorQ[#, NumericQ] &) :> vectorForm[v]},
 Alignment -> Left, Spacings -> {2, 1}, Frame -> All, 
 ItemStyle -> "Text"]

Mathematica graphics

One distinct advantage of this approach is that it leverages Mathematica's expression formatting capabilities and avoids the lack of robustness of ToString[]. Compare the above method with @J42161217's on normalized eigenvectors:

eq01 = {0.01, 1.02};
ev01 = {{1, 3}, {2, 4}};
ev02 = Normalize /@ {{5, 5}, {6, 1}};

(* @MichaelE2 *)
data2 = {{"", "Equilibrium points", "Eigenvalues", 
    "Eigenvectors"}, {"A = 0", eq01, ev01, ev02}};
Grid[data2 /. {
   m_?(MatrixQ[#, NumericQ] &) :> Row[vectorForm /@ m, ","], 
   v_?(VectorQ[#, NumericQ] &) :> vectorForm[v]},
 Alignment -> Left, Spacings -> {2, 1}, Frame -> All, 
 ItemStyle -> "Text"]

Mathematica graphics

(* @J42161217 *)
data2 = StringReplace[#, {"{{" -> "(", "}}" -> ")", "{" -> "(", 
        "}" -> ")"}] & /@ ToString /@ # & /@ {{"", 
     "Equilibrium points", "Eigenvalues", "Eigenvectors"}, {"A = 0", 
     eq01, ev01, ev02}};
Grid[data2, Alignment -> Left, Spacings -> {2, 1}, Frame -> All, 
 ItemStyle -> "Text"]

enter image description here

Something similar happens with ordinary fractions and square roots.


Original formatting, in which a set appears in set notation:

Grid[data1 /. v_?(VectorQ[#, NumericQ] &) :> vectorForm[v], 
 Alignment -> Left, Spacings -> {2, 1}, Frame -> All, 
 ItemStyle -> "Text"]

Mathematica graphics

$\endgroup$
  • $\begingroup$ I think in order to get what OP asks you should replace "{("->"(" $\endgroup$ – J42161217 Apr 12 at 13:14
  • $\begingroup$ @J42161217 Maybe so. I'm not sure, but the OP will clarify, I suppose. $\endgroup$ – Michael E2 Apr 12 at 13:20
  • $\begingroup$ OP clarified in the comments of my answer. I think you should check it $\endgroup$ – J42161217 Apr 12 at 13:31
  • $\begingroup$ @J42161217 I had read that. I'm still not sure, since the OP did not say that the outer braces were unwanted. For all I know it's only the interior formatting that matters. Currently my answer shows the set of eigenvectors in standard notation, which may or may not be a more desirable format. It's easy to change. As I said, you may be right, but it's the OP I want to hear from before I change the answer. $\endgroup$ – Michael E2 Apr 12 at 13:37
  • 1
    $\begingroup$ Yes actually I needed to keep only the internal brackets and make them round. The answer by J42161217 is what I was looking for. Thanks! $\endgroup$ – Esperanta Apr 12 at 14:37
1
$\begingroup$

Another possibility is to define a form that automatically formats numeric vectors and numeric matrices as desired:

MakeBoxes[EigenForm[e_], StandardForm] ^:= Internal`InheritedBlock[
    {MakeBoxes},
    MakeBoxes[m_List?numericVectorQ, StandardForm] := RowBox[
        {"(", MakeBoxes[Row[m, ","], StandardForm], ")"}
    ];
    MakeBoxes[m_List?numericMatrixQ, StandardForm] := RowBox[
        BoxForm`AddCommas @ BoxForm`ListMakeBoxes[m, StandardForm]
    ];
    MakeBoxes[e, StandardForm]
]

SetAttributes[{numericMatrixQ, numericVectorQ}, HoldAll];

numericMatrixQ[e_]:=MatrixQ[Unevaluated[e], System`Dump`HeldNumericQ]
numericVectorQ[e_]:=VectorQ[Unevaluated[e], System`Dump`HeldNumericQ]

Michael's example:

eq01 = {0.01,1.02};
ev01 = {{1,3},{2,4}};
ev02 = Normalize/@{{5,5},{6,1}};

data2={{"","Equilibrium points","Eigenvalues","Eigenvectors"},{"A = 0",eq01,ev01,ev02}};
Grid[
    data2,
    Alignment -> Left, Spacings -> {2, 1}, Frame -> All, ItemStyle -> "Text"
] //EigenForm

enter image description here

$\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.