10 #ifndef EIGEN_SPARSE_COMPRESSED_BASE_H 11 #define EIGEN_SPARSE_COMPRESSED_BASE_H 19 template<
typename Derived>
35 template<
typename Derived>
42 using Base::operator=;
43 using Base::IsRowMajor;
46 class ReverseInnerIterator;
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)
65 return innerNonZeros().sum();
71 inline const Scalar*
valuePtr()
const {
return derived().valuePtr(); }
75 inline Scalar*
valuePtr() {
return derived().valuePtr(); }
135 internal::LowerBoundIndex lower_bound(
Index row,
Index col)
const 137 eigen_internal_assert(row>=0 && row<this->rows() && col>=0 && col<this->cols());
139 const Index outer = Derived::IsRowMajor ? row : col;
140 const Index inner = Derived::IsRowMajor ? col : row;
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");
145 internal::LowerBoundIndex p;
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);
151 friend struct internal::evaluator<SparseCompressedBase<Derived> >;
157 template<
typename Derived>
162 : m_values(0), m_indices(0), m_outer(0), m_id(0), m_end(0)
165 InnerIterator(
const InnerIterator& other)
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)
169 InnerIterator& operator=(
const InnerIterator& other)
171 m_values = other.m_values;
172 m_indices = other.m_indices;
173 const_cast<OuterType&
>(m_outer).setValue(other.m_outer.value());
200 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
203 explicit InnerIterator(
const internal::CompressedStorage<Scalar,StorageIndex>& data)
204 : m_values(data.valuePtr()), m_indices(data.indexPtr()), m_outer(0), m_id(0), m_end(data.size())
206 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
209 inline InnerIterator& operator++() { m_id++;
return *
this; }
210 inline InnerIterator& operator+=(
Index i) { m_id += i ;
return *
this; }
212 inline InnerIterator operator+(
Index i)
214 InnerIterator result = *
this;
219 inline const Scalar& value()
const {
return m_values[m_id]; }
220 inline Scalar& valueRef() {
return const_cast<Scalar&
>(m_values[m_id]); }
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(); }
227 inline operator bool()
const {
return (m_id < m_end); }
230 const Scalar* m_values;
232 typedef internal::variable_if_dynamic<Index,Derived::IsVectorAtCompileTime?0:Dynamic> OuterType;
233 const OuterType m_outer;
243 template<
typename Derived>
268 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
271 explicit ReverseInnerIterator(
const internal::CompressedStorage<Scalar,StorageIndex>& data)
272 : m_values(data.valuePtr()), m_indices(data.indexPtr()), m_outer(0), m_start(0), m_id(data.size())
274 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
277 inline ReverseInnerIterator& operator--() { --m_id;
return *
this; }
278 inline ReverseInnerIterator& operator-=(
Index i) { m_id -= i;
return *
this; }
280 inline ReverseInnerIterator operator-(
Index i)
282 ReverseInnerIterator result = *
this;
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]); }
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(); }
295 inline operator bool()
const {
return (m_id > m_start); }
298 const Scalar* m_values;
300 typedef internal::variable_if_dynamic<Index,Derived::IsVectorAtCompileTime?0:Dynamic> OuterType;
301 const OuterType m_outer;
308 template<
typename Derived>
309 struct evaluator<SparseCompressedBase<Derived> >
310 : evaluator_base<Derived>
312 typedef typename Derived::Scalar Scalar;
313 typedef typename Derived::InnerIterator InnerIterator;
317 Flags = Derived::Flags
320 evaluator() : m_matrix(0), m_zero(0)
322 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
324 explicit evaluator(
const Derived &mat) : m_matrix(&mat), m_zero(0)
326 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
329 inline Index nonZerosEstimate()
const {
330 return m_matrix->nonZeros();
333 operator Derived&() {
return m_matrix->const_cast_derived(); }
334 operator const Derived&()
const {
return *m_matrix; }
336 typedef typename DenseCoeffsBase<Derived,ReadOnlyAccessors>::CoeffReturnType CoeffReturnType;
337 const Scalar& coeff(
Index row,
Index col)
const 339 Index p = find(row,col);
344 return m_matrix->const_cast_derived().valuePtr()[p];
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];
358 internal::LowerBoundIndex p = m_matrix->lower_bound(row,col);
359 return p.found ? p.value :
Dynamic;
362 const Derived *m_matrix;
370 #endif // EIGEN_SPARSE_COMPRESSED_BASE_H bool isCompressed() const
Definition: SparseCompressedBase.h:107
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
Namespace containing all symbols from the Eigen library.
Definition: Core:141
StorageIndex * innerIndexPtr()
Definition: SparseCompressedBase.h:84
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
Index nonZeros() const
Definition: SparseCompressedBase.h:56
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:39
const StorageIndex * innerNonZeroPtr() const
Definition: SparseCompressedBase.h:100
Map< Array< Scalar, Dynamic, 1 > > coeffs()
Definition: SparseCompressedBase.h:126
internal::traits< Block< const SparseMatrix< _Scalar, _Options, _StorageIndex >, BlockRows, BlockCols, true > >::StorageIndex StorageIndex
Definition: SparseMatrixBase.h:43
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:26
StorageIndex * innerNonZeroPtr()
Definition: SparseCompressedBase.h:104
Scalar * valuePtr()
Definition: SparseCompressedBase.h:75
const StorageIndex * innerIndexPtr() const
Definition: SparseCompressedBase.h:80
const StorageIndex * outerIndexPtr() const
Definition: SparseCompressedBase.h:90
StorageIndex * outerIndexPtr()
Definition: SparseCompressedBase.h:95
Definition: Eigen_Colamd.h:50
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:45
const int Dynamic
Definition: Constants.h:22
Common base class for sparse [compressed]-{row|column}-storage format.
Definition: SparseCompressedBase.h:15
const Scalar * valuePtr() const
Definition: SparseCompressedBase.h:71
const Map< const Array< Scalar, Dynamic, 1 > > coeffs() const
Definition: SparseCompressedBase.h:114
SparseCompressedBase()
Definition: SparseCompressedBase.h:130