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>
48 #include <blaze/util/Assert.h>
49 #include <blaze/util/EnableIf.h>
50 #include <blaze/util/Types.h>
52 
53 
54 namespace blaze {
55 
56 //=================================================================================================
57 //
58 // CLASS SMATTRANSPOSER
59 //
60 //=================================================================================================
61 
62 //*************************************************************************************************
68 template< typename MT // Type of the sparse matrix
69  , bool SO > // Storage order
70 class SMatTransposer : public SparseMatrix< SMatTransposer<MT,SO>, SO >
71 {
72  public:
73  //**Type definitions****************************************************************************
75  typedef typename MT::TransposeType ResultType;
76  typedef MT OppositeType;
77  typedef typename MT::ResultType TransposeType;
78  typedef typename MT::ElementType ElementType;
79  typedef typename MT::ReturnType ReturnType;
80  typedef const This& CompositeType;
81  typedef typename MT::Reference Reference;
83  typedef typename MT::Iterator Iterator;
84  typedef typename MT::ConstIterator ConstIterator;
85  //**********************************************************************************************
86 
87  //**Constructor*********************************************************************************
92  explicit inline SMatTransposer( MT& sm )
93  : sm_( sm ) // The sparse matrix operand
94  {}
95  //**********************************************************************************************
96 
97  //**Access operator*****************************************************************************
104  inline Reference operator()( size_t i, size_t j ) const {
105  BLAZE_INTERNAL_ASSERT( i < sm_.columns(), "Invalid row access index" );
106  BLAZE_INTERNAL_ASSERT( j < sm_.row() , "Invalid column access index" );
107  return sm_(j,i);
108  }
109  //**********************************************************************************************
110 
111  //**Begin function******************************************************************************
122  inline ConstIterator begin( size_t i ) const {
123  return ConstIterator( sm_.begin(i) );
124  }
125  //**********************************************************************************************
126 
127  //**End function********************************************************************************
138  inline ConstIterator end( size_t i ) const {
139  return ConstIterator( sm_.end(i) );
140  }
141  //**********************************************************************************************
142 
143  //**Multiplication assignment operator**********************************************************
150  template< typename Other > // Data type of the right-hand side scalar
151  inline typename EnableIf< IsNumeric<Other>, SMatTransposer >::Type& operator*=( Other rhs )
152  {
153  (~sm_) *= rhs;
154  return *this;
155  }
156  //**********************************************************************************************
157 
158  //**Division assignment operator****************************************************************
167  template< typename Other > // Data type of the right-hand side scalar
168  inline typename EnableIf< IsNumeric<Other>, SMatTransposer >::Type& operator/=( Other rhs )
169  {
170  BLAZE_USER_ASSERT( rhs != Other(0), "Division by zero detected" );
171 
172  (~sm_) /= rhs;
173  return *this;
174  }
175  //**********************************************************************************************
176 
177  //**Rows function*******************************************************************************
182  inline size_t rows() const {
183  return sm_.columns();
184  }
185  //**********************************************************************************************
186 
187  //**Columns function****************************************************************************
192  inline size_t columns() const {
193  return sm_.rows();
194  }
195  //**********************************************************************************************
196 
197  //**Reset function******************************************************************************
202  inline void reset() {
203  return sm_.reset();
204  }
205  //**********************************************************************************************
206 
207  //**Insert function*****************************************************************************
220  inline Iterator insert( size_t i, size_t j, const ElementType& value ) {
221  return sm_.insert( j, i, value );
222  }
223  //**********************************************************************************************
224 
225  //**Reserve function****************************************************************************
235  inline void reserve( size_t nonzeros ) {
236  sm_.reserve( nonzeros );
237  }
238  //**********************************************************************************************
239 
240  //**Reserve function****************************************************************************
254  inline void reserve( size_t i, size_t nonzeros ) {
255  sm_.reserve( i, nonzeros );
256  }
257  //**********************************************************************************************
258 
259  //**Append function*****************************************************************************
286  inline void append( size_t i, size_t j, const ElementType& value, bool check=false ) {
287  sm_.append( j, i, value, check );
288  }
289  //**********************************************************************************************
290 
291  //**Finalize function***************************************************************************
304  inline void finalize( size_t i ) {
305  sm_.finalize( i );
306  }
307  //**********************************************************************************************
308 
309  //**********************************************************************************************
315  template< typename Other > // Data type of the foreign expression
316  inline bool canAlias( const Other* alias ) const
317  {
318  return sm_.canAlias( alias );
319  }
320  //**********************************************************************************************
321 
322  //**********************************************************************************************
328  template< typename Other > // Data type of the foreign expression
329  inline bool isAliased( const Other* alias ) const
330  {
331  return sm_.isAliased( alias );
332  }
333  //**********************************************************************************************
334 
335  //**Transpose assignment of row-major sparse matrices*******************************************
346  template< typename MT2 > // Type of the right-hand side sparse matrix
347  inline void assign( const SparseMatrix<MT2,false>& rhs )
348  {
350 
351  BLAZE_INTERNAL_ASSERT( sm_.columns() == (~rhs).rows() , "Invalid number of rows" );
352  BLAZE_INTERNAL_ASSERT( sm_.rows() == (~rhs).columns() , "Invalid number of columns" );
353  BLAZE_INTERNAL_ASSERT( sm_.capacity() >= (~rhs).nonZeros(), "Capacity not sufficient" );
354 
355  typedef typename MT2::ConstIterator RhsIterator;
356 
357  const size_t m( rows() );
358 
359  for( size_t i=0UL; i<m; ++i ) {
360  for( RhsIterator element=(~rhs).begin(i); element!=(~rhs).end(i); ++element )
361  sm_.append( element->index(), i, element->value() );
362  finalize( i );
363  }
364  }
365  //**********************************************************************************************
366 
367  //**Transpose assignment of column-major sparse matrices****************************************
378  template< typename MT2 > // Type of the right-hand side sparse matrix
379  inline void assign( const SparseMatrix<MT2,true>& rhs )
380  {
382 
383  BLAZE_INTERNAL_ASSERT( sm_.columns() == (~rhs).rows() , "Invalid number of rows" );
384  BLAZE_INTERNAL_ASSERT( sm_.rows() == (~rhs).columns() , "Invalid number of columns" );
385  BLAZE_INTERNAL_ASSERT( sm_.capacity() >= (~rhs).nonZeros(), "Capacity not sufficient" );
386 
387  typedef typename MT2::ConstIterator RhsIterator;
388 
389  const size_t m( rows() );
390  const size_t n( columns() );
391 
392  // Counting the number of elements per row
393  std::vector<size_t> rowLengths( m, 0UL );
394  for( size_t j=0UL; j<n; ++j ) {
395  for( RhsIterator element=(~rhs).begin(j); element!=(~rhs).end(j); ++element )
396  ++rowLengths[element->index()];
397  }
398 
399  // Resizing the sparse matrix
400  for( size_t i=0UL; i<m; ++i ) {
401  sm_.reserve( i, rowLengths[i] );
402  }
403 
404  // Appending the elements to the rows of the sparse matrix
405  for( size_t j=0UL; j<n; ++j ) {
406  for( RhsIterator element=(~rhs).begin(j); element!=(~rhs).end(j); ++element ) {
407  sm_.append( j, element->index(), element->value() );
408  }
409  }
410  }
411  //**********************************************************************************************
412 
413  private:
414  //**Member variables****************************************************************************
415  MT& sm_;
416  //**********************************************************************************************
417 
418  //**Compile time checks*************************************************************************
423  //**********************************************************************************************
424 };
425 //*************************************************************************************************
426 
427 
428 
429 
430 //=================================================================================================
431 //
432 // CLASS TEMPLATE SPECIALIZATION FOR ROW-MAJOR MATRICES
433 //
434 //=================================================================================================
435 
436 //*************************************************************************************************
444 template< typename MT > // Type of the sparse matrix
445 class SMatTransposer<MT,true> : public SparseMatrix< SMatTransposer<MT,true>, true >
446 {
447  public:
448  //**Type definitions****************************************************************************
449  typedef SMatTransposer<MT,true> This;
450  typedef typename MT::TransposeType ResultType;
451  typedef MT OppositeType;
452  typedef typename MT::ResultType TransposeType;
453  typedef typename MT::ElementType ElementType;
454  typedef typename MT::ReturnType ReturnType;
455  typedef const This& CompositeType;
456  typedef typename MT::Reference Reference;
457  typedef typename MT::ConstReference ConstReference;
458  typedef typename MT::Iterator Iterator;
459  typedef typename MT::ConstIterator ConstIterator;
460  //**********************************************************************************************
461 
462  //**Constructor*********************************************************************************
467  explicit inline SMatTransposer( MT& sm )
468  : sm_( sm ) // The sparse matrix operand
469  {}
470  //**********************************************************************************************
471 
472  //**Access operator*****************************************************************************
479  inline Reference operator()( size_t i, size_t j ) const {
480  BLAZE_INTERNAL_ASSERT( i < sm_.columns(), "Invalid row access index" );
481  BLAZE_INTERNAL_ASSERT( j < sm_.row() , "Invalid column access index" );
482  return sm_(j,i);
483  }
484  //**********************************************************************************************
485 
486  //**Begin function******************************************************************************
492  inline ConstIterator begin( size_t j ) const {
493  return ConstIterator( sm_.begin(j) );
494  }
495  //**********************************************************************************************
496 
497  //**End function********************************************************************************
503  inline ConstIterator end( size_t j ) const {
504  return ConstIterator( sm_.end(j) );
505  }
506  //**********************************************************************************************
507 
508  //**Rows function*******************************************************************************
513  inline size_t rows() const {
514  return sm_.columns();
515  }
516  //**********************************************************************************************
517 
518  //**Columns function****************************************************************************
523  inline size_t columns() const {
524  return sm_.rows();
525  }
526  //**********************************************************************************************
527 
528  //**Reset function******************************************************************************
533  inline void reset() {
534  return sm_.reset();
535  }
536  //**********************************************************************************************
537 
538  //**Insert function*****************************************************************************
551  inline Iterator insert( size_t i, size_t j, const ElementType& value ) {
552  return sm_.insert( j, i, value );
553  }
554  //**********************************************************************************************
555 
556  //**Reserve function****************************************************************************
566  inline void reserve( size_t nonzeros ) {
567  sm_.reserve( nonzeros );
568  }
569  //**********************************************************************************************
570 
571  //**Reserve function****************************************************************************
582  inline void reserve( size_t i, size_t nonzeros ) {
583  sm_.reserve( i, nonzeros );
584  }
585  //**********************************************************************************************
586 
587  //**Append function*****************************************************************************
614  void append( size_t i, size_t j, const ElementType& value, bool check=false ) {
615  sm_.append( j, i, value, check );
616  }
617  //**********************************************************************************************
618 
619  //**Finalize function***************************************************************************
632  inline void finalize( size_t j ) {
633  sm_.finalize( j );
634  }
635  //**********************************************************************************************
636 
637  //**********************************************************************************************
643  template< typename Other > // Data type of the foreign expression
644  inline bool isAliased( const Other* alias ) const
645  {
646  return sm_.isAliased( alias );
647  }
648  //**********************************************************************************************
649 
650  //**Transpose assignment of row-major sparse matrices*******************************************
661  template< typename MT2 > // Type of the right-hand side sparse matrix
662  inline void assign( const SparseMatrix<MT2,false>& rhs )
663  {
665 
666  BLAZE_INTERNAL_ASSERT( sm_.columns() == (~rhs).rows() , "Invalid number of rows" );
667  BLAZE_INTERNAL_ASSERT( sm_.rows() == (~rhs).columns() , "Invalid number of columns" );
668  BLAZE_INTERNAL_ASSERT( sm_.capacity() >= (~rhs).nonZeros(), "Capacity not sufficient" );
669 
670  typedef typename MT2::ConstIterator RhsIterator;
671 
672  const size_t m( rows() );
673  const size_t n( columns() );
674 
675  // Counting the number of elements per row
676  std::vector<size_t> columnLengths( n, 0UL );
677  for( size_t i=0UL; i<m; ++i ) {
678  for( RhsIterator element=(~rhs).begin(i); element!=(~rhs).end(i); ++element )
679  ++columnLengths[element->index()];
680  }
681 
682  // Resizing the sparse matrix
683  for( size_t j=0UL; j<n; ++j ) {
684  sm_.reserve( j, columnLengths[j] );
685  }
686 
687  // Appending the elements to the columns of the sparse matrix
688  for( size_t i=0UL; i<m; ++i ) {
689  for( RhsIterator element=(~rhs).begin(i); element!=(~rhs).end(i); ++element ) {
690  sm_.append( element->index(), i, element->value() );
691  }
692  }
693  }
694  //**********************************************************************************************
695 
696  //**Transpose assignment of column-major sparse matrices****************************************
707  template< typename MT2 > // Type of the right-hand side sparse matrix
708  inline void assign( const SparseMatrix<MT2,true>& rhs )
709  {
711 
712  BLAZE_INTERNAL_ASSERT( sm_.columns() == (~rhs).rows() , "Invalid number of rows" );
713  BLAZE_INTERNAL_ASSERT( sm_.rows() == (~rhs).columns() , "Invalid number of columns" );
714  BLAZE_INTERNAL_ASSERT( sm_.capacity() >= (~rhs).nonZeros(), "Capacity not sufficient" );
715 
716  typedef typename MT2::ConstIterator RhsIterator;
717 
718  const size_t n( columns() );
719 
720  for( size_t j=0UL; j<n; ++j ) {
721  for( RhsIterator element=(~rhs).begin(j); element!=(~rhs).end(j); ++element )
722  sm_.append( j, element->index(), element->value() );
723  finalize( j );
724  }
725  }
726  //**********************************************************************************************
727 
728  private:
729  //**Member variables****************************************************************************
730  MT& sm_;
731  //**********************************************************************************************
732 
733  //**Compile time checks*************************************************************************
738  //**********************************************************************************************
739 };
741 //*************************************************************************************************
742 
743 
744 
745 
746 //=================================================================================================
747 //
748 // GLOBAL OPERATORS
749 //
750 //=================================================================================================
751 
752 //*************************************************************************************************
760 template< typename MT // Type of the sparse matrix
761  , bool SO > // Storage order
762 inline void reset( SMatTransposer<MT,SO>& m )
763 {
764  m.reset();
765 }
767 //*************************************************************************************************
768 
769 
770 
771 
772 //=================================================================================================
773 //
774 // SUBMATRIXTRAIT SPECIALIZATIONS
775 //
776 //=================================================================================================
777 
778 //*************************************************************************************************
780 template< typename MT, bool SO >
781 struct SubmatrixTrait< SMatTransposer<MT,SO> >
782 {
784 };
786 //*************************************************************************************************
787 
788 } // namespace blaze
789 
790 #endif
MT OppositeType
Result type with opposite storage order for expression template evaluations.
Definition: SMatTransposer.h:76
MT::ConstReference ConstReference
Reference to a constant matrix value.
Definition: SMatTransposer.h:82
MT::ElementType ElementType
Resulting element type.
Definition: SMatTransposer.h:78
ConstIterator begin(size_t i) const
Returns an iterator to the first non-zero element of row/column i.
Definition: SMatTransposer.h:122
void reset(DynamicMatrix< Type, SO > &m)
Resetting the given dense matrix.
Definition: DynamicMatrix.h:4512
#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:196
MT::ReturnType ReturnType
Return type for expression template evaluations.
Definition: SMatTransposer.h:79
void reset()
Resets the matrix elements.
Definition: SMatTransposer.h:202
SMatTransposer< MT, SO > This
Type of this SMatTransposer instance.
Definition: SMatTransposer.h:74
bool canAlias(const Other *alias) const
Returns whether the matrix can alias with the given address alias.
Definition: SMatTransposer.h:316
MT::ResultType TransposeType
Transpose type for expression template evaluations.
Definition: SMatTransposer.h:77
EnableIf< IsNumeric< Other >, SMatTransposer >::Type & operator*=(Other rhs)
Multiplication assignment operator for the multiplication between a matrix and a scalar value ( )...
Definition: SMatTransposer.h:151
Iterator insert(size_t i, size_t j, const ElementType &value)
Inserting an element into the sparse matrix.
Definition: SMatTransposer.h:220
Base class for sparse matrices.The SparseMatrix class is a base class for all sparse matrix classes...
Definition: Forward.h:104
Header file for the SparseMatrix base class.
Constraint on the data type.
MT::Iterator Iterator
Iterator over non-constant elements.
Definition: SMatTransposer.h:83
bool isAliased(const Other *alias) const
Returns whether the matrix is aliased with the given address alias.
Definition: SMatTransposer.h:329
void reserve(size_t i, size_t nonzeros)
Setting the minimum capacity of a specific row/column of the sparse matrix.
Definition: SMatTransposer.h:254
#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:2379
void assign(const SparseMatrix< MT2, false > &rhs)
Implementation of the transpose assignment of a row-major sparse matrix.
Definition: SMatTransposer.h:347
MT::TransposeType ResultType
Result type for expression template evaluations.
Definition: SMatTransposer.h:75
ConstIterator end(size_t i) const
Returns an iterator just past the last non-zero element of row/column i.
Definition: SMatTransposer.h:138
CompressedMatrix< Type, false > TransposeType
Transpose type for expression template evaluations.
Definition: CompressedMatrix.h:2372
Constraints on the storage order of matrix types.
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2373
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:286
const Type & ConstReference
Reference to a constant matrix value.
Definition: CompressedMatrix.h:2377
size_t rows() const
Returns the current number of rows of the matrix.
Definition: SMatTransposer.h:182
Header file for the EnableIf class template.
ColumnIterator< const MT > ConstIterator
Iterator over constant elements.
Definition: DenseColumn.h:1972
Header file for the IsNumeric type trait.
MT::ConstIterator ConstIterator
Iterator over constant elements.
Definition: SMatTransposer.h:84
void reserve(size_t nonzeros)
Setting the minimum capacity of the sparse matrix.
Definition: SMatTransposer.h:235
Expression object for the transposition of a sparse matrix.The SMatTransposer class is a wrapper obje...
Definition: Forward.h:100
#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:2374
Header file for run time assertion macros.
Header file for the submatrix trait.
SMatTransposer(MT &sm)
Constructor for the SMatTransposer class.
Definition: SMatTransposer.h:92
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:415
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2378
Reference operator()(size_t i, size_t j) const
2D-access to the matrix elements.
Definition: SMatTransposer.h:104
const This & CompositeType
Data type for composite expression templates.
Definition: SMatTransposer.h:80
void assign(const SparseMatrix< MT2, true > &rhs)
Implementation of the transpose assignment of a column-major sparse matrix.
Definition: SMatTransposer.h:379
void finalize(size_t i)
Finalizing the element insertion of a row/column.
Definition: SMatTransposer.h:304
EnableIf< IsNumeric< Other >, SMatTransposer >::Type & operator/=(Other rhs)
Division assignment operator for the division of a matrix by a scalar value ( ).
Definition: SMatTransposer.h:168
This ResultType
Result type for expression template evaluations.
Definition: CompressedMatrix.h:2370
Header file for basic type definitions.
MatrixAccessProxy< This > Reference
Reference to a non-constant matrix value.
Definition: CompressedMatrix.h:2376
MT::Reference Reference
Reference to a non-constant matrix value.
Definition: SMatTransposer.h:81
SelectType< useConst, ConstIterator, ColumnIterator< MT > >::Type Iterator
Iterator over non-constant elements.
Definition: DenseColumn.h:1980
#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:192