Blaze 3.9
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.
CompressedVector<int,columnVector> v7( mult( v1, v2 ) ); // Alternative syntax
DynamicVector<double,rowVector> v8( mult( v3, v4 ) ); // Alternative syntax
Efficient implementation of an arbitrary sized sparse vector.
Definition: CompressedVector.h:220
Efficient implementation of an arbitrary sized vector.
Definition: DynamicVector.h:223
BLAZE_ALWAYS_INLINE constexpr decltype(auto) mult(T1 &&lhs, T2 &&rhs) noexcept(noexcept(lhs *rhs))
Multiplication of the two given arguments.
Definition: Mult.h:66


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 result1 = v1 * v2; // Results in the value 15
int result2 = mult( v1, v2 ); // Alternative syntax
Efficient implementation of a fixed-sized vector.
Definition: StaticVector.h:230

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

int result = v1 * trans( v2 ); // Also results in the value 15
decltype(auto) trans(const DenseMatrix< MT, SO > &dm)
Calculation of the transpose of the given dense matrix.
Definition: DMatTransExpr.h:766

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);
decltype(auto) inner(const Vector< VT1, TF1 > &lhs, const Vector< VT2, TF2 > &rhs)
Scalar product (dot/inner product) of two vectors ( ).
Definition: Vector.h:248
decltype(auto) dot(const Vector< VT1, TF1 > &lhs, const Vector< VT2, TF2 > &rhs)
Scalar product (dot/inner product) of two vectors ( ).
Definition: Vector.h:283

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:

// Results in the matrix
//
// ( -2 6 -4 8 )
// A = ( -5 15 -10 20 )
// ( 1 -3 2 -4 )
//
blaze::StaticMatrix<int,3UL,3UL> M1 = v1 * v2; // Outer product
blaze::StaticMatrix<int,3UL,3UL> M2 = mult( v1, v2 ); // Alternative syntax
Efficient implementation of a fixed-sized matrix.
Definition: StaticMatrix.h:249

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

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

blaze::StaticMatrix<int,3UL,4UL> M1 = outer( v1, v2 ); // Outer product between two row vectors
decltype(auto) outer(const Vector< VT1, TF1 > &lhs, const Vector< VT2, TF2 > &rhs)
Outer product of two vectors ( ).
Definition: Vector.h:352


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:

decltype(auto) cross(const Vector< VT1, TF > &lhs, const Vector< VT2, TF > &rhs)
Cross product of two vectors ( ).
Definition: Vector.h:387

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


Kronecker Product


The Kronecker product of two vectors with the same transpose flag can be computed via the kron() function:

DynamicVector<double> v1( 28UL );
CompressedVector<float> v2( 17UL );
// ... Initialization of the vectors
CompressedVector<double> v3 = kron( v1, v2 );
decltype(auto) kron(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the Kronecker product of two dense matrices ( ).
Definition: DMatDMatKronExpr.h:957

Both dense and sparse vectors can be used for a Kronecker product. It is possible to multiply two vectors with different element type, as long as the element types themselves can be multiplied.


Previous: Scalar Multiplication     Next: Vector/Vector Division