SparseBlock.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 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_SPARSE_BLOCK_H
00011 #define EIGEN_SPARSE_BLOCK_H
00012 
00013 namespace Eigen { 
00014 
00015 template<typename XprType, int BlockRows, int BlockCols>
00016 class BlockImpl<XprType,BlockRows,BlockCols,true,Sparse>
00017   : public SparseMatrixBase<Block<XprType,BlockRows,BlockCols,true> >
00018 {
00019     typedef typename internal::remove_all<typename XprType::Nested>::type _MatrixTypeNested;
00020     typedef Block<XprType, BlockRows, BlockCols, true> BlockType;
00021 public:
00022     enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
00023 protected:
00024     enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
00025 public:
00026     EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
00027     
00028     class InnerIterator: public XprType::InnerIterator
00029     {
00030         typedef typename BlockImpl::Index Index;
00031       public:
00032         inline InnerIterator(const BlockType& xpr, Index outer)
00033           : XprType::InnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
00034         {}
00035         inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
00036         inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
00037       protected:
00038         Index m_outer;
00039     };
00040     class ReverseInnerIterator: public XprType::ReverseInnerIterator
00041     {
00042         typedef typename BlockImpl::Index Index;
00043       public:
00044         inline ReverseInnerIterator(const BlockType& xpr, Index outer)
00045           : XprType::ReverseInnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
00046         {}
00047         inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
00048         inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
00049       protected:
00050         Index m_outer;
00051     };
00052 
00053     inline BlockImpl(const XprType& xpr, int i)
00054       : m_matrix(xpr), m_outerStart(i), m_outerSize(OuterSize)
00055     {}
00056 
00057     inline BlockImpl(const XprType& xpr, int startRow, int startCol, int blockRows, int blockCols)
00058       : m_matrix(xpr), m_outerStart(IsRowMajor ? startRow : startCol), m_outerSize(IsRowMajor ? blockRows : blockCols)
00059     {}
00060 
00061     EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
00062     EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
00063 
00064   protected:
00065 
00066     typename XprType::Nested m_matrix;
00067     Index m_outerStart;
00068     const internal::variable_if_dynamic<Index, OuterSize> m_outerSize;
00069 };
00070 
00071 
00072 /***************************************************************************
00073 * specialisation for SparseMatrix
00074 ***************************************************************************/
00075 
00076 template<typename _Scalar, int _Options, typename _Index, int BlockRows, int BlockCols>
00077 class BlockImpl<SparseMatrix<_Scalar, _Options, _Index>,BlockRows,BlockCols,true,Sparse>
00078   : public SparseMatrixBase<Block<SparseMatrix<_Scalar, _Options, _Index>,BlockRows,BlockCols,true> >
00079 {
00080     typedef SparseMatrix<_Scalar, _Options, _Index> SparseMatrixType;
00081     typedef typename internal::remove_all<typename SparseMatrixType::Nested>::type _MatrixTypeNested;
00082     typedef Block<SparseMatrixType, BlockRows, BlockCols, true> BlockType;
00083 public:
00084     enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
00085     EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
00086 protected:
00087     enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
00088 public:
00089     
00090     class InnerIterator: public SparseMatrixType::InnerIterator
00091     {
00092       public:
00093         inline InnerIterator(const BlockType& xpr, Index outer)
00094           : SparseMatrixType::InnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
00095         {}
00096         inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
00097         inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
00098       protected:
00099         Index m_outer;
00100     };
00101     class ReverseInnerIterator: public SparseMatrixType::ReverseInnerIterator
00102     {
00103       public:
00104         inline ReverseInnerIterator(const BlockType& xpr, Index outer)
00105           : SparseMatrixType::ReverseInnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
00106         {}
00107         inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
00108         inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
00109       protected:
00110         Index m_outer;
00111     };
00112 
00113     inline BlockImpl(const SparseMatrixType& xpr, int i)
00114       : m_matrix(xpr), m_outerStart(i), m_outerSize(OuterSize)
00115     {}
00116 
00117     inline BlockImpl(const SparseMatrixType& xpr, int startRow, int startCol, int blockRows, int blockCols)
00118       : m_matrix(xpr), m_outerStart(IsRowMajor ? startRow : startCol), m_outerSize(IsRowMajor ? blockRows : blockCols)
00119     {}
00120 
00121     template<typename OtherDerived>
00122     inline BlockType& operator=(const SparseMatrixBase<OtherDerived>& other)
00123     {
00124       typedef typename internal::remove_all<typename SparseMatrixType::Nested>::type _NestedMatrixType;
00125       _NestedMatrixType& matrix = const_cast<_NestedMatrixType&>(m_matrix);;
00126       // This assignement is slow if this vector set is not empty
00127       // and/or it is not at the end of the nonzeros of the underlying matrix.
00128 
00129       // 1 - eval to a temporary to avoid transposition and/or aliasing issues
00130       SparseMatrix<Scalar, IsRowMajor ? RowMajor : ColMajor, Index> tmp(other);
00131 
00132       // 2 - let's check whether there is enough allocated memory
00133       Index nnz           = tmp.nonZeros();
00134       Index start         = m_outerStart==0 ? 0 : matrix.outerIndexPtr()[m_outerStart]; // starting position of the current block
00135       Index end           = m_matrix.outerIndexPtr()[m_outerStart+m_outerSize.value()]; // ending posiiton of the current block
00136       Index block_size    = end - start;                                                // available room in the current block
00137       Index tail_size     = m_matrix.outerIndexPtr()[m_matrix.outerSize()] - end;
00138       
00139       Index free_size     = m_matrix.isCompressed()
00140                           ? Index(matrix.data().allocatedSize()) + block_size
00141                           : block_size;
00142 
00143       if(nnz>free_size) 
00144       {
00145         // realloc manually to reduce copies
00146         typename SparseMatrixType::Storage newdata(m_matrix.data().allocatedSize() - block_size + nnz);
00147 
00148         std::memcpy(&newdata.value(0), &m_matrix.data().value(0), start*sizeof(Scalar));
00149         std::memcpy(&newdata.index(0), &m_matrix.data().index(0), start*sizeof(Index));
00150 
00151         std::memcpy(&newdata.value(start), &tmp.data().value(0), nnz*sizeof(Scalar));
00152         std::memcpy(&newdata.index(start), &tmp.data().index(0), nnz*sizeof(Index));
00153 
00154         std::memcpy(&newdata.value(start+nnz), &matrix.data().value(end), tail_size*sizeof(Scalar));
00155         std::memcpy(&newdata.index(start+nnz), &matrix.data().index(end), tail_size*sizeof(Index));
00156         
00157         newdata.resize(m_matrix.outerIndexPtr()[m_matrix.outerSize()] - block_size + nnz);
00158 
00159         matrix.data().swap(newdata);
00160       }
00161       else
00162       {
00163         // no need to realloc, simply copy the tail at its respective position and insert tmp
00164         matrix.data().resize(start + nnz + tail_size);
00165 
00166         std::memmove(&matrix.data().value(start+nnz), &matrix.data().value(end), tail_size*sizeof(Scalar));
00167         std::memmove(&matrix.data().index(start+nnz), &matrix.data().index(end), tail_size*sizeof(Index));
00168 
00169         std::memcpy(&matrix.data().value(start), &tmp.data().value(0), nnz*sizeof(Scalar));
00170         std::memcpy(&matrix.data().index(start), &tmp.data().index(0), nnz*sizeof(Index));
00171       }
00172       
00173       // update innerNonZeros
00174       if(!m_matrix.isCompressed())
00175         for(Index j=0; j<m_outerSize.value(); ++j)
00176           matrix.innerNonZeroPtr()[m_outerStart+j] = tmp.innerVector(j).nonZeros();
00177 
00178       // update outer index pointers
00179       Index p = start;
00180       for(Index k=0; k<m_outerSize.value(); ++k)
00181       {
00182         matrix.outerIndexPtr()[m_outerStart+k] = p;
00183         p += tmp.innerVector(k).nonZeros();
00184       }
00185       std::ptrdiff_t offset = nnz - block_size;
00186       for(Index k = m_outerStart + m_outerSize.value(); k<=matrix.outerSize(); ++k)
00187       {
00188         matrix.outerIndexPtr()[k] += offset;
00189       }
00190 
00191       return derived();
00192     }
00193 
00194     inline BlockType& operator=(const BlockType& other)
00195     {
00196       return operator=<BlockType>(other);
00197     }
00198 
00199     inline const Scalar* valuePtr() const
00200     { return m_matrix.valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
00201     inline Scalar* valuePtr()
00202     { return m_matrix.const_cast_derived().valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
00203 
00204     inline const Index* innerIndexPtr() const
00205     { return m_matrix.innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
00206     inline Index* innerIndexPtr()
00207     { return m_matrix.const_cast_derived().innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
00208 
00209     inline const Index* outerIndexPtr() const
00210     { return m_matrix.outerIndexPtr() + m_outerStart; }
00211     inline Index* outerIndexPtr()
00212     { return m_matrix.const_cast_derived().outerIndexPtr() + m_outerStart; }
00213 
00214     Index nonZeros() const
00215     {
00216       if(m_matrix.isCompressed())
00217         return  std::size_t(m_matrix.outerIndexPtr()[m_outerStart+m_outerSize.value()])
00218               - std::size_t(m_matrix.outerIndexPtr()[m_outerStart]);
00219       else if(m_outerSize.value()==0)
00220         return 0;
00221       else
00222         return Map<const Matrix<Index,OuterSize,1> >(m_matrix.innerNonZeroPtr()+m_outerStart, m_outerSize.value()).sum();
00223     }
00224 
00225     const Scalar& lastCoeff() const
00226     {
00227       EIGEN_STATIC_ASSERT_VECTOR_ONLY(BlockImpl);
00228       eigen_assert(nonZeros()>0);
00229       if(m_matrix.isCompressed())
00230         return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart+1]-1];
00231       else
00232         return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart]+m_matrix.innerNonZeroPtr()[m_outerStart]-1];
00233     }
00234 
00235     EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
00236     EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
00237 
00238   protected:
00239 
00240     typename SparseMatrixType::Nested m_matrix;
00241     Index m_outerStart;
00242     const internal::variable_if_dynamic<Index, OuterSize> m_outerSize;
00243 
00244 };
00245 
00246 //----------
00247 
00251 template<typename Derived>
00252 typename SparseMatrixBase<Derived>::InnerVectorReturnType SparseMatrixBase<Derived>::innerVector(Index outer)
00253 { return InnerVectorReturnType(derived(), outer); }
00254 
00258 template<typename Derived>
00259 const typename SparseMatrixBase<Derived>::ConstInnerVectorReturnType SparseMatrixBase<Derived>::innerVector(Index outer) const
00260 { return ConstInnerVectorReturnType(derived(), outer); }
00261 
00265 template<typename Derived>
00266 Block<Derived,Dynamic,Dynamic,true> SparseMatrixBase<Derived>::innerVectors(Index outerStart, Index outerSize)
00267 {
00268   return Block<Derived,Dynamic,Dynamic,true>(derived(),
00269                                              IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart,
00270                                              IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize);
00271   
00272 }
00273 
00277 template<typename Derived>
00278 const Block<const Derived,Dynamic,Dynamic,true> SparseMatrixBase<Derived>::innerVectors(Index outerStart, Index outerSize) const
00279 {
00280   return Block<const Derived,Dynamic,Dynamic,true>(derived(),
00281                                                   IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart,
00282                                                   IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize);
00283   
00284 }
00285 
00289 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
00290 class BlockImpl<XprType,BlockRows,BlockCols,InnerPanel,Sparse>
00291   : public SparseMatrixBase<Block<XprType,BlockRows,BlockCols,InnerPanel> >, internal::no_assignment_operator
00292 {
00293   typedef typename internal::remove_all<typename XprType::Nested>::type _MatrixTypeNested;
00294   typedef Block<XprType, BlockRows, BlockCols, InnerPanel> BlockType;
00295 public:
00296     enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
00297     EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
00298 
00299     
00301     inline BlockImpl(const XprType& xpr, int i)
00302       : m_matrix(xpr),
00303         m_startRow( (BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) ? i : 0),
00304         m_startCol( (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) ? i : 0),
00305         m_blockRows(xpr.rows()),
00306         m_blockCols(xpr.cols())
00307     {}
00308 
00311     inline BlockImpl(const XprType& xpr, int startRow, int startCol, int blockRows, int blockCols)
00312       : m_matrix(xpr), m_startRow(startRow), m_startCol(startCol), m_blockRows(blockRows), m_blockCols(blockCols)
00313     {}
00314 
00315     inline int rows() const { return m_blockRows.value(); }
00316     inline int cols() const { return m_blockCols.value(); }
00317 
00318     inline Scalar& coeffRef(int row, int col)
00319     {
00320       return m_matrix.const_cast_derived()
00321                .coeffRef(row + m_startRow.value(), col + m_startCol.value());
00322     }
00323 
00324     inline const Scalar coeff(int row, int col) const
00325     {
00326       return m_matrix.coeff(row + m_startRow.value(), col + m_startCol.value());
00327     }
00328 
00329     inline Scalar& coeffRef(int index)
00330     {
00331       return m_matrix.const_cast_derived()
00332              .coeffRef(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
00333                        m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
00334     }
00335 
00336     inline const Scalar coeff(int index) const
00337     {
00338       return m_matrix
00339              .coeff(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
00340                     m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
00341     }
00342     
00343     inline const _MatrixTypeNested& nestedExpression() const { return m_matrix; }
00344     
00345     class InnerIterator : public _MatrixTypeNested::InnerIterator
00346     {
00347       typedef typename _MatrixTypeNested::InnerIterator Base;
00348       const BlockType& m_block;
00349       Index m_end;
00350     public:
00351 
00352       EIGEN_STRONG_INLINE InnerIterator(const BlockType& block, Index outer)
00353         : Base(block.derived().nestedExpression(), outer + (IsRowMajor ? block.m_startRow.value() : block.m_startCol.value())),
00354           m_block(block),
00355           m_end(IsRowMajor ? block.m_startCol.value()+block.m_blockCols.value() : block.m_startRow.value()+block.m_blockRows.value())
00356       {
00357         while( (Base::operator bool()) && (Base::index() < (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value())) )
00358           Base::operator++();
00359       }
00360 
00361       inline Index index()  const { return Base::index() - (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value()); }
00362       inline Index outer()  const { return Base::outer() - (IsRowMajor ? m_block.m_startRow.value() : m_block.m_startCol.value()); }
00363       inline Index row()    const { return Base::row()   - m_block.m_startRow.value(); }
00364       inline Index col()    const { return Base::col()   - m_block.m_startCol.value(); }
00365       
00366       inline operator bool() const { return Base::operator bool() && Base::index() < m_end; }
00367     };
00368     class ReverseInnerIterator : public _MatrixTypeNested::ReverseInnerIterator
00369     {
00370       typedef typename _MatrixTypeNested::ReverseInnerIterator Base;
00371       const BlockType& m_block;
00372       Index m_begin;
00373     public:
00374 
00375       EIGEN_STRONG_INLINE ReverseInnerIterator(const BlockType& block, Index outer)
00376         : Base(block.derived().nestedExpression(), outer + (IsRowMajor ? block.m_startRow.value() : block.m_startCol.value())),
00377           m_block(block),
00378           m_begin(IsRowMajor ? block.m_startCol.value() : block.m_startRow.value())
00379       {
00380         while( (Base::operator bool()) && (Base::index() >= (IsRowMajor ? m_block.m_startCol.value()+block.m_blockCols.value() : m_block.m_startRow.value()+block.m_blockRows.value())) )
00381           Base::operator--();
00382       }
00383 
00384       inline Index index()  const { return Base::index() - (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value()); }
00385       inline Index outer()  const { return Base::outer() - (IsRowMajor ? m_block.m_startRow.value() : m_block.m_startCol.value()); }
00386       inline Index row()    const { return Base::row()   - m_block.m_startRow.value(); }
00387       inline Index col()    const { return Base::col()   - m_block.m_startCol.value(); }
00388       
00389       inline operator bool() const { return Base::operator bool() && Base::index() >= m_begin; }
00390     };
00391   protected:
00392     friend class InnerIterator;
00393     friend class ReverseInnerIterator;
00394 
00395     typename XprType::Nested m_matrix;
00396     const internal::variable_if_dynamic<Index, XprType::RowsAtCompileTime == 1 ? 0 : Dynamic> m_startRow;
00397     const internal::variable_if_dynamic<Index, XprType::ColsAtCompileTime == 1 ? 0 : Dynamic> m_startCol;
00398     const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_blockRows;
00399     const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_blockCols;
00400 
00401 };
00402 
00403 } // end namespace Eigen
00404 
00405 #endif // EIGEN_SPARSE_BLOCK_H


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Thu Aug 27 2015 12:00:29