# How to copy Mathematica expression into a Julia function

**URL:** <https://discourse.julialang.org/t/how-to-copy-mathematica-expression-into-a-julia-function/97670>\
**Category:** Modelling & Simulations\
**Tags:** question, mathematica\
**Created:** [April 19, 2023, 7:23pm UTC](https://discourse.julialang.org/t/how-to-copy-mathematica-expression-into-a-julia-function/97670 "2023-04-19T19:23:05Z")\
**Posts on this page:** 8\
**Page:** 1

<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:** [April 19, 2023, 7:23pm UTC](https://discourse.julialang.org/t/how-to-copy-mathematica-expression-into-a-julia-function/97670/1 "2023-04-19T19:23:05Z")

</div>

Very similar questions were asked before in the discourse (see e.g. [this](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/13), [this](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/3), and [this](https://discourse.julialang.org/t/converting-an-expression-to-a-function/44736)). Answers point to `MathLink` where one can plug an expression like this

```julia
using MathLink
sqx = W`Sqrt[x]`

```

and then evaluate this function like so

```julia
weval(sqx; x=2.0)
# 1.4142135623730951

```

I would like to define a normal Julia function like

```julia
f(x) = sqrt(x)

```

using `sqx` above. Is there a way to do that?

My end goal is to be able to take the string output from Mathematica and edit some of the variables to define such a function `f(x)`.

---

<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:** [April 19, 2023, 7:29pm UTC](https://discourse.julialang.org/t/how-to-copy-mathematica-expression-into-a-julia-function/97670/2 "2023-04-19T19:29:12Z")

</div>

Why not simply

```julia
using MathLink
const sqx = W`Sqrt[x]`
f(x) = weval(sqx; x=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:** [April 19, 2023, 7:31pm UTC](https://discourse.julialang.org/t/how-to-copy-mathematica-expression-into-a-julia-function/97670/3 "2023-04-19T19:31:41Z")

</div>

I assume you mean?

```julia
f(x) = weval(sqx; x)

```

When I plug a number to this `f` I get the following type

```julia
typeof(f(2))
# MathLink.WExpr

```

I would like to get just a `Float64`.

---

<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:** [April 20, 2023, 12:04pm UTC](https://discourse.julialang.org/t/how-to-copy-mathematica-expression-into-a-julia-function/97670/4 "2023-04-20T12:04:19Z")

</div>

> [@roi.holtzman](#):
>
> I would like to get just a `Float64`.

```julia
f(x) = weval(Float64, sqx; 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 8, 2023, 3:12pm UTC](https://discourse.julialang.org/t/how-to-copy-mathematica-expression-into-a-julia-function/97670/5 "2023-08-08T15:12:37Z")

</div>

I am reviving this post with a follow-up question.

Is there a way to get an expression from mathematical, for example `Sqrt[x y]` to be typed in julia like

```julia
Sqrt[x y] 

```

would be

```julia
sqrt(x*y)

```

Thanks!

---

<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:** [August 8, 2023, 3:54pm UTC](https://discourse.julialang.org/t/how-to-copy-mathematica-expression-into-a-julia-function/97670/6 "2023-08-08T15:54:14Z")

</div>

> [@roi.holtzman](#):
>
> Is there a way to get an expression from mathematica

See: [Best Practices for converting a Mathematica expression to Julia - #3 by stevengj](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/3)

---

<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, 4:09pm UTC](https://discourse.julialang.org/t/how-to-copy-mathematica-expression-into-a-julia-function/97670/7 "2023-08-14T16:09:50Z")

</div>

For completeness, the answer I was looking for was given in this [post](https://discourse.julialang.org/t/best-practices-for-converting-a-mathematica-expression-to-julia/18335/22) and this is the solution (for more details read the thread in the other post).

```julia
using SymPy
const sympy_parsing_mathematica = SymPy.PyCall.pyimport("sympy.parsing.mathematica")

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

# option 1
SymPy.walk_expression(ex, fns=Dict("Pow"=>:^)) 
#:((1 / 2) * pi ^ (1 / 2) * elliptic_e(m) ^ -1)

# option 2 (should work soon after some updates of SymPy)
SymPy.convert_expr(ex, use_julia_code=true) 
# :(sqrt(pi) ./ (2 * elliptic_e(m)))

```

---

<div class="post-metadata">

**Author:** ![L2Fish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/l2fish/32/207620_2.png) [@L2Fish](https://discourse.julialang.org/u/L2Fish)\
**Post date:** [March 16, 2024, 6:03am UTC](https://discourse.julialang.org/t/how-to-copy-mathematica-expression-into-a-julia-function/97670/8 "2024-03-16T06:03:13Z")

</div>

[https://github.com/JuliaInterop/MathLink.jl/issues/85](https://github.com/JuliaInterop/MathLink.jl/issues/85)
