11 #ifndef EIGEN_SPARSEBLOCKMATRIX_H
12 #define EIGEN_SPARSEBLOCKMATRIX_H
57 template <
typename Scalar_,
int _BlockAtCompileTime = Dynamic,
int Options_ = ColMajor,
typename StorageIndex_ =
int>
58 class BlockSparseMatrix;
60 template <
typename BlockSparseMatrixT>
61 class BlockSparseMatrixView;
64 template <
typename Scalar_,
int _BlockAtCompileTime,
int Options_,
typename Index_>
75 BlockSize = _BlockAtCompileTime,
81 template <
typename BlockSparseMatrixT>
92 template <
typename Iterator,
bool IsColMajor>
94 typedef typename Iterator::value_type
Triplet;
97 return ((
a.col() ==
b.col() &&
a.row() <
b.row()) || (
a.col() <
b.col()));
99 return ((
a.row() ==
b.row() &&
a.col() <
b.col()) || (
a.row() <
b.row()));
105 template <
typename BlockSparseMatrixT>
113 Flags = BlockSparseMatrixT::Options,
141 template <
typename BlockSparseMatrixT,
typename VectorType>
148 Flags = VectorType::Flags
162 return m_vec.middleRows(startRow, rowSize);
167 return m_vec.block(startRow,
j, rowSize, 1);
175 template <
typename VectorType,
typename Index>
179 template <
typename BlockSparseMatrixT,
typename VectorType>
185 Flags = VectorType::Flags
196 return m_vec.middleRows(startRow, rowSize);
201 return m_vec.block(startRow,
j, rowSize, 1);
210 template <
typename Lhs,
typename Rhs>
215 template <
typename BlockSparseMatrixT,
typename VecType>
232 template <
typename Lhs,
typename Rhs>
239 template <
typename Dest>
250 template <
typename Scalar_,
int _BlockAtCompileTime,
int Options_,
typename StorageIndex_>
252 :
public SparseMatrixBase<BlockSparseMatrix<Scalar_, _BlockAtCompileTime, Options_, StorageIndex_> > {
332 std::swap(first.m_innerBSize, second.m_innerBSize);
333 std::swap(first.m_outerBSize, second.m_outerBSize);
334 std::swap(first.m_innerOffset, second.m_innerOffset);
335 std::swap(first.m_outerOffset, second.m_outerOffset);
336 std::swap(first.m_nonzerosblocks, second.m_nonzerosblocks);
337 std::swap(first.m_nonzeros, second.m_nonzeros);
338 std::swap(first.m_values, second.m_values);
339 std::swap(first.m_blockPtr, second.m_blockPtr);
340 std::swap(first.m_indices, second.m_indices);
341 std::swap(first.m_outerIndex, second.m_outerIndex);
342 std::swap(first.m_BlockSize, second.m_blockSize);
365 template <
typename MatrixType>
380 template <
typename MatrixType>
383 "Trying to assign to a zero-size matrix, call resize() first");
393 blockPattern.startVec(bj);
395 typename MatrixType::InnerIterator it_spmat(spmat,
j);
396 for (; it_spmat; ++it_spmat) {
398 if (!nzblocksFlag[bi]) {
400 nzblocksFlag[bi] =
true;
401 blockPattern.insertBackByOuterInnerUnordered(bj, bi) =
true;
408 blockPattern.finalize();
418 typename MatrixType::InnerIterator it_spmat(spmat,
j);
419 for (; it_spmat; ++it_spmat) {
437 m_values[idxVal] = it_spmat.value();
462 template <
typename MatrixType>
464 resize(blockPattern.rows(), blockPattern.cols());
465 reserve(blockPattern.nonZeros());
476 std::vector<int> nzBlockIdx;
477 typename MatrixType::InnerIterator it(blockPattern, bj);
479 nzBlockIdx.push_back(it.index());
481 std::sort(nzBlockIdx.begin(), nzBlockIdx.end());
484 for (
StorageIndex idx = 0; idx < nzBlockIdx.size(); ++idx) {
520 inline void setBlockLayout(
const VectorXi& rowBlocks,
const VectorXi& colBlocks) {
552 "TRYING TO RESERVE ZERO-SIZE MATRICES, CALL resize() first");
579 template <
typename InputIterator>
588 std::sort(begin,
end, tripletcomp);
599 nzblock_outer.setZero();
601 for (InputIterator it(begin); it !=
end; ++it) {
602 eigen_assert(it->row() >= 0 && it->row() < this->blockRows() && it->col() >= 0 && it->col() < this->blockCols());
607 eigen_assert((rowBlocks[it->row()] == 0 || rowBlocks[it->row()] == it->value().rows()) &&
608 "NON CORRESPONDING SIZES FOR ROW BLOCKS");
609 eigen_assert((colBlocks[it->col()] == 0 || colBlocks[it->col()] == it->value().cols()) &&
610 "NON CORRESPONDING SIZES FOR COLUMN BLOCKS");
611 rowBlocks[it->row()] = it->value().rows();
612 colBlocks[it->col()] = it->value().cols();
614 nz_outer(
IsColMajor ? it->col() : it->row()) += it->value().rows() * it->value().cols();
615 nzblock_outer(
IsColMajor ? it->col() : it->row())++;
637 for (InputIterator it(begin); it !=
end; ++it) {
641 StorageIndex block_size = it->value().rows() * it->value().cols();
643 memcpy(&(
m_values[nz_marker]), it->value().data(), block_size *
sizeof(
Scalar));
777 template <
typename VecType>
829 class BlockInnerIterator;
835 s <<
"(" << itb.
row() <<
", " << itb.
col() <<
")\n";
882 template <
typename Scalar_,
int _BlockAtCompileTime,
int Options_,
typename StorageIndex_>
910 return (m_mat.m_blockSize ==
Dynamic) ? (m_mat.m_innerOffset[index() + 1] - m_mat.m_innerOffset[index()])
915 return (m_mat.m_blockSize ==
Dynamic) ? (m_mat.m_outerOffset[m_outer + 1] - m_mat.m_outerOffset[m_outer])
918 inline operator bool()
const {
return (m_id < m_end); }
927 template <
typename Scalar_,
int _BlockAtCompileTime,
int Options_,
typename StorageIndex_>
937 m_id = m_mat.blockInnerIndex(itb.index());
939 m_end = m_mat.blockInnerIndex(itb.index() + 1);
947 m_id = m_mat.blockInnerIndex(itb.index());
949 m_end = m_mat.blockInnerIndex(itb.index() + 1);
954 inline const Scalar&
value()
const {
return itb.value().coeff(m_id - m_start, m_offset); }
955 inline Scalar&
valueRef() {
return itb.valueRef().coeff(m_id - m_start, m_offset); }
960 inline operator bool()
const {
return itb; }
#define eigen_assert(x)
Definition: Macros.h:910
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
Scalar * b
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
Definition: BlockSparseMatrix.h:130
InnerIterator(const BlockSparseMatrixView &mat, Index outer)
Definition: BlockSparseMatrix.h:132
Definition: BlockSparseMatrix.h:106
Index outerSize() const
Definition: BlockSparseMatrix.h:124
BlockSparseMatrixView(const BlockSparseMatrixT &spblockmat)
Definition: BlockSparseMatrix.h:122
BlockSparseMatrixT Nested
Definition: BlockSparseMatrix.h:111
Ref< typename BlockSparseMatrixT::BlockScalar > Scalar
Definition: BlockSparseMatrix.h:108
Scalar coeff(Index row, Index col)
Definition: BlockSparseMatrix.h:127
BlockSparseMatrixT::Index Index
Definition: BlockSparseMatrix.h:110
Index rows() const
Definition: BlockSparseMatrix.h:126
Scalar coeffRef(Index row, Index col)
Definition: BlockSparseMatrix.h:128
const BlockSparseMatrixT & m_spblockmat
Definition: BlockSparseMatrix.h:137
@ MaxColsAtCompileTime
Definition: BlockSparseMatrix.h:117
@ RowsAtCompileTime
Definition: BlockSparseMatrix.h:115
@ MaxRowsAtCompileTime
Definition: BlockSparseMatrix.h:118
@ ColsAtCompileTime
Definition: BlockSparseMatrix.h:116
@ Options
Definition: BlockSparseMatrix.h:114
Index cols() const
Definition: BlockSparseMatrix.h:125
Ref< typename BlockSparseMatrixT::BlockRealScalar > RealScalar
Definition: BlockSparseMatrix.h:109
Definition: BlockSparseMatrix.h:883
Index rows() const
Definition: BlockSparseMatrix.h:909
Index outer() const
Definition: BlockSparseMatrix.h:903
const BlockSparseMatrix< Scalar_, _BlockAtCompileTime, Options_, StorageIndex > & m_mat
Definition: BlockSparseMatrix.h:921
const Map< const BlockScalar > value() const
Definition: BlockSparseMatrix.h:895
Index cols() const
Definition: BlockSparseMatrix.h:914
Index index() const
Definition: BlockSparseMatrix.h:902
Index row() const
Definition: BlockSparseMatrix.h:905
Index col() const
Definition: BlockSparseMatrix.h:907
BlockInnerIterator(const BlockSparseMatrix &mat, const Index outer)
Definition: BlockSparseMatrix.h:887
Index m_id
Definition: BlockSparseMatrix.h:923
BlockInnerIterator & operator++()
Definition: BlockSparseMatrix.h:890
const Index m_outer
Definition: BlockSparseMatrix.h:922
Map< BlockScalar > valueRef()
Definition: BlockSparseMatrix.h:898
Index m_end
Definition: BlockSparseMatrix.h:924
Definition: BlockSparseMatrix.h:928
const Index m_offset
Definition: BlockSparseMatrix.h:967
InnerIterator(const BlockSparseMatrix &mat, Index outer)
Definition: BlockSparseMatrix.h:930
const Scalar & value() const
Definition: BlockSparseMatrix.h:954
Index m_id
Definition: BlockSparseMatrix.h:969
Index col() const
Definition: BlockSparseMatrix.h:958
Index index() const
Definition: BlockSparseMatrix.h:956
Index outer() const
Definition: BlockSparseMatrix.h:957
Index m_end
Definition: BlockSparseMatrix.h:970
Index row() const
Definition: BlockSparseMatrix.h:959
BlockInnerIterator itb
Definition: BlockSparseMatrix.h:966
InnerIterator & operator++()
Definition: BlockSparseMatrix.h:942
const Index m_outer
Definition: BlockSparseMatrix.h:964
Index m_start
Definition: BlockSparseMatrix.h:968
Scalar & valueRef()
Definition: BlockSparseMatrix.h:955
const Index m_outerB
Definition: BlockSparseMatrix.h:965
const BlockSparseMatrix & m_mat
Definition: BlockSparseMatrix.h:963
A versatile sparse matrix representation where each element is a block.
Definition: BlockSparseMatrix.h:252
BlockSparseMatrix & operator=(const MatrixType &spmat)
Assignment from a sparse matrix with the same storage order.
Definition: BlockSparseMatrix.h:381
Index innerToBlock(Index inner) const
Definition: BlockSparseMatrix.h:725
void setBlockStructure(const MatrixType &blockPattern)
Set the nonzero block pattern of the matrix.
Definition: BlockSparseMatrix.h:463
const StorageIndex * outerIndexPtr() const
Definition: BlockSparseMatrix.h:792
Index blockInnerIndex(Index bi) const
Definition: BlockSparseMatrix.h:809
Index blockCols() const
Definition: BlockSparseMatrix.h:709
friend std::ostream & operator<<(std::ostream &s, const BlockSparseMatrix &m)
Definition: BlockSparseMatrix.h:831
BlockSparseMatrix(Index brow, Index bcol)
Construct and resize.
Definition: BlockSparseMatrix.h:296
BlockSparseMatrix & operator=(BlockSparseMatrix other)
Definition: BlockSparseMatrix.h:345
Index outerSize() const
Definition: BlockSparseMatrix.h:700
Matrix< Scalar, _BlockAtCompileTime, _BlockAtCompileTime, IsColMajor ? ColMajor :RowMajor > BlockScalar
Definition: BlockSparseMatrix.h:272
Index blockPtr(Index id) const
Definition: BlockSparseMatrix.h:846
Index m_blockSize
Definition: BlockSparseMatrix.h:879
StorageIndex * m_indices
Definition: BlockSparseMatrix.h:877
Index m_innerBSize
Definition: BlockSparseMatrix.h:868
internal::ref_selector< BlockSparseMatrix< Scalar_, _BlockAtCompileTime, Options_, StorageIndex_ > >::type Nested
Definition: BlockSparseMatrix.h:259
StorageIndex * innerIndexPtr()
Definition: BlockSparseMatrix.h:789
Scalar * m_values
Definition: BlockSparseMatrix.h:874
Index innerBlocks() const
Definition: BlockSparseMatrix.h:712
Matrix< RealScalar, _BlockAtCompileTime, _BlockAtCompileTime, IsColMajor ? ColMajor :RowMajor > BlockRealScalar
Definition: BlockSparseMatrix.h:274
void reserve(const Index nonzerosblocks)
Allocate the internal array of pointers to blocks and their inner indices.
Definition: BlockSparseMatrix.h:550
BlockSparseMatrix()
Definition: BlockSparseMatrix.h:280
Index outerToBlock(Index outer) const
Definition: BlockSparseMatrix.h:715
Map< const BlockScalar > coeff(Index brow, Index bcol) const
Definition: BlockSparseMatrix.h:759
Index blockOuterIndex(Index bj) const
Definition: BlockSparseMatrix.h:806
Index blockRowsIndex(Index bi) const
Definition: BlockSparseMatrix.h:799
Index cols() const
Definition: BlockSparseMatrix.h:688
StorageIndex * m_outerOffset
Definition: BlockSparseMatrix.h:871
BlockSparseMatrix(const MatrixType &spmat)
Constructor from a sparse matrix.
Definition: BlockSparseMatrix.h:366
Index rows() const
Definition: BlockSparseMatrix.h:680
StorageIndex_ StorageIndex
Definition: BlockSparseMatrix.h:256
Index innerSize() const
Definition: BlockSparseMatrix.h:693
void setFromTriplets(const InputIterator &begin, const InputIterator &end)
Fill values in a matrix from a triplet list.
Definition: BlockSparseMatrix.h:580
void setBlockLayout(const VectorXi &rowBlocks, const VectorXi &colBlocks)
Set the row and column block layouts,.
Definition: BlockSparseMatrix.h:520
Ref< BlockScalar > coeffRef(Index brow, Index bcol)
Definition: BlockSparseMatrix.h:738
NumTraits< Scalar >::Real RealScalar
Definition: BlockSparseMatrix.h:255
StorageIndex * m_blockPtr
Definition: BlockSparseMatrix.h:875
Index m_nonzerosblocks
Definition: BlockSparseMatrix.h:872
Index nonZeros() const
Definition: BlockSparseMatrix.h:785
@ IsColMajor
Definition: BlockSparseMatrix.h:270
@ MaxColsAtCompileTime
Definition: BlockSparseMatrix.h:268
@ RowsAtCompileTime
Definition: BlockSparseMatrix.h:265
@ IsVectorAtCompileTime
Definition: BlockSparseMatrix.h:269
@ BlockSize
Definition: BlockSparseMatrix.h:264
@ ColsAtCompileTime
Definition: BlockSparseMatrix.h:266
@ MaxRowsAtCompileTime
Definition: BlockSparseMatrix.h:267
@ Options
Definition: BlockSparseMatrix.h:262
StorageIndex * m_innerOffset
Definition: BlockSparseMatrix.h:870
Index blockInnerSize(Index bi) const
Definition: BlockSparseMatrix.h:814
BlockSparseMatrix(const BlockSparseMatrix &other)
Copy-constructor.
Definition: BlockSparseMatrix.h:311
StorageIndex * outerIndexPtr()
Definition: BlockSparseMatrix.h:791
StorageIndex * m_outerIndex
Definition: BlockSparseMatrix.h:878
Index blockRows() const
Definition: BlockSparseMatrix.h:707
Index blockOuterSize(Index bj) const
Definition: BlockSparseMatrix.h:817
void resize(Index brow, Index bcol)
Set the number of rows and columns blocks.
Definition: BlockSparseMatrix.h:499
~BlockSparseMatrix()
Definition: BlockSparseMatrix.h:352
const StorageIndex * innerIndexPtr() const
Definition: BlockSparseMatrix.h:790
Scalar_ Scalar
Definition: BlockSparseMatrix.h:254
friend void swap(BlockSparseMatrix &first, BlockSparseMatrix &second)
Definition: BlockSparseMatrix.h:331
std::conditional_t< _BlockAtCompileTime==Dynamic, Scalar, BlockScalar > BlockScalarReturnType
Definition: BlockSparseMatrix.h:275
Index outerBlocks() const
Definition: BlockSparseMatrix.h:711
BlockSparseMatrix< Scalar, BlockSize, IsColMajor ? ColMajor :RowMajor, StorageIndex > PlainObject
Definition: BlockSparseMatrix.h:276
Index m_nonzeros
Definition: BlockSparseMatrix.h:873
bool isCompressed() const
for compatibility purposes with the SparseMatrix class
Definition: BlockSparseMatrix.h:795
BlockSparseTimeDenseProduct< BlockSparseMatrix, VecType > operator*(const VecType &lhs) const
Definition: BlockSparseMatrix.h:778
BlockScalarReturnType * valuePtr()
Definition: BlockSparseMatrix.h:787
Index blockColsIndex(Index bj) const
Definition: BlockSparseMatrix.h:804
Index nonZerosBlocks() const
Definition: BlockSparseMatrix.h:783
void setBlockSize(Index blockSize)
set the block size at runtime for fixed-size block layout
Definition: BlockSparseMatrix.h:509
Index m_outerBSize
Definition: BlockSparseMatrix.h:869
Map< BlockScalar > insert(Index brow, Index bcol)
Definition: BlockSparseMatrix.h:233
BlockSparseTimeDenseProduct & operator=(const BlockSparseTimeDenseProduct &)
void scaleAndAddTo(Dest &dest, const typename Rhs::Scalar &alpha) const
Definition: BlockSparseMatrix.h:240
Definition: BlockSparseMatrix.h:180
Ref< Matrix< typename VectorType::Scalar, RowsAtCompileTime, ColsAtCompileTime > > Scalar
Definition: BlockSparseMatrix.h:187
Scalar coeffRef(Index bi)
Definition: BlockSparseMatrix.h:193
BlockSparseMatrixT::Index Index
Definition: BlockSparseMatrix.h:188
BlockVectorReturn(const BlockSparseMatrixT &spblockmat, VectorType &vec)
Definition: BlockSparseMatrix.h:191
@ ColsAtCompileTime
Definition: BlockSparseMatrix.h:183
@ RowsAtCompileTime
Definition: BlockSparseMatrix.h:184
const BlockSparseMatrixT & m_spblockmat
Definition: BlockSparseMatrix.h:205
VectorType & m_vec
Definition: BlockSparseMatrix.h:206
Index size() const
Definition: BlockSparseMatrix.h:192
Scalar coeffRef(Index bi, Index j)
Definition: BlockSparseMatrix.h:198
Definition: BlockSparseMatrix.h:142
BlockVectorView(const BlockSparseMatrixT &spblockmat, const VectorType &vec)
Definition: BlockSparseMatrix.h:156
Index size() const
Definition: BlockSparseMatrix.h:158
Ref< const Matrix< typename BlockSparseMatrixT::Scalar,(RowsAtCompileTime==1) ? 1 :BlockSize,(ColsAtCompileTime==1) ? 1 :BlockSize > > Scalar
Definition: BlockSparseMatrix.h:152
BlockSparseMatrixT::Index Index
Definition: BlockSparseMatrix.h:153
Index cols() const
Definition: BlockSparseMatrix.h:157
const BlockSparseMatrixT & m_spblockmat
Definition: BlockSparseMatrix.h:171
Scalar coeff(Index bi) const
Definition: BlockSparseMatrix.h:159
@ ColsAtCompileTime
Definition: BlockSparseMatrix.h:146
@ BlockSize
Definition: BlockSparseMatrix.h:145
@ RowsAtCompileTime
Definition: BlockSparseMatrix.h:147
const VectorType & m_vec
Definition: BlockSparseMatrix.h:172
Scalar coeff(Index bi, Index j) const
Definition: BlockSparseMatrix.h:164
An InnerIterator allows to loop over the element of any matrix expression.
Definition: CoreIterators.h:37
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:96
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:264
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:30
static constexpr lastp1_t end
Definition: IndexedViewHelper.h:79
const unsigned int LvalueBit
Definition: Constants.h:148
const unsigned int RowMajorBit
Definition: Constants.h:70
RealScalar s
Definition: level1_cplx_impl.h:130
RealScalar alpha
Definition: level1_cplx_impl.h:151
EIGEN_BLAS_FUNC() swap(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:117
EIGEN_BLAS_FUNC() copy(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:32
const Scalar * a
Definition: level2_cplx_impl.h:32
int * m
Definition: level2_cplx_impl.h:294
@ Lhs
Definition: TensorContractionMapper.h:20
@ Rhs
Definition: TensorContractionMapper.h:20
void sparse_time_dense_product(const SparseLhsType &lhs, const DenseRhsType &rhs, DenseResType &res, const AlphaType &alpha)
Definition: SparseDenseProduct.h:165
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
const unsigned int NestByRefBit
Definition: Constants.h:173
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
const int InnerRandomAccessPattern
Definition: SparseUtil.h:42
const int Dynamic
Definition: Constants.h:25
Extend namespace for flags.
Definition: fsi_chan_precond_driver.cc:56
const unsigned nz
Definition: ConstraintElementsUnitTest.cpp:32
type
Definition: compute_granudrum_aor.py:141
Definition: Eigen_Colamd.h:49
Definition: Constants.h:519
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:43
Definition: Constants.h:534
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
Definition: Constants.h:522
Definition: BlockSparseMatrix.h:93
bool operator()(const Triplet &a, const Triplet &b)
Definition: BlockSparseMatrix.h:95
Iterator::value_type Triplet
Definition: BlockSparseMatrix.h:94
Definition: XprHelper.h:506
Ref< Matrix< typename BlockSparseMatrixT::RealScalar, BlockSparseMatrixT::BlockSize, BlockSparseMatrixT::BlockSize > > RealScalar
Definition: BlockSparseMatrix.h:88
Ref< Matrix< typename BlockSparseMatrixT::Scalar, BlockSparseMatrixT::BlockSize, BlockSparseMatrixT::BlockSize > > Scalar
Definition: BlockSparseMatrix.h:85
Index_ Index
Definition: BlockSparseMatrix.h:67
MatrixXpr XprKind
Definition: BlockSparseMatrix.h:69
Scalar_ Scalar
Definition: BlockSparseMatrix.h:66
Sparse StorageKind
Definition: BlockSparseMatrix.h:68
Dense StorageKind
Definition: BlockSparseMatrix.h:217
BlockSparseMatrixT::Index Index
Definition: BlockSparseMatrix.h:220
MatrixXpr XprKind
Definition: BlockSparseMatrix.h:218
BlockSparseMatrixT::Scalar Scalar
Definition: BlockSparseMatrix.h:219
Definition: ForwardDeclarations.h:21
Definition: fft_test_shared.h:66
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2