4.95455

5.0 | 23 ratings Rate this file 93 downloads (last 30 days) File Size: 505.35 KB File ID: #8773

Multiple matrix multiplications, with array expansion enabled

by Paolo de Leva

 

20 Oct 2005 (Updated 26 Jul 2010)

Code covered by the BSD License  

Multiplying matrices, vectors, or scalars contained in two N-D arrays, with array expansion enabled.

Download Now | Watch this File

File Information
Description

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.

Acknowledgements
This submission has inspired the following:
Vector algebra for arrays of any size, with array expansion enabled
MATLAB release MATLAB 7 (R14)
Other requirements MULTIPROD calls the builtin function BSXFUN, available in MATLAB R2007a. For earlier MATLAB releases, use Schwarz's BSXFUN substitute (MATLAB Central, file #23005, http://www.mathworks.com/matlabcentral/fileexchange/23005)
Zip File Content  
Other Files
Appendix A - Algorithm.pdf,
Appendix B - Testing speed and memory usage.pdf,
Appendix C - Syntaxes.pdf,
license.txt,
loc2loc.m,
MULTIPROD Toolbox Manual.pdf,
multiprod.m,
multitransp.m,
Testing/arraylab13.m,
Testing/arraylab131.m,
Testing/arraylab132.m,
Testing/arraylab133.m,
Testing/Data/Memory used by MATLAB statements.xls,
Testing/Data/Timing results.xlsx,
Testing/Data/timing_MX.txt,
Testing/genop.m,
Testing/multiprod13.m,
Testing/readme.txt,
Testing/sysrequirements_for_testing.m,
Testing/testing_memory_usage.m,
Testing/testMULTIPROD.m,
Testing/timing_arraylab_engines.m,
Testing/timing_matlab_commands.m,
Testing/timing_MX.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (23)
08 Nov 2005 Randy Wells

Good job. Very useful when manipulating time dependant transformation matrices (vector of 3x3 matrices). No longer need a for loop.

23 Nov 2005 Bulent Ozer

Good job. Thanks to your program got rid of the loop. My code is much faster now. Thanks

27 Jan 2006 Magnus Karlsson

Super!
Have I longed for this functionality in Matlab! Farewell for-looops!

This should be an obvious part of Matlab, and it is amazing it has not been done until now.
It extends the functionality of Matlab in amazing ways, as simulations requiring for-loops now can take advantage of Matlab's inherent fast matrix multiplication algorithms.

Mr deLeva deserves Mathwork's medal in gold for this.

25 Apr 2006 paul naude

I needed to multiply a matrix M (with variable number of rows) with a calibration vector V and by using multiprod(M,V,[1]), it callibarted my file in this ONE step!

25 Apr 2006 paul naude

Now, where do I find a file that can subtract my DC offset (vector) from my data matrix? I.e B = A - V where A may have many rows and the columns in A and number of elements in V agree. Obviously I don't want to find the size of A first and then run a FOR loop!

27 Apr 2006 Ryan Eustice

Just what I was looking for. This capability really should be a builtin for Matlab.

01 Aug 2006 Matthew G

This is an extremely useful library, I was trying to figure out how to do this on my own, but I'm lucky this page came up in the google search.

04 Aug 2006 Arun Mano

awesome stuff! just got what i was looking for!
Pretty efficient too!

14 Oct 2006 Juan D'Adamo  
04 Dec 2006 Pietro Picerno

just fantastic, many compliments, it's a great and well-done job!

05 Dec 2006 Giuseppe Vannozzi

Awesome! Thank you for sharing this useful package

04 Mar 2007 Lucas Finn

Well documented, well optimized, and works.

28 Sep 2007 Paul Macey

Brilliant - this does exactly what I want, performing vector re-orientation on a 3D array (i.e., 3D x 3 x 3 rotation matrix multiplied by 3D x 3 vector matrix); very fast.

10 Oct 2007 Friedemann Groh

Cool. Thank You!

21 May 2009 Noah Snavely

This is an amazingly useful tool. Anyone who uses matrices will---at some point in their life---be stymied by a problem for which this software is the perfect solution. It just works.

29 Jun 2009 Erdal Bizkevelci  
31 Jul 2009 Chris Sarantos

Question: Can multiprod be used to multiply a single array of matrices together along one dimension? For example if you have a 2x2xN array, and want to multiply the 2x2 matrices together along the third dimension to get a 2x2x1 output?

31 Jul 2009 Paolo de Leva

Chris, thak you for your rating. What you describe is indeed a useful operation which is described in geometry as a transformation matrix composition (a special kind of function composition), but it is different from what MULTIPROD does. MULTIPROD is designed to operate on 2 block arrays of matrices or vectors or scalars, not on a single block array. You can compare it with DOT and CROSS, which also operate on 2 block arrays. In other words, MULTIPROD, DOT and CROSS can be described as binary operations, while you need a unary operation (operating on a single operand). Possibly, you or some other author will be willing to design and publish an optimized function to do what you need. Such a function would be consistent with what I called the "ARRAYLAB" phylosophy (an evolution of the original MATLAB approach, see MULTIPROD manual).
Sorry for rating myself. I did that by mistake and did not find a way to undo it.

12 Aug 2009 Denis Kozlov  
03 Nov 2009 Deepak

Nice program to change all the lines of code into one command. really useful in simulations etc.

04 Mar 2010 Aleksandra  
05 Aug 2010 Tanyer Alan  
10 Aug 2010 Soravit  
Please login to add a comment or rating.
Updates
31 Oct 2005

I discovered an error in the discussion, which I hadn't found in my first editing. I corrected it and also decided to further refine the whole text to improve its readability.

22 Feb 2006

The satellite function REPMAT2 was substituted with MATEXP (matrix expansion). The manual was refined.

22 Feb 2006

The satellite function REPMAT2 was substituted with MATEXP (matrix expansion). The manual was refined.

01 Mar 2006

1. Solved character conversion problem in "File Description" text.
2. Included a "MULTIPROD toolbox manual" in Acrobat (PDF) format.

30 Mar 2006

The code was optimized according to suggestions automatically generated by function M-Lint.

30 Mar 2006

The code was optimized according to suggestions automatically generated by function M-Lint.

21 Nov 2006

Version 1.2.1 (2006b). Improved management of syntax errors by the user. Revised comments and manual.

03 Oct 2007

Fixed a bug: when input contained complex numbers, in some circumstances MULTIPROD returned an incorrect result. MULTIPROD is defined as a multiple matrix algebra multiplication over both the real and complex fields.

03 Oct 2007

Fixed a bug: when input contained complex numbers, in some circumstances MULTIPROD returned an incorrect result. MULTIPROD is defined as a multiple matrix algebra multiplication over both the real and complex fields.

03 Oct 2007

Updated help text in MULTIPROD

27 Feb 2009

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

27 Feb 2009

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.

07 Mar 2009

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

26 Jul 2010

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 (.*)

Tag Activity for this File
Tag Applied By Date/Time
linear algebra Paolo de Leva 22 Oct 2008 08:03:34
matrix Paolo de Leva 22 Oct 2008 08:03:34
vector Paolo de Leva 22 Oct 2008 08:03:34
scalar Paolo de Leva 22 Oct 2008 08:03:34
product Paolo de Leva 22 Oct 2008 08:03:34
arraylab Paolo de Leva 22 Oct 2008 08:03:34
multiplication Paolo de Leva 22 Oct 2008 08:03:34
array Paolo de Leva 24 Feb 2009 04:54:34
multidimensional array Paolo de Leva 24 Feb 2009 04:54:34
algebra Paolo de Leva 24 Feb 2009 04:54:34
linear Paolo de Leva 24 Feb 2009 04:54:34
transformation Paolo de Leva 24 Feb 2009 04:54:34
transposition Paolo de Leva 24 Feb 2009 04:55:47
array expansion Paolo de Leva 24 Feb 2009 04:58:35
replication Paolo de Leva 24 Feb 2009 04:58:35
expansion Paolo de Leva 24 Feb 2009 04:58:35
matrix expansion Paolo de Leva 24 Feb 2009 04:58:35
vector expansion Paolo de Leva 24 Feb 2009 04:58:35
singleton expansion Paolo de Leva 24 Feb 2009 04:58:35
vector algebra Paolo de Leva 24 Feb 2009 05:21:12
dot product Paolo de Leva 24 Feb 2009 05:21:12
inner product Paolo de Leva 24 Feb 2009 05:21:12
scalar product Paolo de Leva 24 Feb 2009 05:21:12
outer product Paolo de Leva 24 Feb 2009 05:21:12
multiproduct Paolo de Leva 24 Feb 2009 08:19:05
multiple product Paolo de Leva 24 Feb 2009 08:20:15
multiple multiplication Paolo de Leva 24 Feb 2009 08:20:15
virtual expansion Paolo de Leva 24 Feb 2009 08:21:26
virtual replication Paolo de Leva 24 Feb 2009 08:21:57
matrix algebra Paolo de Leva 27 Feb 2009 13:08:36
matrix multiplication Paolo de Leva 27 Feb 2009 13:09:08
multiprod Paolo de Leva 27 Feb 2009 13:09:25
multitransp Paolo de Leva 27 Feb 2009 13:09:25
mtimes Paolo de Leva 27 Feb 2009 13:09:25
vec Paolo de Leva 09 Mar 2009 13:48:45
self_rating Matt Fig 31 Jul 2009 11:36:59

Contact us at files@mathworks.com