# Best Practices for converting a Mathematica expression to Julia

**URL:** <https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335>\
**Category:** General Usage\
**Tags:** sympy, mathematica\
**Created:** [December 5, 2018, 5:00pm UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335 "2018-12-05T17:00:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![OvidiusCicero](https://avatars.discourse-cdn.com/v4/letter/o/73ab20/32.png) [@OvidiusCicero](https://discourse.julialang.org/u/OvidiusCicero)\
**Post date:** [December 5, 2018, 5:00pm UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/1 "2018-12-05T17:00:00Z")

</div>

Are there suggestions how to convert a rather long Mathematica expression to Julia?

With Matlab I use [http://library.wolfram.com/infocenter/MathSource/577/](http://library.wolfram.com/infocenter/MathSource/577/)

I also would appreciate suggestions for the same problem with Sage Math

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [December 5, 2018, 5:40pm UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/2 "2018-12-05T17:40:39Z")

</div>

The closest thing to it is this

> **[GitHub - jlapeyre/SymataSyntax.jl: Mathematica syntax for Symata.jl](https://github.com/jlapeyre/SymataSyntax.jl)**
>
> Mathematica syntax for Symata.jl. Contribute to jlapeyre/SymataSyntax.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 5, 2018, 5:53pm UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/3 "2018-12-05T17:53:22Z")

</div>

[Use SymPy.jl](https://github.com/JuliaPy/SymPy.jl). SymPy [can parse Mathematica expressions](https://docs.sympy.org/latest/_modules/sympy/parsing/mathematica.html), and SymPy.jl can convert SymPy expressions to Julia expressions. For example, if we define:

```julia
using SymPy
const sympy_parsing_mathematica = SymPy.PyCall.pyimport("sympy.parsing.mathematica")
mathematica2julia(s::AbstractString, substitutions::Pair{<:AbstractString,<:AbstractString}...) =
    SymPy.lambdify_expr(sympy_parsing_mathematica["mathematica"](s, Dict(substitutions...))).args[2]

```

then we get:

```julia
julia> mathematica2julia("Log[Log[x]]")
:(log(log(x)))

julia> mathematica2julia("Exp[Log3[x]]", "Log3[x]"=>"log(x,3)")
:(x ^ (log(3) ^ -1))

```

---

<div class="post-metadata">

**Author:** ![OvidiusCicero](https://avatars.discourse-cdn.com/v4/letter/o/73ab20/32.png) [@OvidiusCicero](https://discourse.julialang.org/u/OvidiusCicero)\
**Post date:** [December 5, 2018, 6:34pm UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/4 "2018-12-05T18:34:52Z")

</div>

Works charmingly well… thank you!

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [December 6, 2018, 12:32am UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/5 "2018-12-06T00:32:33Z")

</div>

> [@chakravala](#):
>
> The closest thing to it is this

`SymataSyntax` has not been upgraded for v1.0.

---

<div class="post-metadata">

**Author:** ![Xing\_Shi\_Cai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xing_shi_cai/32/14128_2.png) [@Xing\_Shi\_Cai](https://discourse.julialang.org/u/Xing_Shi_Cai)\
**Post date:** [December 14, 2019, 6:24pm UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/6 "2019-12-14T18:24:45Z")

</div>

Ah, this stops working now for SymPy v1.0.10.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 14, 2019, 9:12pm UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/7 "2019-12-14T21:12:23Z")

</div>

> [@Xing\_Shi\_Cai](#):
>
> Ah, this stops working now for SymPy v1.0.10.

The updated code should be:

```julia
using SymPy
const sympy_parsing_mathematica = SymPy.PyCall.pyimport("sympy.parsing.mathematica")
mathematica2julia(s::AbstractString, substitutions::Pair{<:AbstractString,<:AbstractString}...) =
           SymPy.walk_expression(sympy_parsing_mathematica."mathematica"(s, Dict(substitutions...)))

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 14, 2019, 9:38pm UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/8 "2019-12-14T21:38:27Z")

</div>

You may also find [https://github.com/JuliaInterop/MathLink.jl](https://github.com/JuliaInterop/MathLink.jl) helpful.

---

<div class="post-metadata">

**Author:** ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)\
**Post date:** [March 9, 2020, 4:04pm UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/9 "2020-03-09T16:04:27Z")

</div>

How to replace the unknown function?Naively the following code does not work.

```julia
mathematica2julia("(Sqrt[Pi])/(2*Gamma[23/10])","Gamma"=>"gamma")

```

PyError ($(Expr(:escape, :(ccall(#= /Users/gangchen/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:44 =# @pysym(:PyObject\_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) \<class ‘ValueError’\>  
ValueError(“‘Gamma’ function form is invalid.”)  
File “/Users/gangchen/.julia/conda/3/lib/python3.7/site-packages/sympy/parsing/mathematica.py”, line 17, in mathematica  
parser = MathematicaParser(additional\_translations)  
File “/Users/gangchen/.julia/conda/3/lib/python3.7/site-packages/sympy/parsing/mathematica.py”, line 154, in **init**  
d = self.\_compile\_dictionary(additional\_translations)  
File “/Users/gangchen/.julia/conda/3/lib/python3.7/site-packages/sympy/parsing/mathematica.py”, line 187, in \_compile\_dictionary  
raise ValueError(err)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 9, 2020, 5:19pm UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/10 "2020-03-09T17:19:41Z")

</div>

> [@gangchern](#):
>
> How to replace the unknown function?

The dictionary has to map a Mathematica function-call expression to a Python/SymPy function call:

```julia
julia> mathematica2julia("(Sqrt[Pi])/(2*Gamma[23/10])","Gamma[x]"=>"gamma(x)")
:( __prod__ (1 / 2, pi ^ (1 / 2), gamma(23//10) ^ -1))

```

See the documentation for the [`sympy.parsing.mathematica.mathematica` function](https://docs.sympy.org/latest/modules/parsing.html) that is being called here.

---

<div class="post-metadata">

**Author:** ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)\
**Post date:** [March 9, 2020, 10:39pm UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/11 "2020-03-09T22:39:14Z")

</div>

Thanks. That works well

---

<div class="post-metadata">

**Author:** ![Xing\_Shi\_Cai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xing_shi_cai/32/14128_2.png) [@Xing\_Shi\_Cai](https://discourse.julialang.org/u/Xing_Shi_Cai)\
**Post date:** [April 7, 2020, 5:34pm UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/12 "2020-04-07T17:34:44Z")

</div>

I got a Julia expression if I do this. How to turn expression that I get into a function?

---

<div class="post-metadata">

**Author:** ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)\
**Post date:** [April 21, 2020, 1:10pm UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/13 "2020-04-21T13:10:00Z")

</div>

you can use SyntaxTree.jl. The genfun is useful. You can also see the examples in my [UseMathLink.jl](https://github.com/gangchern/usingMathLink)

---

<div class="post-metadata">

**Author:** ![Xing\_Shi\_Cai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xing_shi_cai/32/14128_2.png) [@Xing\_Shi\_Cai](https://discourse.julialang.org/u/Xing_Shi_Cai)\
**Post date:** [April 21, 2020, 3:18pm UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/14 "2020-04-21T15:18:25Z")

</div>

I eventually ended up using sage and copy paste to Julia since the syntax are quite similar. The only problem I found so far is that sage convert `Exp(2)` from Mathematica to `e^2`. But I often want it written as `exp(2)`.

---

<div class="post-metadata">

**Author:** ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)\
**Post date:** [August 13, 2023, 10:43pm UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/15 "2023-08-13T22:43:30Z")

</div>

I am trying to run this now, but I got some errors. I tried to fix it and I got this function

```julia
using SymPy
const sympy_parsing_mathematica = SymPy.PyCall.pyimport("sympy.parsing.mathematica")
mathematica2julia(s::AbstractString, substitutions::Pair{<:AbstractString,<:AbstractString}...) =
           SymPy.walk_expression(sympy_parsing_mathematica."parse_mathematica"(s))

```

When I now try, I get

```julia
mathematica2julia("(Sqrt[Pi])/(2*EllipticE[m])","EllipticE[x]"=>"elliptic_e(x)")
# :((1 / 2) * SymPy. __POW__ (pi, 1 / 2) * SymPy. __POW__ (EllipticE(m), -1))

```

My goal is to get

```julia
sqrt(pi) / (2*elliptic_e(m))

```

How can I get this?

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [August 14, 2023, 2:05am UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/16 "2023-08-14T02:05:49Z")

</div>

You don’t really need the walk expression part, but you need to add the EllipticE function to help sympy out. This seems to work:

```julia
sympy_parsing_mathematica.mathematica(s, Dict("EllipticE[x]"=>"elliptic_e(x)"))

```

---

<div class="post-metadata">

**Author:** ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)\
**Post date:** [August 14, 2023, 2:20am UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/17 "2023-08-14T02:20:19Z")

</div>

Thanks, but I don’t understand the suggestion.  
Following what you wrote I tried to run:

```julia
sympy_parsing_mathematica.mathematica("(Sqrt[Pi])/(2*EllipticE[m])", Dict("EllipticE[x]"=>"elliptic_e(x)"))

```

I get a latex expression. But I actually want to get an expression I then can copy into a julia function like

> [@roi.holtzman](#):
>
> `sqrt(pi) / (2*elliptic_e(m))`

Do you know how to do that?

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [August 14, 2023, 11:55am UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/18 "2023-08-14T11:55:19Z")

</div>

Okay, the “ **POW** ” you are seeing was removed somewhat recently, and `^` should be used in the returned expression. You can update SymPy, or try passing in a dictionary holding the conversion, like `SymPy.walk_expression(out, fns=Dict("Pow"=>:^))`.

---

<div class="post-metadata">

**Author:** ![roi.holtzman](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@roi.holtzman](https://discourse.julialang.org/u/roi.holtzman)\
**Post date:** [August 14, 2023, 2:00pm UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/19 "2023-08-14T14:00:47Z")

</div>

Thanks again!  
But I think the substitutions do not work anyway. In this example

```julia
mathematica2julia("(Sqrt[Pi])/(2*EllipticE[m])", "EllipticE[x]"=>"elliptic_e(x)")
# :((1 / 2) * SymPy. __POW__ (pi, 1 / 2) * SymPy. __POW__ (EllipticE(m), -1))

```

I get that `EllipticE[m]` is not substituted by `elliptic_e(m)`.

---

<div class="post-metadata">

**Author:** ![CarloLucibello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlolucibello/32/3278_2.png) [@CarloLucibello](https://discourse.julialang.org/u/CarloLucibello)\
**Post date:** [August 14, 2023, 2:39pm UTC](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/20 "2023-08-14T14:39:44Z")

</div>

Github Copilot is quite good for these kind of tasks. I use it to convert from latex to Julia.

[Next page](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335.md?page=2)
