Vector/Vector Multiplication


Componentwise Multiplication


Multiplying two vectors with the same transpose flag (i.e. either blaze::columnVector or blaze::rowVector) via the multiplication operator results in a componentwise multiplication of the two vectors:

CompressedVector<int,columnVector> v1( 17UL );
DynamicVector<int,columnVector> v2( 17UL );
StaticVector<double,10UL,rowVector> v3;
DynamicVector<double,rowVector> v4( 10UL );
// ... Initialization of the vectors
CompressedVector<int,columnVector> v5( v1 * v2 ); // Componentwise multiplication of a sparse and
// a dense column vector. The result is a sparse
// column vector.
DynamicVector<double,rowVector> v6( v3 * v4 ); // Componentwise multiplication of two dense row
// vectors. The result is a dense row vector.


Inner Product / Scalar Product / Dot Product


The multiplication between a row vector and a column vector results in an inner product between the two vectors:

int result = v1 * v2; // Results in the value 15

The trans() function can be used to transpose a vector as necessary:

int result = v1 * trans( v2 ); // Also results in the value 15

Alternatively, either the inner() function, the dot() function or the comma operator can be used for any combination of vectors (row or column vectors) to perform an inner product:

// All alternatives for the inner product between a column vector and a row vector
int result1 = trans( v1 ) * trans( v2 );
int result2 = inner( v1, v2 );
int result3 = dot( v1, v2 );
int result4 = (v1,v2);

When using the comma operator, please note the brackets embracing the inner product expression. Due to the low precedence of the comma operator (lower even than the assignment operator) these brackets are strictly required for a correct evaluation of the inner product.


Outer Product


The multiplication between a column vector and a row vector results in the outer product of the two vectors:

StaticMatrix<int,3UL,3UL> M1 = v1 * v2;

The trans() function can be used to transpose a vector as necessary:

int result = trans( v1 ) * v2;

Alternatively, the outer() function can be used for any combination of vectors (row or column vectors) to perform an outer product:

StaticMatrix<int,3UL,3UL> M1 = outer( v1, v2 ); // Outer product between two row vectors


Cross Product


Two vectors with the same transpose flag can be multiplied via the cross product. The cross product between two vectors $ a $ and $ b $ is defined as

\[ \left(\begin{array}{*{1}{c}} c_0 \\ c_1 \\ c_2 \\ \end{array}\right) = \left(\begin{array}{*{1}{c}} a_1 b_2 - a_2 b_1 \\ a_2 b_0 - a_0 b_2 \\ a_0 b_1 - a_1 b_0 \\ \end{array}\right). \]

Due to the absence of a $ \times $ operator in the C++ language, the cross product is realized via the cross() function. Alternatively, the modulo operator (i.e. operator%) can be used in case infix notation is required:

Please note that the cross product is restricted to three dimensional (dense and sparse) column vectors.


Previous: Scalar Multiplication     Next: Vector/Vector Division