Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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.

share|improve this question
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? –  J. M. 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
show 2 more comments

3 Answers

up vote 9 down vote accepted

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.

share|improve this answer
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
add comment

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-matrices $A[m,n,k]$ and $B[p,q,r]$.

share|improve this answer
Hi. Thanks for the answer. Frankly speaking, I just wanted to implement adjustable matrix class. And I separated the N-dimensional storage with it's functions from mathematics. This for example allows me to implement sparse matrix storage in different variations without refactoring whole matrix class. And I thought that I could overload * (multiplication) in the same manner as I did with '+' and '-'. Looks like it's not that easy because there are different operations that can be performed on N-dimensional arrays. –  danny_23 Sep 9 '11 at 19:15
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
add comment

A matrix represents any finite-discrete linear transform of field values.

A matrix is a finite-discrete collection of field values. So if you have a linear transform that converts one matrix to another matrix, then the transform itself can be represented with matrix multiplication. Working out all the indices for the sum of product would be tricky, but for a programmer it should be second nature.

An example of a problem where this came up is Flip all to zero

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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