SparseCompressedBase.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) 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_SPARSE_COMPRESSED_BASE_H
11 #define EIGEN_SPARSE_COMPRESSED_BASE_H
12 
13 namespace Eigen {
14 
15 template<typename Derived> class SparseCompressedBase;
16 
17 namespace internal {
18 
19 template<typename Derived>
20 struct traits<SparseCompressedBase<Derived> > : traits<Derived>
21 {};
22 
23 } // end namespace internal
24 
35 template<typename Derived>
37  : public SparseMatrixBase<Derived>
38 {
39  public:
42  using Base::operator=;
43  using Base::IsRowMajor;
44 
45  class InnerIterator;
46  class ReverseInnerIterator;
47 
48  protected:
49  typedef typename Base::IndexVector IndexVector;
52 
53  public:
54 
56  inline Index nonZeros() const
57  {
58  if(Derived::IsVectorAtCompileTime && outerIndexPtr()==0)
59  return derived().nonZeros();
60  else if(isCompressed())
61  return outerIndexPtr()[derived().outerSize()]-outerIndexPtr()[0];
62  else if(derived().outerSize()==0)
63  return 0;
64  else
65  return innerNonZeros().sum();
66  }
67 
71  inline const Scalar* valuePtr() const { return derived().valuePtr(); }
75  inline Scalar* valuePtr() { return derived().valuePtr(); }
76 
80  inline const StorageIndex* innerIndexPtr() const { return derived().innerIndexPtr(); }
84  inline StorageIndex* innerIndexPtr() { return derived().innerIndexPtr(); }
85 
90  inline const StorageIndex* outerIndexPtr() const { return derived().outerIndexPtr(); }
95  inline StorageIndex* outerIndexPtr() { return derived().outerIndexPtr(); }
96 
100  inline const StorageIndex* innerNonZeroPtr() const { return derived().innerNonZeroPtr(); }
104  inline StorageIndex* innerNonZeroPtr() { return derived().innerNonZeroPtr(); }
105 
107  inline bool isCompressed() const { return innerNonZeroPtr()==0; }
108 
115 
127 
128  protected:
131 
136  {
137  eigen_internal_assert(row>=0 && row<this->rows() && col>=0 && col<this->cols());
138 
139  const Index outer = Derived::IsRowMajor ? row : col;
140  const Index inner = Derived::IsRowMajor ? col : row;
141 
142  Index start = this->outerIndexPtr()[outer];
143  Index end = this->isCompressed() ? this->outerIndexPtr()[outer+1] : this->outerIndexPtr()[outer] + this->innerNonZeroPtr()[outer];
144  eigen_assert(end>=start && "you are using a non finalized sparse matrix or written coefficient does not exist");
146  p.value = std::lower_bound(this->innerIndexPtr()+start, this->innerIndexPtr()+end,inner) - this->innerIndexPtr();
147  p.found = (p.value<end) && (this->innerIndexPtr()[p.value]==inner);
148  return p;
149  }
150 
151  friend struct internal::evaluator<SparseCompressedBase<Derived> >;
152 
153  private:
154  template<typename OtherDerived> explicit SparseCompressedBase(const SparseCompressedBase<OtherDerived>&);
155 };
156 
157 template<typename Derived>
159 {
160  public:
162  : m_values(0), m_indices(0), m_outer(0), m_id(0), m_end(0)
163  {}
164 
166  : m_values(other.m_values), m_indices(other.m_indices), m_outer(other.m_outer), m_id(other.m_id), m_end(other.m_end)
167  {}
168 
170  {
171  m_values = other.m_values;
172  m_indices = other.m_indices;
173  const_cast<OuterType&>(m_outer).setValue(other.m_outer.value());
174  m_id = other.m_id;
175  m_end = other.m_end;
176  return *this;
177  }
178 
180  : m_values(mat.valuePtr()), m_indices(mat.innerIndexPtr()), m_outer(outer)
181  {
182  if(Derived::IsVectorAtCompileTime && mat.outerIndexPtr()==0)
183  {
184  m_id = 0;
185  m_end = mat.nonZeros();
186  }
187  else
188  {
189  m_id = mat.outerIndexPtr()[outer];
190  if(mat.isCompressed())
191  m_end = mat.outerIndexPtr()[outer+1];
192  else
193  m_end = m_id + mat.innerNonZeroPtr()[outer];
194  }
195  }
196 
198  : m_values(mat.valuePtr()), m_indices(mat.innerIndexPtr()), m_outer(0), m_id(0), m_end(mat.nonZeros())
199  {
201  }
202 
204  : m_values(data.valuePtr()), m_indices(data.indexPtr()), m_outer(0), m_id(0), m_end(data.size())
205  {
207  }
208 
209  inline InnerIterator& operator++() { m_id++; return *this; }
210  inline InnerIterator& operator+=(Index i) { m_id += i ; return *this; }
211 
213  {
214  InnerIterator result = *this;
215  result += i;
216  return result;
217  }
218 
219  inline const Scalar& value() const { return m_values[m_id]; }
220  inline Scalar& valueRef() { return const_cast<Scalar&>(m_values[m_id]); }
221 
222  inline StorageIndex index() const { return m_indices[m_id]; }
223  inline Index outer() const { return m_outer.value(); }
224  inline Index row() const { return IsRowMajor ? m_outer.value() : index(); }
225  inline Index col() const { return IsRowMajor ? index() : m_outer.value(); }
226 
227  inline operator bool() const { return (m_id < m_end); }
228 
229  protected:
230  const Scalar* m_values;
231  const StorageIndex* m_indices;
236  private:
237  // If you get here, then you're not using the right InnerIterator type, e.g.:
238  // SparseMatrix<double,RowMajor> A;
239  // SparseMatrix<double>::InnerIterator it(A,0);
240  template<typename T> InnerIterator(const SparseMatrixBase<T>&, Index outer);
241 };
242 
243 template<typename Derived>
245 {
246  public:
248  : m_values(mat.valuePtr()), m_indices(mat.innerIndexPtr()), m_outer(outer)
249  {
250  if(Derived::IsVectorAtCompileTime && mat.outerIndexPtr()==0)
251  {
252  m_start = 0;
253  m_id = mat.nonZeros();
254  }
255  else
256  {
257  m_start = mat.outerIndexPtr()[outer];
258  if(mat.isCompressed())
259  m_id = mat.outerIndexPtr()[outer+1];
260  else
261  m_id = m_start + mat.innerNonZeroPtr()[outer];
262  }
263  }
264 
266  : m_values(mat.valuePtr()), m_indices(mat.innerIndexPtr()), m_outer(0), m_start(0), m_id(mat.nonZeros())
267  {
269  }
270 
272  : m_values(data.valuePtr()), m_indices(data.indexPtr()), m_outer(0), m_start(0), m_id(data.size())
273  {
275  }
276 
277  inline ReverseInnerIterator& operator--() { --m_id; return *this; }
278  inline ReverseInnerIterator& operator-=(Index i) { m_id -= i; return *this; }
279 
281  {
283  result -= i;
284  return result;
285  }
286 
287  inline const Scalar& value() const { return m_values[m_id-1]; }
288  inline Scalar& valueRef() { return const_cast<Scalar&>(m_values[m_id-1]); }
289 
290  inline StorageIndex index() const { return m_indices[m_id-1]; }
291  inline Index outer() const { return m_outer.value(); }
292  inline Index row() const { return IsRowMajor ? m_outer.value() : index(); }
293  inline Index col() const { return IsRowMajor ? index() : m_outer.value(); }
294 
295  inline operator bool() const { return (m_id > m_start); }
296 
297  protected:
298  const Scalar* m_values;
299  const StorageIndex* m_indices;
304 };
305 
306 namespace internal {
307 
308 template<typename Derived>
310  : evaluator_base<Derived>
311 {
312  typedef typename Derived::Scalar Scalar;
313  typedef typename Derived::InnerIterator InnerIterator;
314 
315  enum {
317  Flags = Derived::Flags
318  };
319 
320  evaluator() : m_matrix(0), m_zero(0)
321  {
322  EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
323  }
324  explicit evaluator(const Derived &mat) : m_matrix(&mat), m_zero(0)
325  {
326  EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
327  }
328 
329  inline Index nonZerosEstimate() const {
330  return m_matrix->nonZeros();
331  }
332 
333  operator Derived&() { return m_matrix->const_cast_derived(); }
334  operator const Derived&() const { return *m_matrix; }
335 
337  const Scalar& coeff(Index row, Index col) const
338  {
339  Index p = find(row,col);
340 
341  if(p==Dynamic)
342  return m_zero;
343  else
344  return m_matrix->const_cast_derived().valuePtr()[p];
345  }
346 
348  {
349  Index p = find(row,col);
350  eigen_assert(p!=Dynamic && "written coefficient does not exist");
351  return m_matrix->const_cast_derived().valuePtr()[p];
352  }
353 
354 protected:
355 
357  {
358  internal::LowerBoundIndex p = m_matrix->lower_bound(row,col);
359  return p.found ? p.value : Dynamic;
360  }
361 
362  const Derived *m_matrix;
363  const Scalar m_zero;
364 };
365 
366 }
367 
368 } // end namespace Eigen
369 
370 #endif // EIGEN_SPARSE_COMPRESSED_BASE_H
Eigen::internal::variable_if_dynamic
Definition: XprHelper.h:129
Eigen::SparseCompressedBase::innerIndexPtr
StorageIndex * innerIndexPtr()
Definition: SparseCompressedBase.h:84
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::SparseCompressedBase::InnerIterator::m_end
Index m_end
Definition: SparseCompressedBase.h:235
Eigen::internal::evaluator< SparseCompressedBase< Derived > >::evaluator
evaluator()
Definition: SparseCompressedBase.h:320
Eigen::internal::evaluator< SparseCompressedBase< Derived > >::coeffRef
Scalar & coeffRef(Index row, Index col)
Definition: SparseCompressedBase.h:347
col
m col(1)
Eigen::SparseCompressedBase::InnerIterator::InnerIterator
InnerIterator(const SparseCompressedBase &mat)
Definition: SparseCompressedBase.h:197
Eigen::SparseCompressedBase::ReverseInnerIterator::operator--
ReverseInnerIterator & operator--()
Definition: SparseCompressedBase.h:277
Eigen::SparseCompressedBase::InnerIterator::InnerIterator
InnerIterator(const SparseCompressedBase &mat, Index outer)
Definition: SparseCompressedBase.h:179
Eigen::SparseCompressedBase::valuePtr
const Scalar * valuePtr() const
Definition: SparseCompressedBase.h:71
Eigen::SparseCompressedBase::InnerIterator::InnerIterator
InnerIterator(const InnerIterator &other)
Definition: SparseCompressedBase.h:165
Eigen::SparseCompressedBase::InnerIterator::InnerIterator
InnerIterator(const internal::CompressedStorage< Scalar, StorageIndex > &data)
Definition: SparseCompressedBase.h:203
Eigen::SparseCompressedBase::InnerIterator::index
StorageIndex index() const
Definition: SparseCompressedBase.h:222
Eigen::SparseCompressedBase::ReverseInnerIterator::value
const Scalar & value() const
Definition: SparseCompressedBase.h:287
Eigen::SparseCompressedBase::lower_bound
internal::LowerBoundIndex lower_bound(Index row, Index col) const
Definition: SparseCompressedBase.h:135
Eigen::SparseCompressedBase::valuePtr
Scalar * valuePtr()
Definition: SparseCompressedBase.h:75
Eigen::SparseCompressedBase::InnerIterator::OuterType
internal::variable_if_dynamic< Index, Derived::IsVectorAtCompileTime?0:Dynamic > OuterType
Definition: SparseCompressedBase.h:232
Eigen::SparseCompressedBase::innerNonZeros
Eigen::Map< IndexVector > innerNonZeros()
Definition: SparseCompressedBase.h:50
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:1037
Eigen::SparseCompressedBase::outerIndexPtr
StorageIndex * outerIndexPtr()
Definition: SparseCompressedBase.h:95
Eigen::SparseCompressedBase::outerIndexPtr
const StorageIndex * outerIndexPtr() const
Definition: SparseCompressedBase.h:90
Eigen::SparseCompressedBase::isCompressed
bool isCompressed() const
Definition: SparseCompressedBase.h:107
Eigen::SparseCompressedBase::InnerIterator::outer
Index outer() const
Definition: SparseCompressedBase.h:223
Eigen::internal::evaluator< SparseCompressedBase< Derived > >::InnerIterator
Derived::InnerIterator InnerIterator
Definition: SparseCompressedBase.h:313
Eigen::SparseCompressedBase::ReverseInnerIterator::row
Index row() const
Definition: SparseCompressedBase.h:292
EIGEN_STATIC_ASSERT_VECTOR_ONLY
#define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE)
Definition: StaticAssert.h:142
Eigen::internal::LowerBoundIndex::found
bool found
Definition: SparseUtil.h:148
mat
MatrixXf mat
Definition: Tutorial_AdvancedInitialization_CommaTemporary.cpp:1
Eigen::internal::evaluator_base
Definition: CoreEvaluators.h:110
Eigen::SparseCompressedBase::innerNonZeroPtr
const StorageIndex * innerNonZeroPtr() const
Definition: SparseCompressedBase.h:100
Eigen::internal::evaluator< SparseCompressedBase< Derived > >::coeff
const Scalar & coeff(Index row, Index col) const
Definition: SparseCompressedBase.h:337
Eigen::SparseCompressedBase::ReverseInnerIterator::valueRef
Scalar & valueRef()
Definition: SparseCompressedBase.h:288
result
Values result
Definition: OdometryOptimize.cpp:8
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
Eigen::SparseCompressedBase::ReverseInnerIterator::ReverseInnerIterator
ReverseInnerIterator(const SparseCompressedBase &mat, Index outer)
Definition: SparseCompressedBase.h:247
Eigen::SparseCompressedBase::InnerIterator
Definition: SparseCompressedBase.h:158
eigen_internal_assert
#define eigen_internal_assert(x)
Definition: Macros.h:1043
Eigen::internal::evaluator< SparseCompressedBase< Derived > >::Scalar
Derived::Scalar Scalar
Definition: SparseCompressedBase.h:312
Eigen::SparseCompressedBase::SparseCompressedBase
SparseCompressedBase()
Definition: SparseCompressedBase.h:130
Eigen::SparseCompressedBase::InnerIterator::operator+
InnerIterator operator+(Index i)
Definition: SparseCompressedBase.h:212
Eigen::SparseCompressedBase::InnerIterator::row
Index row() const
Definition: SparseCompressedBase.h:224
Eigen::SparseCompressedBase::Base
SparseMatrixBase< Derived > Base
Definition: SparseCompressedBase.h:40
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
Eigen::SparseCompressedBase::innerIndexPtr
const StorageIndex * innerIndexPtr() const
Definition: SparseCompressedBase.h:80
Eigen::SparseMatrixBase::IsRowMajor
@ IsRowMajor
Definition: SparseMatrixBase.h:100
Eigen::SparseCompressedBase::ReverseInnerIterator::ReverseInnerIterator
ReverseInnerIterator(const SparseCompressedBase &mat)
Definition: SparseCompressedBase.h:265
Eigen::SparseCompressedBase
Common base class for sparse [compressed]-{row|column}-storage format.
Definition: SparseCompressedBase.h:15
EIGEN_SPARSE_PUBLIC_INTERFACE
#define EIGEN_SPARSE_PUBLIC_INTERFACE(Derived)
Definition: SparseUtil.h:43
Eigen::SparseCompressedBase::InnerIterator::value
const Scalar & value() const
Definition: SparseCompressedBase.h:219
data
int data[]
Definition: Map_placement_new.cpp:1
Eigen::SparseCompressedBase::InnerIterator::operator++
InnerIterator & operator++()
Definition: SparseCompressedBase.h:209
Eigen::SparseCompressedBase::ReverseInnerIterator::outer
Index outer() const
Definition: SparseCompressedBase.h:291
Eigen::internal::evaluator< SparseCompressedBase< Derived > >::find
Index find(Index row, Index col) const
Definition: SparseCompressedBase.h:356
Eigen::internal::LowerBoundIndex::value
Index value
Definition: SparseUtil.h:147
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:22
Eigen::internal::evaluator< SparseCompressedBase< Derived > >::CoeffReturnType
DenseCoeffsBase< Derived, ReadOnlyAccessors >::CoeffReturnType CoeffReturnType
Definition: SparseCompressedBase.h:336
Eigen::SparseCompressedBase::ReverseInnerIterator::index
StorageIndex index() const
Definition: SparseCompressedBase.h:290
Eigen::SparseCompressedBase::InnerIterator::InnerIterator
InnerIterator()
Definition: SparseCompressedBase.h:161
Eigen::SparseCompressedBase::ReverseInnerIterator::operator-
ReverseInnerIterator operator-(Index i)
Definition: SparseCompressedBase.h:280
Eigen::SparseCompressedBase::ReverseInnerIterator::m_start
Index m_start
Definition: SparseCompressedBase.h:302
Eigen::internal::evaluator< SparseCompressedBase< Derived > >::evaluator
evaluator(const Derived &mat)
Definition: SparseCompressedBase.h:324
Eigen::SparseCompressedBase::ReverseInnerIterator::m_values
const Scalar * m_values
Definition: SparseCompressedBase.h:298
Eigen::internal::evaluator
Definition: CoreEvaluators.h:90
Eigen::Map
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
Eigen::InnerIterator
An InnerIterator allows to loop over the element of any matrix expression.
Definition: CoreIterators.h:33
Eigen::PlainObjectBase< Array< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > >::Map
static ConstMapType Map(const Scalar *data)
Definition: PlainObjectBase.h:644
Eigen::SparseCompressedBase::ReverseInnerIterator::ReverseInnerIterator
ReverseInnerIterator(const internal::CompressedStorage< Scalar, StorageIndex > &data)
Definition: SparseCompressedBase.h:271
Eigen::SparseCompressedBase::ReverseInnerIterator::m_indices
const StorageIndex * m_indices
Definition: SparseCompressedBase.h:299
Eigen::SparseCompressedBase::coeffs
const Map< const Array< Scalar, Dynamic, 1 > > coeffs() const
Definition: SparseCompressedBase.h:114
Eigen::SparseCompressedBase::ReverseInnerIterator::m_outer
const OuterType m_outer
Definition: SparseCompressedBase.h:301
Eigen::internal::evaluator< SparseCompressedBase< Derived > >::m_zero
const Scalar m_zero
Definition: SparseCompressedBase.h:363
Eigen::SparseCompressedBase::ReverseInnerIterator
Definition: SparseCompressedBase.h:244
Eigen::SparseCompressedBase::InnerIterator::m_values
const Scalar * m_values
Definition: SparseCompressedBase.h:230
Eigen::internal::traits
Definition: ForwardDeclarations.h:17
Eigen::SparseCompressedBase::IndexVector
Base::IndexVector IndexVector
Definition: SparseCompressedBase.h:46
Eigen::internal::evaluator< SparseCompressedBase< Derived > >::nonZerosEstimate
Index nonZerosEstimate() const
Definition: SparseCompressedBase.h:329
Eigen::SparseCompressedBase::innerNonZeros
const Eigen::Map< const IndexVector > innerNonZeros() const
Definition: SparseCompressedBase.h:51
Eigen::SparseCompressedBase::ReverseInnerIterator::m_id
Index m_id
Definition: SparseCompressedBase.h:303
row
m row(1)
Eigen::SparseCompressedBase::InnerIterator::valueRef
Scalar & valueRef()
Definition: SparseCompressedBase.h:220
p
float * p
Definition: Tutorial_Map_using.cpp:9
Eigen::internal::LowerBoundIndex
Definition: SparseUtil.h:144
Eigen::SparseMatrixBase
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:301
Eigen::SparseCompressedBase::ReverseInnerIterator::operator-=
ReverseInnerIterator & operator-=(Index i)
Definition: SparseCompressedBase.h:278
Eigen::SparseCompressedBase::InnerIterator::operator=
InnerIterator & operator=(const InnerIterator &other)
Definition: SparseCompressedBase.h:169
Eigen::internal::CompressedStorage< Scalar, StorageIndex >
Eigen::SparseCompressedBase::ReverseInnerIterator::OuterType
internal::variable_if_dynamic< Index, Derived::IsVectorAtCompileTime?0:Dynamic > OuterType
Definition: SparseCompressedBase.h:300
Eigen::Matrix< StorageIndex, Dynamic, 1 >
Eigen::SparseCompressedBase::InnerIterator::m_indices
const StorageIndex * m_indices
Definition: SparseCompressedBase.h:231
Eigen::internal::evaluator< SparseCompressedBase< Derived > >::m_matrix
const Derived * m_matrix
Definition: SparseCompressedBase.h:362
internal
Definition: BandTriangularSolver.h:13
Eigen::SparseCompressedBase::InnerIterator::operator+=
InnerIterator & operator+=(Index i)
Definition: SparseCompressedBase.h:210
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
Eigen::placeholders::end
static const EIGEN_DEPRECATED end_t end
Definition: IndexedViewHelper.h:181
EIGEN_INTERNAL_CHECK_COST_VALUE
#define EIGEN_INTERNAL_CHECK_COST_VALUE(C)
Definition: StaticAssert.h:218
Eigen::SparseCompressedBase::InnerIterator::m_id
Index m_id
Definition: SparseCompressedBase.h:234
Eigen::SparseCompressedBase::InnerIterator::col
Index col() const
Definition: SparseCompressedBase.h:225
Eigen::SparseCompressedBase::ReverseInnerIterator::col
Index col() const
Definition: SparseCompressedBase.h:293
Eigen::DenseCoeffsBase
Definition: ForwardDeclarations.h:50
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
Eigen::SparseCompressedBase::IsRowMajor
@ IsRowMajor
Definition: SparseMatrixBase.h:100
Eigen::SparseCompressedBase::InnerIterator::m_outer
const OuterType m_outer
Definition: SparseCompressedBase.h:233
Eigen::SparseCompressedBase::nonZeros
Index nonZeros() const
Definition: SparseCompressedBase.h:56
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
Eigen::SparseCompressedBase::innerNonZeroPtr
StorageIndex * innerNonZeroPtr()
Definition: SparseCompressedBase.h:104
Eigen::SparseCompressedBase::coeffs
Map< Array< Scalar, Dynamic, 1 > > coeffs()
Definition: SparseCompressedBase.h:126
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Thu Dec 19 2024 04:03:16