~jmbr/maxima-interface

Simple interface between Common Lisp and Maxima

8ca5af6 Alias TEX for LATEX.

2 months ago

eb6bf75 Add MINF to represent minus infinity.

2 months ago

#Simple interface between Common Lisp and Maxima

#Table of Contents

  1. Introduction
  2. Installation
  3. LaTeX rendering
  4. High level interface
  5. Low level interface
  6. Contact and support

#Introduction

Maxima is a featureful and versatile symbolic computation package. While it runs on most (possibly all) Common Lisp (CL) implementations, using it as an external CL library is not entirely straight-forward despite the fact that the functionality exists. The purpose of this package is to provide a simple interface between Common Lisp and Maxima and facilitate its use.

#Installation

The simplest way to install this package is to compile and install Maxima from source and add the directory /path/to/maxima/src to the ASDF registry. Next, add maxima-interface's directory to the ASDF registry (see ASDF documentation for details).

#LaTeX rendering

The expressions returned by Maxima can be rendered in LaTeX using either Emacs, Jupyter notebooks, or printing the LaTeX strings to a stream. The precise output is governed by the special variable *latex-output* which can be :emacs, :jupyter, or :console.

The function that accomplishes this is named latex. The way a symbol is represented in LaTeX can be controlled by the maxima::texword property of the symbol's property list (see examples below).

#Emacs

LaTeX can be rendered in a resizable fashion within Emacs using tex2svg in order to convert LaTeX strings to SVG files. This requires the custom patch to SLIME included in the file slime.patch as well as the tex2svg utility from MathJax.

img

#Jupyter

Using common-lisp-jupyter it is easy to interface with a Jupyter notebook. Just run the Common Lisp kernel and start using maxima-interface.

img

#Console

This is the default setting. It outputs the LaTeX string corresponding to the given expression.

#High level interface

The aim of this interface is to translate and evaluate expressions back and forth from Common Lisp to Maxima's internal representation as seamlessly as possible.

#diff

Takes the (partial) derivative of an s-expression with regard to a variable, possibly more than once.

#Examples
MAXIMA-INTERFACE-USER> (diff '(sin x) 'x)
(COS X)
MAXIMA-INTERFACE-USER> (diff '(* (expt x 2) (expt y 3)) 'x 2)
(* 2 (EXPT Y 3))

#integrate

Evaluates the definite or indefinite integral of an s-expression with respect to a symbol.

#Examples
MAXIMA-INTERFACE-USER> (integrate '(* (exp x) (sin (exp x))) 'x)
(* -1 (COS (EXPT (EXP 1) X)))
MAXIMA-INTERFACE-USER> (integrate '(/ x) 'x 1 '(exp 1))
1

#expand

Expands products.

#Examples
MAXIMA-INTERFACE-USER> (expand '(expt (+ x y) 2))
(+ (EXPT X 2) (* 2 X Y) (EXPT Y 2))

#simplify

Applies trigonometric and rational simplifications.

#Examples
MAXIMA-INTERFACE-USER> (simplify '(/ (1- (1+ x)) 1))
X
MAXIMA-INTERFACE-USER> (simplify '(+ (expt (cos x) 2) (expt (sin x) 2)))
1

#limit

Takes the limit of an s-expression with regard to a symbol.

#Examples
MAXIMA-INTERFACE-USER> (limit '(/ (+ (expt x 3) x) (* 2 (expt x 5))) 'x 'inf)
0
MAXIMA-INTERFACE-USER> (limit '(/ (sin x) x) 'x 0 'plus)
1

#factor

Factorize an expression.

#Examples
MAXIMA-INTERFACE-USER> (factor '(+ (expt x 2) (* 2 x y) (expt y 2)))
(EXPT (+ X Y) 2)
MAXIMA-INTERFACE-USER> (factor '(- (expt x 2) (expt y 2)))
(* -1 (+ (* -1 X) Y) (+ X Y))

#Low level interface

#maxima-init

This function is called when the module is loaded. It instructs Maxima to set the path names for the packages that will be loaded on-demand when carrying out different types of operations (e.g., simplification, eigendecompositions, etc.).

#maxima-run

Evaluates a Maxima expression passed as a string and prints the result.

#Examples
MAXIMA-INTERFACE-USER> (maxima-run "assume(sigma > 0)$" :display2d t)
[sigma > 0]
MAXIMA-INTERFACE-USER> (maxima-run "integrate(exp(-x^2/(2*sigma^2)), x, -inf, inf);" :display2d t)
sqrt(2) sqrt(%pi) sigma
MAXIMA-INTERFACE-USER> (maxima-run "exp(-x^2/(2*sigma^2));" :display2d nil :return-expression t)
%e^-(x^2/(2*sigma^2))
((MAXIMA::MEXPT MAXIMA::SIMP) MAXIMA::$%E
 ((MAXIMA::MTIMES MAXIMA::SIMP) ((MAXIMA::RAT MAXIMA::SIMP) -1 2)
  ((MAXIMA::MEXPT MAXIMA::SIMP) MAXIMA::$SIGMA -2)
  ((MAXIMA::MEXPT MAXIMA::SIMP) MAXIMA::$X 2)))
MAXIMA-INTERFACE-USER> (maxima-run "trigsimp(cos(x)^2 + sin(x)^2);")
1

#maxima-read

Takes a Maxima expression (represented as a string) as input and returns its internal representation in (Maxima) Lisp.

#Example
MAXIMA-INTERFACE-USER> (maxima-read "x^2$")
((MAXIMA::MEXPT) MAXIMA::$X 2)

#maxima-eval

Evaluates the internal Lisp representation of a Maxima expression and returns the internal Lisp representation of its result.

#Example
MAXIMA-INTERFACE-USER> (maxima-eval '((maxima::$expand)
    ((maxima::mexpt) ((maxima::mplus) maxima::$x maxima::$y) 2)))
((MAXIMA::MPLUS MAXIMA::SIMP) ((MAXIMA::MEXPT MAXIMA::SIMP) MAXIMA::$X 2)
 ((MAXIMA::MTIMES MAXIMA::SIMP) 2 MAXIMA::$X MAXIMA::$Y)
 ((MAXIMA::MEXPT MAXIMA::SIMP) MAXIMA::$Y 2))

#maxima-print

Prints the internal Lisp representation of a Maxima expression in human-readable form to output-stream (which is *standard-output* by default).

The keyword argument display2d is a boolean indicating whether the representation should be done in 2D or not. The keyword argument return-expression is another boolean that determines whether the original expression should be returned by maxima-print.

#Example
MAXIMA-INTERFACE-USER> (maxima-print '((maxima::%integrate maxima::simp)
                                       ((maxima::mexpt) maxima::$%e
                                        ((maxima::mtimes) -1 ((maxima::mexpt) maxima::$x 2)))
                                       maxima::$x 0 maxima::$inf)
                                     :display2d t)
 inf
/             2
[      (- 1) x
I    %e         dx
]
/
 0

#Tips

The document Macsyma’s General Simplifier: Philosophy and Operation by R. Fateman is a useful guide to better understand the inner workings of Maxima.

It is sometimes useful to execute :lisp (trace meval) inside a regular Maxima session (i.e., the REPL you get when invoking maxima from the command line) to see how commands are processed.

#Contact and support

Subscribe to the maxima-interface-devel mailing list for development discussion and to send patches related to the project. For help sending patches to this list, please consult git-send-email.io.