10 #ifndef EIGEN_SPARSE_COMPRESSED_BASE_H 11 #define EIGEN_SPARSE_COMPRESSED_BASE_H 19 template<
typename Derived>
35 template<
typename Derived>
36 class SparseCompressedBase
37 :
public SparseMatrixBase<Derived>
40 typedef SparseMatrixBase<Derived> Base;
42 using Base::operator=;
43 using Base::IsRowMajor;
46 class ReverseInnerIterator;
49 typedef typename Base::IndexVector IndexVector;
65 return innerNonZeros().sum();
135 template<
typename Derived>
136 class SparseCompressedBase<Derived>::InnerIterator
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());
160 if(Derived::IsVectorAtCompileTime && mat.outerIndexPtr()==0)
163 m_end = mat.nonZeros();
167 m_id = mat.outerIndexPtr()[outer];
168 if(mat.isCompressed())
169 m_end = mat.outerIndexPtr()[outer+1];
171 m_end = m_id + mat.innerNonZeroPtr()[outer];
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;
210 template<
typename T> InnerIterator(
const SparseMatrixBase<T>&,
Index outer);
213 template<
typename Derived>
214 class SparseCompressedBase<Derived>::ReverseInnerIterator
220 if(Derived::IsVectorAtCompileTime && mat.outerIndexPtr()==0)
223 m_id = mat.nonZeros();
227 m_start = mat.outerIndexPtr()[outer];
228 if(mat.isCompressed())
229 m_id = mat.outerIndexPtr()[outer+1];
231 m_id = m_start + mat.innerNonZeroPtr()[outer];
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;
278 CoeffReadCost = NumTraits<Scalar>::ReadCost,
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:94
RowXpr row(Index i)
Definition: SparseMatrixBase.h:860
Namespace containing all symbols from the Eigen library.
Definition: Core:306
StorageIndex * innerIndexPtr()
Definition: SparseCompressedBase.h:84
Index outerSize() const
Definition: SparseMatrixBase.h:184
Derived & derived()
Definition: EigenBase.h:45
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
Index size() const
Definition: SparseMatrixBase.h:176
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
StorageIndex * innerNonZeroPtr()
Definition: SparseCompressedBase.h:104
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
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
ColXpr col(Index i)
Definition: SparseMatrixBase.h:839
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