Paolo De Leva

03-01-2006, 07:54 PM

Dear subscribers,

my previous posting with the same subject had extra blank lines which made

it hard to read. The problem was due to a bad conversion, performed by the

LISTSERV software, from my HTML submission to the required plain text

version. I apologize for the inconvenience. Here is the correct version:

The MULTIPROD toolbox for the MATLAB programming language is freely

available on the following web server:

http://www.mathworks.com/matlabcentral/fileexchange/

FILE EXCHANGE CATHEGORY:

- MATLAB Central > File Exchange > Mathematics > Linear Algebra

TITLE:

- Multiplying two N-D arrays of matrices, vectors or scalars

----------------------------------------------------------------

MULTIPROD TOOLBOX

Multiplying two N-D arrays of matrices, vectors or scalars

By Paolo de Leva

----------------------------------------------------------------

KEYWORDS

N-D arrays, Multidimensional arrays, Matrix, Vector, Scalar, Product,

Multiplication, Inner product, Outer product, Matrix expansion,

Transposition.

SUMMARY

Any kind of multiple products between two block arrays of matrices, vectors

or scalars.

DESCRIPTION

MULTIPROD is a powerful generalization for N-D arrays of the MATLAB function

MTIMES and the matrix multiplication operator (*). While MTIMES works only

with 2-D arrays, MULTIPROD works also with multidimensional arrays.

Besides the element-wise multiplication operator (.*), MATLAB 7 includes

only two functions which can perform products between N-D arrays: DOT and

CROSS. However, these functions can only perform two kinds of product: the

dot product and the cross product, respectively. MULTIPROD can perform any

kind of linear algebraic and vectorial products, except for cross products:

1) Both outer and inner products between arrays of vectors

2) Arrays of scalars by arrays of scalars, vectors or matrices

3) Arrays of vectors by arrays of scalars, vectors or matrices

4) Arrays of matrices by arrays of scalars, vectors or matrices

Synthetically, it can be stated that, with MULTIPROD, the "matrix

laboratory" MATLAB evolves becoming ARRAYLAB, an "array laboratory".

Multidimensional arrays may contain matrices or vectors or even scalars

along one or two of their dimensions. For instance, a 2x5x6 array A contains

six 2x5 matrices along its first and second dimension. Thus, array A can be

described as a block array of matrices, and its size can be indicated as

follows: (2x5)x6.

Let's say that:

A is (2x5)x6

B is (5x3)x6

C is (2x3)x6

With MULTIPROD, the six matrices in A can be multiplied by the six matrices

in B in a single step:

C = MULTIPROD(A, B)

Any multidimensional array can be described as a block array. Here are a few

examples:

A is a 2x5x(6x3) array containing matrices along dim. 3 and 4

B is a 2x5x(3x4) array containing matrices along dim. 3 and 4

C is a 2x5x(1x1) array containing scalars along dim. 3 and 4

D is a 2x5x(6) array containing vectors along dim. 3

E is a 2x5x(3) array containing vectors along dim. 3

F is a 2x5x(1) array containing scalars along dim. 3

For instance, MULTIPROD can:

- Multiply the 10 matrices in A by the 10 matrices, vectors or

scalars occupying the same position in B, C, E, or F

- Multi-multiply D by A

- Multi-multiply the arrays of scalars C and F by any of the other arrays

- Perform multiple outer products between D and E

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 or by

using parts of its code are included in the separate toolbox "Matlab Central

> File Exchange > Vector algebra for multidimensional arrays of vectors".

A sample function (LOC2LOC) performing a multiple roto-translation by

calling MULTIPROD is included in this package. This function uses 3-element

translation vectors and 3x3 rotation matrices. If you prefer to work with

homogeneous coordinates and 4x4 roto-translation matrices, you can obtain

the same result just by calling MULTIPROD twice.

If you have a single matrix R, and you want to multiply it by an array of

vectors MultiV, you need to use the function MATEXP to replicate R and

create an array of matrices MultiR which can be multi-multiplied by MultiV

with MULTIPROD:

MultiV2 = MULTIPROD(MultiR, MultiV, ...)

MATEXP is included in this package.

MULTIPROD calls, in some cases, the function MULTITRANSP, performing matrix

transpositions in arrays of matrices. I wrote it as a separate function

because I discovered it can be useful for several different purposes.

MULTITRANSP is included in this package.

The help text in the function was written with extreme care, and should be

enough for those who want to quickly understand and use the function.

Further information is given in a short manual, written in Adobe PDF format.

For those who want to understand the function code, a document written with

MS Word and explaining in detail and schematically the meaning of the

implemented syntaxes is included.

Since I wanted to be of service to as many people as possible, MULTIPROD

itself was written, debugged, refined and in several parts even optimized

for speed with extreme care. I hope it or a future version of it, modified

according to your suggestions, will be included in the next version of

MATLAB. The code ("testMULTIPROD.m") I used to test the function output is

included in this package.

Examples:

1) If A is a 5x(6x3)x2 array,

and B is a 5x(3x4)x2 array,

C = MULTIPROD(A,B,[2 3]) is a 5x(6x4)x2 array.

2) If A is a 5x(6x3)x2 array,

and B is a 5x(1x1)x2 array,

C = MULTIPROD(A,B,[2 3]) is a 5x(6x3)x2 array.

3) If A is a 5x(6x3)x2 4-D array,

and B is a 5x(3)x2 3-D array,

C = MULTIPROD(A,B,[2 3],[2]) is a 5x(6)x2 3-D array.

4) If both A and B are 5x(6)x2 3-D arrays,

C = MULTIPROD(A,B,[2 0],[0 2]) is a 5x(6x6)x2 4-D array.

-----------------------------------------------------------

With my kindest regards,

Paolo de Leva

Department of Human Movement and Sport Sciences

University Institute of Movement Science

Rome, ITALY

my previous posting with the same subject had extra blank lines which made

it hard to read. The problem was due to a bad conversion, performed by the

LISTSERV software, from my HTML submission to the required plain text

version. I apologize for the inconvenience. Here is the correct version:

The MULTIPROD toolbox for the MATLAB programming language is freely

available on the following web server:

http://www.mathworks.com/matlabcentral/fileexchange/

FILE EXCHANGE CATHEGORY:

- MATLAB Central > File Exchange > Mathematics > Linear Algebra

TITLE:

- Multiplying two N-D arrays of matrices, vectors or scalars

----------------------------------------------------------------

MULTIPROD TOOLBOX

Multiplying two N-D arrays of matrices, vectors or scalars

By Paolo de Leva

----------------------------------------------------------------

KEYWORDS

N-D arrays, Multidimensional arrays, Matrix, Vector, Scalar, Product,

Multiplication, Inner product, Outer product, Matrix expansion,

Transposition.

SUMMARY

Any kind of multiple products between two block arrays of matrices, vectors

or scalars.

DESCRIPTION

MULTIPROD is a powerful generalization for N-D arrays of the MATLAB function

MTIMES and the matrix multiplication operator (*). While MTIMES works only

with 2-D arrays, MULTIPROD works also with multidimensional arrays.

Besides the element-wise multiplication operator (.*), MATLAB 7 includes

only two functions which can perform products between N-D arrays: DOT and

CROSS. However, these functions can only perform two kinds of product: the

dot product and the cross product, respectively. MULTIPROD can perform any

kind of linear algebraic and vectorial products, except for cross products:

1) Both outer and inner products between arrays of vectors

2) Arrays of scalars by arrays of scalars, vectors or matrices

3) Arrays of vectors by arrays of scalars, vectors or matrices

4) Arrays of matrices by arrays of scalars, vectors or matrices

Synthetically, it can be stated that, with MULTIPROD, the "matrix

laboratory" MATLAB evolves becoming ARRAYLAB, an "array laboratory".

Multidimensional arrays may contain matrices or vectors or even scalars

along one or two of their dimensions. For instance, a 2x5x6 array A contains

six 2x5 matrices along its first and second dimension. Thus, array A can be

described as a block array of matrices, and its size can be indicated as

follows: (2x5)x6.

Let's say that:

A is (2x5)x6

B is (5x3)x6

C is (2x3)x6

With MULTIPROD, the six matrices in A can be multiplied by the six matrices

in B in a single step:

C = MULTIPROD(A, B)

Any multidimensional array can be described as a block array. Here are a few

examples:

A is a 2x5x(6x3) array containing matrices along dim. 3 and 4

B is a 2x5x(3x4) array containing matrices along dim. 3 and 4

C is a 2x5x(1x1) array containing scalars along dim. 3 and 4

D is a 2x5x(6) array containing vectors along dim. 3

E is a 2x5x(3) array containing vectors along dim. 3

F is a 2x5x(1) array containing scalars along dim. 3

For instance, MULTIPROD can:

- Multiply the 10 matrices in A by the 10 matrices, vectors or

scalars occupying the same position in B, C, E, or F

- Multi-multiply D by A

- Multi-multiply the arrays of scalars C and F by any of the other arrays

- Perform multiple outer products between D and E

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 or by

using parts of its code are included in the separate toolbox "Matlab Central

> File Exchange > Vector algebra for multidimensional arrays of vectors".

A sample function (LOC2LOC) performing a multiple roto-translation by

calling MULTIPROD is included in this package. This function uses 3-element

translation vectors and 3x3 rotation matrices. If you prefer to work with

homogeneous coordinates and 4x4 roto-translation matrices, you can obtain

the same result just by calling MULTIPROD twice.

If you have a single matrix R, and you want to multiply it by an array of

vectors MultiV, you need to use the function MATEXP to replicate R and

create an array of matrices MultiR which can be multi-multiplied by MultiV

with MULTIPROD:

MultiV2 = MULTIPROD(MultiR, MultiV, ...)

MATEXP is included in this package.

MULTIPROD calls, in some cases, the function MULTITRANSP, performing matrix

transpositions in arrays of matrices. I wrote it as a separate function

because I discovered it can be useful for several different purposes.

MULTITRANSP is included in this package.

The help text in the function was written with extreme care, and should be

enough for those who want to quickly understand and use the function.

Further information is given in a short manual, written in Adobe PDF format.

For those who want to understand the function code, a document written with

MS Word and explaining in detail and schematically the meaning of the

implemented syntaxes is included.

Since I wanted to be of service to as many people as possible, MULTIPROD

itself was written, debugged, refined and in several parts even optimized

for speed with extreme care. I hope it or a future version of it, modified

according to your suggestions, will be included in the next version of

MATLAB. The code ("testMULTIPROD.m") I used to test the function output is

included in this package.

Examples:

1) If A is a 5x(6x3)x2 array,

and B is a 5x(3x4)x2 array,

C = MULTIPROD(A,B,[2 3]) is a 5x(6x4)x2 array.

2) If A is a 5x(6x3)x2 array,

and B is a 5x(1x1)x2 array,

C = MULTIPROD(A,B,[2 3]) is a 5x(6x3)x2 array.

3) If A is a 5x(6x3)x2 4-D array,

and B is a 5x(3)x2 3-D array,

C = MULTIPROD(A,B,[2 3],[2]) is a 5x(6)x2 3-D array.

4) If both A and B are 5x(6)x2 3-D arrays,

C = MULTIPROD(A,B,[2 0],[0 2]) is a 5x(6x6)x2 4-D array.

-----------------------------------------------------------

With my kindest regards,

Paolo de Leva

Department of Human Movement and Sport Sciences

University Institute of Movement Science

Rome, ITALY