11 #ifndef EIGEN_SPARSEBLOCKMATRIX_H
12 #define EIGEN_SPARSEBLOCKMATRIX_H
56 template<
typename Scalar_,
int _BlockAtCompileTime=Dynamic,
int Options_=ColMajor,
typename StorageIndex_=
int>
class BlockSparseMatrix;
58 template<
typename BlockSparseMatrixT>
class BlockSparseMatrixView;
61 template<
typename Scalar_,
int _BlockAtCompileTime,
int Options_,
typename Index_>
62 struct traits<BlockSparseMatrix<Scalar_,_BlockAtCompileTime,Options_, Index_> >
64 typedef Scalar_ Scalar;
66 typedef Sparse StorageKind;
67 typedef MatrixXpr XprKind;
73 BlockSize = _BlockAtCompileTime,
79 template<
typename BlockSparseMatrixT>
80 struct traits<BlockSparseMatrixView<BlockSparseMatrixT> >
82 typedef Ref<Matrix<typename BlockSparseMatrixT::Scalar, BlockSparseMatrixT::BlockSize, BlockSparseMatrixT::BlockSize> > Scalar;
83 typedef Ref<Matrix<typename BlockSparseMatrixT::RealScalar, BlockSparseMatrixT::BlockSize, BlockSparseMatrixT::BlockSize> > RealScalar;
88 template<
typename Iterator,
bool IsColMajor>
91 typedef typename Iterator::value_type Triplet;
94 return ((
a.col() ==
b.col() &&
a.row() <
b.row()) || (
a.col() <
b.col()));
96 return ((
a.row() ==
b.row() &&
a.col() <
b.col()) || (
a.row() <
b.row()));
103 template<
typename BlockSparseMatrixT>
109 typedef typename BlockSparseMatrixT::Index
Index;
112 Flags = BlockSparseMatrixT::Options,
145 class InnerIterator :
public BlockSparseMatrixT::BlockInnerIterator
159 template<
typename BlockSparseMatrixT,
typename VectorType>
167 Flags = VectorType::Flags
170 typedef typename BlockSparseMatrixT::Index
Index;
187 return m_vec.middleRows(startRow, rowSize);
193 return m_vec.block(startRow,
j, rowSize, 1);
204 template<
typename BlockSparseMatrixT,
typename VectorType>
211 Flags = VectorType::Flags
214 typedef typename BlockSparseMatrixT::Index
Index;
227 return m_vec.middleRows(startRow, rowSize);
233 return m_vec.block(startRow,
j, rowSize, 1);
242 template<
typename Lhs,
typename Rhs>
247 template<
typename BlockSparseMatrixT,
typename VecType>
250 typedef Dense StorageKind;
252 typedef typename BlockSparseMatrixT::Scalar Scalar;
253 typedef typename BlockSparseMatrixT::Index
Index;
257 MaxRowsAtCompileTime =
Dynamic,
258 MaxColsAtCompileTime =
Dynamic,
260 CoeffReadCost = internal::traits<BlockSparseMatrixT>::CoeffReadCost
265 template<
typename Lhs,
typename Rhs>
267 :
public ProductBase<BlockSparseTimeDenseProduct<Lhs,Rhs>, Lhs, Rhs>
275 template<
typename Dest>
void scaleAndAddTo(Dest& dest,
const typename Rhs::Scalar& alpha)
const
285 template<
typename Scalar_,
int _BlockAtCompileTime,
int Options_,
typename StorageIndex_>
292 typedef typename internal::ref_selector<BlockSparseMatrix<Scalar_, _BlockAtCompileTime, Options_, StorageIndex_> >::type
Nested;
354 std::swap(
first.m_innerBSize, second.m_innerBSize);
355 std::swap(
first.m_outerBSize, second.m_outerBSize);
356 std::swap(
first.m_innerOffset, second.m_innerOffset);
357 std::swap(
first.m_outerOffset, second.m_outerOffset);
358 std::swap(
first.m_nonzerosblocks, second.m_nonzerosblocks);
359 std::swap(
first.m_nonzeros, second.m_nonzeros);
360 std::swap(
first.m_values, second.m_values);
361 std::swap(
first.m_blockPtr, second.m_blockPtr);
362 std::swap(
first.m_indices, second.m_indices);
363 std::swap(
first.m_outerIndex, second.m_outerIndex);
364 std::swap(
first.m_BlockSize, second.m_blockSize);
390 template<
typename MatrixType>
406 template<
typename MatrixType>
410 &&
"Trying to assign to a zero-size matrix, call resize() first");
421 blockPattern.startVec(bj);
424 typename MatrixType::InnerIterator it_spmat(spmat,
j);
425 for(; it_spmat; ++it_spmat)
428 if(!nzblocksFlag[bi])
431 nzblocksFlag[bi] =
true;
432 blockPattern.insertBackByOuterInnerUnordered(bj, bi) =
true;
439 blockPattern.finalize();
451 typename MatrixType::InnerIterator it_spmat(spmat,
j);
452 for(; it_spmat; ++it_spmat)
474 m_values[idxVal] = it_spmat.value();
499 template<
typename MatrixType>
502 resize(blockPattern.rows(), blockPattern.cols());
503 reserve(blockPattern.nonZeros());
515 std::vector<int> nzBlockIdx;
516 typename MatrixType::InnerIterator it(blockPattern, bj);
519 nzBlockIdx.push_back(it.index());
521 std::sort(nzBlockIdx.begin(), nzBlockIdx.end());
524 for(
StorageIndex idx = 0; idx < nzBlockIdx.size(); ++idx)
601 "TRYING TO RESERVE ZERO-SIZE MATRICES, CALL resize() first");
632 template<
typename InputIterator>
641 internal::TripletComp<InputIterator, IsColMajor> tripletcomp;
642 std::sort(begin,
end, tripletcomp);
654 for(InputIterator it(begin); it !=
end; ++it)
656 eigen_assert(it->row() >= 0 && it->row() < this->blockRows() && it->col() >= 0 && it->col() < this->blockCols());
662 eigen_assert((rowBlocks[it->row()] == 0 || rowBlocks[it->row()] == it->
value().rows()) &&
663 "NON CORRESPONDING SIZES FOR ROW BLOCKS");
664 eigen_assert((colBlocks[it->col()] == 0 || colBlocks[it->col()] == it->
value().cols()) &&
665 "NON CORRESPONDING SIZES FOR COLUMN BLOCKS");
666 rowBlocks[it->row()] =it->
value().rows();
667 colBlocks[it->col()] = it->
value().cols();
669 nz_outer(
IsColMajor ? it->col() : it->row()) += it->
value().rows() * it->value().cols();
670 nzblock_outer(
IsColMajor ? it->col() : it->row())++;
694 for(InputIterator it(begin); it!=
end; ++it)
699 StorageIndex block_size = it->value().rows()*it->value().cols();
701 memcpy(&(
m_values[nz_marker]), it->value().data(), block_size *
sizeof(
Scalar));
855 template<
typename VecType>
918 class BlockInnerIterator;
924 BlockInnerIterator itb(
m,
j);
927 s <<
"("<<itb.row() <<
", " << itb.col() <<
")\n";
928 s << itb.value() <<
"\n";
973 template<
typename Scalar_,
int _BlockAtCompileTime,
int Options_,
typename StorageIndex_>
974 class BlockSparseMatrix<Scalar_, _BlockAtCompileTime, Options_, StorageIndex_>::BlockInnerIterator
983 : m_mat(
mat),m_outer(outer),
989 inline BlockInnerIterator&
operator++() {m_id++;
return *
this; }
991 inline const Map<const BlockScalar> value()
const
993 return Map<const BlockScalar>(&(m_mat.m_values[m_mat.blockPtr(m_id)]),
996 inline Map<BlockScalar> valueRef()
998 return Map<BlockScalar>(&(m_mat.m_values[m_mat.blockPtr(m_id)]),
1002 inline Index index()
const {
return m_mat.m_indices[m_id]; }
1003 inline Index outer()
const {
return m_outer; }
1005 inline Index row()
const {
return index(); }
1007 inline Index col()
const {
return outer(); }
1009 inline Index rows()
const {
return (m_mat.m_blockSize==
Dynamic) ? (m_mat.m_innerOffset[index()+1] - m_mat.m_innerOffset[index()]) : m_mat.m_blockSize; }
1011 inline Index cols()
const {
return (m_mat.m_blockSize==
Dynamic) ? (m_mat.m_outerOffset[m_outer+1]-m_mat.m_outerOffset[m_outer]) : m_mat.m_blockSize;}
1012 inline operator bool()
const {
return (m_id < m_end); }
1015 const BlockSparseMatrix<Scalar_, _BlockAtCompileTime, Options_, StorageIndex>& m_mat;
1016 const Index m_outer;
1021 template<
typename Scalar_,
int _BlockAtCompileTime,
int Options_,
typename StorageIndex_>
1022 class BlockSparseMatrix<Scalar_, _BlockAtCompileTime, Options_, StorageIndex_>::InnerIterator
1032 m_id = m_mat.blockInnerIndex(itb.index());
1034 m_end = m_mat.blockInnerIndex(itb.index()+1);
1045 m_id = m_mat.blockInnerIndex(itb.index());
1047 m_end = m_mat.blockInnerIndex(itb.index()+1);
1052 inline const Scalar& value()
const
1054 return itb.value().coeff(m_id - m_start, m_offset);
1056 inline Scalar& valueRef()
1058 return itb.valueRef().coeff(m_id - m_start, m_offset);
1060 inline Index index()
const {
return m_id; }
1061 inline Index outer()
const {
return m_outer; }
1062 inline Index col()
const {
return outer(); }
1063 inline Index row()
const {
return index();}
1064 inline operator bool()
const
1070 const Index m_outer;
1071 const Index m_outerB;
1072 BlockInnerIterator itb;
1073 const Index m_offset;
RowXpr row(Index i) const
ColXpr col(Index i) const
IndexedView_or_VectorBlock operator()(const Indices &indices)
#define EIGEN_STATIC_ASSERT(X, MSG)
Matrix< float, 1, Dynamic > MatrixType
BlockSparseMatrixView(const BlockSparseMatrixT &spblockmat)
BlockSparseMatrixT Nested
Ref< typename BlockSparseMatrixT::BlockScalar > Scalar
Scalar coeff(Index row, Index col)
BlockSparseMatrixT::Index Index
Scalar coeffRef(Index row, Index col)
const BlockSparseMatrixT & m_spblockmat
Ref< typename BlockSparseMatrixT::BlockRealScalar > RealScalar
A versatile sparse matrix representation where each element is a block.
BlockSparseMatrix & operator=(const MatrixType &spmat)
Assignment from a sparse matrix with the same storage order.
Index innerToBlock(Index inner) const
void setBlockStructure(const MatrixType &blockPattern)
Set the nonzero block pattern of the matrix.
const StorageIndex * outerIndexPtr() const
Index blockInnerIndex(Index bi) const
friend std::ostream & operator<<(std::ostream &s, const BlockSparseMatrix &m)
BlockSparseMatrix(Index brow, Index bcol)
Construct and resize.
BlockSparseMatrix & operator=(BlockSparseMatrix other)
Index blockPtr(Index id) const
internal::ref_selector< BlockSparseMatrix< Scalar_, _BlockAtCompileTime, Options_, StorageIndex_ > >::type Nested
StorageIndex * innerIndexPtr()
Index innerBlocks() const
void reserve(const Index nonzerosblocks)
Allocate the internal array of pointers to blocks and their inner indices.
Index outerToBlock(Index outer) const
Map< const BlockScalar > coeff(Index brow, Index bcol) const
Matrix< RealScalar, _BlockAtCompileTime, _BlockAtCompileTime, IsColMajor ? ColMajor :RowMajor > BlockRealScalar
Index blockOuterIndex(Index bj) const
Index blockRowsIndex(Index bi) const
Matrix< Scalar, _BlockAtCompileTime, _BlockAtCompileTime, IsColMajor ? ColMajor :RowMajor > BlockScalar
StorageIndex * m_outerOffset
BlockSparseMatrix(const MatrixType &spmat)
Constructor from a sparse matrix.
StorageIndex_ StorageIndex
void setFromTriplets(const InputIterator &begin, const InputIterator &end)
Fill values in a matrix from a triplet list.
void setBlockLayout(const VectorXi &rowBlocks, const VectorXi &colBlocks)
Set the row and column block layouts,.
Ref< BlockScalar > coeffRef(Index brow, Index bcol)
std::conditional_t< _BlockAtCompileTime==Dynamic, Scalar, BlockScalar > BlockScalarReturnType
NumTraits< Scalar >::Real RealScalar
StorageIndex * m_blockPtr
StorageIndex * m_innerOffset
Index blockInnerSize(Index bi) const
BlockSparseMatrix(const BlockSparseMatrix &other)
Copy-constructor.
StorageIndex * outerIndexPtr()
StorageIndex * m_outerIndex
Index blockOuterSize(Index bj) const
void resize(Index brow, Index bcol)
Set the number of rows and columns blocks.
const StorageIndex * innerIndexPtr() const
friend void swap(BlockSparseMatrix &first, BlockSparseMatrix &second)
Index outerBlocks() const
BlockSparseMatrix< Scalar, BlockSize, IsColMajor ? ColMajor :RowMajor, StorageIndex > PlainObject
bool isCompressed() const
for compatibility purposes with the SparseMatrix class
BlockSparseTimeDenseProduct< BlockSparseMatrix, VecType > operator*(const VecType &lhs) const
BlockScalarReturnType * valuePtr()
Index blockColsIndex(Index bj) const
Index nonZerosBlocks() const
void setBlockSize(Index blockSize)
set the block size at runtime for fixed-size block layout
Map< BlockScalar > insert(Index brow, Index bcol)
BlockSparseTimeDenseProduct & operator=(const BlockSparseTimeDenseProduct &)
void scaleAndAddTo(Dest &dest, const typename Rhs::Scalar &alpha) const
Ref< Matrix< typename VectorType::Scalar, RowsAtCompileTime, ColsAtCompileTime > > Scalar
Scalar coeffRef(Index bi)
BlockSparseMatrixT::Index Index
BlockVectorReturn(const BlockSparseMatrixT &spblockmat, VectorType &vec)
const BlockSparseMatrixT & m_spblockmat
Scalar coeffRef(Index bi, Index j)
BlockVectorView(const BlockSparseMatrixT &spblockmat, const VectorType &vec)
Ref< const Matrix< typename BlockSparseMatrixT::Scalar,(RowsAtCompileTime==1)? 1 :BlockSize,(ColsAtCompileTime==1)? 1 :BlockSize > > Scalar
BlockSparseMatrixT::Index Index
const BlockSparseMatrixT & m_spblockmat
Scalar coeff(Index bi) const
Scalar coeff(Index bi, Index j) const
CoeffReturnType value() const
Matrix< Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_ > & setZero(Index rows, Index cols)
static const lastp1_t end
const unsigned int LvalueBit
const unsigned int RowMajorBit
bfloat16 operator++(bfloat16 &a)
EIGEN_CONSTEXPR Index first(const T &x) EIGEN_NOEXCEPT
void sparse_time_dense_product(const SparseLhsType &lhs, const DenseRhsType &rhs, DenseResType &res, const AlphaType &alpha)
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
const unsigned int NestByRefBit
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
const int InnerRandomAccessPattern