# Is there a 3-dimensional “matrix” by “matrix” product?

Is it possible to multiply A[m,n,k] by B[p,q,r]? Does the regular matrix product have generalized form?

I would appreciate it if you could help me to find out some tutorials online or mathematical 'word' which means N-dimensional matrix product.

Upd. I'm writing a program that can perform matrix calculations. I created a class called matrix and made it independent from the storage using object oriented features of C++. But when I started to write this program I thought that it was some general operation to multiply for all kinds of arrays(matrices). And my plan was to implement this multiplication (and other operators) and get generalized class of objects. Since this site is not concerned with programming I didn't post too much technical details earlier. Now I'm not quite sure if that one general procedure exists. Thanks for all comments.

-
You seem to be asking about tensors, not matrices. There are lots of products, depending on what pairs of indices you want to sum over; the most basic one is the tensor product. –  Zhen Lin Sep 9 '11 at 10:51
You mean rank-3 tensors? –  Ｊ. Ｍ. Sep 9 '11 at 10:52
@168335: I think what is meant is not just tensor products, but tensor contractions. –  Willie Wong Sep 9 '11 at 13:26
I wrote a blog post about the question of why matrices must be two-dimensional. It may be useful to you. wilsonericn.wordpress.com/2011/09/15/… –  Eric Wilson Sep 16 '11 at 1:57

The general procedure is called tensor contraction. Concretely it's given by summing over various indices. For example, just as ordinary matrix multiplication $C = AB$ is given by

$$c_{ij} = \sum_k a_{ik} b_{kj}$$

we can contract by summing across any index. For example, we can write

$$c_{ijlm} = \sum_k a_{ijk} b_{klm}$$

which gives a $4$-tensor ("$4$-dimensional matrix") rather than a $3$-tensor. One can also contract twice, for example

$$c_{il} = \sum_{j,k} a_{ijk} b_{kjl}$$

which gives a $2$-tensor.

The abstract details shouldn't matter terribly unless you explicitly want to implement mixed variance, which as far as I know nobody who writes algorithms for manipulating matrices does.

-
This looks like what I need. Changing the number of indices from 3 to 4 in the second expression and from 3 to 2 in the last one still looks strange to me. I think I have to restrict my matrix class with 2 dimensions for a while and read some books about tensors. Thanks for the answer. –  danny_23 Sep 9 '11 at 19:59
It seems to me, that the question is not about tensor product. Although we can represent second-rank tensor as a matrix of its components in some coordinates, regular matrix product doesn't produce 4-rank tensor. Regular matrix product may be treated as a coordinate way to represent a composition $A(B(x))$ of two linear maps $A$ and $B$.
to Danny_23: It would be nice if you can tell us what kind of operation do you like to represent using your 3-matrix $A[m,n,k]$ and $B[p,q,r]$.
As far as I can see, there is no problem to (re)define multiplication between two sparse matrix (i.e. between two arrays of data $A[m,n,k]$ and $B[p,q,r]$), but it's rather coding question, not math. –  Ivan Polekhin Sep 9 '11 at 19:35
Since your matrix are just two arrays of data, which can be used to construct $N \times N$ sparse matrix, but not matrix (or any other math object) itself, then it's hard to offer you some kind of matrix multiplication. Even if you try to use tensor contraction, then you will face two problems 1) your matrix should be 'cubes', i.e. $A[n,n,n]$ and $B[n,n,n]$, which is not true for sparse matrix storing formats 2) even if your matrix are 'cubes' this operation does not make sense, i.e. you will get some 'cube' $C[n,n,n]$, but it will be quite senseless object. –  Ivan Polekhin Sep 9 '11 at 19:52