Blaze 3.9
Matrix/Matrix Multiplication


Componentwise Multiplication / Schur Product


Multiplying two matrices with the same dimensions (i.e. the same number of rows and columns) via the modulo operator results in a componentwise multiplication (Schur product) of the two matrices:

DynamicMatrix<double> M1( 28UL, 35UL );
CompressedMatrix<float> M2( 28UL, 35UL );
// ... Initialization of the matrices
DynamicMatrix<double> M3 = M1 % M2; // Schur product
DynamicMatrix<double> M4 = schur( M1, M2 ); // Alternative syntax
Efficient implementation of a compressed matrix.
Definition: CompressedMatrix.h:239
Efficient implementation of a dynamic matrix.
Definition: DynamicMatrix.h:242
decltype(auto) schur(const Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Schur product of two matrices ( ).
Definition: Matrix.h:768

Both dense and sparse matrices can be used for a Schur product. The storage order of the two matrices poses no restrictions on the operation, all variations are possible. It is also possible to multiply two matrices with different element type, as long as the element types themselves can be multiplied.


Matrix Product


The matrix/matrix product can be formulated exactly as in mathematical textbooks:

DynamicMatrix<double> M1( 45UL, 85UL );
CompressedMatrix<float> M2( 85UL, 37UL );
// ... Initialization of the matrices
DynamicMatrix<double> M3 = M1 * M2; // Matrix product
DynamicMatrix<double> M4 = mult( M1, M2 ); // Alternative syntax
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

The storage order of the two matrices poses no restrictions on the operation, all variations are possible. It is also possible to multiply two matrices with different element type, as long as the element types themselves can be multiplied and added. Note however that the highest performance for a multiplication between two matrices can be expected for two matrices with the same scalar element type.

In case the resulting matrix is known to be symmetric, Hermitian, lower triangular, upper triangular, or diagonal, the computation can be optimized by explicitly declaring the multiplication as symmetric, Hermitian, lower triangular, upper triangular, or diagonal by means of the Declaration Operations :

DynamicMatrix<double> M1, M2, M3;
// ... Initialization of the square matrices
M3 = declsym ( M1 * M2 ); // Declare the result of the matrix multiplication as symmetric
M3 = declherm( M1 * M2 ); // Declare the result of the matrix multiplication as Hermitian
M3 = decllow ( M1 * M2 ); // Declare the result of the matrix multiplication as lower triangular
M3 = declupp ( M1 * M2 ); // Declare the result of the matrix multiplication as upper triangular
M3 = decldiag( M1 * M2 ); // Declare the result of the matrix multiplication as diagonal
decltype(auto) decldiag(const DenseMatrix< MT, SO > &dm)
Declares the given dense matrix expression dm as diagonal.
Definition: DMatDeclDiagExpr.h:978
decltype(auto) declupp(const DenseMatrix< MT, SO > &dm)
Declares the given dense matrix expression dm as upper.
Definition: DMatDeclUppExpr.h:1004
decltype(auto) decllow(const DenseMatrix< MT, SO > &dm)
Declares the given dense matrix expression dm as lower.
Definition: DMatDeclLowExpr.h:1004
decltype(auto) declherm(const DenseMatrix< MT, SO > &dm)
Declares the given dense matrix expression dm as Hermitian.
Definition: DMatDeclHermExpr.h:1005
decltype(auto) declsym(const DenseMatrix< MT, SO > &dm)
Declares the given dense matrix expression dm as symmetric.
Definition: DMatDeclSymExpr.h:1005

Using a declaration operation on the a multiplication expression can speed up the computation by a factor of 2. Note however that the caller of the according declaration operation takes full responsibility for the correctness of the declaration. Falsely declaring a multiplication as symmetric, Hermitian, lower triangular, upper triangular, or diagonal leads to undefined behavior!


Kronecker Product


The Kronecker product of two matrices can be computed via the kron() function:

DynamicMatrix<double> M1( 28UL, 35UL );
CompressedMatrix<float> M2( 17UL, 11UL );
// ... Initialization of the matrices
CompressedMatrix<double> M3 = kron( M1, M2 );
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 matrices can be used for a Kronecker product. The storage order of the two matrices poses no restrictions on the operation, all variations are possible. It is also possible to multiply two matrices with different element type, as long as the element types themselves can be multiplied.


Previous: Matrix/Vector Multiplication     Next: Bitwise Operations