MrMathematica: Calling Mathematica from Scheme
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 the WSTP dynamic library by copying it into the PLT libraries (see get-lib-search-dirs), or into a systemwide location such as "C:\Windows\SysWOW64", "/lib", "/Library/Frameworks" or "~/Library/Frameworks". On Windows it is "wstp32i4.dll" or "wstp64i4.dll", depending on whether you are on a 32-bit or 64-bit platform. On Unix/Linux, it is "libWSTP32i4.so" or "libWSTP64i4.so". On Mac OS X, it is "wstp.framework".
Make sure that MathKernel or math will launch Mathematica kernel. On Unix/Linux the Mathematica installer should have done that. On Windows setting PATH to include the directory where Mathematica executable files locate should be enough. On Mac OS, MrMathematica looks for "/Applications/Mathematica.app/Contents/MacOS/MathKernel", which should be the correct place.
Install mrmathematica.plt from File menu in DrScheme.
2 Use MrMathematica
(require mrmathematica) | package: base |
procedure
(MathKernel arg ...) → MathLink?
arg : string?
procedure
(current-mathlink) → (or false? MathLink?)
(current-mathlink v) → void? v : (or false? MathLink?)
procedure
(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))) 1/3
> (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)) 1/3
> (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.
procedure
(Mexp->image Mexp [ml]) → (is-a?/c image-snip%)
Mexp : Mexp ml : MathLink? = (current-mathlink)
> (Mexp->image '(Plot (sin x) #(x 0 (* 2 Pi))))
struct
(struct exn:fail:mathlink exn:fail () #:extra-constructor-name make-exn:fail:mathlink #:transparent)
procedure
ml : MathLink? = (current-mathlink)
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 "translation.ss". 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 "translation.ss" as examples.