All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SparseColumn.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_VIEWS_SPARSECOLUMN_H_
36 #define _BLAZE_MATH_VIEWS_SPARSECOLUMN_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <iterator>
44 #include <stdexcept>
55 #include <blaze/math/Functions.h>
57 #include <blaze/math/shims/Reset.h>
67 #include <blaze/util/Assert.h>
68 #include <blaze/util/DisableIf.h>
69 #include <blaze/util/EnableIf.h>
71 #include <blaze/util/mpl/If.h>
72 #include <blaze/util/mpl/Or.h>
73 #include <blaze/util/SelectType.h>
74 #include <blaze/util/Types.h>
78 #include <blaze/util/Unused.h>
79 
80 
81 namespace blaze {
82 
83 //=================================================================================================
84 //
85 // CLASS DEFINITION
86 //
87 //=================================================================================================
88 
89 //*************************************************************************************************
339 template< typename MT // Type of the sparse matrix
340  , bool SO = IsColumnMajorMatrix<MT>::value > // Storage order
341 class SparseColumn : public SparseVector< SparseColumn<MT,SO>, false >
342  , private Column
343 {
344  private:
345  //**Type definitions****************************************************************************
347  typedef typename SelectType< IsExpression<MT>::value, MT, MT& >::Type Operand;
348  //**********************************************************************************************
349 
350  //**********************************************************************************************
352 
358  enum { useConst = IsConst<MT>::value };
359  //**********************************************************************************************
360 
361  public:
362  //**Type definitions****************************************************************************
366  typedef typename MT::ElementType ElementType;
367  typedef typename MT::ReturnType ReturnType;
368  typedef const SparseColumn& CompositeType;
369 
372 
375 
378 
381  //**********************************************************************************************
382 
383  //**Compilation flags***************************************************************************
385  enum { smpAssignable = 0 };
386  //**********************************************************************************************
387 
388  //**Constructors********************************************************************************
391  explicit inline SparseColumn( MT& matrix, size_t index );
392  // No explicitly declared copy constructor.
394  //**********************************************************************************************
395 
396  //**Destructor**********************************************************************************
397  // No explicitly declared destructor.
398  //**********************************************************************************************
399 
400  //**Data access functions***********************************************************************
403  inline Reference operator[]( size_t index );
404  inline ConstReference operator[]( size_t index ) const;
405  inline Iterator begin ();
406  inline ConstIterator begin () const;
407  inline ConstIterator cbegin() const;
408  inline Iterator end ();
409  inline ConstIterator end () const;
410  inline ConstIterator cend () const;
412  //**********************************************************************************************
413 
414  //**Assignment operators************************************************************************
417  inline SparseColumn& operator= ( const SparseColumn& rhs );
418  template< typename VT > inline SparseColumn& operator= ( const DenseVector <VT,false>& rhs );
419  template< typename VT > inline SparseColumn& operator= ( const SparseVector<VT,false>& rhs );
420  template< typename VT > inline SparseColumn& operator+=( const Vector<VT,false>& rhs );
421  template< typename VT > inline SparseColumn& operator-=( const Vector<VT,false>& rhs );
422  template< typename VT > inline SparseColumn& operator*=( const Vector<VT,false>& rhs );
423 
424  template< typename Other >
425  inline typename EnableIf< IsNumeric<Other>, SparseColumn >::Type&
426  operator*=( Other rhs );
427 
428  template< typename Other >
429  inline typename EnableIf< IsNumeric<Other>, SparseColumn >::Type&
430  operator/=( Other rhs );
432  //**********************************************************************************************
433 
434  //**Utility functions***************************************************************************
437  inline size_t size() const;
438  inline size_t capacity() const;
439  inline size_t nonZeros() const;
440  inline void reset();
441  inline Iterator insert ( size_t index, const ElementType& value );
442  inline void erase ( size_t index );
443  inline Iterator erase ( Iterator pos );
444  inline Iterator erase ( Iterator first, Iterator last );
445  inline void reserve( size_t n );
446  template< typename Other > inline SparseColumn& scale ( Other scalar );
448  //**********************************************************************************************
449 
450  //**Lookup functions****************************************************************************
453  inline Iterator find ( size_t index );
454  inline ConstIterator find ( size_t index ) const;
455  inline Iterator lowerBound( size_t index );
456  inline ConstIterator lowerBound( size_t index ) const;
457  inline Iterator upperBound( size_t index );
458  inline ConstIterator upperBound( size_t index ) const;
460  //**********************************************************************************************
461 
462  //**Low-level utility functions*****************************************************************
465  inline void append( size_t index, const ElementType& value, bool check=false );
467  //**********************************************************************************************
468 
469  //**Expression template evaluation functions****************************************************
472  template< typename Other > inline bool canAlias ( const Other* alias ) const;
473  template< typename Other > inline bool isAliased( const Other* alias ) const;
474 
475  template< typename VT > inline void assign ( const DenseVector <VT,false>& rhs );
476  template< typename VT > inline void assign ( const SparseVector<VT,false>& rhs );
477  template< typename VT > inline void addAssign( const DenseVector <VT,false>& rhs );
478  template< typename VT > inline void addAssign( const SparseVector<VT,false>& rhs );
479  template< typename VT > inline void subAssign( const DenseVector <VT,false>& rhs );
480  template< typename VT > inline void subAssign( const SparseVector<VT,false>& rhs );
482  //**********************************************************************************************
483 
484  private:
485  //**Utility functions***************************************************************************
488  inline size_t extendCapacity() const;
490  //**********************************************************************************************
491 
492  //**Member variables****************************************************************************
496  const size_t col_;
497 
498  //**********************************************************************************************
499 
500  //**Friend declarations*************************************************************************
502  template< typename MT2, bool SO2 >
503  friend bool isSame( const SparseColumn<MT2,SO2>& a, const SparseColumn<MT2,SO2>& b );
505  //**********************************************************************************************
506 
507  //**Compile time checks*************************************************************************
514  //**********************************************************************************************
515 };
516 //*************************************************************************************************
517 
518 
519 
520 
521 //=================================================================================================
522 //
523 // CONSTRUCTOR
524 //
525 //=================================================================================================
526 
527 //*************************************************************************************************
534 template< typename MT // Type of the sparse matrix
535  , bool SO > // Storage order
536 inline SparseColumn<MT,SO>::SparseColumn( MT& matrix, size_t index )
537  : matrix_( matrix ) // The sparse matrix containing the column
538  , col_ ( index ) // The index of the column in the matrix
539 {
540  if( matrix_.columns() <= index )
541  throw std::invalid_argument( "Invalid column access index" );
542 }
543 //*************************************************************************************************
544 
545 
546 
547 
548 //=================================================================================================
549 //
550 // DATA ACCESS FUNCTIONS
551 //
552 //=================================================================================================
553 
554 //*************************************************************************************************
560 template< typename MT // Type of the sparse matrix
561  , bool SO > // Storage order
563 {
564  BLAZE_USER_ASSERT( index < size(), "Invalid column access index" );
565  return matrix_(index,col_);
566 }
567 //*************************************************************************************************
568 
569 
570 //*************************************************************************************************
576 template< typename MT // Type of the sparse matrix
577  , bool SO > // Storage order
579 {
580  BLAZE_USER_ASSERT( index < size(), "Invalid column access index" );
581  return const_cast<const MT&>( matrix_ )(index,col_);
582 }
583 //*************************************************************************************************
584 
585 
586 //*************************************************************************************************
593 template< typename MT // Type of the sparse matrix
594  , bool SO > // Storage order
596 {
597  return matrix_.begin( col_ );
598 }
599 //*************************************************************************************************
600 
601 
602 //*************************************************************************************************
609 template< typename MT // Type of the sparse matrix
610  , bool SO > // Storage order
612 {
613  return matrix_.cbegin( col_ );
614 }
615 //*************************************************************************************************
616 
617 
618 //*************************************************************************************************
625 template< typename MT // Type of the sparse matrix
626  , bool SO > // Storage order
628 {
629  return matrix_.cbegin( col_ );
630 }
631 //*************************************************************************************************
632 
633 
634 //*************************************************************************************************
641 template< typename MT // Type of the sparse matrix
642  , bool SO > // Storage order
644 {
645  return matrix_.end( col_ );
646 }
647 //*************************************************************************************************
648 
649 
650 //*************************************************************************************************
657 template< typename MT // Type of the sparse matrix
658  , bool SO > // Storage order
660 {
661  return matrix_.cend( col_ );
662 }
663 //*************************************************************************************************
664 
665 
666 //*************************************************************************************************
673 template< typename MT // Type of the sparse matrix
674  , bool SO > // Storage order
676 {
677  return matrix_.cend( col_ );
678 }
679 //*************************************************************************************************
680 
681 
682 
683 
684 //=================================================================================================
685 //
686 // ASSIGNMENT OPERATORS
687 //
688 //=================================================================================================
689 
690 //*************************************************************************************************
700 template< typename MT // Type of the sparse matrix
701  , bool SO > // Storage order
703 {
704  using blaze::assign;
705 
709 
710  if( this == &rhs || ( &matrix_ == &rhs.matrix_ && col_ == rhs.col_ ) )
711  return *this;
712 
713  if( size() != rhs.size() )
714  throw std::invalid_argument( "Column sizes do not match" );
715 
716  if( rhs.canAlias( &matrix_ ) ) {
717  const ResultType tmp( rhs );
718  matrix_.reset ( col_ );
719  matrix_.reserve( col_, tmp.nonZeros() );
720  assign( *this, tmp );
721  }
722  else {
723  matrix_.reset ( col_ );
724  matrix_.reserve( col_, rhs.nonZeros() );
725  assign( *this, rhs );
726  }
727 
728  return *this;
729 }
730 //*************************************************************************************************
731 
732 
733 //*************************************************************************************************
743 template< typename MT // Type of the sparse matrix
744  , bool SO > // Storage order
745 template< typename VT > // Type of the right-hand side dense vector
747 {
748  using blaze::assign;
749 
753 
754  if( size() != (~rhs).size() )
755  throw std::invalid_argument( "Vector sizes do not match" );
756 
757  if( (~rhs).canAlias( &matrix_ ) ) {
758  const typename VT::ResultType tmp( ~rhs );
759  matrix_.reset( col_ );
760  assign( *this, tmp );
761  }
762  else {
763  matrix_.reset( col_ );
764  assign( *this, ~rhs );
765  }
766 
767  return *this;
768 }
769 //*************************************************************************************************
770 
771 
772 //*************************************************************************************************
782 template< typename MT // Type of the sparse matrix
783  , bool SO > // Storage order
784 template< typename VT > // Type of the right-hand side sparse vector
786 {
787  using blaze::assign;
788 
789  if( size() != (~rhs).size() )
790  throw std::invalid_argument( "Vector sizes do not match" );
791 
795 
796  if( (~rhs).canAlias( &matrix_ ) ) {
797  const typename VT::ResultType tmp( ~rhs );
798  matrix_.reset ( col_ );
799  matrix_.reserve( col_, tmp.nonZeros() );
800  assign( *this, tmp );
801  }
802  else {
803  matrix_.reset ( col_ );
804  matrix_.reserve( col_, (~rhs).nonZeros() );
805  assign( *this, ~rhs );
806  }
807 
808  return *this;
809 }
810 //*************************************************************************************************
811 
812 
813 //*************************************************************************************************
823 template< typename MT // Type of the sparse matrix
824  , bool SO > // Storage order
825 template< typename VT > // Type of the right-hand side vector
827 {
828  using blaze::addAssign;
829 
830  if( size() != (~rhs).size() )
831  throw std::invalid_argument( "Vector sizes do not match" );
832 
833  addAssign( *this, ~rhs );
834 
835  return *this;
836 }
837 //*************************************************************************************************
838 
839 
840 //*************************************************************************************************
850 template< typename MT // Type of the sparse matrix
851  , bool SO > // Storage order
852 template< typename VT > // Type of the right-hand side vector
854 {
855  using blaze::subAssign;
856 
857  if( size() != (~rhs).size() )
858  throw std::invalid_argument( "Vector sizes do not match" );
859 
860  subAssign( *this, ~rhs );
861 
862  return *this;
863 }
864 //*************************************************************************************************
865 
866 
867 //*************************************************************************************************
878 template< typename MT // Type of the sparse matrix
879  , bool SO > // Storage order
880 template< typename VT > // Type of the right-hand side vector
882 {
883  if( size() != (~rhs).size() )
884  throw std::invalid_argument( "Vector sizes do not match" );
885 
886  typedef typename MultTrait<ResultType,typename VT::ResultType>::Type MultType;
887 
890 
891  const MultType tmp( *this * (~rhs) );
892  matrix_.reset( col_ );
893  assign( tmp );
894 
895  return *this;
896 }
897 //*************************************************************************************************
898 
899 
900 //*************************************************************************************************
911 template< typename MT // Type of the sparse matrix
912  , bool SO > // Storage order
913 template< typename Other > // Data type of the right-hand side scalar
914 inline typename EnableIf< IsNumeric<Other>, SparseColumn<MT,SO> >::Type&
916 {
917  for( Iterator element=begin(); element!=end(); ++element )
918  element->value() *= rhs;
919  return *this;
920 }
921 //*************************************************************************************************
922 
923 
924 //*************************************************************************************************
936 template< typename MT // Type of the sparse matrix
937  , bool SO > // Storage order
938 template< typename Other > // Data type of the right-hand side scalar
939 inline typename EnableIf< IsNumeric<Other>, SparseColumn<MT,SO> >::Type&
941 {
942  BLAZE_USER_ASSERT( rhs != Other(0), "Division by zero detected" );
943 
944  typedef typename DivTrait<ElementType,Other>::Type DT;
945  typedef typename If< IsNumeric<DT>, DT, Other >::Type Tmp;
946 
947  // Depending on the two involved data types, an integer division is applied or a
948  // floating point division is selected.
950  const Tmp tmp( Tmp(1)/static_cast<Tmp>( rhs ) );
951  for( Iterator element=begin(); element!=end(); ++element )
952  element->value() *= tmp;
953  }
954  else {
955  for( Iterator element=begin(); element!=end(); ++element )
956  element->value() /= rhs;
957  }
958 
959  return *this;
960 }
961 //*************************************************************************************************
962 
963 
964 
965 
966 //=================================================================================================
967 //
968 // UTILITY FUNCTIONS
969 //
970 //=================================================================================================
971 
972 //*************************************************************************************************
977 template< typename MT // Type of the sparse matrix
978  , bool SO > // Storage order
979 inline size_t SparseColumn<MT,SO>::size() const
980 {
981  return matrix_.rows();
982 }
983 //*************************************************************************************************
984 
985 
986 //*************************************************************************************************
991 template< typename MT // Type of the sparse matrix
992  , bool SO > // Storage order
993 inline size_t SparseColumn<MT,SO>::capacity() const
994 {
995  return matrix_.capacity( col_ );
996 }
997 //*************************************************************************************************
998 
999 
1000 //*************************************************************************************************
1008 template< typename MT // Type of the sparse matrix
1009  , bool SO > // Storage order
1010 inline size_t SparseColumn<MT,SO>::nonZeros() const
1011 {
1012  return matrix_.nonZeros( col_ );
1013 }
1014 //*************************************************************************************************
1015 
1016 
1017 //*************************************************************************************************
1022 template< typename MT // Type of the sparse matrix
1023  , bool SO > // Storage order
1025 {
1026  matrix_.reset( col_ );
1027 }
1028 //*************************************************************************************************
1029 
1030 
1031 //*************************************************************************************************
1043 template< typename MT // Type of the sparse matrix
1044  , bool SO > // Storage order
1045 inline typename SparseColumn<MT,SO>::Iterator
1046  SparseColumn<MT,SO>::insert( size_t index, const ElementType& value )
1047 {
1048  return matrix_.insert( index, col_, value );
1049 }
1050 //*************************************************************************************************
1051 
1052 
1053 //*************************************************************************************************
1061 template< typename MT // Type of the sparse matrix
1062  , bool SO > // Storage order
1063 inline void SparseColumn<MT,SO>::erase( size_t index )
1064 {
1065  matrix_.erase( index, col_ );
1066 }
1067 //*************************************************************************************************
1068 
1069 
1070 //*************************************************************************************************
1078 template< typename MT // Type of the sparse matrix
1079  , bool SO > // Storage order
1081 {
1082  return matrix_.erase( col_, pos );
1083 }
1084 //*************************************************************************************************
1085 
1086 
1087 //*************************************************************************************************
1096 template< typename MT // Type of the sparse matrix
1097  , bool SO > // Storage order
1099 {
1100  return matrix_.erase( col_, first, last );
1101 }
1102 //*************************************************************************************************
1103 
1104 
1105 //*************************************************************************************************
1114 template< typename MT // Type of the sparse matrix
1115  , bool SO > // Storage order
1117 {
1118  matrix_.reserve( col_, n );
1119 }
1120 //*************************************************************************************************
1121 
1122 
1123 //*************************************************************************************************
1129 template< typename MT // Type of the sparse matrix
1130  , bool SO > // Storage order
1131 template< typename Other > // Data type of the scalar value
1133 {
1134  for( Iterator element=begin(); element!=end(); ++element )
1135  element->value() *= scalar;
1136  return *this;
1137 }
1138 //*************************************************************************************************
1139 
1140 
1141 //*************************************************************************************************
1149 template< typename MT // Type of the sparse matrix
1150  , bool SO > // Storage order
1152 {
1153  using blaze::max;
1154  using blaze::min;
1155 
1156  size_t nonzeros( 2UL*capacity()+1UL );
1157  nonzeros = max( nonzeros, 7UL );
1158  nonzeros = min( nonzeros, size() );
1159 
1160  BLAZE_INTERNAL_ASSERT( nonzeros > capacity(), "Invalid capacity value" );
1161 
1162  return nonzeros;
1163 }
1164 //*************************************************************************************************
1165 
1166 
1167 
1168 
1169 //=================================================================================================
1170 //
1171 // LOOKUP FUNCTIONS
1172 //
1173 //=================================================================================================
1174 
1175 //*************************************************************************************************
1188 template< typename MT // Type of the sparse matrix
1189  , bool SO > // Storage order
1191 {
1192  return matrix_.find( index, col_ );
1193 }
1194 //*************************************************************************************************
1195 
1196 
1197 //*************************************************************************************************
1210 template< typename MT // Type of the sparse matrix
1211  , bool SO > // Storage order
1213 {
1214  return matrix_.find( index, col_ );
1215 }
1216 //*************************************************************************************************
1217 
1218 
1219 //*************************************************************************************************
1231 template< typename MT // Type of the sparse matrix
1232  , bool SO > // Storage order
1234 {
1235  return matrix_.lowerBound( index, col_ );
1236 }
1237 //*************************************************************************************************
1238 
1239 
1240 //*************************************************************************************************
1252 template< typename MT // Type of the sparse matrix
1253  , bool SO > // Storage order
1255 {
1256  return matrix_.lowerBound( index, col_ );
1257 }
1258 //*************************************************************************************************
1259 
1260 
1261 //*************************************************************************************************
1273 template< typename MT // Type of the sparse matrix
1274  , bool SO > // Storage order
1276 {
1277  return matrix_.upperBound( index, col_ );
1278 }
1279 //*************************************************************************************************
1280 
1281 
1282 //*************************************************************************************************
1294 template< typename MT // Type of the sparse matrix
1295  , bool SO > // Storage order
1297 {
1298  return matrix_.upperBound( index, col_ );
1299 }
1300 //*************************************************************************************************
1301 
1302 
1303 
1304 
1305 //=================================================================================================
1306 //
1307 // LOW-LEVEL UTILITY FUNCTIONS
1308 //
1309 //=================================================================================================
1310 
1311 //*************************************************************************************************
1335 template< typename MT // Type of the sparse matrix
1336  , bool SO > // Storage order
1337 inline void SparseColumn<MT,SO>::append( size_t index, const ElementType& value, bool check )
1338 {
1339  matrix_.append( index, col_, value, check );
1340 }
1341 //*************************************************************************************************
1342 
1343 
1344 
1345 
1346 //=================================================================================================
1347 //
1348 // EXPRESSION TEMPLATE EVALUATION FUNCTIONS
1349 //
1350 //=================================================================================================
1351 
1352 //*************************************************************************************************
1362 template< typename MT // Type of the sparse matrix
1363  , bool SO > // Storage order
1364 template< typename Other > // Data type of the foreign expression
1365 inline bool SparseColumn<MT,SO>::canAlias( const Other* alias ) const
1366 {
1367  return matrix_.isAliased( alias );
1368 }
1369 //*************************************************************************************************
1370 
1371 
1372 //*************************************************************************************************
1382 template< typename MT // Type of the sparse matrix
1383  , bool SO > // Storage order
1384 template< typename Other > // Data type of the foreign expression
1385 inline bool SparseColumn<MT,SO>::isAliased( const Other* alias ) const
1386 {
1387  return matrix_.isAliased( alias );
1388 }
1389 //*************************************************************************************************
1390 
1391 
1392 //*************************************************************************************************
1403 template< typename MT // Type of the sparse matrix
1404  , bool SO > // Storage order
1405 template< typename VT > // Type of the right-hand side dense vector
1407 {
1408  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
1409  BLAZE_INTERNAL_ASSERT( nonZeros() == 0UL, "Invalid non-zero elements detected" );
1410 
1411  for( size_t i=0UL; i<size(); ++i )
1412  {
1413  if( matrix_.nonZeros( col_ ) == matrix_.capacity( col_ ) )
1414  matrix_.reserve( col_, extendCapacity() );
1415 
1416  matrix_.append( i, col_, (~rhs)[i], true );
1417  }
1418 }
1419 //*************************************************************************************************
1420 
1421 
1422 //*************************************************************************************************
1433 template< typename MT // Type of the sparse matrix
1434  , bool SO > // Storage order
1435 template< typename VT > // Type of the right-hand side sparse vector
1437 {
1438  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
1439  BLAZE_INTERNAL_ASSERT( nonZeros() == 0UL, "Invalid non-zero elements detected" );
1440 
1441  for( typename VT::ConstIterator element=(~rhs).begin(); element!=(~rhs).end(); ++element ) {
1442  matrix_.append( element->index(), col_, element->value() );
1443  }
1444 }
1445 //*************************************************************************************************
1446 
1447 
1448 //*************************************************************************************************
1459 template< typename MT // Type of the sparse matrix
1460  , bool SO > // Storage order
1461 template< typename VT > // Type of the right-hand side dense vector
1463 {
1464  typedef typename AddTrait<ResultType,typename VT::ResultType>::Type AddType;
1465 
1469 
1470  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
1471 
1472  const AddType tmp( serial( *this + (~rhs) ) );
1473  matrix_.reset( col_ );
1474  assign( tmp );
1475 }
1476 //*************************************************************************************************
1477 
1478 
1479 //*************************************************************************************************
1490 template< typename MT // Type of the sparse matrix
1491  , bool SO > // Storage order
1492 template< typename VT > // Type of the right-hand side sparse vector
1494 {
1495  typedef typename AddTrait<ResultType,typename VT::ResultType>::Type AddType;
1496 
1500 
1501  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
1502 
1503  const AddType tmp( serial( *this + (~rhs) ) );
1504  matrix_.reset ( col_ );
1505  matrix_.reserve( col_, tmp.nonZeros() );
1506  assign( tmp );
1507 }
1508 //*************************************************************************************************
1509 
1510 
1511 //*************************************************************************************************
1522 template< typename MT // Type of the sparse matrix
1523  , bool SO > // Storage order
1524 template< typename VT > // Type of the right-hand side dense vector
1526 {
1527  typedef typename SubTrait<ResultType,typename VT::ResultType>::Type SubType;
1528 
1532 
1533  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
1534 
1535  const SubType tmp( serial( *this - (~rhs) ) );
1536  matrix_.reset( col_ );
1537  assign( tmp );
1538 }
1539 //*************************************************************************************************
1540 
1541 
1542 //*************************************************************************************************
1553 template< typename MT // Type of the sparse matrix
1554  , bool SO > // Storage order
1555 template< typename VT > // Type of the right-hand side sparse vector
1557 {
1558  typedef typename SubTrait<ResultType,typename VT::ResultType>::Type SubType;
1559 
1563 
1564  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
1565 
1566  const SubType tmp( serial( *this - (~rhs) ) );
1567  matrix_.reset ( col_ );
1568  matrix_.reserve( col_, tmp.nonZeros() );
1569  assign( tmp );
1570 }
1571 //*************************************************************************************************
1572 
1573 
1574 
1575 
1576 
1577 
1578 
1579 
1580 //=================================================================================================
1581 //
1582 // CLASS TEMPLATE SPECIALIZATION FOR ROW-MAJOR MATRICES
1583 //
1584 //=================================================================================================
1585 
1586 //*************************************************************************************************
1594 template< typename MT > // Type of the sparse matrix
1595 class SparseColumn<MT,false> : public SparseVector< SparseColumn<MT,false>, false >
1596  , private Column
1597 {
1598  private:
1599  //**Type definitions****************************************************************************
1601  typedef typename SelectType< IsExpression<MT>::value, MT, MT& >::Type Operand;
1602  //**********************************************************************************************
1603 
1604  //**********************************************************************************************
1606 
1612  enum { useConst = IsConst<MT>::value };
1613  //**********************************************************************************************
1614 
1615  public:
1616  //**Type definitions****************************************************************************
1617  typedef SparseColumn<MT,false> This;
1618  typedef typename ColumnTrait<MT>::Type ResultType;
1619  typedef typename ResultType::TransposeType TransposeType;
1620  typedef typename MT::ElementType ElementType;
1621  typedef typename MT::ReturnType ReturnType;
1622  typedef const ResultType CompositeType;
1623 
1625  typedef typename MT::ConstReference ConstReference;
1626 
1628  typedef typename SelectType< useConst, ConstReference, typename MT::Reference >::Type Reference;
1629  //**********************************************************************************************
1630 
1631  //**ColumnElement class definition**************************************************************
1634  template< typename MatrixType // Type of the sparse matrix
1635  , typename IteratorType > // Type of the sparse matrix iterator
1636  class ColumnElement
1637  {
1638  private:
1639  //*******************************************************************************************
1641 
1646  enum { returnConst = IsConst<MatrixType>::value };
1647  //*******************************************************************************************
1648 
1649  public:
1650  //**Type definitions*************************************************************************
1651  typedef typename SelectType< returnConst, const ElementType&, ElementType& >::Type ReferenceType;
1652  //*******************************************************************************************
1653 
1654  //**Constructor******************************************************************************
1660  inline ColumnElement( IteratorType pos, size_t row )
1661  : pos_( pos ) // Iterator to the current position within the sparse column
1662  , row_( row ) // Index of the according row
1663  {}
1664  //*******************************************************************************************
1665 
1666  //**Assignment operator**********************************************************************
1672  template< typename T > inline ColumnElement& operator=( const T& v ) {
1673  *pos_ = v;
1674  return *this;
1675  }
1676  //*******************************************************************************************
1677 
1678  //**Addition assignment operator*************************************************************
1684  template< typename T > inline ColumnElement& operator+=( const T& v ) {
1685  *pos_ += v;
1686  return *this;
1687  }
1688  //*******************************************************************************************
1689 
1690  //**Subtraction assignment operator**********************************************************
1696  template< typename T > inline ColumnElement& operator-=( const T& v ) {
1697  *pos_ -= v;
1698  return *this;
1699  }
1700  //*******************************************************************************************
1701 
1702  //**Multiplication assignment operator*******************************************************
1708  template< typename T > inline ColumnElement& operator*=( const T& v ) {
1709  *pos_ *= v;
1710  return *this;
1711  }
1712  //*******************************************************************************************
1713 
1714  //**Division assignment operator*************************************************************
1720  template< typename T > inline ColumnElement& operator/=( const T& v ) {
1721  *pos_ /= v;
1722  return *this;
1723  }
1724  //*******************************************************************************************
1725 
1726  //**Element access operator******************************************************************
1731  inline const ColumnElement* operator->() const {
1732  return this;
1733  }
1734  //*******************************************************************************************
1735 
1736  //**Value function***************************************************************************
1741  inline ReferenceType value() const {
1742  return pos_->value();
1743  }
1744  //*******************************************************************************************
1745 
1746  //**Index function***************************************************************************
1751  inline size_t index() const {
1752  return row_;
1753  }
1754  //*******************************************************************************************
1755 
1756  private:
1757  //**Member variables*************************************************************************
1758  IteratorType pos_;
1759  size_t row_;
1760  //*******************************************************************************************
1761  };
1762  //**********************************************************************************************
1763 
1764  //**ColumnIterator class definition*************************************************************
1767  template< typename MatrixType // Type of the sparse matrix
1768  , typename IteratorType > // Type of the sparse matrix iterator
1769  class ColumnIterator
1770  {
1771  public:
1772  //**Type definitions*************************************************************************
1773  typedef std::forward_iterator_tag IteratorCategory;
1774  typedef ColumnElement<MatrixType,IteratorType> ValueType;
1775  typedef ValueType PointerType;
1776  typedef ValueType ReferenceType;
1777  typedef ptrdiff_t DifferenceType;
1778 
1779  // STL iterator requirements
1780  typedef IteratorCategory iterator_category;
1781  typedef ValueType value_type;
1782  typedef PointerType pointer;
1783  typedef ReferenceType reference;
1784  typedef DifferenceType difference_type;
1785  //*******************************************************************************************
1786 
1787  //**Constructor******************************************************************************
1794  inline ColumnIterator( MatrixType& matrix, size_t row, size_t column )
1795  : matrix_( matrix ) // The sparse matrix containing the column.
1796  , row_ ( row ) // The current row index.
1797  , column_( column ) // The current column index.
1798  , pos_ () // Iterator to the current sparse element.
1799  {
1800  for( ; row_<matrix_.rows(); ++row_ ) {
1801  pos_ = matrix_.find( row_, column_ );
1802  if( pos_ != matrix_.end( row_ ) ) break;
1803  }
1804  }
1805  //*******************************************************************************************
1806 
1807  //**Constructor******************************************************************************
1815  inline ColumnIterator( MatrixType& matrix, size_t row, size_t column, IteratorType pos )
1816  : matrix_( matrix ) // The sparse matrix containing the column.
1817  , row_ ( row ) // The current row index.
1818  , column_( column ) // The current column index.
1819  , pos_ ( pos ) // Iterator to the current sparse element.
1820  {
1821  BLAZE_INTERNAL_ASSERT( matrix.find( row, column ) == pos, "Invalid initial iterator position" );
1822  }
1823  //*******************************************************************************************
1824 
1825  //**Constructor******************************************************************************
1830  template< typename MatrixType2, typename IteratorType2 >
1831  inline ColumnIterator( const ColumnIterator<MatrixType2,IteratorType2>& it )
1832  : matrix_( it.matrix_ ) // The sparse matrix containing the column.
1833  , row_ ( it.row_ ) // The current row index.
1834  , column_( it.column_ ) // The current column index.
1835  , pos_ ( it.pos_ ) // Iterator to the current sparse element.
1836  {}
1837  //*******************************************************************************************
1838 
1839  //**Prefix increment operator****************************************************************
1844  inline ColumnIterator& operator++() {
1845  ++row_;
1846  for( ; row_<matrix_.rows(); ++row_ ) {
1847  pos_ = matrix_.find( row_, column_ );
1848  if( pos_ != matrix_.end( row_ ) ) break;
1849  }
1850 
1851  return *this;
1852  }
1853  //*******************************************************************************************
1854 
1855  //**Postfix increment operator***************************************************************
1860  inline const ColumnIterator operator++( int ) {
1861  const ColumnIterator tmp( *this );
1862  ++(*this);
1863  return tmp;
1864  }
1865  //*******************************************************************************************
1866 
1867  //**Element access operator******************************************************************
1872  inline ReferenceType operator*() const {
1873  return ReferenceType( pos_, row_ );
1874  }
1875  //*******************************************************************************************
1876 
1877  //**Element access operator******************************************************************
1882  inline PointerType operator->() const {
1883  return PointerType( pos_, row_ );
1884  }
1885  //*******************************************************************************************
1886 
1887  //**Equality operator************************************************************************
1893  template< typename MatrixType2, typename IteratorType2 >
1894  inline bool operator==( const ColumnIterator<MatrixType2,IteratorType2>& rhs ) const {
1895  return ( &matrix_ == &rhs.matrix_ ) && ( row_ == rhs.row_ ) && ( column_ == rhs.column_ );
1896  }
1897  //*******************************************************************************************
1898 
1899  //**Inequality operator**********************************************************************
1905  template< typename MatrixType2, typename IteratorType2 >
1906  inline bool operator!=( const ColumnIterator<MatrixType2,IteratorType2>& rhs ) const {
1907  return !( *this == rhs );
1908  }
1909  //*******************************************************************************************
1910 
1911  //**Subtraction operator*********************************************************************
1917  inline DifferenceType operator-( const ColumnIterator& rhs ) const {
1918  size_t counter( 0UL );
1919  for( size_t i=rhs.row_; i<row_; ++i ) {
1920  if( matrix_.find( i, column_ ) != matrix_.end( i ) )
1921  ++counter;
1922  }
1923  return counter;
1924  }
1925  //*******************************************************************************************
1926 
1927  private:
1928  //**Member variables*************************************************************************
1929  MatrixType& matrix_;
1930  size_t row_;
1931  size_t column_;
1932  IteratorType pos_;
1933  //*******************************************************************************************
1934 
1935  //**Friend declarations**********************************************************************
1936  template< typename MatrixType2, typename IteratorType2 > friend class ColumnIterator;
1937  template< typename MT2, bool SO2 > friend class SparseColumn;
1938  //*******************************************************************************************
1939  };
1940  //**********************************************************************************************
1941 
1942  //**Type definitions****************************************************************************
1944  typedef ColumnIterator<const MT,typename MT::ConstIterator> ConstIterator;
1945 
1947  typedef typename SelectType< useConst, ConstIterator, ColumnIterator<MT,typename MT::Iterator> >::Type Iterator;
1948  //**********************************************************************************************
1949 
1950  //**Compilation flags***************************************************************************
1952  enum { smpAssignable = 0 };
1953  //**********************************************************************************************
1954 
1955  //**Constructors********************************************************************************
1958  explicit inline SparseColumn( MT& matrix, size_t index );
1959  // No explicitly declared copy constructor.
1961  //**********************************************************************************************
1962 
1963  //**Destructor**********************************************************************************
1964  // No explicitly declared destructor.
1965  //**********************************************************************************************
1966 
1967  //**Data access functions***********************************************************************
1970  inline Reference operator[]( size_t index );
1971  inline ConstReference operator[]( size_t index ) const;
1972  inline Iterator begin ();
1973  inline ConstIterator begin () const;
1974  inline ConstIterator cbegin() const;
1975  inline Iterator end ();
1976  inline ConstIterator end () const;
1977  inline ConstIterator cend () const;
1979  //**********************************************************************************************
1980 
1981  //**Assignment operators************************************************************************
1984  inline SparseColumn& operator= ( const SparseColumn& rhs );
1985  template< typename VT > inline SparseColumn& operator= ( const Vector<VT,false>& rhs );
1986  template< typename VT > inline SparseColumn& operator+=( const Vector<VT,false>& rhs );
1987  template< typename VT > inline SparseColumn& operator-=( const Vector<VT,false>& rhs );
1988  template< typename VT > inline SparseColumn& operator*=( const Vector<VT,false>& rhs );
1989 
1990  template< typename Other >
1991  inline typename EnableIf< IsNumeric<Other>, SparseColumn >::Type&
1992  operator*=( Other rhs );
1993 
1994  template< typename Other >
1995  inline typename EnableIf< IsNumeric<Other>, SparseColumn >::Type&
1996  operator/=( Other rhs );
1998  //**********************************************************************************************
1999 
2000  //**Utility functions***************************************************************************
2003  inline size_t size() const;
2004  inline size_t capacity() const;
2005  inline size_t nonZeros() const;
2006  inline void reset();
2007  inline Iterator insert ( size_t index, const ElementType& value );
2008  inline void erase ( size_t index );
2009  inline Iterator erase ( Iterator pos );
2010  inline Iterator erase ( Iterator first, Iterator last );
2011  inline void reserve( size_t n );
2012  template< typename Other > inline SparseColumn& scale ( Other scalar );
2014  //**********************************************************************************************
2015 
2016  //**Lookup functions****************************************************************************
2019  inline Iterator find ( size_t index );
2020  inline ConstIterator find ( size_t index ) const;
2021  inline Iterator lowerBound( size_t index );
2022  inline ConstIterator lowerBound( size_t index ) const;
2023  inline Iterator upperBound( size_t index );
2024  inline ConstIterator upperBound( size_t index ) const;
2026  //**********************************************************************************************
2027 
2028  //**Low-level utility functions*****************************************************************
2031  inline void append( size_t index, const ElementType& value, bool check=false );
2033  //**********************************************************************************************
2034 
2035  //**Expression template evaluation functions****************************************************
2038  template< typename Other > inline bool canAlias ( const Other* alias ) const;
2039  template< typename Other > inline bool isAliased( const Other* alias ) const;
2040 
2041  template< typename VT > inline void assign ( const DenseVector <VT,false>& rhs );
2042  template< typename VT > inline void assign ( const SparseVector<VT,false>& rhs );
2043  template< typename VT > inline void addAssign( const Vector<VT,false>& rhs );
2044  template< typename VT > inline void subAssign( const Vector<VT,false>& rhs );
2046  //**********************************************************************************************
2047 
2048  private:
2049  //**Member variables****************************************************************************
2052  Operand matrix_;
2053  const size_t col_;
2054 
2055  //**********************************************************************************************
2056 
2057  //**Friend declarations*************************************************************************
2058  template< typename MT2, bool SO2 >
2059  friend bool isSame( const SparseColumn<MT2,SO2>& a, const SparseColumn<MT2,SO2>& b );
2060  //**********************************************************************************************
2061 
2062  //**Compile time checks*************************************************************************
2067  //**********************************************************************************************
2068 };
2070 //*************************************************************************************************
2071 
2072 
2073 
2074 
2075 //=================================================================================================
2076 //
2077 // CONSTRUCTOR
2078 //
2079 //=================================================================================================
2080 
2081 //*************************************************************************************************
2089 template< typename MT > // Type of the sparse matrix
2090 inline SparseColumn<MT,false>::SparseColumn( MT& matrix, size_t index )
2091  : matrix_( matrix ) // The sparse matrix containing the column
2092  , col_ ( index ) // The index of the column in the matrix
2093 {
2094  if( matrix_.columns() <= index )
2095  throw std::invalid_argument( "Invalid column access index" );
2096 }
2098 //*************************************************************************************************
2099 
2100 
2101 
2102 
2103 //=================================================================================================
2104 //
2105 // DATA ACCESS FUNCTIONS
2106 //
2107 //=================================================================================================
2108 
2109 //*************************************************************************************************
2116 template< typename MT > // Type of the sparse matrix
2118 {
2119  BLAZE_USER_ASSERT( index < size(), "Invalid column access index" );
2120  return matrix_(index,col_);
2121 }
2123 //*************************************************************************************************
2124 
2125 
2126 //*************************************************************************************************
2133 template< typename MT > // Type of the sparse matrix
2135 {
2136  BLAZE_USER_ASSERT( index < size(), "Invalid column access index" );
2137  return const_cast<const MT&>( matrix_ )(index,col_);
2138 }
2140 //*************************************************************************************************
2141 
2142 
2143 //*************************************************************************************************
2151 template< typename MT > // Type of the sparse matrix
2153 {
2154  return Iterator( matrix_, 0UL, col_ );
2155 }
2157 //*************************************************************************************************
2158 
2159 
2160 //*************************************************************************************************
2168 template< typename MT > // Type of the sparse matrix
2170 {
2171  return ConstIterator( matrix_, 0UL, col_ );
2172 }
2174 //*************************************************************************************************
2175 
2176 
2177 //*************************************************************************************************
2185 template< typename MT > // Type of the sparse matrix
2187 {
2188  return ConstIterator( matrix_, 0UL, col_ );
2189 }
2191 //*************************************************************************************************
2192 
2193 
2194 //*************************************************************************************************
2202 template< typename MT > // Type of the sparse matrix
2204 {
2205  return Iterator( matrix_, size(), col_ );
2206 }
2208 //*************************************************************************************************
2209 
2210 
2211 //*************************************************************************************************
2219 template< typename MT > // Type of the sparse matrix
2221 {
2222  return ConstIterator( matrix_, size(), col_ );
2223 }
2225 //*************************************************************************************************
2226 
2227 
2228 //*************************************************************************************************
2236 template< typename MT > // Type of the sparse matrix
2238 {
2239  return ConstIterator( matrix_, size(), col_ );
2240 }
2242 //*************************************************************************************************
2243 
2244 
2245 
2246 
2247 //=================================================================================================
2248 //
2249 // ASSIGNMENT OPERATORS
2250 //
2251 //=================================================================================================
2252 
2253 //*************************************************************************************************
2264 template< typename MT > // Type of the sparse matrix
2265 inline SparseColumn<MT,false>& SparseColumn<MT,false>::operator=( const SparseColumn& rhs )
2266 {
2267  using blaze::assign;
2268 
2272 
2273  if( this == &rhs || ( &matrix_ == &rhs.matrix_ && col_ == rhs.col_ ) )
2274  return *this;
2275 
2276  if( size() != rhs.size() )
2277  throw std::invalid_argument( "Column sizes do not match" );
2278 
2279  if( rhs.canAlias( &matrix_ ) ) {
2280  const ResultType tmp( rhs );
2281  assign( *this, tmp );
2282  }
2283  else {
2284  assign( *this, rhs );
2285  }
2286 
2287  return *this;
2288 }
2290 //*************************************************************************************************
2291 
2292 
2293 //*************************************************************************************************
2304 template< typename MT > // Type of the sparse matrix
2305 template< typename VT > // Type of the right-hand side vector
2306 inline SparseColumn<MT,false>& SparseColumn<MT,false>::operator=( const Vector<VT,false>& rhs )
2307 {
2308  using blaze::assign;
2309 
2310  if( size() != (~rhs).size() )
2311  throw std::invalid_argument( "Vector sizes do not match" );
2312 
2313  const typename VT::CompositeType tmp( ~rhs );
2314  assign( *this, tmp );
2315 
2316  return *this;
2317 }
2319 //*************************************************************************************************
2320 
2321 
2322 //*************************************************************************************************
2333 template< typename MT > // Type of the sparse matrix
2334 template< typename VT > // Type of the right-hand side vector
2335 inline SparseColumn<MT,false>& SparseColumn<MT,false>::operator+=( const Vector<VT,false>& rhs )
2336 {
2337  using blaze::addAssign;
2338 
2339  if( size() != (~rhs).size() )
2340  throw std::invalid_argument( "Vector sizes do not match" );
2341 
2342  addAssign( *this, ~rhs );
2343 
2344  return *this;
2345 }
2347 //*************************************************************************************************
2348 
2349 
2350 //*************************************************************************************************
2361 template< typename MT > // Type of the sparse matrix
2362 template< typename VT > // Type of the right-hand side vector
2363 inline SparseColumn<MT,false>& SparseColumn<MT,false>::operator-=( const Vector<VT,false>& rhs )
2364 {
2365  using blaze::subAssign;
2366 
2367  if( size() != (~rhs).size() )
2368  throw std::invalid_argument( "Vector sizes do not match" );
2369 
2370  subAssign( *this, ~rhs );
2371 
2372  return *this;
2373 }
2375 //*************************************************************************************************
2376 
2377 
2378 //*************************************************************************************************
2390 template< typename MT > // Type of the sparse matrix
2391 template< typename VT > // Type of the right-hand side vector
2392 inline SparseColumn<MT,false>& SparseColumn<MT,false>::operator*=( const Vector<VT,false>& rhs )
2393 {
2394  if( size() != (~rhs).size() )
2395  throw std::invalid_argument( "Vector sizes do not match" );
2396 
2397  typedef typename MultTrait<ResultType,typename VT::ResultType>::Type MultType;
2398 
2401 
2402  const MultType tmp( *this * (~rhs) );
2403  assign( tmp );
2404 
2405  return *this;
2406 }
2408 //*************************************************************************************************
2409 
2410 
2411 //*************************************************************************************************
2423 template< typename MT > // Type of the sparse matrix
2424 template< typename Other > // Data type of the right-hand side scalar
2425 inline typename EnableIf< IsNumeric<Other>, SparseColumn<MT,false> >::Type&
2426  SparseColumn<MT,false>::operator*=( Other rhs )
2427 {
2428  for( Iterator element=begin(); element!=end(); ++element )
2429  element->value() *= rhs;
2430  return *this;
2431 }
2433 //*************************************************************************************************
2434 
2435 
2436 //*************************************************************************************************
2449 template< typename MT > // Type of the sparse matrix
2450 template< typename Other > // Data type of the right-hand side scalar
2451 inline typename EnableIf< IsNumeric<Other>, SparseColumn<MT,false> >::Type&
2452  SparseColumn<MT,false>::operator/=( Other rhs )
2453 {
2454  BLAZE_USER_ASSERT( rhs != Other(0), "Division by zero detected" );
2455 
2456  typedef typename DivTrait<ElementType,Other>::Type DT;
2457  typedef typename If< IsNumeric<DT>, DT, Other >::Type Tmp;
2458 
2459  // Depending on the two involved data types, an integer division is applied or a
2460  // floating point division is selected.
2461  if( IsNumeric<DT>::value && IsFloatingPoint<DT>::value ) {
2462  const Tmp tmp( Tmp(1)/static_cast<Tmp>( rhs ) );
2463  for( Iterator element=begin(); element!=end(); ++element )
2464  element->value() *= tmp;
2465  }
2466  else {
2467  for( Iterator element=begin(); element!=end(); ++element )
2468  element->value() /= rhs;
2469  }
2470 
2471  return *this;
2472 }
2474 //*************************************************************************************************
2475 
2476 
2477 
2478 
2479 //=================================================================================================
2480 //
2481 // UTILITY FUNCTIONS
2482 //
2483 //=================================================================================================
2484 
2485 //*************************************************************************************************
2491 template< typename MT > // Type of the sparse matrix
2492 inline size_t SparseColumn<MT,false>::size() const
2493 {
2494  return matrix_.rows();
2495 }
2497 //*************************************************************************************************
2498 
2499 
2500 //*************************************************************************************************
2506 template< typename MT > // Type of the sparse matrix
2507 inline size_t SparseColumn<MT,false>::capacity() const
2508 {
2509  return matrix_.rows();
2510 }
2512 //*************************************************************************************************
2513 
2514 
2515 //*************************************************************************************************
2524 template< typename MT > // Type of the sparse matrix
2525 inline size_t SparseColumn<MT,false>::nonZeros() const
2526 {
2527  size_t counter( 0UL );
2528  for( ConstIterator element=begin(); element!=end(); ++element ) {
2529  ++counter;
2530  }
2531  return counter;
2532 }
2534 //*************************************************************************************************
2535 
2536 
2537 //*************************************************************************************************
2543 template< typename MT > // Type of the sparse matrix
2544 inline void SparseColumn<MT,false>::reset()
2545 {
2546  for( size_t i=0UL; i<size(); ++i ) {
2547  matrix_.erase( i, col_ );
2548  }
2549 }
2551 //*************************************************************************************************
2552 
2553 
2554 //*************************************************************************************************
2567 template< typename MT > // Type of the sparse matrix
2568 inline typename SparseColumn<MT,false>::Iterator
2569  SparseColumn<MT,false>::insert( size_t index, const ElementType& value )
2570 {
2571  return Iterator( matrix_, index, col_, matrix_.insert( index, col_, value ) );
2572 }
2574 //*************************************************************************************************
2575 
2576 
2577 //*************************************************************************************************
2586 template< typename MT > // Type of the sparse matrix
2587 inline void SparseColumn<MT,false>::erase( size_t index )
2588 {
2589  matrix_.erase( index, col_ );
2590 }
2592 //*************************************************************************************************
2593 
2594 
2595 //*************************************************************************************************
2604 template< typename MT > // Type of the sparse matrix
2606 {
2607  const size_t row( pos.row_ );
2608 
2609  if( row == size() )
2610  return pos;
2611 
2612  matrix_.erase( row, pos.pos_ );
2613  return Iterator( matrix_, row+1UL, col_ );
2614 }
2616 //*************************************************************************************************
2617 
2618 
2619 //*************************************************************************************************
2629 template< typename MT > // Type of the sparse matrix
2631 {
2632  for( ; first!=last; ++first ) {
2633  matrix_.erase( first.row_, first.pos_ );
2634  }
2635  return last;
2636 }
2638 //*************************************************************************************************
2639 
2640 
2641 //*************************************************************************************************
2651 template< typename MT > // Type of the sparse matrix
2652 void SparseColumn<MT,false>::reserve( size_t n )
2653 {
2654  UNUSED_PARAMETER( n );
2655  return;
2656 }
2658 //*************************************************************************************************
2659 
2660 
2661 //*************************************************************************************************
2668 template< typename MT > // Type of the sparse matrix
2669 template< typename Other > // Data type of the scalar value
2670 inline SparseColumn<MT,false>& SparseColumn<MT,false>::scale( Other scalar )
2671 {
2672  for( Iterator element=begin(); element!=end(); ++element )
2673  element->value() *= scalar;
2674  return *this;
2675 }
2677 //*************************************************************************************************
2678 
2679 
2680 
2681 
2682 //=================================================================================================
2683 //
2684 // LOOKUP FUNCTIONS
2685 //
2686 //=================================================================================================
2687 
2688 //*************************************************************************************************
2702 template< typename MT > // Type of the sparse matrix
2703 inline typename SparseColumn<MT,false>::Iterator SparseColumn<MT,false>::find( size_t index )
2704 {
2705  const typename MT::Iterator pos( matrix_.find( index, col_ ) );
2706 
2707  if( pos != matrix_.end( index ) )
2708  return Iterator( matrix_, index, col_, pos );
2709  else
2710  return end();
2711 }
2713 //*************************************************************************************************
2714 
2715 
2716 //*************************************************************************************************
2730 template< typename MT > // Type of the sparse matrix
2731 inline typename SparseColumn<MT,false>::ConstIterator SparseColumn<MT,false>::find( size_t index ) const
2732 {
2733  const typename MT::ConstIterator pos( matrix_.find( index, col_ ) );
2734 
2735  if( pos != matrix_.end( index ) )
2736  return ConstIterator( matrix_, index, col_, pos );
2737  else
2738  return end();
2739 }
2741 //*************************************************************************************************
2742 
2743 
2744 //*************************************************************************************************
2757 template< typename MT > // Type of the sparse matrix
2759 {
2760  for( size_t i=index; i<size(); ++i )
2761  {
2762  const typename MT::Iterator pos( matrix_.find( i, col_ ) );
2763 
2764  if( pos != matrix_.end( i ) )
2765  return Iterator( matrix_, i, col_, pos );
2766  }
2767 
2768  return end();
2769 }
2771 //*************************************************************************************************
2772 
2773 
2774 //*************************************************************************************************
2787 template< typename MT > // Type of the sparse matrix
2789 {
2790  for( size_t i=index; i<size(); ++i )
2791  {
2792  const typename MT::ConstIterator pos( matrix_.find( i, col_ ) );
2793 
2794  if( pos != matrix_.end( i ) )
2795  return ConstIterator( matrix_, i, col_, pos );
2796  }
2797 
2798  return end();
2799 }
2801 //*************************************************************************************************
2802 
2803 
2804 //*************************************************************************************************
2817 template< typename MT > // Type of the sparse matrix
2819 {
2820  for( size_t i=index+1UL; i<size(); ++i )
2821  {
2822  const typename MT::Iterator pos( matrix_.find( i, col_ ) );
2823 
2824  if( pos != matrix_.end( i ) )
2825  return Iterator( matrix_, i, col_, pos );
2826  }
2827 
2828  return end();
2829 }
2831 //*************************************************************************************************
2832 
2833 
2834 //*************************************************************************************************
2847 template< typename MT > // Type of the sparse matrix
2849 {
2850  for( size_t i=index+1UL; i<size(); ++i )
2851  {
2852  const typename MT::ConstIterator pos( matrix_.find( i, col_ ) );
2853 
2854  if( pos != matrix_.end( i ) )
2855  return ConstIterator( matrix_, i, col_, pos );
2856  }
2857 
2858  return end();
2859 }
2861 //*************************************************************************************************
2862 
2863 
2864 
2865 
2866 //=================================================================================================
2867 //
2868 // LOW-LEVEL UTILITY FUNCTIONS
2869 //
2870 //=================================================================================================
2871 
2872 //*************************************************************************************************
2897 template< typename MT > // Type of the sparse matrix
2898 inline void SparseColumn<MT,false>::append( size_t index, const ElementType& value, bool check )
2899 {
2900  if( !check || !isDefault( value ) )
2901  matrix_.insert( index, col_, value );
2902 }
2904 //*************************************************************************************************
2905 
2906 
2907 
2908 
2909 //=================================================================================================
2910 //
2911 // EXPRESSION TEMPLATE EVALUATION FUNCTIONS
2912 //
2913 //=================================================================================================
2914 
2915 //*************************************************************************************************
2926 template< typename MT > // Type of the sparse matrix
2927 template< typename Other > // Data type of the foreign expression
2928 inline bool SparseColumn<MT,false>::canAlias( const Other* alias ) const
2929 {
2930  return matrix_.isAliased( alias );
2931 }
2933 //*************************************************************************************************
2934 
2935 
2936 //*************************************************************************************************
2943 template< typename MT > // Type of the sparse matrix
2944 template< typename Other > // Data type of the foreign expression
2945 inline bool SparseColumn<MT,false>::isAliased( const Other* alias ) const
2946 {
2947  return matrix_.isAliased( alias );
2948 }
2950 //*************************************************************************************************
2951 
2952 
2953 //*************************************************************************************************
2965 template< typename MT > // Type of the sparse matrix
2966 template< typename VT > // Type of the right-hand side dense vector
2967 inline void SparseColumn<MT,false>::assign( const DenseVector<VT,false>& rhs )
2968 {
2969  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
2970 
2971  for( size_t i=0UL; i<(~rhs).size(); ++i ) {
2972  matrix_(i,col_) = (~rhs)[i];
2973  }
2974 }
2976 //*************************************************************************************************
2977 
2978 
2979 //*************************************************************************************************
2991 template< typename MT > // Type of the sparse matrix
2992 template< typename VT > // Type of the right-hand side sparse vector
2993 inline void SparseColumn<MT,false>::assign( const SparseVector<VT,false>& rhs )
2994 {
2995  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
2996 
2997  size_t i( 0UL );
2998 
2999  for( typename VT::ConstIterator element=(~rhs).begin(); element!=(~rhs).end(); ++element ) {
3000  for( ; i<element->index(); ++i )
3001  matrix_.erase( i, col_ );
3002  matrix_(i++,col_) = element->value();
3003  }
3004  for( ; i<size(); ++i ) {
3005  matrix_.erase( i, col_ );
3006  }
3007 }
3009 //*************************************************************************************************
3010 
3011 
3012 //*************************************************************************************************
3024 template< typename MT > // Type of the sparse matrix
3025 template< typename VT > // Type of the right-hand side vector
3026 inline void SparseColumn<MT,false>::addAssign( const Vector<VT,false>& rhs )
3027 {
3028  typedef typename AddTrait<ResultType,typename VT::ResultType>::Type AddType;
3029 
3032 
3033  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
3034 
3035  const AddType tmp( serial( *this + (~rhs) ) );
3036  assign( tmp );
3037 }
3039 //*************************************************************************************************
3040 
3041 
3042 //*************************************************************************************************
3054 template< typename MT > // Type of the sparse matrix
3055 template< typename VT > // Type of the right-hand side vector
3056 inline void SparseColumn<MT,false>::subAssign( const Vector<VT,false>& rhs )
3057 {
3058  typedef typename SubTrait<ResultType,typename VT::ResultType>::Type SubType;
3059 
3062 
3063  BLAZE_INTERNAL_ASSERT( size() == (~rhs).size(), "Invalid vector sizes" );
3064 
3065  const SubType tmp( serial( *this - (~rhs) ) );
3066  assign( tmp );
3067 }
3069 //*************************************************************************************************
3070 
3071 
3072 
3073 
3074 
3075 
3076 
3077 
3078 //=================================================================================================
3079 //
3080 // SPARSECOLUMN OPERATORS
3081 //
3082 //=================================================================================================
3083 
3084 //*************************************************************************************************
3087 template< typename MT, bool SO >
3088 inline void reset( SparseColumn<MT,SO>& column );
3089 
3090 template< typename MT, bool SO >
3091 inline void clear( SparseColumn<MT,SO>& column );
3092 
3093 template< typename MT, bool SO >
3094 inline bool isDefault( const SparseColumn<MT,SO>& column );
3095 
3096 template< typename MT, bool SO >
3097 inline bool isSame( const SparseColumn<MT,SO>& a, const SparseColumn<MT,SO>& b );
3099 //*************************************************************************************************
3100 
3101 
3102 //*************************************************************************************************
3109 template< typename MT // Type of the sparse matrix
3110  , bool SO > // Storage order
3111 inline void reset( SparseColumn<MT,SO>& column )
3112 {
3113  column.reset();
3114 }
3115 //*************************************************************************************************
3116 
3117 
3118 //*************************************************************************************************
3127 template< typename MT // Type of the sparse matrix
3128  , bool SO > // Storage order
3129 inline void clear( SparseColumn<MT,SO>& column )
3130 {
3131  column.reset();
3132 }
3133 //*************************************************************************************************
3134 
3135 
3136 //*************************************************************************************************
3154 template< typename MT // Type of the sparse matrix
3155  , bool SO > // Storage order
3156 inline bool isDefault( const SparseColumn<MT,SO>& column )
3157 {
3159 
3160  const ConstIterator end( column.end() );
3161  for( ConstIterator element=column.begin(); element!=end; ++element )
3162  if( !isDefault( element->value() ) ) return false;
3163  return true;
3164 }
3165 //*************************************************************************************************
3166 
3167 
3168 //*************************************************************************************************
3180 template< typename MT, bool SO >
3181 inline bool isSame( const SparseColumn<MT,SO>& a, const SparseColumn<MT,SO>& b )
3182 {
3183  return ( isSame( a.matrix_, b.matrix_ ) && ( a.col_ == b.col_ ) );
3184 }
3185 //*************************************************************************************************
3186 
3187 
3188 
3189 
3190 //=================================================================================================
3191 //
3192 // SUBVECTORTRAIT SPECIALIZATIONS
3193 //
3194 //=================================================================================================
3195 
3196 //*************************************************************************************************
3198 template< typename MT, bool SO >
3199 struct SubvectorTrait< SparseColumn<MT,SO> >
3200 {
3201  typedef typename SubvectorTrait< typename SparseColumn<MT,SO>::ResultType >::Type Type;
3202 };
3204 //*************************************************************************************************
3205 
3206 } // namespace blaze
3207 
3208 #endif
Constraint on the data type.
Compile time check for numeric types.This type trait tests whether or not the given template paramete...
Definition: IsNumeric.h:98
Header file for mathematical functions.
void subAssign(const DenseVector< VT, false > &rhs)
Default implementation of the subtraction assignment of a dense vector.
Definition: SparseColumn.h:1525
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
Operand matrix_
The sparse matrix containing the column.
Definition: SparseColumn.h:495
Header file for the UNUSED_PARAMETER function template.
Compile time type selection.The If class template selects one of the two given types T2 and T3 depend...
Definition: If.h:112
const DMatDMatMultExpr< T1, T2 > operator*(const DenseMatrix< T1, false > &lhs, const DenseMatrix< T2, false > &rhs)
Multiplication operator for the multiplication of two row-major dense matrices ( ).
Definition: DMatDMatMultExpr.h:4329
Header file for the subtraction trait.
Iterator end()
Returns an iterator just past the last element of the column.
Definition: SparseColumn.h:643
Header file for the SparseVector base class.
ConstIterator cend() const
Returns an iterator just past the last element of the column.
Definition: SparseColumn.h:675
Base template for the ColumnTrait class.
Definition: ColumnTrait.h:114
#define BLAZE_CONSTRAINT_MUST_BE_COLUMN_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a column dense or sparse vector type...
Definition: TransposeFlag.h:159
bool isDefault(const DynamicMatrix< Type, SO > &m)
Returns whether the given dense matrix is in default state.
Definition: DynamicMatrix.h:4642
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
Header file for the IsColumnMajorMatrix type trait.
void assign(const DenseVector< VT, false > &rhs)
Default implementation of the assignment of a dense vector.
Definition: SparseColumn.h:1406
const This & CompositeType
Data type for composite expression templates.
Definition: CompressedMatrix.h:2408
DisableIf< Or< IsComputation< MT >, IsTransExpr< MT > >, typename ColumnExprTrait< MT >::Type >::Type column(Matrix< MT, SO > &matrix, size_t index)
Creating a view on a specific column of the given matrix.
Definition: Column.h:103
void UNUSED_PARAMETER(const T1 &)
Suppression of unused parameter warnings.
Definition: Unused.h:84
Iterator find(size_t index)
Searches for a specific column element.
Definition: SparseColumn.h:1190
const DMatSerialExpr< MT, SO > serial(const DenseMatrix< MT, SO > &dm)
Forces the serial evaluation of the given dense matrix expression dm.
Definition: DMatSerialExpr.h:690
Access proxy for sparse, matrices.The MatrixAccessProxy provides safe access to the elements of a no...
Definition: MatrixAccessProxy.h:86
ResultType::TransposeType TransposeType
Transpose type for expression template evaluations.
Definition: SparseColumn.h:365
#define BLAZE_CONSTRAINT_MUST_NOT_BE_TRANSEXPR_TYPE(T)
Constraint on the data type.In case the given data type T is a transposition expression (i...
Definition: TransExpr.h:118
void clear(DynamicMatrix< Type, SO > &m)
Clearing the given dense matrix.
Definition: DynamicMatrix.h:4615
bool isSame(const Matrix< MT1, SO1 > &a, const Matrix< MT2, SO2 > &b)
Returns whether the two given matrices represent the same observable state.
Definition: Matrix.h:409
Header file for the column base class.
Constraint on the data type.
MT::ConstIterator ConstIterator
Iterator over constant elements.
Definition: SparseColumn.h:377
SelectType< useConst, ConstIterator, typename MT::Iterator >::Type Iterator
Iterator over non-constant elements.
Definition: SparseColumn.h:380
Compile time type selection.The SelectType class template selects one of the two given types T1 and T...
Definition: SelectType.h:59
Header file for the DisableIf class template.
Header file for the multiplication trait.
MT::ElementType ElementType
Type of the column elements.
Definition: SparseColumn.h:366
Header file for the If class template.
Header file for the IsFloatingPoint type trait.
#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
Header file for the Or class template.
Iterator begin()
Returns an iterator to the first element of the column.
Definition: SparseColumn.h:595
void addAssign(const DenseVector< VT, false > &rhs)
Default implementation of the addition assignment of a dense vector.
Definition: SparseColumn.h:1462
size_t size() const
Returns the current size/dimension of the sparse column.
Definition: SparseColumn.h:979
size_t nonZeros(const Matrix< MT, SO > &m)
Returns the total number of non-zero elements in the matrix.
Definition: Matrix.h:224
MT::ConstReference ConstReference
Reference to a constant column value.
Definition: SparseColumn.h:371
Base class for all columns.The Column class serves as a tag for all columns (i.e. dense and sparse co...
Definition: Column.h:64
Header file for the subvector trait.
Iterator lowerBound(size_t index)
Returns an iterator to the first index not less then the given index.
Definition: SparseColumn.h:1233
void assign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the assignment of a matrix to a matrix.
Definition: Matrix.h:271
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:70
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a sparse, N-dimensional vector type...
Definition: SparseVector.h:79
Constraint on the data type.
Iterator upperBound(size_t index)
Returns an iterator to the first index greater then the given index.
Definition: SparseColumn.h:1275
Constraint on the data type.
size_t nonZeros() const
Returns the number of non-zero elements in the column.
Definition: SparseColumn.h:1010
Constraints on the storage order of matrix types.
Constraint on the data type.
Compile time check for floating point data types.This type trait tests whether or not the given templ...
Definition: IsFloatingPoint.h:94
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2406
Constraint on the data type.
Header file for the SelectType class template.
Constraint on the data type.
const Type & ConstReference
Reference to a constant matrix value.
Definition: CompressedMatrix.h:2410
Header file for the EnableIf class template.
Header file for the serial shim.
Header file for the IsNumeric type trait.
const size_t col_
The index of the column in the matrix.
Definition: SparseColumn.h:496
DisableIf< Or< IsComputation< MT >, IsTransExpr< MT > >, typename RowExprTrait< MT >::Type >::Type row(Matrix< MT, SO > &matrix, size_t index)
Creating a view on a specific row of the given matrix.
Definition: Row.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 the IsConst type trait.
void erase(size_t index)
Erasing an element from the sparse column.
Definition: SparseColumn.h:1063
SparseColumn< MT, SO > This
Type of this SparseColumn instance.
Definition: SparseColumn.h:363
Header file for run time assertion macros.
Base template for the AddTrait class.
Definition: AddTrait.h:141
Base template for the MultTrait class.
Definition: MultTrait.h:141
SelectType< IsExpression< MT >::value, MT, MT & >::Type Operand
Composite data type of the dense matrix expression.
Definition: SparseColumn.h:347
Header file for the addition trait.
ColumnTrait< MT >::Type ResultType
Result type for expression template evaluations.
Definition: SparseColumn.h:364
Header file for the division trait.
void addAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the addition assignment of a matrix to a matrix.
Definition: Matrix.h:301
void reserve(size_t n)
Setting the minimum capacity of the sparse column.
Definition: SparseColumn.h:1116
const DenseIterator< Type > operator-(const DenseIterator< Type > &it, ptrdiff_t inc)
Subtraction between a DenseIterator and an integral value.
Definition: DenseIterator.h:585
Substitution Failure Is Not An Error (SFINAE) class.The EnableIf class template is an auxiliary tool ...
Definition: EnableIf.h:184
Header file for the reset shim.
void subAssign(Matrix< MT1, SO1 > &lhs, const Matrix< MT2, SO2 > &rhs)
Default implementation of the subtraction assignment of a matrix to matrix.
Definition: Matrix.h:331
const VT::ElementType max(const SparseVector< VT, TF > &sv)
Returns the largest element of the sparse vector.
Definition: SparseVector.h:408
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2411
Header file for the column trait.
Header file for the isDefault shim.
Iterator insert(size_t index, const ElementType &value)
Inserting an element into the sparse column.
Definition: SparseColumn.h:1046
size_t capacity() const
Returns the maximum capacity of the sparse column.
Definition: SparseColumn.h:993
#define BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION(T)
Constraint on the data type.In case the given data type T requires an intermediate evaluation within ...
Definition: RequiresEvaluation.h:118
ConstIterator cbegin() const
Returns an iterator to the first element of the column.
Definition: SparseColumn.h:627
Compile time check for constant data types.The IsConst type trait tests whether or not the given temp...
Definition: IsConst.h:94
const SparseColumn & CompositeType
Data type for composite expression templates.
Definition: SparseColumn.h:368
Base template for the DivTrait class.
Definition: DivTrait.h:141
MT::ReturnType ReturnType
Return type for expression template evaluations.
Definition: SparseColumn.h:367
#define BLAZE_CONSTRAINT_MUST_BE_DENSE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a dense, N-dimensional vector type...
Definition: DenseVector.h:79
Base class for N-dimensional vectors.The Vector class is a base class for all arbitrarily sized (N-di...
Definition: Forward.h:151
CompressedMatrix< Type,!SO > TransposeType
Transpose type for expression template evaluations.
Definition: CompressedMatrix.h:250
SparseColumn(MT &matrix, size_t index)
The constructor for SparseColumn.
Definition: SparseColumn.h:536
SparseColumn & operator=(const SparseColumn &rhs)
Copy assignment operator for SparseColumn.
Definition: SparseColumn.h:702
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:108
bool operator==(const NegativeAccuracy< A > &lhs, const T &rhs)
Equality comparison between a NegativeAccuracy object and a floating point value. ...
Definition: Accuracy.h:249
This ResultType
Result type for expression template evaluations.
Definition: CompressedMatrix.h:2403
void reset()
Reset to the default initial values.
Definition: SparseColumn.h:1024
bool operator!=(const NegativeAccuracy< A > &lhs, const T &rhs)
Inequality comparison between a NegativeAccuracy object and a floating point value.
Definition: Accuracy.h:289
Header file for basic type definitions.
Reference operator[](size_t index)
Subscript operator for the direct access to the column elements.
Definition: SparseColumn.h:562
SelectType< useConst, ConstReference, typename MT::Reference >::Type Reference
Reference to a non-constant column value.
Definition: SparseColumn.h:374
MatrixAccessProxy< This > Reference
Reference to a non-constant matrix value.
Definition: CompressedMatrix.h:2409
Base template for the SubTrait class.
Definition: SubTrait.h:141
const VT::ElementType min(const SparseVector< VT, TF > &sv)
Returns the smallest element of the sparse vector.
Definition: SparseVector.h:351
bool canAlias(const Other *alias) const
Returns whether the sparse column can alias with the given address alias.
Definition: SparseColumn.h:1365
void append(size_t index, const ElementType &value, bool check=false)
Appending an element to the sparse column.
Definition: SparseColumn.h:1337
bool isAliased(const Other *alias) const
Returns whether the sparse column is aliased with the given address alias.
Definition: SparseColumn.h:1385
size_t extendCapacity() const
Calculating a new sparse column capacity.
Definition: SparseColumn.h:1151
#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
Reference to a specific column of a sparse matrix.The SparseColumn template represents a reference to...
Definition: Forward.h:51
size_t capacity(const Matrix< MT, SO > &m)
Returns the maximum capacity of the matrix.
Definition: Matrix.h:186
Header file for the IsExpression type trait class.
Header file for the FunctionTrace class.