35 #ifndef _BLAZE_MATH_EXPRESSIONS_SMATSMATADDEXPR_H_ 36 #define _BLAZE_MATH_EXPRESSIONS_SMATSMATADDEXPR_H_ 92 template<
typename MT1
95 :
public MatMatAddExpr< SparseMatrix< SMatSMatAddExpr<MT1,MT2>, false > >
115 static constexpr
bool returnExpr = !IsTemporary_v<RN1> && !IsTemporary_v<RN2>;
128 template<
typename T1,
typename T2,
typename T3 >
129 static constexpr
bool UseSymmetricKernel_v =
130 ( IsColumnMajorMatrix_v<T1> && IsSymmetric_v<T2> && IsSymmetric_v<T3> );
141 template<
typename MT >
142 static constexpr
bool UseSMPAssign_v = MT::smpAssignable;
211 if( i >=
lhs_.rows() ) {
214 if( j >=
lhs_.columns() ) {
226 inline size_t rows() const noexcept {
237 return lhs_.columns();
247 return lhs_.nonZeros() +
rhs_.nonZeros();
258 return lhs_.nonZeros(i) +
rhs_.nonZeros(i);
288 template<
typename T >
289 inline bool canAlias(
const T* alias )
const noexcept {
290 return (
lhs_.canAlias( alias ) ||
rhs_.canAlias( alias ) );
300 template<
typename T >
301 inline bool isAliased(
const T* alias )
const noexcept {
302 return (
lhs_.isAliased( alias ) ||
rhs_.isAliased( alias ) );
324 template<
typename MT
333 assign( ~lhs, rhs.lhs_ );
336 addAssign( ~lhs, rhs.rhs_ );
347 for(
size_t i=0UL; i<(~lhs).
rows(); ++i ) {
348 const auto end( B.end(i) );
349 for(
auto element=B.begin(i); element!=
end; ++element ) {
350 if(
isDefault( (~lhs)(i,element->index()) ) )
351 (~lhs)(i,element->index()) = element->value();
353 (~lhs)(i,element->index()) += element->value();
373 template<
typename MT >
392 (~lhs).reserve( A.nonZeros() + B.nonZeros() );
395 for(
size_t i=0UL; i<(~lhs).
rows(); ++i )
397 const auto lend( A.end(i) );
398 const auto rend( B.end(i) );
400 auto l( A.begin(i) );
401 auto r( B.begin(i) );
403 while( l != lend && r != rend )
405 if( l->index() < r->index() ) {
406 (~lhs).append( i, l->index(), l->value() );
409 else if( l->index() > r->index() ) {
410 (~lhs).append( i, r->index(), r->value() );
414 (~lhs).append( i, l->index(), l->value() + r->value() );
421 (~lhs).append( i, l->index(), l->value() );
426 (~lhs).append( i, r->index(), r->value() );
430 (~lhs).finalize( i );
448 template<
typename MT >
449 friend inline auto assign( SparseMatrix<MT,true>& lhs,
const SMatSMatAddExpr& rhs )
450 -> DisableIf_t< UseSymmetricKernel_v<MT,MT1,MT2> >
469 const size_t m( A.rows() );
470 const size_t n( A.columns() );
473 std::vector<size_t> nonzeros( n, 0UL );
474 for(
size_t i=0UL; i<m; ++i )
476 const auto lend( A.end(i) );
477 const auto rend( B.end(i) );
479 auto l( A.begin(i) );
480 auto r( B.begin(i) );
482 while( l != lend && r != rend )
484 if( l->index() < r->index() ) {
485 ++nonzeros[l->index()];
488 else if( l->index() > r->index() ) {
489 ++nonzeros[r->index()];
493 ++nonzeros[l->index()];
500 ++nonzeros[l->index()];
505 ++nonzeros[r->index()];
511 for(
size_t j=0UL; j<n; ++j ) {
512 (~lhs).reserve( j, nonzeros[j] );
516 for(
size_t i=0UL; i<m; ++i )
518 const auto lend( A.end(i) );
519 const auto rend( B.end(i) );
521 auto l( A.begin(i) );
522 auto r( B.begin(i) );
524 while( l != lend && r != rend )
526 if( l->index() < r->index() ) {
527 (~lhs).append( i, l->index(), l->value() );
530 else if( l->index() > r->index() ) {
531 (~lhs).append( i, r->index(), r->value() );
535 (~lhs).append( i, l->index(), l->value() + r->value() );
542 (~lhs).append( i, l->index(), l->value() );
547 (~lhs).append( i, r->index(), r->value() );
567 template<
typename MT >
568 friend inline auto assign( SparseMatrix<MT,true>& lhs,
const SMatSMatAddExpr& rhs )
569 -> EnableIf_t< UseSymmetricKernel_v<MT,MT1,MT2> >
578 assign( ~lhs,
trans( rhs.lhs_ ) +
trans( rhs.rhs_ ) );
595 template<
typename MT
597 friend inline void addAssign( DenseMatrix<MT,SO>& lhs,
const SMatSMatAddExpr& rhs )
604 addAssign( ~lhs, rhs.lhs_ );
605 addAssign( ~lhs, rhs.rhs_ );
626 template<
typename MT
628 friend inline void subAssign( DenseMatrix<MT,SO>& lhs,
const SMatSMatAddExpr& rhs )
635 subAssign( ~lhs, rhs.lhs_ );
636 subAssign( ~lhs, rhs.rhs_ );
657 template<
typename MT
659 friend inline void schurAssign( DenseMatrix<MT,SO>& lhs,
const SMatSMatAddExpr& rhs )
671 schurAssign( ~lhs, tmp );
710 template<
typename MT
713 -> EnableIf_t< UseSMPAssign_v<MT> >
744 template<
typename MT
747 -> EnableIf_t< UseSMPAssign_v<MT> >
779 template<
typename MT
782 -> EnableIf_t< UseSMPAssign_v<MT> >
844 template<
typename MT1
846 ,
DisableIf_t< ( ( IsZero_v<MT1> || IsZero_v<MT2> ) &&
847 IsSame_v< ElementType_t<MT1>, ElementType_t<MT2> > ) ||
848 ( IsZero_v<MT1> && IsZero_v<MT2> ) >* =
nullptr >
849 inline const SMatSMatAddExpr<MT1,MT2>
850 smatsmatadd(
const SparseMatrix<MT1,false>& lhs,
const SparseMatrix<MT2,false>& rhs )
857 return SMatSMatAddExpr<MT1,MT2>( ~lhs, ~rhs );
877 template<
typename MT1
879 , EnableIf_t< !IsZero_v<MT1> && IsZero_v<MT2> &&
880 IsSame_v< ElementType_t<MT1>, ElementType_t<MT2> > >* =
nullptr >
882 smatsmatadd(
const SparseMatrix<MT1,false>& lhs,
const SparseMatrix<MT2,false>& rhs )
911 template<
typename MT1
913 , EnableIf_t< IsZero_v<MT1> && !IsZero_v<MT2> &&
914 IsSame_v< ElementType_t<MT1>, ElementType_t<MT2> > >* =
nullptr >
916 smatsmatadd(
const SparseMatrix<MT1,false>& lhs,
const SparseMatrix<MT2,false>& rhs )
943 template<
typename MT1
945 , EnableIf_t< IsZero_v<MT1> && IsZero_v<MT2> >* =
nullptr >
946 inline decltype(
auto)
947 smatsmatadd( const SparseMatrix<MT1,false>& lhs, const SparseMatrix<MT2,false>& rhs )
956 using ReturnType =
const AddTrait_t< ResultType_t<MT1>, ResultType_t<MT2> >;
961 return ReturnType( (~lhs).
rows(), (~lhs).
columns() );
993 template<
typename MT1
995 inline decltype(
auto)
1004 return smatsmatadd( ~lhs, ~rhs );
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.This macro encapsulates the default way o...
Definition: Exception.h:235
Header file for auxiliary alias declarations.
Header file for basic type definitions.
typename If< Condition, T1, T2 >::Type If_t
Auxiliary alias template for the If class template.The If_t alias template provides a convenient shor...
Definition: If.h:109
typename T::ResultType ResultType_t
Alias declaration for nested ResultType type definitions.The ResultType_t alias declaration provides ...
Definition: Aliases.h:390
Header file for the serial shim.
const ResultType CompositeType
Data type for composite expression templates.
Definition: SMatSMatAddExpr.h:159
Header file for the IsSame and IsStrictlySame type traits.
Header file for the IsColumnMajorMatrix type trait.
CompositeType_t< MT2 > CT2
Composite type of the right-hand side sparse matrix expression.
Definition: SMatSMatAddExpr.h:103
Constraint on the data type.
CompositeType_t< MT1 > CT1
Composite type of the left-hand side sparse matrix expression.
Definition: SMatSMatAddExpr.h:102
Header file for the MAYBE_UNUSED function template.
size_t nonZeros(size_t i) const
Returns the number of non-zero elements in the specified row.
Definition: SMatSMatAddExpr.h:257
ReturnType at(size_t i, size_t j) const
Checked access to the matrix elements.
Definition: SMatSMatAddExpr.h:210
Header file for the Computation base class.
Constraints on the storage order of matrix types.
ElementType_t< ResultType > ElementType
Resulting element type.
Definition: SMatSMatAddExpr.h:153
typename T::ReturnType ReturnType_t
Alias declaration for nested ReturnType type definitions.The ReturnType_t alias declaration provides ...
Definition: Aliases.h:410
constexpr size_t columns(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of columns of the matrix.
Definition: Matrix.h:514
SMatSMatAddExpr(const MT1 &lhs, const MT2 &rhs) noexcept
Constructor for the SMatSMatAddExpr class.
Definition: SMatSMatAddExpr.h:179
Base class for dense matrices.The DenseMatrix class is a base class for all dense matrix classes....
Definition: DenseMatrix.h:81
Base class for sparse matrices.The SparseMatrix class is a base class for all sparse matrix classes....
Definition: Forward.h:145
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.The ElementType_t alias declaration provide...
Definition: Aliases.h:170
RightOperand rightOperand() const noexcept
Returns the right-hand side sparse matrix operand.
Definition: SMatSMatAddExpr.h:277
Header file for the SparseMatrix base class.
Constraint on the data type.
constexpr bool IsResizable_v
Auxiliary variable template for the IsResizable type trait.The IsResizable_v variable template provid...
Definition: IsResizable.h:133
Header file for the DisableIf class template.
Header file for the IsTemporary type trait class.
Header file for the IsSymmetric type trait.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Header file for the If class template.
#define BLAZE_CONSTRAINT_MUST_BE_ZERO_TYPE(T)
Constraint on the data type.In case the given data type T is not a zero vector or matrix type,...
Definition: Zero.h:61
ResultType_t< MT2 > RT2
Result type of the right-hand side sparse matrix expression.
Definition: SMatSMatAddExpr.h:101
ReturnType_t< MT2 > RN2
Return type of the right-hand side sparse matrix expression.
Definition: SMatSMatAddExpr.h:105
#define BLAZE_THROW_OUT_OF_RANGE(MESSAGE)
Macro for the emission of a std::out_of_range exception.This macro encapsulates the default way of Bl...
Definition: Exception.h:331
size_t nonZeros() const
Returns the number of non-zero elements in the sparse matrix.
Definition: SMatSMatAddExpr.h:246
constexpr void MAYBE_UNUSED(const Args &...)
Suppression of unused parameter warnings.
Definition: MaybeUnused.h:81
typename AddTrait< T1, T2 >::Type AddTrait_t
Auxiliary alias declaration for the AddTrait class template.The AddTrait_t alias declaration provides...
Definition: AddTrait.h:238
LeftOperand leftOperand() const noexcept
Returns the left-hand side sparse matrix operand.
Definition: SMatSMatAddExpr.h:267
RightOperand rhs_
Right-hand side sparse matrix of the addition expression.
Definition: SMatSMatAddExpr.h:309
OppositeType_t< ResultType > OppositeType
Result type with opposite storage order for expression template evaluations.
Definition: SMatSMatAddExpr.h:151
Header file for the exception macros of the math module.
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:438
#define BLAZE_CONSTRAINT_MUST_FORM_VALID_MATMATADDEXPR(T1, T2)
Constraint on the data type.In case the given data types T1 and T2 do not form a valid matrix/matrix ...
Definition: MatMatAddExpr.h:103
Constraint on the data type.
Header file for the EnableIf class template.
typename T::OppositeType OppositeType_t
Alias declaration for nested OppositeType type definitions.The OppositeType_t alias declaration provi...
Definition: Aliases.h:270
#define BLAZE_CONSTRAINT_MUST_NOT_BE_SYMMETRIC_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is a symmetric matrix type,...
Definition: Symmetric.h:79
#define BLAZE_CONSTRAINT_MUST_BE_ROW_MAJOR_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a row-major dense or sparse matrix t...
Definition: RowMajorMatrix.h:61
typename T::TransposeType TransposeType_t
Alias declaration for nested TransposeType type definitions.The TransposeType_t alias declaration pro...
Definition: Aliases.h:470
Header file for run time assertion macros.
typename T::CompositeType CompositeType_t
Alias declaration for nested CompositeType type definitions.The CompositeType_t alias declaration pro...
Definition: Aliases.h:90
Header file for the addition trait.
auto smpAddAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs) -> EnableIf_t< IsDenseMatrix_v< MT1 > >
Default implementation of the SMP addition assignment of a matrix to a dense matrix.
Definition: DenseMatrix.h:131
If_t< IsExpression_v< MT2 >, const MT2, const MT2 & > RightOperand
Composite type of the right-hand side sparse matrix expression.
Definition: SMatSMatAddExpr.h:165
LeftOperand lhs_
Left-hand side sparse matrix of the addition expression.
Definition: SMatSMatAddExpr.h:308
Constraint on the data type.
Header file for the IsZero type trait.
static constexpr bool returnExpr
Compilation switch for the selection of the subscript operator return type.
Definition: SMatSMatAddExpr.h:115
bool canAlias(const T *alias) const noexcept
Returns whether the expression can alias with the given address alias.
Definition: SMatSMatAddExpr.h:289
#define BLAZE_FUNCTION_TRACE
Function trace macro.This macro can be used to reliably trace function calls. In case function tracin...
Definition: FunctionTrace.h:94
Header file for all forward declarations for expression class templates.
static constexpr bool smpAssignable
Compilation switch for the expression template assignment strategy.
Definition: SMatSMatAddExpr.h:170
Header file for the isDefault shim.
decltype(std::declval< RN1 >()+std::declval< RN2 >()) ExprReturnType
Expression return type for the subscript operator.
Definition: SMatSMatAddExpr.h:118
Constraint on the data type.
decltype(auto) serial(const DenseMatrix< MT, SO > &dm)
Forces the serial evaluation of the given dense matrix expression dm.
Definition: DMatSerialExpr.h:808
#define BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION(T)
Constraint on the data type.In case the given data type T requires an intermediate evaluation within ...
Definition: RequiresEvaluation.h:81
Header file for the MatMatAddExpr base class.
auto smpSchurAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs) -> EnableIf_t< IsDenseMatrix_v< MT1 > >
Default implementation of the SMP Schur product assignment of a matrix to dense matrix.
Definition: DenseMatrix.h:194
constexpr size_t rows(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of rows of the matrix.
Definition: Matrix.h:498
decltype(auto) trans(const DenseMatrix< MT, SO > &dm)
Calculation of the transpose of the given dense matrix.
Definition: DMatTransExpr.h:765
size_t rows() const noexcept
Returns the current number of rows of the matrix.
Definition: SMatSMatAddExpr.h:226
Header file for the IsComputation type trait class.
If_t< IsExpression_v< MT1 >, const MT1, const MT1 & > LeftOperand
Composite type of the left-hand side sparse matrix expression.
Definition: SMatSMatAddExpr.h:162
Expression object for sparse matrix-sparse matrix additions.The SMatSMatAddExpr class represents the ...
Definition: Forward.h:130
auto smpSubAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs) -> EnableIf_t< IsDenseMatrix_v< MT1 > >
Default implementation of the SMP subtraction assignment of a matrix to dense matrix.
Definition: DenseMatrix.h:162
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: SMatSMatAddExpr.h:301
const If_t< returnExpr, ExprReturnType, ElementType > ReturnType
Return type for expression template evaluations.
Definition: SMatSMatAddExpr.h:156
TransposeType_t< ResultType > TransposeType
Transpose type for expression template evaluations.
Definition: SMatSMatAddExpr.h:152
Header file for the IntegralConstant class template.
ResultType_t< MT1 > RT1
Result type of the left-hand side sparse matrix expression.
Definition: SMatSMatAddExpr.h:100
ReturnType operator()(size_t i, size_t j) const
2D-access to the matrix elements.
Definition: SMatSMatAddExpr.h:195
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:635
typename DisableIf< Condition, T >::Type DisableIf_t
Auxiliary type for the DisableIf class template.The DisableIf_t alias declaration provides a convenie...
Definition: DisableIf.h:138
Header file for the IsResizable type trait.
ReturnType_t< MT1 > RN1
Return type of the left-hand side sparse matrix expression.
Definition: SMatSMatAddExpr.h:104
constexpr bool IsSame_v
Auxiliary variable template for the IsSame type trait.The IsSame_v variable template provides a conve...
Definition: IsSame.h:159
size_t columns() const noexcept
Returns the current number of columns of the matrix.
Definition: SMatSMatAddExpr.h:236
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression,...
Definition: Assert.h:101
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a sparse, N-dimensional matrix type,...
Definition: SparseMatrix.h:61
AddTrait_t< RT1, RT2 > ResultType
Result type for expression template evaluations.
Definition: SMatSMatAddExpr.h:150
Header file for the IsExpression type trait class.
Header file for the function trace functionality.