Ruby/Mathematica library

In this documents we will explain about two classes, named Mathematica and Computation.

module Mathematica

Mathematica module is an abstract module of Ruby/Mathematica

class Mathematica

Mathematica class is an interface of Ruby/Mathematica

instance methods in Mathematica::Mathematica class.

#start(opt=nil)
starts Mathematica.
When opt is omitted, then a Mathematica kernel may start with default
option of Unix. (``-linkname "math -mathlink"'')

For example:

  math = Mathematica.new
  math.start
#eval_foreground(exp)
sends expression exp to the Mathematica kernel and evaluates on foreground.
This method may take a while to compute the expression.
See also #eval_background.

For example:

  puts math.eval_foreground('Integrate[Cos[x], x]') #=> 'Sin[x]'
#eval_background(exp)
sends expression exp to the Mathematica kernel and evaluates on background.
This method products a thread to compute the expression and returns
a Computation object. See also #eval_foreground and class Computation.

For example:

  r = math.eval_background('Integrate[Cos[x], x]') #=> a Computation obj.
  # ... some jobs ...
  sleep(1) until r.state
  puts r.result # => 'Sin[x]'

Computation class

Computation class is an abstract class to describe a computation on Mathematica kernel. Computation object has two fields, that are #state and #result. While Mathematica kernel is computing, #state returns false or nil, otherwise when computing is finished #state returns true. Then #result returns a meaningful answer to given expressions. Mathematica#eval_background treats Computation objects. See also Mathematica#eval_background.

#state

returns true or false or nil. if #state returns true then given computing is finished, otherwise computing is not finished or occurs some errors. You may have to wait until #state becomes true. See also Mathematica#eval_background.

#result

returns result if computation is finished. You can check by call #state whether computation is finished. See also #state and Mathematica#eval_background.