7

I want to make a small "library" to be used by my future maxima scripts, but I am not quite sure on how to proceed (I use wxMaxima). Maxima's documentation covers the save(), load() and loadFile() functions, yet does not provide examples. Therefore, I am not sure whether I am using the proper/best way or not. My current solution, which is based on this post, stores my library in the *.lisp format.

As a simple example, let's say that my library defines the cosSin(x) function. I open a new session and define this function as

(%i0) cosSin(x) := cos(x) * sin(x);

I then save it to a lisp file located in the /tmp/ directory.

(%i1) save("/tmp/lib.lisp");

I then open a new instance of maxima and load the library

(%i0) loadfile("/tmp/lib.lisp");

The cosSin(x) is now defined and can be called

(%i1) cosSin(%pi/4)

(%o1) 1/2

However, I noticed that a substantial number of the libraries shipped with maxima are of *.mac format: the /usr/share/maxima/5.37.2/share/ directory contains 428 *.mac files and 516 *.lisp files. Is it a better format? How would I generate such files?

More generally, what are the different ways a library can be saved and loaded? What is the recommended approach?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Gael Lorieul
  • 3,006
  • 4
  • 25
  • 50

2 Answers2

5

Usually people put the functions they need in a file name something.mac and then load("something.mac"); loads the functions into Maxima.

A file can contain any number of functions. A file can load other files, so if you have somethingA.mac and somethingB.mac, then you can have another file that just says load("somethingA.mac"); load("somethingB.mac");.

One can also create Lisp files and load them too, but it is not required to write functions in Lisp.

Unless you are specifically interested in writing Lisp functions, my advice is to write your functions in the Maxima language and put them in a file, using an ordinary text editor. Also, I recommend that you don't use save to save the functions to a file as Lisp code; just type the functions into a file, as Maxima code, with a plain text editor.

Take a look at the files in share to get a feeling for how other people have gone about it. I am looking right now at share/contrib/ggf.mac and I see it has a lengthy comment header describing its purpose -- such comments are always a good idea.

Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
  • Thanks for your answer : I was able to create and load a \*.mac file following your instructions :) . I still have a question though : is there a way to convert a wxMaxima file which is saved as \*.wxmx (in previous versions of wxMaxima it was saved as \*.wxm) into a \*.mac file ? I tried to simply rename the file from lib.wxmx to lib.mac, and that did not work… – Gael Lorieul Feb 04 '17 at 19:07
  • A wxmx file is actually a zip file containing the Maxima code plus any images, and maybe some other stuff. So that's why just renaming it doesn't work. Probably the easiest way to extract the Maxima code is to open the wxmx with wxMaxima and then choose "Save as ..." on the File menu and set the output type to "Maxima batch file (.mac)". I worked on a tool to extract the Maxima code without using wxMaxima, but it turns out that is laborious. If you are interested, the code for that project is available on Github, see: https://github.com/robert-dodier/maxima-read-wxmx – Robert Dodier Feb 05 '17 at 06:49
  • 1
    Indeed, I was able to save a \*.mac file from wxMaxima following your instructions by using the "file > export" button in the menu. That is probably the best option : thanks ! – Gael Lorieul Feb 06 '17 at 07:48
  • @RobertDodier can `load` calls be a part of a .mac file, amongst other things? I'm building up a library of useful functions, but have been saving them as separate .mac files, for the time being, until I figure out what should be a part of the core library. I had hoped to include all necessary / useful `load` calls within the .mac file of a given function (_i.e._ first 2-3 lines are `load` commands, followed by the new function's definition); however, my initial attempt did not seem to work. – Rax Adaam Nov 24 '20 at 01:22
  • 1
    @RaxAdaam yes, a .mac file can in turn call `load` to load other files. However, note that Maxima isn't smart enough to look for files in the same place where it got the first one; e.g. if you say `load("some/path/to/foo.mac")` then any `load("bar.mac")` within `foo.mac` are not searched for in `some/path/to`; that's a not exactly a bug, although it does make it less convenient. If you have multiple files to load, you can say `push("some/path/to/###.mac", file_search_maxima)` and then `load("foo.mac")` and within that `load("bar.mac")` and so on. More info: `? file_search` – Robert Dodier Nov 24 '20 at 18:51
0

For principiants, like me,

  1. Menu Edit:configure:Startup commands
  2. Copy all the functions you have verified in the first box (this will write your wxmaxima-init.mac in the location indicated below)
  3. Restart Wxmaxima.

Now you can access the functions whitout any load() command

JOM
  • 119
  • 3