Welcome to the world of Mathematica + Macintosh Common Lisp!
***** Overview *****
By way of powerful programming features of Macintosh Common Lisp, my
mma-link.lisp can provide those who program Mathematica some very helpful
interfaces to that endeavor. Just loading mma-link.lisp will enable you to
access full range of Mathematica's documentation interfaces at your disposal.
And issuing the command (init-mma-link), which can be put in the init.lisp
file for convenience, will automatically launch ToolServer, Mathematica, and
Rotater in that order; thereby allowing you to access full range of all of
these powerful programs, all from within the MCL environment.
***** File Preparation *****
In order to take full advantage of mma-link.lisp's features, you need the
following programs and files in addition to your regular Mathematica and MCL
distributions:
fred-word-completion.lisp (comes with your MCL CD)
toolbox-ref-lookup.lisp (also comes with your CD)
fredenv.lisp (for MCL 3.0 users, can be found in the Lib folder on CD)
mark-menu.lisp (optional, on your CD)
ToolServer (optional, Apple's application on your MCL CD)
SaveToRotaterFormat.m (see below)
Rotater (freeware 3D application, also see below)
I suggest that you compile all *.lisp files for a faster loading, including
mma-link.lisp and appleevent-toolkit.lisp if it's not already compiled.
The last two items, SaveToRotaterFormat.m and Rotater, can be downloaded
from Xah Lee's astonishing Mathematica-related homepage:
http://www.best.com/~xah/PageTwo_dir/MathPrograms_dir/mathPrograms.html
and follow the pointers there.
***** For Mathematica programmers *****
If Mathematica programming is your main daily business, you need only
memorize few simple commands. They are so simple that I will simply copy
here what I wrote in the mma-link.lisp source file:
; Usage Note: Control-Enter on an expression in Listener or Fred to make Mma
; calculations. Return on Mma output to get Out[#]. Shift-Return
; on Mma output to get InputForm of Out[#]. Control-Shift-Return
; on Mma output to get numbers translated into Lisp expressions.
; Control-x Control-Shift-D to get Mma documentations. Moreover,
; hit Escape to toggle candidates of word completions in-line,
; and hit Tab for indentations of multi-lined Mma expressions.
; Finally, use freeware Rotater to display 3D freestyle rotation
; of an image obtained in Mma (Control-x Control-3 for options).
; Option-Enter on an expression in Listener to execute ToolServer
; commands.
For example, after loading mma-link.lisp and running Mathematica, typing in
"Series[Sin[x],{x,0,5}]" (without quotes) at MCL prompt and then hitting
Control-Enter produce the following output (just like Mathematica notebooks):
-----------------MCL Listener -----------------
Welcome to Macintosh Common Lisp Version 2.0.1!
Mma[1]? Series[Sin[x],{x,0,5}]
3 5
x x 6
x - -- + --- + O[x]
6 120
?
-----------------------------------------------
Now click anywhere in that Mathematica output. Let's say, right after O[x].
Then hit Return. Yes just hit Return on that Mma output. You will get
---------------
? Out[1]
---------------
at the next MCL prompt. Move your mouse right after "]" of this input
expression, and double click. You see that all of "Out[1]" is selected.
Delete this, and now once again click anywhere on the Mma output, and
this time hit Shift-Return (hold the Shift key and hit Return). You get
-----------------------------------------------------
? SeriesData[x, 0, {1, 0, -1/6, 0, 1/120}, 1, 6, 1]
-----------------------------------------------------
which is the InputForm of the expression you just obtained. I hope you'll
find these features convenient to use. Let's delete the last input
(SeriesData[...]), and then type in "{2/3, 3.5, 8/7+5/2 I}" instead. Hit
Control-Enter to get
-----------------MCL Listener -----------------
Mma[2]? {2/3, 3.5, 8/7+5/2 I}
2 8 5 I
{-, 3.5, - + ---}
3 7 2
?
-----------------------------------------------
You can repeat the procedures I just described to get similar effects, but
with the numerical outputs, you can do one more trick. Click on this Mma
output, and hit Control-Shift-Return. You get
--------------------------
? (2/3 3.5 #c(8/7 5/2))
--------------------------
which is the Lisp counterpart of Mma output just obtained. Surprise? Well,
not really :-) Now go back to Mma[1] input and click on Series. You can
just click anywhere on this word, or even highlight the entire word. Then
hit Control-x Control-Shift-D. (For beginners, just hold down both Control
and Shift keys and hit "x" and "d" in succession.) In a short moment, you'll
have the documentation of "Series" in a window by itself. This is just one
of three main documentation features of mma-link.lisp. You may have noticed
that when you type in a word fragment like "Ser", you will see in the lower
right-hand corner of the Listener window something like "SeriesTerm". This
is the word completion feature. Hitting ESC (escape) key brings completion
candidates up at the insertion point. This is convenient for long commands,
the likes of many Mathematica package commands. More than 2,600 Mma terms
are already remembered by MCL, and you can add your own just as easily. The
function templates, if they exist, will show up in the lower mini-window of
the Listener whenever you hit the "[" key. Try it with "Series[". More
than 1,400 function templates are in store.
Please remember to load SaveToRotaterFormat.m prior to issuing the first
3D graphics command. You can load up the file in init.m for convenience.
To really make you enlightened, let's close the examples with Lorenz
attractor. From Mathematica Book 2nd Ed. pp.702--703, I have
NDSolve[{x'[t]==-3(x[t]-y[t]),
y'[t]==-x[t]z[t]+26.5x[t]-y[t],
z'[t]==x[t]y[t]-z[t],
x[0]==z[0]==0,y[0]==1},
{x,y,z},{t,0,20},MaxSteps->3000]
Just click on the last "]" which is the end of the NDSolve expression. Then
hit Control-Enter right here. In a few moment, you'll see answers in the
Listener window like this:
-------------------------- MCL Listener ----------------------------
Mma[3]? mma-link doc {BigDisk:Desktop Folder:}, lines 119--123
{{x -> InterpolatingFunction[{0., 20.}, <>],
y -> InterpolatingFunction[{0., 20.}, <>],
z -> InterpolatingFunction[{0., 20.}, <>]}}
-----------
Then do the same thing with the following Mma expression:
ParametricPlot3D[Evaluate[{x[t],y[t],z[t]}/.%],{t,0,20},PlotPoints->1000]
(Just point your mouse to the end and click, and Control-Enter.) After a
while, you'll see a 3D rendition of Lorenz attractor in the Rotater window.
***** For MCL programmers *****
If you intend to use Lisp and Mathematica simultaneously (or side by side,
or whatever fits the description), please see the following example and read
the notes following that.
(defun foo (k &optional (init-value 1))
(when (< k 1)
[Block[{ y, x = ~k /2 (1+Cos[y])},
x/.FindMinimum[ -x^2+x^3, {y,~init-value}] [[2]]]]n))
(mapcar #'foo '(.1 .2 .3 .4 .5 .6 .7 .8 .9 1))
=> (0.1 0.2 0.3 0.4 0.5 0.6 0.666666685879612 0.6666666664785192
0.6666666666666623 NIL)
Notice the space after ~k. This isn't for pretty printing, but instead
necessary. That's because Lisp compiles anything after ~ within the
bracketed expressions, and as such, "k" must be delimited by a terminating
character such as space. The second such case, ~init-value, doesn't need
an extra space because the closing brace "}" acted as a terminating
character. As you noticed, "}" is normally a constituent. Well, because
of the necessity to accomodate Mma expressions, I've made brackets and braces
terminating macro characters. Please bear this in mind. In the outermost
brackets, you can also notice the "n" after the closing bracket "]". This
signifies the compiler that numerical values will be returned. Other
options include plain [] and []o, a little alphabetic "o" for output. The
results from both [] and []o pairs are strings. Although all bracket pairs
work fine for their intended purposes, you may encounter strange returns
especially when using the numerical brackets []n. My original purpose for
bracketed pairs was for numerical IO, and []n does some extra activities
both before and after transferring the control to Mma. For example, a Lisp
list '(1 2 3), when used within [] or []o, results in a string "6". But
[~'(1 2 3)]n can return '(1 2 3). In short, use [] and []o pairs for passing
Lisp strings (such as from user input), and use []n for numerical IO,
including lists. Please let me know of problems you might encounter and
possible suggestions. (Presently, I don't know how to pass nil to Mma.
At this point, nil is passed as Mathematica's Null.)
Daitaro Hagihara