Clone wiki

blaze / 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:

using blaze::DynamicVector;
using blaze::CompressedVector;

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 dense and a sparse 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:

blaze::StaticVector<int,3UL,rowVector> v1{  2, 5, -1 };
blaze::DynamicVector<int,columnVector> v2{ -1, 3, -2 };

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

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

blaze::StaticVector<int,3UL,rowVector> v1{  2, 5, -1 };
blaze::StaticVector<int,3UL,rowVector> v2{ -1, 3, -2 };

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:

blaze::StaticVector<int,3UL,columnVector> v1{  2, 5, -1 };
blaze::StaticVector<int,3UL,rowVector>    v2{ -1, 3, -2 };

// 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:

blaze::StaticVector<int,3UL,columnVector> v1{ 2, 5, -1 };
blaze::DynamicVector<int,rowVector> v2{ -1, 3, -2 };

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

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

blaze::StaticVector<int,3UL,rowVector> v1{  2, 5, -1 };
blaze::StaticVector<int,3UL,rowVector> v2{ -1, 3, -2 };

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:

blaze::StaticVector<int,3UL,rowVector> v1{  2, 5, -1 };
blaze::StaticVector<int,3UL,rowVector> v2{ -1, 3, -2 };

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

images/crossProduct.jpg

Due to the absence of a x 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:

blaze::StaticVector<int,3UL,columnVector> v1{  2, 5, -1 };
blaze::DynamicVector<int,columnVector>    v2{ -1, 3, -2 };

blaze::StaticVector<int,3UL,columnVector> v3( cross( v1, v2 ) );
blaze::StaticVector<int,3UL,columnVector> v4( v1 % v2 );

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


Previous: Scalar Multiplication ---- Next: Vector/Vector Division

Updated