Message Boards Message Boards

0
|
2287 Views
|
6 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Convert the irrational expression of GAP to Wolfram language

Posted 1 year ago

I'm working with GAP and Wolfram languages at the same time. Sometimes, I must use the data generated by GAP as the input of Wolfram. In this case, a tough problem is converting the irrational number expression of GAP to the corresponding wolfram language command. The following is an example of my current practice:

exp//ClearAll;
exp[n_]:=Exp[2 Pi I/n]

In[430]:= (*gap> conjPGBC157:=FindTransformationMatrix(PGBC157);*)
conjPGBC157="[ [ 0, 1, -E(3)^2 ], [ 0, -E(3), E(3) ], [ 1, 0, 0 ] ]"//StringTrim//StringReplace[#,{"["->"{","]"->"}","I"->"E(4)",RegularExpression["E\\(([^)]+)\\)"]->"exp[$1]"} ]&//ToExpression
(*gap> repPGGenSetBC157:=List(PGGenSetBC157, x->x^conjPGBC157);*)
repPGGenSetBC157="[ [ [ 1, 0, 0 ], [ 0, E(3), 0 ], [ 0, 0, E(3)^2 ] ], [ [ 1, 0, 0 ], [ 0, 0, 1 ], [ 0, 1, 0 ] ] ]"//StringTrim//StringReplace[#,{"["->"{","]"->"}","I "->"E(4)",RegularExpression["E\\(([^)]+)\\)"]->"exp[$1]"} ]&//ToExpression

Out[430]= {{0, 1, -E^(-((2 I \[Pi])/3))}, {0, -E^(((2 I \[Pi])/3)), 
  E^((2 I \[Pi])/3)}, {1, 0, 0}}

Out[431]= {{{1, 0, 0}, {0, E^((2 I \[Pi])/3), 0}, {0, 0, 
   E^(-((2 I \[Pi])/3))}}, {{1, 0, 0}, {0, 0, 1}, {0, 1, 0}}}

In short, Exp[2 Pi I/4], aka, I, is expressed as E(4) in GAP, and Exp[2 Pi I/n] corresponds to the representation of cyclotomic numbers, aka, E(n). If I try to use the output data by GAP in Wolfram language, the above conversion must be done first correctly.

Although I have shown one approach above, it is too cumbersome to implement. I wonder if there is a better way to solve the problem.

Regards, Zhao

POSTED BY: Hongyi Zhao
6 Replies
Posted 1 year ago

Thank you for showing me this technique which is closer to natural language.

POSTED BY: Hongyi Zhao

Another variant that uses WL string patterns instead of regular expressions:

"[ [ 0, 1, -E(3)^2 ], [ 0, -E(3), E(3) ], [ 1, 0, 0 ] ]"
StringReplace[%, {"[" -> "{", "]" -> "}"}]
StringReplace[%, 
 "E(" ~~ (n : NumberString) ~~ ")" :> "Exp[2 Pi I/" ~~ n ~~ "]"]
% // ToExpression
POSTED BY: Gianluca Gorni
Posted 1 year ago
  1. In E(n), n must be a positive small integer.
  2. The parentheses () may be appeared elsewhere, but always come in pairs.

So it seems that using RegularExpression in this scenario is a more robust and practical way.

POSTED BY: Hongyi Zhao

If you can be sure that E(n) only appears with an explicit integer n and that the parenteses () never appear elsewhere, you don't need regular expressions:

"[ [ 0, 1, -E(3)^2 ], [ 0, -E(3), E(3) ], [ 1, 0, 0 ] ]"
StringReplace[%, {"[" -> "{", "]" -> "}"}]
StringReplace[%, {"E(" -> "Exp[2Pi I/(", ")" -> ")]"}]
% // ToExpression
POSTED BY: Gianluca Gorni
Posted 1 year ago

Building these dazzling regular expressions are error-prone, such as writing more or less symbols.

POSTED BY: Hongyi Zhao

Why is it cumbersome? It seems to be correct, provided that the parameter n in E(n) is a string numeral. Here is a slight variation:

"[ [ 0, 1, -E(3)^2 ], [ 0, -E(3), E(3) ], [ 1, 0, 0 ] ]"
StringReplace[%, {"[" -> "{", "]" -> "}"}]
StringReplace[%, 
 RegularExpression["E\\(([^)]+)\\)"] -> "Exp[2 Pi I/($1)]"]
% // ToExpression
POSTED BY: Gianluca Gorni
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