Eigen  3.4.90 (git rev a4098ac676528a83cfb73d4d26ce1b42ec05f47c)
SparseCompressedBase.h
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#include "./InternalHeaderCheck.h"
14
15namespace Eigen {
16
17template<typename Derived> class SparseCompressedBase;
18
19namespace internal {
20
21template<typename Derived>
22struct traits<SparseCompressedBase<Derived> > : traits<Derived>
23{};
24
25} // end namespace internal
26
37template<typename Derived>
39 : public SparseMatrixBase<Derived>
40{
41 public:
43 EIGEN_SPARSE_PUBLIC_INTERFACE(SparseCompressedBase)
44 using Base::operator=;
45 using Base::IsRowMajor;
46
47 class InnerIterator;
48 class ReverseInnerIterator;
49
50 protected:
51 typedef typename Base::IndexVector IndexVector;
54
55 public:
56
58 inline Index nonZeros() const
59 {
60 if(Derived::IsVectorAtCompileTime && outerIndexPtr()==0)
61 return derived().nonZeros();
62 else if(isCompressed())
63 return outerIndexPtr()[derived().outerSize()]-outerIndexPtr()[0];
64 else if(derived().outerSize()==0)
65 return 0;
66 else
67 return innerNonZeros().sum();
68 }
69
73 inline const Scalar* valuePtr() const { return derived().valuePtr(); }
77 inline Scalar* valuePtr() { return derived().valuePtr(); }
78
82 inline const StorageIndex* innerIndexPtr() const { return derived().innerIndexPtr(); }
86 inline StorageIndex* innerIndexPtr() { return derived().innerIndexPtr(); }
87
92 inline const StorageIndex* outerIndexPtr() const { return derived().outerIndexPtr(); }
97 inline StorageIndex* outerIndexPtr() { return derived().outerIndexPtr(); }
98
102 inline const StorageIndex* innerNonZeroPtr() const { return derived().innerNonZeroPtr(); }
106 inline StorageIndex* innerNonZeroPtr() { return derived().innerNonZeroPtr(); }
109 inline bool isCompressed() const { return innerNonZeroPtr()==0; }
110
117
129
130 protected:
133
137 internal::LowerBoundIndex lower_bound(Index row, Index col) const
138 {
139 eigen_internal_assert(row>=0 && row<this->rows() && col>=0 && col<this->cols());
140
141 const Index outer = Derived::IsRowMajor ? row : col;
142 const Index inner = Derived::IsRowMajor ? col : row;
143
144 Index start = this->outerIndexPtr()[outer];
145 Index end = this->isCompressed() ? this->outerIndexPtr()[outer+1] : this->outerIndexPtr()[outer] + this->innerNonZeroPtr()[outer];
146 eigen_assert(end>=start && "you are using a non finalized sparse matrix or written coefficient does not exist");
147 internal::LowerBoundIndex p;
148 p.value = std::lower_bound(this->innerIndexPtr()+start, this->innerIndexPtr()+end,inner) - this->innerIndexPtr();
149 p.found = (p.value<end) && (this->innerIndexPtr()[p.value]==inner);
150 return p;
151 }
152
153 friend struct internal::evaluator<SparseCompressedBase<Derived> >;
154
155 private:
156 template<typename OtherDerived> explicit SparseCompressedBase(const SparseCompressedBase<OtherDerived>&);
157};
158
159template<typename Derived>
160class SparseCompressedBase<Derived>::InnerIterator
161{
162 public:
163 InnerIterator()
164 : m_values(0), m_indices(0), m_outer(0), m_id(0), m_end(0)
165 {}
166
167 InnerIterator(const InnerIterator& other)
168 : 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 {}
170
171 InnerIterator& operator=(const InnerIterator& other)
172 {
173 m_values = other.m_values;
174 m_indices = other.m_indices;
175 const_cast<OuterType&>(m_outer).setValue(other.m_outer.value());
176 m_id = other.m_id;
177 m_end = other.m_end;
178 return *this;
179 }
180
181 InnerIterator(const SparseCompressedBase& mat, Index outer)
182 : m_values(mat.valuePtr()), m_indices(mat.innerIndexPtr()), m_outer(outer)
183 {
184 if(Derived::IsVectorAtCompileTime && mat.outerIndexPtr()==0)
185 {
186 m_id = 0;
187 m_end = mat.nonZeros();
188 }
189 else
190 {
191 m_id = mat.outerIndexPtr()[outer];
192 if(mat.isCompressed())
193 m_end = mat.outerIndexPtr()[outer+1];
194 else
195 m_end = m_id + mat.innerNonZeroPtr()[outer];
196 }
197 }
198
199 explicit InnerIterator(const SparseCompressedBase& mat)
200 : m_values(mat.valuePtr()), m_indices(mat.innerIndexPtr()), m_outer(0), m_id(0), m_end(mat.nonZeros())
201 {
202 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
203 }
204
205 explicit InnerIterator(const internal::CompressedStorage<Scalar,StorageIndex>& data)
206 : m_values(data.valuePtr()), m_indices(data.indexPtr()), m_outer(0), m_id(0), m_end(data.size())
207 {
208 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
209 }
210
211 inline InnerIterator& operator++() { m_id++; return *this; }
212 inline InnerIterator& operator+=(Index i) { m_id += i ; return *this; }
213
214 inline InnerIterator operator+(Index i)
215 {
216 InnerIterator result = *this;
217 result += i;
218 return result;
219 }
220
221 inline const Scalar& value() const { return m_values[m_id]; }
222 inline Scalar& valueRef() { return const_cast<Scalar&>(m_values[m_id]); }
223
224 inline StorageIndex index() const { return m_indices[m_id]; }
225 inline Index outer() const { return m_outer.value(); }
226 inline Index row() const { return IsRowMajor ? m_outer.value() : index(); }
227 inline Index col() const { return IsRowMajor ? index() : m_outer.value(); }
228
229 inline operator bool() const { return (m_id < m_end); }
230
231 protected:
232 const Scalar* m_values;
233 const StorageIndex* m_indices;
234 typedef internal::variable_if_dynamic<Index,Derived::IsVectorAtCompileTime?0:Dynamic> OuterType;
235 const OuterType m_outer;
236 Index m_id;
237 Index m_end;
238 private:
239 // If you get here, then you're not using the right InnerIterator type, e.g.:
240 // SparseMatrix<double,RowMajor> A;
241 // SparseMatrix<double>::InnerIterator it(A,0);
242 template<typename T> InnerIterator(const SparseMatrixBase<T>&, Index outer);
243};
244
245template<typename Derived>
246class SparseCompressedBase<Derived>::ReverseInnerIterator
247{
248 public:
249 ReverseInnerIterator(const SparseCompressedBase& mat, Index outer)
250 : m_values(mat.valuePtr()), m_indices(mat.innerIndexPtr()), m_outer(outer)
251 {
252 if(Derived::IsVectorAtCompileTime && mat.outerIndexPtr()==0)
253 {
254 m_start = 0;
255 m_id = mat.nonZeros();
256 }
257 else
258 {
259 m_start = mat.outerIndexPtr()[outer];
260 if(mat.isCompressed())
261 m_id = mat.outerIndexPtr()[outer+1];
262 else
263 m_id = m_start + mat.innerNonZeroPtr()[outer];
264 }
265 }
266
267 explicit ReverseInnerIterator(const SparseCompressedBase& mat)
268 : m_values(mat.valuePtr()), m_indices(mat.innerIndexPtr()), m_outer(0), m_start(0), m_id(mat.nonZeros())
269 {
270 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
271 }
272
273 explicit ReverseInnerIterator(const internal::CompressedStorage<Scalar,StorageIndex>& data)
274 : m_values(data.valuePtr()), m_indices(data.indexPtr()), m_outer(0), m_start(0), m_id(data.size())
275 {
276 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
277 }
278
279 inline ReverseInnerIterator& operator--() { --m_id; return *this; }
280 inline ReverseInnerIterator& operator-=(Index i) { m_id -= i; return *this; }
281
282 inline ReverseInnerIterator operator-(Index i)
283 {
284 ReverseInnerIterator result = *this;
285 result -= i;
286 return result;
287 }
288
289 inline const Scalar& value() const { return m_values[m_id-1]; }
290 inline Scalar& valueRef() { return const_cast<Scalar&>(m_values[m_id-1]); }
291
292 inline StorageIndex index() const { return m_indices[m_id-1]; }
293 inline Index outer() const { return m_outer.value(); }
294 inline Index row() const { return IsRowMajor ? m_outer.value() : index(); }
295 inline Index col() const { return IsRowMajor ? index() : m_outer.value(); }
296
297 inline operator bool() const { return (m_id > m_start); }
298
299 protected:
300 const Scalar* m_values;
301 const StorageIndex* m_indices;
302 typedef internal::variable_if_dynamic<Index,Derived::IsVectorAtCompileTime?0:Dynamic> OuterType;
303 const OuterType m_outer;
304 Index m_start;
305 Index m_id;
306};
307
308namespace internal {
309
310template<typename Derived>
311struct evaluator<SparseCompressedBase<Derived> >
312 : evaluator_base<Derived>
313{
314 typedef typename Derived::Scalar Scalar;
315 typedef typename Derived::InnerIterator InnerIterator;
316
317 enum {
318 CoeffReadCost = NumTraits<Scalar>::ReadCost,
319 Flags = Derived::Flags
320 };
321
322 evaluator() : m_matrix(0), m_zero(0)
323 {
324 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
325 }
326 explicit evaluator(const Derived &mat) : m_matrix(&mat), m_zero(0)
327 {
328 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
329 }
330
331 inline Index nonZerosEstimate() const {
332 return m_matrix->nonZeros();
333 }
334
335 operator Derived&() { return m_matrix->const_cast_derived(); }
336 operator const Derived&() const { return *m_matrix; }
337
338 typedef typename DenseCoeffsBase<Derived,ReadOnlyAccessors>::CoeffReturnType CoeffReturnType;
339 const Scalar& coeff(Index row, Index col) const
340 {
341 Index p = find(row,col);
342
343 if(p==Dynamic)
344 return m_zero;
345 else
346 return m_matrix->const_cast_derived().valuePtr()[p];
347 }
348
349 Scalar& coeffRef(Index row, Index col)
350 {
351 Index p = find(row,col);
352 eigen_assert(p!=Dynamic && "written coefficient does not exist");
353 return m_matrix->const_cast_derived().valuePtr()[p];
354 }
355
356protected:
357
358 Index find(Index row, Index col) const
359 {
360 internal::LowerBoundIndex p = m_matrix->lower_bound(row,col);
361 return p.found ? p.value : Dynamic;
362 }
363
364 const Derived *m_matrix;
365 const Scalar m_zero;
366};
367
368}
369
370} // end namespace Eigen
371
372#endif // EIGEN_SPARSE_COMPRESSED_BASE_H
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:98
static ConstMapType Map(const Scalar *data)
Definition: PlainObjectBase.h:643
Common base class for sparse [compressed]-{row|column}-storage format.
Definition: SparseCompressedBase.h:40
StorageIndex * innerNonZeroPtr()
Definition: SparseCompressedBase.h:106
Index nonZeros() const
Definition: SparseCompressedBase.h:58
const StorageIndex * innerIndexPtr() const
Definition: SparseCompressedBase.h:82
StorageIndex * innerIndexPtr()
Definition: SparseCompressedBase.h:86
const Scalar * valuePtr() const
Definition: SparseCompressedBase.h:73
bool isCompressed() const
Definition: SparseCompressedBase.h:109
const StorageIndex * outerIndexPtr() const
Definition: SparseCompressedBase.h:92
Scalar * valuePtr()
Definition: SparseCompressedBase.h:77
Map< Array< Scalar, Dynamic, 1 > > coeffs()
Definition: SparseCompressedBase.h:128
const StorageIndex * innerNonZeroPtr() const
Definition: SparseCompressedBase.h:102
StorageIndex * outerIndexPtr()
Definition: SparseCompressedBase.h:97
const Map< const Array< Scalar, Dynamic, 1 > > coeffs() const
Definition: SparseCompressedBase.h:116
SparseCompressedBase()
Definition: SparseCompressedBase.h:132
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:30
internal::traits< Derived >::StorageIndex StorageIndex
Definition: SparseMatrixBase.h:45
Index size() const
Definition: SparseMatrixBase.h:183
Index rows() const
Definition: SparseMatrixBase.h:178
Index outerSize() const
Definition: SparseMatrixBase.h:191
Index cols() const
Definition: SparseMatrixBase.h:180
static const lastp1_t end
Definition: IndexedViewHelper.h:183
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const int Dynamic
Definition: Constants.h:24
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41
Derived & derived()
Definition: EigenBase.h:48