Multiple matrix multiplications, with array expansion enabled

Multiplying matrices, vectors, or scalars contained in two N-D arrays, with array expansion enabled.
9.8K Downloads
Updated 26 Jul 2010

View License

MULTIPROD is a powerful, quick and memory efficient generalization for N-D arrays of the MATLAB matrix multiplication operator (*). While the latter works only with 2-D arrays, MULTIPROD works also with multidimensional arrays.
MULTIPROD performs multiple multiplications between matrices, vectors, or scalars contained in two multidimensional arrays, with automatic virtual array expansion (AX) enabled. AX allows you, for instance, to multiply a single matrix A by an array of matrices B, by virtually replicating A to obtain an array compatible with B.

Multidimensional arrays may contain matrices or vectors or even scalars along one or two of their dimensions. For instance, a 4×5×3 array A contains three 4×5 matrices along its first and second dimension (fig. 1). Thus, array A can be described as a block array the elements of which are matrices, and its size can be denoted by (4×5)×3.

MULTIPROD can be also described as a generalization of the built-in function TIMES. While TIMES operates element-by-element multiplications (e.g. A .* B), MULTIPROD operates block-by-block matrix multiplications.

EXAMPLES

Let's say that

A is (2×5)×6, and
B is (5×3)×6.

With MULTIPROD the six matrices in A can be multiplied by those in B in a single intuitively appealing step:

C = MULTIPROD(A, B).

where C is (2×3)×6.

By automatically applying AX, MULTIPROD can multiply a single matrix by all the blocks of a block array. So, if

A is 2×5 (single matrix), and
B is (5×3)×1000×10,

then C = MULTIPROD(A, B) yields a (2×3)×1000×10 array. A is virtually expanded to a (2×5)×1000×10 size, then multi-multiplied by B. This is done without using loops, and without actually replicating the matrix (see Appendix A). We refer to this particular application of AX as virtual matrix expansion. In a system running MATLAB R2008a, MULTIPROD performs it about 380 times faster than the following equivalent loop (see Appendix B):

for i = 1:1000
for j = 1:10
C(:,:,i,j) = A * B(:,:,i,j);
end
end

AX generalizes matrix expansion to multidimensional arrays of any size. For instance, if

A is (2×5)×10, and
B is (5×3)×1×6,

then C = MULTIPROD(A, B) multiplies each of the 10 matrices in A by each of the 6 matrices in B, obtaining 60 matrices stored in a (2×3)×10×6 array C. It does that by virtually expanding A to (2×5)×10×6, and B to (5×3)×10×6. A detailed definition of AX is provided in the manual.

APPLICATIONS

MULTIPROD has a broad field of potential applications. By calling MULTIPROD, multiple geometrical transformations such as rotations or roto-translations can be performed on large arrays of vectors in a single step and with no loops. Multiple operations such as normalizing an array of vectors, or finding their projection along the axes indicated by another array of vectors can be performed easily, with no loops and with two or three rows of code.

Sample functions performing some of these tasks by calling MULTIPROD are included in the separate toolbox "Vector algebra for multidimensional arrays of vectors" (MATLAB Central, file #8782).

OPTIMIZATION AND TESTING

Since I wanted to be of service to as many people as possible, MULTIPROD was designed, debugged, and optimized for speed and memory efficiency with extreme care. Precious advices by Jinhui Bai (Georgetown University) helped me to make it even faster, more efficient and more versatile. Suggestions to improve it further will be welcome. The code ("testMULTIPROD.m") I used to systematically test the function output is included in this package.

THE ARRAYLAB TOOLBOX

In sum, MULTIPROD is a generalization for N-D arrays of the matrix multiplication function MTIMES, with AX enabled.

Vector inner, outer, and cross products generalized for N-D arrays and with AX enabled are performed by DOT2, OUTER, and CROSS2 (MATLAB Central, file #8782, http://www.mathworks.com/matlabcentral/fileexchange/8782).

Element-by-element multiplications (see TIMES) and other elementwise binary operations (such as PLUS and EQ) with AX enabled are performed by BAXFUN (MATLAB Central, file #23084, http://www.mathworks.com/matlabcentral/fileexchange/23084).

Together, these functions make up the “ARRAYLAB toolbox”. I hope that The MathWorks will include it in the next version of MATLAB.

MULTITRANSP

This package includes the function MULTITRANSP, performing multiple matrix transpositions. B = MULTITRANSP(A, DIM) transposes all the matrices contained along dimensions DIM and DIM+1 of A.

Cite As

Paolo de Leva (2024). Multiple matrix multiplications, with array expansion enabled (https://www.mathworks.com/matlabcentral/fileexchange/8773-multiple-matrix-multiplications-with-array-expansion-enabled), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R14
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Guidance, Navigation, and Control (GNC) in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.37.0.0

Version 2.1 (2009) - Updating manual and description. MULTIPROD operates "block-by-block" matrix multiplication, on block arrays. It is a generalization of both matrix multiplication (*) and element-by-element multiplication (.*)

1.35.0.0

Version 2.1 (2009), with array expansion enabled, and memory efficient double-kernel engine. Updating manual and adding screenshot.

1.33.0.0

Version 2.1 (2009) exploits a quicker, more efficient, and more powerful double-kernel engine. It also introduces virtual array expansion, which allows you, for instance, to multiply a single matrix by an array of matrices.

1.30.0.0

Warning. New version available as soon as technical problems are solved by The Mathworks.

1.0.0.0

Updated help text in MULTIPROD