devyn barrie


Introduction to Maxima presentation

The following is an adapted version of the slide deck from a presentation I gave to the University of Ottawa Math Club session of November 29, 2024, entitled "Introduction to Computer Algebra and Maxima". Most of the presentation was live-coded, so be sure to check out the linked Maxima worksheets as you read this.

Preamble

This is a presentation about two things: computer algebra and a particular software for it, Maxima. I am a fourth year mathematics student, with a minor in physics, and computer algebra is something I've been interested in and used as a tool for several years now. I got interested in computer algebra when I was first learning mathematics as an adult. In Ontario, mathematics is only required up to grade 9 or 10, and I stopped there because it did not interest me at the time—I wanted to be a journalist! I had never been mathematically inclined and always struggled in my math courses up to then.

When the math bug bit me later on, I discovered it is harder to learn the topic as an autodidact. While there are many resources available online, trying to teach yourself without the support of a school environment is simply more difficult. Luckily, I discovered Wolfram Alpha, a sort of "computer algebra light" website that offered step-by-step solutions for a lot of the basic algebra I was trying to learn. That really sold me on the value that these computer algebra systems (CASs) can bring to education.

In math undergrad, the problems one tackles are considerably more advanced than what I faced when starting out. There may be a need to move a step up, past Wolfram Alpha—to the desktop CAS. Maxima is an example of this, and that's what we will be taking a look at next.

What is Maxima?

  • One of the oldest computer algebra systems (CAS) in active development.
  • Written in Common Lisp and provides its own programming language in an ALGOL-like syntax with Lisp-like semantics.
  • Free open-source software under the GPL. Available on Windows, Mac, Linux, BSD and has an active community.

Why Use Maxima?

  • When would you use a CAS? Some situations:

    • Derive exact expressions or formulas.
    • You need to do calculations that would be impractical or error-prone to do on paper.
    • Spin up concrete examples for a more theoretical problem.
    • You enjoy math AND programming, if only there were a way to combine the two…
  • Why use Maxima?

    • It's free and has a long history.
    • Easy to pick up, with lots of literature available and an intuitive syntax, in my opinion.
    • Active community — daily messages in the mailing list.

A quick look at some basic operations one may do with Maxima:

Operation/Object Example
Variable x:5
Define function of n variables f(x_1,...,x_n) :​​= foo
…Of arbitrary variables f([u]) := foo
Vectors/lists [x,y,z]
Matrix matrix([rows])
Matrix multiplication and inner product A . v, w . v
Mathematical constants %pi
Get floating point value float(%pi)
End of line ;
End of line, suppress output $

For more introductory examples of Maxima, check out the Maxima Crash Course worksheet. (While one can use Maxima in the terminal, I generally recommend using wxMaxima, a free third-party notebook-style graphical interface.)

Compute a Path Integral

Maxima has lots of functions for common mathematical operations, including integration. While there are procedures for symbolic and numerical integrals, there is no function available for a path (line) integral. This is a common situation with all computer algebra systems—the software developers cannot possibly anticipate everything a user may want to accomplish. Often, one needs to implement things themselves.

In the worksheet, we follow this process to compute \(\int_\gamma 4x^3 y\ \textrm{d}S\), where \(\gamma\) is the parameterized line \(\gamma(t) = (1-3t)\hat{\textrm{i}} + (2-3t)\hat{\textrm{j}}\), \(t \in [0,1]\). The steps here are:

  1. Differentiate the path to obtain \(\gamma \prime (t)\) and use this to calculate \(\gamma \prime (t) \cdot \gamma \prime (t) = \lVert \gamma \prime (t) \rVert\). (This is denoted in the code, by abuse of notation, as \(\textrm{d}S\).)
  2. Use the map function and sublis to re-parameterize \(4x^3 y\) using the components of \(\gamma(t)\).
  3. Integrate the re-parameterized function multiplied by \(\textrm{d}S\). That is, compute \(\int_0^1 f(\gamma(t)) \lVert \gamma \prime (t) \rVert \textrm{d}t\).

In general, the above will work to integrate a scalar field along a path in Maxima. To integrate a vector field \(\vec{F}\), apply the steps to the scalar field given by \(\vec{F}(\gamma(t)) \cdot \frac{\gamma\prime (t)}{\lVert \gamma \prime (t) \rVert}\).

Lorenz Attractor

I have previously written about this particular example in this post.

Curvilinear Coordinates

As a further demonstration, we use Maxima to obtain the form of the gradient in the polar and spherical coordinate systems. The relevant worksheet is posted here.

  • Many problems with certain symmetries can be simplified by a change to orthogonal curvilinear coordinates.
  • The unit vectors \(\{\hat{e_i}\}\) of the new system are not constant, so derivatives look slightly different.
  • In general, scale factors \(h_i\) of the coordinate change are involved in differential identities. For example, the gradient of \(\Phi(u_1,u_2,u_3)\) is,
\begin{align*} \nabla \Phi &= \frac{1}{h_1} \frac{\partial \Phi}{\partial u_1} \hat{e_1} + \frac{1}{h_2}\frac{\partial \Phi}{\partial u_2} \hat{e_2} + \frac{1}{h_3}\frac{\partial \Phi}{\partial u_3} \hat{e_3},\\ &\textrm{with } \textrm{d}s^2 = h_1^2 \textrm{d}{u_1}^2 + h_2^2 \textrm{d} {u_2}^2 + h_3^2 \textrm{d}{u_3}^2. \end{align*}

In Cartesian coordinates, these scale factors are all equal to 1.

If a position vector \(\vec{r}\) is expressed in an orthogonal coordinate system, say \(\vec{r}(t,u,v) = r_1(t,u,v) \hat{e}_1 + r_2 (t,u,v) \hat{e}_2 + r_3 (t,u,v) \hat{e}_3\), then one may obtain the scale factors by reading off the coefficients of \(\lVert \textrm{D}\vec{r} \rVert^2 = \textrm{d}s^2\). That is, the norm squared of the total differential.

Alternatively, the form of the gradient can be read off the Jacobian matrix of the transformation from Cartesian coordinates to the orthogonal curvilinear coordinate system. For example, the vector equation to convert polar (\(r,\phi\)) coordinates to Cartesian are

\[\begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} r \cos \phi \\ r \sin \phi \end{bmatrix} = r \hat{e}_r.\]

In the plane polar coordinate system, the unit vectors are \(\hat{e}_r\) as above, and \(\hat{e}_\phi = \frac{\textrm{d} \hat{e}_r}{\textrm{d} \phi}\).

The Jacobian matrix is,

\[J = \begin{bmatrix}\cos{\left( \phi \right) } & \mathop{-}\left( r \sin{\left( \phi \right) }\right) \\ \sin{\left( \phi \right) } & r \cos{\left( \phi \right) }\end{bmatrix}\]

The inverse of this matrix is the Jacobian for the transformation \((x,y) \to (r,\phi)\),

\[J^{-1} = \begin{bmatrix}\cos{\left( \phi \right) } & \sin{\left( \phi \right) }\\ \mathop{-}\left( \frac{\sin{\left( \phi \right) }}{r}\right) & \frac{\cos{\left( \phi \right) }}{r}\end{bmatrix}.\]

Now, in general, if \(\vec{F}\) is a vector field with component functions \((f_1,f_2,...,f_n)\), the i-th row of the Jacobian matrix of \(\vec{F}\) is \(\nabla^T f_i\) (i.e., the transposed gradient of the i-th component function). One can identify the form of the gradients in the alternate coordinate system this way. In the case of polar coordinates, from \(J^{-1}\) we have in terms of the components of \(\vec{R} = r \hat{e}_r\),

\[\nabla^T R_1 = \begin{bmatrix} \cos(\phi) & \sin(\phi)\end{bmatrix}\]

\[\nabla^T R_2 = \begin{bmatrix} -\left( \frac{\sin{\left( \phi \right) }}{r}\right) & \frac{\cos{\left( \phi \right)}}{r}\end{bmatrix}\]

The first is simply \(\hat{e}_r\), while the second equation is \(\frac{1}{r} \hat{e}_\phi\). Hence, the gradient of \(\Phi(r,\phi)\) in plane polar coordinates is,

\[\nabla \Phi = \frac{\partial \Phi}{\partial r} \hat{e}_r + \frac{1}{r} \frac{\partial \Phi}{\partial \phi} \hat{e}_\phi.\]

More such exploration is done in the relevant worksheet from the presentation.

Numerical Analysis vs Computer Algebra

  • Numerical analysis software broadly focus on computing numerical approximations, generally using floating point numbers.

    • Approximation is often necessary; a lot of problems don't have an analytic solution.
  • Computer algebra emphasizes exact representation of mathematical objects and quantities.

    • The use cases for computer algebra are different and sometimes more theoretical.
    • You can often use a CAS to complement numerical analysis, for example to massage a problem into a form such that a numerical method can be applied effectively.
  • Whether you choose the numerical or symbolic approach for your problem, keep your B.S. detector nearby. One can generate a lot of nonsense with either options if they are not careful.

Maxima includes a number of numerical procedures that can often be useful. Some examples:

  • QUADPACK routines for numerical integration. The typical one to use is quad_qags(f,x,a,b), and there are other routines available for specific cases.
  • mnewton — implementation of Newton's method for systems of nonlinear equations.
  • rk — 4th order Runge-Kutta method (fixed step size) for a system of first-order ODEs.
  • rkf45 — Runge-Kutta-Fehlberg method, introduces adaptive step size.
(%i1) load(rkf45)$
(%i2) sol:rkf45(-3*x*y^2+1/(x^3+1),y,0,[x,0,5],report=true)$
  • Eulix is an ODE solver for stiff equations.
  • drawdf to plot direction fields.

For rkf45, there is no documentation included in the Maxima "help" commands, but a paper with examples and discussion is included in the Maxima share directory (see your share/contrib directory in the Maxima path.)

Some Closing Remarks

Maxima is a wonderful tool which I believe can be invaluable to any user of mathematics, at any level of complexity. But do not forget it is just a tool, which must be used with caution. Some of these examples have hopefully illustrated that Maxima is great at running through complicated calculations with ease, but the interpretation of the results are up to the person using Maxima.

As a final note, there are some further free resources that I recommend for learning Maxima:

Tags: math, maxima, programming.