Mr  Mathematica:   Calling Mathematica from Scheme
1 Install Mr  Mathematica
2 Use Mr  Mathematica
Math  Kernel
Math  Link?
Math  Eval
exn:  fail:  mathlink
Math  Exit
3 Use Mr  Mathematica without Mr  Ed
4 Translation between Scheme and Mathematica

MrMathematica: Calling Mathematica from Scheme

Chongkai Zhu

Mathematica is a registered trademark of Wolfram Research, Inc.

MrMathematica allows you to call Mathematica from Racket. Wolfram Engine is also tested to work.

1 Install MrMathematica

Before installing MrMathematica, you need to do the following two things:

Install mrmathematica.plt from File menu in DrScheme.

2 Use MrMathematica

 (require mrmathematica) package: base


(MathKernel arg ...)  MathLink?

  arg : string?
Opens a MathLink connection. The arguments are passed to MathLink function MLOpenArgcArgv. If no argument is given, "-linkname" "math -mathlink" will be used as default, which in general will launch a new local Mathematica kernel. Remote MathKernel is supported.


(MathLink? v)  boolean?

  v : any/c
Returns #t if v is a MathLink connection, #f otherwise.


(current-mathlink)  (or false? MathLink?)

(current-mathlink v)  void?
  v : (or false? MathLink?)
A parameter that determines the default MathLink connection to use. #f indicates no current connection. Each time MathKernel is called, the return value will be automatically set as current-mathlink.


(MathEval Mexp [ml])  Mexp

  Mexp : 
(flat-rec-contract Mexp
  number? boolean? symbol? string? void? eof-object?
  (vectorof Mexp)
  (cons/c Mexp (listof Mexp)))
  ml : MathLink? = (current-mathlink)

Caution: For floating point numbers, PLT Scheme only supports machine precision.

Uses Mathematica to evaluate. You should write Mexp as an S-exp and it will be translated to Mathematica style automatically. Only number, boolean, symbol, string, void, eof, vector of Mexps, or none empty list of Mexps is recognized as Mexp. See Translation between Scheme and Mathematica for details.

The optional argument ml specifies which Mathematica kernel to be used to do the computation. If no connection is given and (current-mathlink) is #f, (MathKernel) will be called to create one.

> (MathEval '(Integrate (/ 1 (+ (expt x 2) 1)) x))

'(atan x)

> (MathEval '(Integrate (/ 1 (+ (expt x 2) -1)) x))

'(+ (* 1/2 (log (+ 1 (* -1 x)))) (* -1/2 (log (+ 1 x))))

> (MathEval '(Integrate (expt x 2) #(x 0 1)))


> (define f (MathEval '(Integrate (expt x 2) x)))
> f

'(* 1/3 (expt x 3))

> (define s (eval `(lambda (x) ,f) (make-base-namespace)))
> (- (s 1) (s 0))


> (define (factor-integer n)
    (MathEval `(FactorInteger ,n)))
> (factor-integer 111111111111111111)

'#(#(3 2) #(7 1) #(11 1) #(13 1) #(19 1) #(37 1) #(52579 1) #(333667 1))

MathEval is thread-safe. Breaks during MathEval are forwarded to Mathematica.


(Mexp->image Mexp [ml])  (is-a?/c image-snip%)

  Mexp : Mexp
  ml : MathLink? = (current-mathlink)
Converts Mexp to an image. For example:

> (Mexp->image '(Plot (sin x) #(x 0 (* 2 Pi))))


(struct exn:fail:mathlink exn:fail ()
    #:extra-constructor-name make-exn:fail:mathlink
Raised for MathLink error.


(MathExit [ml])  void?

  ml : MathLink? = (current-mathlink)
Closes MathLink connection ml. If ml is already closed, MathExit has no effect.

3 Use MrMathematica without MrEd

 (require mrmathematica/light) package: base

The same as (require mrmathematica), for command line use in case The Racket Graphical Interface Toolkit is not available. It doesn’t provide Mexp->image.

4 Translation between Scheme and Mathematica

S-exp such as (f x y) is automatically translated to f[x,y] and send to Mathematica Kernel. Scheme vector such as #(1 2) is translated to Mathematica list {1,2}. The return expression of Mathematica is translated back into Scheme. Besides, MrMathematica also use the following dictionary to translate functions between Scheme and Mathematica:

((* . Times)
 (- . Minus)
 (+ . Plus)
 (/ . Divide)
 (< . Less)
 (<= . LessEqual)
 (= . Equal)
 (> . Greater)
 (>= . GreaterEqual)
 (abs . Abs)
 (acos . ArcCos)
 (and . And)
 (angle . Arg)
 (asin . ArcSin)
 (atan . ArcTan)
 (begin . CompoundExpression)
 (ceiling . Ceiling)
 (cos . Cos)
 (denominator . Denominator)
 (exp . Exp)
 (expt . Power)
 (floor . Floor)
 (gcd . GCD)
 (if . If)
 (imag-part . Im)
 (lcm . LCM)
 (list . List)
 (log . Log)
 (magnitude . Abs)
 (max . Max)
 (min . Min)
 (modulo . Mod)
 (negative? . Negative)
 (not . Not)
 (number? . NumberQ)
 (numerator . Numerator)
 (or . Or)
 (positive? . Positive)
 (quotient . Quotient)
 (rationalize . Rationalize)
 (round . Round)
 (sin . Sin)
 (sqrt . Sqrt)
 (string-length . StringLength)
 (tan . Tan)
 (truncate . IntegerPart))

The translation table is defined in "". You can change this file to adapt for your special needs, say you just want no translation, or, you want some more functions that are similar in Mathematica and Scheme also to be automatically translated. Please also note that the right hand side of the table can also be a function that takes the all the arguments as input, thus enable arbitrary translation rule. See the - and Rational rule in "" as examples.