10 #ifndef EIGEN_DYNAMIC_SPARSEMATRIX_H 11 #define EIGEN_DYNAMIC_SPARSEMATRIX_H 36 template<
typename _Scalar,
int _Options,
typename _StorageIndex>
37 struct traits<DynamicSparseMatrix<_Scalar, _Options, _StorageIndex> >
39 typedef _Scalar Scalar;
40 typedef _StorageIndex StorageIndex;
41 typedef Sparse StorageKind;
42 typedef MatrixXpr XprKind;
44 RowsAtCompileTime = Dynamic,
45 ColsAtCompileTime = Dynamic,
46 MaxRowsAtCompileTime = Dynamic,
47 MaxColsAtCompileTime = Dynamic,
48 Flags = _Options | NestByRefBit | LvalueBit,
49 CoeffReadCost = NumTraits<Scalar>::ReadCost,
50 SupportedAccessPatterns = OuterRandomAccessPattern
55 template<
typename _Scalar,
int _Options,
typename _StorageIndex>
57 :
public SparseMatrixBase<DynamicSparseMatrix<_Scalar, _Options, _StorageIndex> >
59 typedef SparseMatrixBase<DynamicSparseMatrix> Base;
60 using Base::convert_index;
66 typedef MappedSparseMatrix<Scalar,Flags> Map;
67 using Base::IsRowMajor;
68 using Base::operator=;
78 std::vector<internal::CompressedStorage<Scalar,StorageIndex> > m_data;
82 inline Index rows()
const {
return IsRowMajor ? outerSize() : m_innerSize; }
83 inline Index cols()
const {
return IsRowMajor ? m_innerSize : outerSize(); }
84 inline Index innerSize()
const {
return m_innerSize; }
85 inline Index outerSize()
const {
return convert_index(m_data.size()); }
86 inline Index innerNonZeros(Index j)
const {
return m_data[j].size(); }
88 std::vector<internal::CompressedStorage<Scalar,StorageIndex> >& _data() {
return m_data; }
89 const std::vector<internal::CompressedStorage<Scalar,StorageIndex> >& _data()
const {
return m_data; }
94 inline Scalar
coeff(Index row, Index col)
const 96 const Index outer = IsRowMajor ? row : col;
97 const Index inner = IsRowMajor ? col : row;
98 return m_data[outer].at(inner);
107 const Index outer = IsRowMajor ? row : col;
108 const Index inner = IsRowMajor ? col : row;
109 return m_data[outer].atWithInsertion(inner);
113 class ReverseInnerIterator;
117 for (Index j=0; j<outerSize(); ++j)
125 for (Index j=0; j<outerSize(); ++j)
126 res += m_data[j].size();
132 void reserve(Index reserveSize = 1000)
136 Index reserveSizePerVector = (std::max)(reserveSize/outerSize(),Index(4));
137 for (Index j=0; j<outerSize(); ++j)
139 m_data[j].reserve(reserveSizePerVector);
154 return insertBackByOuterInner(IsRowMajor?row:col, IsRowMajor?col:row);
160 eigen_assert(outer<Index(m_data.size()) && inner<m_innerSize &&
"out of range");
161 eigen_assert(((m_data[outer].size()==0) || (m_data[outer].index(m_data[outer].size()-1)<inner))
162 &&
"wrong sorted insertion");
163 m_data[outer].append(0, inner);
164 return m_data[outer].value(m_data[outer].size()-1);
167 inline Scalar& insert(Index row, Index col)
169 const Index outer = IsRowMajor ? row : col;
170 const Index inner = IsRowMajor ? col : row;
173 Index
id =
static_cast<Index
>(m_data[outer].size()) - 1;
174 m_data[outer].resize(
id+2,1);
176 while ( (
id >= startId) && (m_data[outer].index(
id) > inner) )
178 m_data[outer].index(
id+1) = m_data[outer].index(
id);
179 m_data[outer].value(
id+1) = m_data[outer].value(
id);
182 m_data[outer].index(
id+1) = inner;
183 m_data[outer].value(
id+1) = 0;
184 return m_data[outer].value(
id+1);
191 void prune(Scalar reference, RealScalar epsilon = NumTraits<RealScalar>::dummy_precision())
193 for (Index j=0; j<outerSize(); ++j)
194 m_data[j].prune(reference,epsilon);
201 const Index outerSize = IsRowMajor ? rows : cols;
202 m_innerSize = convert_index(IsRowMajor ? cols : rows);
204 if (Index(m_data.size()) != outerSize)
206 m_data.resize(outerSize);
210 void resizeAndKeepData(Index rows, Index cols)
212 const Index outerSize = IsRowMajor ? rows : cols;
213 const Index innerSize = IsRowMajor ? cols : rows;
214 if (m_innerSize>innerSize)
221 if (m_data.size() != outerSize)
223 m_data.resize(outerSize);
229 : m_innerSize(0), m_data(0)
231 eigen_assert(innerSize()==0 && outerSize()==0);
242 template<
typename OtherDerived>
246 Base::operator=(other.derived());
250 : Base(), m_innerSize(0)
252 *
this = other.derived();
255 inline void swap(DynamicSparseMatrix& other)
258 std::swap(m_innerSize, other.m_innerSize);
260 m_data.swap(other.m_data);
263 inline DynamicSparseMatrix& operator=(
const DynamicSparseMatrix& other)
265 if (other.isRValue())
267 swap(other.const_cast_derived());
271 resize(other.rows(), other.cols());
272 m_data = other.m_data;
284 EIGEN_DEPRECATED
void startFill(Index reserveSize = 1000)
287 reserve(reserveSize);
299 EIGEN_DEPRECATED Scalar&
fill(Index row, Index col)
301 const Index outer = IsRowMajor ? row : col;
302 const Index inner = IsRowMajor ? col : row;
303 return insertBack(outer,inner);
311 EIGEN_DEPRECATED Scalar&
fillrand(Index row, Index col)
313 return insert(row,col);
320 # ifdef EIGEN_DYNAMICSPARSEMATRIX_PLUGIN 321 # include EIGEN_DYNAMICSPARSEMATRIX_PLUGIN 325 template<
typename Scalar,
int _Options,
typename _StorageIndex>
326 class DynamicSparseMatrix<Scalar,_Options,_StorageIndex>::InnerIterator :
public SparseVector<Scalar,_Options,_StorageIndex>::InnerIterator
328 typedef typename SparseVector<Scalar,_Options,_StorageIndex>::InnerIterator Base;
331 : Base(mat.m_data[outer]), m_outer(outer)
334 inline Index row()
const {
return IsRowMajor ? m_outer : Base::index(); }
335 inline Index col()
const {
return IsRowMajor ? Base::index() : m_outer; }
336 inline Index outer()
const {
return m_outer; }
342 template<
typename Scalar,
int _Options,
typename _StorageIndex>
343 class DynamicSparseMatrix<Scalar,_Options,_StorageIndex>::ReverseInnerIterator :
public SparseVector<Scalar,_Options,_StorageIndex>::ReverseInnerIterator
345 typedef typename SparseVector<Scalar,_Options,_StorageIndex>::ReverseInnerIterator Base;
348 : Base(mat.m_data[outer]), m_outer(outer)
351 inline Index row()
const {
return IsRowMajor ? m_outer : Base::index(); }
352 inline Index col()
const {
return IsRowMajor ? Base::index() : m_outer; }
353 inline Index outer()
const {
return m_outer; }
361 template<
typename _Scalar,
int _Options,
typename _StorageIndex>
362 struct evaluator<DynamicSparseMatrix<_Scalar,_Options,_StorageIndex> >
363 : evaluator_base<DynamicSparseMatrix<_Scalar,_Options,_StorageIndex> >
365 typedef _Scalar Scalar;
367 typedef typename SparseMatrixType::InnerIterator InnerIterator;
368 typedef typename SparseMatrixType::ReverseInnerIterator ReverseInnerIterator;
371 CoeffReadCost = NumTraits<_Scalar>::ReadCost,
372 Flags = SparseMatrixType::Flags
375 evaluator() : m_matrix(0) {}
376 evaluator(
const SparseMatrixType &mat) : m_matrix(&mat) {}
378 operator SparseMatrixType&() {
return m_matrix->const_cast_derived(); }
379 operator const SparseMatrixType&()
const {
return *m_matrix; }
381 Scalar coeff(Index row, Index col)
const {
return m_matrix->coeff(row,col); }
383 Index nonZerosEstimate()
const {
return m_matrix->nonZeros(); }
385 const SparseMatrixType *m_matrix;
392 #endif // EIGEN_DYNAMIC_SPARSEMATRIX_H Scalar & insertBackByOuterInner(Index outer, Index inner)
Definition: DynamicSparseMatrix.h:158
Scalar & insertBack(Index row, Index col)
Definition: DynamicSparseMatrix.h:152
void startVec(Index)
Definition: DynamicSparseMatrix.h:145
Namespace containing all symbols from the Eigen library.
Definition: AdolcForward:45
void resize(Index rows, Index cols)
Definition: DynamicSparseMatrix.h:199
Scalar coeff(Index row, Index col) const
Definition: DynamicSparseMatrix.h:94
~DynamicSparseMatrix()
Definition: DynamicSparseMatrix.h:278
EIGEN_DEPRECATED void startFill(Index reserveSize=1000)
Definition: DynamicSparseMatrix.h:284
EIGEN_DEPRECATED Scalar & fillrand(Index row, Index col)
Definition: DynamicSparseMatrix.h:311
EIGEN_DEPRECATED void endFill()
Definition: DynamicSparseMatrix.h:318
EIGEN_DEPRECATED Scalar & fill(Index row, Index col)
Definition: DynamicSparseMatrix.h:299
Index nonZeros() const
Definition: DynamicSparseMatrix.h:122
A sparse matrix class designed for matrix assembly purpose.
Definition: DynamicSparseMatrix.h:56
EIGEN_DEPRECATED DynamicSparseMatrix(Index rows, Index cols)
Definition: DynamicSparseMatrix.h:235
Scalar & coeffRef(Index row, Index col)
Definition: DynamicSparseMatrix.h:105
void finalize()
Definition: DynamicSparseMatrix.h:188
EIGEN_DEPRECATED DynamicSparseMatrix()
Definition: DynamicSparseMatrix.h:228
void prune(Scalar reference, RealScalar epsilon=NumTraits< RealScalar >::dummy_precision())
Definition: DynamicSparseMatrix.h:191
EIGEN_DEPRECATED DynamicSparseMatrix(const SparseMatrixBase< OtherDerived > &other)
Definition: DynamicSparseMatrix.h:243