# Is there an interface to Mathematica through wolframescript?

**URL:** <https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727>\
**Category:** General Usage\
**Tags:** mathematica\
**Created:** [March 8, 2020, 5:43pm UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727 "2020-03-08T17:43:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)\
**Post date:** [March 8, 2020, 5:43pm UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/1 "2020-03-08T17:43:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [March 8, 2020, 7:15pm UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/2 "2020-03-08T19:15:46Z")

</div>

Have you tried [Mathematica.jl](https://github.com/JuliaInterop/Mathematica.jl)?

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [March 8, 2020, 7:34pm UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/3 "2020-03-08T19:34:36Z")

</div>

Not directly answering your question, but if you’re not tied to Mathematica you could use [SymPy.jl](https://github.com/JuliaPy/SymPy.jl) or [SymEngine.jl](https://github.com/symengine/SymEngine.jl) for symbolic computations.

---

<div class="post-metadata">

**Author:** ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)\
**Post date:** [March 9, 2020, 8:37am UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/4 "2020-03-09T08:37:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)\
**Post date:** [March 9, 2020, 8:44am UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/5 "2020-03-09T08:44:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [March 9, 2020, 8:48am UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/6 "2020-03-09T08:48:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [March 9, 2020, 9:35am UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/7 "2020-03-09T09:35:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)\
**Post date:** [March 9, 2020, 9:46am UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/9 "2020-03-09T09:46:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [March 9, 2020, 9:55am UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/10 "2020-03-09T09:55:19Z")

</div>

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

```julia
(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).

---

<div class="post-metadata">

**Author:** ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)\
**Post date:** [March 9, 2020, 10:25am UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/12 "2020-03-09T10:25:41Z")

</div>

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.

 ![WeChat11ca76e99c67ea49ca09a91bfb727e2c](https://global.discourse-cdn.com/julialang/original/3X/6/1/6103e04876f7aad609c9a33949750dee0869ddfa.png)

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [March 9, 2020, 11:03am UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/13 "2020-03-09T11:03:00Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [March 9, 2020, 11:15am UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/14 "2020-03-09T11:15:27Z")

</div>

Agreed. But from the [README](https://github.com/JuliaInterop/Mathematica.jl/blob/master/README.md) it seems like Mathematica.jl supported plotting but MathLink.jl [doesn’t](https://github.com/JuliaInterop/MathLink.jl/issues/36)?

---

<div class="post-metadata">

**Author:** ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)\
**Post date:** [March 9, 2020, 11:21am UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/15 "2020-03-09T11:21:58Z")

</div>

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

 ![WeChat5a009bd1ed79d30c4c75124cd5b0e33b](https://global.discourse-cdn.com/julialang/original/3X/8/8/8868563b69cf7e70f1b913de8db89d7855fd9b2d.png)

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [March 9, 2020, 11:48am UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/16 "2020-03-09T11:48:13Z")

</div>

If you just want a Number, you can try

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

````

---

<div class="post-metadata">

**Author:** ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)\
**Post date:** [March 10, 2020, 4:39pm UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/17 "2020-03-10T16:39:17Z")

</div>

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

```julia
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.

```julia
symExpr=math2symEngine(MLExpr) #Transform from mathematica MathLink output to symEngine expression
juExpr=math2Expr(MLExpr) #Transform from mathematica MathLink output to Julia expression

```

```julia
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

```julia
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)

 ![WeChat2fe5564a74aaf1a5ec2cbda4c293ae97](https://global.discourse-cdn.com/julialang/original/3X/5/a/5aacd794e21aac25b1e333c0e8dc5082e45ff132.png)

The whole document can be download from my homepage.

> **[GitHub - AmplitudeGravity/usingMathLink: It convert the MathLink output to...](https://github.com/AmplitudeGravity/usingMathLink)**
>
> It convert the MathLink output to Julia function and symengine function. The functions are used to perform the integral by Cuba - GitHub - AmplitudeGravity/usingMathLink: It convert the MathLink ou...

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [April 3, 2020, 12:07pm UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/19 "2020-04-03T12:07:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![xuy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xuy/32/37137_2.png) [@xuy](https://discourse.julialang.org/u/xuy)\
**Post date:** [April 3, 2020, 12:22pm UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/21 "2020-04-03T12:22:26Z")

</div>

My Mathematica version is 12.1.

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [April 3, 2020, 12:27pm UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/22 "2020-04-03T12:27:16Z")

</div>

Can you run `wolframscript` from your terminal?

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [April 3, 2020, 12:51pm UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/24 "2020-04-03T12:51:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [April 3, 2020, 2:24pm UTC](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727/26 "2020-04-03T14:24:55Z")

</div>

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

[Next page](https://discourse.julialang.org/t/is-there-an-interface-to-mathematica-through-wolframescript/35727.md?page=2)
