Matrix algebra in IDL

The two multiplication operators # and ## operate on 2D IDL arrays interpreted as mathematical matrices and vectors. (I will try to keep this distinction between the IDL variable type and the mathematical entities.) The two operators use different conventions for the index order. The # operator uses (row, column) order, while the ## operator uses (column, row) order. Simple, right? Well, this confused me a bit and apparently also others (see thread in comp.lang.idl-pwvave).

Part of my own confusion, I realized, was the way the matrices were printed. The IDL print command can print arrays but it is apparently not designed to print matrices! (I'll make a distinction between the IDL array variable type and the concept of matrices and vectors in linear algebra.) For one thing, matrices need to be printed differently depending on whether they are meant to be interpreted by the # operator or the ## operator. Not taking the operator into account will only result in explanations such as this, where, even when presented as a matrix with brackets and all, an array was simply printed in the order the IDL print command would. For the matrix operations to make sense, the matrices need to be printed in the way they are to be interpreted.

PrintMatrix, M

The PrintMatrix procedure (printmatrix.pro) prints a matrix M and takes the operator as an optional keyword (with ## as the default) and prints the matrix accordingly.

Matrices

When printing matrices with PrintMatrix, the operations and conventions make sense. In the frames below I show a script and its journal output, where I define two arrays, A and B, and print them as well as their matrix products. The matrix dimensions match the way they are supposed to. For both operators!

Note that in the two examples the IDL arrays are the same but their interpretation as matrices differ.

Vectors

Because IDL removes trailing dimensions if they are of length 1, vectors add some more uncertainty. But when printed correctly, also they make sense.

Conventions used in existing routines

So which conventions are used by some relevant IDL codes?

Singular Value Decomposition

For Singular Value Decomposition (SVD) I believe the code of choice is LA_SVD. This routine interprets the arrays as matrices using the ## convention.

QR factorization

The QRfac routine add a bit of extra confusion because it uses the # convention for input and the ## convention for output! Maybe the best way of thinking about it is that it should be called with the transpose of the matrix we want to factorize.


Time-stamp: <2012-04-17 11:41:28 mats>