Blaze 3.9
SVecRepeatExpr.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_EXPRESSIONS_SVECREPEATEXPR_H_
36#define _BLAZE_MATH_EXPRESSIONS_SVECREPEATEXPR_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
43#include <blaze/math/Aliases.h>
59#include <blaze/util/mpl/If.h>
60#include <blaze/util/Types.h>
61
62
63namespace blaze {
64
65//=================================================================================================
66//
67// CLASS SVECREPEATEXPR
68//
69//=================================================================================================
70
71//*************************************************************************************************
78template< typename VT // Type of the sparse vector
79 , bool TF // Transpose flag
80 , size_t... CRAs > // Compile time repeater arguments
82 : public VecRepeatExpr< SparseVector< SVecRepeatExpr<VT,TF,CRAs...>, TF >, CRAs... >
83 , private If_t< IsComputation_v<VT>, Computation, Transformation >
84 , private RepeatExprData<1UL,CRAs...>
85{
86 private:
87 //**Type definitions****************************************************************************
88 using DataType = RepeatExprData<1UL,CRAs...>;
89 //**********************************************************************************************
90
91 public:
92 //**Type definitions****************************************************************************
94 using This = SVecRepeatExpr<VT,TF,CRAs...>;
95
98
99 using ResultType = RepeatTrait_t<VT,CRAs...>;
103 using CompositeType = const ResultType;
104
106 using Operand = If_t< IsExpression_v<VT>, const VT, const VT& >;
107 //**********************************************************************************************
108
109 public:
110 //**Compilation flags***************************************************************************
112 static constexpr bool smpAssignable = false;
113 //**********************************************************************************************
114
115 //**Constructor*********************************************************************************
121 template< typename... RRAs > // Runtime repeater arguments
122 explicit inline SVecRepeatExpr( const VT& sv, RRAs... args ) noexcept
123 : DataType( args... ) // Base class initialization
124 , sv_ ( sv ) // Sparse vector of the repeater expression
125 {}
126 //**********************************************************************************************
127
128 //**Subscript operator**************************************************************************
134 inline ReturnType operator[]( size_t index ) const {
135 BLAZE_INTERNAL_ASSERT( index < size(), "Invalid vector access index" );
136 return sv_[index%sv_.size()];
137 }
138 //**********************************************************************************************
139
140 //**At function*********************************************************************************
147 inline ReturnType at( size_t index ) const {
148 if( index >= size() ) {
149 BLAZE_THROW_OUT_OF_RANGE( "Invalid vector access index" );
150 }
151 return (*this)[index];
152 }
153 //**********************************************************************************************
154
155 //**Size function*******************************************************************************
160 inline size_t size() const noexcept {
161 return sv_.size() * this->template repetitions<0UL>();
162 }
163 //**********************************************************************************************
164
165 //**NonZeros function***************************************************************************
170 inline size_t nonZeros() const {
171 return sv_.nonZeros() * this->template repetitions<0UL>();
172 }
173 //**********************************************************************************************
174
175 //**Operand access******************************************************************************
180 inline Operand operand() const noexcept {
181 return sv_;
182 }
183 //**********************************************************************************************
184
185 //**********************************************************************************************
186 using DataType::repetitions;
187 //**********************************************************************************************
188
189 //**********************************************************************************************
195 template< typename T >
196 inline bool canAlias( const T* alias ) const noexcept {
197 return IsExpression_v<VT> && sv_.canAlias( alias );
198 }
199 //**********************************************************************************************
200
201 //**********************************************************************************************
207 template< typename T >
208 inline bool isAliased( const T* alias ) const noexcept {
209 return sv_.isAliased( alias );
210 }
211 //**********************************************************************************************
212
213 //**********************************************************************************************
218 inline bool canSMPAssign() const noexcept {
219 return false;
220 }
221 //**********************************************************************************************
222
223 private:
224 //**Member variables****************************************************************************
226 //**********************************************************************************************
227
228 //**Assignment to dense vectors*****************************************************************
240 template< typename VT2 > // Type of the target dense vector
241 friend inline void assign( DenseVector<VT2,TF>& lhs, const SVecRepeatExpr& rhs )
242 {
244
245 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
246
247 CompositeType_t<VT> x( serial( rhs.sv_ ) ); // Evaluation of the sparse vector operand
248
249 const size_t reps( rhs.template repetitions<0UL>() );
250 const size_t size( x.size() );
251
252 for( size_t rep=0UL; rep<reps; ++rep ) {
253 subvector( *lhs, rep*size, size, unchecked ) = serial( x );
254 }
255 }
257 //**********************************************************************************************
258
259 //**Assignment to sparse vectors****************************************************************
271 template< typename VT2 > // Type of the target sparse vector
272 friend inline void assign( SparseVector<VT2,TF>& lhs, const SVecRepeatExpr& rhs )
273 {
275
276 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
277
278 CompositeType_t<VT> x( serial( rhs.sv_ ) ); // Evaluation of the sparse vector operand
279
280 const size_t reps( rhs.template repetitions<0UL>() );
281 const size_t size( x.size() );
282
283 (*lhs).reserve( reps*size );
284
285 for( size_t rep=0UL; rep<reps; ++rep ) {
286 for( auto element=x.begin(); element!=x.end(); ++element ) {
287 (*lhs).append( rep*size+element->index(), element->value(), true );
288 }
289 }
290 }
292 //**********************************************************************************************
293
294 //**Addition assignment to dense vectors********************************************************
306 template< typename VT2 > // Type of the target dense vector
307 friend inline void addAssign( DenseVector<VT2,TF>& lhs, const SVecRepeatExpr& rhs )
308 {
310
311 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
312
313 CompositeType_t<VT> x( serial( rhs.sv_ ) ); // Evaluation of the sparse vector operand
314
315 const size_t reps( rhs.template repetitions<0UL>() );
316 const size_t size( x.size() );
317
318 for( size_t rep=0UL; rep<reps; ++rep ) {
319 subvector( *lhs, rep*size, size, unchecked ) += serial( x );
320 }
321 }
323 //**********************************************************************************************
324
325 //**Addition assignment to sparse vectors*******************************************************
326 // No special implementation for the addition assignment to sparse vectors.
327 //**********************************************************************************************
328
329 //**Subtraction assignment to dense vectors*****************************************************
341 template< typename VT2 > // Type of the target dense vector
342 friend inline void subAssign( DenseVector<VT2,TF>& lhs, const SVecRepeatExpr& rhs )
343 {
345
346 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
347
348 CompositeType_t<VT> x( serial( rhs.sv_ ) ); // Evaluation of the sparse vector operand
349
350 const size_t reps( rhs.template repetitions<0UL>() );
351 const size_t size( x.size() );
352
353 for( size_t rep=0UL; rep<reps; ++rep ) {
354 subvector( *lhs, rep*size, size, unchecked ) -= serial( x );
355 }
356 }
358 //**********************************************************************************************
359
360 //**Multiplication assignment to sparse vectors*************************************************
361 // No special implementation for the multiplication assignment to sparse vectors.
362 //**********************************************************************************************
363
364 //**Multiplication assignment to dense vectors**************************************************
376 template< typename VT2 > // Type of the target dense vector
377 friend inline void multAssign( DenseVector<VT2,TF>& lhs, const SVecRepeatExpr& rhs )
378 {
380
381 BLAZE_INTERNAL_ASSERT( (*lhs).size() == rhs.size(), "Invalid vector sizes" );
382
383 CompositeType_t<VT> x( serial( rhs.sv_ ) ); // Evaluation of the sparse vector operand
384
385 const size_t reps( rhs.template repetitions<0UL>() );
386 const size_t size( x.size() );
387
388 for( size_t rep=0UL; rep<reps; ++rep ) {
389 subvector( *lhs, rep*size, size, unchecked ) *= serial( x );
390 }
391 }
393 //**********************************************************************************************
394
395 //**Multiplication assignment to sparse vectors*************************************************
396 // No special implementation for the multiplication assignment to sparse vectors.
397 //**********************************************************************************************
398
399 //**Compile time checks*************************************************************************
404 //**********************************************************************************************
405};
406//*************************************************************************************************
407
408
409
410
411//=================================================================================================
412//
413// GLOBAL FUNCTIONS
414//
415//=================================================================================================
416
417//*************************************************************************************************
443template< typename VT // Type of the sparse vector
444 , bool TF > // Transpose flag
445inline decltype(auto) repeat( const SparseVector<VT,TF>& sv, size_t repetitions )
446{
448
449 using ReturnType = const SVecRepeatExpr<VT,TF>;
450 return ReturnType( *sv, repetitions );
451}
452//*************************************************************************************************
453
454
455//*************************************************************************************************
480template< size_t R // Compile time repetitions
481 , typename VT // Type of the sparse vector
482 , bool TF > // Transpose flag
483inline decltype(auto) repeat( const SparseVector<VT,TF>& sv )
484{
486
487 using ReturnType = const SVecRepeatExpr<VT,TF,R>;
488 return ReturnType( *sv );
489}
490//*************************************************************************************************
491
492
493//*************************************************************************************************
505template< size_t R0 // Compile time repetitions
506 , typename VT // Type of the sparse vector
507 , bool TF > // Transpose flag
508inline decltype(auto) repeat( const SparseVector<VT,TF>& sv, size_t repetitions )
509{
510 MAYBE_UNUSED( repetitions );
511
513
514 using ReturnType = const SVecRepeatExpr<VT,TF,R0>;
515 return ReturnType( *sv );
516}
518//*************************************************************************************************
519
520} // namespace blaze
521
522#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::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.
Definition: Aliases.h:190
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 implementation of the RepeatExprData class template.
Header file for the repeat trait.
Base class for N-dimensional dense vectors.
Definition: DenseVector.h:77
Auxiliary class template for the data members of repeater expression classes.
Definition: RepeatExprData.h:65
Expression object for the sparse vector repeat() function.
Definition: SVecRepeatExpr.h:85
Operand operand() const noexcept
Returns the sparse vector operand.
Definition: SVecRepeatExpr.h:180
bool canSMPAssign() const noexcept
Returns whether the expression can be used in SMP assignments.
Definition: SVecRepeatExpr.h:218
RepeatTrait_t< VT, CRAs... > ResultType
Result type for expression template evaluations.
Definition: SVecRepeatExpr.h:99
size_t nonZeros() const
Returns the number of non-zero elements in the sparse vector.
Definition: SVecRepeatExpr.h:170
RepeatExprData< 1UL, CRAs... > DataType
The type of the RepeatExprData base class.
Definition: SVecRepeatExpr.h:88
bool canAlias(const T *alias) const noexcept
Returns whether the expression can alias with the given address alias.
Definition: SVecRepeatExpr.h:196
ReturnType at(size_t index) const
Checked access to the vector elements.
Definition: SVecRepeatExpr.h:147
static constexpr bool smpAssignable
Compilation switch for the expression template assignment strategy.
Definition: SVecRepeatExpr.h:112
const ResultType CompositeType
Data type for composite expression templates.
Definition: SVecRepeatExpr.h:103
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: SVecRepeatExpr.h:208
Operand sv_
Sparse vector of the repeater expression.
Definition: SVecRepeatExpr.h:225
ElementType_t< VT > ElementType
Resulting element type.
Definition: SVecRepeatExpr.h:101
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: SVecRepeatExpr.h:160
ReturnType_t< VT > ReturnType
Return type for expression template evaluations.
Definition: SVecRepeatExpr.h:102
TransposeType_t< ResultType > TransposeType
Transpose type for expression template evaluations.
Definition: SVecRepeatExpr.h:100
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SVecRepeatExpr.h:134
SVecRepeatExpr(const VT &sv, RRAs... args) noexcept
Constructor for the SVecRepeatExpr class.
Definition: SVecRepeatExpr.h:122
If_t< IsExpression_v< VT >, const VT, const VT & > Operand
Composite data type of the sparse vector expression.
Definition: SVecRepeatExpr.h:106
Base class for sparse vectors.
Definition: SparseVector.h:72
Constraint on the data type.
Header file for the Computation base class.
Header file for the SparseVector base class.
Header file for the Transformation base class.
Header file for the VecRepeatExpr 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_MUST_BE_VECTOR_WITH_TRANSPOSE_FLAG(T, TF)
Constraint on the data type.
Definition: TransposeFlag.h:63
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE(T)
Constraint on the data type.
Definition: SparseVector.h:61
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) subvector(Vector< VT, TF > &, RSAs...)
Creating a view on a specific subvector of the given vector.
Definition: Subvector.h:158
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.
Constraint on the data type.
Header file for all forward declarations for expression class templates.
Header file for the serial shim.
Base class for all vector repeater expression templates.
Definition: VecRepeatExpr.h:69
Header file for basic type definitions.
Subvector specialization for dense vectors.