The Vectorizer program for matlab tensor calculations

download (an executable; compiled on a utoronto cslab machine)

Problem description
It is often necessary to compute sums of products of high-dimensional matrices (tensors) in matlab.
Examples

The above expressions are easily implemented in matlab, as simple matrix multiplications. However, there are many simple cases that matlab cannot handle easily: The last expression is a useful example that describes the expressions in question. In general, we are talking about nested sums products of matrices along certain dimensions, although the matrices could also be added to one another. Matlab, unfortunately, does not support easy computation of such expressions.

The general recipe for computing such expressions is to recursively repmat every matrix, permute its dimensions, perform element-wise multiplication (or addition or subtraction). This is precisely what the Vectorizer does. It accepts expressions written in a rather easy to understand way, and outputs matlab code to compute the answer. It is written in Haskell and the source code is provided, compiled using ghc.

The above examples are translated to the Vectorizer language. They are similar, except that the ranges of each variables must be specified. This is because being a syntactic program, it does not know the dimensions of the matrices which are needed in order to reshape and repmat the matrices. If a similar program is ever implemented inside matlab then specifying the ranges of variables will not be necessary.
The examples below should be analyzed, because they reveal the operation of the program. It is not hard, since the program does the intuitively obvious thing (to me, at least).

Warning: never use * in the vectorizer; always use .*. The reason is that the program always assumes that all operations are elementwise, and treats them as "syntactic things"; it does not care what symbol you give it for the operation, be it +, -, *, .*, .+, or anything else. Because of that, if * is used instead of .*, the code will almost surely produce an error, but it may sometimes run incorrectly.

Explanation of the operation of the program
The program operates on a syntactic level, so the user must provide the program with the dimensions of the matrix. This is done by the declaration C[m,n,...](i,j,...); In this case, the variable i ranges from 1 to m, j ranges from 1 to n, etc. When we write sum(i=m, j=n,...|...), then i also ranges from 1 to m, and the variable j ranges from 1 to n. When a matrix indexing operation is written as A(i,j,...), it is assumed that its first dimension is of size m and second is of size n, depending on the range of the indecies. Every matrix is merely a symbol and the same letter A can be used in different manners, although this is misleading. In addition, the program does not understand references such as A(i,j,i,...). Writing this will probably result in an error, or worse, incorrect code.

Some simple optimizations are implemented as well, namely, that prevent from making too large repmats of the matrices in the inner-most sums. Unfortunately, I forgot the details.

It goes without saying that there is no guarantee that in some obscure case this program will fail to produce an answer that does what expected, although I never encountered such a case.


Important Notes


home