14
$\begingroup$

I'm writing a program that multiples a matrix by its transpose, and was trying to find efficiency hacks I could exploit considering that the two matrices being multiplied are related. Any ideas?

$\endgroup$
  • 8
    $\begingroup$ The product is symmetric, so you only need $1+2+3+\cdots+n=\frac{n(n+1)}{2}$ entries to fill out the rest. I don't know whether you're looking for something more. $\endgroup$ – Jonas Meyer Mar 22 '11 at 0:04
  • $\begingroup$ @Jonas: +1. Your comment can go as an answer. $\endgroup$ – user17762 Mar 22 '11 at 0:22
16
$\begingroup$

After Sivaram's suggestion I'm turning my comment into an answer.

In general, $(AB)^\textrm{T}=B^\textrm{T}A^\textrm{T}$, so for the product of a matrix with its transpose, you have $AA^\textrm{T}=(AA^\textrm{T})^\textrm{T}$ is symmetric. You can also see this directly by observing that if the $ij$ entry of $A$ is $a_{ij}$, then the $ij$ entry of $AA^\textrm{T}$ is $\sum_{k=1}^n a_{ik}a_{jk}$, which is the same if you switch $i$ and $j$. Therefore if you compute the entries with $i\leq j$, you get the entries with $i>j$ for free. There are $1+2+3+\cdots+n=\frac{n(n+1)}{2}$ such entries, instead of the $n^2$ you generally need.

(I was thinking of $n$-by-$n$ matrices, but if your input is $m$-by-$n$, this will give you $\frac{m(m+1)}{2}$ entry computations, each of which is a sum of $n$ products.)

$\endgroup$
  • 4
    $\begingroup$ Good answer. Here's a related question, for which I don't know the answer: Can you compute this product in-place, that is, overwriting $A$? (Note that even computing the transpose in-place is not trivial if $A$ is not square: see en.wikipedia.org/wiki/In-place_matrix_transposition $\endgroup$ – lhf Mar 22 '11 at 0:38

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.