Message Boards Message Boards

0
|
2531 Views
|
10 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Generate pretty formatted strings form output of numbers

Posted 2 years ago

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

POSTED BY: Hongyi Zhao
10 Replies
Posted 2 years ago

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]]]"
POSTED BY: Hongyi Zhao

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.

POSTED BY: Michael Rogers
Posted 2 years ago

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.

POSTED BY: Eric Rimbey
Posted 2 years ago

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"
POSTED BY: Hongyi Zhao
Posted 2 years ago

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

POSTED BY: Eric Rimbey
Posted 2 years ago

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

POSTED BY: Hongyi Zhao
Posted 2 years ago

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}]

enter image description here

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", "]"}, {"[", ", ", "]"}, {"[", ", ", "]"}]

enter image description here

POSTED BY: Eric Rimbey
Posted 2 years ago

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

POSTED BY: Hongyi Zhao
Posted 2 years ago

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]]]"
POSTED BY: Eric Rimbey
Posted 2 years ago

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

POSTED BY: Hongyi Zhao
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract