1c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This file is part of Eigen, a lightweight C++ template library
2c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// for linear algebra.
3c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
4c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
6c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This Source Code Form is subject to the terms of the Mozilla
7c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Public License v. 2.0. If a copy of the MPL was not distributed
8c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
10c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#ifndef EIGEN_SPARSE_BLOCK_H
11c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define EIGEN_SPARSE_BLOCK_H
12c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
13c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnamespace Eigen {
14c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
157faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate<typename XprType, int BlockRows, int BlockCols>
167faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezclass BlockImpl<XprType,BlockRows,BlockCols,true,Sparse>
177faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  : public SparseMatrixBase<Block<XprType,BlockRows,BlockCols,true> >
18c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
197faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    typedef typename internal::remove_all<typename XprType::Nested>::type _MatrixTypeNested;
207faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    typedef Block<XprType, BlockRows, BlockCols, true> BlockType;
217faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezpublic:
227faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
237faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezprotected:
247faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
257faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezpublic:
267faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
277faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
287faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    class InnerIterator: public XprType::InnerIterator
29c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
307faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        typedef typename BlockImpl::Index Index;
31c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      public:
327faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        inline InnerIterator(const BlockType& xpr, Index outer)
337faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez          : XprType::InnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
34c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        {}
35c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
36c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
37c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      protected:
38c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        Index m_outer;
39c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    };
407faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    class ReverseInnerIterator: public XprType::ReverseInnerIterator
41c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
427faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        typedef typename BlockImpl::Index Index;
43c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      public:
447faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        inline ReverseInnerIterator(const BlockType& xpr, Index outer)
457faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez          : XprType::ReverseInnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
46c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        {}
47c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
48c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
49c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      protected:
50c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        Index m_outer;
51c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    };
52c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
537faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    inline BlockImpl(const XprType& xpr, int i)
547faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      : m_matrix(xpr), m_outerStart(i), m_outerSize(OuterSize)
557faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    {}
56c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
577faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    inline BlockImpl(const XprType& xpr, int startRow, int startCol, int blockRows, int blockCols)
587faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      : m_matrix(xpr), m_outerStart(IsRowMajor ? startRow : startCol), m_outerSize(IsRowMajor ? blockRows : blockCols)
597faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    {}
60c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
61c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
62c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
63c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
64c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  protected:
65c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
667faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    typename XprType::Nested m_matrix;
67c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Index m_outerStart;
687faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    const internal::variable_if_dynamic<Index, OuterSize> m_outerSize;
697faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
707faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl)
71c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
72c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
73c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
74c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/***************************************************************************
75c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath* specialisation for SparseMatrix
76c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath***************************************************************************/
77c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
787faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate<typename _Scalar, int _Options, typename _Index, int BlockRows, int BlockCols>
797faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezclass BlockImpl<SparseMatrix<_Scalar, _Options, _Index>,BlockRows,BlockCols,true,Sparse>
807faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  : public SparseMatrixBase<Block<SparseMatrix<_Scalar, _Options, _Index>,BlockRows,BlockCols,true> >
81c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
827faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    typedef SparseMatrix<_Scalar, _Options, _Index> SparseMatrixType;
837faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    typedef typename internal::remove_all<typename SparseMatrixType::Nested>::type _MatrixTypeNested;
847faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    typedef Block<SparseMatrixType, BlockRows, BlockCols, true> BlockType;
857faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezpublic:
867faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
877faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
887faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezprotected:
897faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
907faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezpublic:
917faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
927faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    class InnerIterator: public SparseMatrixType::InnerIterator
93c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
94c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      public:
957faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        inline InnerIterator(const BlockType& xpr, Index outer)
967faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez          : SparseMatrixType::InnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
97c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        {}
98c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
99c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
100c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      protected:
101c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        Index m_outer;
102c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    };
1037faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    class ReverseInnerIterator: public SparseMatrixType::ReverseInnerIterator
104c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
105c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      public:
1067faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        inline ReverseInnerIterator(const BlockType& xpr, Index outer)
1077faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez          : SparseMatrixType::ReverseInnerIterator(xpr.m_matrix, xpr.m_outerStart + outer), m_outer(outer)
108c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        {}
109c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        inline Index row() const { return IsRowMajor ? m_outer : this->index(); }
110c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        inline Index col() const { return IsRowMajor ? this->index() : m_outer; }
111c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      protected:
112c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        Index m_outer;
113c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    };
114c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
1157faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    inline BlockImpl(const SparseMatrixType& xpr, int i)
1167faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      : m_matrix(xpr), m_outerStart(i), m_outerSize(OuterSize)
1177faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    {}
118c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
1197faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    inline BlockImpl(const SparseMatrixType& xpr, int startRow, int startCol, int blockRows, int blockCols)
1207faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      : m_matrix(xpr), m_outerStart(IsRowMajor ? startRow : startCol), m_outerSize(IsRowMajor ? blockRows : blockCols)
1217faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    {}
122c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
123c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename OtherDerived>
1247faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    inline BlockType& operator=(const SparseMatrixBase<OtherDerived>& other)
125c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
1267faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      typedef typename internal::remove_all<typename SparseMatrixType::Nested>::type _NestedMatrixType;
127c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      _NestedMatrixType& matrix = const_cast<_NestedMatrixType&>(m_matrix);;
128c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      // This assignement is slow if this vector set is not empty
129c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      // and/or it is not at the end of the nonzeros of the underlying matrix.
130c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
131c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      // 1 - eval to a temporary to avoid transposition and/or aliasing issues
132c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      SparseMatrix<Scalar, IsRowMajor ? RowMajor : ColMajor, Index> tmp(other);
133c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
134c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      // 2 - let's check whether there is enough allocated memory
135c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      Index nnz           = tmp.nonZeros();
1367faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      Index start         = m_outerStart==0 ? 0 : matrix.outerIndexPtr()[m_outerStart]; // starting position of the current block
1377faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      Index end           = m_matrix.outerIndexPtr()[m_outerStart+m_outerSize.value()]; // ending posiiton of the current block
1387faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      Index block_size    = end - start;                                                // available room in the current block
1397faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      Index tail_size     = m_matrix.outerIndexPtr()[m_matrix.outerSize()] - end;
1407faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
1417faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      Index free_size     = m_matrix.isCompressed()
1427faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez                          ? Index(matrix.data().allocatedSize()) + block_size
1437faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez                          : block_size;
1447faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
1457faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      if(nnz>free_size)
146c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      {
147c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        // realloc manually to reduce copies
1487faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        typename SparseMatrixType::Storage newdata(m_matrix.data().allocatedSize() - block_size + nnz);
149c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
1507faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        std::memcpy(&newdata.value(0), &m_matrix.data().value(0), start*sizeof(Scalar));
1517faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        std::memcpy(&newdata.index(0), &m_matrix.data().index(0), start*sizeof(Index));
152c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
1537faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        std::memcpy(&newdata.value(start), &tmp.data().value(0), nnz*sizeof(Scalar));
1547faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        std::memcpy(&newdata.index(start), &tmp.data().index(0), nnz*sizeof(Index));
155c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
1567faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        std::memcpy(&newdata.value(start+nnz), &matrix.data().value(end), tail_size*sizeof(Scalar));
1577faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        std::memcpy(&newdata.index(start+nnz), &matrix.data().index(end), tail_size*sizeof(Index));
1587faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
1597faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        newdata.resize(m_matrix.outerIndexPtr()[m_matrix.outerSize()] - block_size + nnz);
160c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
161c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        matrix.data().swap(newdata);
162c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      }
163c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      else
164c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      {
165c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        // no need to realloc, simply copy the tail at its respective position and insert tmp
1667faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        matrix.data().resize(start + nnz + tail_size);
1677faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
1687faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        std::memmove(&matrix.data().value(start+nnz), &matrix.data().value(end), tail_size*sizeof(Scalar));
1697faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        std::memmove(&matrix.data().index(start+nnz), &matrix.data().index(end), tail_size*sizeof(Index));
1707faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
1717faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        std::memcpy(&matrix.data().value(start), &tmp.data().value(0), nnz*sizeof(Scalar));
1727faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        std::memcpy(&matrix.data().index(start), &tmp.data().index(0), nnz*sizeof(Index));
173c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      }
1747faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
1757faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      // update innerNonZeros
1767faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      if(!m_matrix.isCompressed())
1777faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        for(Index j=0; j<m_outerSize.value(); ++j)
1787faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez          matrix.innerNonZeroPtr()[m_outerStart+j] = tmp.innerVector(j).nonZeros();
179c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
180c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      // update outer index pointers
1817faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      Index p = start;
182c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      for(Index k=0; k<m_outerSize.value(); ++k)
183c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      {
184c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        matrix.outerIndexPtr()[m_outerStart+k] = p;
185c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        p += tmp.innerVector(k).nonZeros();
186c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      }
1877faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      std::ptrdiff_t offset = nnz - block_size;
188c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      for(Index k = m_outerStart + m_outerSize.value(); k<=matrix.outerSize(); ++k)
189c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      {
190c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        matrix.outerIndexPtr()[k] += offset;
191c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      }
192c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
1937faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      return derived();
194c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
195c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
1967faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    inline BlockType& operator=(const BlockType& other)
197c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
1987faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      return operator=<BlockType>(other);
199c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
200c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
201c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    inline const Scalar* valuePtr() const
202c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return m_matrix.valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
203c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    inline Scalar* valuePtr()
204c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return m_matrix.const_cast_derived().valuePtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
205c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
206c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    inline const Index* innerIndexPtr() const
207c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return m_matrix.innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
208c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    inline Index* innerIndexPtr()
209c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return m_matrix.const_cast_derived().innerIndexPtr() + m_matrix.outerIndexPtr()[m_outerStart]; }
210c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
211c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    inline const Index* outerIndexPtr() const
212c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return m_matrix.outerIndexPtr() + m_outerStart; }
213c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    inline Index* outerIndexPtr()
214c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return m_matrix.const_cast_derived().outerIndexPtr() + m_outerStart; }
215c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
216c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Index nonZeros() const
217c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
218c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      if(m_matrix.isCompressed())
219c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        return  std::size_t(m_matrix.outerIndexPtr()[m_outerStart+m_outerSize.value()])
220c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath              - std::size_t(m_matrix.outerIndexPtr()[m_outerStart]);
221c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      else if(m_outerSize.value()==0)
222c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        return 0;
223c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      else
2247faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        return Map<const Matrix<Index,OuterSize,1> >(m_matrix.innerNonZeroPtr()+m_outerStart, m_outerSize.value()).sum();
225c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
226c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
227c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    const Scalar& lastCoeff() const
228c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
2297faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      EIGEN_STATIC_ASSERT_VECTOR_ONLY(BlockImpl);
230c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      eigen_assert(nonZeros()>0);
231c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      if(m_matrix.isCompressed())
232c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart+1]-1];
233c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      else
234c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        return m_matrix.valuePtr()[m_matrix.outerIndexPtr()[m_outerStart]+m_matrix.innerNonZeroPtr()[m_outerStart]-1];
235c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
236c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
237c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE Index rows() const { return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
238c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE Index cols() const { return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
239c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
240c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  protected:
241c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
2427faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    typename SparseMatrixType::Nested m_matrix;
243c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Index m_outerStart;
2447faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    const internal::variable_if_dynamic<Index, OuterSize> m_outerSize;
245c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
246c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
247c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
248c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//----------
249c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
250c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/** \returns the \a outer -th column (resp. row) of the matrix \c *this if \c *this
251c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * is col-major (resp. row-major).
252c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  */
253c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Derived>
2547faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztypename SparseMatrixBase<Derived>::InnerVectorReturnType SparseMatrixBase<Derived>::innerVector(Index outer)
2557faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez{ return InnerVectorReturnType(derived(), outer); }
256c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
257c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/** \returns the \a outer -th column (resp. row) of the matrix \c *this if \c *this
258c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * is col-major (resp. row-major). Read-only.
259c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  */
260c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Derived>
2617faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezconst typename SparseMatrixBase<Derived>::ConstInnerVectorReturnType SparseMatrixBase<Derived>::innerVector(Index outer) const
2627faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez{ return ConstInnerVectorReturnType(derived(), outer); }
263c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
2647faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez/** \returns the \a outer -th column (resp. row) of the matrix \c *this if \c *this
2657faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  * is col-major (resp. row-major).
2667faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  */
267c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Derived>
2687faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezBlock<Derived,Dynamic,Dynamic,true> SparseMatrixBase<Derived>::innerVectors(Index outerStart, Index outerSize)
269c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
2707faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  return Block<Derived,Dynamic,Dynamic,true>(derived(),
2717faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez                                             IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart,
2727faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez                                             IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize);
2737faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
274c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
275c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
2767faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez/** \returns the \a outer -th column (resp. row) of the matrix \c *this if \c *this
2777faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  * is col-major (resp. row-major). Read-only.
2787faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  */
279c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Derived>
2807faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezconst Block<const Derived,Dynamic,Dynamic,true> SparseMatrixBase<Derived>::innerVectors(Index outerStart, Index outerSize) const
281c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
2827faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  return Block<const Derived,Dynamic,Dynamic,true>(derived(),
2837faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez                                                  IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart,
2847faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez                                                  IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize);
2857faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
286c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
287c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
2887faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez/** Generic implementation of sparse Block expression.
2897faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  * Real-only.
2907faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  */
2917faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate<typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
2927faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezclass BlockImpl<XprType,BlockRows,BlockCols,InnerPanel,Sparse>
2937faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  : public SparseMatrixBase<Block<XprType,BlockRows,BlockCols,InnerPanel> >, internal::no_assignment_operator
294c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
2957faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef typename internal::remove_all<typename XprType::Nested>::type _MatrixTypeNested;
2967faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  typedef Block<XprType, BlockRows, BlockCols, InnerPanel> BlockType;
2977faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezpublic:
2987faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    enum { IsRowMajor = internal::traits<BlockType>::IsRowMajor };
2997faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_SPARSE_PUBLIC_INTERFACE(BlockType)
3007faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
3017faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    /** Column or Row constructor
3027faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      */
3037faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    inline BlockImpl(const XprType& xpr, int i)
3047faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      : m_matrix(xpr),
3057faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        m_startRow( (BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) ? i : 0),
3067faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        m_startCol( (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) ? i : 0),
3077faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        m_blockRows(xpr.rows()),
3087faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        m_blockCols(xpr.cols())
3097faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    {}
3107faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
3117faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    /** Dynamic-size constructor
3127faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      */
3137faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    inline BlockImpl(const XprType& xpr, int startRow, int startCol, int blockRows, int blockCols)
3147faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      : m_matrix(xpr), m_startRow(startRow), m_startCol(startCol), m_blockRows(blockRows), m_blockCols(blockCols)
3157faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    {}
3167faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
3177faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    inline int rows() const { return m_blockRows.value(); }
3187faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    inline int cols() const { return m_blockCols.value(); }
3197faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
3207faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    inline Scalar& coeffRef(int row, int col)
3217faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    {
3227faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      return m_matrix.const_cast_derived()
3237faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez               .coeffRef(row + m_startRow.value(), col + m_startCol.value());
3247faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    }
325c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
3267faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    inline const Scalar coeff(int row, int col) const
3277faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    {
3287faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      return m_matrix.coeff(row + m_startRow.value(), col + m_startCol.value());
3297faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    }
330c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
3317faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    inline Scalar& coeffRef(int index)
3327faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    {
3337faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      return m_matrix.const_cast_derived()
3347faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez             .coeffRef(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
3357faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez                       m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
3367faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    }
337c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
3387faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    inline const Scalar coeff(int index) const
3397faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    {
3407faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      return m_matrix
3417faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez             .coeff(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
3427faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez                    m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
3437faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    }
3447faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
3457faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    inline const _MatrixTypeNested& nestedExpression() const { return m_matrix; }
3467faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
3477faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    class InnerIterator : public _MatrixTypeNested::InnerIterator
3487faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    {
3497faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      typedef typename _MatrixTypeNested::InnerIterator Base;
3507faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      const BlockType& m_block;
3517faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      Index m_end;
3527faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    public:
3537faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
3547faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      EIGEN_STRONG_INLINE InnerIterator(const BlockType& block, Index outer)
3557faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        : Base(block.derived().nestedExpression(), outer + (IsRowMajor ? block.m_startRow.value() : block.m_startCol.value())),
3567faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez          m_block(block),
3577faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez          m_end(IsRowMajor ? block.m_startCol.value()+block.m_blockCols.value() : block.m_startRow.value()+block.m_blockRows.value())
3587faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      {
3597faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        while( (Base::operator bool()) && (Base::index() < (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value())) )
3607faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez          Base::operator++();
3617faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      }
362c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
3637faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      inline Index index()  const { return Base::index() - (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value()); }
3647faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      inline Index outer()  const { return Base::outer() - (IsRowMajor ? m_block.m_startRow.value() : m_block.m_startCol.value()); }
3657faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      inline Index row()    const { return Base::row()   - m_block.m_startRow.value(); }
3667faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      inline Index col()    const { return Base::col()   - m_block.m_startCol.value(); }
3677faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
3687faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      inline operator bool() const { return Base::operator bool() && Base::index() < m_end; }
3697faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    };
3707faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    class ReverseInnerIterator : public _MatrixTypeNested::ReverseInnerIterator
3717faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    {
3727faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      typedef typename _MatrixTypeNested::ReverseInnerIterator Base;
3737faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      const BlockType& m_block;
3747faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      Index m_begin;
3757faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    public:
3767faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
3777faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      EIGEN_STRONG_INLINE ReverseInnerIterator(const BlockType& block, Index outer)
3787faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        : Base(block.derived().nestedExpression(), outer + (IsRowMajor ? block.m_startRow.value() : block.m_startCol.value())),
3797faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez          m_block(block),
3807faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez          m_begin(IsRowMajor ? block.m_startCol.value() : block.m_startRow.value())
3817faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      {
3827faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        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())) )
3837faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez          Base::operator--();
3847faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      }
385c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
3867faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      inline Index index()  const { return Base::index() - (IsRowMajor ? m_block.m_startCol.value() : m_block.m_startRow.value()); }
3877faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      inline Index outer()  const { return Base::outer() - (IsRowMajor ? m_block.m_startRow.value() : m_block.m_startCol.value()); }
3887faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      inline Index row()    const { return Base::row()   - m_block.m_startRow.value(); }
3897faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      inline Index col()    const { return Base::col()   - m_block.m_startCol.value(); }
3907faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
3917faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      inline operator bool() const { return Base::operator bool() && Base::index() >= m_begin; }
3927faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    };
3937faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  protected:
3947faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    friend class InnerIterator;
3957faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    friend class ReverseInnerIterator;
3967faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
3977faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl)
3987faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
3997faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    typename XprType::Nested m_matrix;
4007faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    const internal::variable_if_dynamic<Index, XprType::RowsAtCompileTime == 1 ? 0 : Dynamic> m_startRow;
4017faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    const internal::variable_if_dynamic<Index, XprType::ColsAtCompileTime == 1 ? 0 : Dynamic> m_startCol;
4027faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_blockRows;
4037faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_blockCols;
4047faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
4057faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez};
406c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
407c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath} // end namespace Eigen
408c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
409c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#endif // EIGEN_SPARSE_BLOCK_H
410