Is there an interface to Mathematica through wolframescript?

It should working as:

  1. input from julia , transform to Mathematica input ,
  2. call run command: wolframescript some input .
  3. read the mathematica output and transform to julia output form

So that I can use the mathematica CAS to get symbolic result and then using it through numerical programe.

PS. I know there is MathLink.jl but it seem hard to install it.

1 Like

Have you tried Mathematica.jl?

Not directly answering your question, but if you’re not tied to Mathematica you could use SymPy.jl or SymEngine.jl for symbolic computations.

Yes, I have tried that. But it related to MathLink which fails to be installed in my computer.

Thanks for the suggestion anyway. I have tried both of them. But sympy.jl seems quite slow. SymEngine.jl is faster but it is not quite mature now. It does not even contain solve, differential equation solve and integration functions.

Yeah, Mathematica ist a beast when in comes to symbolic computations. But in my cases I often don’t mind the slowdown of SymPy. My bottlenecks are mostly in numeric simulations in Julia. Anyways, good luck with getting the Mathematica interop to work.

1 Like

Maybe you could elaborate on what fails? I had zero issues installing or using it

Wooo,that’s a good news. Could you tell me how to install mathlink.jl?

I’m confused. Which Julia version are you on? I immediately get when trying to ] add or ] dev it

(mathinterop) pkg> dev --local https://github.com/JuliaInterop/Mathematica.jl
    Cloning git-repo `https://github.com/JuliaInterop/Mathematica.jl`
ERROR: could not find project file in package at https://github.com/JuliaInterop/Mathematica.jl

However, installing MathLink.jl works fine and since Mathematica.jl is a tiny wrapper around it, it should be easy to port. In fact, I’m giving it half an hour of my time right now. Will report back if I get it to work (in which case I’ll make a PR).

1 Like

Thanks a lot. I use Julia 1.1.0. And the MathLink.jl seems works now in my computer. But when I try to use Mathematica.jl. It generate the following errors. I’m totally helpless with this kind of problem.

You can do what you want with MathLink alone, like this:

using MathLink
W```
<here come your Mathematica commands>
``` |> weval
1 Like

Agreed. But from the README it seems like Mathematica.jl supported plotting but MathLink.jl doesn’t?

That is cool. Thanks a lot. I tries one example. It works. Perhaps the Mathematica.jl can make the output results looks more nice.

If you just want a Number, you can try

W```Integrate[x^(1/2)(1+x)^(-23/10),{x,0,1}] // N``` |> weval
1 Like

Thanks for the help from all of you. Now the problem is solved.

using SymEngine
using SyntaxTree
using Cuba, SpecialFunctions
using MathLink
function math2symEngine(symb::MathLink.WSymbol)
    SymEngine.symbols(symb.name)
end
function math2symEngine(num::Int)
    num
end
function math2symEngine(num::Number)
    num
end
function math2symEngine(expr::MathLink.WExpr)
    if expr.head.name=="Times"
        return *(map(math2symEngine,expr.args)...)
    elseif expr.head.name=="Plus"
        return +(map(math2symEngine,expr.args)...)
    elseif expr.head.name=="Power"
        return ^(map(math2symEngine,expr.args)...)
    elseif expr.head.name=="Rational"
        return  //(map(math2symEngine,expr.args)...)
    else
        return SymEngine.SymFunction(expr.head.name)(map(math2symEngine,expr.args)...)
    end
end
#Mathematica to julia expr
function math2Expr(symb::MathLink.WSymbol)
    Symbol(symb.name)
end
function math2Expr(num::Number)
    num
end
function math2Expr(expr::MathLink.WExpr)
    if expr.head.name=="Times"
        return Expr(:call, :*, map(math2Expr,expr.args)...)
    elseif expr.head.name=="Plus"
        return Expr(:call, :+,map(math2Expr,expr.args)...)
    elseif expr.head.name=="Power"
        return Expr(:call, :^, map(math2Expr,expr.args)...)
    elseif expr.head.name=="Rational"
        return  Expr(:call, ://, map(math2Expr,expr.args)...)
    else
        return Expr(:call, Symbol(expr.head.name), map(math2Expr,expr.args)...)
    end
end
function myconvert(::Type{Expr},ex::Basic)
    Expr(:call, :*, Symbol(SymEngine.toString(ex)), 1)
end
function evalSym(ex::SymEngine.Basic)
    fn = SymEngine.get_symengine_class(ex)     
    if fn == :FunctionSymbol
        as=get_args(ex)
        return Expr(:call, Symbol(get_name(ex)), [evalSym(a) for a in as]...)|>eval
    elseif fn == :Symbol
        return Symbol(SymEngine.toString(ex))|>eval
    elseif (fn in SymEngine.number_types) || (fn == :Constant)    
        return N(ex)|>eval
    elseif fn==:Mul
        as=get_args(ex)
        return *([evalSym(a) for a in as]...) 
    elseif fn==:Add
        as=get_args(ex)
        return +([evalSym(a) for a in as]...) 
    elseif fn==:Pow
        as=get_args(ex)
        return ^([evalSym(a) for a in as]...)
    elseif fn==:Rational
        as=get_args(ex)
        return //([evalSym(a) for a in as]...)
    end
end
macro genfun(expr,args)
    :($(Expr(:tuple,args.args...))->$expr)
end
genfun(expr,args) = :(@genfun $expr [$(args...)]) |> eval

Using MathLink to get the mathematica result.

symExpr=math2symEngine(MLExpr) #Transform from mathematica MathLink output to symEngine expression
juExpr=math2Expr(MLExpr) #Transform from mathematica MathLink output to Julia expression
symfun=lambdify(math2symEngine(tb),(:α2,:α3,:α4)) #Transform to the symEgnine function
jufun=genfun(juExpr,[:α2,:α3,:α4]) #Transform to the julia function 

Using the following code to generate the Cuba integrand function

function intSec1t(x, f)#α1 sector decomposition
    tmp=tsym(Complex.(x)...)
    f[1],f[2]=reim(tmp)
end
function intSec1s(x, f)#α1 sector decomposition   
    tmp=jufun(Complex.(x)...)
    f[1],f[2]=reim(tmp)
end

The out put results are as following (intSec1t is from the symEngine function and intSec1s is from the Julia function)

The whole document can be download from my homepage.

4 Likes

No idea, it loads fine for me.
Can you perhaps post your ]st and versioninfo() outputs? What Mathematica version did you install?

My Mathematica version is 12.1.

Can you run wolframscript from your terminal?

Cool to see someone’s using my SyntaxTree package… I’m curious where you got the idea to use it.

1 Like

I’m afraid I cannot help you, since I cannot replicate your problem. Some MathLink developer might.