1
$\begingroup$

A general output from GiNaC (https://www.ginac.de/) of harmonic polylogarithms is H(a,b,c...,x). We want to convert it to Mathematica format (https://www.physik.uzh.ch/data/HPL/) HPL[{a,b,c...},x].

Note: we need "a general string expression" for the "a,b,c..." in H(a,b,c...,x) as we have to deal with a lot of Hs, for example, converting:

H(2,2,x),H(2,1,x),H(2,1,2,x),H(1,2,3,4,x),H(1,2,3,4,5,x) 

to

HPL[{2,2},x],HPL[{1,2},x],HPL[{1,2,3},x],HPL[{1,2,3,4},x],HPL[{1,2,3,4,5},x] 
$\endgroup$
3
  • 1
    $\begingroup$ Does something like StringCases["H(2,2,x),H(2,1,x),H(2,1,2,x),H(1,2,3,4,x),H(1,2,3,4,5,x)", RegularExpression["H\\(([\\w,]+)\\)?"] :> With[{res = ToExpression /@ StringSplit["$1", ","]}, HPL[Most[res], Last[res]]]] work for you? $\endgroup$ Commented Jan 13, 2021 at 11:41
  • $\begingroup$ How arbitrary is "arbitrary"? $\endgroup$ Commented Jan 13, 2021 at 11:58
  • $\begingroup$ @J.M.'sennui Ah! I see thank you for you code. My problem solved $\endgroup$ Commented Jan 13, 2021 at 12:15

2 Answers 2

1
$\begingroup$

Here is a general solution: We treat the input as a string, use StringReplace to make the necessary replacements and then add braces, so that we can change it to a MMA list:

str = StringReplace[ "H(2,2,x),H(2,1,x),H(2,1,2,x),H(1,2,3,4,x),H(1,2,3,4,5,x)" , "H(" ~~ a : Except[")"] .. ~~ ")" -> "HPL[" ~~ a ~~ "]"] ToExpression["{" <> str <> "}"] 

This gives:

{HPL[2, 2, x], HPL[2, 1, x], HPL[2, 1, 2, x], HPL[1, 2, 3, 4, x], HPL[1, 2, 3, 4, 5, x]} 
$\endgroup$
3
  • 1
    $\begingroup$ The first few should be in a list, and separated from the last one. $\endgroup$ Commented Jan 13, 2021 at 12:15
  • $\begingroup$ @J.M.'sennui I fixed this str = StringReplace[ "H(2,2,x),H(2,1,x),H(2,1,2,x),H(1,2,3,4,x),H(1,2,3,4,5,x)", "H(" ~~ a : Except[")"] .. ~~ ",x)" -> "HPL[{" ~~ a ~~ "},x]"] $\endgroup$ Commented Jan 13, 2021 at 12:26
  • 1
    $\begingroup$ @YU StringCases[] would still be the better route if you don't like regexes like in my previous comment: StringCases["H(2,2,x),H(2,1,x),H(2,1,2,x),H(1,2,3,4,x),H(1,2,3,4,5,x)", "H(" ~~ a : Except[")"] .. ~~ ")" :> With[{r = ToExpression /@ StringSplit[a, ","]}, HPL[Most[r], Last[r]]]]. $\endgroup$ Commented Jan 13, 2021 at 12:52
1
$\begingroup$

A related, but potentially more flexible approach than Daniel Huber's. First do the most minimal transformations necessary to allow Mathematica to interpret this as an expression

hexpr = "H(2,2,x),H(2,1,x),H(2,1,2,x),H(1,2,3,4,x),H(1,2,3,4,5,x)" // StringReplace[{"(" -> "[", ")" -> "]"}] // ToExpression["{" <> # <> "}"] & {H[2, 2, x], H[2, 1, x], H[2, 1, 2, x], H[1, 2, 3, 4, x], H[1, 2, 3, 4, 5, x]} 

then use Mathematica's symbolic manipulation tools to transform this into the desired output

hexpr // ReplaceAll[ H[args__, var_] :> HPL[{args}, var] ] {HPL[{2, 2}, x], HPL[{2, 1}, x], HPL[{2, 1, 2}, x], HPL[{1, 2, 3, 4}, x], HPL[{1, 2, 3, 4, 5}, x]} 

basically the idea is to delegate all of the string parsing effort to Mathematica, if possible, and then just transform it using standard methods

$\endgroup$
1
  • $\begingroup$ Beautiful code! I understand. Thank you @b3m2a1 $\endgroup$ Commented Jan 15, 2021 at 20:27

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.