Maxima as a library

Discussion of Common Lisp
Post Reply
beren
Posts: 11
Joined: Thu Mar 05, 2009 4:08 pm

Maxima as a library

Post by beren » Sun Aug 23, 2009 5:36 pm

I am starting a new project where I will need some algebraic manipulation of equations, so I would like to use some maxima functions.

I know from inside maxima I can access the underlying lisp interpreter, but as the project is in lisp I would like to call maxima functions from lisp, and not to call lisp from maxima, which would be rather ugly.

I have already tried a solution I found on some forum, which consist in defining the functions I will need in maxima and using something like

Code: Select all

:lisp (ext:saveinitmem "session.mem") 
which saves the definitions. Then you can do

Code: Select all

sbcl --core sesion.mem
and you can use the functions you defined in maxima inside lisp. The problem is, the core you are using is not the "default" one, but the modified one of maxima, which leads my code to funny behaviours.

Is there a way I can use a maxima defined function more like a package rather than a core?
or is there a more standar way to use maxima functions inside lisp?

thanks!

dmitry_vk
Posts: 96
Joined: Sat Jun 28, 2008 8:01 am
Location: Russia, Kazan
Contact:

Re: Maxima as a library

Post by dmitry_vk » Sun Aug 23, 2009 9:03 pm

Maxima is written in lisp so you should just be able to load its code into a lisp image (load its code from source files). Of course, you should remove parts of code that deal with user input and program starting.

dmitry_vk
Posts: 96
Joined: Sat Jun 28, 2008 8:01 am
Location: Russia, Kazan
Contact:

Re: Maxima as a library

Post by dmitry_vk » Sun Aug 23, 2009 9:04 pm

beren wrote:

Code: Select all

:lisp (ext:saveinitmem "session.mem") 
which saves the definitions. Then you can do

Code: Select all

sbcl --core sesion.mem
ext:saveinitmem is a clisp function. You can not load clisp images with sbcl.

smithzv
Posts: 94
Joined: Wed Jul 23, 2008 11:36 am

Re: Maxima as a library

Post by smithzv » Sun Aug 23, 2009 10:42 pm

Maxima does some extensive setup so using it as a library is a bit problematic. I like your solution of saving a core which you can load up later. You will want to execute the function (MAXIMA::RUN) to get the Maxima REPL back (if you want it...).

My solution has been to set up a swank server inside Maxima. This is geared toward interactive work. Start up Maxima normally, then use this:

Code: Select all

:lisp (asdf:oos 'asdf:load-op :swank)
:lisp (swank:create-server :port 7777 :dont-close t)
Then, from Emacs, do a M-x slime-connect -- address is 127.0.0.1 -- port is 7777-- and you will get a Lisp REPL. Now, if you want to do some Maxima stuff, jump to your Maxima REPL and use it; if you want to do Lisp stuff, jump to the Slime REPL. This has the nice benefit of allowing you to use something like iMaxima to interact with Maxima interactively. But you can also use the full Slime setup to build a program that uses the Maxima facilities. And you can develop in CL and Maxima at the same time.

Some warnings about using Maxima for this purpose, however. First, I learned fairly quickly that it seems Maxima is not in any way thread safe. There will probably be some Maxima people who will disagree, I don't know. I do know that trying to call the same function in parallel yields weird results. Sometimes using seemingly unrelated Maxima stuff at the same time will cause problems. I would not trust calculations where more than one MEVAL or MFUNCALL is being used at a time. This is my experience from 6-10 months ago, things may be different now.

Second, Maxima is quite slow at number crunching. I was using Maxima to calculate matrices which I then used in an integration. I was integrating via Romberg. It would have been smarter to translate the matrix to a CL data type, then perform the calculations via GSL or something. What I mean is, use Maxima for things that can be slow but try to turn the Maxima output into CL where efficiency matters.

beren
Posts: 11
Joined: Thu Mar 05, 2009 4:08 pm

Re: Maxima as a library

Post by beren » Mon Aug 24, 2009 5:25 am

dmitry_vk wrote: ext:saveinitmem is a clisp function. You can not load clisp images with sbcl.
ups, you are right, I put a bad example. This should work instead:

Code: Select all

:lisp (sb-ext:save-lisp-and-die "session.mem") 
Can you give me a hint on how to load the code of maxima into lisp?


@smithzv
ty for your answer, but the solution you propose is still using lisp from within maxima and I would really like to do it the other way around :? .

smithzv
Posts: 94
Joined: Wed Jul 23, 2008 11:36 am

Re: Maxima as a library

Post by smithzv » Mon Aug 24, 2009 2:30 pm

beren wrote:ty for your answer, but the solution you propose is still using lisp from within maxima and I would really like to do it the other way around :? .
Fair enough. I don't know of a way to load Maxima into a Lisp image without going through its own bootstrapping. Not sure there is a way. If there is, then presumably that would require "altering" the Lisp image in the same way that Maxima does on startup. A while ago I noticed a maxima.asd file in the maxima source. As far as I can tell, it doesn't work to try and LOAD-OP this.

Mind if I ask what issues you are running into by using your Maxima image dump? I don't remember experiencing any weird problems. If you could post some examples of this odd behavior, that would be very interesting to me. The only thing I can think of would be the reader macro they install on '$', i.e. #$<maxima code>$, but you could get rid of that by restoring the default readtable.

Lastly, if you have not, I suggest you try the Maxima mailing list. I should warn that several people have asked for something along these lines before only to find that many of the active developers and users have little interest in supporting this.

Good luck

jjgarcia
Posts: 38
Joined: Mon Oct 13, 2008 2:48 pm

Re: Maxima as a library

Post by jjgarcia » Tue Aug 25, 2009 12:41 am

smithzv wrote:
beren wrote:ty for your answer, but the solution you propose is still using lisp from within maxima and I would really like to do it the other way around :? .
Fair enough. I don't know of a way to load Maxima into a Lisp image without going through its own bootstrapping. Not sure there is a way. If there is, then presumably that would require "altering" the Lisp image in the same way that Maxima does on startup. A while ago I noticed a maxima.asd file in the maxima source. As far as I can tell, it doesn't work to try and LOAD-OP this.
[/quote}

Maxima contains two system definitions: one for defsystem and one for ASDF that I cured some time ago but has since been broken by the developers. You simply have to take the files in maxima/src and use something like (load "../lisp-utils/defystem") (mk:operate-on-system "maxima" :load) Or, as I said, you fix the ASDF file and use it instead.

Maxima has a function called MEVAL that can be used to evaluate expressions written in lisp form, but then you need to learn this syntax -- I presume it results just from translating Infix to Prefix, but I am not entirely sure. It has been suggested in the Sage mailing list -- a project that uses Maxima -- that the only complication is a special syntax for polynomials.
smithzv wrote:Lastly, if you have not, I suggest you try the Maxima mailing list. I should warn that several people have asked for something along these lines before only to find that many of the active developers and users have little interest in supporting this.
I know that the Maxima mailing list is not a friendly place, beginning with the oldest contributor, Richard Fateman. However, there are nice people there, including one of the main developers, Robert Dodier. The idea of using Maxima as a library is not out of the scope of the project, for right now one the most important uses of Maxima is as symbolic caculus engine in the Sage project.

However, I agree that there are difficulties, an important one being that the code is not thread safe: it is coded as Emacs Lisp, using almost no lexically bound variables, just specials everywhere.
Last edited by jjgarcia on Tue Aug 25, 2009 2:39 pm, edited 1 time in total.

beren
Posts: 11
Joined: Thu Mar 05, 2009 4:08 pm

Re: Maxima as a library

Post by beren » Tue Aug 25, 2009 2:19 am

I have looked into the maxima's mailing list. In fact, if I remember well, the solution I post at the OP was given by Robert Dodier.

As the question was already in the list with no "satisfactory" answer, I thought in trying in a new place :)

I think the solution is given in http://d.hatena.ne.jp/niitsuma/20080328/1226706399
I will try this later, and post here the results, in case someone else is interested in this.

cheers!

smithzv
Posts: 94
Joined: Wed Jul 23, 2008 11:36 am

Re: Maxima as a library

Post by smithzv » Tue Aug 25, 2009 8:12 am

Thanks for the clarification Jaunjo.

Post Reply