Message Boards Message Boards

0
|
2225 Views
|
5 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Export the expression in fraction format

Posted 1 year ago

See my following code snippet:

gMatrix=SparseArray[{{1,3}->1,{2,1}->1,{3,2}->1,{4,4}->-(1/2),{4,5}->-(1/2),{4,6}->-(1/2),{4,7}->-(1/2),{5,4}->1/2,{5,5}->-(1/2),{5,6}->1/2,{5,7}->-(1/2),{6,4}->1/2,{6,5}->-(1/2),{6,6}->-(1/2),{6,7}->1/2,{7,4}->1/2,{7,5}->1/2,{7,6}->-(1/2),{7,7}->-(1/2),{_,_}->0},7];
hMatrix=SparseArray[{{1,5}->1,{2,2}->-(1/2),{2,3}->-(1/2),{2,6}->1/2,{2,7}->1/2,{3,2}->1/2,{3,3}->-(1/2),{3,6}->-(1/2),{3,7}->1/2,{4,1}->1,{5,4}->1,{6,2}->-(1/2),{6,3}->1/2,{6,6}->-(1/2),{6,7}->1/2,{7,2}->-(1/2),{7,3}->-(1/2),{7,6}->-(1/2),{7,7}->-(1/2),{_,_}->0},7];
kMatrix=SparseArray[{{1,4}->-1,{2,2}->-(1/2),{2,3}->-(1/2),{2,6}->-(1/2),{2,7}->1/2,{3,2}->1/2,{3,3}->-(1/2),{3,6}->-(1/2),{3,7}->-(1/2),{4,5}->-1,{5,1}->1,{6,2}->1/2,{6,3}->1/2,{6,6}->-(1/2),{6,7}->1/2,{7,2}->-(1/2),{7,3}->1/2,{7,6}->-(1/2),{7,7}->-(1/2),{_,_}->0},7];

In[47]:= {gMatrix, hMatrix, kMatrix} // Normal // Rationalize // ExportString[#, "PythonExpression"] &

Out[47]= "[[[0, 0, 1, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0], [0, 1, 0, \
0, 0, 0, 0], [0, 0, 0, -0.5, -0.5, -0.5, -0.5], [0, 0, 0, 0.5, -0.5, \
0.5, -0.5], [0, 0, 0, 0.5, -0.5, -0.5, 0.5], [0, 0, 0, 0.5, 0.5, \
-0.5, -0.5]], [[0, 0, 0, 0, 1, 0, 0], [0, -0.5, -0.5, 0, 0, 0.5, \
0.5], [0, 0.5, -0.5, 0, 0, -0.5, 0.5], [1, 0, 0, 0, 0, 0, 0], [0, 0, \
0, 1, 0, 0, 0], [0, -0.5, 0.5, 0, 0, -0.5, 0.5], [0, -0.5, -0.5, 0, \
0, -0.5, -0.5]], [[0, 0, 0, -1, 0, 0, 0], [0, -0.5, -0.5, 0, 0, -0.5, \
0.5], [0, 0.5, -0.5, 0, 0, -0.5, -0.5], [0, 0, 0, 0, -1, 0, 0], [1, \
0, 0, 0, 0, 0, 0], [0, 0.5, 0.5, 0, 0, -0.5, 0.5], [0, -0.5, 0.5, 0, \
0, -0.5, -0.5]]]"

I want to export the expression in fraction format, that is, in this example, all 0.5 are displayed as 1/2. Are there any tips to achieve this goal?

Regards,
Zhao

POSTED BY: Hongyi Zhao
5 Replies
Posted 1 year ago

Thank you, Steve Coy. For my case, the numbers are exact rational expressed in float form, so the following usage of Rationalize should enough: Rationalize[x].

In[63]:= (Rationalize[Pi, 1/Infinity] // 
   N[#, 50000] &) == (Rationalize[Pi] // 
   N[#, 50000] &) == (Rationalize[Pi, 0] // N[#, 50000] &)

Out[63]= True
POSTED BY: Hongyi Zhao
Posted 1 year ago

What about Rationalize?
Rationalize[Pi, .1]

In[33]:= Rationalize[Pi, .1]

Out[33]= 22/7
POSTED BY: Steve Coy
Posted 1 year ago

According to the comments here, the following method does the trick:

In[200]:= gMatrix=SparseArray[{{1,3}->1,{2,1}->1,{3,2}->1,{4,4}->-(1/2),{4,5}->-(1/2),{4,6}->-(1/2),{4,7}->-(1/2),{5,4}->1/2,{5,5}->-(1/2),{5,6}->1/2,{5,7}->-(1/2),{6,4}->1/2,{6,5}->-(1/2),{6,6}->-(1/2),{6,7}->1/2,{7,4}->1/2,{7,5}->1/2,{7,6}->-(1/2),{7,7}->-(1/2),{_,_}->0},7];
hMatrix=SparseArray[{{1,5}->1,{2,2}->-(1/2),{2,3}->-(1/2),{2,6}->1/2,{2,7}->1/2,{3,2}->1/2,{3,3}->-(1/2),{3,6}->-(1/2),{3,7}->1/2,{4,1}->1,{5,4}->1,{6,2}->-(1/2),{6,3}->1/2,{6,6}->-(1/2),{6,7}->1/2,{7,2}->-(1/2),{7,3}->-(1/2),{7,6}->-(1/2),{7,7}->-(1/2),{_,_}->0},7];
kMatrix=SparseArray[{{1,4}->-1,{2,2}->-(1/2),{2,3}->-(1/2),{2,6}->-(1/2),{2,7}->1/2,{3,2}->1/2,{3,3}->-(1/2),{3,6}->-(1/2),{3,7}->-(1/2),{4,5}->-1,{5,1}->1,{6,2}->1/2,{6,3}->1/2,{6,6}->-(1/2),{6,7}->1/2,{7,2}->-(1/2),{7,3}->1/2,{7,6}->-(1/2),{7,7}->-(1/2),{_,_}->0},7];

{gMatrix,hMatrix,kMatrix}//Normal//Rationalize;
StringReplace[#,{"{"->"[","}"->"]"}]&@ToString[#,InputForm]&/@%;
"["<>StringJoin@Riffle[%,",\n"]<>"]"

Out[205]= "[[[0, 0, 1, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0], [0, 1, 0, \
0, 0, 0, 0], [0, 0, 0, -1/2, -1/2, -1/2, -1/2], [0, 0, 0, 1/2, -1/2, \
1/2, -1/2], [0, 0, 0, 1/2, -1/2, -1/2, 1/2], [0, 0, 0, 1/2, 1/2, \
-1/2, -1/2]],
[[0, 0, 0, 0, 1, 0, 0], [0, -1/2, -1/2, 0, 0, 1/2, 1/2], [0, 1/2, \
-1/2, 0, 0, -1/2, 1/2], [1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], \
[0, -1/2, 1/2, 0, 0, -1/2, 1/2], [0, -1/2, -1/2, 0, 0, -1/2, -1/2]],
[[0, 0, 0, -1, 0, 0, 0], [0, -1/2, -1/2, 0, 0, -1/2, 1/2], [0, 1/2, \
-1/2, 0, 0, -1/2, -1/2], [0, 0, 0, 0, -1, 0, 0], [1, 0, 0, 0, 0, 0, \
0], [0, 1/2, 1/2, 0, 0, -1/2, 1/2], [0, -1/2, 1/2, 0, 0, -1/2, \
-1/2]]]"
POSTED BY: Hongyi Zhao
Posted 1 year ago

In fact, I want to use the result in GAP to construct the corresponding matrix group. Considering that GAP only support rationalized form in this case, I have to do this kind of treatment. The above data comes from here, and I want to check if this group is isomorphic to U3(3). The complete GAP code is as follows:

gap> G:=Group([[[0, 0, 1, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], [0, 0, 0, -1/2, -1/2, -1/2, -1/2], [0, 0, 0, 1/2, -1/2, 1/2, -1/2], [0, 0, 0, 1/2, -1/2, -1/2, 1/2], [0, 0, 0, 1/2, 1/2, -1/2, -1/2]], 
>   [[0, 0, 0, 0, 1, 0, 0], [0, -1/2, -1/2, 0, 0, 1/2, 1/2], [0, 1/2, -1/2, 0, 0, -1/2, 1/2], 
>   [1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, -1/2, 1/2, 0, 0, -1/2, 1/2], 
>   [0, -1/2, -1/2, 0, 0, -1/2, -1/2]], 
> [[0, 0, 0, -1, 0, 0, 0], [0, -1/2, -1/2, 0, 0, -1/2, 1/2], [0, 1/2, -1/2, 0, 0, -1/2, -1/2], [0, 0, 0, 0, -1, 0, 0], [1, 0, 0, 0, 0, 0, 0], [0, 1/2, 1/2, 0, 0, -1/2, 1/2], [0, -1/2, 1/2, 0, 0, -1/2, -1/2]]]);
<matrix group with 3 generators>
gap> gensU33:=[
> [[-1,0,0,0,0,0,0],
> [0,-1,0,0,0,0,0],
> [0,1,0,0,0,1,0],
> [-1,0,0,0,0,0,1],
> [0,0,0,0,1,0,0],
> [0,1,1,0,0,0,0],
> [-1,0,0,1,0,0,0]],
> [[0,-1,0,0,0,-1,0],
> [0,1,1,0,0,0,0],
> [0,-1,0,0,0,0,0],
> [1,0,0,0,0,0,0],
> [0,0,0,0,0,0,-1],
> [0,0,0,-1,0,0,1],
> [0,0,0,0,1,0,1]]];
[ [ [ -1, 0, 0, 0, 0, 0, 0 ], [ 0, -1, 0, 0, 0, 0, 0 ], [ 0, 1, 0, 0, 0, 1, 0 ], [ -1, 0, 0, 0, 0, 0, 1 ], [ 0, 0, 0, 0, 1, 0, 0 ], 
      [ 0, 1, 1, 0, 0, 0, 0 ], [ -1, 0, 0, 1, 0, 0, 0 ] ], [ [ 0, -1, 0, 0, 0, -1, 0 ], [ 0, 1, 1, 0, 0, 0, 0 ], [ 0, -1, 0, 0, 0, 0, 0 ], [ 1, 0, 0, 0, 0, 0, 0 ], 
      [ 0, 0, 0, 0, 0, 0, -1 ], [ 0, 0, 0, -1, 0, 0, 1 ], [ 0, 0, 0, 0, 1, 0, 1 ] ] ]
gap> U33:=Group(gensU33);
<matrix group with 2 generators>
gap> IsomorphismGroups(U33,G);
#I  Forcing finiteness test
CompositionMapping( [ (1,9)(3,49)(4,5)(6,53)(7,59)(8,10)(13,18)(15,21)(16,57)(17,23)(20,27)(24,58)(25,36)(28,46)(29,41)(31,43)(32,62)(33,44)(34,61)(35,47)(39,55)(40,
    50)(45,48)(54,56), (1,40,63,51,2,52,57)(3,46,34,9,10,59,43)(4,58,18,5,6,37,39)(7,47,11,55,23,54,8)(12,14,48,31,49,15,30)(13,17,20,25,27,44,24)(16,41,36,56,50,22,
    19)(21,60,38,45,35,26,62)(28,61,29,33,53,42,32) ] -> [ [ [ 0, 0, 0, 1/2, -1/2, 1/2, 1/2 ], [ 0, -1, 0, 0, 0, 0, 0 ], [ 0, 0, 0, -1/2, -1/2, 1/2, -1/2 ], 
      [ 1/2, 0, -1/2, 0, -1/2, -1/2, 0 ], [ -1/2, 0, -1/2, -1/2, 0, 0, 1/2 ], [ 1/2, 0, 1/2, -1/2, 0, 0, 1/2 ], [ 1/2, 0, -1/2, 0, 1/2, 1/2, 0 ] ], 
  [ [ 0, -1/2, 1/2, 0, 0, -1/2, -1/2 ], [ 1/2, 0, -1/2, 0, 1/2, -1/2, 0 ], [ 1/2, 1/2, 0, 0, -1/2, 0, -1/2 ], [ 1/2, -1/2, 0, 0, -1/2, 0, 1/2 ], 
      [ 1/2, 0, 1/2, 0, 1/2, 1/2, 0 ], [ 0, 0, 0, 1, 0, 0, 0 ], [ 0, -1/2, -1/2, 0, 0, 1/2, -1/2 ] ] ], <action isomorphism> )

BTW, for other rational numbers, is there a general method to solve this format conversion problem?

Regards, Zhao

POSTED BY: Hongyi Zhao

For this specific example you could append // StringReplace["0.5" -> "1/2"]. Is there a reason for wanting to do this? Python will convert 1/2 to a floating point number anyway. Were you planning to use the fractions module?

POSTED BY: Rohit Namjoshi
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