Change the order of digits and symbols in the output expression

In the following example:

In[454]:= SGGenSet229me[[1,1;;3]]
% . {x,y,z,1}

Out[454]= {{0, -1, 0, 1/4}, {0, 0, -1, 1/4}, {-1, 0, 0, 1/4}}

Out[455]= {1/4 - y, 1/4 - z, 1/4 - x}

The desired result is as follows: {-y + 1/4, -z + 1/4, -x + 1/4}. How can I achieve this goal?

Regards,
Zhao

The closest I can easily get to the display you want is

TraditionalForm[-1*{1/4 - y, 1/4 - z, 1/4 - x}]

but the output of TraditionalForm is for display only, do not try to do further calculations on the result, use

-1*{1/4 - y, 1/4 - z, 1/4 - x}

if you must do further calculations on this.

I want to get the final result in string format.

ToString[TraditionalForm[-1*{1/4 - y, 1/4 - z, 1/4 - x}]]

but that does not keep the signs that you had.

Trying to fight the default behavior seems to usually be challenging.

When you have two “contexts” (e.g. logic and presentation), build two separate families of functions. When doing computations with your structures, don’t mess with their representations–let WL do the work. When you want to present your structures, define your own presentation functions. So, if you want a string, you need to determine rules for what a correctly formatted string representation of your structure is. From those rules, you can build the necessary functions. For example, if I just make guesses as to your presentation rules, I might come up with this:

TermString[term_Plus] := 
 StringRiffle[
  ToString@*InputForm /@ SortBy[List @@ term, FreeQ[x | y | z]], 
  " + "];
CoordinatesString[coords : {_, _, _}] := 
 StringRiffle[TermString /@ coords, {"{", ", ", "}"}]

Trying it out:

CoordinatesString[{1/4 - y, 1/4 - z, 1/4 - x}]
(* "{-y + 1/4, -z + 1/4, -x + 1/4}" *)

I haven’t tested this much, and of course I don’t know your exact presentation rules, so you’ll probably need to adapt this.

I want to make it suitable for more variables, as shown below:

In[44]:= TermString[term_Plus] := 
 StringRiffle[
  ToString@*InputForm /@ SortBy[List @@ term, FreeQ[x | y | z|t|u|v]], 
  " + "];
CoordinatesString[coords : {_, _, _}] := 
 StringRiffle[TermString /@ coords, {"{", ", ", "}"}]

CoordinatesString[{1/4 - y, 1/4 - z, 1/4 - x}]
CoordinatesString[{1/4 - y, 1/4 - z, 1/4 - x, 2-t}]
CoordinatesString[{1/4 - y, 1/4 - z, 1/4 - x, 1/2-t,2/3+u}]
CoordinatesString[{1/4 - y, 1/4 - z, 1/4 - x, 1/2-t,2/3+u,4/5+v}]

Out[46]= "{-y + 1/4, -z + 1/4, -x + 1/4}"

During evaluation of In[46]:= StringRiffle::string: String expected at position 2 in StringRiffle[{-y + 1/4,-z + 1/4,-x + 1/4,-t + 2},{{,, ,, ,}}].

Out[47]= StringRiffle[{"-y + 1/4", "-z + 1/4", "-x + 1/4", 
  "-t + 2"}, {"{", ", ", ", ", "}"}]

Out[48]= CoordinatesString[{1/4 - y, 1/4 - z, 1/4 - x, 1/2 - t, 
  2/3 + u}]

Out[49]= CoordinatesString[{1/4 - y, 1/4 - z, 1/4 - x, 1/2 - t, 
  2/3 + u, 4/5 + v}]

It seems that the following method works well:

In[171]:= {{1/4 - y, 1/4 - z, 1/4 - x,1/2-t},{1/4 - y, 1/4 - z, 1/4 - x,1/2-t,3/4+u,5/6+v}}
(ToString[#[[2]],InputForm]<>"+"<>ToString[#[[1]],InputForm]&/@List@@@#)&/@%//StringRiffle[#,","]&//StringReplace[#,{"("->"",")"->"","},"->"\n","{"->"","}"->""}]&

Out[171]= {{1/4 - y, 1/4 - z, 1/4 - x, 1/2 - t}, {1/4 - y, 1/4 - z, 
  1/4 - x, 1/2 - t, 3/4 + u, 5/6 + v}}

Out[172]= "-y+1/4, -z+1/4, -x+1/4, -t+1/2
-y+1/4, -z+1/4, -x+1/4, -t+1/2, u+3/4, v+5/6"

However, when I further tried it on my actual data, I still didn’t get the expected results:

gensFindGroup//ClearAll;
gensFindGroup[{m__?MatrixQ} | m__?MatrixQ]:=Block[{gens,dim,vec,vars,x,y,z,t,u,v,gensop},
  gens={m};
  dim=Dimensions[gens][[2]]-1;
  vars = {x, y, z, t, u, v};
  vec = Append[Take[vars, dim], 1];
  (*vec = {x, y, z, t, u, v}[[;; dim]]~Join~{1}*)
  
  (*Table[(i[[1;;3,1;;3]] . Transpose[{{x,y,z}}] + i[[1;;3,4]])//Transpose,{i,gens}]//ArrayFlatten[#,1]&//ToString[#,InputForm]&//StringReplace[#,{"("->"",")"->""}]&;*)
  (*Plus@@{ArrayFlatten[#[[1;;3,1;;3]] . Transpose[{{x,y,z}}],1],#[[1;;3,4]]}&/@gens// InputForm // ToString //StringReplace[#,{"("->"",")"->""}]&*)
gensop=Map[Dot[#, vec] &, gens[[All, 1 ;; dim]], {-2}];
(ToString[#[[2]],InputForm]<>"+"<>ToString[#[[1]],InputForm]&/@List@@@#)&/@gensop//StringRiffle[#,","]&//StringReplace[#,{"("->"",")"->"","},"->"\n","{"->"","}"->""}]&
]

In[318]:= "gensSGc2false:=[[[0,-1,0,1/4],[0,0,-1,1/4],[-1,0,0,1/4],[0,0,0,1]],[[-1,2,-1,-3/8],[-3/2,3/2,-1/2,-1/16],[-1/2,3/2,-3/2,-17/16],[0,0,0,1]]]"//StringReplace[#,{"["->"{","]"->"}"}]&//ToExpression
gensSGc2false//gensFindGroup

Out[319]= "-y+1/4, -z+1/4, -x+1/4
-x+-3/8, -3*x/2+-1/16, -1/2*x+-17/16"

The expected results should be as follows:

-y + 1/4, -z + 1/4, -x + 1/4 
-x + 2*y - z -3/8, - 3*x/2 + 3*y/2 - z/2 -1/16, - x/2 + 3*y/2 - 3*z/2 -17/16

TermString[term_Plus] :=
StringRiffle[ToString@*InputForm /@ SortBy[List @@ term, NumericQ],
" + “];
CoordinatesString[coords : {__}] :=
StringRiffle[TermString /@ coords, {”{", ", ", “}”}]

CoordinatesString[{1/4 - y, 1/4 - z, 1/4 - x}]
(* "{-y + 1/4, -z + 1/4, -x + 1/4}" *)

CoordinatesString[{1/4 - y, 1/4 - z, 1/4 - x, 2 - t}]
(* "{-y + 1/4, -z + 1/4, -x + 1/4, -t + 2}" *)

CoordinatesString[{1/4 - y, 1/4 - z, 1/4 - x, 1/2 - t, 2/3 + u}]
(* "{-y + 1/4, -z + 1/4, -x + 1/4, -t + 1/2, u + 2/3}" *)

CoordinatesString[{1/4 - y, 1/4 - z, 1/4 - x, 1/2 - t, 2/3 + u, 4/5 + v}]
(* "{-y + 1/4, -z + 1/4, -x + 1/4, -t + 1/2, u + 2/3, v + 4/5}" *)

As I already said, you need to be able to rigorously articulate what your presentation rules actually are.

As for your gensFindGroup, I have no idea where you’re going with this. Please stop moving the goalposts.

Thank you very much, Eric,

As for your gensFindGroup, I have no idea where you’re going with
this. Please stop moving the goalposts.

Sorry, I digress too much. But the question discussed here is really a part of the question below.

Transform the affine matrix of space group elements to the desired generators required by Bilbao Crystallographic Server IDENTIFY GROUP , as shown below:

Based on your suggested approach, I present the following complete example:

In[228]:= TermString[term_Plus] := 
 StringRiffle[ToString@*InputForm /@ SortBy[List @@ term, NumericQ], 
  " + "];
CoordinatesString[coords : {__}] := 
 StringRiffle[TermString /@ coords, {"{", ", ", "}"}]

gens={{{0, -1, 0, 1/4}, {0, 0, -1, 1/4}, {-1, 0, 0, 1/4}, {0, 0, 0, 
   1}}, {{-1, 2, -1, -(3/8)}, {-(3/2), 3/2, -(1/2), -(1/16)}, {-(1/2),
    3/2, -(3/2), -(17/16)}, {0, 0, 0, 1}}};
dim=Dimensions[gens][[2]]-1;
vars = {x, y, z, t, u, v};
vec = Append[Take[vars, dim], 1];
Map[Dot[#, vec] &, gens[[All, 1 ;; dim]], {-2}];
      
CoordinatesString/@%;
StringReplace[#,{"("->"",")"->"","+ -"->"- "}]&/@%;
StringJoin[%]//StringReplace[#,{"}{"->"\n","{"->"","}"->""}]&

Out[237]= "-y + 1/4, -z + 1/4, -x + 1/4
-x + 2*y - z - 3/8, -3*x/2 + 3*y/2 - 1/2*z - 1/16, -1/2*x + 3*y/2 + \
-3*z/2 - 17/16"