aboutsummaryrefslogtreecommitdiff
path: root/Eigen/src/SparseCore/SparseVector.h
diff options
context:
space:
mode:
Diffstat (limited to 'Eigen/src/SparseCore/SparseVector.h')
-rw-r--r--Eigen/src/SparseCore/SparseVector.h213
1 files changed, 122 insertions, 91 deletions
diff --git a/Eigen/src/SparseCore/SparseVector.h b/Eigen/src/SparseCore/SparseVector.h
index 7e15c814b..19b0fbc9d 100644
--- a/Eigen/src/SparseCore/SparseVector.h
+++ b/Eigen/src/SparseCore/SparseVector.h
@@ -1,7 +1,7 @@
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
-// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
+// Copyright (C) 2008-2015 Gael Guennebaud <gael.guennebaud@inria.fr>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
@@ -22,15 +22,15 @@ namespace Eigen {
* See http://www.netlib.org/linalg/html_templates/node91.html for details on the storage scheme.
*
* This class can be extended with the help of the plugin mechanism described on the page
- * \ref TopicCustomizingEigen by defining the preprocessor symbol \c EIGEN_SPARSEVECTOR_PLUGIN.
+ * \ref TopicCustomizing_Plugins by defining the preprocessor symbol \c EIGEN_SPARSEVECTOR_PLUGIN.
*/
namespace internal {
-template<typename _Scalar, int _Options, typename _Index>
-struct traits<SparseVector<_Scalar, _Options, _Index> >
+template<typename _Scalar, int _Options, typename _StorageIndex>
+struct traits<SparseVector<_Scalar, _Options, _StorageIndex> >
{
typedef _Scalar Scalar;
- typedef _Index Index;
+ typedef _StorageIndex StorageIndex;
typedef Sparse StorageKind;
typedef MatrixXpr XprKind;
enum {
@@ -40,8 +40,7 @@ struct traits<SparseVector<_Scalar, _Options, _Index> >
ColsAtCompileTime = IsColVector ? 1 : Dynamic,
MaxRowsAtCompileTime = RowsAtCompileTime,
MaxColsAtCompileTime = ColsAtCompileTime,
- Flags = _Options | NestByRefBit | LvalueBit | (IsColVector ? 0 : RowMajorBit),
- CoeffReadCost = NumTraits<Scalar>::ReadCost,
+ Flags = _Options | NestByRefBit | LvalueBit | (IsColVector ? 0 : RowMajorBit) | CompressedAccessBit,
SupportedAccessPatterns = InnerRandomAccessPattern
};
};
@@ -61,18 +60,18 @@ struct sparse_vector_assign_selector;
}
-template<typename _Scalar, int _Options, typename _Index>
+template<typename _Scalar, int _Options, typename _StorageIndex>
class SparseVector
- : public SparseMatrixBase<SparseVector<_Scalar, _Options, _Index> >
+ : public SparseCompressedBase<SparseVector<_Scalar, _Options, _StorageIndex> >
{
- typedef SparseMatrixBase<SparseVector> SparseBase;
-
+ typedef SparseCompressedBase<SparseVector> Base;
+ using Base::convert_index;
public:
EIGEN_SPARSE_PUBLIC_INTERFACE(SparseVector)
EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(SparseVector, +=)
EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(SparseVector, -=)
- typedef internal::CompressedStorage<Scalar,Index> Storage;
+ typedef internal::CompressedStorage<Scalar,StorageIndex> Storage;
enum { IsColVector = internal::traits<SparseVector>::IsColVector };
enum {
@@ -84,11 +83,16 @@ class SparseVector
EIGEN_STRONG_INLINE Index innerSize() const { return m_size; }
EIGEN_STRONG_INLINE Index outerSize() const { return 1; }
- EIGEN_STRONG_INLINE const Scalar* valuePtr() const { return &m_data.value(0); }
- EIGEN_STRONG_INLINE Scalar* valuePtr() { return &m_data.value(0); }
+ EIGEN_STRONG_INLINE const Scalar* valuePtr() const { return m_data.valuePtr(); }
+ EIGEN_STRONG_INLINE Scalar* valuePtr() { return m_data.valuePtr(); }
- EIGEN_STRONG_INLINE const Index* innerIndexPtr() const { return &m_data.index(0); }
- EIGEN_STRONG_INLINE Index* innerIndexPtr() { return &m_data.index(0); }
+ EIGEN_STRONG_INLINE const StorageIndex* innerIndexPtr() const { return m_data.indexPtr(); }
+ EIGEN_STRONG_INLINE StorageIndex* innerIndexPtr() { return m_data.indexPtr(); }
+
+ inline const StorageIndex* outerIndexPtr() const { return 0; }
+ inline StorageIndex* outerIndexPtr() { return 0; }
+ inline const StorageIndex* innerNonZeroPtr() const { return 0; }
+ inline StorageIndex* innerNonZeroPtr() { return 0; }
/** \internal */
inline Storage& data() { return m_data; }
@@ -103,13 +107,13 @@ class SparseVector
inline Scalar coeff(Index i) const
{
eigen_assert(i>=0 && i<m_size);
- return m_data.at(i);
+ return m_data.at(StorageIndex(i));
}
inline Scalar& coeffRef(Index row, Index col)
{
eigen_assert(IsColVector ? (col==0 && row>=0 && row<m_size) : (row==0 && col>=0 && col<m_size));
- return coeff(IsColVector ? row : col);
+ return coeffRef(IsColVector ? row : col);
}
/** \returns a reference to the coefficient value at given index \a i
@@ -121,18 +125,19 @@ class SparseVector
inline Scalar& coeffRef(Index i)
{
eigen_assert(i>=0 && i<m_size);
- return m_data.atWithInsertion(i);
+
+ return m_data.atWithInsertion(StorageIndex(i));
}
public:
- class InnerIterator;
- class ReverseInnerIterator;
+ typedef typename Base::InnerIterator InnerIterator;
+ typedef typename Base::ReverseInnerIterator ReverseInnerIterator;
inline void setZero() { m_data.clear(); }
/** \returns the number of non zero coefficients */
- inline Index nonZeros() const { return static_cast<Index>(m_data.size()); }
+ inline Index nonZeros() const { return m_data.size(); }
inline void startVec(Index outer)
{
@@ -151,6 +156,18 @@ class SparseVector
m_data.append(0, i);
return m_data.value(m_data.size()-1);
}
+
+ Scalar& insertBackByOuterInnerUnordered(Index outer, Index inner)
+ {
+ EIGEN_UNUSED_VARIABLE(outer);
+ eigen_assert(outer==0);
+ return insertBackUnordered(inner);
+ }
+ inline Scalar& insertBackUnordered(Index i)
+ {
+ m_data.append(0, i);
+ return m_data.value(m_data.size()-1);
+ }
inline Scalar& insert(Index row, Index col)
{
@@ -158,6 +175,7 @@ class SparseVector
Index inner = IsColVector ? row : col;
Index outer = IsColVector ? col : row;
+ EIGEN_ONLY_USED_FOR_DEBUG(outer);
eigen_assert(outer==0);
return insert(inner);
}
@@ -176,7 +194,7 @@ class SparseVector
m_data.value(p+1) = m_data.value(p);
--p;
}
- m_data.index(p+1) = i;
+ m_data.index(p+1) = convert_index(i);
m_data.value(p+1) = 0;
return m_data.value(p+1);
}
@@ -188,28 +206,59 @@ class SparseVector
inline void finalize() {}
+ /** \copydoc SparseMatrix::prune(const Scalar&,const RealScalar&) */
void prune(const Scalar& reference, const RealScalar& epsilon = NumTraits<RealScalar>::dummy_precision())
{
m_data.prune(reference,epsilon);
}
+ /** Resizes the sparse vector to \a rows x \a cols
+ *
+ * This method is provided for compatibility with matrices.
+ * For a column vector, \a cols must be equal to 1.
+ * For a row vector, \a rows must be equal to 1.
+ *
+ * \sa resize(Index)
+ */
void resize(Index rows, Index cols)
{
- eigen_assert(rows==1 || cols==1);
+ eigen_assert((IsColVector ? cols : rows)==1 && "Outer dimension must equal 1");
resize(IsColVector ? rows : cols);
}
+ /** Resizes the sparse vector to \a newSize
+ * This method deletes all entries, thus leaving an empty sparse vector
+ *
+ * \sa conservativeResize(), setZero() */
void resize(Index newSize)
{
m_size = newSize;
m_data.clear();
}
+ /** Resizes the sparse vector to \a newSize, while leaving old values untouched.
+ *
+ * If the size of the vector is decreased, then the storage of the out-of bounds coefficients is kept and reserved.
+ * Call .data().squeeze() to free extra memory.
+ *
+ * \sa reserve(), setZero()
+ */
+ void conservativeResize(Index newSize)
+ {
+ if (newSize < m_size)
+ {
+ Index i = 0;
+ while (i<m_data.size() && m_data.index(i)<newSize) ++i;
+ m_data.resize(i);
+ }
+ m_size = newSize;
+ }
+
void resizeNonZeros(Index size) { m_data.resize(size); }
inline SparseVector() : m_size(0) { check_template_parameters(); resize(0); }
- inline SparseVector(Index size) : m_size(0) { check_template_parameters(); resize(size); }
+ explicit inline SparseVector(Index size) : m_size(0) { check_template_parameters(); resize(size); }
inline SparseVector(Index rows, Index cols) : m_size(0) { check_template_parameters(); resize(rows,cols); }
@@ -217,12 +266,15 @@ class SparseVector
inline SparseVector(const SparseMatrixBase<OtherDerived>& other)
: m_size(0)
{
+ #ifdef EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
+ EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
+ #endif
check_template_parameters();
*this = other.derived();
}
inline SparseVector(const SparseVector& other)
- : SparseBase(other), m_size(0)
+ : Base(other), m_size(0)
{
check_template_parameters();
*this = other.derived();
@@ -238,6 +290,14 @@ class SparseVector
m_data.swap(other.m_data);
}
+ template<int OtherOptions>
+ inline void swap(SparseMatrix<Scalar,OtherOptions,StorageIndex>& other)
+ {
+ eigen_assert(other.outerSize()==1);
+ std::swap(m_size, other.m_innerSize);
+ m_data.swap(other.m_data);
+ }
+
inline SparseVector& operator=(const SparseVector& other)
{
if (other.isRValue())
@@ -336,7 +396,7 @@ protected:
static void check_template_parameters()
{
- EIGEN_STATIC_ASSERT(NumTraits<Index>::IsSigned,THE_INDEX_TYPE_MUST_BE_A_SIGNED_TYPE);
+ EIGEN_STATIC_ASSERT(NumTraits<StorageIndex>::IsSigned,THE_INDEX_TYPE_MUST_BE_A_SIGNED_TYPE);
EIGEN_STATIC_ASSERT((_Options&(ColMajor|RowMajor))==Options,INVALID_MATRIX_TEMPLATE_PARAMETERS);
}
@@ -344,77 +404,46 @@ protected:
Index m_size;
};
-template<typename Scalar, int _Options, typename _Index>
-class SparseVector<Scalar,_Options,_Index>::InnerIterator
-{
- public:
- InnerIterator(const SparseVector& vec, Index outer=0)
- : m_data(vec.m_data), m_id(0), m_end(static_cast<Index>(m_data.size()))
- {
- EIGEN_UNUSED_VARIABLE(outer);
- eigen_assert(outer==0);
- }
-
- InnerIterator(const internal::CompressedStorage<Scalar,Index>& data)
- : m_data(data), m_id(0), m_end(static_cast<Index>(m_data.size()))
- {}
-
- inline InnerIterator& operator++() { m_id++; return *this; }
-
- inline Scalar value() const { return m_data.value(m_id); }
- inline Scalar& valueRef() { return const_cast<Scalar&>(m_data.value(m_id)); }
-
- inline Index index() const { return m_data.index(m_id); }
- inline Index row() const { return IsColVector ? index() : 0; }
- inline Index col() const { return IsColVector ? 0 : index(); }
-
- inline operator bool() const { return (m_id < m_end); }
-
- protected:
- const internal::CompressedStorage<Scalar,Index>& m_data;
- Index m_id;
- const Index m_end;
-};
+namespace internal {
-template<typename Scalar, int _Options, typename _Index>
-class SparseVector<Scalar,_Options,_Index>::ReverseInnerIterator
+template<typename _Scalar, int _Options, typename _Index>
+struct evaluator<SparseVector<_Scalar,_Options,_Index> >
+ : evaluator_base<SparseVector<_Scalar,_Options,_Index> >
{
- public:
- ReverseInnerIterator(const SparseVector& vec, Index outer=0)
- : m_data(vec.m_data), m_id(static_cast<Index>(m_data.size())), m_start(0)
- {
- EIGEN_UNUSED_VARIABLE(outer);
- eigen_assert(outer==0);
- }
-
- ReverseInnerIterator(const internal::CompressedStorage<Scalar,Index>& data)
- : m_data(data), m_id(static_cast<Index>(m_data.size())), m_start(0)
- {}
-
- inline ReverseInnerIterator& operator--() { m_id--; return *this; }
-
- inline Scalar value() const { return m_data.value(m_id-1); }
- inline Scalar& valueRef() { return const_cast<Scalar&>(m_data.value(m_id-1)); }
-
- inline Index index() const { return m_data.index(m_id-1); }
- inline Index row() const { return IsColVector ? index() : 0; }
- inline Index col() const { return IsColVector ? 0 : index(); }
-
- inline operator bool() const { return (m_id > m_start); }
+ typedef SparseVector<_Scalar,_Options,_Index> SparseVectorType;
+ typedef evaluator_base<SparseVectorType> Base;
+ typedef typename SparseVectorType::InnerIterator InnerIterator;
+ typedef typename SparseVectorType::ReverseInnerIterator ReverseInnerIterator;
+
+ enum {
+ CoeffReadCost = NumTraits<_Scalar>::ReadCost,
+ Flags = SparseVectorType::Flags
+ };
- protected:
- const internal::CompressedStorage<Scalar,Index>& m_data;
- Index m_id;
- const Index m_start;
+ evaluator() : Base() {}
+
+ explicit evaluator(const SparseVectorType &mat) : m_matrix(&mat)
+ {
+ EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
+ }
+
+ inline Index nonZerosEstimate() const {
+ return m_matrix->nonZeros();
+ }
+
+ operator SparseVectorType&() { return m_matrix->const_cast_derived(); }
+ operator const SparseVectorType&() const { return *m_matrix; }
+
+ const SparseVectorType *m_matrix;
};
-namespace internal {
-
template< typename Dest, typename Src>
struct sparse_vector_assign_selector<Dest,Src,SVA_Inner> {
static void run(Dest& dst, const Src& src) {
eigen_internal_assert(src.innerSize()==src.size());
- for(typename Src::InnerIterator it(src, 0); it; ++it)
+ typedef internal::evaluator<Src> SrcEvaluatorType;
+ SrcEvaluatorType srcEval(src);
+ for(typename SrcEvaluatorType::InnerIterator it(srcEval, 0); it; ++it)
dst.insert(it.index()) = it.value();
}
};
@@ -423,9 +452,11 @@ template< typename Dest, typename Src>
struct sparse_vector_assign_selector<Dest,Src,SVA_Outer> {
static void run(Dest& dst, const Src& src) {
eigen_internal_assert(src.outerSize()==src.size());
- for(typename Dest::Index i=0; i<src.size(); ++i)
+ typedef internal::evaluator<Src> SrcEvaluatorType;
+ SrcEvaluatorType srcEval(src);
+ for(Index i=0; i<src.size(); ++i)
{
- typename Src::InnerIterator it(src, i);
+ typename SrcEvaluatorType::InnerIterator it(srcEval, i);
if(it)
dst.insert(i) = it.value();
}