Medial Code Documentation
Loading...
Searching...
No Matches
SparseVector.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-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_SPARSEVECTOR_H
11#define EIGEN_SPARSEVECTOR_H
12
13namespace Eigen {
14
28namespace internal {
29template<typename _Scalar, int _Options, typename _StorageIndex>
30struct traits<SparseVector<_Scalar, _Options, _StorageIndex> >
31{
32 typedef _Scalar Scalar;
33 typedef _StorageIndex StorageIndex;
34 typedef Sparse StorageKind;
35 typedef MatrixXpr XprKind;
36 enum {
37 IsColVector = (_Options & RowMajorBit) ? 0 : 1,
38
39 RowsAtCompileTime = IsColVector ? Dynamic : 1,
40 ColsAtCompileTime = IsColVector ? 1 : Dynamic,
41 MaxRowsAtCompileTime = RowsAtCompileTime,
42 MaxColsAtCompileTime = ColsAtCompileTime,
43 Flags = _Options | NestByRefBit | LvalueBit | (IsColVector ? 0 : RowMajorBit) | CompressedAccessBit,
44 SupportedAccessPatterns = InnerRandomAccessPattern
45 };
46};
47
48// Sparse-Vector-Assignment kinds:
49enum {
50 SVA_RuntimeSwitch,
51 SVA_Inner,
52 SVA_Outer
53};
54
55template< typename Dest, typename Src,
56 int AssignmentKind = !bool(Src::IsVectorAtCompileTime) ? SVA_RuntimeSwitch
57 : Src::InnerSizeAtCompileTime==1 ? SVA_Outer
58 : SVA_Inner>
60
61}
62
63template<typename _Scalar, int _Options, typename _StorageIndex>
65 : public SparseCompressedBase<SparseVector<_Scalar, _Options, _StorageIndex> >
66{
68 using Base::convert_index;
69 public:
70 EIGEN_SPARSE_PUBLIC_INTERFACE(SparseVector)
71 EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(SparseVector, +=)
72 EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(SparseVector, -=)
73
76
77 enum {
78 Options = _Options
79 };
80
81 EIGEN_STRONG_INLINE Index rows() const { return IsColVector ? m_size : 1; }
82 EIGEN_STRONG_INLINE Index cols() const { return IsColVector ? 1 : m_size; }
83 EIGEN_STRONG_INLINE Index innerSize() const { return m_size; }
84 EIGEN_STRONG_INLINE Index outerSize() const { return 1; }
85
86 EIGEN_STRONG_INLINE const Scalar* valuePtr() const { return m_data.valuePtr(); }
87 EIGEN_STRONG_INLINE Scalar* valuePtr() { return m_data.valuePtr(); }
88
89 EIGEN_STRONG_INLINE const StorageIndex* innerIndexPtr() const { return m_data.indexPtr(); }
90 EIGEN_STRONG_INLINE StorageIndex* innerIndexPtr() { return m_data.indexPtr(); }
91
92 inline const StorageIndex* outerIndexPtr() const { return 0; }
93 inline StorageIndex* outerIndexPtr() { return 0; }
94 inline const StorageIndex* innerNonZeroPtr() const { return 0; }
95 inline StorageIndex* innerNonZeroPtr() { return 0; }
96
98 inline Storage& data() { return m_data; }
100 inline const Storage& data() const { return m_data; }
101
102 inline Scalar coeff(Index row, Index col) const
103 {
104 eigen_assert(IsColVector ? (col==0 && row>=0 && row<m_size) : (row==0 && col>=0 && col<m_size));
105 return coeff(IsColVector ? row : col);
106 }
107 inline Scalar coeff(Index i) const
108 {
109 eigen_assert(i>=0 && i<m_size);
110 return m_data.at(StorageIndex(i));
111 }
112
113 inline Scalar& coeffRef(Index row, Index col)
114 {
115 eigen_assert(IsColVector ? (col==0 && row>=0 && row<m_size) : (row==0 && col>=0 && col<m_size));
116 return coeffRef(IsColVector ? row : col);
117 }
118
125 inline Scalar& coeffRef(Index i)
126 {
127 eigen_assert(i>=0 && i<m_size);
128
129 return m_data.atWithInsertion(StorageIndex(i));
130 }
131
132 public:
133
134 typedef typename Base::InnerIterator InnerIterator;
135 typedef typename Base::ReverseInnerIterator ReverseInnerIterator;
136
137 inline void setZero() { m_data.clear(); }
138
140 inline Index nonZeros() const { return m_data.size(); }
141
142 inline void startVec(Index outer)
143 {
144 EIGEN_UNUSED_VARIABLE(outer);
145 eigen_assert(outer==0);
146 }
147
148 inline Scalar& insertBackByOuterInner(Index outer, Index inner)
149 {
150 EIGEN_UNUSED_VARIABLE(outer);
151 eigen_assert(outer==0);
152 return insertBack(inner);
153 }
154 inline Scalar& insertBack(Index i)
155 {
156 m_data.append(0, i);
157 return m_data.value(m_data.size()-1);
158 }
159
160 Scalar& insertBackByOuterInnerUnordered(Index outer, Index inner)
161 {
162 EIGEN_UNUSED_VARIABLE(outer);
163 eigen_assert(outer==0);
164 return insertBackUnordered(inner);
165 }
166 inline Scalar& insertBackUnordered(Index i)
167 {
168 m_data.append(0, i);
169 return m_data.value(m_data.size()-1);
170 }
171
172 inline Scalar& insert(Index row, Index col)
173 {
174 eigen_assert(IsColVector ? (col==0 && row>=0 && row<m_size) : (row==0 && col>=0 && col<m_size));
175
176 Index inner = IsColVector ? row : col;
177 Index outer = IsColVector ? col : row;
178 EIGEN_ONLY_USED_FOR_DEBUG(outer);
179 eigen_assert(outer==0);
180 return insert(inner);
181 }
182 Scalar& insert(Index i)
183 {
184 eigen_assert(i>=0 && i<m_size);
185
186 Index startId = 0;
187 Index p = Index(m_data.size()) - 1;
188 // TODO smart realloc
189 m_data.resize(p+2,1);
190
191 while ( (p >= startId) && (m_data.index(p) > i) )
192 {
193 m_data.index(p+1) = m_data.index(p);
194 m_data.value(p+1) = m_data.value(p);
195 --p;
196 }
197 m_data.index(p+1) = convert_index(i);
198 m_data.value(p+1) = 0;
199 return m_data.value(p+1);
200 }
201
204 inline void reserve(Index reserveSize) { m_data.reserve(reserveSize); }
205
206
207 inline void finalize() {}
208
210 void prune(const Scalar& reference, const RealScalar& epsilon = NumTraits<RealScalar>::dummy_precision())
211 {
212 m_data.prune(reference,epsilon);
213 }
214
223 void resize(Index rows, Index cols)
224 {
225 eigen_assert((IsColVector ? cols : rows)==1 && "Outer dimension must equal 1");
226 resize(IsColVector ? rows : cols);
227 }
228
234 {
235 m_size = newSize;
236 m_data.clear();
237 }
238
247 {
248 if (newSize < m_size)
249 {
250 Index i = 0;
251 while (i<m_data.size() && m_data.index(i)<newSize) ++i;
252 m_data.resize(i);
253 }
254 m_size = newSize;
255 }
256
257 void resizeNonZeros(Index size) { m_data.resize(size); }
258
259 inline SparseVector() : m_size(0) { check_template_parameters(); resize(0); }
260
261 explicit inline SparseVector(Index size) : m_size(0) { check_template_parameters(); resize(size); }
262
263 inline SparseVector(Index rows, Index cols) : m_size(0) { check_template_parameters(); resize(rows,cols); }
264
265 template<typename OtherDerived>
266 inline SparseVector(const SparseMatrixBase<OtherDerived>& other)
267 : m_size(0)
268 {
269 #ifdef EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
270 EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
271 #endif
272 check_template_parameters();
273 *this = other.derived();
274 }
275
276 inline SparseVector(const SparseVector& other)
277 : Base(other), m_size(0)
278 {
279 check_template_parameters();
280 *this = other.derived();
281 }
282
287 inline void swap(SparseVector& other)
288 {
289 std::swap(m_size, other.m_size);
290 m_data.swap(other.m_data);
291 }
292
293 template<int OtherOptions>
295 {
296 eigen_assert(other.outerSize()==1);
297 std::swap(m_size, other.m_innerSize);
298 m_data.swap(other.m_data);
299 }
300
301 inline SparseVector& operator=(const SparseVector& other)
302 {
303 if (other.isRValue())
304 {
305 swap(other.const_cast_derived());
306 }
307 else
308 {
309 resize(other.size());
310 m_data = other.m_data;
311 }
312 return *this;
313 }
314
315 template<typename OtherDerived>
316 inline SparseVector& operator=(const SparseMatrixBase<OtherDerived>& other)
317 {
318 SparseVector tmp(other.size());
319 internal::sparse_vector_assign_selector<SparseVector,OtherDerived>::run(tmp,other.derived());
320 this->swap(tmp);
321 return *this;
322 }
323
324 #ifndef EIGEN_PARSED_BY_DOXYGEN
325 template<typename Lhs, typename Rhs>
326 inline SparseVector& operator=(const SparseSparseProduct<Lhs,Rhs>& product)
327 {
328 return Base::operator=(product);
329 }
330 #endif
331
332#ifndef EIGEN_NO_IO
333 friend std::ostream & operator << (std::ostream & s, const SparseVector& m)
334 {
335 for (Index i=0; i<m.nonZeros(); ++i)
336 s << "(" << m.m_data.value(i) << "," << m.m_data.index(i) << ") ";
337 s << std::endl;
338 return s;
339 }
340#endif
341
343 inline ~SparseVector() {}
344
346 Scalar sum() const;
347
348 public:
349
351 EIGEN_DEPRECATED void startFill(Index reserve)
352 {
353 setZero();
354 m_data.reserve(reserve);
355 }
356
358 EIGEN_DEPRECATED Scalar& fill(Index r, Index c)
359 {
360 eigen_assert(r==0 || c==0);
361 return fill(IsColVector ? r : c);
362 }
363
365 EIGEN_DEPRECATED Scalar& fill(Index i)
366 {
367 m_data.append(0, i);
368 return m_data.value(m_data.size()-1);
369 }
370
372 EIGEN_DEPRECATED Scalar& fillrand(Index r, Index c)
373 {
374 eigen_assert(r==0 || c==0);
375 return fillrand(IsColVector ? r : c);
376 }
377
379 EIGEN_DEPRECATED Scalar& fillrand(Index i)
380 {
381 return insert(i);
382 }
383
385 EIGEN_DEPRECATED void endFill() {}
386
387 // These two functions were here in the 3.1 release, so let's keep them in case some code rely on them.
389 EIGEN_DEPRECATED Storage& _data() { return m_data; }
391 EIGEN_DEPRECATED const Storage& _data() const { return m_data; }
392
393# ifdef EIGEN_SPARSEVECTOR_PLUGIN
394# include EIGEN_SPARSEVECTOR_PLUGIN
395# endif
396
397protected:
398
399 static void check_template_parameters()
400 {
401 EIGEN_STATIC_ASSERT(NumTraits<StorageIndex>::IsSigned,THE_INDEX_TYPE_MUST_BE_A_SIGNED_TYPE);
402 EIGEN_STATIC_ASSERT((_Options&(ColMajor|RowMajor))==Options,INVALID_MATRIX_TEMPLATE_PARAMETERS);
403 }
404
405 Storage m_data;
406 Index m_size;
407};
408
409namespace internal {
410
411template<typename _Scalar, int _Options, typename _Index>
412struct evaluator<SparseVector<_Scalar,_Options,_Index> >
413 : evaluator_base<SparseVector<_Scalar,_Options,_Index> >
414{
418 typedef typename SparseVectorType::ReverseInnerIterator ReverseInnerIterator;
419
420 enum {
421 CoeffReadCost = NumTraits<_Scalar>::ReadCost,
422 Flags = SparseVectorType::Flags
423 };
424
425 evaluator() : Base() {}
426
427 explicit evaluator(const SparseVectorType &mat) : m_matrix(&mat)
428 {
429 EIGEN_INTERNAL_CHECK_COST_VALUE(CoeffReadCost);
430 }
431
432 inline Index nonZerosEstimate() const {
433 return m_matrix->nonZeros();
434 }
435
436 operator SparseVectorType&() { return m_matrix->const_cast_derived(); }
437 operator const SparseVectorType&() const { return *m_matrix; }
438
439 const SparseVectorType *m_matrix;
440};
441
442template< typename Dest, typename Src>
444 static void run(Dest& dst, const Src& src) {
445 eigen_internal_assert(src.innerSize()==src.size());
446 typedef internal::evaluator<Src> SrcEvaluatorType;
447 SrcEvaluatorType srcEval(src);
448 for(typename SrcEvaluatorType::InnerIterator it(srcEval, 0); it; ++it)
449 dst.insert(it.index()) = it.value();
450 }
451};
452
453template< typename Dest, typename Src>
455 static void run(Dest& dst, const Src& src) {
456 eigen_internal_assert(src.outerSize()==src.size());
457 typedef internal::evaluator<Src> SrcEvaluatorType;
458 SrcEvaluatorType srcEval(src);
459 for(Index i=0; i<src.size(); ++i)
460 {
461 typename SrcEvaluatorType::InnerIterator it(srcEval, i);
462 if(it)
463 dst.insert(i) = it.value();
464 }
465 }
466};
467
468template< typename Dest, typename Src>
475
476}
477
478} // end namespace Eigen
479
480#endif // EIGEN_SPARSEVECTOR_H
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index innerSize() const
Definition DenseBase.h:235
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index outerSize() const
Definition DenseBase.h:223
EIGEN_DEVICE_FUNC CoeffReturnType value() const
Definition DenseBase.h:526
An InnerIterator allows to loop over the element of any matrix expression.
Definition CoreIterators.h:34
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
Common base class for sparse [compressed]-{row|column}-storage format.
Definition SparseCompressedBase.h:38
internal::traits< Derived >::StorageIndex StorageIndex
The integer type used to store indices within a SparseMatrix.
Definition SparseMatrixBase.h:43
Index size() const
Definition SparseMatrixBase.h:181
NumTraits< Scalar >::Real RealScalar
This is the "real scalar" type; if the Scalar type is already real numbers (e.g.
Definition SparseMatrixBase.h:128
a sparse vector class
Definition SparseVector.h:66
Scalar sum() const
Overloaded for performance.
Definition SparseRedux.h:41
void resize(Index newSize)
Resizes the sparse vector to newSize This method deletes all entries, thus leaving an empty sparse ve...
Definition SparseVector.h:233
void resize(Index rows, Index cols)
Resizes the sparse vector to rows x cols.
Definition SparseVector.h:223
Scalar & coeffRef(Index i)
Definition SparseVector.h:125
void swap(SparseVector &other)
Swaps the values of *this and other.
Definition SparseVector.h:287
void conservativeResize(Index newSize)
Resizes the sparse vector to newSize, while leaving old values untouched.
Definition SparseVector.h:246
~SparseVector()
Destructor.
Definition SparseVector.h:343
Index nonZeros() const
Definition SparseVector.h:140
void prune(const Scalar &reference, const RealScalar &epsilon=NumTraits< RealScalar >::dummy_precision())
Suppresses all nonzeros which are much smaller than reference under the tolerance epsilon.
Definition SparseVector.h:210
Definition CompressedStorage.h:23
Scalar at(Index key, const Scalar &defaultValue=Scalar(0)) const
Definition CompressedStorage.h:146
Scalar & atWithInsertion(Index key, const Scalar &defaultValue=Scalar(0))
Definition CompressedStorage.h:174
@ ColMajor
Storage order is column major (see TopicStorageOrders).
Definition Constants.h:319
@ RowMajor
Storage order is row major (see TopicStorageOrders).
Definition Constants.h:321
const unsigned int LvalueBit
Means the expression has a coeffRef() method, i.e.
Definition Constants.h:144
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition Constants.h:66
const unsigned int CompressedAccessBit
Means that the underlying coefficients can be accessed through pointers to the sparse (un)compressed ...
Definition Constants.h:191
Namespace containing all symbols from the Eigen library.
Definition LDLT.h:16
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:74
const int Dynamic
This value means that a positive quantity (e.g., a size) is not known at compile-time,...
Definition Constants.h:22
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition json.hpp:24418
The type used to identify a matrix expression.
Definition Constants.h:522
Holds information about the various numeric (i.e.
Definition NumTraits.h:236
The type used to identify a general sparse storage.
Definition Constants.h:510
Definition CoreEvaluators.h:111
Definition CoreEvaluators.h:91
Definition ForwardDeclarations.h:17