35#ifndef _BLAZE_MATH_EXPRESSIONS_DMATSMATKRONEXPR_H_
36#define _BLAZE_MATH_EXPRESSIONS_DMATSMATKRONEXPR_H_
92 :
public MatMatKronExpr< SparseMatrix< DMatSMatKronExpr<MT1,MT2,SO>, SO > >
112 static constexpr bool returnExpr = ( !IsTemporary_v<RN1> && !IsTemporary_v<RN2> );
199 inline size_t rows() const noexcept {
210 return lhs_.columns() *
rhs_.columns();
264 template<
typename T >
265 inline bool canAlias(
const T* alias )
const noexcept {
266 return (
lhs_.canAlias( alias ) ||
rhs_.canAlias( alias ) );
276 template<
typename T >
277 inline bool isAliased(
const T* alias )
const noexcept {
278 return (
lhs_.isAliased( alias ) ||
rhs_.isAliased( alias ) );
300 template<
typename MT
321 const size_t M( B.rows() );
322 const size_t N( B.columns() );
326 for(
size_t j=0UL; j<A.columns(); ++j )
328 const size_t ibegin( ( IsLower_v<MT1> )
329 ?( ( IsStrictlyLower_v<MT1> ? j+1UL : j ) )
331 const size_t iend( ( IsUpper_v<MT1> )
332 ?( IsStrictlyUpper_v<MT1> ? j : j+1UL )
336 for(
size_t l=0UL; l<N; ++l ) {
337 for(
size_t i=ibegin; i<iend; ++i ) {
338 if( !isDefault<strict>( A(i,j) ) ) {
339 const auto bbegin( B.begin(l) );
340 const auto bend ( B.end(l) );
341 for(
auto belem=bbegin; belem!=bend; ++belem ) {
342 (*lhs)(i*M+belem->index(),j*N+l) = A(i,j) * belem->value();
351 for(
size_t i=0UL; i<A.rows(); ++i )
353 const size_t jbegin( ( IsUpper_v<MT1> )
354 ?( ( IsStrictlyUpper_v<MT1> ? i+1UL : i ) )
356 const size_t jend( ( IsLower_v<MT1> )
357 ?( IsStrictlyLower_v<MT1> ? i : i+1UL )
361 for(
size_t k=0UL; k<M; ++k ) {
362 for(
size_t j=jbegin; j<jend; ++j ) {
363 if( !isDefault<strict>( A(i,j) ) ) {
364 const auto bbegin( B.begin(k) );
365 const auto bend ( B.end(k) );
366 for(
auto belem=bbegin; belem!=bend; ++belem ) {
367 (*lhs)(i*M+k,j*N+belem->index()) = A(i,j) * belem->value();
391 template<
typename MT >
411 const size_t M( B.rows() );
412 const size_t N( B.columns() );
417 std::vector<size_t> nonzeros( M, 0UL );
418 for(
size_t j=0UL; j<N; ++j ) {
419 const auto end( B.end(j) );
420 for(
auto elem=B.begin(j); elem!=
end; ++elem ) {
421 ++nonzeros[elem->index()];
426 for(
size_t i=0UL; i<A.rows(); ++i ) {
427 for(
size_t j=0UL; j<M; ++j ) {
428 (*lhs).reserve( i*M+j, A.columns()*nonzeros[j] );
433 for(
size_t j=0UL; j<A.columns(); ++j )
435 const size_t ibegin( ( IsLower_v<MT1> )
436 ?( ( IsStrictlyLower_v<MT1> ? j+1UL : j ) )
438 const size_t iend( ( IsUpper_v<MT1> )
439 ?( IsStrictlyUpper_v<MT1> ? j : j+1UL )
443 for(
size_t l=0UL; l<N; ++l ) {
444 for(
size_t i=ibegin; i<iend; ++i ) {
445 if( !isDefault<strict>( A(i,j) ) ) {
446 const auto end( B.end(l) );
447 for(
auto belem=B.begin(l); belem!=
end; ++belem ) {
448 (*lhs).append( i*M+belem->index(), j*N+l, A(i,j) * belem->value(),
true );
457 for(
size_t i=0UL; i<A.rows(); ++i )
459 const size_t jbegin( ( IsUpper_v<MT1> )
460 ?( ( IsStrictlyUpper_v<MT1> ? i+1UL : i ) )
462 const size_t jend( ( IsLower_v<MT1> )
463 ?( IsStrictlyLower_v<MT1> ? i : i+1UL )
467 for(
size_t k=0UL; k<M; ++k ) {
468 for(
size_t j=jbegin; j<jend; ++j ) {
469 if( !isDefault<strict>( A(i,j) ) ) {
470 const auto bend( B.end(k) );
471 for(
auto belem=B.begin(k); belem!=bend; ++belem )
472 (*lhs).append( i*M+k, j*N+belem->index(), A(i,j) * belem->value(),
true );
475 (*lhs).finalize( i*M+k );
496 template<
typename MT >
497 friend inline void assign( SparseMatrix<MT,true>& lhs,
const DMatSMatKronExpr& rhs )
504 if( rhs.rows() == 0UL || rhs.columns() == 0UL ) {
516 const size_t M( B.rows() );
517 const size_t N( B.columns() );
521 for(
size_t j=0UL; j<A.columns(); ++j )
523 const size_t ibegin( ( IsLower_v<MT1> )
524 ?( ( IsStrictlyLower_v<MT1> ? j+1UL : j ) )
526 const size_t iend( ( IsUpper_v<MT1> )
527 ?( IsStrictlyUpper_v<MT1> ? j : j+1UL )
531 for(
size_t l=0UL; l<N; ++l ) {
532 for(
size_t i=ibegin; i<iend; ++i ) {
533 if( !isDefault<strict>( A(i,j) ) ) {
534 const auto end( B.end(l) );
535 for(
auto belem=B.begin(l); belem!=
end; ++belem )
536 (*lhs).append( i*M+belem->index(), j*N+l, A(i,j) * belem->value(),
true );
539 (*lhs).finalize( j*N+l );
546 std::vector<size_t> nonzeros( N, 0UL );
547 for(
size_t i=0UL; i<M; ++i ) {
548 const auto end( B.end(i) );
549 for(
auto elem=B.begin(i); elem!=
end; ++elem ) {
550 ++nonzeros[elem->index()];
555 for(
size_t i=0UL; i<A.columns(); ++i ) {
556 for(
size_t j=0UL; j<N; ++j ) {
557 (*lhs).reserve( i*N+j, A.rows()*nonzeros[j] );
562 for(
size_t i=0UL; i<A.rows(); ++i )
564 const size_t jbegin( ( IsUpper_v<MT1> )
565 ?( ( IsStrictlyUpper_v<MT1> ? i+1UL : i ) )
567 const size_t jend( ( IsLower_v<MT1> )
568 ?( IsStrictlyLower_v<MT1> ? i : i+1UL )
572 for(
size_t k=0UL; k<M; ++k ) {
573 for(
size_t j=jbegin; j<jend; ++j ) {
574 if( !isDefault<strict>( A(i,j) ) ) {
575 const auto end( B.end(k) );
576 for(
auto belem=B.begin(k); belem!=
end; ++belem ) {
577 (*lhs).append( i*M+k, j*N+belem->index(), A(i,j) * belem->value(),
true );
600 template<
typename MT
602 friend inline void addAssign( DenseMatrix<MT,SO2>& lhs,
const DMatSMatKronExpr& rhs )
609 if( rhs.rows() == 0UL || rhs.columns() == 0UL ) {
621 const size_t M( B.rows() );
622 const size_t N( B.columns() );
626 for(
size_t j=0UL; j<A.columns(); ++j )
628 const size_t ibegin( ( IsLower_v<MT1> )
629 ?( ( IsStrictlyLower_v<MT1> ? j+1UL : j ) )
631 const size_t iend( ( IsUpper_v<MT1> )
632 ?( IsStrictlyUpper_v<MT1> ? j : j+1UL )
636 for(
size_t l=0UL; l<N; ++l ) {
637 for(
size_t i=ibegin; i<iend; ++i ) {
638 if( !isDefault<strict>( A(i,j) ) ) {
639 const auto bbegin( B.begin(l) );
640 const auto bend ( B.end(l) );
641 for(
auto belem=bbegin; belem!=bend; ++belem )
642 (*lhs)(i*M+belem->index(),j*N+l) += A(i,j) * belem->value();
650 for(
size_t i=0UL; i<A.rows(); ++i )
652 const size_t jbegin( ( IsUpper_v<MT1> )
653 ?( ( IsStrictlyUpper_v<MT1> ? i+1UL : i ) )
655 const size_t jend( ( IsLower_v<MT1> )
656 ?( IsStrictlyLower_v<MT1> ? i : i+1UL )
660 for(
size_t k=0UL; k<M; ++k ) {
661 for(
size_t j=jbegin; j<jend; ++j ) {
662 if( !isDefault<strict>( A(i,j) ) ) {
663 const auto bbegin( B.begin(k) );
664 const auto bend ( B.end(k) );
665 for(
auto belem=bbegin; belem!=bend; ++belem )
666 (*lhs)(i*M+k,j*N+belem->index()) += A(i,j) * belem->value();
693 template<
typename MT
695 friend inline void subAssign( DenseMatrix<MT,SO2>& lhs,
const DMatSMatKronExpr& rhs )
702 if( rhs.rows() == 0UL || rhs.columns() == 0UL ) {
714 const size_t M( B.rows() );
715 const size_t N( B.columns() );
719 for(
size_t j=0UL; j<A.columns(); ++j )
721 const size_t ibegin( ( IsLower_v<MT1> )
722 ?( ( IsStrictlyLower_v<MT1> ? j+1UL : j ) )
724 const size_t iend( ( IsUpper_v<MT1> )
725 ?( IsStrictlyUpper_v<MT1> ? j : j+1UL )
729 for(
size_t l=0UL; l<N; ++l ) {
730 for(
size_t i=ibegin; i<iend; ++i ) {
731 if( !isDefault<strict>( A(i,j) ) ) {
732 const auto bbegin( B.begin(l) );
733 const auto bend ( B.end(l) );
734 for(
auto belem=bbegin; belem!=bend; ++belem )
735 (*lhs)(i*M+belem->index(),j*N+l) -= A(i,j) * belem->value();
743 for(
size_t i=0UL; i<A.rows(); ++i )
745 const size_t jbegin( ( IsUpper_v<MT1> )
746 ?( ( IsStrictlyUpper_v<MT1> ? i+1UL : i ) )
748 const size_t jend( ( IsLower_v<MT1> )
749 ?( IsStrictlyLower_v<MT1> ? i : i+1UL )
753 for(
size_t k=0UL; k<M; ++k ) {
754 for(
size_t j=jbegin; j<jend; ++j ) {
755 if( !isDefault<strict>( A(i,j) ) ) {
756 const auto bbegin( B.begin(k) );
757 const auto bend ( B.end(k) );
758 for(
auto belem=bbegin; belem!=bend; ++belem )
759 (*lhs)(i*M+k,j*N+belem->index()) -= A(i,j) * belem->value();
786 template<
typename MT
788 friend inline void schurAssign( DenseMatrix<MT,SO2>& lhs,
const DMatSMatKronExpr& rhs )
795 if( rhs.rows() == 0UL || rhs.columns() == 0UL ) {
807 const size_t M( B.rows() );
808 const size_t N( B.columns() );
812 for(
size_t j=0UL; j<A.columns(); ++j ) {
813 for(
size_t l=0UL; l<N; ++l )
815 const auto bbegin( B.begin(l) );
816 const auto bend ( B.end(l) );
818 for(
size_t i=0UL; i<A.rows(); ++i )
822 if( !isDefault<strict>( A(i,j) ) ) {
823 for(
auto belem=bbegin; belem!=bend; ++belem, ++k ) {
824 for( ; k<belem->index(); ++k )
825 reset( (*lhs)(i*M+k,j*N+l) );
826 (*lhs)(i*M+k,j*N+l) *= A(i,j) * belem->value();
831 reset( (*lhs)(i*M+k,j*N+l) );
839 for(
size_t i=0UL; i<A.rows(); ++i ) {
840 for(
size_t k=0UL; k<M; ++k )
842 const auto bbegin( B.begin(k) );
843 const auto bend ( B.end(k) );
845 for(
size_t j=0UL; j<A.columns(); ++j )
849 if( !isDefault<strict>( A(i,j) ) ) {
850 for(
auto belem=bbegin; belem!=bend; ++belem, ++l ) {
851 for( ; l<belem->index(); ++l )
852 reset( (*lhs)(i*M+k,j*N+l) );
853 (*lhs)(i*M+k,j*N+l) *= A(i,j) * belem->value();
858 reset( (*lhs)(i*M+k,j*N+l) );
914template<
typename MT1
918 , DisableIf_t< IsZero_v<MT2> >* =
nullptr >
919inline const DMatSMatKronExpr<MT1,MT2,SO2>
920 dmatsmatkron(
const DenseMatrix<MT1,SO1>& lhs,
const SparseMatrix<MT2,SO2>& rhs )
924 return DMatSMatKronExpr<MT1,MT2,SO2>( *lhs, *rhs );
943template<
typename MT1
947 , EnableIf_t< IsZero_v<MT2> >* =
nullptr >
949 dmatsmatkron(
const DenseMatrix<MT1,SO1>& lhs,
const SparseMatrix<MT2,SO2>& rhs )
953 using ReturnType =
const KronTrait_t< ResultType_t<MT1>, ResultType_t<MT2> >;
958 return ReturnType( (*lhs).rows()*(*rhs).rows(), (*lhs).columns()*(*rhs).columns() );
986template<
typename MT1
995 return dmatsmatkron( *lhs, *rhs );
Header file for auxiliary alias declarations.
typename T::CompositeType CompositeType_t
Alias declaration for nested CompositeType type definitions.
Definition: Aliases.h:110
typename T::ReturnType ReturnType_t
Alias declaration for nested ReturnType type definitions.
Definition: Aliases.h:470
typename T::ResultType ResultType_t
Alias declaration for nested ResultType type definitions.
Definition: Aliases.h:450
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.
Definition: Aliases.h:190
typename T::OppositeType OppositeType_t
Alias declaration for nested OppositeType type definitions.
Definition: Aliases.h:310
typename T::TransposeType TransposeType_t
Alias declaration for nested TransposeType type definitions.
Definition: Aliases.h:550
Header file for run time assertion macros.
Header file for the EnableIf class template.
Header file for the function trace functionality.
Header file for the If class template.
Header file for the isDefault shim.
Header file for the IsExpression type trait class.
Header file for the IsLower type trait.
Header file for the IsStrictlyLower type trait.
Header file for the IsStrictlyUpper type trait.
Header file for the IsTemporary type trait class.
Header file for the IsUpper type trait.
Header file for the Kron product trait.
Constraint on the data type.
Expression object for dense matrix-sparse matrix Kronecker product.
Definition: DMatSMatKronExpr.h:94
LeftOperand leftOperand() const noexcept
Returns the left-hand side dense matrix operand.
Definition: DMatSMatKronExpr.h:243
RightOperand rhs_
Right-hand side sparse matrix of the Kronecker product expression.
Definition: DMatSMatKronExpr.h:285
ReturnType at(size_t i, size_t j) const
Checked access to the matrix elements.
Definition: DMatSMatKronExpr.h:183
ReturnType operator()(size_t i, size_t j) const
2D-access to the matrix elements.
Definition: DMatSMatKronExpr.h:168
LeftOperand lhs_
Left-hand side dense matrix of the Kronecker product expression.
Definition: DMatSMatKronExpr.h:284
size_t rows() const noexcept
Returns the current number of rows of the matrix.
Definition: DMatSMatKronExpr.h:199
DMatSMatKronExpr(const MT1 &lhs, const MT2 &rhs) noexcept
Constructor for the DMatSMatKronExpr class.
Definition: DMatSMatKronExpr.h:155
size_t columns() const noexcept
Returns the current number of columns of the matrix.
Definition: DMatSMatKronExpr.h:209
If_t< IsExpression_v< MT1 >, const MT1, const MT1 & > LeftOperand
Composite type of the left-hand side dense matrix expression.
Definition: DMatSMatKronExpr.h:138
If_t< IsExpression_v< MT2 >, const MT2, const MT2 & > RightOperand
Composite type of the right-hand side sparse matrix expression.
Definition: DMatSMatKronExpr.h:141
size_t nonZeros(size_t i) const
Returns the number of non-zero elements in the specified row.
Definition: DMatSMatKronExpr.h:230
TransposeType_t< ResultType > TransposeType
Transpose type for expression template evaluations.
Definition: DMatSMatKronExpr.h:128
CompositeType_t< MT1 > CT1
Composite type of the left-hand side dense matrix expression.
Definition: DMatSMatKronExpr.h:101
KronTrait_t< RT1, RT2 > ResultType
Result type for expression template evaluations.
Definition: DMatSMatKronExpr.h:126
ElementType_t< ResultType > ElementType
Resulting element type.
Definition: DMatSMatKronExpr.h:129
ResultType_t< MT1 > RT1
Result type of the left-hand side dense matrix expression.
Definition: DMatSMatKronExpr.h:97
RightOperand rightOperand() const noexcept
Returns the right-hand side sparse matrix operand.
Definition: DMatSMatKronExpr.h:253
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: DMatSMatKronExpr.h:277
static constexpr bool returnExpr
Compilation switch for the selection of the subscript operator return type.
Definition: DMatSMatKronExpr.h:112
size_t nonZeros() const
Returns the number of non-zero elements in the sparse matrix.
Definition: DMatSMatKronExpr.h:219
bool canAlias(const T *alias) const noexcept
Returns whether the expression can alias with the given address alias.
Definition: DMatSMatKronExpr.h:265
OppositeType_t< ResultType > OppositeType
Result type with opposite storage order for expression template evaluations.
Definition: DMatSMatKronExpr.h:127
const If_t< returnExpr, ExprReturnType, ElementType > ReturnType
Return type for expression template evaluations.
Definition: DMatSMatKronExpr.h:132
const ResultType CompositeType
Data type for composite expression templates.
Definition: DMatSMatKronExpr.h:135
ReturnType_t< MT1 > RN1
Return type of the left-hand side dense matrix expression.
Definition: DMatSMatKronExpr.h:99
CompositeType_t< MT2 > CT2
Composite type of the right-hand side sparse matrix expression.
Definition: DMatSMatKronExpr.h:102
ReturnType_t< MT2 > RN2
Return type of the right-hand side sparse matrix expression.
Definition: DMatSMatKronExpr.h:100
decltype(std::declval< RN1 >() *std::declval< RN2 >()) ExprReturnType
Expression return type for the subscript operator.
Definition: DMatSMatKronExpr.h:115
ResultType_t< MT2 > RT2
Result type of the right-hand side sparse matrix expression.
Definition: DMatSMatKronExpr.h:98
static constexpr bool smpAssignable
Compilation switch for the expression template assignment strategy.
Definition: DMatSMatKronExpr.h:146
Base class for dense matrices.
Definition: DenseMatrix.h:82
Base class for sparse matrices.
Definition: SparseMatrix.h:77
Constraint on the data type.
Constraint on the data type.
Constraint on the data type.
Header file for the Computation base class.
Header file for the DenseMatrix base class.
Header file for the MatMatKronExpr base class.
Header file for the SparseMatrix base class.
decltype(auto) serial(const DenseMatrix< MT, SO > &dm)
Forces the serial evaluation of the given dense matrix expression dm.
Definition: DMatSerialExpr.h:812
#define BLAZE_CONSTRAINT_MUST_NOT_BE_ZERO_TYPE(T)
Constraint on the data type.
Definition: Zero.h:81
#define BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE(T)
Constraint on the data type.
Definition: DenseMatrix.h:61
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_MATRIX_TYPE(T)
Constraint on the data type.
Definition: SparseMatrix.h:61
#define BLAZE_CONSTRAINT_MUST_FORM_VALID_MATMATKRONEXPR(T1, T2)
Constraint on the data type.
Definition: MatMatKronExpr.h:102
#define BLAZE_CONSTRAINT_MUST_BE_ZERO_TYPE(T)
Constraint on the data type.
Definition: Zero.h:61
#define BLAZE_CONSTRAINT_MUST_BE_MATRIX_WITH_STORAGE_ORDER(T, SO)
Constraint on the data type.
Definition: StorageOrder.h:63
typename KronTrait< T1, T2 >::Type KronTrait_t
Auxiliary alias declaration for the KronTrait class template.
Definition: KronTrait.h:137
constexpr void reset(Matrix< MT, SO > &matrix)
Resetting the given matrix.
Definition: Matrix.h:806
MT::Iterator end(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator just past the last element of row/column i.
Definition: Matrix.h:584
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.
Definition: Assert.h:101
decltype(auto) kron(const DenseMatrix< MT1, SO1 > &lhs, const SparseMatrix< MT2, SO2 > &rhs)
Computes the Kronecker product of a dense matrix and a sparse matrix ( ).
Definition: DMatSMatKronExpr.h:991
typename If< Condition >::template Type< T1, T2 > If_t
Auxiliary alias template for the If class template.
Definition: If.h:108
#define BLAZE_THROW_OUT_OF_RANGE(MESSAGE)
Macro for the emission of a std::out_of_range exception.
Definition: Exception.h:331
#define BLAZE_FUNCTION_TRACE
Function trace macro.
Definition: FunctionTrace.h:94
Header file for the exception macros of the math module.
Constraints on the storage order of matrix types.
Header file for the reset shim.
Header file for the serial shim.
Base class for all compute expression templates.
Definition: Computation.h:68
Base class for all matrix/matrix Kronecker expression templates.
Definition: MatMatKronExpr.h:69
Header file for the IsZero type trait.
Header file for basic type definitions.