About MathLink and Ruby/Mathematica (for earlier version 0.0.1) library

MathLink(TM) is a library of C language functions that implement a protocol for sending and receiving Mathematica(TM) expressions. Ruby/Mathematica is a library of Ruby with MathLink. Now Ruby/Mathematica does not have all MathLink functions, but we can use only Ruby/Mathematica's method to communicate Mathemtica.

For further details, read the Todd's MathLink tutorial. The tutorial is very easy to understand. You get the tutorial from MathSource(TM). <URL:http://www.mathsource.com/cgi-bin/msitem?0206-693>

However, Todd's tutorial is for MathLink C library, we have to learn to write Ruby program with Ruby/Mathematica library. This document will show you how to write Ruby code to communicate Mathematica kernel.

Using MathLink with Ruby/Mathematica library

Let's look below Ruby code to compute n-th prime number when you give the number n. (This example is ../samples/prime.rb) Note: You do not have to know what is prime number. I mark like # --- *** num *** --- to explain the code.

# prime.rb for Unix
# one of the sample Ruby/Mathematica

# -- *** 1 *** --
require 'mathematica/mathlink'

include Mathematica
include MathLink

# --- *** 2 *** ---
argv = ['-linkname', 'math -mathlink']

link = MathLink.new.open(argv.size, argv)

# --- *** 3 *** ---
print 'Please type positive number:'
num = STDIN.gets

# --- *** 4 *** ---
link.put_function('Prime', 1)
link.put_integer(num)
link.end_packet

# --- *** 5 *** ---
link.new_packet  while(link.next_packet != RETURNPKT)

# --- *** 6 *** ---
printf "%d-th prime number is %d.\n", num, link.get_integer

exit

In next section, I will show details about the prime.rb code.

load Ruby/Mathematica library

First, the program needs to declare Ruby/Mathematica Library.

# -- *** 1 *** --
require 'mathematica/mathlink'

include Mathematica
include MathLink

Then we can call MathLink C library from Ruby.

connect Mathematica kernel

Second, it needs to connect Mathematica kernel.

# --- *** 2 *** ---
argv = ['-linkname', 'math -mathlink']

link = MathLink.new.open(argv.size, argv)

The variable argv includes some Mathematica kernel's launch options. For details about Mathematica kernel's launch options, you can see Mathematica manuals or Todd's tutorial. Anyway, it prepares to use Mathematica kernel. Note that the options is difference when you use Mathematica on non-Unix, e.g. Windows or Macintosh.

get user input using Ruby

Third, we can construct an user interface using Ruby. Ruby is very powerful and very easy to treat user inputs than C or Mathematica. (Using 'gets' that reads stdin or apply to regular expression with Ruby).

# --- *** 3 *** ---
print 'Please type positive number:'
num = STDIN.gets

For explanation, the example is quite simple.

put Mathematica expressions to Mathematica kernel via MathLink

Then we can put some Mathematica expression to Mathematica kernel.

# --- *** 4 *** ---
link.put_function('Prime', 1)
link.put_integer(num)
link.end_packet

Here we let Mathematica to compute num-th prime number. For details about put_function or put_integer or end_packet, see MathLink documents.

wait few seconds for computing.

We have to wait few seconds to compute result.

# --- *** 5 *** ---
link.new_packet  while(link.next_packet != RETURNPKT)

Here is simple skip any packets before the first RETURNPKT.

get an answer from Mathematica kernel

The method link.get_integer returns the result integer.

# --- *** 6 *** ---
printf "%d-th prime number is %d.\n", num, link.get_integer

Now we get num-th prime number from Mathematica.

Another Samples

See ../samples directory. There are some samples that are imported the ones included MathLink C library. It helps you to understand MathLink to compare Ruby samples and C samples. C samples locates the directory , your Mathematica Add-Ons directory.

Some notes

Reference

For Ruby,

For MathLink,

and some member of IRC channels related Ruby and I may help you.

Wishing

I welcome to you to

and all your kindness.

Author

IKEGAMI Daisuke <URL:mailto:daisu-ik@is.aist-nara.ac.jp>

Last modified: "Sun Jun 10 20:54:41 2001"