Sparse.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_ADAPTORS_DIAGONALMATRIX_SPARSE_H_
36 #define _BLAZE_MATH_ADAPTORS_DIAGONALMATRIX_SPARSE_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <algorithm>
44 #include <stdexcept>
45 #include <vector>
56 #include <blaze/math/shims/Clear.h>
58 #include <blaze/math/shims/Move.h>
64 #include <blaze/util/Assert.h>
69 #include <blaze/util/DisableIf.h>
70 #include <blaze/util/EnableIf.h>
72 #include <blaze/util/Types.h>
73 
74 
75 namespace blaze {
76 
77 //=================================================================================================
78 //
79 // CLASS TEMPLATE SPECIALIZATION FOR SPARSE MATRICES
80 //
81 //=================================================================================================
82 
83 //*************************************************************************************************
91 template< typename MT // Type of the adapted sparse matrix
92  , bool SO > // Storage order of the adapted sparse matrix
93 class DiagonalMatrix<MT,SO,false>
94  : public SparseMatrix< DiagonalMatrix<MT,SO,false>, SO >
95 {
96  private:
97  //**Type definitions****************************************************************************
98  typedef typename MT::OppositeType OT;
99  typedef typename MT::TransposeType TT;
100  typedef typename MT::ElementType ET;
101  //**********************************************************************************************
102 
103  public:
104  //**Type definitions****************************************************************************
105  typedef DiagonalMatrix<MT,SO,false> This;
106  typedef This ResultType;
107  typedef DiagonalMatrix<OT,!SO,false> OppositeType;
108  typedef DiagonalMatrix<TT,!SO,false> TransposeType;
109  typedef ET ElementType;
110  typedef typename MT::ReturnType ReturnType;
111  typedef const This& CompositeType;
112  typedef DiagonalProxy<MT> Reference;
113  typedef typename MT::ConstReference ConstReference;
114  typedef typename MT::Iterator Iterator;
115  typedef typename MT::ConstIterator ConstIterator;
116  //**********************************************************************************************
117 
118  //**Rebind struct definition********************************************************************
121  template< typename ET > // Data type of the other matrix
122  struct Rebind {
124  typedef DiagonalMatrix< typename MT::template Rebind<ET>::Other > Other;
125  };
126  //**********************************************************************************************
127 
128  //**Compilation flags***************************************************************************
130  enum { smpAssignable = 0 };
131  //**********************************************************************************************
132 
133  //**Constructors********************************************************************************
136  explicit inline DiagonalMatrix();
137  explicit inline DiagonalMatrix( size_t n );
138  explicit inline DiagonalMatrix( size_t n, size_t nonzeros );
139  explicit inline DiagonalMatrix( size_t n, const std::vector<size_t>& nonzeros );
140 
141  inline DiagonalMatrix( const DiagonalMatrix& m );
142  template< typename MT2, bool SO2 > inline DiagonalMatrix( const Matrix<MT2,SO2>& m );
144  //**********************************************************************************************
145 
146  //**Destructor**********************************************************************************
147  // No explicitly declared destructor.
148  //**********************************************************************************************
149 
150  //**Data access functions***********************************************************************
153  inline Reference operator()( size_t i, size_t j );
154  inline ConstReference operator()( size_t i, size_t j ) const;
155  inline Iterator begin ( size_t i );
156  inline ConstIterator begin ( size_t i ) const;
157  inline ConstIterator cbegin( size_t i ) const;
158  inline Iterator end ( size_t i );
159  inline ConstIterator end ( size_t i ) const;
160  inline ConstIterator cend ( size_t i ) const;
162  //**********************************************************************************************
163 
164  //**Assignment operators************************************************************************
167  inline DiagonalMatrix& operator=( const DiagonalMatrix& rhs );
168 
169  template< typename MT2, bool SO2 >
170  inline typename DisableIf< IsComputation<MT2>, DiagonalMatrix& >::Type
171  operator=( const Matrix<MT2,SO2>& rhs );
172 
173  template< typename MT2, bool SO2 >
174  inline typename EnableIf< IsComputation<MT2>, DiagonalMatrix& >::Type
175  operator=( const Matrix<MT2,SO2>& rhs );
176 
177  template< typename MT2, bool SO2 >
178  inline typename DisableIf< IsComputation<MT2>, DiagonalMatrix& >::Type
179  operator+=( const Matrix<MT2,SO2>& rhs );
180 
181  template< typename MT2, bool SO2 >
182  inline typename EnableIf< IsComputation<MT2>, DiagonalMatrix& >::Type
183  operator+=( const Matrix<MT2,SO2>& rhs );
184 
185  template< typename MT2, bool SO2 >
186  inline typename DisableIf< IsComputation<MT2>, DiagonalMatrix& >::Type
187  operator-=( const Matrix<MT2,SO2>& rhs );
188 
189  template< typename MT2, bool SO2 >
190  inline typename EnableIf< IsComputation<MT2>, DiagonalMatrix& >::Type
191  operator-=( const Matrix<MT2,SO2>& rhs );
192 
193  template< typename MT2, bool SO2 >
194  inline DiagonalMatrix& operator*=( const Matrix<MT2,SO2>& rhs );
195 
196  template< typename Other >
197  inline typename EnableIf< IsNumeric<Other>, DiagonalMatrix >::Type&
198  operator*=( Other rhs );
199 
200  template< typename Other >
201  inline typename EnableIf< IsNumeric<Other>, DiagonalMatrix >::Type&
202  operator/=( Other rhs );
204  //**********************************************************************************************
205 
206  //**Utility functions***************************************************************************
209  inline size_t rows() const;
210  inline size_t columns() const;
211  inline size_t capacity() const;
212  inline size_t capacity( size_t i ) const;
213  inline size_t nonZeros() const;
214  inline size_t nonZeros( size_t i ) const;
215  inline void reset();
216  inline void reset( size_t i );
217  inline void clear();
218  inline Iterator set( size_t i, size_t j, const ElementType& value );
219  inline Iterator insert( size_t i, size_t j, const ElementType& value );
220  inline void erase( size_t i, size_t j );
221  inline Iterator erase( size_t i, Iterator pos );
222  inline Iterator erase( size_t i, Iterator first, Iterator last );
223  inline void resize ( size_t n, bool preserve=true );
224  inline void reserve( size_t nonzeros );
225  inline void reserve( size_t i, size_t nonzeros );
226  inline void trim();
227  inline void trim( size_t i );
228  template< typename Other > inline DiagonalMatrix& scale( const Other& scalar );
229  template< typename Other > inline DiagonalMatrix& scaleDiagonal( Other scale );
230  inline void swap( DiagonalMatrix& m ) /* throw() */;
232  //**********************************************************************************************
233 
234  //**Lookup functions****************************************************************************
237  inline Iterator find ( size_t i, size_t j );
238  inline ConstIterator find ( size_t i, size_t j ) const;
239  inline Iterator lowerBound( size_t i, size_t j );
240  inline ConstIterator lowerBound( size_t i, size_t j ) const;
241  inline Iterator upperBound( size_t i, size_t j );
242  inline ConstIterator upperBound( size_t i, size_t j ) const;
244  //**********************************************************************************************
245 
246  //**Low-level utility functions*****************************************************************
249  inline void append ( size_t i, size_t j, const ElementType& value, bool check=false );
250  inline void finalize( size_t i );
252  //**********************************************************************************************
253 
254  //**Expression template evaluation functions****************************************************
257  template< typename Other > inline bool canAlias ( const Other* alias ) const;
258  template< typename Other > inline bool isAliased( const Other* alias ) const;
259 
260  inline bool canSMPAssign() const;
262  //**********************************************************************************************
263 
264  private:
265  //**Utility functions***************************************************************************
268  inline void resetNonDiagonal();
270  //**********************************************************************************************
271 
272  //**Member variables****************************************************************************
275  MT matrix_;
276 
277  //**********************************************************************************************
278 
279  //**Friend declarations*************************************************************************
280  template< typename MT2, bool SO2, bool DF2 >
281  friend bool isDefault( const DiagonalMatrix<MT2,SO2,DF2>& m );
282 
283  template< typename MT2, bool SO2, bool DF2 >
284  friend MT2& derestrict( DiagonalMatrix<MT2,SO2,DF2>& m );
285  //**********************************************************************************************
286 
287  //**Compile time checks*************************************************************************
299  BLAZE_STATIC_ASSERT( IsResizable<MT>::value || IsSquare<MT>::value );
300  //**********************************************************************************************
301 };
303 //*************************************************************************************************
304 
305 
306 
307 
308 //=================================================================================================
309 //
310 // CONSTRUCTORS
311 //
312 //=================================================================================================
313 
314 //*************************************************************************************************
318 template< typename MT // Type of the adapted sparse matrix
319  , bool SO > // Storage order of the adapted sparse matrix
320 inline DiagonalMatrix<MT,SO,false>::DiagonalMatrix()
321  : matrix_() // The adapted sparse matrix
322 {
323  BLAZE_INTERNAL_ASSERT( isSquare( matrix_ ), "Non-square diagonal matrix detected" );
324 }
326 //*************************************************************************************************
327 
328 
329 //*************************************************************************************************
337 template< typename MT // Type of the adapted sparse matrix
338  , bool SO > // Storage order of the adapted sparse matrix
339 inline DiagonalMatrix<MT,SO,false>::DiagonalMatrix( size_t n )
340  : matrix_( n, n ) // The adapted sparse matrix
341 {
343 
344  BLAZE_INTERNAL_ASSERT( isSquare( matrix_ ), "Non-square diagonal matrix detected" );
345 }
347 //*************************************************************************************************
348 
349 
350 //*************************************************************************************************
359 template< typename MT // Type of the adapted sparse matrix
360  , bool SO > // Storage order of the adapted sparse matrix
361 inline DiagonalMatrix<MT,SO,false>::DiagonalMatrix( size_t n, size_t nonzeros )
362  : matrix_( n, n, nonzeros ) // The adapted sparse matrix
363 {
365 
366  BLAZE_INTERNAL_ASSERT( isSquare( matrix_ ), "Non-square diagonal matrix detected" );
367 }
369 //*************************************************************************************************
370 
371 
372 //*************************************************************************************************
383 template< typename MT // Type of the adapted sparse matrix
384  , bool SO > // Storage order of the adapted sparse matrix
385 inline DiagonalMatrix<MT,SO,false>::DiagonalMatrix( size_t n, const std::vector<size_t>& nonzeros )
386  : matrix_( n, n, nonzeros ) // The adapted sparse matrix
387 {
389 
390  BLAZE_INTERNAL_ASSERT( isSquare( matrix_ ), "Non-square diagonal matrix detected" );
391 }
393 //*************************************************************************************************
394 
395 
396 //*************************************************************************************************
402 template< typename MT // Type of the adapted sparse matrix
403  , bool SO > // Storage order of the adapted sparse matrix
404 inline DiagonalMatrix<MT,SO,false>::DiagonalMatrix( const DiagonalMatrix& m )
405  : matrix_( m.matrix_ ) // The adapted sparse matrix
406 {
407  BLAZE_INTERNAL_ASSERT( isSquare( matrix_ ), "Non-square diagonal matrix detected" );
408 }
410 //*************************************************************************************************
411 
412 
413 //*************************************************************************************************
423 template< typename MT // Type of the adapted sparse matrix
424  , bool SO > // Storage order of the adapted sparse matrix
425 template< typename MT2 // Type of the foreign matrix
426  , bool SO2 > // Storage order of the foreign matrix
427 inline DiagonalMatrix<MT,SO,false>::DiagonalMatrix( const Matrix<MT2,SO2>& m )
428  : matrix_( ~m ) // The adapted sparse matrix
429 {
430  if( !IsDiagonal<MT2>::value && !isDiagonal( matrix_ ) )
431  throw std::invalid_argument( "Invalid setup of diagonal matrix" );
432 
433  if( !IsDiagonal<MT2>::value )
434  resetNonDiagonal();
435 
436  BLAZE_INTERNAL_ASSERT( isSquare( matrix_ ), "Non-square diagonal matrix detected" );
437 }
439 //*************************************************************************************************
440 
441 
442 
443 
444 //=================================================================================================
445 //
446 // DATA ACCESS FUNCTIONS
447 //
448 //=================================================================================================
449 
450 //*************************************************************************************************
462 template< typename MT // Type of the adapted sparse matrix
463  , bool SO > // Storage order of the adapted sparse matrix
465  DiagonalMatrix<MT,SO,false>::operator()( size_t i, size_t j )
466 {
467  BLAZE_USER_ASSERT( i<rows() , "Invalid row access index" );
468  BLAZE_USER_ASSERT( j<columns(), "Invalid column access index" );
469 
470  return Reference( matrix_, i, j );
471 }
473 //*************************************************************************************************
474 
475 
476 //*************************************************************************************************
488 template< typename MT // Type of the adapted sparse matrix
489  , bool SO > // Storage order of the adapted sparse matrix
491  DiagonalMatrix<MT,SO,false>::operator()( size_t i, size_t j ) const
492 {
493  BLAZE_USER_ASSERT( i<rows() , "Invalid row access index" );
494  BLAZE_USER_ASSERT( j<columns(), "Invalid column access index" );
495 
496  return matrix_(i,j);
497 }
499 //*************************************************************************************************
500 
501 
502 //*************************************************************************************************
514 template< typename MT // Type of the adapted sparse matrix
515  , bool SO > // Storage order of the adapted sparse matrix
518 {
519  return matrix_.begin(i);
520 }
522 //*************************************************************************************************
523 
524 
525 //*************************************************************************************************
537 template< typename MT // Type of the adapted sparse matrix
538  , bool SO > // Storage order of the adapted sparse matrix
540  DiagonalMatrix<MT,SO,false>::begin( size_t i ) const
541 {
542  return matrix_.begin(i);
543 }
545 //*************************************************************************************************
546 
547 
548 //*************************************************************************************************
560 template< typename MT // Type of the adapted sparse matrix
561  , bool SO > // Storage order of the adapted sparse matrix
563  DiagonalMatrix<MT,SO,false>::cbegin( size_t i ) const
564 {
565  return matrix_.cbegin(i);
566 }
568 //*************************************************************************************************
569 
570 
571 //*************************************************************************************************
583 template< typename MT // Type of the adapted sparse matrix
584  , bool SO > // Storage order of the adapted sparse matrix
587 {
588  return matrix_.end(i);
589 }
591 //*************************************************************************************************
592 
593 
594 //*************************************************************************************************
606 template< typename MT // Type of the adapted sparse matrix
607  , bool SO > // Storage order of the adapted sparse matrix
609  DiagonalMatrix<MT,SO,false>::end( size_t i ) const
610 {
611  return matrix_.end(i);
612 }
614 //*************************************************************************************************
615 
616 
617 //*************************************************************************************************
629 template< typename MT // Type of the adapted sparse matrix
630  , bool SO > // Storage order of the adapted sparse matrix
632  DiagonalMatrix<MT,SO,false>::cend( size_t i ) const
633 {
634  return matrix_.cend(i);
635 }
637 //*************************************************************************************************
638 
639 
640 
641 
642 //=================================================================================================
643 //
644 // ASSIGNMENT OPERATORS
645 //
646 //=================================================================================================
647 
648 //*************************************************************************************************
658 template< typename MT // Type of the adapted sparse matrix
659  , bool SO > // Storage order of the adapted sparse matrix
660 inline DiagonalMatrix<MT,SO,false>&
661  DiagonalMatrix<MT,SO,false>::operator=( const DiagonalMatrix& rhs )
662 {
663  matrix_ = rhs.matrix_;
664 
665  return *this;
666 }
668 //*************************************************************************************************
669 
670 
671 //*************************************************************************************************
684 template< typename MT // Type of the adapted sparse matrix
685  , bool SO > // Storage order of the adapted sparse matrix
686 template< typename MT2 // Type of the right-hand side matrix
687  , bool SO2 > // Storage order of the right-hand side matrix
688 inline typename DisableIf< IsComputation<MT2>, DiagonalMatrix<MT,SO,false>& >::Type
689  DiagonalMatrix<MT,SO,false>::operator=( const Matrix<MT2,SO2>& rhs )
690 {
691  if( !IsDiagonal<MT2>::value && !isDiagonal( ~rhs ) )
692  throw std::invalid_argument( "Invalid assignment to diagonal matrix" );
693 
694  matrix_ = ~rhs;
695 
696  if( !IsDiagonal<MT2>::value )
697  resetNonDiagonal();
698 
699  return *this;
700 }
702 //*************************************************************************************************
703 
704 
705 //*************************************************************************************************
718 template< typename MT // Type of the adapted sparse matrix
719  , bool SO > // Storage order of the adapted sparse matrix
720 template< typename MT2 // Type of the right-hand side matrix
721  , bool SO2 > // Storage order of the right-hand side matrix
722 inline typename EnableIf< IsComputation<MT2>, DiagonalMatrix<MT,SO,false>& >::Type
723  DiagonalMatrix<MT,SO,false>::operator=( const Matrix<MT2,SO2>& rhs )
724 {
725  if( !IsSquare<MT2>::value && !isSquare( ~rhs ) )
726  throw std::invalid_argument( "Invalid assignment to diagonal matrix" );
727 
728  if( IsDiagonal<MT2>::value ) {
729  matrix_ = ~rhs;
730  }
731  else {
732  MT tmp( ~rhs );
733 
734  if( !isDiagonal( tmp ) )
735  throw std::invalid_argument( "Invalid assignment to diagonal matrix" );
736 
737  move( matrix_, tmp );
738  }
739 
740  if( !IsDiagonal<MT2>::value )
741  resetNonDiagonal();
742 
743  return *this;
744 }
746 //*************************************************************************************************
747 
748 
749 //*************************************************************************************************
762 template< typename MT // Type of the adapted sparse matrix
763  , bool SO > // Storage order of the adapted sparse matrix
764 template< typename MT2 // Type of the right-hand side matrix
765  , bool SO2 > // Storage order of the right-hand side matrix
766 inline typename DisableIf< IsComputation<MT2>, DiagonalMatrix<MT,SO,false>& >::Type
767  DiagonalMatrix<MT,SO,false>::operator+=( const Matrix<MT2,SO2>& rhs )
768 {
769  if( !IsDiagonal<MT2>::value && !isDiagonal( ~rhs ) )
770  throw std::invalid_argument( "Invalid assignment to diagonal matrix" );
771 
772  matrix_ += ~rhs;
773 
774  if( !IsDiagonal<MT2>::value )
775  resetNonDiagonal();
776 
777  return *this;
778 }
780 //*************************************************************************************************
781 
782 
783 //*************************************************************************************************
796 template< typename MT // Type of the adapted sparse matrix
797  , bool SO > // Storage order of the adapted sparse matrix
798 template< typename MT2 // Type of the right-hand side matrix
799  , bool SO2 > // Storage order of the right-hand side matrix
800 inline typename EnableIf< IsComputation<MT2>, DiagonalMatrix<MT,SO,false>& >::Type
801  DiagonalMatrix<MT,SO,false>::operator+=( const Matrix<MT2,SO2>& rhs )
802 {
803  if( !IsSquare<MT2>::value && !isSquare( ~rhs ) )
804  throw std::invalid_argument( "Invalid assignment to diagonal matrix" );
805 
806  if( IsDiagonal<MT2>::value ) {
807  matrix_ += ~rhs;
808  }
809  else {
810  typename MT2::ResultType tmp( ~rhs );
811 
812  if( !isDiagonal( tmp ) )
813  throw std::invalid_argument( "Invalid assignment to diagonal matrix" );
814 
815  matrix_ += tmp;
816  }
817 
818  if( !IsDiagonal<MT2>::value )
819  resetNonDiagonal();
820 
821  return *this;
822 }
824 //*************************************************************************************************
825 
826 
827 //*************************************************************************************************
840 template< typename MT // Type of the adapted sparse matrix
841  , bool SO > // Storage order of the adapted sparse matrix
842 template< typename MT2 // Type of the right-hand side matrix
843  , bool SO2 > // Storage order of the right-hand side matrix
844 inline typename DisableIf< IsComputation<MT2>, DiagonalMatrix<MT,SO,false>& >::Type
845  DiagonalMatrix<MT,SO,false>::operator-=( const Matrix<MT2,SO2>& rhs )
846 {
847  if( !IsDiagonal<MT2>::value && !isDiagonal( ~rhs ) )
848  throw std::invalid_argument( "Invalid assignment to diagonal matrix" );
849 
850  matrix_ -= ~rhs;
851 
852  if( !IsDiagonal<MT2>::value )
853  resetNonDiagonal();
854 
855  return *this;
856 }
858 //*************************************************************************************************
859 
860 
861 //*************************************************************************************************
874 template< typename MT // Type of the adapted sparse matrix
875  , bool SO > // Storage order of the adapted sparse matrix
876 template< typename MT2 // Type of the right-hand side matrix
877  , bool SO2 > // Storage order of the right-hand side matrix
878 inline typename EnableIf< IsComputation<MT2>, DiagonalMatrix<MT,SO,false>& >::Type
879  DiagonalMatrix<MT,SO,false>::operator-=( const Matrix<MT2,SO2>& rhs )
880 {
881  if( !IsSquare<MT2>::value && !isSquare( ~rhs ) )
882  throw std::invalid_argument( "Invalid assignment to diagonal matrix" );
883 
884  if( IsDiagonal<MT2>::value ) {
885  matrix_ -= ~rhs;
886  }
887  else {
888  typename MT2::ResultType tmp( ~rhs );
889 
890  if( !isDiagonal( tmp ) )
891  throw std::invalid_argument( "Invalid assignment to diagonal matrix" );
892 
893  matrix_ -= tmp;
894  }
895 
896  if( !IsDiagonal<MT2>::value )
897  resetNonDiagonal();
898 
899  return *this;
900 }
902 //*************************************************************************************************
903 
904 
905 //*************************************************************************************************
917 template< typename MT // Type of the adapted sparse matrix
918  , bool SO > // Storage order of the adapted sparse matrix
919 template< typename MT2 // Type of the right-hand side matrix
920  , bool SO2 > // Storage order of the right-hand side matrix
921 inline DiagonalMatrix<MT,SO,false>&
922  DiagonalMatrix<MT,SO,false>::operator*=( const Matrix<MT2,SO2>& rhs )
923 {
924  if( matrix_.rows() != (~rhs).columns() )
925  throw std::invalid_argument( "Invalid assignment to diagonal matrix" );
926 
927  MT tmp( matrix_ * ~rhs );
928 
929  if( !isDiagonal( tmp ) )
930  throw std::invalid_argument( "Invalid assignment to diagonal matrix" );
931 
932  move( matrix_, tmp );
933 
934  if( !IsDiagonal<MT2>::value )
935  resetNonDiagonal();
936 
937  return *this;
938 }
940 //*************************************************************************************************
941 
942 
943 //*************************************************************************************************
951 template< typename MT // Type of the adapted sparse matrix
952  , bool SO > // Storage order of the adapted sparse matrix
953 template< typename Other > // Data type of the right-hand side scalar
954 inline typename EnableIf< IsNumeric<Other>, DiagonalMatrix<MT,SO,false> >::Type&
955  DiagonalMatrix<MT,SO,false>::operator*=( Other rhs )
956 {
957  matrix_ *= rhs;
958  return *this;
959 }
960 //*************************************************************************************************
961 
962 
963 //*************************************************************************************************
971 template< typename MT // Type of the adapted sparse matrix
972  , bool SO > // Storage order of the adapted sparse matrix
973 template< typename Other > // Data type of the right-hand side scalar
974 inline typename EnableIf< IsNumeric<Other>, DiagonalMatrix<MT,SO,false> >::Type&
975  DiagonalMatrix<MT,SO,false>::operator/=( Other rhs )
976 {
977  BLAZE_USER_ASSERT( rhs != Other(0), "Division by zero detected" );
978 
979  matrix_ /= rhs;
980  return *this;
981 }
983 //*************************************************************************************************
984 
985 
986 
987 
988 //=================================================================================================
989 //
990 // UTILITY FUNCTIONS
991 //
992 //=================================================================================================
993 
994 //*************************************************************************************************
1000 template< typename MT // Type of the adapted sparse matrix
1001  , bool SO > // Storage order of the adapted sparse matrix
1002 inline size_t DiagonalMatrix<MT,SO,false>::rows() const
1003 {
1004  return matrix_.rows();
1005 }
1007 //*************************************************************************************************
1008 
1009 
1010 //*************************************************************************************************
1016 template< typename MT // Type of the adapted sparse matrix
1017  , bool SO > // Storage order of the adapted sparse matrix
1018 inline size_t DiagonalMatrix<MT,SO,false>::columns() const
1019 {
1020  return matrix_.columns();
1021 }
1023 //*************************************************************************************************
1024 
1025 
1026 //*************************************************************************************************
1032 template< typename MT // Type of the adapted sparse matrix
1033  , bool SO > // Storage order of the adapted sparse matrix
1034 inline size_t DiagonalMatrix<MT,SO,false>::capacity() const
1035 {
1036  return matrix_.capacity();
1037 }
1039 //*************************************************************************************************
1040 
1041 
1042 //*************************************************************************************************
1054 template< typename MT // Type of the adapted sparse matrix
1055  , bool SO > // Storage order of the adapted sparse matrix
1056 inline size_t DiagonalMatrix<MT,SO,false>::capacity( size_t i ) const
1057 {
1058  return matrix_.capacity(i);
1059 }
1061 //*************************************************************************************************
1062 
1063 
1064 //*************************************************************************************************
1070 template< typename MT // Type of the adapted sparse matrix
1071  , bool SO > // Storage order of the adapted sparse matrix
1072 inline size_t DiagonalMatrix<MT,SO,false>::nonZeros() const
1073 {
1074  return matrix_.nonZeros();
1075 }
1077 //*************************************************************************************************
1078 
1079 
1080 //*************************************************************************************************
1092 template< typename MT // Type of the adapted sparse matrix
1093  , bool SO > // Storage order of the adapted sparse matrix
1094 inline size_t DiagonalMatrix<MT,SO,false>::nonZeros( size_t i ) const
1095 {
1096  return matrix_.nonZeros(i);
1097 }
1099 //*************************************************************************************************
1100 
1101 
1102 //*************************************************************************************************
1108 template< typename MT // Type of the adapted sparse matrix
1109  , bool SO > // Storage order of the adapted sparse matrix
1111 {
1112  matrix_.reset();
1113 }
1115 //*************************************************************************************************
1116 
1117 
1118 //*************************************************************************************************
1131 template< typename MT // Type of the adapted sparse matrix
1132  , bool SO > // Storage order of the adapted sparse matrix
1133 inline void DiagonalMatrix<MT,SO,false>::reset( size_t i )
1134 {
1135  matrix_.reset( i );
1136 }
1138 //*************************************************************************************************
1139 
1140 
1141 //*************************************************************************************************
1149 template< typename MT // Type of the adapted sparse matrix
1150  , bool SO > // Storage order of the adapted sparse matrix
1152 {
1153  using blaze::clear;
1154 
1155  clear( matrix_ );
1156 }
1158 //*************************************************************************************************
1159 
1160 
1161 //*************************************************************************************************
1177 template< typename MT // Type of the adapted sparse matrix
1178  , bool SO > // Storage order of the adapted sparse matrix
1180  DiagonalMatrix<MT,SO,false>::set( size_t i, size_t j, const ElementType& value )
1181 {
1182  if( i < j )
1183  throw std::invalid_argument( "Invalid access to upper matrix element" );
1184 
1185  return matrix_.set( i, j, value );
1186 }
1188 //*************************************************************************************************
1189 
1190 
1191 //*************************************************************************************************
1208 template< typename MT // Type of the adapted sparse matrix
1209  , bool SO > // Storage order of the adapted sparse matrix
1211  DiagonalMatrix<MT,SO,false>::insert( size_t i, size_t j, const ElementType& value )
1212 {
1213  if( i < j )
1214  throw std::invalid_argument( "Invalid access to upper matrix element" );
1215 
1216  return matrix_.insert( i, j, value );
1217 }
1219 //*************************************************************************************************
1220 
1221 
1222 //*************************************************************************************************
1232 template< typename MT // Type of the adapted sparse matrix
1233  , bool SO > // Storage order of the adapted sparse matrix
1234 inline void DiagonalMatrix<MT,SO,false>::erase( size_t i, size_t j )
1235 {
1236  matrix_.erase( i, j );
1237 }
1239 //*************************************************************************************************
1240 
1241 
1242 //*************************************************************************************************
1254 template< typename MT // Type of the adapted sparse matrix
1255  , bool SO > // Storage order of the adapted sparse matrix
1257  DiagonalMatrix<MT,SO,false>::erase( size_t i, Iterator pos )
1258 {
1259  return matrix_.erase( i, pos );
1260 }
1262 //*************************************************************************************************
1263 
1264 
1265 //*************************************************************************************************
1279 template< typename MT // Type of the adapted sparse matrix
1280  , bool SO > // Storage order of the adapted sparse matrix
1282  DiagonalMatrix<MT,SO,false>::erase( size_t i, Iterator first, Iterator last )
1283 {
1284  return matrix_.erase( i, first, last );
1285 }
1287 //*************************************************************************************************
1288 
1289 
1290 //*************************************************************************************************
1305 template< typename MT // Type of the adapted sparse matrix
1306  , bool SO > // Storage order of the adapted sparse matrix
1307 void DiagonalMatrix<MT,SO,false>::resize( size_t n, bool preserve )
1308 {
1310 
1311  BLAZE_INTERNAL_ASSERT( isSquare( matrix_ ), "Non-square diagonal matrix detected" );
1312 
1313  matrix_.resize( n, n, preserve );
1314 }
1316 //*************************************************************************************************
1317 
1318 
1319 //*************************************************************************************************
1330 template< typename MT // Type of the adapted sparse matrix
1331  , bool SO > // Storage order of the adapted sparse matrix
1332 inline void DiagonalMatrix<MT,SO,false>::reserve( size_t nonzeros )
1333 {
1334  matrix_.reserve( nonzeros );
1335 }
1337 //*************************************************************************************************
1338 
1339 
1340 //*************************************************************************************************
1354 template< typename MT // Type of the adapted sparse matrix
1355  , bool SO > // Storage order of the adapted sparse matrix
1356 inline void DiagonalMatrix<MT,SO,false>::reserve( size_t i, size_t nonzeros )
1357 {
1358  matrix_.reserve( i, nonzeros );
1359 }
1361 //*************************************************************************************************
1362 
1363 
1364 //*************************************************************************************************
1375 template< typename MT // Type of the adapted sparse matrix
1376  , bool SO > // Storage order of the adapted sparse matrix
1377 inline void DiagonalMatrix<MT,SO,false>::trim()
1378 {
1379  matrix_.trim();
1380 }
1382 //*************************************************************************************************
1383 
1384 
1385 //*************************************************************************************************
1397 template< typename MT // Type of the adapted sparse matrix
1398  , bool SO > // Storage order of the adapted sparse matrix
1399 inline void DiagonalMatrix<MT,SO,false>::trim( size_t i )
1400 {
1401  matrix_.trim( i );
1402 }
1404 //*************************************************************************************************
1405 
1406 
1407 //*************************************************************************************************
1414 template< typename MT // Type of the adapted sparse matrix
1415  , bool SO > // Storage order of the adapted sparse matrix
1416 template< typename Other > // Data type of the scalar value
1417 inline DiagonalMatrix<MT,SO,false>&
1418  DiagonalMatrix<MT,SO,false>::scale( const Other& scalar )
1419 {
1420  matrix_.scale( scalar );
1421  return *this;
1422 }
1424 //*************************************************************************************************
1425 
1426 
1427 //*************************************************************************************************
1434 template< typename MT // Type of the adapted sparse matrix
1435  , bool SO > // Storage order of the adapted sparse matrix
1436 template< typename Other > // Data type of the scalar value
1437 inline DiagonalMatrix<MT,SO,false>&
1438  DiagonalMatrix<MT,SO,false>::scaleDiagonal( Other scalar )
1439 {
1440  matrix_.scaleDiagonal( scalar );
1441  return *this;
1442 }
1444 //*************************************************************************************************
1445 
1446 
1447 //*************************************************************************************************
1455 template< typename MT // Type of the adapted sparse matrix
1456  , bool SO > // Storage order of the adapted sparse matrix
1457 inline void DiagonalMatrix<MT,SO,false>::swap( DiagonalMatrix& m ) /* throw() */
1458 {
1459  using std::swap;
1460 
1461  swap( matrix_, m.matrix_ );
1462 }
1464 //*************************************************************************************************
1465 
1466 
1467 //*************************************************************************************************
1473 template< typename MT // Type of the adapted dense matrix
1474  , bool SO > // Storage order of the adapted dense matrix
1475 inline void DiagonalMatrix<MT,SO,false>::resetNonDiagonal()
1476 {
1477  for( size_t i=0UL; i<rows(); ++i ) {
1478  matrix_.erase( i, matrix_.begin( i ), matrix_.lowerBound( i, i ) );
1479  matrix_.erase( i, matrix_.upperBound( i, i ), matrix_.end( i ) );
1480  }
1481 }
1483 //*************************************************************************************************
1484 
1485 
1486 
1487 
1488 //=================================================================================================
1489 //
1490 // LOOKUP FUNCTIONS
1491 //
1492 //=================================================================================================
1493 
1494 //*************************************************************************************************
1510 template< typename MT // Type of the adapted sparse matrix
1511  , bool SO > // Storage order of the adapted sparse matrix
1513  DiagonalMatrix<MT,SO,false>::find( size_t i, size_t j )
1514 {
1515  return matrix_.find( i, j );
1516 }
1518 //*************************************************************************************************
1519 
1520 
1521 //*************************************************************************************************
1537 template< typename MT // Type of the adapted sparse matrix
1538  , bool SO > // Storage order of the adapted sparse matrix
1540  DiagonalMatrix<MT,SO,false>::find( size_t i, size_t j ) const
1541 {
1542  return matrix_.find( i, j );
1543 }
1545 //*************************************************************************************************
1546 
1547 
1548 //*************************************************************************************************
1564 template< typename MT // Type of the adapted sparse matrix
1565  , bool SO > // Storage order of the adapted sparse matrix
1567  DiagonalMatrix<MT,SO,false>::lowerBound( size_t i, size_t j )
1568 {
1569  return matrix_.lowerBound( i, j );
1570 }
1572 //*************************************************************************************************
1573 
1574 
1575 //*************************************************************************************************
1591 template< typename MT // Type of the adapted sparse matrix
1592  , bool SO > // Storage order of the adapted sparse matrix
1594  DiagonalMatrix<MT,SO,false>::lowerBound( size_t i, size_t j ) const
1595 {
1596  return matrix_.lowerBound( i, j );
1597 }
1599 //*************************************************************************************************
1600 
1601 
1602 //*************************************************************************************************
1618 template< typename MT // Type of the adapted sparse matrix
1619  , bool SO > // Storage order of the adapted sparse matrix
1621  DiagonalMatrix<MT,SO,false>::upperBound( size_t i, size_t j )
1622 {
1623  return matrix_.upperBound( i, j );
1624 }
1626 //*************************************************************************************************
1627 
1628 
1629 //*************************************************************************************************
1645 template< typename MT // Type of the adapted sparse matrix
1646  , bool SO > // Storage order of the adapted sparse matrix
1648  DiagonalMatrix<MT,SO,false>::upperBound( size_t i, size_t j ) const
1649 {
1650  return matrix_.upperBound( i, j );
1651 }
1653 //*************************************************************************************************
1654 
1655 
1656 
1657 
1658 //=================================================================================================
1659 //
1660 // LOW-LEVEL UTILITY FUNCTIONS
1661 //
1662 //=================================================================================================
1663 
1664 //*************************************************************************************************
1714 template< typename MT // Type of the adapted sparse matrix
1715  , bool SO > // Storage order of the adapted sparse matrix
1716 inline void DiagonalMatrix<MT,SO,false>::append( size_t i, size_t j, const ElementType& value, bool check )
1717 {
1718  if( i < j )
1719  throw std::invalid_argument( "Invalid access to upper matrix element" );
1720 
1721  matrix_.append( i, j, value, check );
1722 }
1724 //*************************************************************************************************
1725 
1726 
1727 //*************************************************************************************************
1741 template< typename MT // Type of the adapted sparse matrix
1742  , bool SO > // Storage order of the adapted sparse matrix
1743 inline void DiagonalMatrix<MT,SO,false>::finalize( size_t i )
1744 {
1745  matrix_.finalize( i );
1746 }
1748 //*************************************************************************************************
1749 
1750 
1751 
1752 
1753 //=================================================================================================
1754 //
1755 // EXPRESSION TEMPLATE EVALUATION FUNCTIONS
1756 //
1757 //=================================================================================================
1758 
1759 //*************************************************************************************************
1770 template< typename MT // Type of the adapted sparse matrix
1771  , bool SO > // Storage order of the adapted sparse matrix
1772 template< typename Other > // Data type of the foreign expression
1773 inline bool DiagonalMatrix<MT,SO,false>::canAlias( const Other* alias ) const
1774 {
1775  return matrix_.canAlias( alias );
1776 }
1778 //*************************************************************************************************
1779 
1780 
1781 //*************************************************************************************************
1792 template< typename MT // Type of the adapted sparse matrix
1793  , bool SO > // Storage order of the adapted sparse matrix
1794 template< typename Other > // Data type of the foreign expression
1795 inline bool DiagonalMatrix<MT,SO,false>::isAliased( const Other* alias ) const
1796 {
1797  return matrix_.isAliased( alias );
1798 }
1800 //*************************************************************************************************
1801 
1802 
1803 //*************************************************************************************************
1814 template< typename MT // Type of the adapted sparse matrix
1815  , bool SO > // Storage order of the adapted sparse matrix
1816 inline bool DiagonalMatrix<MT,SO,false>::canSMPAssign() const
1817 {
1818  return matrix_.canSMPAssign();
1819 }
1821 //*************************************************************************************************
1822 
1823 } // namespace blaze
1824 
1825 #endif
#define BLAZE_CONSTRAINT_MUST_NOT_BE_CONST(T)
Constraint on the data type.In case the given data type is a const-qualified type, a compilation error is created.
Definition: Const.h:116
#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
bool isDiagonal(const DenseMatrix< MT, SO > &dm)
Checks if the give dense matrix is diagonal.
Definition: DenseMatrix.h:1431
Header file for basic type definitions.
BLAZE_ALWAYS_INLINE bool isSquare(const Matrix< MT, SO > &matrix)
Checks if the given matrix is a square matrix.
Definition: Matrix.h:902
BLAZE_ALWAYS_INLINE MT::ConstIterator cbegin(const Matrix< MT, SO > &matrix, size_t i)
Returns an iterator to the first element of row/column i.
Definition: Matrix.h:237
void move(CompressedMatrix< Type, SO > &dst, CompressedMatrix< Type, SO > &src)
Moving the contents of one compressed matrix to another.
Definition: CompressedMatrix.h:4825
BLAZE_ALWAYS_INLINE MT::ConstIterator cend(const Matrix< MT, SO > &matrix, size_t i)
Returns an iterator just past the last element of row/column i.
Definition: Matrix.h:300
BLAZE_ALWAYS_INLINE MT::Iterator end(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator just past the last element of row/column i.
Definition: Matrix.h:258
#define BLAZE_CONSTRAINT_MUST_BE_MATRIX_WITH_STORAGE_ORDER(T, SO)
Constraint on the data type.In case the given data type T is not a dense or sparse matrix type and in...
Definition: StorageOrder.h:242
Header file for the IsDiagonal type trait.
Header file for the implementation of the base template of the DiagonalMatrix.
Constraint on the data type.
Constraint on the data type.
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:821
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2507
BLAZE_ALWAYS_INLINE size_t capacity(const Matrix< MT, SO > &matrix)
Returns the maximum capacity of the matrix.
Definition: Matrix.h:348
BLAZE_ALWAYS_INLINE size_t rows(const Matrix< MT, SO > &matrix)
Returns the current number of rows of the matrix.
Definition: Matrix.h:316
void clear(CompressedMatrix< Type, SO > &m)
Clearing the given compressed matrix.
Definition: CompressedMatrix.h:4762
CompressedMatrix< Type, true > This
Type of this CompressedMatrix instance.
Definition: CompressedMatrix.h:2501
#define BLAZE_CONSTRAINT_MUST_NOT_BE_VOLATILE(T)
Constraint on the data type.In case the given data type is a volatile-qualified type, a compilation error is created.
Definition: Volatile.h:116
BLAZE_ALWAYS_INLINE size_t nonZeros(const Matrix< MT, SO > &matrix)
Returns the total number of non-zero elements in the matrix.
Definition: Matrix.h:386
Constraint on the data type.
CompressedMatrix< Type, false > OppositeType
Result type with opposite storage order for expression template evaluations.
Definition: CompressedMatrix.h:2503
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:861
Constraint on the data type.
Header file for the SparseMatrix base class.
Header file for utility functions for sparse matrices.
Header file for the IsSquare type trait.
Constraint on the data type.
Header file for the DisableIf class template.
Header file for the DiagonalProxy class.
Header file for the clear shim.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
void swap(CompressedMatrix< Type, SO > &a, CompressedMatrix< Type, SO > &b)
Swapping the contents of two compressed matrices.
Definition: CompressedMatrix.h:4807
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2511
#define BLAZE_CONSTRAINT_MUST_NOT_BE_POINTER_TYPE(T)
Constraint on the data type.In case the given data type T is not a pointer type, a compilation error ...
Definition: Pointer.h:116
Constraint on the data type.
CompressedMatrix< Type, false > TransposeType
Transpose type for expression template evaluations.
Definition: CompressedMatrix.h:2504
Constraints on the storage order of matrix types.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_UPPER_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is a upper triangular matrix type...
Definition: Upper.h:118
BLAZE_ALWAYS_INLINE MT::Iterator begin(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator to the first element of row/column i.
Definition: Matrix.h:195
BLAZE_ALWAYS_INLINE void resize(Matrix< MT, SO > &matrix, size_t rows, size_t columns, bool preserve=true)
Changing the size of the matrix.
Definition: Matrix.h:535
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2505
const Type & ConstReference
Reference to a constant matrix value.
Definition: CompressedMatrix.h:2509
Header file for the EnableIf class template.
void clear(const DiagonalProxy< MT > &proxy)
Clearing the represented element.
Definition: DiagonalProxy.h:841
Header file for the IsNumeric type trait.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_SYMMETRIC_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is a symmetric matrix type, a compilation error is created.
Definition: Symmetric.h:116
const Type & ReturnType
Return type for expression template evaluations.
Definition: CompressedMatrix.h:2506
Header file for run time assertion macros.
Constraint on the data type.
Constraint on the data type.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_LOWER_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is a lower triangular matrix type...
Definition: Lower.h:118
#define BLAZE_CONSTRAINT_MUST_NOT_BE_REFERENCE_TYPE(T)
Constraint on the data type.In case the given data type T is not a reference type, a compilation error is created.
Definition: Reference.h:116
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2510
Header file for the isDefault shim.
Constraint on the data type.
Constraint on the data type.
void swap(DiagonalMatrix< MT, SO, DF > &a, DiagonalMatrix< MT, SO, DF > &b)
Swapping the contents of two matrices.
Definition: DiagonalMatrix.h:200
BLAZE_ALWAYS_INLINE EnableIf< And< IsIntegral< T >, HasSize< T, 2UL > >, sse_int16_t >::Type set(T value)
Sets all values in the vector to the given 2-byte integral value.
Definition: Set.h:73
Header file for the move shim.
#define BLAZE_CONSTRAINT_MUST_BE_RESIZABLE(T)
Constraint on the data type.In case the given data type T is not resizable, i.e. does not have a 'res...
Definition: Resizable.h:79
Header file for the IsComputation type trait class.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_EXPRESSION_TYPE(T)
Constraint on the data type.In case the given data type T is an expression (i.e. a type derived from ...
Definition: Expression.h:118
This ResultType
Result type for expression template evaluations.
Definition: CompressedMatrix.h:2502
BLAZE_ALWAYS_INLINE size_t columns(const Matrix< MT, SO > &matrix)
Returns the current number of columns of the matrix.
Definition: Matrix.h:332
MatrixAccessProxy< This > Reference
Reference to a non-constant matrix value.
Definition: CompressedMatrix.h:2508
#define BLAZE_STATIC_ASSERT(expr)
Compile time assertion macro.In case of an invalid compile time expression, a compilation error is cr...
Definition: StaticAssert.h:143
Header file for the IsResizable type trait.
#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