All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SMatTransposer.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_SMATTRANSPOSER_H_
36 #define _BLAZE_MATH_EXPRESSIONS_SMATTRANSPOSER_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <vector>
49 #include <blaze/util/Assert.h>
50 #include <blaze/util/EnableIf.h>
51 #include <blaze/util/Types.h>
53 
54 
55 namespace blaze {
56 
57 //=================================================================================================
58 //
59 // CLASS SMATTRANSPOSER
60 //
61 //=================================================================================================
62 
63 //*************************************************************************************************
69 template< typename MT // Type of the sparse matrix
70  , bool SO > // Storage order
71 class SMatTransposer : public SparseMatrix< SMatTransposer<MT,SO>, SO >
72 {
73  public:
74  //**Type definitions****************************************************************************
76  typedef typename MT::TransposeType ResultType;
77  typedef MT OppositeType;
78  typedef typename MT::ResultType TransposeType;
79  typedef typename MT::ElementType ElementType;
80  typedef typename MT::ReturnType ReturnType;
81  typedef const This& CompositeType;
82  typedef typename MT::Reference Reference;
84  typedef typename MT::Iterator Iterator;
85  typedef typename MT::ConstIterator ConstIterator;
86  //**********************************************************************************************
87 
88  //**Compilation flags***************************************************************************
90 
93  enum { smpAssignable = MT::smpAssignable };
94  //**********************************************************************************************
95 
96  //**Constructor*********************************************************************************
101  explicit inline SMatTransposer( MT& sm )
102  : sm_( sm ) // The sparse matrix operand
103  {}
104  //**********************************************************************************************
105 
106  //**Access operator*****************************************************************************
113  inline ConstReference operator()( size_t i, size_t j ) const {
114  BLAZE_INTERNAL_ASSERT( i < sm_.columns(), "Invalid row access index" );
115  BLAZE_INTERNAL_ASSERT( j < sm_.row() , "Invalid column access index" );
116  return sm_(j,i);
117  }
118  //**********************************************************************************************
119 
120  //**Begin function******************************************************************************
131  inline ConstIterator begin( size_t i ) const {
132  return sm_.cbegin(i);
133  }
134  //**********************************************************************************************
135 
136  //**Cbegin function*****************************************************************************
147  inline ConstIterator cbegin( size_t i ) const {
148  return sm_.cbegin(i);
149  }
150  //**********************************************************************************************
151 
152  //**End function********************************************************************************
163  inline ConstIterator end( size_t i ) const {
164  return sm_.cend(i);
165  }
166  //**********************************************************************************************
167 
168  //**Cend function*******************************************************************************
179  inline ConstIterator cend( size_t i ) const {
180  return sm_.cend(i);
181  }
182  //**********************************************************************************************
183 
184  //**Multiplication assignment operator**********************************************************
191  template< typename Other > // Data type of the right-hand side scalar
192  inline typename EnableIf< IsNumeric<Other>, SMatTransposer >::Type& operator*=( Other rhs )
193  {
194  (~sm_) *= rhs;
195  return *this;
196  }
197  //**********************************************************************************************
198 
199  //**Division assignment operator****************************************************************
208  template< typename Other > // Data type of the right-hand side scalar
209  inline typename EnableIf< IsNumeric<Other>, SMatTransposer >::Type& operator/=( Other rhs )
210  {
211  BLAZE_USER_ASSERT( rhs != Other(0), "Division by zero detected" );
212 
213  (~sm_) /= rhs;
214  return *this;
215  }
216  //**********************************************************************************************
217 
218  //**Rows function*******************************************************************************
223  inline size_t rows() const {
224  return sm_.columns();
225  }
226  //**********************************************************************************************
227 
228  //**Columns function****************************************************************************
233  inline size_t columns() const {
234  return sm_.rows();
235  }
236  //**********************************************************************************************
237 
238  //**Reset function******************************************************************************
243  inline void reset() {
244  return sm_.reset();
245  }
246  //**********************************************************************************************
247 
248  //**Insert function*****************************************************************************
261  inline Iterator insert( size_t i, size_t j, const ElementType& value ) {
262  return sm_.insert( j, i, value );
263  }
264  //**********************************************************************************************
265 
266  //**Reserve function****************************************************************************
276  inline void reserve( size_t nonzeros ) {
277  sm_.reserve( nonzeros );
278  }
279  //**********************************************************************************************
280 
281  //**Reserve function****************************************************************************
295  inline void reserve( size_t i, size_t nonzeros ) {
296  sm_.reserve( i, nonzeros );
297  }
298  //**********************************************************************************************
299 
300  //**Append function*****************************************************************************
327  inline void append( size_t i, size_t j, const ElementType& value, bool check=false ) {
328  sm_.append( j, i, value, check );
329  }
330  //**********************************************************************************************
331 
332  //**Finalize function***************************************************************************
345  inline void finalize( size_t i ) {
346  sm_.finalize( i );
347  }
348  //**********************************************************************************************
349 
350  //**********************************************************************************************
356  template< typename Other > // Data type of the foreign expression
357  inline bool canAlias( const Other* alias ) const
358  {
359  return sm_.canAlias( alias );
360  }
361  //**********************************************************************************************
362 
363  //**********************************************************************************************
369  template< typename Other > // Data type of the foreign expression
370  inline bool isAliased( const Other* alias ) const
371  {
372  return sm_.isAliased( alias );
373  }
374  //**********************************************************************************************
375 
376  //**Transpose assignment of row-major sparse matrices*******************************************
387  template< typename MT2 > // Type of the right-hand side sparse matrix
388  inline void assign( const SparseMatrix<MT2,false>& rhs )
389  {
391 
392  BLAZE_INTERNAL_ASSERT( sm_.columns() == (~rhs).rows() , "Invalid number of rows" );
393  BLAZE_INTERNAL_ASSERT( sm_.rows() == (~rhs).columns() , "Invalid number of columns" );
394  BLAZE_INTERNAL_ASSERT( sm_.capacity() >= (~rhs).nonZeros(), "Capacity not sufficient" );
395 
396  typedef typename MT2::ConstIterator RhsIterator;
397 
398  const size_t m( rows() );
399 
400  for( size_t i=0UL; i<m; ++i ) {
401  for( RhsIterator element=(~rhs).begin(i); element!=(~rhs).end(i); ++element )
402  sm_.append( element->index(), i, element->value() );
403  finalize( i );
404  }
405  }
406  //**********************************************************************************************
407 
408  //**Transpose assignment of column-major sparse matrices****************************************
419  template< typename MT2 > // Type of the right-hand side sparse matrix
420  inline void assign( const SparseMatrix<MT2,true>& rhs )
421  {
423 
424  BLAZE_INTERNAL_ASSERT( sm_.columns() == (~rhs).rows() , "Invalid number of rows" );
425  BLAZE_INTERNAL_ASSERT( sm_.rows() == (~rhs).columns() , "Invalid number of columns" );
426  BLAZE_INTERNAL_ASSERT( sm_.capacity() >= (~rhs).nonZeros(), "Capacity not sufficient" );
427 
428  typedef typename MT2::ConstIterator RhsIterator;
429 
430  const size_t m( rows() );
431  const size_t n( columns() );
432 
433  // Counting the number of elements per row
434  std::vector<size_t> rowLengths( m, 0UL );
435  for( size_t j=0UL; j<n; ++j ) {
436  for( RhsIterator element=(~rhs).begin(j); element!=(~rhs).end(j); ++element )
437  ++rowLengths[element->index()];
438  }
439 
440  // Resizing the sparse matrix
441  for( size_t i=0UL; i<m; ++i ) {
442  sm_.reserve( i, rowLengths[i] );
443  }
444 
445  // Appending the elements to the rows of the sparse matrix
446  for( size_t j=0UL; j<n; ++j ) {
447  for( RhsIterator element=(~rhs).begin(j); element!=(~rhs).end(j); ++element ) {
448  sm_.append( j, element->index(), element->value() );
449  }
450  }
451  }
452  //**********************************************************************************************
453 
454  private:
455  //**Member variables****************************************************************************
456  MT& sm_;
457  //**********************************************************************************************
458 
459  //**Compile time checks*************************************************************************
465  //**********************************************************************************************
466 };
467 //*************************************************************************************************
468 
469 
470 
471 
472 //=================================================================================================
473 //
474 // CLASS TEMPLATE SPECIALIZATION FOR ROW-MAJOR MATRICES
475 //
476 //=================================================================================================
477 
478 //*************************************************************************************************
486 template< typename MT > // Type of the sparse matrix
487 class SMatTransposer<MT,true> : public SparseMatrix< SMatTransposer<MT,true>, true >
488 {
489  public:
490  //**Type definitions****************************************************************************
491  typedef SMatTransposer<MT,true> This;
492  typedef typename MT::TransposeType ResultType;
493  typedef MT OppositeType;
494  typedef typename MT::ResultType TransposeType;
495  typedef typename MT::ElementType ElementType;
496  typedef typename MT::ReturnType ReturnType;
497  typedef const This& CompositeType;
498  typedef typename MT::Reference Reference;
499  typedef typename MT::ConstReference ConstReference;
500  typedef typename MT::Iterator Iterator;
501  typedef typename MT::ConstIterator ConstIterator;
502  //**********************************************************************************************
503 
504  //**Compilation flags***************************************************************************
506 
509  enum { smpAssignable = MT::smpAssignable };
510  //**********************************************************************************************
511 
512  //**Constructor*********************************************************************************
517  explicit inline SMatTransposer( MT& sm )
518  : sm_( sm ) // The sparse matrix operand
519  {}
520  //**********************************************************************************************
521 
522  //**Access operator*****************************************************************************
529  inline ConstReference operator()( size_t i, size_t j ) const {
530  BLAZE_INTERNAL_ASSERT( i < sm_.columns(), "Invalid row access index" );
531  BLAZE_INTERNAL_ASSERT( j < sm_.row() , "Invalid column access index" );
532  return sm_(j,i);
533  }
534  //**********************************************************************************************
535 
536  //**Begin function******************************************************************************
542  inline ConstIterator begin( size_t j ) const {
543  return sm_.cbegin(j);
544  }
545  //**********************************************************************************************
546 
547  //**Cbegin function*****************************************************************************
553  inline ConstIterator cbegin( size_t j ) const {
554  return sm_.begin(j);
555  }
556  //**********************************************************************************************
557 
558  //**End function********************************************************************************
564  inline ConstIterator end( size_t j ) const {
565  return sm_.end(j);
566  }
567  //**********************************************************************************************
568 
569  //**Cend function*******************************************************************************
575  inline ConstIterator cend( size_t j ) const {
576  return sm_.end(j);
577  }
578  //**********************************************************************************************
579 
580  //**Multiplication assignment operator**********************************************************
587  template< typename Other > // Data type of the right-hand side scalar
588  inline typename EnableIf< IsNumeric<Other>, SMatTransposer >::Type& operator*=( Other rhs )
589  {
590  (~sm_) *= rhs;
591  return *this;
592  }
593  //**********************************************************************************************
594 
595  //**Division assignment operator****************************************************************
604  template< typename Other > // Data type of the right-hand side scalar
605  inline typename EnableIf< IsNumeric<Other>, SMatTransposer >::Type& operator/=( Other rhs )
606  {
607  BLAZE_USER_ASSERT( rhs != Other(0), "Division by zero detected" );
608 
609  (~sm_) /= rhs;
610  return *this;
611  }
612  //**********************************************************************************************
613 
614  //**Rows function*******************************************************************************
619  inline size_t rows() const {
620  return sm_.columns();
621  }
622  //**********************************************************************************************
623 
624  //**Columns function****************************************************************************
629  inline size_t columns() const {
630  return sm_.rows();
631  }
632  //**********************************************************************************************
633 
634  //**Reset function******************************************************************************
639  inline void reset() {
640  return sm_.reset();
641  }
642  //**********************************************************************************************
643 
644  //**Insert function*****************************************************************************
657  inline Iterator insert( size_t i, size_t j, const ElementType& value ) {
658  return sm_.insert( j, i, value );
659  }
660  //**********************************************************************************************
661 
662  //**Reserve function****************************************************************************
672  inline void reserve( size_t nonzeros ) {
673  sm_.reserve( nonzeros );
674  }
675  //**********************************************************************************************
676 
677  //**Reserve function****************************************************************************
688  inline void reserve( size_t i, size_t nonzeros ) {
689  sm_.reserve( i, nonzeros );
690  }
691  //**********************************************************************************************
692 
693  //**Append function*****************************************************************************
720  void append( size_t i, size_t j, const ElementType& value, bool check=false ) {
721  sm_.append( j, i, value, check );
722  }
723  //**********************************************************************************************
724 
725  //**Finalize function***************************************************************************
738  inline void finalize( size_t j ) {
739  sm_.finalize( j );
740  }
741  //**********************************************************************************************
742 
743  //**********************************************************************************************
749  template< typename Other > // Data type of the foreign expression
750  inline bool isAliased( const Other* alias ) const
751  {
752  return sm_.isAliased( alias );
753  }
754  //**********************************************************************************************
755 
756  //**Transpose assignment of row-major sparse matrices*******************************************
767  template< typename MT2 > // Type of the right-hand side sparse matrix
768  inline void assign( const SparseMatrix<MT2,false>& rhs )
769  {
771 
772  BLAZE_INTERNAL_ASSERT( sm_.columns() == (~rhs).rows() , "Invalid number of rows" );
773  BLAZE_INTERNAL_ASSERT( sm_.rows() == (~rhs).columns() , "Invalid number of columns" );
774  BLAZE_INTERNAL_ASSERT( sm_.capacity() >= (~rhs).nonZeros(), "Capacity not sufficient" );
775 
776  typedef typename MT2::ConstIterator RhsIterator;
777 
778  const size_t m( rows() );
779  const size_t n( columns() );
780 
781  // Counting the number of elements per row
782  std::vector<size_t> columnLengths( n, 0UL );
783  for( size_t i=0UL; i<m; ++i ) {
784  for( RhsIterator element=(~rhs).begin(i); element!=(~rhs).end(i); ++element )
785  ++columnLengths[element->index()];
786  }
787 
788  // Resizing the sparse matrix
789  for( size_t j=0UL; j<n; ++j ) {
790  sm_.reserve( j, columnLengths[j] );
791  }
792 
793  // Appending the elements to the columns of the sparse matrix
794  for( size_t i=0UL; i<m; ++i ) {
795  for( RhsIterator element=(~rhs).begin(i); element!=(~rhs).end(i); ++element ) {
796  sm_.append( element->index(), i, element->value() );
797  }
798  }
799  }
800  //**********************************************************************************************
801 
802  //**Transpose assignment of column-major sparse matrices****************************************
813  template< typename MT2 > // Type of the right-hand side sparse matrix
814  inline void assign( const SparseMatrix<MT2,true>& rhs )
815  {
817 
818  BLAZE_INTERNAL_ASSERT( sm_.columns() == (~rhs).rows() , "Invalid number of rows" );
819  BLAZE_INTERNAL_ASSERT( sm_.rows() == (~rhs).columns() , "Invalid number of columns" );
820  BLAZE_INTERNAL_ASSERT( sm_.capacity() >= (~rhs).nonZeros(), "Capacity not sufficient" );
821 
822  typedef typename MT2::ConstIterator RhsIterator;
823 
824  const size_t n( columns() );
825 
826  for( size_t j=0UL; j<n; ++j ) {
827  for( RhsIterator element=(~rhs).begin(j); element!=(~rhs).end(j); ++element )
828  sm_.append( j, element->index(), element->value() );
829  finalize( j );
830  }
831  }
832  //**********************************************************************************************
833 
834  private:
835  //**Member variables****************************************************************************
836  MT& sm_;
837  //**********************************************************************************************
838 
839  //**Compile time checks*************************************************************************
845  //**********************************************************************************************
846 };
848 //*************************************************************************************************
849 
850 
851 
852 
853 //=================================================================================================
854 //
855 // GLOBAL OPERATORS
856 //
857 //=================================================================================================
858 
859 //*************************************************************************************************
867 template< typename MT // Type of the sparse matrix
868  , bool SO > // Storage order
869 inline void reset( SMatTransposer<MT,SO>& m )
870 {
871  m.reset();
872 }
874 //*************************************************************************************************
875 
876 
877 
878 
879 //=================================================================================================
880 //
881 // SUBMATRIXTRAIT SPECIALIZATIONS
882 //
883 //=================================================================================================
884 
885 //*************************************************************************************************
887 template< typename MT, bool SO >
888 struct SubmatrixTrait< SMatTransposer<MT,SO> >
889 {
891 };
893 //*************************************************************************************************
894 
895 } // namespace blaze
896 
897 #endif
MT OppositeType
Result type with opposite storage order for expression template evaluations.
Definition: SMatTransposer.h:77
MT::ConstReference ConstReference
Reference to a constant matrix value.
Definition: SMatTransposer.h:83
MT::ElementType ElementType
Resulting element type.
Definition: SMatTransposer.h:79
Constraint on the data type.
ConstIterator begin(size_t i) const
Returns an iterator to the first non-zero element of row/column i.
Definition: SMatTransposer.h:131
void reset(DynamicMatrix< Type, SO > &m)
Resetting the given dense matrix.
Definition: DynamicMatrix.h:4599
#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
Efficient implementation of a compressed matrix.The CompressedMatrix class template is the represent...
Definition: CompressedMatrix.h:199
#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
MT::ReturnType ReturnType
Return type for expression template evaluations.
Definition: SMatTransposer.h:80
void reset()
Resets the matrix elements.
Definition: SMatTransposer.h:243
SMatTransposer< MT, SO > This
Type of this SMatTransposer instance.
Definition: SMatTransposer.h:75
bool canAlias(const Other *alias) const
Returns whether the matrix can alias with the given address alias.
Definition: SMatTransposer.h:357
MT::ResultType TransposeType
Transpose type for expression template evaluations.
Definition: SMatTransposer.h:78
EnableIf< IsNumeric< Other >, SMatTransposer >::Type & operator*=(Other rhs)
Multiplication assignment operator for the multiplication between a matrix and a scalar value ( )...
Definition: SMatTransposer.h:192
Iterator insert(size_t i, size_t j, const ElementType &value)
Inserting an element into the sparse matrix.
Definition: SMatTransposer.h:261
ConstIterator cend(size_t i) const
Returns an iterator just past the last non-zero element of row/column i.
Definition: SMatTransposer.h:179
Base class for sparse matrices.The SparseMatrix class is a base class for all sparse matrix classes...
Definition: Forward.h:107
Header file for the SparseMatrix base class.
Constraint on the data type.
MT::Iterator Iterator
Iterator over non-constant elements.
Definition: SMatTransposer.h:84
bool isAliased(const Other *alias) const
Returns whether the matrix is aliased with the given address alias.
Definition: SMatTransposer.h:370
void reserve(size_t i, size_t nonzeros)
Setting the minimum capacity of a specific row/column of the sparse matrix.
Definition: SMatTransposer.h:295
#define BLAZE_CONSTRAINT_MUST_BE_COLUMN_MAJOR_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a column-major dense or sparse matri...
Definition: StorageOrder.h:161
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2412
size_t nonZeros(const Matrix< MT, SO > &m)
Returns the total number of non-zero elements in the matrix.
Definition: Matrix.h:224
void assign(const SparseMatrix< MT2, false > &rhs)
Implementation of the transpose assignment of a row-major sparse matrix.
Definition: SMatTransposer.h:388
MT::TransposeType ResultType
Result type for expression template evaluations.
Definition: SMatTransposer.h:76
ConstIterator end(size_t i) const
Returns an iterator just past the last non-zero element of row/column i.
Definition: SMatTransposer.h:163
CompressedMatrix< Type, false > TransposeType
Transpose type for expression template evaluations.
Definition: CompressedMatrix.h:2405
Constraints on the storage order of matrix types.
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2406
void append(size_t i, size_t j, const ElementType &value, bool check=false)
Appending an element to the specified row/column of the sparse matrix.
Definition: SMatTransposer.h:327
const Type & ConstReference
Reference to a constant matrix value.
Definition: CompressedMatrix.h:2410
size_t rows() const
Returns the current number of rows of the matrix.
Definition: SMatTransposer.h:223
Header file for the EnableIf class template.
Header file for the IsNumeric type trait.
MT::ConstIterator ConstIterator
Iterator over constant elements.
Definition: SMatTransposer.h:85
void reserve(size_t nonzeros)
Setting the minimum capacity of the sparse matrix.
Definition: SMatTransposer.h:276
Expression object for the transposition of a sparse matrix.The SMatTransposer class is a wrapper obje...
Definition: Forward.h:103
#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: StorageOrder.h:81
const Type & ReturnType
Return type for expression template evaluations.
Definition: CompressedMatrix.h:2407
Header file for run time assertion macros.
Header file for the submatrix trait.
SMatTransposer(MT &sm)
Constructor for the SMatTransposer class.
Definition: SMatTransposer.h:101
Substitution Failure Is Not An Error (SFINAE) class.The EnableIf class template is an auxiliary tool ...
Definition: EnableIf.h:184
MT & sm_
The sparse matrix operand.
Definition: SMatTransposer.h:456
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2411
ConstReference operator()(size_t i, size_t j) const
2D-access to the matrix elements.
Definition: SMatTransposer.h:113
ConstIterator cbegin(size_t i) const
Returns an iterator to the first non-zero element of row/column i.
Definition: SMatTransposer.h:147
const This & CompositeType
Data type for composite expression templates.
Definition: SMatTransposer.h:81
void assign(const SparseMatrix< MT2, true > &rhs)
Implementation of the transpose assignment of a column-major sparse matrix.
Definition: SMatTransposer.h:420
void finalize(size_t i)
Finalizing the element insertion of a row/column.
Definition: SMatTransposer.h:345
EnableIf< IsNumeric< Other >, SMatTransposer >::Type & operator/=(Other rhs)
Division assignment operator for the division of a matrix by a scalar value ( ).
Definition: SMatTransposer.h:209
This ResultType
Result type for expression template evaluations.
Definition: CompressedMatrix.h:2403
Header file for basic type definitions.
MatrixAccessProxy< This > Reference
Reference to a non-constant matrix value.
Definition: CompressedMatrix.h:2409
MT::Reference Reference
Reference to a non-constant matrix value.
Definition: SMatTransposer.h:82
#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
#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:79
size_t columns() const
Returns the current number of columns of the matrix.
Definition: SMatTransposer.h:233