Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.1K Views
|
6 Replies
|
1 Total Like
View groups...
Share
Share this post:

Convert the irrational expression of GAP to Wolfram language

Posted 3 years ago
POSTED BY: Hongyi Zhao
6 Replies
Posted 3 years 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 3 years 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 3 years 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