10 #ifndef EIGEN_SPARSE_BLOCK_H
11 #define EIGEN_SPARSE_BLOCK_H
16 template<
typename XprType,
int BlockRows,
int BlockCols>
25 enum { OuterSize = IsRowMajor ? BlockRows : BlockCols };
32 : m_matrix(xpr), m_outerStart(
convert_index(i)), m_outerSize(OuterSize)
36 : m_matrix(xpr), m_outerStart(
convert_index(IsRowMajor ? startRow : startCol)), m_outerSize(
convert_index(IsRowMajor ? blockRows : blockCols))
45 EvaluatorType matEval(m_matrix);
47 Index end = m_outerStart + m_outerSize.value();
48 for(
Index j=m_outerStart; j<end; ++j)
49 for(
typename EvaluatorType::InnerIterator it(matEval, j); it; ++it)
56 return m_matrix.coeff(
row + (IsRowMajor ? m_outerStart : 0),
col + (IsRowMajor ? 0 : m_outerStart));
61 return m_matrix.coeff(IsRowMajor ? m_outerStart : index, IsRowMajor ? index : m_outerStart);
68 Index blockRows()
const {
return IsRowMajor ? m_outerSize.value() : m_matrix.rows(); }
69 Index blockCols()
const {
return IsRowMajor ? m_matrix.cols() : m_outerSize.value(); }
95 template<
typename SparseMatrixType,
int BlockRows,
int BlockCols>
96 class sparse_matrix_block_impl
97 :
public SparseCompressedBase<Block<SparseMatrixType,BlockRows,BlockCols,true> >
119 template<
typename OtherDerived>
132 Index nnz = tmp.nonZeros();
135 Index block_size = end - start;
139 ?
Index(
matrix.data().allocatedSize()) + block_size
142 Index tmp_start = tmp.outerIndexPtr()[0];
144 bool update_trailing_pointers =
false;
148 typename SparseMatrixType::Storage newdata(
m_matrix.data().allocatedSize() - block_size + nnz);
153 internal::smart_copy(tmp.valuePtr() + tmp_start, tmp.valuePtr() + tmp_start + nnz, newdata.valuePtr() + start);
154 internal::smart_copy(tmp.innerIndexPtr() + tmp_start, tmp.innerIndexPtr() + tmp_start + nnz, newdata.indexPtr() + start);
159 newdata.resize(
m_matrix.outerIndexPtr()[
m_matrix.outerSize()] - block_size + nnz);
161 matrix.data().swap(newdata);
163 update_trailing_pointers =
true;
170 matrix.data().resize(start + nnz + tail_size);
175 update_trailing_pointers =
true;
183 if(IsVectorAtCompileTime)
191 StorageIndex p = StorageIndex(start);
194 StorageIndex nnz_k = internal::convert_index<StorageIndex>(tmp.innerVector(k).nonZeros());
202 if(update_trailing_pointers)
204 StorageIndex offset = internal::convert_index<StorageIndex>(nnz - block_size);
207 matrix.outerIndexPtr()[k] += offset;
227 {
return m_matrix.innerIndexPtr(); }
286 template<
typename _Scalar,
int _Options,
typename _StorageIndex,
int BlockRows,
int BlockCols>
299 :
Base(xpr, startRow, startCol, blockRows, blockCols)
302 using Base::operator=;
305 template<
typename _Scalar,
int _Options,
typename _StorageIndex,
int BlockRows,
int BlockCols>
318 :
Base(xpr, startRow, startCol, blockRows, blockCols)
321 using Base::operator=;
332 template<
typename Derived>
339 template<
typename Derived>
346 template<
typename Derived>
351 IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart,
352 IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize);
359 template<
typename Derived>
364 IsRowMajor ? outerStart : 0, IsRowMajor ? 0 : outerStart,
365 IsRowMajor ? outerSize : rows(), IsRowMajor ? cols() : outerSize);
372 template<
typename XprType,
int BlockRows,
int BlockCols,
bool InnerPanel>
389 m_startRow( (BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) ?
convert_index(i) : 0),
390 m_startCol( (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) ?
convert_index(i) : 0),
391 m_blockRows(BlockRows==1 ? 1 : xpr.rows()),
392 m_blockCols(BlockCols==1 ? 1 : xpr.cols())
401 inline Index rows()
const {
return m_blockRows.value(); }
402 inline Index cols()
const {
return m_blockCols.value(); }
406 return m_matrix.coeffRef(
row + m_startRow.value(),
col + m_startCol.value());
411 return m_matrix.coeff(
row + m_startRow.value(),
col + m_startCol.value());
416 return m_matrix.coeffRef(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
417 m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
422 return m_matrix.coeff(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
423 m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
459 template<
typename ArgType,
int BlockRows,
int BlockCols,
bool InnerPanel>
461 :
public evaluator_base<Block<ArgType,BlockRows,BlockCols,InnerPanel> >
463 class InnerVectorInnerIterator;
464 class OuterVectorInnerIterator;
471 IsRowMajor = XprType::IsRowMajor,
473 OuterVector = (BlockCols==1 && ArgType::IsRowMajor)
476 (BlockRows==1 && !ArgType::IsRowMajor),
479 Flags = XprType::Flags
485 : m_argImpl(op.nestedExpression()), m_block(op)
489 Index nnz = m_block.nonZeros();
491 return m_argImpl.nonZerosEstimate() * m_block.size() / m_block.nestedExpression().size();
502 template<
typename ArgType,
int BlockRows,
int BlockCols,
bool InnerPanel>
506 enum { IsRowMajor = unary_evaluator::IsRowMajor };
512 :
EvalIterator(aEval.m_argImpl, outer + (IsRowMajor ? aEval.m_block.startRow() : aEval.m_block.startCol())),
513 m_block(aEval.m_block),
514 m_end(IsRowMajor ? aEval.m_block.startCol()+aEval.m_block.blockCols() : aEval.m_block.startRow()+aEval.m_block.blockRows())
516 while( (EvalIterator::operator
bool()) && (EvalIterator::index() < (IsRowMajor ? m_block.startCol() : m_block.startRow())) )
517 EvalIterator::operator++();
520 inline StorageIndex index()
const {
return EvalIterator::index() - convert_index<StorageIndex>(IsRowMajor ? m_block.startCol() : m_block.startRow()); }
521 inline Index outer()
const {
return EvalIterator::outer() - (IsRowMajor ? m_block.startRow() : m_block.startCol()); }
525 inline operator bool()
const {
return EvalIterator::operator bool() && EvalIterator::index() < m_end; }
528 template<
typename ArgType,
int BlockRows,
int BlockCols,
bool InnerPanel>
531 enum { IsRowMajor = unary_evaluator::IsRowMajor };
541 m_outerPos( (IsRowMajor ? aEval.m_block.startCol() : aEval.m_block.startRow()) ),
542 m_innerIndex(IsRowMajor ? aEval.m_block.startRow() : aEval.m_block.startCol()),
543 m_end(IsRowMajor ? aEval.m_block.startCol()+aEval.m_block.blockCols() : aEval.m_block.startRow()+aEval.m_block.blockRows()),
544 m_it(m_eval.m_argImpl, m_outerPos)
549 while(m_it && m_it.index() < m_innerIndex) ++m_it;
550 if((!m_it) || (m_it.index()!=m_innerIndex))
554 inline StorageIndex index()
const {
return convert_index<StorageIndex>(m_outerPos - (IsRowMajor ? m_eval.m_block.startCol() : m_eval.m_block.startRow())); }
556 inline Index row()
const {
return IsRowMajor ? 0 : index(); }
557 inline Index col()
const {
return IsRowMajor ? index() : 0; }
565 while(++m_outerPos<m_end)
568 m_it.~EvalIterator();
569 ::new (&m_it)
EvalIterator(m_eval.m_argImpl, m_outerPos);
571 while(m_it && m_it.index() < m_innerIndex) ++m_it;
572 if(m_it && m_it.index()==m_innerIndex)
break;
577 inline operator bool()
const {
return m_outerPos < m_end; }
580 template<
typename _Scalar,
int _Options,
typename _StorageIndex,
int BlockRows,
int BlockCols>
582 :
evaluator<SparseCompressedBase<Block<SparseMatrix<_Scalar, _Options, _StorageIndex>,BlockRows,BlockCols,true> > >
589 template<
typename _Scalar,
int _Options,
typename _StorageIndex,
int BlockRows,
int BlockCols>
591 :
evaluator<SparseCompressedBase<Block<const SparseMatrix<_Scalar, _Options, _StorageIndex>,BlockRows,BlockCols,true> > >
603 #endif // EIGEN_SPARSE_BLOCK_H