1

While there are many ways to convert a Mathematica expression into other Languages, I was wondering if there is something reverse for Python. There are some discussions in Python groups, but I am interested if something basic is available in Mathematica itself.

Example:

expr="2*cos(pi*eps)/(1-eps)*exp(EulerGamma*(2*eps))/gamma(1-2*eps)*eps";

Is there any way to convert it to Mathematica expression?

There is one discussion as to convert it using TeXForm with ToExpression. But this works only with ALL Latex expressions.

Convert python Code To Mathematica?

One possibility is to use StringReplace but it seems it is of limited use.

StringReplace[expr, {"gamma(" ~~ x__ ~~ ")" :> "Gamma[" <> x <> "]"}]

But this way it looks for pattern-matching for the very last ). For example, it will not work on cos(pi*eps) in this expression as it will find a closing bracket ) later.

Expected output:

expr=2*Cos[Pi*eps]/(1-eps)*Exp[EulerGamma*(2*eps)]/Gamma[1-2*eps]*eps;

Any suggestions on how to achieve this if at all possible?

BabaYaga
  • 1,836
  • 9
  • 19
  • 3
    ToExpression[expr, TraditionalForm] ? Note that you have to then manually replace gamma because there is no way Mathematica can know what gamma is supposed to represent: ToExpression[expr, TraditionalForm] /. gamma -> Gamma. – Domen Oct 02 '23 at 15:20
  • 1
    I do not use Python from inside Mathematica, but I know you can call Python from Mathematica? If so, Python itself has function that convert python code to Mathematica! so you just need to figure how to call that function from Mathematica to do and it should return the result back. Here is the code to do it. You just need to figure how to call Python with this code :) Mathematica graphics – Nasser Oct 02 '23 at 19:21
  • 2
    I've put screen shot above to make it easier to read. Here is the code `>python

    from sympy import * eps=symbols('eps') expr=2cos(pieps)/(1-eps)exp(EulerGamma(2eps))/gamma(1-2eps)*eps; mathematica_code(expr)

    '2epsExp[2EulerGammaeps]Cos[Pieps]/((1 - eps)Gamma[1 - 2eps])'The function is calledmathematica_codewhich is part ofsympy` it converts python code to Mathematica code.

    – Nasser Oct 02 '23 at 19:23

0 Answers0