This toolbox was written to complete the incomplete set of vectorial operations provided with MATLAB, and to enhance the features of two of them (DOT and CROSS), by enabling virtual array expansion (AX). AX is enabled in all the binary operations included in this toolbox, and allows you, for instance, to multiply a single vector with an array of vectors, by virtually replicating the vector to match the size of the array.
All the functions in this toolbox can automatically operate on block arrays (arrays of any size containing vectors along one of their dimensions).
MATLAB includes four functions which can apply binary operations on the vectors contained in two block arrays:
+ (generic operator usable to perform vector additions)
- (generic operator usable to perform vector subtractions)
DOT (specific function performing dot products)
CROSS (specific function performing cross products)
They all perform multiple operations, but they cannot perform automatic AX. MATLAB also includes function SUM, which can be used to add together two or more vectors contained in a single matrix or N-D array. For instance, if A is a MN matrix, regarded as a concatenation of N column vectors, these vectors can be added together by using SUM(A, 2), which yields an M1 matrix.
DOT2 and CROSS2, included in this toolbox, enable AX for dot and cross products.
Outer products, orthogonal cross divisions, and generalized cross divisions with AX enabled, can be performed by calling functions OUTER, OCD, and CROSSDIV, included in this toolbox.
As for cross divisions (CDs), they were not invented until now (see Weisstein, 2008), but I needed them, so I defined and implemented them (de Leva, 2008). They determine one of the infinitely many anticrossproducts of the cross product a b (see screenshot). They were not indispensable, but markedly simplified my equations and my code, and those who appreciate short symbolic equations are likely to love them. Without CDs, in some cases you are forced to write scalar equations, containing negative terms and operations involving various combinations of scalar components. In other cases, you need to write longer vectorial equations. Those equations are not complex, but they are certainly less simple than a CD. For instance, a typo is more likely to occur when you write scalar equations than when you write a CD. See CROSSDIV help for more detailed explanations.
A list of simple vectorial operations is given below. Only the first five are implemented in intrinsic MATLAB functions. The others are implemented in this toolbox. All of them can operate on N-D arrays of vectors. See the respective help texts for further details.
Operation: MATLAB implementation
Repeated addition: SUM
Binary addition: +
Dot product: DOT
Cross product: CROSS
Operation: ARRAYLAB implementation
Euclidean norm: MAGN
Dot product (AX enabled): DOT2
Cross product (AX enabled): CROSS2
Outer product (AX enabled): OUTER
Orthogonal cross division (AX enabled): OCD
Generalized cross division (AX enabled): CROSSDIV
Projection (AX enabled): PROJECTION
Rejection (AX enabled): REJECTION
A detailed definition of AX is given in the manual of the MULTIPROD toolbox, published on MATLAB Central (file #8773).
The ARRAYLAB toolbox
The functions included in this toolbox comply with the ARRAYLAB philosophy, and are a part of the "ARRAYLAB toolbox" (see the manual of the MULTIPROD toolbox, MATLAB Central, file #8773).
Some of them call MULTIPROD. This function is a powerful generalization for N-D arrays of the MATLAB function MTIMES and the matrix multiplication operator (*).
MULTIPROD has a broad field of potential applications. For instance, it can use large arrays of 33 or 44 transformation matrices to perform, in a single step and with no loops, multiple geometrical transformations (rotations, roto-translations) on arrays of vectors. Thus, it deserves a separate introduction and I published it in a separate package on MATLAB Central (file #8773).
Some other functions of this toolbox call BAXFUN, another function which belongs to the ARRAYLAB toolbox and which is published separately on MATLAB Central (file #23084).
The functions testDOT2, testCROSS2, testOUTER, testPROJECTION, testREJECTION, testUNIT, testXDIV, testOCD, stored in a separate folder "Testing", contain the code I used to test the output of the main functions. Since UNIT calls MAGN, testUNIT tests both UNIT and MAGN.
de Leva P., 2008. Anticrossproducts and cross divisions. Journal of Biomechanics, 8, 1790-1800 (http://dx.doi.org/doi:10.1016/j.jbiomech.2007.09.030)
Weisstein E. W. Vector Division. From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/VectorDivision.html (Retreived on 2008 Dec 4)
Dear Xiaodong, thank you for your offer, but I cannot include your function DOT3 in my package. The dot product requires the complex conjugate of its first operand to deal with complex vectors. Even the MATLAB function DOT applies it:
C = SUM(CONJ(A).*B, DIM)
Notice that, for real arrays,
CONJ(A) = A
hence, your function DOT3 will give correct results only when operating on real vectors, while DOT2, included in my package, is supposed to give correct results for both real and complex vectors.
Moreover, your function does nothing else than calling MULTIPROD. It would be more convenient to call it directly, using syntax 4b:
C = MULTIPROD(A, B, DA1, DB2)
For instance, if vectors are contained along dimension 2 of A and B, and you do not want to compute the conj(A), you can use
C = MULTIPROD(A, B, 2, 2)
or more simply
C = MULTIPROD(A, B, 2).
There's a typo at the end of my previous comment. I should have written:
...if vectors are contained along dimension 2 of A and B, you can use
C = MULTIPROD(A, B, 2, 2)
or more simply
C = MULTIPROD(A, B, 2).
Paolo and users of this package: I made a code to do the dot product similar to dot2 but without applying complex conjugate of the first input array: http://www.mathworks.com/matlabcentral/fileexchange/41794-dot-product-of-two-n-dimensional-arrays
In this updated function, C=dot3(A,B)=transpose(A) dot product with B. In Paolo's function library, dot2(A,B)=transpose(conj(A)) dot product with B.
Download as needed. Paolo, welcome to include this function in your package :)
Paolo's Vector Algebra lab was the solution I have been looking for. This should be a part of the standard Matlab distribution because it realizes the full potential of what Matlab was meant to do.
Mark Flynn posted this question elsewhere:
"...I have 2 sets of vectors where I want to get the inner product of each vector in the first set with each vector in the second. E.g. 24 10x1 vectors with 64 10x1 vectors... could you give me some suggestions for how I could do it?"
The Array expansion (AX) engine used by all the functions in this toolbox can solve this kind of problems quickly and efficiently.
DOT2 can perform the operation described by Mark Flynn with a single command, provided that you define the two block arrays as a 24x1x10 array A and a 1x64x10 array B. Both arrays contain 10-element vectors along dimension 3.
A = rand(24,1,10)
B = rand(1,64,10)
PRODUCTS = DOT2(A, B, 3)
PRODUCTS will be a 24 x 64 matrix.
See MULTIPROD manual for details about the Array expansion (AX) engine.
Enjoy my ARRAYLAB toolboxes.
Much much faster than the repmat --> vectorized dot product combination I was using with a large data set. It got called a lot. Thank you for the hours this code saves me!
Hi Pablo, Would you mind re-submitting the MULTIPROD function please? I think it has become lost in the re-jig of The Mathworks' site.
Excellent work, Paolo!
something new to write in Maths' books: the cross division. Excellent!
Version 2.0. Includes two new functions (DOT2, CROSS2). Exploits quicker, more efficient, and more powerful engines. Introduces virtual array expansion, which allows you, for instance, to multiply a single vector by an array of vectors.
Removed two unnecessary and not commented files from the "Testing" folder
Added references, inserted figure about cross division in toolbox manual
Function CROSSDIV was generalized. The new version (2), compatible with the previous ones, can automatically perform any kind of definite cross division. A function performing orthogonal cross divisions was added. The manual was updated.
The function CROSSDIV was generalized (v.2). Now it can automatically perform any kind of definite cross division. A function performing orthogonal cross divisions was added.
The code was optimized according to suggestions automatically generated by function M-Lint.
Updated function CROSSDIV to version 1.2.2. Revised manual.
For the sake of completeness, after working on my other two packages, I thoroughly revised also this Description, and added a "Manual" in the attached file.
With respect to my first update, submitted but not yet published, I updated here only the attached file, now containing new versions of PROJECTION and REJECTION, in which I added help text.
Keyword list and Description were updated. The keyword list had been truncated arbitrarily to 70 characters. Some characters in the Description were not readable and some refinements were needed.
Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.