# Fun with algebra

Quite often, the key to finding an efficient solution to a given problem is to rewrite it in simpler terms. This requires a fair amount of experience and a good toolbox of rules one can apply to transform things.

For me, problems are often algebraic and I have to deal with products of (tensors,) matrices and vectors and with their determinants, traces, and derivatives thereof.

A very good collection of rules for these kinds of problems has always been the matrix cookbook by K. B. Petersen and M. S. Pedersen. It is available freely in PDF form. The official website seems to be down at the day of writing this post but a google search for matrix cookbook reveals several alternative websites where it can be downloaded.

Over the years I have used many of the rules in the book, but I have also found some that are not listed. I’ve tried to contact the authors and after one initial positive response never heard anything again. Therefore, I will start collecting rules I find here as well, hoping that others may find them useful.

Here is a rule I use very, very frequently. Let $\ma{A} \in \compl^{M \times N}$, $\ma{X} \in \compl^{N \times P}$, and $\ma{B} \in \compl^{P \times Q}$. Then
$${\rm vec}\left(\ma{A} \cdot \ma{X} \cdot \ma{B} \right) = \left(\ma{B}^{\rm T} \otimes \ma{A} \right)\cdot {\rm vec}(\ma{X}),$$
where $\otimes$ represents the Kronecker product and ${\rm vec}\{.\}$ is an operator that stacks all elements of a matrix into a vector, proceeding column by column. This rule is extremely handy for rewriting linear forms. The philosophy is that the left-hand side represents a vector where each element is a linear combination of the elements of $\ma{X}$. Therefore, it is a linear mapping from ${\rm vec}(\ma{X})$ to a new vector. As any linear mapping can be expressed by a matrix multiplication, there must exist such a matrix. We only need to find it. This is what the Kronecker product is good for, it gives us a handy way of expressing such a mapping directly in terms of $\ma{A}$ and $\ma{B}$.

It is often needed to rewrite linear forms like that, “pulling out” all parameters of interest, i.e., when solving least squares problems or finding expectations.

So why not think further? What about quadratic forms? Here are two rules I found for quadratic forms. Albeit not quite the same as for linear forms, both have proven very useful:

$${\rm trace}\left(\ma{X} \cdot \ma{R} \cdot \ma{X}^{\rm H}\right) = {\rm vec}\left(\ma{X}\right)^{\rm H} \cdot \left( \ma{R}^{\rm T} \otimes \ma{I} \right) \cdot {\rm vec}\left(\ma{X}\right)$$

$$\ma{a}^{\rm H}\cdot \ma{X} \cdot \ma{B} \cdot \ma{X}^{\rm H} \cdot \ma{c} = {\rm vec}(\ma{X})^{\rm H}\cdot \left(\ma{B}^{\rm T} \otimes \ma{c}\cdot \ma{a}^{\rm H}\right)\cdot{\rm vec}(\ma{X})$$

where $\ma{a}$ and $\ma{c}$ are column vectors and all other quantities are matrices of arbitrary dimensions as long as they match for the matrix products to be well-defined. The second rule is more general, it can be used to prove the first. To prove the second, use the rule from linear forms twice. Try it, it’s easy.

By the way, a more general version of the first, which is not a quadratic form anymore, would be

$${\rm trace}\left(\ma{X} \cdot \ma{R} \cdot \ma{Y}^{\rm H}\right) = {\rm vec}\left(\ma{Y}\right)^{\rm H} \cdot \left( \ma{R}^{\rm T} \otimes \ma{I} \right) \cdot {\rm vec}\left(\ma{X}\right)$$

Have fun and let me know in case you know related rules!

# Syntax highlighting

Along with advocating the pure beauty of math, parts of my blog may be about praising the pure awesomeness of MATLAB. I will say a lot more about why I have learned to love it so much later. What I will need to make these points is a decent way of performing syntax highlighting. Therefore, I gave the WP-syntax plugin a try which supports syntax highlighting for many languages, including MATLAB.

This is what it turns out to look like:

% test F = fft(eye(4));

Line numbers can be added too:

1 2 3 4 5 6 7 8 9 10 11 % eyecandy demo: OpenGL rendering of PEAKS function using phong shading [X,Y,Z] = peaks(99); figure(1); clf; surf(X,Y,Z); axis vis3d; colormap copper; shading interp; lighting phong; light; camlight;

however they do not render very nicely…

The result of the last script would look like that:

# Mathjax test

Sometimes, my blog posts my contain formulas. This could be simple inline formulas like $e^{j \pi} + 1 = 0$ or longer ones line $\int_{-\infty}^\infty e^{-x^2} {\rm d}x = \sqrt{\pi}$. It could also be displayed equations like

$$\sum_{n=0}^\infty q^n = \frac{1}{1-q}, \quad q \in \real, |q| < 1.$$

And, in particular they might involve vectors ($\ma{a}, \ma{b}$), matrices ($\ma{A}, \ma{B}$), and tensors ($\ten{A}, \ten{B}$), which may appear in longer equations too

$$\ten{I}_{R,d} \times_1 \ma{F}_1 \times_2 \ma{F}_2 \ldots \times_R \ma{F}_R = \ten{S}^{\rm[s]} \times_1 \ma{U}_1^{\rm[s]} \times_2 \ma{U}_2^{\rm[s]} \ldots \times_R \ma{U}_R^{\rm[s]}.$$

All these should render on any browser without much hassle. It seems I have found the perfect solution for this and it is called MathJax. I have no idea how these guys do it, but I am convinced there is a lot of magic involved. It is basically incredible. You add one line to the header of your HTML file, eh voilá, you’re free to use LaTeX commands right away. It needs no Java no Flash, not even graphics. Only JavaScript. And magic. Gotta be.

Anyways, here is my honest appreciation to the guys from MathJax for creating something which behaves like it should: It just works.

Should the impossible situation occur that you are having trouble to see the rendered equations, please tell me so instantly, e.g., by leaving a comment. Don’t forget to mention your browser.

# Blog

So, here it is. I give the world a gift it has not been waiting for: Adding yet another blog to the endless list of millions of blogs with totally infrequent updates and no coherent content. Why am I doing that?

Well, from time to time, there are small things, I find neat and I would like to document for myself. The question is, where to put them really? Yes, I have a website with an extremely rudimentary “CMS” that allows me to do “newsposts” but it is just too rudimentary. Not having updated there for a long time I finally realized that I need something new. Why not a blog?

As of now, I have no idea where this will be going. Math/Geek/MATLAB stuff? Probably. Personal things and thoughts? Maybe. Never being updated again? I hope not. At least I can say that I tried wordpress once.