00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef EIGEN_SPARSEVECTOR_H
00026 #define EIGEN_SPARSEVECTOR_H
00027
00040 namespace internal {
00041 template<typename _Scalar, int _Options, typename _Index>
00042 struct traits<SparseVector<_Scalar, _Options, _Index> >
00043 {
00044 typedef _Scalar Scalar;
00045 typedef _Index Index;
00046 typedef Sparse StorageKind;
00047 typedef MatrixXpr XprKind;
00048 enum {
00049 IsColVector = _Options & RowMajorBit ? 0 : 1,
00050
00051 RowsAtCompileTime = IsColVector ? Dynamic : 1,
00052 ColsAtCompileTime = IsColVector ? 1 : Dynamic,
00053 MaxRowsAtCompileTime = RowsAtCompileTime,
00054 MaxColsAtCompileTime = ColsAtCompileTime,
00055 Flags = _Options | NestByRefBit | LvalueBit,
00056 CoeffReadCost = NumTraits<Scalar>::ReadCost,
00057 SupportedAccessPatterns = InnerRandomAccessPattern
00058 };
00059 };
00060 }
00061
00062 template<typename _Scalar, int _Options, typename _Index>
00063 class SparseVector
00064 : public SparseMatrixBase<SparseVector<_Scalar, _Options, _Index> >
00065 {
00066 public:
00067 EIGEN_SPARSE_PUBLIC_INTERFACE(SparseVector)
00068 EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(SparseVector, +=)
00069 EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(SparseVector, -=)
00070
00071
00072 protected:
00073 public:
00074
00075 typedef SparseMatrixBase<SparseVector> SparseBase;
00076 enum { IsColVector = internal::traits<SparseVector>::IsColVector };
00077
00078 enum {
00079 Options = _Options
00080 };
00081
00082 CompressedStorage<Scalar,Index> m_data;
00083 Index m_size;
00084
00085 CompressedStorage<Scalar,Index>& _data() { return m_data; }
00086 CompressedStorage<Scalar,Index>& _data() const { return m_data; }
00087
00088 public:
00089
00090 EIGEN_STRONG_INLINE Index rows() const { return IsColVector ? m_size : 1; }
00091 EIGEN_STRONG_INLINE Index cols() const { return IsColVector ? 1 : m_size; }
00092 EIGEN_STRONG_INLINE Index innerSize() const { return m_size; }
00093 EIGEN_STRONG_INLINE Index outerSize() const { return 1; }
00094 EIGEN_STRONG_INLINE Index innerNonZeros(Index j) const { eigen_assert(j==0); return m_size; }
00095
00096 EIGEN_STRONG_INLINE const Scalar* _valuePtr() const { return &m_data.value(0); }
00097 EIGEN_STRONG_INLINE Scalar* _valuePtr() { return &m_data.value(0); }
00098
00099 EIGEN_STRONG_INLINE const Index* _innerIndexPtr() const { return &m_data.index(0); }
00100 EIGEN_STRONG_INLINE Index* _innerIndexPtr() { return &m_data.index(0); }
00101
00102 inline Scalar coeff(Index row, Index col) const
00103 {
00104 eigen_assert((IsColVector ? col : row)==0);
00105 return coeff(IsColVector ? row : col);
00106 }
00107 inline Scalar coeff(Index i) const { return m_data.at(i); }
00108
00109 inline Scalar& coeffRef(Index row, Index col)
00110 {
00111 eigen_assert((IsColVector ? col : row)==0);
00112 return coeff(IsColVector ? row : col);
00113 }
00114
00121 inline Scalar& coeffRef(Index i)
00122 {
00123 return m_data.atWithInsertion(i);
00124 }
00125
00126 public:
00127
00128 class InnerIterator;
00129
00130 inline void setZero() { m_data.clear(); }
00131
00133 inline Index nonZeros() const { return static_cast<Index>(m_data.size()); }
00134
00135 inline void startVec(Index outer)
00136 {
00137 eigen_assert(outer==0);
00138 }
00139
00140 inline Scalar& insertBackByOuterInner(Index outer, Index inner)
00141 {
00142 eigen_assert(outer==0);
00143 return insertBack(inner);
00144 }
00145 inline Scalar& insertBack(Index i)
00146 {
00147 m_data.append(0, i);
00148 return m_data.value(m_data.size()-1);
00149 }
00150
00151 inline Scalar& insert(Index row, Index col)
00152 {
00153 Index inner = IsColVector ? row : col;
00154 Index outer = IsColVector ? col : row;
00155 eigen_assert(outer==0);
00156 return insert(inner);
00157 }
00158 Scalar& insert(Index i)
00159 {
00160 Index startId = 0;
00161 Index p = m_data.size() - 1;
00162
00163 m_data.resize(p+2,1);
00164
00165 while ( (p >= startId) && (m_data.index(p) > i) )
00166 {
00167 m_data.index(p+1) = m_data.index(p);
00168 m_data.value(p+1) = m_data.value(p);
00169 --p;
00170 }
00171 m_data.index(p+1) = i;
00172 m_data.value(p+1) = 0;
00173 return m_data.value(p+1);
00174 }
00175
00178 inline void reserve(Index reserveSize) { m_data.reserve(reserveSize); }
00179
00180
00181 inline void finalize() {}
00182
00183 void prune(Scalar reference, RealScalar epsilon = NumTraits<RealScalar>::dummy_precision())
00184 {
00185 m_data.prune(reference,epsilon);
00186 }
00187
00188 void resize(Index rows, Index cols)
00189 {
00190 eigen_assert(rows==1 || cols==1);
00191 resize(IsColVector ? rows : cols);
00192 }
00193
00194 void resize(Index newSize)
00195 {
00196 m_size = newSize;
00197 m_data.clear();
00198 }
00199
00200 void resizeNonZeros(Index size) { m_data.resize(size); }
00201
00202 inline SparseVector() : m_size(0) { resize(0); }
00203
00204 inline SparseVector(Index size) : m_size(0) { resize(size); }
00205
00206 inline SparseVector(Index rows, Index cols) : m_size(0) { resize(rows,cols); }
00207
00208 template<typename OtherDerived>
00209 inline SparseVector(const MatrixBase<OtherDerived>& other)
00210 : m_size(0)
00211 {
00212 *this = other.derived();
00213 }
00214
00215 template<typename OtherDerived>
00216 inline SparseVector(const SparseMatrixBase<OtherDerived>& other)
00217 : m_size(0)
00218 {
00219 *this = other.derived();
00220 }
00221
00222 inline SparseVector(const SparseVector& other)
00223 : m_size(0)
00224 {
00225 *this = other.derived();
00226 }
00227
00228 inline void swap(SparseVector& other)
00229 {
00230 std::swap(m_size, other.m_size);
00231 m_data.swap(other.m_data);
00232 }
00233
00234 inline SparseVector& operator=(const SparseVector& other)
00235 {
00236 if (other.isRValue())
00237 {
00238 swap(other.const_cast_derived());
00239 }
00240 else
00241 {
00242 resize(other.size());
00243 m_data = other.m_data;
00244 }
00245 return *this;
00246 }
00247
00248 template<typename OtherDerived>
00249 inline SparseVector& operator=(const SparseMatrixBase<OtherDerived>& other)
00250 {
00251 if (int(RowsAtCompileTime)!=int(OtherDerived::RowsAtCompileTime))
00252 return Base::operator=(other.transpose());
00253 else
00254 return Base::operator=(other);
00255 }
00256
00257 #ifndef EIGEN_PARSED_BY_DOXYGEN
00258 template<typename Lhs, typename Rhs>
00259 inline SparseVector& operator=(const SparseSparseProduct<Lhs,Rhs>& product)
00260 {
00261 return Base::operator=(product);
00262 }
00263 #endif
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 friend std::ostream & operator << (std::ostream & s, const SparseVector& m)
00316 {
00317 for (Index i=0; i<m.nonZeros(); ++i)
00318 s << "(" << m.m_data.value(i) << "," << m.m_data.index(i) << ") ";
00319 s << std::endl;
00320 return s;
00321 }
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00346 inline ~SparseVector() {}
00347
00349 Scalar sum() const;
00350
00351 public:
00352
00354 EIGEN_DEPRECATED void startFill(Index reserve)
00355 {
00356 setZero();
00357 m_data.reserve(reserve);
00358 }
00359
00361 EIGEN_DEPRECATED Scalar& fill(Index r, Index c)
00362 {
00363 eigen_assert(r==0 || c==0);
00364 return fill(IsColVector ? r : c);
00365 }
00366
00368 EIGEN_DEPRECATED Scalar& fill(Index i)
00369 {
00370 m_data.append(0, i);
00371 return m_data.value(m_data.size()-1);
00372 }
00373
00375 EIGEN_DEPRECATED Scalar& fillrand(Index r, Index c)
00376 {
00377 eigen_assert(r==0 || c==0);
00378 return fillrand(IsColVector ? r : c);
00379 }
00380
00382 EIGEN_DEPRECATED Scalar& fillrand(Index i)
00383 {
00384 return insert(i);
00385 }
00386
00388 EIGEN_DEPRECATED void endFill() {}
00389
00390 # ifdef EIGEN_SPARSEVECTOR_PLUGIN
00391 # include EIGEN_SPARSEVECTOR_PLUGIN
00392 # endif
00393 };
00394
00395 template<typename Scalar, int _Options, typename _Index>
00396 class SparseVector<Scalar,_Options,_Index>::InnerIterator
00397 {
00398 public:
00399 InnerIterator(const SparseVector& vec, Index outer=0)
00400 : m_data(vec.m_data), m_id(0), m_end(static_cast<Index>(m_data.size()))
00401 {
00402 eigen_assert(outer==0);
00403 }
00404
00405 InnerIterator(const CompressedStorage<Scalar,Index>& data)
00406 : m_data(data), m_id(0), m_end(static_cast<Index>(m_data.size()))
00407 {}
00408
00409 template<unsigned int Added, unsigned int Removed>
00410 InnerIterator(const Flagged<SparseVector,Added,Removed>& vec, Index )
00411 : m_data(vec._expression().m_data), m_id(0), m_end(m_data.size())
00412 {}
00413
00414 inline InnerIterator& operator++() { m_id++; return *this; }
00415
00416 inline Scalar value() const { return m_data.value(m_id); }
00417 inline Scalar& valueRef() { return const_cast<Scalar&>(m_data.value(m_id)); }
00418
00419 inline Index index() const { return m_data.index(m_id); }
00420 inline Index row() const { return IsColVector ? index() : 0; }
00421 inline Index col() const { return IsColVector ? 0 : index(); }
00422
00423 inline operator bool() const { return (m_id < m_end); }
00424
00425 protected:
00426 const CompressedStorage<Scalar,Index>& m_data;
00427 Index m_id;
00428 const Index m_end;
00429 };
00430
00431 #endif // EIGEN_SPARSEVECTOR_H