WolframExpr
Incomplete work in progress. Open an issue 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]);
ERROR: MethodError: no method matching wexpr_to_expr(::String) Closest candidates are: wexpr_to_expr(!Matched::MathLink.WExpr) @ WolframExpr /__w/WolframExpr.jl/WolframExpr.jl/src/WolframExpr.jl:102 wexpr_to_expr(!Matched::MathLink.WSymbol) @ WolframExpr /__w/WolframExpr.jl/WolframExpr.jl/src/WolframExpr.jl:90 wexpr_to_expr(!Matched::Number) @ WolframExpr /__w/WolframExpr.jl/WolframExpr.jl/src/WolframExpr.jl:98
julia> A(x, y) = x^2 + y^2;
julia> f(A, 1, 2)
ERROR: UndefVarError: `f` not defined
For best results using expressions from Mathematica, pass your expression through FullForm to remove special syntax.
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 MathLink. See its documentation for installation and troubleshooting directions.
API
WolframExpr.string_to_expr
— Methodstring_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_function
— Methodstring_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_wexpr
— Methodstring_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_expr
— Methodwexpr_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)