10 #ifndef EIGEN_SPARSEVECTOR_H 11 #define EIGEN_SPARSEVECTOR_H 29 template<
typename _Scalar,
int _Options,
typename _StorageIndex>
32 typedef _Scalar Scalar;
33 typedef _StorageIndex StorageIndex;
39 RowsAtCompileTime = IsColVector ?
Dynamic : 1,
40 ColsAtCompileTime = IsColVector ? 1 : Dynamic,
41 MaxRowsAtCompileTime = RowsAtCompileTime,
42 MaxColsAtCompileTime = ColsAtCompileTime,
44 SupportedAccessPatterns = InnerRandomAccessPattern
55 template<
typename Dest,
typename Src,
56 int AssignmentKind = !bool(Src::IsVectorAtCompileTime) ? SVA_RuntimeSwitch
57 : Src::InnerSizeAtCompileTime==1 ? SVA_Outer
63 template<
typename _Scalar,
int _Options,
typename _StorageIndex>
68 using Base::convert_index;
71 EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(
SparseVector, +=)
72 EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(SparseVector, -=)
81 EIGEN_STRONG_INLINE
Index rows()
const {
return IsColVector ? m_size : 1; }
82 EIGEN_STRONG_INLINE
Index cols()
const {
return IsColVector ? 1 : m_size; }
83 EIGEN_STRONG_INLINE
Index innerSize()
const {
return m_size; }
84 EIGEN_STRONG_INLINE
Index outerSize()
const {
return 1; }
86 EIGEN_STRONG_INLINE
const Scalar* valuePtr()
const {
return m_data.valuePtr(); }
87 EIGEN_STRONG_INLINE Scalar* valuePtr() {
return m_data.valuePtr(); }
89 EIGEN_STRONG_INLINE
const StorageIndex* innerIndexPtr()
const {
return m_data.indexPtr(); }
90 EIGEN_STRONG_INLINE StorageIndex* innerIndexPtr() {
return m_data.indexPtr(); }
92 inline const StorageIndex* outerIndexPtr()
const {
return 0; }
93 inline StorageIndex* outerIndexPtr() {
return 0; }
94 inline const StorageIndex* innerNonZeroPtr()
const {
return 0; }
95 inline StorageIndex* innerNonZeroPtr() {
return 0; }
98 inline Storage& data() {
return m_data; }
100 inline const Storage& data()
const {
return m_data; }
102 inline Scalar coeff(
Index row,
Index col)
const 104 eigen_assert(IsColVector ? (col==0 && row>=0 && row<m_size) : (row==0 && col>=0 && col<m_size));
105 return coeff(IsColVector ? row : col);
107 inline Scalar coeff(
Index i)
const 109 eigen_assert(i>=0 && i<m_size);
110 return m_data.at(StorageIndex(i));
115 eigen_assert(IsColVector ? (col==0 && row>=0 && row<m_size) : (row==0 && col>=0 && col<m_size));
116 return coeffRef(IsColVector ? row : col);
127 eigen_assert(i>=0 && i<m_size);
129 return m_data.atWithInsertion(StorageIndex(i));
137 inline void setZero() { m_data.clear(); }
142 inline void startVec(
Index outer)
144 EIGEN_UNUSED_VARIABLE(outer);
145 eigen_assert(outer==0);
148 inline Scalar& insertBackByOuterInner(
Index outer,
Index inner)
150 EIGEN_UNUSED_VARIABLE(outer);
151 eigen_assert(outer==0);
152 return insertBack(inner);
154 inline Scalar& insertBack(
Index i)
157 return m_data.value(m_data.size()-1);
160 Scalar& insertBackByOuterInnerUnordered(
Index outer,
Index inner)
162 EIGEN_UNUSED_VARIABLE(outer);
163 eigen_assert(outer==0);
164 return insertBackUnordered(inner);
166 inline Scalar& insertBackUnordered(
Index i)
169 return m_data.value(m_data.size()-1);
174 eigen_assert(IsColVector ? (col==0 && row>=0 && row<m_size) : (row==0 && col>=0 && col<m_size));
176 Index inner = IsColVector ? row : col;
177 Index outer = IsColVector ? col : row;
178 EIGEN_ONLY_USED_FOR_DEBUG(outer);
179 eigen_assert(outer==0);
180 return insert(inner);
182 Scalar& insert(
Index i)
184 eigen_assert(i>=0 && i<m_size);
189 m_data.resize(p+2,1);
191 while ( (p >= startId) && (m_data.index(p) > i) )
193 m_data.index(p+1) = m_data.index(p);
194 m_data.value(p+1) = m_data.value(p);
197 m_data.index(p+1) = convert_index(i);
198 m_data.value(p+1) = 0;
199 return m_data.value(p+1);
204 inline void reserve(
Index reserveSize) { m_data.reserve(reserveSize); }
207 inline void finalize() {}
212 m_data.prune(reference,epsilon);
225 eigen_assert((IsColVector ? cols : rows)==1 &&
"Outer dimension must equal 1");
226 resize(IsColVector ? rows : cols);
248 if (newSize < m_size)
251 while (i<m_data.size() && m_data.index(i)<newSize) ++i;
257 void resizeNonZeros(
Index size) { m_data.resize(size); }
259 inline SparseVector() : m_size(0) { check_template_parameters(); resize(0); }
261 explicit inline SparseVector(
Index size) : m_size(0) { check_template_parameters(); resize(size); }
263 inline SparseVector(
Index rows,
Index cols) : m_size(0) { check_template_parameters(); resize(rows,cols); }
265 template<
typename OtherDerived>
269 #ifdef EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN 270 EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
272 check_template_parameters();
273 *
this = other.derived();
277 : Base(other), m_size(0)
279 check_template_parameters();
280 *
this = other.derived();
289 std::swap(m_size, other.m_size);
290 m_data.swap(other.m_data);
293 template<
int OtherOptions>
297 std::swap(m_size, other.m_innerSize);
298 m_data.swap(other.m_data);
303 if (other.isRValue())
305 swap(other.const_cast_derived());
309 resize(other.
size());
310 m_data = other.m_data;
315 template<
typename OtherDerived>
324 #ifndef EIGEN_PARSED_BY_DOXYGEN 325 template<
typename Lhs,
typename Rhs>
328 return Base::operator=(product);
332 friend std::ostream & operator << (std::ostream & s,
const SparseVector& m)
335 s <<
"(" << m.m_data.value(i) <<
"," << m.m_data.index(i) <<
") ";
349 EIGEN_DEPRECATED
void startFill(
Index reserve)
352 m_data.reserve(reserve);
356 EIGEN_DEPRECATED Scalar& fill(
Index r,
Index c)
358 eigen_assert(r==0 || c==0);
359 return fill(IsColVector ? r : c);
363 EIGEN_DEPRECATED Scalar& fill(
Index i)
366 return m_data.value(m_data.size()-1);
370 EIGEN_DEPRECATED Scalar& fillrand(
Index r,
Index c)
372 eigen_assert(r==0 || c==0);
373 return fillrand(IsColVector ? r : c);
377 EIGEN_DEPRECATED Scalar& fillrand(
Index i)
383 EIGEN_DEPRECATED
void endFill() {}
387 EIGEN_DEPRECATED
Storage& _data() {
return m_data; }
389 EIGEN_DEPRECATED
const Storage& _data()
const {
return m_data; }
391 # ifdef EIGEN_SPARSEVECTOR_PLUGIN 392 # include EIGEN_SPARSEVECTOR_PLUGIN 397 static void check_template_parameters()
400 EIGEN_STATIC_ASSERT((_Options&(
ColMajor|
RowMajor))==Options,INVALID_MATRIX_TEMPLATE_PARAMETERS);
409 template<
typename _Scalar,
int _Options,
typename _Index>
420 Flags = SparseVectorType::Flags
425 explicit evaluator(
const SparseVectorType &mat) : m_matrix(&mat)
427 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
430 inline Index nonZerosEstimate()
const {
431 return m_matrix->nonZeros();
434 operator SparseVectorType&() {
return m_matrix->const_cast_derived(); }
435 operator const SparseVectorType&()
const {
return *m_matrix; }
437 const SparseVectorType *m_matrix;
440 template<
typename Dest,
typename Src>
442 static void run(Dest& dst,
const Src& src) {
443 eigen_internal_assert(src.innerSize()==src.size());
445 SrcEvaluatorType srcEval(src);
446 for(
typename SrcEvaluatorType::InnerIterator it(srcEval, 0); it; ++it)
447 dst.insert(it.index()) = it.value();
451 template<
typename Dest,
typename Src>
453 static void run(Dest& dst,
const Src& src) {
454 eigen_internal_assert(src.outerSize()==src.size());
456 SrcEvaluatorType srcEval(src);
457 for(
Index i=0; i<src.size(); ++i)
459 typename SrcEvaluatorType::InnerIterator it(srcEval, i);
461 dst.insert(i) = it.value();
466 template<
typename Dest,
typename Src>
468 static void run(Dest& dst,
const Src& src) {
478 #endif // EIGEN_SPARSEVECTOR_H Index nonZeros() const
Definition: SparseVector.h:140
Storage order is column major (see TopicStorageOrders).
Definition: Constants.h:320
const unsigned int CompressedAccessBit
Means that the underlying coefficients can be accessed through pointers to the sparse (un)compressed ...
Definition: Constants.h:186
Definition: SparseUtil.h:61
A versatible sparse matrix representation.
Definition: SparseMatrix.h:92
const unsigned int LvalueBit
Means the expression has a coeffRef() method, i.e.
Definition: Constants.h:139
Definition: CoreEvaluators.h:90
Scalar & coeffRef(Index i)
Definition: SparseVector.h:125
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:85
Holds information about the various numeric (i.e.
Definition: NumTraits.h:150
Definition: CompressedStorage.h:22
void conservativeResize(Index newSize)
Resizes the sparse vector to newSize, while leaving old values untouched.
Definition: SparseVector.h:246
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:37
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition: Constants.h:61
Definition: SparseVector.h:59
~SparseVector()
Destructor.
Definition: SparseVector.h:341
void swap(SparseVector &other)
Swaps the values of *this and other.
Definition: SparseVector.h:287
Definition: CoreEvaluators.h:109
The type used to identify a matrix expression.
Definition: Constants.h:506
Index size() const
Definition: SparseMatrixBase.h:172
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:281
a sparse vector class
Definition: SparseUtil.h:54
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Definition: SparseCompressedBase.h:136
void resize(Index newSize)
Resizes the sparse vector to newSize This method deletes all entries, thus leaving an empty sparse ve...
Definition: SparseVector.h:233
The type used to identify a general sparse storage.
Definition: Constants.h:494
void resize(Index rows, Index cols)
Resizes the sparse vector to rows x cols.
Definition: SparseVector.h:223
Definition: BandTriangularSolver.h:13
Definition: AssignEvaluator.h:740
Storage order is row major (see TopicStorageOrders).
Definition: Constants.h:322
const int Dynamic
This value means that a positive quantity (e.g., a size) is not known at compile-time, and that instead the value is stored in some runtime variable.
Definition: Constants.h:21
void prune(const Scalar &reference, const RealScalar &epsilon=NumTraits< RealScalar >::dummy_precision())
Suppresses all nonzeros which are much smaller than reference under the tolerence epsilon...
Definition: SparseVector.h:210
Index outerSize() const
Definition: SparseMatrix.h:139
NumTraits< Scalar >::Real RealScalar
This is the "real scalar" type; if the Scalar type is already real numbers (e.g.
Definition: SparseMatrixBase.h:119
Common base class for sparse [compressed]-{row|column}-storage format.
Definition: SparseCompressedBase.h:15
Definition: ForwardDeclarations.h:17
Definition: SparseCompressedBase.h:214