SVecTransposer.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_SVECTRANSPOSER_H_
36 #define _BLAZE_MATH_EXPRESSIONS_SVECTRANSPOSER_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <blaze/math/Aliases.h>
47 #include <blaze/math/Exception.h>
49 #include <blaze/math/Functions.h>
52 #include <blaze/util/Assert.h>
53 #include <blaze/util/EnableIf.h>
54 #include <blaze/util/Types.h>
56 
57 
58 namespace blaze {
59 
60 //=================================================================================================
61 //
62 // CLASS SVECTRANSPOSER
63 //
64 //=================================================================================================
65 
66 //*************************************************************************************************
72 template< typename VT // Type of the sparse vector
73  , bool TF > // Transpose flag
74 class SVecTransposer : public SparseVector< SVecTransposer<VT,TF>, TF >
75 {
76  public:
77  //**Type definitions****************************************************************************
83  typedef const This& CompositeType;
88  //**********************************************************************************************
89 
90  //**Compilation flags***************************************************************************
92 
95  enum : bool { smpAssignable = VT::smpAssignable };
96  //**********************************************************************************************
97 
98  //**Constructor*********************************************************************************
103  explicit inline SVecTransposer( VT& sv ) noexcept
104  : sv_( sv ) // The sparse vector operand
105  {}
106  //**********************************************************************************************
107 
108  //**Subscript operator**************************************************************************
114  inline ConstReference operator[]( size_t index ) const {
115  BLAZE_USER_ASSERT( index < sv_.size(), "Invalid vector access index" );
116  return sv_[index];
117  }
118  //**********************************************************************************************
119 
120  //**At function*********************************************************************************
127  inline ConstReference at( size_t index ) const {
128  if( index >= sv_.size() ) {
129  BLAZE_THROW_OUT_OF_RANGE( "Invalid vector access index" );
130  }
131  return (*this)[index];
132  }
133  //**********************************************************************************************
134 
135  //**Begin function******************************************************************************
140  inline Iterator begin() {
141  return sv_.begin();
142  }
143  //**********************************************************************************************
144 
145  //**Begin function******************************************************************************
150  inline ConstIterator begin() const {
151  return sv_.cbegin();
152  }
153  //**********************************************************************************************
154 
155  //**Cbegin function*****************************************************************************
160  inline ConstIterator cbegin() const {
161  return sv_.cbegin();
162  }
163  //**********************************************************************************************
164 
165  //**End function********************************************************************************
170  inline Iterator end() {
171  return sv_.end();
172  }
173  //**********************************************************************************************
174 
175  //**End function********************************************************************************
180  inline ConstIterator end() const {
181  return sv_.cend();
182  }
183  //**********************************************************************************************
184 
185  //**Cend function*******************************************************************************
190  inline ConstIterator cend() const {
191  return sv_.cend();
192  }
193  //**********************************************************************************************
194 
195  //**Multiplication assignment operator**********************************************************
202  template< typename Other > // Data type of the right-hand side scalar
204  {
205  (~sv_) *= rhs;
206  return *this;
207  }
208  //**********************************************************************************************
209 
210  //**Division assignment operator****************************************************************
219  template< typename Other > // Data type of the right-hand side scalar
221  {
222  BLAZE_USER_ASSERT( rhs != Other(0), "Division by zero detected" );
223 
224  (~sv_) /= rhs;
225  return *this;
226  }
227  //**********************************************************************************************
228 
229  //**Size function*******************************************************************************
234  inline size_t size() const noexcept {
235  return sv_.size();
236  }
237  //**********************************************************************************************
238 
239  //**Reset function******************************************************************************
244  inline void reset() {
245  return sv_.reset();
246  }
247  //**********************************************************************************************
248 
249  //**Insert function*****************************************************************************
261  inline Iterator insert( size_t index, const ElementType& value ) {
262  return sv_.insert( index, value );
263  }
264  //**********************************************************************************************
265 
266  //**Find function*******************************************************************************
279  inline Iterator find( size_t index ) {
280  return sv_.find( index );
281  }
282  //**********************************************************************************************
283 
284  //**Reserve function****************************************************************************
293  inline void reserve( size_t nonzeros ) {
294  sv_.reserve( nonzeros );
295  }
296  //**********************************************************************************************
297 
298  //**Append function*****************************************************************************
323  inline void append( size_t index, const ElementType& value, bool check=false ) {
324  sv_.append( index, value, check );
325  }
326  //**********************************************************************************************
327 
328  //**CanAlias function***************************************************************************
334  template< typename Other > // Data type of the foreign expression
335  inline bool canAlias( const Other* alias ) const noexcept
336  {
337  return sv_.canAlias( alias );
338  }
339  //**********************************************************************************************
340 
341  //**IsAliased function**************************************************************************
347  template< typename Other > // Data type of the foreign expression
348  inline bool isAliased( const Other* alias ) const noexcept
349  {
350  return sv_.isAliased( alias );
351  }
352  //**********************************************************************************************
353 
354  //**CanSMPAssign function***********************************************************************
359  inline bool canSMPAssign() const noexcept
360  {
361  return sv_.canSMPAssign();
362  }
363  //**********************************************************************************************
364 
365  //**Transpose assignment of dense vectors*******************************************************
376  template< typename VT2 > // Type of the right-hand side dense vector
377  inline void assign( const DenseVector<VT2,TF>& rhs )
378  {
380 
381  BLAZE_INTERNAL_ASSERT( sv_.size() == (~rhs).size(), "Invalid vector sizes" );
382 
383  size_t nonzeros( 0UL );
384 
385  for( size_t i=0UL; i<sv_.size(); ++i ) {
386  if( !isDefault( (~rhs)[i] ) ) {
387  if( nonzeros++ == sv_.capacity() )
388  sv_.reserve( extendCapacity() );
389  sv_.append( i, (~rhs)[i] );
390  }
391  }
392  }
393  //**********************************************************************************************
394 
395  //**Transpose assignment of sparse vectors******************************************************
406  template< typename VT2 > // Type of the right-hand side sparse vector
407  inline void assign( const SparseVector<VT2,TF>& rhs )
408  {
410 
411  BLAZE_INTERNAL_ASSERT( sv_.size() == (~rhs).size(), "Invalid vector sizes" );
412 
413  // Using the following formulation instead of a std::copy function call of the form
414  //
415  // end_ = std::copy( (~rhs).begin(), (~rhs).end(), begin_ );
416  //
417  // results in much less requirements on the ConstIterator type provided from the right-hand
418  // sparse vector type
419  for( ConstIterator_<VT2> element=(~rhs).begin(); element!=(~rhs).end(); ++element )
420  sv_.append( element->index(), element->value() );
421  }
422  //**********************************************************************************************
423 
424  private:
425  //**********************************************************************************************
433  inline size_t extendCapacity() const noexcept
434  {
435  using blaze::max;
436  using blaze::min;
437 
438  size_t nonzeros( 2UL*sv_.capacity()+1UL );
439  nonzeros = max( nonzeros, 7UL );
440  nonzeros = min( nonzeros, sv_.size() );
441 
442  BLAZE_INTERNAL_ASSERT( nonzeros > sv_.capacity(), "Invalid capacity value" );
443 
444  return nonzeros;
445  }
446  //**********************************************************************************************
447 
448  //**Member variables****************************************************************************
449  VT& sv_;
450  //**********************************************************************************************
451 
452  //**Compile time checks*************************************************************************
458  //**********************************************************************************************
459 };
460 //*************************************************************************************************
461 
462 
463 
464 
465 //=================================================================================================
466 //
467 // GLOBAL OPERATORS
468 //
469 //=================================================================================================
470 
471 //*************************************************************************************************
479 template< typename VT // Type of the sparse vector
480  , bool TF > // Transpose flag
481 inline void reset( SVecTransposer<VT,TF>& v )
482 {
483  v.reset();
484 }
486 //*************************************************************************************************
487 
488 
489 
490 
491 //=================================================================================================
492 //
493 // SUBVECTORTRAIT SPECIALIZATIONS
494 //
495 //=================================================================================================
496 
497 //*************************************************************************************************
499 template< typename VT, bool TF >
500 struct SubvectorTrait< SVecTransposer<VT,TF> >
501 {
502  using Type = SubvectorTrait_< ResultType_< SVecTransposer<VT,TF> > >;
503 };
505 //*************************************************************************************************
506 
507 } // namespace blaze
508 
509 #endif
Constraint on the data type.
Iterator find(size_t index)
Inserting an element into the sparse vector.
Definition: SVecTransposer.h:279
Header file for auxiliary alias declarations.
Header file for mathematical functions.
#define BLAZE_USER_ASSERT(expr, msg)
Run time assertion macro for user checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_USER_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERT flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:117
Header file for basic type definitions.
Header file for the SparseVector base class.
size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: SVecTransposer.h:234
#define BLAZE_CONSTRAINT_MUST_NOT_BE_COMPUTATION_TYPE(T)
Constraint on the data type.In case the given data type T is a computational expression (i...
Definition: Computation.h:81
Iterator_< VT > Iterator
Iterator over non-constant elements.
Definition: SVecTransposer.h:86
const This & CompositeType
Data type for composite expression templates.
Definition: SVecTransposer.h:83
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:533
ConstReference at(size_t index) const
Checked access to the vector elements.
Definition: SVecTransposer.h:127
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1669
typename T::ResultType ResultType_
Alias declaration for nested ResultType type definitions.The ResultType_ alias declaration provides a...
Definition: Aliases.h:323
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1716
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:573
ElementType_< VT > ElementType
Resulting element type.
Definition: SVecTransposer.h:81
ConstReference operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SVecTransposer.h:114
typename T::ReturnType ReturnType_
Alias declaration for nested ReturnType type definitions.The ReturnType_ alias declaration provides a...
Definition: Aliases.h:343
TransposeType_< VT > ResultType
Result type for expression template evaluations.
Definition: SVecTransposer.h:79
void append(size_t index, const ElementType &value, bool check=false)
Appending an element to the sparse vector.
Definition: SVecTransposer.h:323
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
#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
ConstReference_< VT > ConstReference
Reference to a constant matrix value.
Definition: SVecTransposer.h:85
Header file for the subvector trait.
typename T::ElementType ElementType_
Alias declaration for nested ElementType type definitions.The ElementType_ alias declaration provides...
Definition: Aliases.h:163
Expression object for the transposition of a sparse vector.The SVecTransposer class is a wrapper obje...
Definition: Forward.h:126
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:70
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a sparse, N-dimensional vector type...
Definition: SparseVector.h:61
Constraint on the data type.
ConstIterator cend() const
Returns an iterator just past the last non-zero element of the sparse vector.
Definition: SVecTransposer.h:190
size_t extendCapacity() const noexcept
Calculating a new vector capacity.
Definition: SVecTransposer.h:433
Header file for the exception macros of the math module.
SVecTransposer< VT, TF > This
Type of this SVecTransposer instance.
Definition: SVecTransposer.h:78
Constraint on the data type.
ConstIterator cbegin() const
Returns an iterator to the first non-zero element of the sparse vector.
Definition: SVecTransposer.h:160
void reset()
Resets the vector elements.
Definition: SVecTransposer.h:244
EnableIf_< IsNumeric< Other >, SVecTransposer > & operator*=(Other rhs)
Multiplication assignment operator for the multiplication between a vector and a scalar value ( )...
Definition: SVecTransposer.h:203
typename T::Reference Reference_
Alias declaration for nested Reference type definitions.The Reference_ alias declaration provides a c...
Definition: Aliases.h:283
Header file for the EnableIf class template.
void reserve(size_t nonzeros)
Setting the minimum capacity of the sparse vector.
Definition: SVecTransposer.h:293
ConstIterator begin() const
Returns an iterator to the first non-zero element of the sparse vector.
Definition: SVecTransposer.h:150
void assign(const SparseVector< VT2, TF > &rhs)
Implementation of the transpose assignment of a sparse vector.
Definition: SVecTransposer.h:407
Header file for the IsNumeric type trait.
Header file for run time assertion macros.
void assign(const DenseVector< VT2, TF > &rhs)
Implementation of the transpose assignment of a dense vector.
Definition: SVecTransposer.h:377
bool isAliased(const Other *alias) const noexcept
Returns whether the vector is aliased with the given address alias.
Definition: SVecTransposer.h:348
Reference_< VT > Reference
Reference to a non-constant matrix value.
Definition: SVecTransposer.h:84
Iterator end()
Returns an iterator just past the last non-zero element of the sparse vector.
Definition: SVecTransposer.h:170
Header file for the isDefault shim.
typename EnableIf< Condition, T >::Type EnableIf_
Auxiliary alias declaration for the EnableIf class template.The EnableIf_ alias declaration provides ...
Definition: EnableIf.h:223
typename T::Iterator Iterator_
Alias declaration for nested Iterator type definitions.The Iterator_ alias declaration provides a con...
Definition: Aliases.h:183
typename T::ConstReference ConstReference_
Alias declaration for nested ConstReference type definitions.The ConstReference_ alias declaration pr...
Definition: Aliases.h:143
EnableIf_< IsNumeric< Other >, SVecTransposer > & operator/=(Other rhs)
Division assignment operator for the division of a vector by a scalar value ( ).
Definition: SVecTransposer.h:220
typename T::ConstIterator ConstIterator_
Alias declaration for nested ConstIterator type definitions.The ConstIterator_ alias declaration prov...
Definition: Aliases.h:103
VT & sv_
The sparse vector operand.
Definition: SVecTransposer.h:449
bool canAlias(const Other *alias) const noexcept
Returns whether the vector can alias with the given address alias.
Definition: SVecTransposer.h:335
ConstIterator_< VT > ConstIterator
Iterator over constant elements.
Definition: SVecTransposer.h:87
Iterator begin()
Returns an iterator to the first non-zero element of the sparse vector.
Definition: SVecTransposer.h:140
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:110
Iterator insert(size_t index, const ElementType &value)
Inserting an element into the sparse vector.
Definition: SVecTransposer.h:261
ConstIterator end() const
Returns an iterator just past the last non-zero element of the sparse vector.
Definition: SVecTransposer.h:180
typename T::TransposeType TransposeType_
Alias declaration for nested TransposeType type definitions.The TransposeType_ alias declaration prov...
Definition: Aliases.h:403
ResultType_< VT > TransposeType
Transpose type for expression template evaluations.
Definition: SVecTransposer.h:80
#define BLAZE_CONSTRAINT_MUST_BE_VECTOR_WITH_TRANSPOSE_FLAG(T, TF)
Constraint on the data type.In case the given data type T is not a dense or sparse vector type and in...
Definition: TransposeFlag.h:63
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101
SVecTransposer(VT &sv) noexcept
Constructor for the SVecTransposer class.
Definition: SVecTransposer.h:103
bool canSMPAssign() const noexcept
Returns whether the vector can be used in SMP assignments.
Definition: SVecTransposer.h:359
ReturnType_< VT > ReturnType
Return type for expression template evaluations.
Definition: SVecTransposer.h:82