Generate pretty formatted strings form output of numbers

I’ve the following code snippet in Mathematica:

<<SpaceGroupIrep`
In[641]:= PGinfo[[32]][[6]]
getRotMat["CubiFace",#]&/@PGinfo[[32]][[7]]//ToString//StringReplace[#,{"{"->"[","}"->"]"}]&
(generateGroup[PGinfo[[32]][[7]],"E",RotTimes]//Union)==(getSGElem[227][[;;,1]]//Union)

Out[641]= {48, 7}

Out[642]= "[[[0, 0, -1], [-1, 0, 0], [0, -1, 0]], [[1, 1, 1], [0, 0, \
-1], [0, -1, 0]], [[0, -1, 0], [-1, 0, 0], [1, 1, 1]], [[-1, 0, 0], \
[1, 1, 1], [0, 0, -1]]]"

Out[643]= True

In the above output, the string result will be further used as the input of GAP, but the readability of this form is very poor. I want to get something similar to the following form:

[[[0, 0, -1], [-1, 0, 0], [0, -1, 0]], 
  [[1, 1, 1], [0, 0, -1], [0, -1, 0]], 
  [[0, -1, 0], [-1, 0, 0], [1, 1, 1]],
  [[-1, 0, 0], [1, 1, 1], [0, 0, -1]]]

Any hints for tweaking the above code snippet to obtain this more desired pretty format?

Regards,
HZ

Why not use TableForm or MatrixForm on the data as it exists before string-ifying it?

data=<…code to get the data…>;
dataAsString=<…the stuff you did to make data into a string…>;
MatrixForm[data]

MatrixForm takes options that control the layout, like direction for each level.

Process each row with ToString etc. Maybe something like this, but I can’t test it:

step1= StringReplace[#,{"{"->"[","}"->"]"}]&@ ToString@ getRotMat["CubiFace",#]&/@PGinfo[[32]][[7]]

Then

step2 = StringJoin@ Riffle[step1, ",\n"]

You probably also need to add a "[" and a "]" to the beginning and end, respectively. But I can’t test it.

I tried with the following code snippet, but the result doesn’t meet the requirement:

In[12]:= getRotMat["CubiFace",#]&/@PGinfo[[32]][[7]]//MatrixForm//ToString//StringReplace[#,{"{"->"[","}"->"]"}]&

Out[12]= "0    -1   0
0    0    -1
-1   0    0

1    0    0
1    0    -1
1    -1   0

0    -1   1
-1   0    1
0    0    1

-1   1    0
0    1    0
0    1    -1"

Nice trick, the following method does the trick:

In[25]:= step1= StringReplace[#,{"{"->"[","}"->"]"}]&@ ToString@ getRotMat["CubiFace",#]&/@PGinfo[[32]][[7]];
step2 = "[" <> StringJoin@ Riffle[step1, ",\n"] <> "]"

Out[26]= "[[[0, 0, -1], [-1, 0, 0], [0, -1, 0]],
[[1, 1, 1], [0, 0, -1], [0, -1, 0]],
[[0, -1, 0], [-1, 0, 0], [1, 1, 1]],
[[-1, 0, 0], [1, 1, 1], [0, 0, -1]]]"

But I think that the slightly modified version below is more portable:

In[791]:= step1=StringReplace[#,{"{"->"[","}"->"]"}]&@ ToString[#]&/@ getRotMat["CubiFace",#]&/@PGinfo[[32]][[7]]
step2="[" <> StringJoin@ Riffle[step1, ",\n"] <> "]"

Out[791]= {{"[0, 0, -1]", "[-1, 0, 0]", "[0, -1, 0]"}, {"[1, 1, 1]", 
  "[0, 0, -1]", "[0, -1, 0]"}, {"[0, -1, 0]", "[-1, 0, 0]", 
  "[1, 1, 1]"}, {"[-1, 0, 0]", "[1, 1, 1]", "[0, 0, -1]"}}

Out[792]= "[[0, 0, -1][-1, 0, 0][0, -1, 0],
[1, 1, 1][0, 0, -1][0, -1, 0],
[0, -1, 0][-1, 0, 0][1, 1, 1],
[-1, 0, 0][1, 1, 1][0, 0, -1]]"

Here is another example using this trick:

In[793]:= (*
List the C2v generators and the corresponding matrices.
*)
showPGinfo[];
C2v=Select[PGinfo, StringMatchQ[#[[2]], "C2v"] &];
(*posC2v=Position[PGinfo, #] & /@ C2v*)
genC2v=C2v[[1]][[7]];
generateGroup[genC2v,"E", RotTimes];
getRotMat[getSGLatt[25],#]&/@%;
step1=StringReplace[#,{"{"->"[","}"->"]"}]&@ ToString[#]&/@ %
step2="[" <> StringJoin@ Riffle[step1, ",\n"] <> "]"

Out[798]= {"[[-1, 0, 0], [0, -1, 0], [0, 0, 1]]", "[[1, 0, 0], [0, 1, \
0], [0, 0, 1]]", "[[1, 0, 0], [0, -1, 0], [0, 0, 1]]", "[[-1, 0, 0], \
[0, 1, 0], [0, 0, 1]]"}

Out[799]= "[[[-1, 0, 0], [0, -1, 0], [0, 0, 1]],
[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
[[1, 0, 0], [0, -1, 0], [0, 0, 1]],
[[-1, 0, 0], [0, 1, 0], [0, 0, 1]]]"

That’s not what I suggested, but it sounds like you have a solution that you’re happy with, so it doesn’t matter.

I just wonder what’s the method exactly proposed by you, so that I can have one more choice to solve the problem.

Okay, I’ll try to be explicit:

<<SpaceGroupIrep`

I don’t know what this is, and your question doesn’t seem to be about how to use this package. As a result, I have no real idea if I’m understanding your need.

PGinfo[[32]][[6]]
getRotMat["CubiFace",#]&/@PGinfo[[32]][[7]]//ToString//StringReplace[#,{"{"->"[","}"->"]"}]&
(generateGroup[PGinfo[[32]][[7]],"E",RotTimes]//Union)==(getSGElem[227][[;;,1]]//Union)

Same comment.

Next you say

In the above output, the string result will be further used as the input of GAP, but the readability of this form is very poor. I want to get something similar to the following form:

Here’s how I interpreted that, but remember, that up until this point I had absolutely no idea what was going on:

After doing a bunch of stuff that I don’t understand, HZ now has some data in the form of a string that will be passed off to GAP (yet another thing that I’m not familiar with and that has nothing to do with Mathematica as far as I can tell). So, HZ’s first problem is already solved. HZ’s second problem is to see a readable form of this data.

Well, it seems to me that you must have an array of data at this point in your code:

getRotMat["CubiFace",#]&/@PGinfo[[32]][[7]]

I can’t execute your code, so I can only infer what the data might look like at that point based on what I see later. For example, the next function you apply is ToString, so everything after that must be working with the string form of the data. So, my assumption is that the little snippet above generates data as an array structure. I’ll go further and assume that your data is actually this:

data = {{{0, 0, -1}, {-1, 0, 0}, {0, -1, 0}}, {{1, 1, 1}, {0, 0, -1}, {0, -1, 0}}, {{0, -1, 0}, {-1, 0, 0}, {1, 1, 1}}, {{-1, 0, 0}, {1, 1, 1}, {0, 0, -1}}}

Okay, well, we have some built-in ways to make such data more readable, and it seemed to me that MatrixForm might suit your needs:

MatrixForm[data, TableDirections -> {Column, Row, Row}]

sample-matrixform.bmp (67.9 KB)

Now, having seen some subsequent comments, it seems that what you really wanted was to make the string form of the data (the data that you generated to use as input to GAP) itself more readable as a string. Since I don’t know anything about GAP, I’m not sure whether fiddling with the string form will make the data invalid or not, but to get a string that approximates the output you said you wanted, you could do this:

StringRiffle[data, {"[", "\n", "]"}, {"[", ", ", "]"}, {"[", ", ", "]"}]

sample-matrixform-2.bmp (86.2 KB)

Thank you for your explanation. It does the trick.

<<SpaceGroupIrep`
In[14]:= data=getRotMat["CubiFace",#]&/@PGinfo[[32]][[7]];
MatrixForm[data, TableDirections -> {Column, Row, Row}];
StringRiffle[data, {"[", "\n", "]"}, {"[", ", ", "]"}, {"[", ", ", "]"}]

Out[16]= "[[[0, 0, -1], [-1, 0, 0], [0, -1, 0]]
[[1, 1, 1], [0, 0, -1], [0, -1, 0]]
[[0, -1, 0], [-1, 0, 0], [1, 1, 1]]
[[-1, 0, 0], [1, 1, 1], [0, 0, -1]]]"

The SpaceGroupIrep package mentioned above is located here.

Yours,
HZ

HZ, I think you should realize that asking people here to download code or even follow links is an imposition. Posting code that is not executable with a standard installation of Mathematica is going to engender antipathy or at least annoyance. Furthmermore, your question didn’t even depend on those external packages/links, and so it was actually rather rude to include them.

Your question could have been simply:

Given a 3D array of data like this

{{{0, 0, -1}, {-1, -1, -1}}, {{-1, 1, 1}, {0, 1, -1}}}

How can I generate a corresponding string formatted like this:

"[[[0, 0, -1], [-1, -1, -1]],\n[[-1, 1, 1], [0, 1, -1]]]"

Thank you very much for your pertinent suggestions and advice. I will pay attention to and abide by these good practice rules in the future.

Yours,
HZ