How to copy Mathematica expression into a Julia function

Very similar questions were asked before in the discourse (see e.g. this, this, and this). Answers point to MathLink where one can plug an expression like this

using MathLink
sqx = W`Sqrt[x]`

and then evaluate this function like so

weval(sqx; x=2.0)
# 1.4142135623730951

I would like to define a normal Julia function like

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).

Why not simply

using MathLink
const sqx = W`Sqrt[x]`
f(x) = weval(sqx; x=x)

?

I assume you mean?

f(x) = weval(sqx; x)

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

typeof(f(2))
# MathLink.WExpr

I would like to get just a Float64.

f(x) = weval(Float64, sqx; x)

?

1 Like