## Multiple matrix multiplications, with array expansion enabled (Scripts) Publisher's description

from Paolo de Leva

### MULTIPROD is a powerful, quick and memory efficient generalization for N-D arrays of the MATLAB matrix multiplication operator (*)

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.

#### System Requirements:

MATLAB 7 (R14)**Program Release Status:**New Release

**Program Install Support:**Install and Uninstall