10#ifndef EIGEN_SPARSEVECTOR_H
11#define EIGEN_SPARSEVECTOR_H
13#include "./InternalHeaderCheck.h"
31template<
typename Scalar_,
int Options_,
typename StorageIndex_>
32struct traits<SparseVector<Scalar_, Options_, StorageIndex_> >
34 typedef Scalar_ Scalar;
35 typedef StorageIndex_ StorageIndex;
36 typedef Sparse StorageKind;
37 typedef MatrixXpr XprKind;
41 RowsAtCompileTime = IsColVector ?
Dynamic : 1,
42 ColsAtCompileTime = IsColVector ? 1 :
Dynamic,
43 MaxRowsAtCompileTime = RowsAtCompileTime,
44 MaxColsAtCompileTime = ColsAtCompileTime,
46 SupportedAccessPatterns = InnerRandomAccessPattern
57template<
typename Dest,
typename Src,
58 int AssignmentKind = !bool(Src::IsVectorAtCompileTime) ? SVA_RuntimeSwitch
59 : Src::InnerSizeAtCompileTime==1 ? SVA_Outer
61struct sparse_vector_assign_selector;
65template<
typename Scalar_,
int Options_,
typename StorageIndex_>
70 using Base::convert_index;
73 EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(
SparseVector, +=)
74 EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(
SparseVector, -=)
76 typedef internal::CompressedStorage<Scalar,StorageIndex> Storage;
77 enum { IsColVector = internal::traits<SparseVector>::IsColVector };
83 EIGEN_STRONG_INLINE
Index rows()
const {
return IsColVector ? m_size : 1; }
84 EIGEN_STRONG_INLINE
Index cols()
const {
return IsColVector ? 1 : m_size; }
85 EIGEN_STRONG_INLINE
Index innerSize()
const {
return m_size; }
86 EIGEN_STRONG_INLINE
Index outerSize()
const {
return 1; }
88 EIGEN_STRONG_INLINE
const Scalar* valuePtr()
const {
return m_data.valuePtr(); }
89 EIGEN_STRONG_INLINE Scalar* valuePtr() {
return m_data.valuePtr(); }
91 EIGEN_STRONG_INLINE
const StorageIndex* innerIndexPtr()
const {
return m_data.indexPtr(); }
92 EIGEN_STRONG_INLINE
StorageIndex* innerIndexPtr() {
return m_data.indexPtr(); }
94 inline const StorageIndex* outerIndexPtr()
const {
return 0; }
96 inline const StorageIndex* innerNonZeroPtr()
const {
return 0; }
100 inline Storage& data() {
return m_data; }
102 inline const Storage& data()
const {
return m_data; }
104 inline Scalar coeff(
Index row,
Index col)
const
106 eigen_assert(IsColVector ? (col==0 && row>=0 && row<m_size) : (row==0 && col>=0 && col<m_size));
107 return coeff(IsColVector ? row : col);
109 inline Scalar coeff(
Index i)
const
111 eigen_assert(i>=0 && i<m_size);
117 eigen_assert(IsColVector ? (col==0 && row>=0 && row<m_size) : (row==0 && col>=0 && col<m_size));
118 return coeffRef(IsColVector ? row : col);
129 eigen_assert(i>=0 && i<m_size);
136 typedef typename Base::InnerIterator InnerIterator;
137 typedef typename Base::ReverseInnerIterator ReverseInnerIterator;
139 inline void setZero() { m_data.clear(); }
144 inline void startVec(
Index outer)
146 EIGEN_UNUSED_VARIABLE(outer);
147 eigen_assert(outer==0);
150 inline Scalar& insertBackByOuterInner(
Index outer,
Index inner)
152 EIGEN_UNUSED_VARIABLE(outer);
153 eigen_assert(outer==0);
154 return insertBack(inner);
156 inline Scalar& insertBack(
Index i)
159 return m_data.value(m_data.size()-1);
162 Scalar& insertBackByOuterInnerUnordered(
Index outer,
Index inner)
164 EIGEN_UNUSED_VARIABLE(outer);
165 eigen_assert(outer==0);
166 return insertBackUnordered(inner);
168 inline Scalar& insertBackUnordered(
Index i)
171 return m_data.value(m_data.size()-1);
176 eigen_assert(IsColVector ? (col==0 && row>=0 && row<m_size) : (row==0 && col>=0 && col<m_size));
178 Index inner = IsColVector ? row : col;
179 Index outer = IsColVector ? col : row;
180 EIGEN_ONLY_USED_FOR_DEBUG(outer);
181 eigen_assert(outer==0);
182 return insert(inner);
184 Scalar& insert(
Index i)
186 eigen_assert(i>=0 && i<m_size);
191 m_data.resize(p+2,1);
193 while ( (p >= startId) && (m_data.index(p) > i) )
195 m_data.index(p+1) = m_data.index(p);
196 m_data.value(p+1) = m_data.value(p);
199 m_data.index(p+1) = convert_index(i);
200 m_data.value(p+1) = 0;
201 return m_data.value(p+1);
206 inline void reserve(
Index reserveSize) { m_data.reserve(reserveSize); }
209 inline void finalize() {}
213 return prune([&](
const Scalar& val){
return !internal::isMuchSmallerThan(val, reference, epsilon); });
227 Index n = m_data.size();
228 for (
Index i = 0; i < n; ++i)
230 if (keep_predicate(m_data.value(i)))
232 m_data.value(k) = std::move(m_data.value(i));
233 m_data.index(k) = m_data.index(i);
251 eigen_assert((IsColVector ? cols : rows)==1 &&
"Outer dimension must equal 1");
252 resize(IsColVector ? rows : cols);
274 if (newSize < m_size)
277 while (i<m_data.size() && m_data.index(i)<newSize) ++i;
285 inline SparseVector() : m_size(0) {
resize(0); }
289 inline SparseVector(
Index rows,
Index cols) : m_size(0) {
resize(rows,cols); }
291 template<
typename OtherDerived>
292 inline SparseVector(
const SparseMatrixBase<OtherDerived>& other)
295 #ifdef EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
296 EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
298 *
this = other.derived();
301 inline SparseVector(
const SparseVector& other)
302 : Base(other), m_size(0)
304 *
this = other.derived();
313 std::swap(m_size, other.m_size);
314 m_data.swap(other.m_data);
317 template<
int OtherOptions>
321 std::swap(m_size, other.m_innerSize);
322 m_data.swap(other.m_data);
325 inline SparseVector& operator=(
const SparseVector& other)
327 if (other.isRValue())
329 swap(other.const_cast_derived());
334 m_data = other.m_data;
339 template<
typename OtherDerived>
340 inline SparseVector& operator=(
const SparseMatrixBase<OtherDerived>& other)
342 SparseVector tmp(other.size());
343 internal::sparse_vector_assign_selector<SparseVector,OtherDerived>::run(tmp,other.derived());
348 #ifndef EIGEN_PARSED_BY_DOXYGEN
349 template<
typename Lhs,
typename Rhs>
350 inline SparseVector& operator=(
const SparseSparseProduct<Lhs,Rhs>& product)
352 return Base::operator=(product);
356 friend std::ostream & operator << (std::ostream & s,
const SparseVector& m)
358 for (
Index i=0; i<m.nonZeros(); ++i)
359 s <<
"(" << m.m_data.value(i) <<
"," << m.m_data.index(i) <<
") ";
373 EIGEN_DEPRECATED
void startFill(
Index reserve)
376 m_data.reserve(reserve);
380 EIGEN_DEPRECATED Scalar& fill(
Index r,
Index c)
382 eigen_assert(r==0 || c==0);
383 return fill(IsColVector ? r : c);
387 EIGEN_DEPRECATED Scalar& fill(
Index i)
390 return m_data.value(m_data.size()-1);
394 EIGEN_DEPRECATED Scalar& fillrand(
Index r,
Index c)
396 eigen_assert(r==0 || c==0);
397 return fillrand(IsColVector ? r : c);
401 EIGEN_DEPRECATED Scalar& fillrand(
Index i)
407 EIGEN_DEPRECATED
void endFill() {}
411 EIGEN_DEPRECATED Storage& _data() {
return m_data; }
413 EIGEN_DEPRECATED
const Storage& _data()
const {
return m_data; }
415# ifdef EIGEN_SPARSEVECTOR_PLUGIN
416# include EIGEN_SPARSEVECTOR_PLUGIN
420 EIGEN_STATIC_ASSERT(NumTraits<StorageIndex>::IsSigned,THE_INDEX_TYPE_MUST_BE_A_SIGNED_TYPE)
421 EIGEN_STATIC_ASSERT((Options_&(
ColMajor|
RowMajor))==Options,INVALID_MATRIX_TEMPLATE_PARAMETERS)
429template<
typename Scalar_,
int Options_,
typename Index_>
430struct evaluator<SparseVector<Scalar_,Options_,Index_> >
431 : evaluator_base<SparseVector<Scalar_,Options_,Index_> >
433 typedef SparseVector<Scalar_,Options_,Index_> SparseVectorType;
434 typedef evaluator_base<SparseVectorType> Base;
435 typedef typename SparseVectorType::InnerIterator InnerIterator;
436 typedef typename SparseVectorType::ReverseInnerIterator ReverseInnerIterator;
439 CoeffReadCost = NumTraits<Scalar_>::ReadCost,
440 Flags = SparseVectorType::Flags
443 evaluator() : Base() {}
445 explicit evaluator(
const SparseVectorType &mat) : m_matrix(&mat)
447 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
450 inline Index nonZerosEstimate()
const {
451 return m_matrix->nonZeros();
454 operator SparseVectorType&() {
return m_matrix->const_cast_derived(); }
455 operator const SparseVectorType&()
const {
return *m_matrix; }
457 const SparseVectorType *m_matrix;
460template<
typename Dest,
typename Src>
461struct sparse_vector_assign_selector<Dest,Src,SVA_Inner> {
462 static void run(Dest& dst,
const Src& src) {
463 eigen_internal_assert(src.innerSize()==src.size());
464 typedef internal::evaluator<Src> SrcEvaluatorType;
465 SrcEvaluatorType srcEval(src);
466 for(
typename SrcEvaluatorType::InnerIterator it(srcEval, 0); it; ++it)
467 dst.insert(it.index()) = it.value();
471template<
typename Dest,
typename Src>
472struct sparse_vector_assign_selector<Dest,Src,SVA_Outer> {
473 static void run(Dest& dst,
const Src& src) {
474 eigen_internal_assert(src.outerSize()==src.size());
475 typedef internal::evaluator<Src> SrcEvaluatorType;
476 SrcEvaluatorType srcEval(src);
477 for(
Index i=0; i<src.size(); ++i)
479 typename SrcEvaluatorType::InnerIterator it(srcEval, i);
481 dst.insert(i) = it.value();
486template<
typename Dest,
typename Src>
487struct sparse_vector_assign_selector<Dest,Src,SVA_RuntimeSwitch> {
488 static void run(Dest& dst,
const Src& src) {
489 if(src.outerSize()==1) sparse_vector_assign_selector<Dest,Src,SVA_Inner>::run(dst, src);
490 else sparse_vector_assign_selector<Dest,Src,SVA_Outer>::run(dst, src);
Common base class for sparse [compressed]-{row|column}-storage format.
Definition: SparseCompressedBase.h:40
internal::traits< SparseVector< Scalar_, Options_, StorageIndex_ > >::StorageIndex StorageIndex
Definition: SparseMatrixBase.h:45
Index size() const
Definition: SparseMatrixBase.h:183
A versatible sparse matrix representation.
Definition: SparseMatrix.h:100
Index outerSize() const
Definition: SparseMatrix.h:147
a sparse vector class
Definition: SparseVector.h:68
void conservativeResize(Index newSize)
Definition: SparseVector.h:272
Scalar & coeffRef(Index i)
Definition: SparseVector.h:127
Index prune(const Scalar &reference, const RealScalar &epsilon=NumTraits< RealScalar >::dummy_precision())
Definition: SparseVector.h:212
void resize(Index rows, Index cols)
Definition: SparseVector.h:249
~SparseVector()
Definition: SparseVector.h:365
void swap(SparseVector &other)
Definition: SparseVector.h:311
Index prune(F &&keep_predicate)
Prunes the entries of the vector based on a predicate
Definition: SparseVector.h:224
Index nonZeros() const
Definition: SparseVector.h:142
void resize(Index newSize)
Definition: SparseVector.h:259
Scalar sum() const
Definition: SparseRedux.h:43
@ ColMajor
Definition: Constants.h:321
@ RowMajor
Definition: Constants.h:323
const unsigned int LvalueBit
Definition: Constants.h:146
const unsigned int RowMajorBit
Definition: Constants.h:68
const unsigned int CompressedAccessBit
Definition: Constants.h:193
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const int Dynamic
Definition: Constants.h:24
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:235