This post is about authoring questions about row reduced echelon form, matrix inverses, and determinants in the Numbas e-assessment system.

Getting extensions working

There is a Numbas extension with some RREF-related functions but it’s not available on the online editor. If you have the editor installed on your own computer you can try following the instructions for installing extensions but they didn’t work for me - there’s an issue with the filenames in the zip file you get from git, I think. All you need to do is create an extension in the editor and paste the linearalgebra.js file into that though.

Although the online editor doesn’t have the linearalgebra extension, it does have a lot of RREF questions e.g. this one which have had a lot of work put into them - lots of custom javascript. The smartest thing is probably to re-use these, but I wanted to write my own.

RREF

Using the linearalgebra extension you could simply generate a random matrix entry by entry and ask for its RREF. The trouble is that with extremely high probability it will have full rank, and you probably don’t always want that.

A solution is to generate some random linearly independent row vectors, pick a rank at random, then make a matrix whose rows are linear combinations of your row vectors.

Inverses

Setting Linear Algebra Problems by John D. Steele is very helpful whether you’re writing pen-and-paper or computer exercises.

Steele’s suggestion for matrices with easy-to-calculate (no big fractions) determinants and inverses amounts to using the Bruhat decomposition: every matrix in \(\mathsf{GL}_n(\mathbb{C})\) can be written in the form \(DBPB'\) where \(B\) and \(B'\) are lower unitriangular, \(P\) is a permutation matrix, and \(D\) is diagonal. If you choose \(B\) and \(B'\) to be integer matrices and \(D\) to have very small integers on the diagonal then the inverse of \(DBPB'\), which is \(B'^{-1}P^{-1}B^{-1}D^{-1}\), will involve only fractions with small denominators.

This means you need a JME function to create permutation matrices. The following list expression will do it:

matrix(map(map(if(p[jj]=ii, 1, 0), jj, 1..n), ii, 1..n))

(don’t use i!) where p is a permutation as implemented in the permutation extension.

Unfortunately Numbas and its linearalgebra extension don’t have a matrix inverse function. For permutation matrices you can just call the permutation matrix function above with the inverse permutation (which the permutation extension will calculate). You can probably do it with the rref functions if you know how to extract columns, but for 3x3 or 4x4 lower unitriangular matrices it’s easy enough to write down the formula by hand.

Determinants

The determinant function from the linearalgebra extension doesn’t work for matrices of size 4x4 or higher, but if you use the \(DBPB\) method the determinant is the product of the diagonal entries.