The First Computer Algebra System: Maxima
Setup and Operator Algebra
Why
We learned to do proofs and derivations with pen and paper. There are two
problems with this method. Firstly, there will be errors and typos. Secondly,
efficient preservation of handwritten materials is hard. As a programmer, a tool
that helps to overcome such problems is a combination of computer algebra system
(CAS) and a proof assistant. There have been mature and feature rich CAS like
Mathematica
and Maple
. However, I have two problems with them. Firstly, I
don't like their syntax and the steep learning curve that comes with it. Less
personally, they are both proprietary and close-sourced. Contrary to
open-sourced software, this means I cannot hack on them and the support and
evolution of them ceases to exist should the company abandons them. As the
direct descendant of the ancestor of all modern CAS, Maxima
comes up as a
promising alternative. The goal of this post is to document how to download and
start using Maxima
for the operator algebra that is used frequently in
condensed matter physics and quantum mechanics related topics.
Maxima
Maxima
is a CAS written in Lisp
. A CAS is a software that can manipulate
symbolic and numerical expressions. Hence, it can perform the laborious manual
computation in mathematics and physics in place of human being. Maxima
was the
descendant of Macsyma
, the lengendary CAS developed in MIT during the 1960s.
Macsyma
inspired both Mathematica
and Maple
. Maxima
was maintained by
William Schelter from 1982 until 2001 when he passed away. If it weren't for his
effort to converting the project to open-source and maintaining it, we would
have lost a great tool in open-source CAS. Currently, Maxima
is maintained by
the community.
Installation
To install maxima
, we simply rely on our favourite package manager. For
example, on a Mac, you can do
brew install maxima
After installing maxima
, you will need a lisp
compiler in order to run it. You can do that by
brew install sbcl
Let's verify that we have successfully installed maxima
.
maxima
Maxima 5.47.0 https://maxima.sourceforge.io using Lisp SBCL 2.4.0 Distributed under the GNU Public License. See the file COPYING. Dedicated to the memory of William Schelter. The function bug_report() provides bug reporting information. (%i1)
Looks like we are able to start the maxima
repl successfully.
Configuration
I will be mainly interacting with maxima
either through org-babel
or the
terminal directly. Therefore, I will not demonstrate how to install a GUI
frontend for maxima
. The interested user should find this guide helpful.
In order to run maxima
in org-mode
, we have to install Maxima.el
package
in Emacs
. You may use the following code from the package repo in your config.
I used use-package!
because I am using doom emacs
.
(use-package! maxima
:init
(setq org-format-latex-options (plist-put org-format-latex-options :scale 2.0)
maxima-display-maxima-buffer nil)
(add-to-list 'auto-mode-alist
(cons "\\.mac\\'" 'maxima-mode))
(add-to-list 'interpreter-mode-alist
(cons "maxima" 'maxima-mode)))
For maxima
to be available to org-babel
, the following configuration is
necessary.
(org-babel-do-load-languages
'org-babel-load-languages
'((maxima . t)))
Now, we can use maxima
in org-babel
.
programmode: false;
tex(diff(exp(x)^2));
(linenum:0, $$2\,e^{2\,x}\,dx$$
External libraries
Maxima
is already a feature rich CAS by itself. It can do differentiation,
integration, solve ODE, and etc. It can be make more powerful by loading
libraries written by others for more features.
Search Path Expansion
A simple way of including other's code is to include the file containing the
code. Assuming the external library you would like to include is in file
something.mac
.
load("something.mac");
To include a larger library with multiple files, you could follow the instruction in Clifford's repo.
Firstly, clone the repo to your local machine.
cd ~/projects/
git clone git@github.com:dprodanov/clifford.git
Then, you would add the path of this repo to Maxima's search path.
file_search_demo: append (file_search_demo,["~/projects/clifford/$$$.{dem.wxm}"]);
file_search_maxima: append (file_search_maxima,["~/projects/clifford/$$$.{mac,wxm}"]);
setup_autoload ("clifford.mac", clifford);
load(clifford);
tex(clifford(dx,3));
(linenum:0, define: warning: redefining the built-in function clifford package name: clifford.mac author: Dimiter Prodanov version: v25 Recommended location: share/contrib last update: 9 May 2023 $$\left[ 1 , 1 , 1 \right] $$
Interested reader should try these examples given in the Clifford package repo.
Alternative Method
An alternative would be to directly add the files to the default place where
maxima
looks for code. This is at .maxima/
directory. Using the install.sh
script with qinf package, you can directly move the necessary files into the
desired directory.
Non-commutative Algebra
Now that we have things all setup, we could move on to the real business: having
maxima
carry out the tedious operator algebra for us.
Problem Statement
In condensed matter physics, canonical transformations like Holstein-Primakoff transformation are used to transform into a picture where quasi-particles emerge and fascilate the better understanding of certain phenomenon.
In the Holstein-Primakoff transformation, we define the bosonic creation and anhilation operator of magnons at site $i$ as \(\hat{a}^{\dagger}_{i}\) and \(\hat{a}_{i}\) respectively. The raising and lowering quantum spin operator are defined as \(\hat{S_{j}}^{+} = \sqrt{2S_{j} - \hat{n_{j}}} \hat{a_{j}}\) and \(\hat{S_{j}}^{-} = \hat{a_{j}}^{\dagger} \sqrt{2S_{j} - \hat{n_{j}}} \). $S_j$ is a number representing the maximum spin at site $j$. $\hat{n}_{j}$ is the number operator of magnon at site $j$. We would like to verify the commutation relation \([\hat{S_{j}}^{+},\hat{S_{j}}^{-}] = 2 \hat{S_{j}}^{z} \delta_{ij}\) where \(S_{j} - \hat{n_{j}}\)
Code
It does not appear to be easily doable in maxima
. I need to postpone this.
Outlooks
There is a blog written in 2015 that compares the result of some real world
usage of Maxima
vs Mathematica
. The conclusion is that Maxima
will return
either incorrect or no result at all for some problems.
Let's verify some of the problems still exist:
programmode: false;
tex(integrate(1/(x^2 - x^3)^(1/3),x,0,1));
$$\int_{0}^{1}{{{1}\over{\left(x^2-x^3\right)^{{{1}\over{3}}}}}\;dx}$$
It seems like at least this problem still remains. Maxima
is unable to carry
out this integration.