SparseVector.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
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 //     EIGEN_SPARSE_INHERIT_ASSIGNMENT_OPERATOR(SparseVector, =)
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       // TODO smart realloc
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 //       const bool needToTranspose = (Flags & RowMajorBit) != (OtherDerived::Flags & RowMajorBit);
00266 //       if (needToTranspose)
00267 //       {
00268 //         // two passes algorithm:
00269 //         //  1 - compute the number of coeffs per dest inner vector
00270 //         //  2 - do the actual copy/eval
00271 //         // Since each coeff of the rhs has to be evaluated twice, let's evauluate it if needed
00272 //         typedef typename internal::nested<OtherDerived,2>::type OtherCopy;
00273 //         OtherCopy otherCopy(other.derived());
00274 //         typedef typename internal::remove_all<OtherCopy>::type _OtherCopy;
00275 //
00276 //         resize(other.rows(), other.cols());
00277 //         Eigen::Map<VectorXi>(m_outerIndex,outerSize()).setZero();
00278 //         // pass 1
00279 //         // FIXME the above copy could be merged with that pass
00280 //         for (int j=0; j<otherCopy.outerSize(); ++j)
00281 //           for (typename _OtherCopy::InnerIterator it(otherCopy, j); it; ++it)
00282 //             ++m_outerIndex[it.index()];
00283 //
00284 //         // prefix sum
00285 //         int count = 0;
00286 //         VectorXi positions(outerSize());
00287 //         for (int j=0; j<outerSize(); ++j)
00288 //         {
00289 //           int tmp = m_outerIndex[j];
00290 //           m_outerIndex[j] = count;
00291 //           positions[j] = count;
00292 //           count += tmp;
00293 //         }
00294 //         m_outerIndex[outerSize()] = count;
00295 //         // alloc
00296 //         m_data.resize(count);
00297 //         // pass 2
00298 //         for (int j=0; j<otherCopy.outerSize(); ++j)
00299 //           for (typename _OtherCopy::InnerIterator it(otherCopy, j); it; ++it)
00300 //           {
00301 //             int pos = positions[it.index()]++;
00302 //             m_data.index(pos) = j;
00303 //             m_data.value(pos) = it.value();
00304 //           }
00305 //
00306 //         return *this;
00307 //       }
00308 //       else
00309 //       {
00310 //         // there is no special optimization
00311 //         return SparseMatrixBase<SparseMatrix>::operator=(other.derived());
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     // this specialized version does not seems to be faster
00324 //     Scalar dot(const SparseVector& other) const
00325 //     {
00326 //       int i=0, j=0;
00327 //       Scalar res = 0;
00328 //       asm("#begindot");
00329 //       while (i<nonZeros() && j<other.nonZeros())
00330 //       {
00331 //         if (m_data.index(i)==other.m_data.index(j))
00332 //         {
00333 //           res += m_data.value(i) * internal::conj(other.m_data.value(j));
00334 //           ++i; ++j;
00335 //         }
00336 //         else if (m_data.index(i)<other.m_data.index(j))
00337 //           ++i;
00338 //         else
00339 //           ++j;
00340 //       }
00341 //       asm("#enddot");
00342 //       return res;
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


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:33:39