Matrix Operations and STrassens Algorithm
Matrix Operations and STrassens Algorithm
Their product, AB, will be an n by n matrix and will therefore have n2 elements.
Each one of these elements is naturally expressed as the sum of n products, each of an
element of A with one of B. Thus we have
and the number of multiplications involved in producing the product in this way is n3.
In fact, a matrix product of this kind can be obtained using a smaller number of
operations, and we will describe how this can be done.
Also, with one easy instruction, whose entry is similar to applying the formula for
computing the determinant of a 2 by 2 matrix (along with some copying), we can
compute the determinant of any square matrix A of any size, and even obtain all the
cofactors of the elements A.
The first idea is that of “partitioning matrices” which in our context is:
Then their product is exactly the same as the product of the matrices
where we have
When we perform matrix multiplication here we take the dot products of the rows
of A with the columns of B. In doing so we sum over an intermediate variable.
The difference between the normal representation of the product and the
“partitioned representation as a 2 by 2 product of 2 by 2 matrices is the difference
between representing the indices to be summed over as 1 2 3 and 4, or as 11 12 21 and
22.
And of course the same thing is true for matrices whose dimensions are 2k by 2k.
We can represent the indices either as integers from 1 to 2k or as k-tuples, each element
of which is 1 or 2.
And with the latter representation we can interpret matrix multiplication as the 2
by 2 product of 2k-1 by 2k-1 matrices each one of which is a 2 by 2 product of 2k-2 by 2k-2
matrices, and so on.
The consequence we draw from this fact is: if we can multiply 2 by 2 matrices
using only 7 multiplications instead of the usual 8, we can parlay that into multiplying 4
by 4 matrices using 7 multiplications of 2 by 2 matrices each of which requires 7
multiplications of numbers, for a total of 49 multiplications.
You might note here that this kind of matrix partitioning can be done whenever
you have n by n matrices and n is the product of a and b. You can then write the product
of n by n matrices as the product of a by a matrices each one of which is a b by b matrix,
again by labeling the indices accordingly.
From now on then we will consider the problem of computing the 4 entries in the
product of two 2 by 2 matrices.
Explicitly, given 2 by 2 matrices with elements aij and bjk we want to compute the
four combinations
The second idea here is that we will get a better grasp of the problem if we
combine these four combinations into one entity. And we can do this by multiplying
each one by an indeterminate (or variable) and adding them up.
The result will be a polynomial, and our task will be both to compute this
polynomial from the a’s and b’s using 7 multiplications, and to find the coefficients of
our indeterminates in the polynomial.
And here is where we run into amazing luck. If we call our indeterminates zkj and
multiply the combinations above by z11, z21, z12 and z22 respectively, our polynomial can
be written as
which is
Σj,s,k=12 ajsbskzkj.
Our luck is the fact that this polynomial has quite a bit of symmetry.
If we perform the first of these two permutations the first and last terms stay
fixed, but the term a12b21z11 becomes a21b11z12 (j=1 s=2 k=1 becomes j=2 s=1 k=1) and so
on.
Thus there is a group of index changes that leave our polynomial unchanged, and
this group has the following six elements
The identity; (j,s,k); (j,k,s); reverse 1 and 2; reverse 1 and 2 and (j,s,k); reverse 1
and 2 and (j,k,s).
The 8 terms in our polynomial form two orbits under the action of this group.
One is the pair consisting of the first and last term above whose indices are all the
same. These are stabilized by the identity (j,s,k) and (j,k,s).
The other orbit consists of the remaining terms. Notice that each of these terms
has one factor with repeated indices and two factors whose indices are each 12 or 21.
(By the way, you should read (j,s,k) as: the new value of j is the old value of s,
the new value of s is the old value of k, and the new k value is the old j value.)
The next question is: how can we exploit this symmetry to find a way to write our
polynomial as the sum of 7 products.
First we must introduce a product that will give us the first and last entries.
We can find a single product which has all the same symmetries as our
polynomial, that will give us both of these: namely
If we write this product out, however, it consists of 8 terms, 2 of which are what
we want, namely our first and last terms, but there are 6 terms we do not want and of
course 6 terms in our polynomial that are not present in this product.
In fact the difference between our polynomial and this product is given by this
twelve term polynomial:
If you look at this difference, it consists of two complete orbits under our
symmetry group: both orbits have two indices the same and one different; one has
only diagonal matrix elements, the other has off diagonal ones.
We need to find an invariant that will add the positive terms here and get rid of
the negative ones.
How can we get an invariant here?
The obvious way is to find a single asymmetric product that will handle two of
the terms we need, apply each of the symmetries in our group to it, and add them up.
This will produce an invariant, consisting of six products, and has a chance
of being what we need, since it will certainly fix the 12 terms we want. If it does no
other harm it will do what we need here.
Suppose we want a term that will produce a12b21z11 - a22b22z11. We can try
And here is the great thing. These two terms are in the same orbit under our
group action and have the opposite sign. Thus if we apply all our group operations to
them and add up the results, we will get that orbit minus itself or 0.
And the terms we want will give us the difference in orbits that we want.
Let us recall what this means. The products indicated are the products of the a’s
and b’s here. The z’s tell us where the products go in the product matrix.
Thus in the first term, we must put the first product, (a11 +a22) (b11 +b22) in both
the 11 and 22 entries of the product matrix. Similarly, the second product, (a12 - a22) (b21 +
b22) goes in the 11 entry, the last, (a12 + a11 ) b22 appears with a positive sign in the 12
entry and with a negative sign in the 11 entry of the matrix.
Then, once the results are obtained for these multiplications, they must be
reassembled into the product matrix. This requires an additional 8 additions or
subtractions of such matrices for each product of same, for a total of 18 additions or
subtractions of 2j by 2j matrices for each multiplication of same.
1 2k level product, 7 2k-1 level products, 72 2k-2 level products, and so on.
At level 2k there will be 18 additions each of 22k-2 matrix elements (namely of all
the elements of the matrices of half the size of the original matrix that form the elements
of the top level 2 by 2 matrix)
At the next level there will 7/4 as many sums: the 7 comes from there being 7
times as many matrices to deal with, the 4 from the fact that they have half the size and
hence a quarter as many elements.
To do it you must form the 7 combinations of a’s and b’s to be multiplied. Then
multiply together the appropriate combinations, then put them in the right places in the
resulting matrix. (You have to remember that the coefficient of z12 goes into the 21
element of the product matrix-which is the transpose of what you might think, but that is
the only tricky point.)
4. Matrix Magic on a Spreadsheet
Thus if you enter your k by k matrix A in k rows and columns and put B
somewhere next to it, you can place the product AB similarly next to B, by entering the
dot product of the first row of A with the first column of B in its upper left corner.
If you put dollar signs on all occurrences of the middle index, (the one summed
over) when you copy this entry into the k rows and columns starting from it, you get the
product AB, since the other indices will vary and give you the dot product of the rows of
A with the columns of B.
But here is where magic occurs. If you copy further to the right, beyond where the
matrix AB should be, you find another matrix and another and another.
The spreadsheet iterates the matrix multiplication. So what you get after the
product AB is the product of A with it, namely A2B, then A3B, and so on.
And you get all this at the cost only of copying one entry.
You may find this quite mundane, but it seems remarkable to me. But here is
something even you will find remarkable.
Lewis Carroll, the author of Alice in Wonderland, was a mathematician and the
discoverer of a useful theorem about determinants.
Similarly let us define Ajk,lm to be the matrix obtained from A by omitting its j-th
and k-th rows, and l-th and m-th columns.
It then states that the determinant of a matrix multiplied by the determinant of the
matrix obtained by throwing away its outside rows and columns, is the product of the
determinants of submatrices obtained by throwing away the top row and left column with
the one obtained by throwing away the last row and right column, less the product of the
determinant obtained after throwing away the top row and right column and the
determinant obtained after throwing away the last row and left column.
The wonderful thing about this theorem is that you can implement it on a
spreadsheet with one instruction, and it will, if you start with a matrix of 1’s (representing
0 by 0 determinants) then enter your matrix, then enter one entry and copy it down and
across, it will first compute the two by two determinants of submatrices consisting of
adjacent rows and columns, then similar 3 by 3 determinants then 4 by 4 etc., until it
produces the determinant of your original matrix.
It can fail if you try to divide by a determinant that is 0; but this can be avoided by
adding a very small increment appropriately to elements of the original matrix to make all
determinants obtained slightly different from 0. By varying these increments if necessary
you can eliminate any errors they might introduce.
Choose a blank space q rows below the first column of your matrix, and
enter the 2 by 2 determinant of the first two rows and columns of your matrix
divided by the entry q-1 rows above the top of your matrix, and one row to its right,
(which should be a 1).
You must prepare by filling the squares above your matrix up to the q-1’st row by
1’s.
This algorithm computes the determinant with a cubic number of operations, since
it has to compute the sum of 1 + 22 + 32 + . . . (n-1)2 determinants each of which
involves two multiplications a subtraction and a division.
In the previous iteration, just before computing the determinant, this procedure
produces the determinants of matrices obtained by omitting the last row and column, the
last row and first column and under these the determinants of matrices obtained by
omitting the first row and last column and first row and first column.
These are all plus or minus cofactors of elements in the omitted places.
If you want all the cofactors, you can obtain them by copying columns 1 through
n-1 immediately to the right of the n-th column, (you can do that with one instruction
(=top left element) copied into the n-1 next columns) and copying the firsr n-1 rows of
the resulting matrix (similarly) into the n-1 rows immediately beneath it, before you enter
the instruction for your algorithm. You will then have a 2n-1 by 2n-1 square matrix.
This will give you at the end a whole matrix of evaluations of the determinant,
and on the previous iteration will give you the cofactors, up to sign, starting in the second
row and second column.
The reason for this is that when you do this at the next to the last iteration each
adjacent submatrix will omit exactly one row and column and will be a cofactor of the
entry in that row and column, up to a sign. 0.
When n is odd, these values will actually be the cofactors. When n is even,
however, the signs of every second one must be reversed to get the cofactors.
Recall that the cofactors of the matrix divided by the determinant give the
transpose of the inverse of the matrix. Thus this simple algorithm gives all the
information you need to deduce the inverse of the matrix.