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 
47 #include <blaze/math/Functions.h>
50 #include <blaze/util/Assert.h>
51 #include <blaze/util/EnableIf.h>
52 #include <blaze/util/Exception.h>
53 #include <blaze/util/Types.h>
55 
56 
57 namespace blaze {
58 
59 //=================================================================================================
60 //
61 // CLASS SVECTRANSPOSER
62 //
63 //=================================================================================================
64 
65 //*************************************************************************************************
71 template< typename VT // Type of the sparse vector
72  , bool TF > // Transpose flag
73 class SVecTransposer : public SparseVector< SVecTransposer<VT,TF>, TF >
74 {
75  public:
76  //**Type definitions****************************************************************************
78  typedef typename VT::TransposeType ResultType;
79  typedef typename VT::ResultType TransposeType;
80  typedef typename VT::ElementType ElementType;
81  typedef typename VT::ReturnType ReturnType;
82  typedef const This& CompositeType;
83  typedef typename VT::Reference Reference;
85  typedef typename VT::Iterator Iterator;
86  typedef typename VT::ConstIterator ConstIterator;
87  //**********************************************************************************************
88 
89  //**Compilation flags***************************************************************************
91 
94  enum { smpAssignable = VT::smpAssignable };
95  //**********************************************************************************************
96 
97  //**Constructor*********************************************************************************
102  explicit inline SVecTransposer( VT& sv )
103  : sv_( sv ) // The sparse vector operand
104  {}
105  //**********************************************************************************************
106 
107  //**Subscript operator**************************************************************************
113  inline ConstReference operator[]( size_t index ) const {
114  BLAZE_USER_ASSERT( index < sv_.size(), "Invalid vector access index" );
115  return sv_[index];
116  }
117  //**********************************************************************************************
118 
119  //**At function*********************************************************************************
126  inline ConstReference at( size_t index ) const {
127  if( index >= sv_.size() ) {
128  BLAZE_THROW_OUT_OF_RANGE( "Invalid vector access index" );
129  }
130  return (*this)[index];
131  }
132  //**********************************************************************************************
133 
134  //**Begin function******************************************************************************
139  inline Iterator begin() {
140  return sv_.begin();
141  }
142  //**********************************************************************************************
143 
144  //**Begin function******************************************************************************
149  inline ConstIterator begin() const {
150  return sv_.cbegin();
151  }
152  //**********************************************************************************************
153 
154  //**Cbegin function*****************************************************************************
159  inline ConstIterator cbegin() const {
160  return sv_.cbegin();
161  }
162  //**********************************************************************************************
163 
164  //**End function********************************************************************************
169  inline Iterator end() {
170  return sv_.end();
171  }
172  //**********************************************************************************************
173 
174  //**End function********************************************************************************
179  inline ConstIterator end() const {
180  return sv_.cend();
181  }
182  //**********************************************************************************************
183 
184  //**Cend function*******************************************************************************
189  inline ConstIterator cend() const {
190  return sv_.cend();
191  }
192  //**********************************************************************************************
193 
194  //**Multiplication assignment operator**********************************************************
201  template< typename Other > // Data type of the right-hand side scalar
202  inline typename EnableIf< IsNumeric<Other>, SVecTransposer >::Type& operator*=( Other rhs )
203  {
204  (~sv_) *= rhs;
205  return *this;
206  }
207  //**********************************************************************************************
208 
209  //**Division assignment operator****************************************************************
218  template< typename Other > // Data type of the right-hand side scalar
219  inline typename EnableIf< IsNumeric<Other>, SVecTransposer >::Type& operator/=( Other rhs )
220  {
221  BLAZE_USER_ASSERT( rhs != Other(0), "Division by zero detected" );
222 
223  (~sv_) /= rhs;
224  return *this;
225  }
226  //**********************************************************************************************
227 
228  //**Size function*******************************************************************************
233  inline size_t size() const {
234  return sv_.size();
235  }
236  //**********************************************************************************************
237 
238  //**Reset function******************************************************************************
243  inline void reset() {
244  return sv_.reset();
245  }
246  //**********************************************************************************************
247 
248  //**Insert function*****************************************************************************
260  inline Iterator insert( size_t index, const ElementType& value ) {
261  return sv_.insert( index, value );
262  }
263  //**********************************************************************************************
264 
265  //**Find function*******************************************************************************
278  inline Iterator find( size_t index ) {
279  return sv_.find( index );
280  }
281  //**********************************************************************************************
282 
283  //**Reserve function****************************************************************************
292  inline void reserve( size_t nonzeros ) {
293  sv_.reserve( nonzeros );
294  }
295  //**********************************************************************************************
296 
297  //**Append function*****************************************************************************
322  inline void append( size_t index, const ElementType& value, bool check=false ) {
323  sv_.append( index, value, check );
324  }
325  //**********************************************************************************************
326 
327  //**CanAlias function***************************************************************************
333  template< typename Other > // Data type of the foreign expression
334  inline bool canAlias( const Other* alias ) const
335  {
336  return sv_.canAlias( alias );
337  }
338  //**********************************************************************************************
339 
340  //**IsAliased function**************************************************************************
346  template< typename Other > // Data type of the foreign expression
347  inline bool isAliased( const Other* alias ) const
348  {
349  return sv_.isAliased( alias );
350  }
351  //**********************************************************************************************
352 
353  //**CanSMPAssign function***********************************************************************
358  inline bool canSMPAssign() const
359  {
360  return sv_.canSMPAssign();
361  }
362  //**********************************************************************************************
363 
364  //**Transpose assignment of dense vectors*******************************************************
375  template< typename VT2 > // Type of the right-hand side dense vector
376  inline void assign( const DenseVector<VT2,TF>& rhs )
377  {
379 
380  BLAZE_INTERNAL_ASSERT( sv_.size() == (~rhs).size(), "Invalid vector sizes" );
381 
382  size_t nonzeros( 0UL );
383 
384  for( size_t i=0UL; i<sv_.size(); ++i ) {
385  if( !isDefault( (~rhs)[i] ) ) {
386  if( nonzeros++ == sv_.capacity() )
387  sv_.reserve( extendCapacity() );
388  sv_.append( i, (~rhs)[i] );
389  }
390  }
391  }
392  //**********************************************************************************************
393 
394  //**Transpose assignment of sparse vectors******************************************************
405  template< typename VT2 > // Type of the right-hand side sparse vector
406  inline void assign( const SparseVector<VT2,TF>& rhs )
407  {
409 
410  BLAZE_INTERNAL_ASSERT( sv_.size() == (~rhs).size(), "Invalid vector sizes" );
411 
412  // Using the following formulation instead of a std::copy function call of the form
413  //
414  // end_ = std::copy( (~rhs).begin(), (~rhs).end(), begin_ );
415  //
416  // results in much less requirements on the ConstIterator type provided from the right-hand
417  // sparse vector type
418  for( typename VT2::ConstIterator element=(~rhs).begin(); element!=(~rhs).end(); ++element )
419  sv_.append( element->index(), element->value() );
420  }
421  //**********************************************************************************************
422 
423  private:
424  //**********************************************************************************************
432  inline size_t extendCapacity() const
433  {
434  using blaze::max;
435  using blaze::min;
436 
437  size_t nonzeros( 2UL*sv_.capacity()+1UL );
438  nonzeros = max( nonzeros, 7UL );
439  nonzeros = min( nonzeros, sv_.size() );
440 
441  BLAZE_INTERNAL_ASSERT( nonzeros > sv_.capacity(), "Invalid capacity value" );
442 
443  return nonzeros;
444  }
445  //**********************************************************************************************
446 
447  //**Member variables****************************************************************************
448  VT& sv_;
449  //**********************************************************************************************
450 
451  //**Compile time checks*************************************************************************
457  //**********************************************************************************************
458 };
459 //*************************************************************************************************
460 
461 
462 
463 
464 //=================================================================================================
465 //
466 // GLOBAL OPERATORS
467 //
468 //=================================================================================================
469 
470 //*************************************************************************************************
478 template< typename VT // Type of the sparse vector
479  , bool TF > // Transpose flag
480 inline void reset( SVecTransposer<VT,TF>& v )
481 {
482  v.reset();
483 }
485 //*************************************************************************************************
486 
487 
488 
489 
490 //=================================================================================================
491 //
492 // SUBVECTORTRAIT SPECIALIZATIONS
493 //
494 //=================================================================================================
495 
496 //*************************************************************************************************
498 template< typename VT, bool TF >
499 struct SubvectorTrait< SVecTransposer<VT,TF> >
500 {
502 };
504 //*************************************************************************************************
505 
506 } // namespace blaze
507 
508 #endif
Constraint on the data type.
Iterator find(size_t index)
Inserting an element into the sparse vector.
Definition: SVecTransposer.h:278
const MT::ElementType max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1729
Header file for mathematical functions.
VT::ConstReference ConstReference
Reference to a constant matrix value.
Definition: SVecTransposer.h:84
#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.
VT::ElementType ElementType
Resulting element type.
Definition: SVecTransposer.h:80
Header file for the SparseVector base class.
EnableIf< IsNumeric< Other >, SVecTransposer >::Type & operator/=(Other rhs)
Division assignment operator for the division of a vector by a scalar value ( ).
Definition: SVecTransposer.h:219
bool canAlias(const Other *alias) const
Returns whether the vector can alias with the given address alias.
Definition: SVecTransposer.h:334
#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:118
const This & CompositeType
Data type for composite expression templates.
Definition: SVecTransposer.h:82
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:507
ConstReference at(size_t index) const
Checked access to the vector elements.
Definition: SVecTransposer.h:126
size_t extendCapacity() const
Calculating a new vector capacity.
Definition: SVecTransposer.h:432
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:547
VT::ResultType TransposeType
Transpose type for expression template evaluations.
Definition: SVecTransposer.h:79
ConstReference operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SVecTransposer.h:113
VT::ConstIterator ConstIterator
Iterator over constant elements.
Definition: SVecTransposer.h:86
VT::Reference Reference
Reference to a non-constant matrix value.
Definition: SVecTransposer.h:83
VT::TransposeType ResultType
Result type for expression template evaluations.
Definition: SVecTransposer.h:78
void append(size_t index, const ElementType &value, bool check=false)
Appending an element to the sparse vector.
Definition: SVecTransposer.h:322
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2592
#define BLAZE_THROW_OUT_OF_RANGE(MESSAGE)
Macro for the emission of a std::out_of_range exceptionThis macro encapsulates the default way of Bla...
Definition: Exception.h:331
const MT::ElementType min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1682
Header file for the subvector trait.
Expression object for the transposition of a sparse vector.The SVecTransposer class is a wrapper obje...
Definition: Forward.h:136
VT::ReturnType ReturnType
Return type for expression template evaluations.
Definition: SVecTransposer.h:81
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:79
size_t size() const
Returns the current size/dimension of the vector.
Definition: SVecTransposer.h:233
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:189
CompressedMatrix< Type, false > TransposeType
Transpose type for expression template evaluations.
Definition: CompressedMatrix.h:2585
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2586
SVecTransposer< VT, TF > This
Type of this SVecTransposer instance.
Definition: SVecTransposer.h:77
Constraint on the data type.
const Type & ConstReference
Reference to a constant matrix value.
Definition: CompressedMatrix.h:2590
ConstIterator cbegin() const
Returns an iterator to the first non-zero element of the sparse vector.
Definition: SVecTransposer.h:159
void reset()
Resets the vector elements.
Definition: SVecTransposer.h:243
Header file for the EnableIf class template.
void reserve(size_t nonzeros)
Setting the minimum capacity of the sparse vector.
Definition: SVecTransposer.h:292
ConstIterator begin() const
Returns an iterator to the first non-zero element of the sparse vector.
Definition: SVecTransposer.h:149
VT::Iterator Iterator
Iterator over non-constant elements.
Definition: SVecTransposer.h:85
void assign(const SparseVector< VT2, TF > &rhs)
Implementation of the transpose assignment of a sparse vector.
Definition: SVecTransposer.h:406
Header file for the IsNumeric type trait.
bool canSMPAssign() const
Returns whether the vector can be used in SMP assignments.
Definition: SVecTransposer.h:358
const Type & ReturnType
Return type for expression template evaluations.
Definition: CompressedMatrix.h:2587
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:376
Substitution Failure Is Not An Error (SFINAE) class.The EnableIf class template is an auxiliary tool ...
Definition: EnableIf.h:184
bool isAliased(const Other *alias) const
Returns whether the vector is aliased with the given address alias.
Definition: SVecTransposer.h:347
Iterator end()
Returns an iterator just past the last non-zero element of the sparse vector.
Definition: SVecTransposer.h:169
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2591
Header file for the isDefault shim.
VT & sv_
The sparse vector operand.
Definition: SVecTransposer.h:448
Iterator begin()
Returns an iterator to the first non-zero element of the sparse vector.
Definition: SVecTransposer.h:139
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:118
This ResultType
Result type for expression template evaluations.
Definition: CompressedMatrix.h:2583
Iterator insert(size_t index, const ElementType &value)
Inserting an element into the sparse vector.
Definition: SVecTransposer.h:260
MatrixAccessProxy< This > Reference
Reference to a non-constant matrix value.
Definition: CompressedMatrix.h:2589
ConstIterator end() const
Returns an iterator just past the last non-zero element of the sparse vector.
Definition: SVecTransposer.h:179
Header file for exception macros.
#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:81
#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)
Constructor for the SVecTransposer class.
Definition: SVecTransposer.h:102
EnableIf< IsNumeric< Other >, SVecTransposer >::Type & operator*=(Other rhs)
Multiplication assignment operator for the multiplication between a vector and a scalar value ( )...
Definition: SVecTransposer.h:202