SchemeLink for Mathematica


SchemeLink aims at full combination of Mathematica and Scheme. SchemeLink allows user either to call PLT Scheme from Mathematica(in the notebook interface of Mathematica), or to call Mathematica from PLT Scheme (either in stand alone MzScheme or in DrScheme). In both direction, ALL Mathemativca/PLT Scheme commands are supported.

As Mathematica users, we know that in some cases a forign language interface to a general purpose programming language is needed. MathLink and J/Link are provided for such purpose. SchemeLink tries to do better than the J/Link. Afterall, Lisp/Scheme is a functional programming language (the same as Mathematica), while Java is Object Oriented.

System Requirements


SchemeLink for PLT v3xx

Compiled version for Windows/PLT v372 (No C compiler needed if you install this one on Windows. It also works for other platforms, but a C compiler is needed during installation)

Before installing, you need to tell the system where MathLink is (if you use the pre-compiled version, this is not needed). Under Windows, I do it by setting the OS enviroment variable "PLT_EXTENSION_LIB_PATHS" to "C:\Program Files\Wolfram Research\Mathematica\6.0\SystemFiles\Links\MathLink\DeveloperKit\Windows\CompilerAdditions\mldev32"; Under other platform, I do this by copy MathLink files (from /usr/local/Wolfram/Mathematica/6.0/SystemFiles/Links/MathLink/DeveloperKit/Linux/CompilerAdditions) into /usr/include and /usr/lib. Install the .plt file from DrScheme, File menu.

Why MrMathematica

An introduction of Lisp

My paper about MrMatheamtica: PDF slides(a Slideshow program)

WebMathematica on Scheme

A solver for the "Lights out" game that comes with PLT Scheme

Digital Philosophy


Call PLT Scheme from Mathematica, please check this file

Call Mathematica from PLT Scheme

After MrMathematica has been installed, start DrScheme and then type in the following expression in the Interaction window:

> (require (lib "" "mrmathematica"))

MrMathematica will open the Mathematica Kernel at the first call to MathEval. If it fails to find your Mathematica, a dialog will ask you where it is. (Tip: Under Windows, you can call (MathKernel #"-linkname" #"C:/Program Files/Wolfram Research/Mathematica/5.2/MathKernel.exe -mathlink") to avoid the dialog.)

Here are some examples about Integrate:

> (MathEval '(Integrate (/ 1 (+ (expt x 2) 1)) x))
(atan x)
> (MathEval '(Integrate (/ 1 (+ (expt x 2) -1)) x))
(+ (* 1/2 (log (+ -1 x))) (* -1/2 (log (+ 1 x))))
> (define f (MathEval '(Integrate (expt x 2) x)))
> f
(* 1/3 (expt x 3))
> (define s (eval `(lambda (x) ,f)))
> (- (s 1) (s 0))
> (MathEval '(Integrate (expt x 2) (list x 0 1)))

Both Mathematica and Scheme support lambda-calculus:

> (MathEval '((Function x (+ x 1)) 0))

MrMathematica can handle arbitrary long number:

> (MathEval '(Factorial 100))

You can define your Scheme function that use MathEval, thus using the power of Mathematica with nearly no efforts:

> (define (factorinteger n)
    (eval (MathEval `(FactorInteger ,n))))
> (factorinteger 111111111111111111)
((3 2) (7 1) (11 1) (13 1) (19 1) (37 1) (52579 1) (333667 1))

A more efficient version:

> (define-syntax factorinteger
    (syntax-rules ()
      ((_ n) (map cdr (cdr (MathEval `(FactorInteger ,n)))))))

You can call multiple Mathematica Kernel at the same time:

> (define ml1 (MathKernel))
> (define ml2 (MathKernel))
> (MathEval '(Set mm 1) ml1)
> (MathEval '(Set mm 2) ml2)
> (MathEval 'mm ml1)
> (MathEval 'mm ml2)

Plot is fully supported.

To call MathEval with different formats of Input and Outout, the following macros might be useful:

;input with out quote
(define-syntax me1
  (syntax-rules ()
    ((_ exp)
     (MathEval `exp))))

;input and output in string-form(StandardForm, InputForm and so on)
(define-syntax me2
  (syntax-rules ()
    ((_ string-form)
     (MathEval '(ToString (ToExpression string-form))))))

(define-syntax me3
  (syntax-rules ()
    ((_ string-form)
     (MathEval '(ToExpression string-form)))))

;introduce a Mathematica function into Scheme
(define-syntax MathFunction1
  (syntax-rules ()
    ((_ name)
     (define (name . args)
       (eval (MathEval `(name ,@args)))))))

(define-syntax MathFunction2
  (syntax-rules ()
    ((_ name)
     (define (name . args)
       (MathEval `(name ,@args))))))


Mathematica is a registered trademark of Wolfram Research, Inc.. Special thanks to WRI for letting me improve SchemeLink during my internship there.

Last update 2008/03/14