Blaze 3.9
SMatRepeatExpr.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_EXPRESSIONS_SMATREPEATEXPR_H_
36#define _BLAZE_MATH_EXPRESSIONS_SMATREPEATEXPR_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
43#include <blaze/math/Aliases.h>
60#include <blaze/util/mpl/If.h>
61#include <blaze/util/Types.h>
63
64
65namespace blaze {
66
67//=================================================================================================
68//
69// CLASS SMATREPEATEXPR
70//
71//=================================================================================================
72
73//*************************************************************************************************
80template< typename MT // Type of the sparse matrix
81 , bool SO // Storage order
82 , size_t... CRAs > // Compile time repeater arguments
84 : public MatRepeatExpr< SparseMatrix< SMatRepeatExpr<MT,SO,CRAs...>, SO >, CRAs... >
85 , private If_t< IsComputation_v<MT>, Computation, Transformation >
86 , private RepeatExprData<2UL,CRAs...>
87{
88 private:
89 //**Type definitions****************************************************************************
90 using DataType = RepeatExprData<2UL,CRAs...>;
91 //**********************************************************************************************
92
93 public:
94 //**Type definitions****************************************************************************
96 using This = SMatRepeatExpr<MT,SO,CRAs...>;
97
100
101 using ResultType = RepeatTrait_t<MT,CRAs...>;
106 using CompositeType = const ResultType;
107
109 using Operand = If_t< IsExpression_v<MT>, const MT, const MT& >;
110 //**********************************************************************************************
111
112 public:
113 //**Compilation flags***************************************************************************
115 static constexpr bool smpAssignable = false;
116 //**********************************************************************************************
117
118 //**Constructor*********************************************************************************
124 template< typename... RRAs > // Runtime repeater arguments
125 explicit inline SMatRepeatExpr( const MT& sm, RRAs... args ) noexcept
126 : DataType( args... ) // Base class initialization
127 , sm_ ( sm ) // Sparse matrix of the repeater expression
128 {}
129 //**********************************************************************************************
130
131 //**Access operator*****************************************************************************
138 inline ReturnType operator()( size_t i, size_t j ) const {
139 BLAZE_INTERNAL_ASSERT( i < rows() , "Invalid row access index" );
140 BLAZE_INTERNAL_ASSERT( j < columns(), "Invalid column access index" );
141 return sm_( i%sm_.rows(), j%sm_.columns() );
142 }
143 //**********************************************************************************************
144
145 //**At function*********************************************************************************
153 inline ReturnType at( size_t i, size_t j ) const {
154 if( i >= rows() ) {
155 BLAZE_THROW_OUT_OF_RANGE( "Invalid row access index" );
156 }
157 if( j >= columns() ) {
158 BLAZE_THROW_OUT_OF_RANGE( "Invalid column access index" );
159 }
160 return (*this)(i,j);
161 }
162 //**********************************************************************************************
163
164 //**Rows function*******************************************************************************
169 inline size_t rows() const noexcept {
170 return sm_.rows() * this->template repetitions<0UL>();
171 }
172 //**********************************************************************************************
173
174 //**Columns function****************************************************************************
179 inline size_t columns() const noexcept {
180 return sm_.columns() * this->template repetitions<1UL>();
181 }
182 //**********************************************************************************************
183
184 //**NonZeros function***************************************************************************
189 inline size_t nonZeros() const {
190 return sm_.nonZeros() * this->template repetitions<0UL>() * this->template repetitions<1UL>();
191 }
192 //**********************************************************************************************
193
194 //**NonZeros function***************************************************************************
200 inline size_t nonZeros( size_t i ) const {
201 if( SO )
202 return sm_.nonZeros(i) * this->template repetitions<0UL>();
203 else
204 return sm_.nonZeros(i) * this->template repetitions<1UL>();
205 }
206 //**********************************************************************************************
207
208 //**Operand access******************************************************************************
213 inline Operand operand() const noexcept {
214 return sm_;
215 }
216 //**********************************************************************************************
217
218 //**********************************************************************************************
219 using DataType::repetitions;
220 //**********************************************************************************************
221
222 //**********************************************************************************************
228 template< typename T >
229 inline bool canAlias( const T* alias ) const noexcept {
230 return IsExpression_v<MT> && sm_.canAlias( alias );
231 }
232 //**********************************************************************************************
233
234 //**********************************************************************************************
240 template< typename T >
241 inline bool isAliased( const T* alias ) const noexcept {
242 return sm_.isAliased( alias );
243 }
244 //**********************************************************************************************
245
246 //**********************************************************************************************
251 inline bool canSMPAssign() const noexcept {
252 return false;
253 }
254 //**********************************************************************************************
255
256 private:
257 //**Member variables****************************************************************************
259 size_t reps_;
260 //**********************************************************************************************
261
262 //**Assignment to row-major dense matrices******************************************************
274 template< typename MT2 // Type of the target dense matrix
275 , bool SO2 > // Storage order of the target dense matrix
276 friend inline void assign( DenseMatrix<MT2,SO2>& lhs, const SMatRepeatExpr& rhs )
277 {
279
280 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
281 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
282
283 CompositeType_t<MT> A( serial( rhs.sm_ ) ); // Evaluation of the sparse matrix operand
284
285 const size_t reps0( rhs.template repetitions<0UL>() );
286 const size_t reps1( rhs.template repetitions<1UL>() );
287 const size_t M( A.rows() );
288 const size_t N( A.columns() );
289
290 for( size_t rep0=0UL; rep0<reps0; ++rep0 ) {
291 for( size_t rep1=0UL; rep1<reps1; ++rep1 ) {
292 submatrix( *lhs, rep0*M, rep1*N, M, N, unchecked ) = serial( A );
293 }
294 }
295 }
297 //**********************************************************************************************
298
299 //**Assignment to row-major sparse matrices*****************************************************
311 template< typename MT2 // Type of the target sparse matrix
312 , bool SO2 > // Storage order of the target sparse matrix
313 friend inline void assign( SparseMatrix<MT2,SO2>& lhs, const SMatRepeatExpr& rhs )
314 {
316
318
325
326 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
327 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
328
329 TmpType A( serial( rhs.sm_ ) ); // Evaluation of the sparse matrix operand
330
331 const size_t reps0( rhs.template repetitions<0UL>() );
332 const size_t reps1( rhs.template repetitions<1UL>() );
333 const size_t M( A.rows() );
334 const size_t N( A.columns() );
335
336 (*lhs).reserve( reps0*reps1*M*N );
337
338 if( SO2 == rowMajor )
339 {
340 for( size_t rep0=0UL; rep0<reps0; ++rep0 ) {
341 for( size_t i=0UL; i<M; ++i ) {
342 for( size_t rep1=0UL; rep1<reps1; ++rep1 ) {
343 for( auto element=A.begin(i); element!=A.end(i); ++element ) {
344 (*lhs).append( rep0*M+i, rep1*N+element->index(), element->value(), true );
345 }
346 }
347 (*lhs).finalize( rep0*M+i );
348 }
349 }
350 }
351 else
352 {
353 for( size_t rep1=0UL; rep1<reps1; ++rep1 ) {
354 for( size_t j=0UL; j<N; ++j ) {
355 for( size_t rep0=0UL; rep0<reps0; ++rep0 ) {
356 for( auto element=A.begin(j); element!=A.end(j); ++element ) {
357 (*lhs).append( rep0*M+element->index(), rep1*N+j, element->value(), true );
358 }
359 }
360 (*lhs).finalize( rep1*N+j );
361 }
362 }
363 }
364 }
366 //**********************************************************************************************
367
368 //**Addition assignment to row-major dense matrices*********************************************
380 template< typename MT2 // Type of the target dense matrix
381 , bool SO2 > // Storage order of the target dense matrix
382 friend inline void addAssign( DenseMatrix<MT2,SO2>& lhs, const SMatRepeatExpr& rhs )
383 {
385
386 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
387 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
388
389 CompositeType_t<MT> A( serial( rhs.sm_ ) ); // Evaluation of the sparse matrix operand
390
391 const size_t reps0( rhs.template repetitions<0UL>() );
392 const size_t reps1( rhs.template repetitions<1UL>() );
393 const size_t M( A.rows() );
394 const size_t N( A.columns() );
395
396 for( size_t rep0=0UL; rep0<reps0; ++rep0 ) {
397 for( size_t rep1=0UL; rep1<reps1; ++rep1 ) {
398 submatrix( *lhs, rep0*M, rep1*N, M, N, unchecked ) += serial( A );
399 }
400 }
401 }
403 //**********************************************************************************************
404
405 //**Addition assignment to sparse matrices******************************************************
406 // No special implementation for the addition assignment to sparse matrices.
407 //**********************************************************************************************
408
409 //**Subtraction assignment to row-major dense matrices******************************************
421 template< typename MT2 // Type of the target dense matrix
422 , bool SO2 > // Storage order of the target dense matrix
423 friend inline void subAssign( DenseMatrix<MT2,SO2>& lhs, const SMatRepeatExpr& rhs )
424 {
426
427 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
428 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
429
430 CompositeType_t<MT> A( serial( rhs.sm_ ) ); // Evaluation of the sparse matrix operand
431
432 const size_t reps0( rhs.template repetitions<0UL>() );
433 const size_t reps1( rhs.template repetitions<1UL>() );
434 const size_t M( A.rows() );
435 const size_t N( A.columns() );
436
437 for( size_t rep0=0UL; rep0<reps0; ++rep0 ) {
438 for( size_t rep1=0UL; rep1<reps1; ++rep1 ) {
439 submatrix( *lhs, rep0*M, rep1*N, M, N, unchecked ) -= serial( A );
440 }
441 }
442 }
444 //**********************************************************************************************
445
446 //**Subtraction assignment to sparse matrices***************************************************
447 // No special implementation for the subtraction assignment to sparse matrices.
448 //**********************************************************************************************
449
450 //**Schur product assignment to row-major dense matrices****************************************
462 template< typename MT2 // Type of the target dense matrix
463 , bool SO2 > // Storage order of the target dense matrix
464 friend inline void schurAssign( DenseMatrix<MT2,SO2>& lhs, const SMatRepeatExpr& rhs )
465 {
467
468 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
469 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
470
471 CompositeType_t<MT> A( serial( rhs.sm_ ) ); // Evaluation of the sparse matrix operand
472
473 const size_t reps0( rhs.template repetitions<0UL>() );
474 const size_t reps1( rhs.template repetitions<1UL>() );
475 const size_t M( A.rows() );
476 const size_t N( A.columns() );
477
478 for( size_t rep0=0UL; rep0<reps0; ++rep0 ) {
479 for( size_t rep1=0UL; rep1<reps1; ++rep1 ) {
480 submatrix( *lhs, rep0*M, rep1*N, M, N, unchecked ) %= serial( A );
481 }
482 }
483 }
485 //**********************************************************************************************
486
487 //**Schur product assignment to sparse matrices*************************************************
488 // No special implementation for the Schur product assignment to sparse matrices.
489 //**********************************************************************************************
490
491 //**Multiplication assignment to dense matrices*************************************************
492 // No special implementation for the multiplication assignment to dense matrices.
493 //**********************************************************************************************
494
495 //**Multiplication assignment to sparse matrices************************************************
496 // No special implementation for the multiplication assignment to sparse matrices.
497 //**********************************************************************************************
498
499 //**Compile time checks*************************************************************************
504 //**********************************************************************************************
505};
506//*************************************************************************************************
507
508
509
510
511//=================================================================================================
512//
513// GLOBAL FUNCTIONS
514//
515//=================================================================================================
516
517//*************************************************************************************************
561template< typename MT // Type of the sparse matrix
562 , bool SO > // Storage order
563inline decltype(auto) repeat( const SparseMatrix<MT,SO>& sm, size_t m, size_t n )
564{
566
567 using ReturnType = const SMatRepeatExpr<MT,SO>;
568 return ReturnType( *sm, m, n );
569}
570//*************************************************************************************************
571
572
573//*************************************************************************************************
615template< size_t R0 // Compile time row-wise repetitions
616 , size_t R1 // Compile time column-wise repetitions
617 , typename MT // Type of the sparse matrix
618 , bool SO > // Storage order
619inline decltype(auto) repeat( const SparseMatrix<MT,SO>& sm )
620{
622
623 using ReturnType = const SMatRepeatExpr<MT,SO,R0,R1>;
624 return ReturnType( *sm );
625}
626//*************************************************************************************************
627
628
629//*************************************************************************************************
642template< size_t R0 // Compile time row-wise repetitions
643 , size_t R1 // Compile time column-wise repetitions
644 , typename MT // Type of the sparse matrix
645 , bool SO > // Storage order
646inline decltype(auto) repeat( const SparseMatrix<MT,SO>& sm, size_t m, size_t n )
647{
648 MAYBE_UNUSED( m, n );
649
651
652 using ReturnType = const SMatRepeatExpr<MT,SO,R0,R1>;
653 return ReturnType( *sm );
654}
656//*************************************************************************************************
657
658} // namespace blaze
659
660#endif
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 the blaze::checked and blaze::unchecked instances.
Header file for the function trace functionality.
Header file for the If class template.
Header file for the IsExpression type trait class.
Header file for the RemoveReference type trait.
Header file for the implementation of the RepeatExprData class template.
Header file for the repeat trait.
Base class for dense matrices.
Definition: DenseMatrix.h:82
Auxiliary class template for the data members of repeater expression classes.
Definition: RepeatExprData.h:65
Expression object for the sparse matrix repeat() function.
Definition: SMatRepeatExpr.h:87
RepeatExprData< 2UL, CRAs... > DataType
The type of the RepeatExprData base class.
Definition: SMatRepeatExpr.h:90
bool canAlias(const T *alias) const noexcept
Returns whether the expression can alias with the given address alias.
Definition: SMatRepeatExpr.h:229
static constexpr bool smpAssignable
Compilation switch for the expression template assignment strategy.
Definition: SMatRepeatExpr.h:115
TransposeType_t< ResultType > TransposeType
Transpose type for expression template evaluations.
Definition: SMatRepeatExpr.h:103
Operand operand() const noexcept
Returns the sparse matrix operand.
Definition: SMatRepeatExpr.h:213
bool canSMPAssign() const noexcept
Returns whether the expression can be used in SMP assignments.
Definition: SMatRepeatExpr.h:251
Operand sm_
Sparse matrix of the repeater expression.
Definition: SMatRepeatExpr.h:258
RepeatTrait_t< MT, CRAs... > ResultType
Result type for expression template evaluations.
Definition: SMatRepeatExpr.h:101
const ResultType CompositeType
Data type for composite expression templates.
Definition: SMatRepeatExpr.h:106
size_t nonZeros(size_t i) const
Returns the number of non-zero elements in the specified row.
Definition: SMatRepeatExpr.h:200
size_t columns() const noexcept
Returns the current number of columns of the matrix.
Definition: SMatRepeatExpr.h:179
ElementType_t< MT > ElementType
Resulting element type.
Definition: SMatRepeatExpr.h:104
size_t reps_
The number of repetitions.
Definition: SMatRepeatExpr.h:259
ReturnType_t< MT > ReturnType
Return type for expression template evaluations.
Definition: SMatRepeatExpr.h:105
If_t< IsExpression_v< MT >, const MT, const MT & > Operand
Composite data type of the sparse matrix expression.
Definition: SMatRepeatExpr.h:109
ReturnType operator()(size_t i, size_t j) const
2D-access to the matrix elements.
Definition: SMatRepeatExpr.h:138
ReturnType at(size_t i, size_t j) const
Checked access to the matrix elements.
Definition: SMatRepeatExpr.h:153
size_t rows() const noexcept
Returns the current number of rows of the matrix.
Definition: SMatRepeatExpr.h:169
SMatRepeatExpr(const MT &sm, RRAs... args) noexcept
Constructor for the SMatRepeatExpr class.
Definition: SMatRepeatExpr.h:125
size_t nonZeros() const
Returns the number of non-zero elements in the sparse matrix.
Definition: SMatRepeatExpr.h:189
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: SMatRepeatExpr.h:241
OppositeType_t< ResultType > OppositeType
Result type with opposite storage order for expression template evaluations.
Definition: SMatRepeatExpr.h:102
Base class for sparse matrices.
Definition: SparseMatrix.h:77
Constraint on the data type.
Constraint on the data type.
Header file for the Computation base class.
Header file for the MatRepeatExpr base class.
Header file for the SparseMatrix base class.
Header file for the Transformation base class.
decltype(auto) repeat(const DenseMatrix< MT, SO > &dm, size_t m, size_t n)
Repeats the given dense matrix.
Definition: DMatRepeatExpr.h:543
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_MATRICES_MUST_HAVE_SAME_STORAGE_ORDER(T1, T2)
Constraint on the data type.
Definition: StorageOrder.h:84
#define BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION(T)
Constraint on the data type.
Definition: RequiresEvaluation.h:81
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_MATRIX_TYPE(T)
Constraint on the data type.
Definition: SparseMatrix.h:61
#define BLAZE_CONSTRAINT_MUST_BE_MATRIX_WITH_STORAGE_ORDER(T, SO)
Constraint on the data type.
Definition: StorageOrder.h:63
typename RepeatTrait< T, CRAs... >::Type RepeatTrait_t
Auxiliary alias declaration for the RepeatTrait type trait.
Definition: RepeatTrait.h:151
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.
Definition: Assert.h:101
decltype(auto) submatrix(Matrix< MT, SO > &, RSAs...)
Creating a view on a specific submatrix of the given matrix.
Definition: Submatrix.h:181
typename RemoveReference< T >::Type RemoveReference_t
Auxiliary alias declaration for the RemoveReference type trait.
Definition: RemoveReference.h:95
constexpr void MAYBE_UNUSED(const Args &...)
Suppression of unused parameter warnings.
Definition: MaybeUnused.h:81
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
constexpr Unchecked unchecked
Global Unchecked instance.
Definition: Check.h:146
Header file for the exception macros of the math module.
Constraints on the storage order of matrix types.
Header file for all forward declarations for expression class templates.
Header file for the serial shim.
Base class for all matrix repeat expression templates.
Definition: MatRepeatExpr.h:69
Header file for basic type definitions.
Submatrix specialization for dense matrices.