Using Julia to Interact with Mathematica

Written by on July 22, 2016 in Julia, Mathematica, Programming with 0 Comments

Mathematica is a powerful environment for symbolic and numerical computation. I have been using it for many years now. In this post I had explained how we can use Mathematica bundled with Raspberry distribution to control littleBits devices.

When I saw that there is support in Julia for interacting with Mathematica, I decided to investigate it further. Please note that for this to work, you must have a local copy of Mathematica; Julia does not replace Mathematica.

The package Mathematica.jl is required for this interaction. I found out that this package has dependency on a couple of other files (MathLink) that are not automatically installed by Julia’s package manager. The files are located here: https://github.com/MikeInnes/MathLink.jl. So I manually downloaded the ZIP file and extracted the SRC files to my local directory.

For convenience in launching Julia from the Terminal on my iMac (that is where I have my Mathematica installation), I set up an alias:

alias julia=”/Applications/Julia-0.4.6.app/Contents/Resources/julia/bin/julia”

Launch Julia on the Terminal by typing julia.

We have to install the Mathematica.jl package just once through Julia’s package manager. In my case, I had already installed it in an earlier session. The next important step is to tell Julia where it can locate the MathLink dependency files. See the following image:

Julia Session

Julia Session

 

Now we are all set to interact with Mathematica. The nice thing is that we need not launch Mathematica manually for this interaction to work.

Let us start by calculating the Nth prime number, where N is 10^10:

julia> Prime(10^10)

252097800623

Note that the answer is coming from Mathematica, not Julia.

Next, let us create an array of Lucas numbers for N = 10 to 20:

julia> res = [LucasL(n) for n = 10:20]

11-element Array{Any,1}:

   123

   199

   322

   521

   843

  1364

  2207

  3571

  5778

  9349

 15127

What is interesting is that the result is a Julia expression, so we can access the elements of the returned array.

julia> res[1] + res[2]

322

Because the syntax of Mathematica and Julia are different, one way to execute an actual Mathematica expression in Julia is to represent it as a String and call Mathematica’s ToExpression function on the String.

As an example, let us use Mathematica to play some music – the notes C4 and D4, each lasting 1 second:

julia> ToExpression(“””EmitSound[Sound[{SoundNote[“C4”], SoundNote[“D4″]}]]”””)

When you execute the above code, you can hear Mathematica playing music.

A slight variation of the above is to play a Chord using notes C4-E4-G4, for 0.5 second using Violin:

julia> ToExpression(“””EmitSound[Sound[SoundNote[{“C4”, “E4”, “G4”}, 0.5, “Violin”]]]”””)

As the final example, I can define my own Mathematica function and execute it:

julia> ToExpression(“MyFunction[a_] = a ^ 34;MyFunction[2]”)

17179869184

So it is quite remarkable that we can interact with Mathematica and take advantage of its vast computational power from within Julia. I am sure that as Julia evolves, further enhancements to the Mathematica.jl package will be made.

Tags: ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *

Top