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

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

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

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

which gives a -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.

Answer from Qiaochu Yuan on Stack Exchange
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-3d-matrix-multiplication
NumPy - 3D matrix multiplication - GeeksforGeeks
July 23, 2025 - A 3D matrix is nothing but a collection ... multiplication of 3D matrices involves multiple multiplications of 2D matrices, which eventually boils down to a dot product between their row/column vectors....
Top answer
1 of 5
53

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

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

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

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

which gives a -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.

2 of 5
9

Sorry to revive the thread, but what I found might answer the original question and help others who might stumble into this in the future. This came up for me when I wanted to avoid using for-loop and instead do one big multiplication on 3D matrices.

So first, let's look how matrix multiplication works. Say you have A[m,n] and B[n,p]. One requirement is that number of columns of A must match the number of rows of B. Then, all you do is iterate over rows of A (i) and columns of B (j) and the common dimension of both (k) (matlab/octave example):

m=2;n=3;p=4;A=randn(m,n);B=randn(n,p);
C=zeros(m,p);
for i = 1:m
    for j = 1:p
        for k = 1:n
             C(i,j) = C(i,j) + A(i,k)*B(k,j);   
        end
    end
end
C-A*B %to check the code, should output zeros

So the common dimension n got "contracted" I believe (Qiaochu Yuan's answer made so much sense once I started coding it).

Now, assuming you want something similar to happen in 3D case, ie one common dimension to contract, what would you do? Assume you have A[l,m,n] and B[n,p,q]. The requirement of the common dimension is still there - the last one of A must equal the first one of B. Then theoretically (this is just one way to do it and it just makes sense to me, no other foundation for this), n just cancels in LxMxNxNxPxQ and what you get is LxMxPxQ. The result is not even the same kind of creature, it is not 3-dimensional, instead it grew to 4D (just like Qiaochu Yuan pointed out btw). But oh well, how would you compute it? Well, just append 2 more for loops to iterate over new dimensions:

l=5;m=2;n=3;p=4;q=6;A=randn(l,m,n);B=randn(n,p,q);
C=zeros(l,m,p,q);
for h = 1:l
    for i = 1:m
        for j = 1:p
            for g = 1:q
                for k = 1:n
                    C(h,i,j,g) = C(h,i,j,g) + A(h,i,k)*B(k,j,g);
                end
            end
        end
    end
end

At the heart of it, it is still row-by-column kind of operation (hence only one dimension "contracts"), just over more data.

Now, my real problem was actually A[m,n] and B[n,p,q], where the creatures came from different dimensions (2D times 3D), but it seems doable nonetheless (afterall matrix times vector is 2D times 1D). So for me the result is C[m,p,q]:

m=2;n=3;p=4;q=5;A=randn(m,n);B=randn(n,p,q);
C=zeros(m,p,q);Ct=C;
for i = 1:m
    for j = 1:p
        for g = 1:q
            for k = 1:n
                C(i,j,g) = C(i,j,g) + A(i,k)*B(k,j,g);
            end
        end
    end
end

which checks out against using the full for-loops:

for j = 1:p
    for g = 1:q
        Ct(:,j,g) = A*B(:,j,g); %"true", but still uses for-loops
    end
end
C-Ct

but doesn't achieve my initial goal of just calling some built-in matlab function to do the work for me. Still, it was fun to play with this.

Discussions

tensors - Multiplying 3D matrix - Mathematics Stack Exchange
I was wondering if it is possible to multiply a 3D matrix (say a cube $n\times n\times n$) to a matrix of dimension $n\times 1$? If yes, then how. Maybe you can suggest some resources which I can r... More on math.stackexchange.com
🌐 math.stackexchange.com
3D matrix multiplication
3D matrix multiplication. Learn more about 3d matrix multiplication More on mathworks.com
🌐 mathworks.com
5
1
June 24, 2011
matlab - Multiply a 3D matrix with a 2D matrix - Stack Overflow
Suppose I have an AxBxC matrix X and a BxD matrix Y. Is there a non-loop method by which I can multiply each of the C AxB matrices with Y? More on stackoverflow.com
🌐 stackoverflow.com
A loopless 3D matrix multiplication in python - Stack Overflow
I am looking to do the following operation in python (numpy). Matrix A is M x N x R Matrix B is N x 1 x R Matrix multiply AB = C, where C is a M x 1 x R matrix. Essentially each M x N layer of A ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnmath › can we multiply 3 dimension matrices?
r/learnmath on Reddit: Can we multiply 3 dimension matrices?
July 9, 2022 -

I'm reading these notes on reinforcement learning: https://nbro.gitlab.io/blogging/2022/02/16/bellman-equations/#vectorized-form

The author gives the equation

Q𝜋 = R + 𝛾PV𝜋

Where Q𝜋 and R are defined as matrices in the space of the cartesian product of two sets, which I think makes sense. Also V𝜋 is a vector.

But P is defined as a cartesian product of three sets, meaning that it would be a 3 dimensional matrix right? How do we multiply a vector by a 3D matrix (or how do we multiply 3D matrices at all for that matter)?

🌐
Illinois
rsim.cs.illinois.edu › arch › qual_papers › systems › 11.pdf pdf
A three- dimensional approach to parallel matrix multiplication
with B, and C,. The 3D algorithm features a single matrix · multiplication, A,B,, on every processor (i, j , 1 ).
🌐
IEEE Xplore
ieeexplore.ieee.org › document › 5389455
A three-dimensional approach to parallel matrix multiplication | IBM Journals & Magazine | IEEE Xplore
A three-dimensional (3D) matrix multiplication algorithm for massively parallel processing systems is presented. The P processors are configured as a “virtual” processing cube with dimensions p 1 , p 2 , and p 3 proportional to the matrices' dimensions—M, N, and K. Each processor performs a single local matrix multiplication of size M/p 1 × N/p 2 × K/p 3 ...
Find elsewhere
🌐
Gamemath
gamemath.com › book › matrixintro.html
Introduction to Matrices - 3D Math Primer for Graphics and Game Development
Section 4.1.4 describes matrix transposition. Section 4.1.5 explains how to multiply a matrix by a scalar.
🌐
University of Texas
cs.utexas.edu › ~flame › pubs › FLAWN62.pdf pdf
Parallel Matrix Multiplication: 2D and 3D FLAME Working Note #62 Martin Schatz
We call this general class of algorithms General 3D algorithms, or · G3D algorithms for short. ... • It systematically exposes the link between collective communication, matrix and vector distribution, and matrix-vector operations like matrix-vector multiplication and rank-1 update.
🌐
YouTube
youtube.com › watch
Matrix Multiplication for 3D Rendering - YouTube
In this video, I write a function to perform matrix multiplication in Java. This is in anticipation of videos about 3D projection and rotation.🔗 Matrix Mult...
Published: August 20, 2018
🌐
YouTube
youtube.com › udacity
Matrix Vector Multiplication - Interactive 3D Graphics - YouTube
This video is part of an online course, Interactive 3D Graphics. Check out the course here: https://www.udacity.com/course/cs291.
Published: February 23, 2015
Views: 1K
🌐
NV5 Geospatial
nv5geospatialsoftware.com › learn › blogs › blog-details › internal-three-dimensional-3d-matrix-multiplication
Internal: Three-dimensional (3D) matrix multiplication - NV5 Geospatial
May 9, 2017 - The array multiply operator '#' only multiplies at the first level of an array even though no error is generated. The following lines are an example: IDL> a = FINDGEN(3, 3, 3) IDL> b = FINDGEN(3, 3, 3) IDL> c = b # a IDL> PRINT, c 15.0000 18.0000 21.0000 42.0000 54.0000 66.0000 69.0000 90.0000 111.000 IDL> HELP, c C FLOAT = Array(3, 3) To access the third dimension with the '#' operator you must explicitly state...
🌐
SCIRP
scirp.org › home › open access library journal › vol.4 no.5, may 2017 › 3d matrix ring with a “common” multiplication
3D Matrix Ring with a “Common” Multiplication
May 15, 2017 - A problem here was the definition of multiplication, which we have given in analogy with the usual 2D matrices. The goal here is 3D matrices to be a generalization of 2D matrices. Work initially we started with 3×3×3 matrix, and then we extended to m×n×p matrices.
🌐
YouTube
youtube.com › watch
Understanding Numpy Matmul in 3D through Examples - YouTube
Here I take you through many examples of Numpy matrix multiplication with combinations of 3D, 2D, and 1D arrays. By the end of the video, you'll have a deep ...
Published: August 23, 2023
🌐
Fast.ai
forums.fast.ai › part 1 (2018)
3D matrix multiplication - Part 1 (2018) - fast.ai Course Forums
January 23, 2018 - I’m into Lesson 8 - of Intro to ML . I’m not able to wrap my head around 3d matrix multiplication. Multiplying two matrices of size MN and NQ gives M*Q. This is intuitive and I can visualize. I’m not able to do the same…
🌐
PyTorch
pytorch.org › blog › inside-the-matrix
Inside the Matrix: Visualizing Matrix Multiplication, Attention and Beyond - PyTorch
November 14, 2024 - Use 3D to visualize matrix multiplication expressions, attention heads with real weights, and more. Matrix multiplications (matmuls) are the building blocks of today’s ML models. This note presents mm, a visualization...