SparseVector.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2015 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_SPARSEVECTOR_H
11 #define EIGEN_SPARSEVECTOR_H
12 
13 namespace Eigen {
14 
28 namespace internal {
29 template<typename _Scalar, int _Options, typename _StorageIndex>
30 struct traits<SparseVector<_Scalar, _Options, _StorageIndex> >
31 {
32  typedef _Scalar Scalar;
33  typedef _StorageIndex StorageIndex;
35  typedef MatrixXpr XprKind;
36  enum {
37  IsColVector = (_Options & RowMajorBit) ? 0 : 1,
38 
39  RowsAtCompileTime = IsColVector ? Dynamic : 1,
40  ColsAtCompileTime = IsColVector ? 1 : Dynamic,
41  MaxRowsAtCompileTime = RowsAtCompileTime,
42  MaxColsAtCompileTime = ColsAtCompileTime,
43  Flags = _Options | NestByRefBit | LvalueBit | (IsColVector ? 0 : RowMajorBit) | CompressedAccessBit,
44  SupportedAccessPatterns = InnerRandomAccessPattern
45  };
46 };
47 
48 // Sparse-Vector-Assignment kinds:
49 enum {
53 };
54 
55 template< typename Dest, typename Src,
56  int AssignmentKind = !bool(Src::IsVectorAtCompileTime) ? SVA_RuntimeSwitch
57  : Src::InnerSizeAtCompileTime==1 ? SVA_Outer
58  : SVA_Inner>
60 
61 }
62 
63 template<typename _Scalar, int _Options, typename _StorageIndex>
64 class SparseVector
65  : public SparseCompressedBase<SparseVector<_Scalar, _Options, _StorageIndex> >
66 {
68  using Base::convert_index;
69  public:
73 
74  typedef internal::CompressedStorage<Scalar,StorageIndex> Storage;
76 
77  enum {
78  Options = _Options
79  };
80 
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; }
85 
86  EIGEN_STRONG_INLINE const Scalar* valuePtr() const { return m_data.valuePtr(); }
87  EIGEN_STRONG_INLINE Scalar* valuePtr() { return m_data.valuePtr(); }
88 
89  EIGEN_STRONG_INLINE const StorageIndex* innerIndexPtr() const { return m_data.indexPtr(); }
90  EIGEN_STRONG_INLINE StorageIndex* innerIndexPtr() { return m_data.indexPtr(); }
91 
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; }
96 
98  inline Storage& data() { return m_data; }
100  inline const Storage& data() const { return m_data; }
101 
102  inline Scalar coeff(Index row, Index col) const
103  {
104  eigen_assert(IsColVector ? (col==0 && row>=0 && row<m_size) : (row==0 && col>=0 && col<m_size));
105  return coeff(IsColVector ? row : col);
106  }
107  inline Scalar coeff(Index i) const
108  {
109  eigen_assert(i>=0 && i<m_size);
110  return m_data.at(StorageIndex(i));
111  }
112 
114  {
115  eigen_assert(IsColVector ? (col==0 && row>=0 && row<m_size) : (row==0 && col>=0 && col<m_size));
116  return coeffRef(IsColVector ? row : col);
117  }
118 
126  {
127  eigen_assert(i>=0 && i<m_size);
128 
129  return m_data.atWithInsertion(StorageIndex(i));
130  }
131 
132  public:
133 
134  typedef typename Base::InnerIterator InnerIterator;
135  typedef typename Base::ReverseInnerIterator ReverseInnerIterator;
136 
137  inline void setZero() { m_data.clear(); }
138 
140  inline Index nonZeros() const { return m_data.size(); }
141 
142  inline void startVec(Index outer)
143  {
144  EIGEN_UNUSED_VARIABLE(outer);
145  eigen_assert(outer==0);
146  }
147 
148  inline Scalar& insertBackByOuterInner(Index outer, Index inner)
149  {
150  EIGEN_UNUSED_VARIABLE(outer);
151  eigen_assert(outer==0);
152  return insertBack(inner);
153  }
155  {
156  m_data.append(0, i);
157  return m_data.value(m_data.size()-1);
158  }
159 
161  {
162  EIGEN_UNUSED_VARIABLE(outer);
163  eigen_assert(outer==0);
164  return insertBackUnordered(inner);
165  }
167  {
168  m_data.append(0, i);
169  return m_data.value(m_data.size()-1);
170  }
171 
173  {
174  eigen_assert(IsColVector ? (col==0 && row>=0 && row<m_size) : (row==0 && col>=0 && col<m_size));
175 
176  Index inner = IsColVector ? row : col;
177  Index outer = IsColVector ? col : row;
179  eigen_assert(outer==0);
180  return insert(inner);
181  }
183  {
184  eigen_assert(i>=0 && i<m_size);
185 
186  Index startId = 0;
187  Index p = Index(m_data.size()) - 1;
188  // TODO smart realloc
189  m_data.resize(p+2,1);
190 
191  while ( (p >= startId) && (m_data.index(p) > i) )
192  {
193  m_data.index(p+1) = m_data.index(p);
194  m_data.value(p+1) = m_data.value(p);
195  --p;
196  }
197  m_data.index(p+1) = convert_index(i);
198  m_data.value(p+1) = 0;
199  return m_data.value(p+1);
200  }
201 
204  inline void reserve(Index reserveSize) { m_data.reserve(reserveSize); }
205 
206 
207  inline void finalize() {}
208 
211  {
212  m_data.prune(reference,epsilon);
213  }
214 
224  {
225  eigen_assert((IsColVector ? cols : rows)==1 && "Outer dimension must equal 1");
226  resize(IsColVector ? rows : cols);
227  }
228 
233  void resize(Index newSize)
234  {
235  m_size = newSize;
236  m_data.clear();
237  }
238 
246  void conservativeResize(Index newSize)
247  {
248  if (newSize < m_size)
249  {
250  Index i = 0;
251  while (i<m_data.size() && m_data.index(i)<newSize) ++i;
252  m_data.resize(i);
253  }
254  m_size = newSize;
255  }
256 
257  void resizeNonZeros(Index size) { m_data.resize(size); }
258 
259  inline SparseVector() : m_size(0) { check_template_parameters(); resize(0); }
260 
261  explicit inline SparseVector(Index size) : m_size(0) { check_template_parameters(); resize(size); }
262 
263  inline SparseVector(Index rows, Index cols) : m_size(0) { check_template_parameters(); resize(rows,cols); }
264 
265  template<typename OtherDerived>
267  : m_size(0)
268  {
269  #ifdef EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
271  #endif
272  check_template_parameters();
273  *this = other.derived();
274  }
275 
277  : Base(other), m_size(0)
278  {
279  check_template_parameters();
280  *this = other.derived();
281  }
282 
287  inline void swap(SparseVector& other)
288  {
289  std::swap(m_size, other.m_size);
290  m_data.swap(other.m_data);
291  }
292 
293  template<int OtherOptions>
295  {
296  eigen_assert(other.outerSize()==1);
297  std::swap(m_size, other.m_innerSize);
298  m_data.swap(other.m_data);
299  }
300 
302  {
303  if (other.isRValue())
304  {
305  swap(other.const_cast_derived());
306  }
307  else
308  {
309  resize(other.size());
310  m_data = other.m_data;
311  }
312  return *this;
313  }
314 
315  template<typename OtherDerived>
317  {
318  SparseVector tmp(other.size());
320  this->swap(tmp);
321  return *this;
322  }
323 
324  #ifndef EIGEN_PARSED_BY_DOXYGEN
325  template<typename Lhs, typename Rhs>
327  {
328  return Base::operator=(product);
329  }
330  #endif
331 
332  friend std::ostream & operator << (std::ostream & s, const SparseVector& m)
333  {
334  for (Index i=0; i<m.nonZeros(); ++i)
335  s << "(" << m.m_data.value(i) << "," << m.m_data.index(i) << ") ";
336  s << std::endl;
337  return s;
338  }
339 
341  inline ~SparseVector() {}
342 
344  Scalar sum() const;
345 
346  public:
347 
350  {
351  setZero();
352  m_data.reserve(reserve);
353  }
354 
357  {
358  eigen_assert(r==0 || c==0);
359  return fill(IsColVector ? r : c);
360  }
361 
364  {
365  m_data.append(0, i);
366  return m_data.value(m_data.size()-1);
367  }
368 
371  {
372  eigen_assert(r==0 || c==0);
373  return fillrand(IsColVector ? r : c);
374  }
375 
378  {
379  return insert(i);
380  }
381 
384 
385  // These two functions were here in the 3.1 release, so let's keep them in case some code rely on them.
387  EIGEN_DEPRECATED Storage& _data() { return m_data; }
389  EIGEN_DEPRECATED const Storage& _data() const { return m_data; }
390 
391 # ifdef EIGEN_SPARSEVECTOR_PLUGIN
392 # include EIGEN_SPARSEVECTOR_PLUGIN
393 # endif
394 
395 protected:
396 
398  {
399  EIGEN_STATIC_ASSERT(NumTraits<StorageIndex>::IsSigned,THE_INDEX_TYPE_MUST_BE_A_SIGNED_TYPE);
400  EIGEN_STATIC_ASSERT((_Options&(ColMajor|RowMajor))==Options,INVALID_MATRIX_TEMPLATE_PARAMETERS);
401  }
402 
405 };
406 
407 namespace internal {
408 
409 template<typename _Scalar, int _Options, typename _Index>
410 struct evaluator<SparseVector<_Scalar,_Options,_Index> >
411  : evaluator_base<SparseVector<_Scalar,_Options,_Index> >
412 {
417 
418  enum {
420  Flags = SparseVectorType::Flags
421  };
422 
423  evaluator() : Base() {}
424 
425  explicit evaluator(const SparseVectorType &mat) : m_matrix(&mat)
426  {
427  EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
428  }
429 
430  inline Index nonZerosEstimate() const {
431  return m_matrix->nonZeros();
432  }
433 
434  operator SparseVectorType&() { return m_matrix->const_cast_derived(); }
435  operator const SparseVectorType&() const { return *m_matrix; }
436 
437  const SparseVectorType *m_matrix;
438 };
439 
440 template< typename Dest, typename Src>
442  static void run(Dest& dst, const Src& src) {
443  eigen_internal_assert(src.innerSize()==src.size());
444  typedef internal::evaluator<Src> SrcEvaluatorType;
445  SrcEvaluatorType srcEval(src);
446  for(typename SrcEvaluatorType::InnerIterator it(srcEval, 0); it; ++it)
447  dst.insert(it.index()) = it.value();
448  }
449 };
450 
451 template< typename Dest, typename Src>
453  static void run(Dest& dst, const Src& src) {
454  eigen_internal_assert(src.outerSize()==src.size());
455  typedef internal::evaluator<Src> SrcEvaluatorType;
456  SrcEvaluatorType srcEval(src);
457  for(Index i=0; i<src.size(); ++i)
458  {
459  typename SrcEvaluatorType::InnerIterator it(srcEval, i);
460  if(it)
461  dst.insert(i) = it.value();
462  }
463  }
464 };
465 
466 template< typename Dest, typename Src>
468  static void run(Dest& dst, const Src& src) {
469  if(src.outerSize()==1) sparse_vector_assign_selector<Dest,Src,SVA_Inner>::run(dst, src);
471  }
472 };
473 
474 }
475 
476 } // end namespace Eigen
477 
478 #endif // EIGEN_SPARSEVECTOR_H
Matrix3f m
Scalar & insert(Index row, Index col)
Definition: SparseVector.h:172
SparseVector & operator=(const SparseSparseProduct< Lhs, Rhs > &product)
Definition: SparseVector.h:326
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
A insert(1, 2)=0
Index nonZeros() const
Definition: SparseVector.h:140
StorageIndex * innerNonZeroPtr()
Definition: SparseVector.h:95
const unsigned int CompressedAccessBit
Definition: Constants.h:191
Scalar coeff(Index row, Index col) const
Definition: SparseVector.h:102
Scalar & insertBack(Index i)
Definition: SparseVector.h:154
A versatible sparse matrix representation.
Definition: SparseMatrix.h:96
SparseVector(const SparseMatrixBase< OtherDerived > &other)
Definition: SparseVector.h:266
EIGEN_DEPRECATED Scalar & fillrand(Index i)
Definition: SparseVector.h:377
Scalar & insert(Index i)
Definition: SparseVector.h:182
const int InnerRandomAccessPattern
Definition: SparseUtil.h:48
const unsigned int LvalueBit
Definition: Constants.h:144
const Storage & data() const
Definition: SparseVector.h:100
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
Base::ReverseInnerIterator ReverseInnerIterator
Definition: SparseVector.h:135
Scalar & coeffRef(Index i)
Definition: SparseVector.h:125
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
EIGEN_STRONG_INLINE Index cols() const
Definition: SparseVector.h:82
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:127
EIGEN_DEVICE_FUNC IndexDest convert_index(const IndexSrc &idx)
Definition: XprHelper.h:31
void conservativeResize(Index newSize)
Definition: SparseVector.h:246
EIGEN_STRONG_INLINE Index rows() const
Definition: SparseVector.h:81
const StorageIndex * outerIndexPtr() const
Definition: SparseVector.h:92
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:39
const unsigned int RowMajorBit
Definition: Constants.h:66
Scalar & insertBackByOuterInnerUnordered(Index outer, Index inner)
Definition: SparseVector.h:160
static double epsilon
Definition: testRot3.cpp:37
void swap(SparseVector &other)
Definition: SparseVector.h:287
void reserve(Index reserveSize)
Definition: SparseVector.h:204
#define EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
internal::traits< SparseVector< double, _Options, _StorageIndex > >::StorageIndex StorageIndex
#define EIGEN_INTERNAL_CHECK_COST_VALUE(C)
Definition: StaticAssert.h:218
#define EIGEN_SPARSE_PUBLIC_INTERFACE(Derived)
Definition: SparseUtil.h:43
Base class of any sparse matrices or sparse expressions.
internal::traits< SparseVector< double, _Options, _StorageIndex > >::Scalar Scalar
m row(1)
a sparse vector class
Definition: SparseUtil.h:54
void resizeNonZeros(Index size)
Definition: SparseVector.h:257
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
SparseVector(const SparseVector &other)
Definition: SparseVector.h:276
#define eigen_assert(x)
Definition: Macros.h:1037
void resize(Index newSize)
Definition: SparseVector.h:233
RealScalar s
EIGEN_STRONG_INLINE Scalar * valuePtr()
Definition: SparseVector.h:87
EIGEN_STRONG_INLINE Index outerSize() const
Definition: SparseVector.h:84
SparseVector(Index rows, Index cols)
Definition: SparseVector.h:263
void startVec(Index outer)
Definition: SparseVector.h:142
#define EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(Derived, Op)
Definition: SparseUtil.h:21
EIGEN_DEPRECATED Scalar & fill(Index i)
Definition: SparseVector.h:363
const StorageIndex * innerNonZeroPtr() const
Definition: SparseVector.h:94
EIGEN_STRONG_INLINE Index innerSize() const
Definition: SparseVector.h:83
void resize(Index rows, Index cols)
Definition: SparseVector.h:223
Scalar & insertBackByOuterInner(Index outer, Index inner)
Definition: SparseVector.h:148
void swap(GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &a, GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &b)
EIGEN_STRONG_INLINE const StorageIndex * innerIndexPtr() const
Definition: SparseVector.h:89
const unsigned int NestByRefBit
Definition: Constants.h:169
EIGEN_CONSTEXPR Index size(const T &x)
Definition: Meta.h:479
Scalar & insertBackUnordered(Index i)
Definition: SparseVector.h:166
void swap(SparseMatrix< Scalar, OtherOptions, StorageIndex > &other)
Definition: SparseVector.h:294
EIGEN_DEPRECATED void startFill(Index reserve)
Definition: SparseVector.h:349
EIGEN_DEPRECATED void endFill()
Definition: SparseVector.h:383
EIGEN_DEPRECATED Scalar & fill(Index r, Index c)
Definition: SparseVector.h:356
Scalar coeff(Index i) const
Definition: SparseVector.h:107
Derived & const_cast_derived() const
EIGEN_DEPRECATED const Storage & _data() const
Definition: SparseVector.h:389
#define EIGEN_DEPRECATED
Definition: Macros.h:1058
float * p
EIGEN_STRONG_INLINE StorageIndex * innerIndexPtr()
Definition: SparseVector.h:90
const Derived & derived() const
EIGEN_STRONG_INLINE const Scalar * valuePtr() const
Definition: SparseVector.h:86
SparseCompressedBase< SparseVector > Base
Definition: SparseVector.h:67
Index outerSize() const
Definition: SparseMatrix.h:145
m col(1)
StorageIndex * outerIndexPtr()
Definition: SparseVector.h:93
Storage & data()
Definition: SparseVector.h:98
static void check_template_parameters()
Definition: SparseVector.h:397
const int Dynamic
Definition: Constants.h:22
#define eigen_internal_assert(x)
Definition: Macros.h:1043
void prune(const Scalar &reference, const RealScalar &epsilon=NumTraits< RealScalar >::dummy_precision())
Definition: SparseVector.h:210
SparseVector(Index size)
Definition: SparseVector.h:261
Common base class for sparse [compressed]-{row|column}-storage format.
v resize(3)
EIGEN_DEPRECATED Storage & _data()
Definition: SparseVector.h:387
EIGEN_DEPRECATED Scalar & fillrand(Index r, Index c)
Definition: SparseVector.h:370
SparseVector & operator=(const SparseVector &other)
Definition: SparseVector.h:301
SparseVector & operator=(const SparseMatrixBase< OtherDerived > &other)
Definition: SparseVector.h:316
Scalar & coeffRef(Index row, Index col)
Definition: SparseVector.h:113
std::ostream & operator<<(std::ostream &s, const Packet16c &v)
#define EIGEN_UNUSED_VARIABLE(var)
Definition: Macros.h:1076
void product(const MatrixType &m)
Definition: product.h:20
void swap(scoped_array< T > &a, scoped_array< T > &b)
Definition: Memory.h:709
Base::InnerIterator InnerIterator
Definition: SparseVector.h:134
#define EIGEN_ONLY_USED_FOR_DEBUG(x)
Definition: Macros.h:1049
v setZero(3)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:36:17