Interoperability between Symbolics.jl and MathLink.jl

Dear All,

Is interoperability possible between Symbolics.jl and MathLink.jl? I would like to create a symbolic object in Julia, do part of the calculation using Symbolics.jl and then pass some intermediate symbolic object to Mathematica or Wolfram Engine via MathLink.jl for further manipulation, and then convert it back to Symbolics variables.

For example, consider the following example, where I multiply two matrices in Symbolics.jl and then compute the eigenvalues of the new matrix in MathLink.jl.

using Symbolics, MathLink


@variables α β
a = Symbolics.variables(:a, 1:2, 1:2) 
a[1,2] = a[2,1] 
# will create 
# 2×2 Matrix{Num}:
# a₁ˏ₁  a₂ˏ₁
# a₂ˏ₁  a₂ˏ₂

b = Symbolics.variables(:b, 1:2, 1:2)
b[1,1] = α
b[2, 2] = β
b[2, 1] = 0
b[1, 2] = 0

# at this stage b is
# 2×2 Matrix{Num}:
# α  0
# 0  β

c = a*b 
# c is equal to 
# 2×2 Matrix{Any}:
# a₁ˏ₁*α  a₂ˏ₁*β
# a₂ˏ₁*α  a₂ˏ₂*β

To compute the eigenvalues of c in Mathematica, I convert c into a MathLink.WExpr object manually and call the Wolfram function Eigenvalues via weval as follows:

weval(W`
Eigenvalues[{{a₁ˏ₁*\[Alpha], a₂ˏ₁*\[Beta]}, {a₂ˏ₁*\[Alpha], a₂ˏ₂*\[Beta]}}]
`)     

which gives the correct eigenvalues, but now I have to painstackingly bring back the MathLink.WExpr into Symbolics variables. I have to do these steps manually, and I was wondering if there is any way of automating the process? I found the Julia package https://github.com/eswagel/SymbolicsMathLink.jl which I believe has similar use cases, unfortunately it does not work even for simplest of examples and seem to have many bugs.

Any tips or suggestions regarding how to automate this conversion process between Symbolics.jl and MathLink.jl will be much appreciated.

(Of course, I am talking about this specific example just to illustrate what my intention is. My actual use case is somewhat involved and requires to use some special functions in Mathematica that is not currently possible in Symbolics.jl.)

There’s nothing fundamental that stops it, but I don’t think any link has gotten serious attention. It’s worth opening an issue on Symbolics.jl, though it may not get attention for a while.

3 Likes

Thanks for your response @ChrisRackauckas , I will open an issue on Symbolics.jl !