WolframExpr

Incomplete work in progress building on work by github.com/eswagel. Open an issue or pull request if you find bugs or have requests.

WolframExpr is a package for translating Wolfram Language expressions to Julia functions.

Mathematica, Wolfram, MathLink are all trademarks of Wolfram Research.

The primary function is string_to_function, which converts a string containing a Wolfram Language expression to a Julia function.

For example,

julia> using WolframExpr
julia> f = string_to_function("A[x,y]+y", [:A, :x, :y]);
julia> A(x, y) = x^2 + y^2;
julia> f(A, 1, 2)7

For best results using expressions from Mathematica, pass your expression through FullForm to remove special syntax.

SymbolicsMathLink.jl has related functionality.

Installation

WolframExpr depends on MathLink.jl, which requires an installation of either Mathematica or the free Wolfram Engine. If those exist, the usual Julia installation methods will install WolframExpr:

] add https://github.com/musoke/WolframExpr.jl

If this fails, the problem is likely with MathLink.jl. See its documentation for installation and troubleshooting directions.

API

WolframExpr.string_to_exprMethod
string_to_expr(string)

Convert a string to a Julia Expr.

Examples

julia> using WolframExpr

julia> string_to_expr("(x+y)/2")
:((x + y) * 2 ^ -1)
WolframExpr.string_to_functionMethod
string_to_function(string, symbols)

Convert a string to a Julia function with arguments in symbols.

The resulting method has signature matching symbols.

Examples

julia> using WolframExpr

julia> f = string_to_function("x+y", [:x, :y]);

julia> f(1, 2)
3

julia> f(1.2, 2)
3.2
WolframExpr.string_to_wexprMethod
string_to_wexpr(wolfram)

Convert a string to a MathLink.WExpr.

Very shallow wrapper around MathLink's parsing.

Examples

julia> using WolframExpr

julia> string_to_wexpr("Sin[1]")
W`Sin[1]`
WolframExpr.wexpr_to_exprMethod
wexpr_to_expr(wolfram)

Convert a Wolfram expression from a MathLink.WExpr to a Julia expression.

Examples

julia> using MathLink, WolframExpr

julia> wexpr_to_expr(W`1. + 1`)
:(1.0 + 1.0)