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 template<
typename Derived>
140 : m_values(0), m_indices(0), m_outer(0), m_id(0), m_end(0)
143 InnerIterator(
const InnerIterator& other)
144 : 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)
147 InnerIterator& operator=(
const InnerIterator& other)
149 m_values = other.m_values;
150 m_indices = other.m_indices;
151 const_cast<OuterType&
>(m_outer).setValue(other.m_outer.value());
178 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
181 explicit InnerIterator(
const internal::CompressedStorage<Scalar,StorageIndex>& data)
182 : m_values(data.valuePtr()), m_indices(data.indexPtr()), m_outer(0), m_id(0), m_end(data.size())
184 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
187 inline InnerIterator& operator++() { m_id++;
return *
this; }
189 inline const Scalar& value()
const {
return m_values[m_id]; }
190 inline Scalar& valueRef() {
return const_cast<Scalar&
>(m_values[m_id]); }
192 inline StorageIndex index()
const {
return m_indices[m_id]; }
193 inline Index outer()
const {
return m_outer.value(); }
194 inline Index row()
const {
return IsRowMajor ? m_outer.value() : index(); }
195 inline Index col()
const {
return IsRowMajor ? index() : m_outer.value(); }
197 inline operator bool()
const {
return (m_id < m_end); }
200 const Scalar* m_values;
202 typedef internal::variable_if_dynamic<Index,Derived::IsVectorAtCompileTime?0:Dynamic> OuterType;
203 const OuterType m_outer;
213 template<
typename Derived>
238 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
241 explicit ReverseInnerIterator(
const internal::CompressedStorage<Scalar,StorageIndex>& data)
242 : m_values(data.valuePtr()), m_indices(data.indexPtr()), m_outer(0), m_start(0), m_id(data.size())
244 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
247 inline ReverseInnerIterator& operator--() { --m_id;
return *
this; }
249 inline const Scalar& value()
const {
return m_values[m_id-1]; }
250 inline Scalar& valueRef() {
return const_cast<Scalar&
>(m_values[m_id-1]); }
252 inline StorageIndex index()
const {
return m_indices[m_id-1]; }
253 inline Index outer()
const {
return m_outer.value(); }
254 inline Index row()
const {
return IsRowMajor ? m_outer.value() : index(); }
255 inline Index col()
const {
return IsRowMajor ? index() : m_outer.value(); }
257 inline operator bool()
const {
return (m_id > m_start); }
260 const Scalar* m_values;
262 typedef internal::variable_if_dynamic<Index,Derived::IsVectorAtCompileTime?0:Dynamic> OuterType;
263 const OuterType m_outer;
270 template<
typename Derived>
271 struct evaluator<SparseCompressedBase<Derived> >
272 : evaluator_base<Derived>
274 typedef typename Derived::Scalar Scalar;
275 typedef typename Derived::InnerIterator InnerIterator;
279 Flags = Derived::Flags
282 evaluator() : m_matrix(0), m_zero(0)
284 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
286 explicit evaluator(
const Derived &mat) : m_matrix(&mat), m_zero(0)
288 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
291 inline Index nonZerosEstimate()
const {
292 return m_matrix->nonZeros();
295 operator Derived&() {
return m_matrix->const_cast_derived(); }
296 operator const Derived&()
const {
return *m_matrix; }
298 typedef typename DenseCoeffsBase<Derived,ReadOnlyAccessors>::CoeffReturnType CoeffReturnType;
299 const Scalar& coeff(
Index row,
Index col)
const 301 Index p = find(row,col);
306 return m_matrix->const_cast_derived().valuePtr()[p];
311 Index p = find(row,col);
312 eigen_assert(p!=
Dynamic &&
"written coefficient does not exist");
313 return m_matrix->const_cast_derived().valuePtr()[p];
320 eigen_internal_assert(row>=0 && row<m_matrix->rows() && col>=0 && col<m_matrix->cols());
322 const Index outer = Derived::IsRowMajor ? row : col;
323 const Index inner = Derived::IsRowMajor ? col : row;
325 Index start = m_matrix->outerIndexPtr()[outer];
326 Index end = m_matrix->isCompressed() ? m_matrix->outerIndexPtr()[outer+1] : m_matrix->outerIndexPtr()[outer] + m_matrix->innerNonZeroPtr()[outer];
327 eigen_assert(end>=start &&
"you are using a non finalized sparse matrix or written coefficient does not exist");
328 const Index p = std::lower_bound(m_matrix->innerIndexPtr()+start, m_matrix->innerIndexPtr()+end,inner) - m_matrix->innerIndexPtr();
330 return ((p<end) && (m_matrix->innerIndexPtr()[p]==inner)) ? p :
Dynamic;
333 const Derived *m_matrix;
341 #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:88
Namespace containing all symbols from the Eigen library.
Definition: Core:287
StorageIndex * innerIndexPtr()
Definition: SparseCompressedBase.h:84
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:150
Index nonZeros() const
Definition: SparseCompressedBase.h:56
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:38
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: ForwardDeclarations.h:281
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:21
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