10#ifndef EIGEN_BLOCKSPARSEMATRIX_H
11#define EIGEN_BLOCKSPARSEMATRIX_H
14#include "./InternalHeaderCheck.h"
23template <
typename,
int,
bool>
25template <
typename,
int,
bool>
27template <
typename Scalar_,
int Options_,
int BlockRows_,
int BlockCols_,
typename StorageIndex_>
35 static std::string debugName() {
return "BlockSparseShape"; }
41template <
bool Conj,
typename T>
42std::enable_if_t<Conj, decltype(std::declval<const T&>().adjoint())> adjoint_if(
const T& m) {
46template <
bool Conj,
typename T>
47std::enable_if_t<!Conj, decltype(std::declval<const T&>().transpose())> adjoint_if(
const T& m) {
51struct storage_kind_to_evaluator_kind<BlockSparse> {
52 using Kind = IndexBased;
56struct storage_kind_to_shape<BlockSparse> {
57 using Shape = BlockSparseShape;
60template <
typename Scalar_,
int Options_,
int BlockRows_,
int BlockCols_,
typename StorageIndex_>
61struct traits<BlockSparseMatrix<Scalar_, Options_, BlockRows_, BlockCols_, StorageIndex_>> {
62 using Scalar = Scalar_;
63 using StorageIndex = StorageIndex_;
64 using StorageKind = BlockSparse;
65 using XprKind = MatrixXpr;
67 static constexpr Index RowsAtCompileTime = Dynamic;
68 static constexpr Index ColsAtCompileTime = Dynamic;
69 static constexpr Index MaxRowsAtCompileTime = Dynamic;
70 static constexpr Index MaxColsAtCompileTime = Dynamic;
71 static constexpr int Options = Options_;
72 static constexpr unsigned int Flags = Options_ | NestByRefBit |
LvalueBit;
88template <
typename Scalar_,
int BlockRows_,
int BlockCols_,
int Options_ = ColMajor,
typename StorageIndex_ =
int>
91 using Scalar = Scalar_;
92 using StorageIndex = StorageIndex_;
97 static constexpr int BlockSize = BlockRows_ * BlockCols_;
99 BlockTriplet() =
default;
101 BlockTriplet(StorageIndex blockRow, StorageIndex blockCol,
const BlockType& block)
102 : m_row(blockRow), m_col(blockCol) {
103 BlockMapType{m_value} = block;
106 StorageIndex row()
const {
return m_row; }
107 StorageIndex col()
const {
return m_col; }
109 ConstBlockMapType value()
const {
return ConstBlockMapType(m_value); }
112 StorageIndex m_row = 0;
113 StorageIndex m_col = 0;
115 Scalar m_value[BlockSize];
158template <
typename Scalar_,
int Options_,
int BlockRows_,
int BlockCols_,
typename StorageIndex_ =
int>
159class BlockSparseMatrix
160 :
public EigenBase<BlockSparseMatrix<Scalar_, Options_, BlockRows_, BlockCols_, StorageIndex_>> {
161 EIGEN_STATIC_ASSERT(BlockRows_ >= 1, BLOCKROWS_MUST_BE_A_POSITIVE_COMPILE_TIME_SIZE)
162 EIGEN_STATIC_ASSERT(BlockCols_ >= 1, BLOCKCOLS_MUST_BE_A_POSITIVE_COMPILE_TIME_SIZE)
163 EIGEN_STATIC_ASSERT(std::is_integral<StorageIndex_>::value&& std::is_signed<StorageIndex_>::value,
164 STORAGEINDEX_MUST_BE_A_SIGNED_INTEGRAL_TYPE)
168 EIGEN_STATIC_ASSERT(!(BlockCols_ == 1 && BlockRows_ != 1 &&
bool(Options_ &
RowMajorBit)),
169 INVALID_MATRIX_TEMPLATE_PARAMETERS)
170 EIGEN_STATIC_ASSERT(!(BlockRows_ == 1 && BlockCols_ != 1 && !
bool(Options_ &
RowMajorBit)),
171 INVALID_MATRIX_TEMPLATE_PARAMETERS)
177 using Scalar = Scalar_;
178 using StorageIndex = StorageIndex_;
182 static constexpr int Options = Options_;
183 static constexpr Index BlockRows = BlockRows_;
184 static constexpr Index BlockCols = BlockCols_;
185 static constexpr bool IsRowMajor = Options_ &
RowMajorBit;
186 static constexpr Index BlockSize = BlockRows_ * BlockCols_;
191 static constexpr std::size_t BlockBytes = std::size_t(BlockSize) *
sizeof(Scalar);
192 static constexpr int BlockMapAlignment = ((BlockBytes & (BlockBytes - 1)) == 0 && BlockBytes >= 8)
193 ?
int(numext::mini(BlockBytes, std::size_t(EIGEN_MAX_ALIGN_BYTES)))
215 Index rows() const noexcept {
return (IsRowMajor ? m_blockOuterSize : m_blockInnerSize) * BlockRows_; }
217 Index cols() const noexcept {
return (IsRowMajor ? m_blockInnerSize : m_blockOuterSize) * BlockCols_; }
220 Index blockRows()
const {
return IsRowMajor ? m_blockOuterSize : m_blockInnerSize; }
222 Index blockCols()
const {
return IsRowMajor ? m_blockInnerSize : m_blockOuterSize; }
244 const StorageIndex* outerIndexPtr()
const {
return m_outerIndex.
data(); }
245 StorageIndex* outerIndexPtr() {
return m_outerIndex.
data(); }
246 const StorageIndex* innerIndexPtr()
const {
return m_innerIndex.data(); }
247 StorageIndex* innerIndexPtr() {
return m_innerIndex.data(); }
248 const Scalar* valuePtr()
const {
return m_values.data(); }
249 Scalar* valuePtr() {
return m_values.data(); }
256 ConstBlockMap
blockRef(
Index k)
const {
return ConstBlockMap(m_values.data() + k * BlockSize); }
258 BlockMap
blockRef(
Index k) {
return BlockMap(m_values.data() + k * BlockSize); }
269 class InnerIterator {
271 EIGEN_STRONG_INLINE InnerIterator(
const BlockSparseMatrix& mat,
Index outer)
272 : m_mat(mat), m_id(mat.m_outerIndex(
outer)), m_end(mat.m_outerIndex(
outer + 1)), m_outer(
outer) {}
274 EIGEN_STRONG_INLINE
operator bool()
const {
return m_id < m_end; }
275 EIGEN_STRONG_INLINE InnerIterator& operator++() {
283 EIGEN_STRONG_INLINE
Index index()
const {
return m_mat.m_innerIndex(m_id); }
290 EIGEN_STRONG_INLINE ConstBlockMap
value()
const {
return m_mat.blockRef(m_id); }
293 return BlockMap(
const_cast<Scalar*
>(m_mat.m_values.data()) + m_id * BlockSize);
297 const BlockSparseMatrix& m_mat;
312 m_outerIndex.resize(m_blockOuterSize + 1);
313 m_outerIndex.setZero();
318 m_outerIndex.resize(m_blockOuterSize + 1);
319 m_outerIndex.setZero();
325 if (n >
Index(m_innerIndex.size())) conservativeResizeBlockStorage_(n);
331 if (nnz <
Index(m_innerIndex.size())) conservativeResizeBlockStorage_(nnz);
340 EIGEN_STATIC_ASSERT(BlockRows_ == BlockCols_, THIS_METHOD_IS_ONLY_FOR_SQUARE_BLOCK_MATRICES)
341 Index n = (std::min)(m_blockOuterSize, m_blockInnerSize);
342 m_outerIndex.resize(m_blockOuterSize + 1);
343 resizeBlockStorage_(n);
344 for (
Index i = 0; i <= m_blockOuterSize; ++i) m_outerIndex(i) = StorageIndex((std::min)(i, n));
345 for (StorageIndex i = 0; i < n; ++i) {
358 const StorageIndex_* innerPtr) {
363 resizeBlockStorage_(nnzBlocks);
381 template <
typename InputIterator>
416 eigen_assert(row >= 0 && row <
rows() && col >= 0 && col <
cols());
417 Index bOuter = IsRowMajor ? (row / BlockRows_) : (col / BlockCols_);
418 Index bInner = IsRowMajor ? (col / BlockCols_) : (row / BlockRows_);
419 Index localRow = row % BlockRows_;
420 Index localCol = col % BlockCols_;
421 const StorageIndex* beg = m_innerIndex.data() + m_outerIndex(bOuter);
422 const StorageIndex* fin = m_innerIndex.data() + m_outerIndex(bOuter + 1);
423 const StorageIndex* it = std::lower_bound(beg, fin, StorageIndex(bInner));
424 if (it == fin || *it != bInner)
return Scalar(0);
425 return blockRef(
static_cast<Index>(it - m_innerIndex.data()))(localRow, localCol);
436 constexpr Index OuterB = IsRowMajor ? BlockRows_ : BlockCols_;
437 constexpr Index InnerB = IsRowMajor ? BlockCols_ : BlockRows_;
441 for (
Index out = 0; out < m_blockOuterSize; ++out) {
442 const Index scalarOuterBegin = out * OuterB;
443 if (scalarOuterBegin >= diagSize)
break;
444 const Index scalarOuterEnd = numext::mini(scalarOuterBegin + OuterB, diagSize);
449 Index i = scalarOuterBegin;
450 while (i < scalarOuterEnd) {
451 const Index bInner = i / InnerB;
452 const Index groupEnd = numext::mini((bInner + 1) * InnerB, scalarOuterEnd);
454 const StorageIndex* beg = m_innerIndex.data() + m_outerIndex(out);
455 const StorageIndex* fin = m_innerIndex.data() + m_outerIndex(out + 1);
456 const StorageIndex* it = std::lower_bound(beg, fin, StorageIndex(bInner));
457 if (it != fin && *it == StorageIndex(bInner)) {
458 const ConstBlockMap blk =
blockRef(
static_cast<Index>(it - m_innerIndex.data()));
459 for (
Index j = i; j < groupEnd; ++j) {
460 const Index localRow = IsRowMajor ? (j % OuterB) : (j % InnerB);
461 const Index localCol = IsRowMajor ? (j % InnerB) : (j % OuterB);
462 diag(j) = blk(localRow, localCol);
476 BlockSparseMatrix
operator+(
const BlockSparseMatrix& other)
const {
return disjunctionWith_(other, AddOp_{}); }
479 BlockSparseMatrix
operator-(
const BlockSparseMatrix& other)
const {
return disjunctionWith_(other, SubOp_{}); }
483 return conjunctionWith_(other, CwiseMulOp_{});
487 template <
typename ScalarFunc>
489 return withValues_([&func](
const auto& v) {
return v.unaryExpr(func); });
501 template <
typename ScalarFunc>
502 BlockSparseMatrix
disjunctionExpr(
const BlockSparseMatrix& other, ScalarFunc func)
const {
503 return disjunctionWith_(other, DisjExprAdapter_<ScalarFunc>{func});
508 template <
typename ScalarFunc>
509 BlockSparseMatrix
conjunctionExpr(
const BlockSparseMatrix& other, ScalarFunc func)
const {
510 return conjunctionWith_(other, [&func](
const auto& a,
const auto& b) {
return a.binaryExpr(b, func); });
515 return withValues_([](
const auto& v) {
return -v; });
519 BlockSparseMatrix& operator-=(
const BlockSparseMatrix& other) {
return *
this = *
this - other; }
523 return withValues_([&s](
const auto& v) {
return v * s; });
529 BlockSparseMatrix operator/(
const Scalar& s)
const {
530 return withValues_([&s](
const auto& v) {
return v / s; });
532 BlockSparseMatrix& operator/=(
const Scalar& s) {
538 friend BlockSparseMatrix
operator*(
const Scalar& s,
const BlockSparseMatrix& m) {
return m * s; }
556 template <
typename OtherDerived>
559 (std::is_same<Scalar, typename OtherDerived::Scalar>::value),
560 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
574 template <
typename OtherDerived>
576 const BlockSparseMatrix& bsm) {
578 (std::is_same<Scalar_, typename OtherDerived::Scalar>::value),
579 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
595 template <
int RhsBlockCols>
603 const typename NumTraits<Scalar>::Real& prec = NumTraits<Scalar>::dummy_precision())
const {
604 using Comparison = internal::sparse_approx_comparison<Scalar>;
605 using Wide =
typename Comparison::Wide;
612 return m.m_values.head(m.nonZeros()).matrix().template cast<Wide>().squaredNorm();
614 return Comparison::compare(
615 squaredNorm(*
this), squaredNorm(other),
nonZeros() + other.
nonZeros(), prec,
616 [&] { return squaredNorm(*this - other); }, [&] { return toSparse().isApprox(other.toSparse(), prec); });
625 BlockSparseMatrix<Scalar_, Options_, BlockCols_, BlockRows_, StorageIndex_>
transpose()
const;
629 BlockSparseMatrix<Scalar_, Options_, BlockCols_, BlockRows_, StorageIndex_>
adjoint()
const;
645 template <
int Mode,
bool DiagIsTriangular = false>
646 BlockSparseTriangularView<BlockSparseMatrix, Mode, DiagIsTriangular>
triangularView()
const {
647 return BlockSparseTriangularView<BlockSparseMatrix, Mode, DiagIsTriangular>(*
this);
666 template <
int UpLo,
bool DiagIsSelfAdjo
int = false>
667 BlockSparseSelfAdjointView<BlockSparseMatrix, UpLo, DiagIsSelfAdjoint>
selfadjointView()
const {
668 EIGEN_STATIC_ASSERT(BlockRows_ == BlockCols_, THIS_METHOD_IS_ONLY_FOR_SQUARE_BLOCK_MATRICES)
669 return BlockSparseSelfAdjointView<BlockSparseMatrix, UpLo, DiagIsSelfAdjoint>(*
this);
674 template <
typename A,
typename B>
675 BlockType operator()(
const A& a,
const B& b)
const {
678 template <
typename A>
679 BlockType lhs(
const A& a)
const {
682 template <
typename B>
683 BlockType rhs(
const B& b)
const {
689 template <
typename A,
typename B>
690 BlockType operator()(
const A& a,
const B& b)
const {
693 template <
typename A>
694 BlockType lhs(
const A& a)
const {
697 template <
typename B>
698 BlockType rhs(
const B& b)
const {
705 template <
typename ScalarFunc>
706 struct DisjExprAdapter_ {
708 template <
typename A,
typename B>
709 BlockType operator()(
const A& a,
const B& b)
const {
710 return a.binaryExpr(b, func_);
712 template <
typename A>
713 BlockType lhs(
const A& a)
const {
714 return a.unaryExpr([
this](
const Scalar& x) {
return func_.lhs(x); });
716 template <
typename B>
717 BlockType rhs(
const B& b)
const {
718 return b.unaryExpr([
this](
const Scalar& x) {
return func_.rhs(x); });
723 template <
typename A,
typename B>
724 BlockType operator()(
const A& a,
const B& b)
const {
725 return a.cwiseProduct(b);
733 template <
typename F>
734 BlockSparseMatrix withValues_(F f)
const {
737 result.m_outerIndex = m_outerIndex;
738 result.m_innerIndex = m_innerIndex.head(nnz);
739 result.m_values = f(m_values.head(nnz * BlockSize));
747 template <
typename Op>
748 BlockSparseMatrix disjunctionWith_(
const BlockSparseMatrix& other, Op op)
const {
750 "BlockSparseMatrix size mismatch");
752 result.resizeBlockStorage_(
nonZeroBlocks() + other.nonZeroBlocks());
754 for (
Index j = 0; j < m_blockOuterSize; ++j) {
755 result.m_outerIndex(j) = StorageIndex_(nnz);
756 Index aId = m_outerIndex(j);
757 Index aEnd = m_outerIndex(j + 1);
758 Index bId = other.m_outerIndex(j);
759 Index bEnd = other.m_outerIndex(j + 1);
760 while (aId < aEnd || bId < bEnd) {
761 bool hasA = aId < aEnd;
762 bool hasB = bId < bEnd;
763 Index aInner = hasA ?
Index(m_innerIndex(aId)) : -1;
764 Index bInner = hasB ?
Index(other.m_innerIndex(bId)) : -1;
767 if (hasA && (!hasB || aInner < bInner)) {
768 result.m_innerIndex(nnz) = StorageIndex_(aInner);
769 result.blockRef(nnz) = op.lhs(
blockRef(aId++));
770 }
else if (hasB && (!hasA || bInner < aInner)) {
771 result.m_innerIndex(nnz) = StorageIndex_(bInner);
772 result.blockRef(nnz) = op.rhs(other.blockRef(bId++));
774 result.m_innerIndex(nnz) = StorageIndex_(aInner);
775 result.blockRef(nnz) = op(
blockRef(aId++), other.blockRef(bId++));
780 result.m_outerIndex(m_blockOuterSize) = StorageIndex_(nnz);
781 result.conservativeResizeBlockStorage_(nnz);
786 template <
typename BinaryOp>
787 BlockSparseMatrix conjunctionWith_(
const BlockSparseMatrix& other, BinaryOp func)
const {
789 "BlockSparseMatrix size mismatch");
791 result.resizeBlockStorage_((std::min)(
nonZeroBlocks(), other.nonZeroBlocks()));
793 for (
Index j = 0; j < m_blockOuterSize; ++j) {
794 result.m_outerIndex(j) = StorageIndex_(nnz);
795 Index aId = m_outerIndex(j);
796 Index aEnd = m_outerIndex(j + 1);
797 Index bId = other.m_outerIndex(j);
798 Index bEnd = other.m_outerIndex(j + 1);
799 while (aId < aEnd && bId < bEnd) {
800 Index aInner = m_innerIndex(aId);
801 Index bInner = other.m_innerIndex(bId);
802 if (aInner < bInner) {
804 }
else if (bInner < aInner) {
807 result.m_innerIndex(nnz) = StorageIndex_(aInner);
808 result.blockRef(nnz) = func(
blockRef(aId++), other.blockRef(bId++));
813 result.m_outerIndex(m_blockOuterSize) = StorageIndex_(nnz);
814 result.conservativeResizeBlockStorage_(nnz);
818 template <
bool Conjugate>
819 BlockSparseMatrix<Scalar_, Options_, BlockCols_, BlockRows_, StorageIndex_> transposeImpl()
const;
822 void resizeBlockStorage_(
Index n) {
823 m_innerIndex.resize(n);
824 m_values.resize(n * BlockSize);
827 void conservativeResizeBlockStorage_(
Index n) {
828 m_innerIndex.conservativeResize(n);
829 m_values.conservativeResize(n * BlockSize);
835 Index m_blockOuterSize = 0;
836 Index m_blockInnerSize = 0;
838 Array<StorageIndex, Dynamic, 1> m_outerIndex =
839 decltype(m_outerIndex)::Zero(m_blockOuterSize + 1);
840 Array<StorageIndex, Dynamic, 1> m_innerIndex;
843 Array<Scalar, Dynamic, 1> m_values;
856 template <
typename SparseMatrixType>
857 class MultiInnerIterator {
858 using StorageIndex =
typename SparseMatrixType::StorageIndex;
859 static constexpr int BlockOuterSize_ = IsRowMajor ? BlockRows_ : BlockCols_;
862 MultiInnerIterator(
const SparseMatrixType& mat,
Index outerBase)
863 : m_outerPtr(mat.outerIndexPtr()),
864 m_innerPtr(mat.innerIndexPtr()),
865 m_valuePtr(mat.valuePtr()),
866 m_outerBase(outerBase) {
867 for (
int k = 0; k < BlockOuterSize_; ++k) m_pos[k] = m_outerPtr[outerBase + k];
871 EIGEN_STRONG_INLINE
operator bool()
const {
return m_valid; }
873 EIGEN_STRONG_INLINE MultiInnerIterator& operator++() {
880 EIGEN_STRONG_INLINE
Index outer()
const {
return m_outerBase + m_active; }
882 EIGEN_STRONG_INLINE StorageIndex index()
const {
return m_innerPtr[m_pos[m_active]]; }
884 EIGEN_STRONG_INLINE Scalar value()
const {
return m_valuePtr[m_pos[m_active]]; }
890 for (
int k = 0; k < BlockOuterSize_; ++k) {
891 if (m_pos[k] < m_outerPtr[m_outerBase + k + 1]) {
892 if (!m_valid || m_innerPtr[m_pos[k]] < m_innerPtr[m_pos[m_active]]) {
900 const StorageIndex* m_outerPtr;
901 const StorageIndex* m_innerPtr;
902 const Scalar* m_valuePtr;
904 StorageIndex m_pos[BlockOuterSize_];
906 bool m_valid =
false;
911 template <
typename,
int,
int,
int,
typename>
912 friend class BlockSparseMatrix;
913 template <
typename,
int,
bool>
914 friend class BlockSparseTriangularView;
915 template <
typename,
int,
bool>
916 friend class BlockSparseSelfAdjointView;
927template <
typename Scalar_,
int Options_,
int BlockRows_,
int BlockCols_,
typename StorageIndex_>
928template <
typename InputIterator>
931 Index n =
static_cast<Index>(std::distance(begin, end));
938 for (InputIterator it = begin; it != end; ++it, ++k) {
939 eigen_assert(it->row() >= 0 && it->row() <
blockRows() &&
"setFromTriplets: block row out of range");
940 eigen_assert(it->col() >= 0 && it->col() <
blockCols() &&
"setFromTriplets: block col out of range");
941 tOuter(k) = IsRowMajor ? StorageIndex(it->row()) : StorageIndex(it->col());
942 tInner(k) = IsRowMajor ? StorageIndex(it->col()) : StorageIndex(it->row());
943 BlockMap(tValues.
data() + k * BlockSize) = it->value();
953 for (
Index i = 0; i < n; ++i) count(tInner(i) + 1)++;
954 for (
Index i = 0; i < m_blockInnerSize; ++i) count(i + 1) += count(i);
955 for (
Index i = 0; i < n; ++i) order(count(tInner(i))++) = i;
959 for (
Index i = 0; i < n; ++i) count(tOuter(i) + 1)++;
960 for (
Index i = 0; i < m_blockOuterSize; ++i) count(i + 1) += count(i);
961 for (
Index i = 0; i < n; ++i) {
962 Index idx = order(i);
963 scratch(count(tOuter(idx))++) = idx;
969 m_outerIndex.resize(m_blockOuterSize + 1);
970 m_outerIndex.setZero();
971 resizeBlockStorage_(n);
977 StorageIndex outer = tOuter(pi);
978 StorageIndex inner = tInner(pi);
980 BlockType block = ConstBlockMap(tValues.
data() + pi * BlockSize);
986 if (tOuter(pk) != outer || tInner(pk) != inner)
break;
987 block += ConstBlockMap(tValues.
data() + pk * BlockSize);
991 m_innerIndex(nnz) = inner;
993 m_outerIndex(outer + 1)++;
998 conservativeResizeBlockStorage_(nnz);
1001 for (
Index j = 0; j < m_blockOuterSize; ++j) {
1002 m_outerIndex(j + 1) += m_outerIndex(j);
1010template <
typename Scalar_,
int Options_,
int BlockRows_,
int BlockCols_,
typename StorageIndex_>
1020 for (
Index j = 0; j < m_blockOuterSize; ++j) {
1021 for (
Index c = 0; c < BlockCols_; ++c) {
1022 result.startVec(j * BlockCols_ + c);
1023 for (
Index id = m_outerIndex(j);
id < m_outerIndex(j + 1); ++id) {
1024 Index bi = m_innerIndex(
id);
1026 for (
Index r = 0; r < BlockRows_; ++r) {
1027 result.insertBack(bi * BlockRows_ + r, j * BlockCols_ + c) = blk(r, c);
1036 for (
Index bi = 0; bi < m_blockOuterSize; ++bi) {
1037 for (
Index r = 0; r < BlockRows_; ++r) {
1038 result.startVec(bi * BlockRows_ + r);
1039 for (
Index id = m_outerIndex(bi);
id < m_outerIndex(bi + 1); ++id) {
1040 Index j = m_innerIndex(
id);
1042 for (
Index c = 0; c < BlockCols_; ++c) {
1043 result.insertBack(bi * BlockRows_ + r, j * BlockCols_ + c) = blk(r, c);
1058template <
typename Scalar_,
int Options_,
int BlockRows_,
int BlockCols_,
typename StorageIndex_>
1062 eigen_assert(sp.
rows() % BlockRows_ == 0 &&
"matrix rows not divisible by BlockRows");
1063 eigen_assert(sp.
cols() % BlockCols_ == 0 &&
"matrix cols not divisible by BlockCols");
1064 eigen_assert(sp.
isCompressed() &&
"fromSparse requires a compressed SparseMatrix");
1071 constexpr Index BlockOuterSize = IsRowMajor ? BlockRows_ : BlockCols_;
1072 constexpr Index BlockInnerSize = IsRowMajor ? BlockCols_ : BlockRows_;
1073 constexpr StorageIndex_ kEmptyIndex = -1;
1077 BlockSparseMatrix result(bRows, bCols);
1081 for (
Index outerBlock = 0; outerBlock < result.m_blockOuterSize; ++outerBlock) {
1082 StorageIndex_ prevInnerBlock = kEmptyIndex;
1083 for (MultiInnerIterator<SpMat> it(sp, outerBlock * BlockOuterSize); it; ++it) {
1084 StorageIndex_ innerBlock = it.index() / StorageIndex_(BlockInnerSize);
1085 if (innerBlock != prevInnerBlock) {
1086 result.m_outerIndex(outerBlock + 1)++;
1087 prevInnerBlock = innerBlock;
1093 for (
Index j = 0; j < result.m_blockOuterSize; ++j) result.m_outerIndex(j + 1) += result.m_outerIndex(j);
1095 Index nBlocks = result.m_outerIndex(result.m_blockOuterSize);
1096 result.resizeBlockStorage_(nBlocks);
1101 for (
Index outerBlock = 0; outerBlock < result.m_blockOuterSize; ++outerBlock) {
1102 Index blockId = result.m_outerIndex(outerBlock) - 1;
1103 StorageIndex_ prevInnerBlock = kEmptyIndex;
1105 for (MultiInnerIterator<SpMat> it(sp, outerBlock * BlockOuterSize); it; ++it) {
1106 Index absOuter = it.outer();
1107 StorageIndex_ innerIdx = it.index();
1108 StorageIndex_ innerBlock = innerIdx / StorageIndex_(BlockInnerSize);
1110 if (innerBlock != prevInnerBlock) {
1112 result.m_innerIndex(blockId) = innerBlock;
1113 prevInnerBlock = innerBlock;
1119 Index localOuter = absOuter % BlockOuterSize;
1120 Index localInner = innerIdx % BlockInnerSize;
1121 Index offset = localOuter * BlockInnerSize + localInner;
1123 result.m_values(blockId * BlockSize + offset) = it.value();
1134template <
typename Scalar_,
int Options_,
int BlockRows_,
int BlockCols_,
typename StorageIndex_>
1135template <
int RhsBlockCols>
1138 const BlockSparseMatrix<Scalar_, Options_, BlockCols_, RhsBlockCols, StorageIndex_>& rhs)
const {
1139 using RhsMatrix = BlockSparseMatrix<Scalar_, Options_, BlockCols_, RhsBlockCols, StorageIndex_>;
1140 using ResultMatrix = BlockSparseMatrix<Scalar_, Options_, BlockRows_, RhsBlockCols, StorageIndex_>;
1142 constexpr int ResultBlockSize = BlockRows_ * RhsBlockCols;
1144 eigen_assert(
blockCols() == rhs.
blockRows() &&
"BlockSparseMatrix product: lhs.blockCols() != rhs.blockRows()");
1148 ResultMatrix result(cBlockRows, cBlockCols);
1152 Index maskSize = IsRowMajor ? cBlockCols : cBlockRows;
1167 Index cOuterSize = result.m_blockOuterSize;
1168 Index maxResultNnz = cBlockRows * cBlockCols;
1170 result.resizeBlockStorage_(capacity);
1173 for (
Index out = 0; out < cOuterSize; ++out) {
1174 result.m_outerIndex(out) = StorageIndex_(nnz);
1180 for (
Index rhsId = rhs.m_outerIndex(j); rhsId < rhs.m_outerIndex(j + 1); ++rhsId) {
1181 Index k = rhs.m_innerIndex(rhsId);
1182 typename RhsMatrix::ConstBlockMap Bkj = rhs.
blockRef(rhsId);
1183 for (
Index lhsId = m_outerIndex(k); lhsId < m_outerIndex(k + 1); ++lhsId) {
1184 Index bi = m_innerIndex(lhsId);
1185 if (!mask.
coeff(bi)) {
1188 indices(nIndices++) = bi;
1198 for (
Index lhsId = m_outerIndex(bi); lhsId < m_outerIndex(bi + 1); ++lhsId) {
1199 Index k = m_innerIndex(lhsId);
1200 ConstBlockMap Aik =
blockRef(lhsId);
1201 for (
Index rhsId = rhs.m_outerIndex(k); rhsId < rhs.m_outerIndex(k + 1); ++rhsId) {
1202 Index j = rhs.m_innerIndex(rhsId);
1203 if (!mask.
coeff(j)) {
1206 indices(nIndices++) = j;
1215 std::sort(indices.
data(), indices.
data() + nIndices);
1216 if (nnz + nIndices > capacity) {
1217 capacity = numext::mini(maxResultNnz, numext::maxi(2 * capacity, nnz + nIndices));
1218 result.conservativeResizeBlockStorage_(capacity);
1220 for (
Index ki = 0; ki < nIndices; ++ki) {
1221 Index idx = indices(ki);
1222 result.m_innerIndex(nnz) = StorageIndex_(idx);
1229 result.m_outerIndex(cOuterSize) = StorageIndex_(nnz);
1232 result.conservativeResizeBlockStorage_(nnz);
1256template <
typename BSM,
int Mode,
bool DiagIsTriangular = false>
1257class BlockSparseTriangularView {
1259 using Scalar =
typename BSM::Scalar;
1260 using StorageIndex =
typename BSM::StorageIndex;
1261 using BlockType =
typename BSM::BlockType;
1262 using BlockMap =
typename BSM::BlockMap;
1263 using ConstBlockMap =
typename BSM::ConstBlockMap;
1264 static constexpr int BlockRows = BSM::BlockRows;
1265 static constexpr int BlockCols = BSM::BlockCols;
1266 static constexpr int BlockSize = BSM::BlockSize;
1267 static constexpr bool IsRowMajor = BSM::IsRowMajor;
1268 static constexpr bool IsUpper = (Mode &
Upper) != 0;
1270 explicit BlockSparseTriangularView(
const BSM& m) : m_matrix(m) {}
1272 Index rows()
const {
return m_matrix.rows(); }
1273 Index cols()
const {
return m_matrix.cols(); }
1282 const BSM& m = m_matrix;
1283 BSM result(m.blockRows(), m.blockCols());
1284 result.resizeBlockStorage_(m.nonZeroBlocks());
1287 for (Index out = 0; out < m.m_blockOuterSize; ++out) {
1288 result.m_outerIndex(out) = StorageIndex(nnz);
1289 for (Index
id = m.m_outerIndex(out);
id < m.m_outerIndex(out + 1); ++
id) {
1290 Index inner = m.m_innerIndex(
id);
1291 Index bi = IsRowMajor ? out : inner;
1292 Index bj = IsRowMajor ? inner : out;
1293 if (IsUpper ? (bj < bi) : (bj > bi))
continue;
1294 result.m_innerIndex(nnz) = StorageIndex(inner);
1295 result.m_values.template segment<BlockSize>(nnz * BlockSize) =
1296 m.m_values.template segment<BlockSize>(
id * BlockSize);
1297 EIGEN_IF_CONSTEXPR (!DiagIsTriangular) {
1299 BlockMap(result.m_values.data() + nnz * BlockSize).template triangularView<ZeroMode>().setZero();
1304 result.m_outerIndex(m.m_blockOuterSize) = StorageIndex(nnz);
1305 result.conservativeResizeBlockStorage_(nnz);
1315 BSM operator-(
const BlockSparseTriangularView& other)
const {
return eval() - other.eval(); }
1318 template <
int RhsBlockCols>
1321 return eval() * rhs;
1326 template <
typename OtherDerived>
1328 EIGEN_STATIC_ASSERT(
1329 (std::is_same<Scalar, typename OtherDerived::Scalar>::value),
1330 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
1331 EIGEN_INTERNAL_ASSERT_PRODUCT_SHAPE_MSG(m_matrix, rhs,
"BlockSparseTriangularView * Dense: dimension mismatch");
1333 ResultType result = ResultType::Zero(m_matrix.rows(), rhs.cols());
1334 for (Index out = 0; out < m_matrix.m_blockOuterSize; ++out) {
1335 for (Index
id = m_matrix.m_outerIndex(out);
id < m_matrix.m_outerIndex(out + 1); ++
id) {
1336 Index inner = m_matrix.m_innerIndex(
id);
1337 Index bi = IsRowMajor ? out : inner;
1338 Index bj = IsRowMajor ? inner : out;
1339 if (IsUpper ? (bj < bi) : (bj > bi))
continue;
1340 constexpr int DiagMode = IsUpper ?
Upper :
Lower;
1341 if (!DiagIsTriangular && bi == bj)
1342 result.template middleRows<BlockRows>(bi * BlockRows).noalias() +=
1343 m_matrix.blockRef(
id).template triangularView<DiagMode>() *
1344 rhs.template middleRows<BlockCols>(bj * BlockCols);
1346 result.template middleRows<BlockRows>(bi * BlockRows).noalias() +=
1347 m_matrix.blockRef(
id) * rhs.template middleRows<BlockCols>(bj * BlockCols);
1353 template <
typename OtherDerived>
1354 friend Matrix<Scalar, OtherDerived::RowsAtCompileTime, Dynamic>
operator*(
const MatrixBase<OtherDerived>& lhs,
1355 const BlockSparseTriangularView& tri) {
1356 EIGEN_STATIC_ASSERT(
1357 (std::is_same<Scalar, typename OtherDerived::Scalar>::value),
1358 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
1359 EIGEN_INTERNAL_ASSERT_PRODUCT_SHAPE_MSG(lhs, tri.m_matrix,
"Dense * BlockSparseTriangularView: dimension mismatch");
1360 constexpr bool isRM = BSM::IsRowMajor;
1361 using ResultType = Matrix<Scalar, OtherDerived::RowsAtCompileTime, Dynamic>;
1365 const BSM& m = tri.m_matrix;
1366 const StorageIndex* outerPtr = m.outerIndexPtr();
1367 const StorageIndex* innerPtr = m.innerIndexPtr();
1368 ResultType result = ResultType::Zero(lhs.rows(), m.cols());
1369 for (Index out = 0; out < m.blockOuterSize(); ++out) {
1370 for (Index
id = outerPtr[out];
id < outerPtr[out + 1]; ++id) {
1371 Index inner = innerPtr[id];
1372 Index bi = isRM ? out : inner;
1373 Index bj = isRM ? inner : out;
1374 if (IsUpper ? (bj < bi) : (bj > bi))
continue;
1375 constexpr int DiagMode = IsUpper ?
Upper :
Lower;
1376 if (!DiagIsTriangular && bi == bj)
1377 result.template middleCols<BlockCols>(bj * BlockCols).noalias() +=
1378 lhs.template middleCols<BlockRows>(bi * BlockRows) * m.blockRef(
id).template triangularView<DiagMode>();
1380 result.template middleCols<BlockCols>(bj * BlockCols).noalias() +=
1381 lhs.template middleCols<BlockRows>(bi * BlockRows) * m.blockRef(
id);
1395 template <
typename Derived>
1397 doSolveImpl<false, false>(x.derived());
1402 const BlockSparseTriangularView& m_tri;
1403 template <
typename Derived>
1405 m_tri.template doSolveImpl<true, false>(x.derived());
1411 const BlockSparseTriangularView& m_tri;
1412 template <
typename Derived>
1414 m_tri.template doSolveImpl<true, true>(x.derived());
1419 AdjointReturnType adjoint()
const {
return {*
this}; }
1422 const BSM& m_matrix;
1431 template <
typename Derived>
1432 void doSolveDirect(Derived& x)
const {
1433 EIGEN_STATIC_ASSERT(BlockRows == BlockCols, THIS_METHOD_IS_ONLY_FOR_SQUARE_BLOCK_MATRICES)
1434 constexpr int DiagMode = IsUpper ?
Upper :
Lower;
1435 constexpr bool diagFirst = (IsUpper == BSM::IsRowMajor);
1436 Index nb = m_matrix.blockCols();
1437 EIGEN_INTERNAL_ASSERT_SAME_ROWS_MSG(x, m_matrix,
"solveInPlace: size mismatch");
1439 const StorageIndex* innerPtr = m_matrix.innerIndexPtr();
1440 const StorageIndex* outerPtr = m_matrix.outerIndexPtr();
1442 Index outerStart = IsUpper ? nb - 1 : 0;
1443 Index outerEnd = IsUpper ? -1 : nb;
1444 constexpr Index kStep = IsUpper ? -1 : 1;
1446 for (Index k = outerStart; k != outerEnd; k += kStep) {
1447 const StorageIndex* beg = innerPtr + outerPtr[k];
1448 const StorageIndex* end = innerPtr + outerPtr[k + 1];
1450 if (diagFirst) beg = std::lower_bound(beg, end, StorageIndex(k));
1451 if (!diagFirst) end = std::upper_bound(beg, end, StorageIndex(k));
1452 eigen_assert(beg != end &&
"solveInPlace: missing diagonal block");
1453 if (beg == end)
continue;
1454 const StorageIndex* diag_ptr = diagFirst ? beg : end - 1;
1455 const StorageIndex* off_beg = diagFirst ? beg + 1 : beg;
1456 const StorageIndex* off_end = diagFirst ? end : end - 1;
1457 eigen_assert(*diag_ptr == k);
1458 EIGEN_IF_CONSTEXPR (!BSM::IsRowMajor) {
1459 m_matrix.blockRef(diag_ptr - innerPtr)
1460 .template triangularView<DiagMode>()
1461 .solveInPlace(x.template middleRows<BlockRows>(k * BlockRows));
1462 for (
const StorageIndex* it = off_beg; it != off_end; ++it)
1463 x.template middleRows<BlockRows>(*it * BlockRows).noalias() -=
1464 m_matrix.blockRef(it - innerPtr) * x.template middleRows<BlockRows>(k * BlockRows);
1466 for (
const StorageIndex* it = off_beg; it != off_end; ++it)
1467 x.template middleRows<BlockRows>(k * BlockRows).noalias() -=
1468 m_matrix.blockRef(it - innerPtr) * x.template middleRows<BlockRows>(*it * BlockRows);
1469 m_matrix.blockRef(diag_ptr - innerPtr)
1470 .template triangularView<DiagMode>()
1471 .solveInPlace(x.template middleRows<BlockRows>(k * BlockRows));
1481 template <
bool Conjugate,
typename Derived>
1482 void doSolveTransposed(Derived& x)
const {
1483 EIGEN_STATIC_ASSERT(BlockRows == BlockCols, THIS_METHOD_IS_ONLY_FOR_SQUARE_BLOCK_MATRICES)
1484 constexpr int DiagMode = IsUpper ?
Upper :
Lower;
1485 constexpr bool diagFirst = (IsUpper == BSM::IsRowMajor);
1486 Index nb = m_matrix.blockCols();
1487 EIGEN_INTERNAL_ASSERT_SAME_ROWS_MSG(x, m_matrix,
"solveInPlace: size mismatch");
1489 const StorageIndex* innerPtr = m_matrix.innerIndexPtr();
1490 const StorageIndex* outerPtr = m_matrix.outerIndexPtr();
1492 Index outerStart = IsUpper ? 0 : nb - 1;
1493 Index outerEnd = IsUpper ? nb : -1;
1494 constexpr Index kStep = IsUpper ? 1 : -1;
1496 for (Index k = outerStart; k != outerEnd; k += kStep) {
1497 const StorageIndex* beg = innerPtr + outerPtr[k];
1498 const StorageIndex* end = innerPtr + outerPtr[k + 1];
1500 if (diagFirst) beg = std::lower_bound(beg, end, StorageIndex(k));
1501 if (!diagFirst) end = std::upper_bound(beg, end, StorageIndex(k));
1502 eigen_assert(beg != end &&
"solveInPlace: missing diagonal block");
1503 if (beg == end)
continue;
1504 const StorageIndex* diag_ptr = diagFirst ? beg : end - 1;
1505 const StorageIndex* off_beg = diagFirst ? beg + 1 : beg;
1506 const StorageIndex* off_end = diagFirst ? end : end - 1;
1507 eigen_assert(*diag_ptr == k);
1508 EIGEN_IF_CONSTEXPR (!BSM::IsRowMajor) {
1509 for (
const StorageIndex* it = off_beg; it != off_end; ++it)
1510 x.template middleRows<BlockRows>(k * BlockRows).noalias() -=
1511 internal::adjoint_if<Conjugate>(m_matrix.blockRef(it - innerPtr)) *
1512 x.template middleRows<BlockRows>(*it * BlockRows);
1513 internal::adjoint_if<Conjugate>(m_matrix.blockRef(diag_ptr - innerPtr).template triangularView<DiagMode>())
1514 .solveInPlace(x.template middleRows<BlockRows>(k * BlockRows));
1516 internal::adjoint_if<Conjugate>(m_matrix.blockRef(diag_ptr - innerPtr).template triangularView<DiagMode>())
1517 .solveInPlace(x.template middleRows<BlockRows>(k * BlockRows));
1518 for (
const StorageIndex* it = off_beg; it != off_end; ++it)
1519 x.template middleRows<BlockRows>(*it * BlockRows).noalias() -=
1520 internal::adjoint_if<Conjugate>(m_matrix.blockRef(it - innerPtr)) *
1521 x.template middleRows<BlockRows>(k * BlockRows);
1526 template <
bool Transposed,
bool Conjugate,
typename Derived>
1527 void doSolveImpl(Derived& x)
const {
1528 EIGEN_IF_CONSTEXPR (!Transposed)
1531 doSolveTransposed<Conjugate>(x);
1552template <
typename BSM,
int UpLo,
bool DiagIsSelfAdjo
int>
1553class BlockSparseSelfAdjointView {
1555 using Scalar =
typename BSM::Scalar;
1556 using StorageIndex =
typename BSM::StorageIndex;
1557 using BlockType =
typename BSM::BlockType;
1558 using BlockMap =
typename BSM::BlockMap;
1559 using ConstBlockMap =
typename BSM::ConstBlockMap;
1560 static constexpr int BlockRows = BSM::BlockRows;
1561 static constexpr int BlockCols = BSM::BlockCols;
1562 static constexpr int BlockSize = BSM::BlockSize;
1563 static constexpr bool IsRowMajor = BSM::IsRowMajor;
1564 static constexpr bool IsUpper = (UpLo &
Upper) != 0;
1566 static constexpr int DiagUpLo = IsUpper ?
Upper :
Lower;
1568 explicit BlockSparseSelfAdjointView(
const BSM& m) : m_matrix(m) {}
1570 Index rows()
const {
return m_matrix.rows(); }
1571 Index cols()
const {
return m_matrix.cols(); }
1580 const BSM& m = m_matrix;
1582 Index nDiag = 0, nOff = 0;
1583 for (Index out = 0; out < m.m_blockOuterSize; ++out)
1584 for (Index
id = m.m_outerIndex(out);
id < m.m_outerIndex(out + 1); ++
id) {
1585 Index inner = m.m_innerIndex(
id);
1586 Index bi = IsRowMajor ? out : inner;
1587 Index bj = IsRowMajor ? inner : out;
1588 if (IsUpper ? (bj < bi) : (bj > bi))
continue;
1595 Index nTotal = nDiag + 2 * nOff;
1601 for (Index out = 0; out < m.m_blockOuterSize; ++out)
1602 for (Index
id = m.m_outerIndex(out);
id < m.m_outerIndex(out + 1); ++
id) {
1603 Index inner = m.m_innerIndex(
id);
1604 Index bi = IsRowMajor ? out : inner;
1605 Index bj = IsRowMajor ? inner : out;
1606 if (IsUpper ? (bj < bi) : (bj > bi))
continue;
1608 brows(k) = StorageIndex(bi);
1609 bcols(k) = StorageIndex(bj);
1610 if (!DiagIsSelfAdjoint && bi == bj)
1611 BlockMap(bvals.
data() + k * BlockSize) = m.blockRef(
id).template selfadjointView<DiagUpLo>();
1613 BlockMap(bvals.
data() + k * BlockSize) = m.blockRef(
id);
1617 brows(k) = StorageIndex(bj);
1618 bcols(k) = StorageIndex(bi);
1619 BlockMap(bvals.
data() + k * BlockSize) = m.blockRef(
id).adjoint();
1626 std::iota(perm.
data(), perm.
data() + nTotal, Index(0));
1627 std::sort(perm.
data(), perm.
data() + nTotal, [&](Index a, Index b) {
1628 StorageIndex ao = IsRowMajor ? brows(a) : bcols(a);
1629 StorageIndex bo = IsRowMajor ? brows(b) : bcols(b);
1630 if (ao != bo) return ao < bo;
1631 return (IsRowMajor ? bcols(a) : brows(a)) < (IsRowMajor ? bcols(b) : brows(b));
1634 BSM result(m.blockRows(), m.blockCols());
1635 result.resizeBlockStorage_(nTotal);
1637 for (Index ki = 0; ki < nTotal; ++ki) {
1638 Index pi = perm(ki);
1639 StorageIndex outer = IsRowMajor ? brows(pi) : bcols(pi);
1640 StorageIndex inner = IsRowMajor ? bcols(pi) : brows(pi);
1641 result.m_outerIndex(outer + 1)++;
1642 result.m_innerIndex(ki) = inner;
1643 result.m_values.template segment<BlockSize>(ki * BlockSize) = bvals.template segment<BlockSize>(pi * BlockSize);
1645 for (Index j = 0; j < result.m_blockOuterSize; ++j) result.m_outerIndex(j + 1) += result.m_outerIndex(j);
1656 BSM operator-(
const BlockSparseSelfAdjointView& other)
const {
return eval() - other.eval(); }
1659 template <
int RhsBlockCols>
1662 return eval() * rhs;
1678 template <
typename OtherDerived>
1680 EIGEN_STATIC_ASSERT(
1681 (std::is_same<Scalar, typename OtherDerived::Scalar>::value),
1682 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
1683 EIGEN_INTERNAL_ASSERT_PRODUCT_SHAPE_MSG(m_matrix, rhs,
"BlockSparseSelfAdjointView * Dense: dimension mismatch");
1685 ResultType result = ResultType::Zero(m_matrix.rows(), rhs.cols());
1687 for (Index out = 0; out < m_matrix.m_blockOuterSize; ++out)
1688 for (Index
id = m_matrix.m_outerIndex(out);
id < m_matrix.m_outerIndex(out + 1); ++
id) {
1689 Index inner = m_matrix.m_innerIndex(
id);
1690 Index bi = IsRowMajor ? out : inner;
1691 Index bj = IsRowMajor ? inner : out;
1692 if (IsUpper ? (bj < bi) : (bj > bi))
continue;
1695 EIGEN_IF_CONSTEXPR (DiagIsSelfAdjoint) {
1696 result.template middleRows<BlockRows>(bi * BlockRows).noalias() +=
1697 m_matrix.blockRef(
id) * rhs.template middleRows<BlockCols>(bj * BlockCols);
1703 BlockType diag = m_matrix.blockRef(
id).template selfadjointView<DiagUpLo>();
1704 result.template middleRows<BlockRows>(bi * BlockRows).noalias() +=
1705 diag * rhs.template middleRows<BlockCols>(bj * BlockCols);
1708 result.template middleRows<BlockRows>(bi * BlockRows).noalias() +=
1709 m_matrix.blockRef(
id) * rhs.template middleRows<BlockCols>(bj * BlockCols);
1710 result.template middleRows<BlockRows>(bj * BlockRows).noalias() +=
1711 m_matrix.blockRef(
id).adjoint() * rhs.template middleRows<BlockRows>(bi * BlockRows);
1718 template <
typename OtherDerived>
1720 const BlockSparseSelfAdjointView& view) {
1721 EIGEN_STATIC_ASSERT(
1722 (std::is_same<Scalar, typename OtherDerived::Scalar>::value),
1723 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
1724 return (view * lhs.adjoint()).adjoint();
1728 const BSM& m_matrix;
1735template <
typename Scalar_,
int Options_,
int BlockRows_,
int BlockCols_,
typename StorageIndex_>
1736template <
bool Conjugate>
1737BlockSparseMatrix<Scalar_, Options_, BlockCols_, BlockRows_, StorageIndex_>
1738BlockSparseMatrix<Scalar_, Options_, BlockRows_, BlockCols_, StorageIndex_>::transposeImpl()
const {
1739 using ResultType = BlockSparseMatrix<Scalar_, Options_, BlockCols_, BlockRows_, StorageIndex_>;
1740 ResultType result(blockCols(), blockRows());
1743 for (Index
id = 0;
id < nonZeroBlocks(); ++id) result.m_outerIndex(m_innerIndex(
id) + 1)++;
1746 for (Index j = 0; j < result.m_blockOuterSize; ++j) result.m_outerIndex(j + 1) += result.m_outerIndex(j);
1748 Index nnz = nonZeroBlocks();
1749 result.resizeBlockStorage_(nnz);
1754 Array<StorageIndex_, Dynamic, 1> pos = result.m_outerIndex.head(result.m_blockOuterSize);
1756 for (Index oldOuter = 0; oldOuter < m_blockOuterSize; ++oldOuter) {
1757 for (Index
id = m_outerIndex(oldOuter);
id < m_outerIndex(oldOuter + 1); ++id) {
1758 Index newOuter = m_innerIndex(
id);
1759 Index insertAt = pos(newOuter)++;
1760 result.m_innerIndex(insertAt) = StorageIndex_(oldOuter);
1761 result.blockRef(insertAt) = internal::adjoint_if<Conjugate>(blockRef(
id));
1767template <
typename Scalar_,
int Options_,
int BlockRows_,
int BlockCols_,
typename StorageIndex_>
1770 return transposeImpl<false>();
1773template <
typename Scalar_,
int Options_,
int BlockRows_,
int BlockCols_,
typename StorageIndex_>
1776 return transposeImpl<true>();
1785template <
typename Lhs,
typename Rhs,
int ProductType>
1786struct generic_product_impl<Lhs, Rhs,
BlockSparseShape, DenseShape, ProductType>
1787 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, BlockSparseShape, DenseShape, ProductType>> {
1790 template <
typename Dst>
1791 static void scaleAndAddTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs,
const Scalar& alpha) {
1792 constexpr bool IsRM = (Lhs::Options &
RowMajorBit) != 0;
1793 constexpr int BR = Lhs::BlockRows;
1794 constexpr int BC = Lhs::BlockCols;
1795 const typename Lhs::StorageIndex* outerPtr = lhs.outerIndexPtr();
1796 const typename Lhs::StorageIndex* innerPtr = lhs.innerIndexPtr();
1799 bool a1 = (alpha == Scalar(1));
1800 bool am1 = (alpha == Scalar(-1));
1801 for (Eigen::Index out = 0; out < lhs.blockOuterSize(); ++out) {
1802 for (Eigen::Index
id = outerPtr[out];
id < outerPtr[out + 1]; ++id) {
1803 Eigen::Index inner = innerPtr[id];
1804 Eigen::Index bi = IsRM ? out : inner;
1805 Eigen::Index bj = IsRM ? inner : out;
1806 auto dst_seg = dst.template middleRows<BR>(bi * BR);
1807 auto rhs_seg = rhs.template middleRows<BC>(bj * BC);
1808 if (EIGEN_PREDICT_TRUE(a1))
1809 dst_seg.noalias() += lhs.blockRef(
id) * rhs_seg;
1811 dst_seg.noalias() -= lhs.blockRef(
id) * rhs_seg;
1816 TmpType tmp(BR, rhs.cols());
1817 tmp.noalias() = lhs.blockRef(
id) * rhs_seg;
1818 dst_seg += alpha * tmp;
1828template <
typename Lhs,
typename Rhs,
int ProductType>
1829struct generic_product_impl<Lhs, Rhs, DenseShape, BlockSparseShape, ProductType>
1830 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs, DenseShape, BlockSparseShape, ProductType>> {
1831 using Scalar =
typename Product<Lhs, Rhs>::Scalar;
1833 template <
typename Dst>
1834 static void scaleAndAddTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs,
const Scalar& alpha) {
1835 constexpr bool IsRM = (Rhs::Options &
RowMajorBit) != 0;
1836 constexpr int BR = Rhs::BlockRows;
1837 constexpr int BC = Rhs::BlockCols;
1838 const typename Rhs::StorageIndex* outerPtr = rhs.outerIndexPtr();
1839 const typename Rhs::StorageIndex* innerPtr = rhs.innerIndexPtr();
1840 bool a1 = (alpha == Scalar(1));
1841 bool am1 = (alpha == Scalar(-1));
1842 for (Eigen::Index out = 0; out < rhs.blockOuterSize(); ++out) {
1843 for (Eigen::Index
id = outerPtr[out];
id < outerPtr[out + 1]; ++id) {
1844 Eigen::Index inner = innerPtr[id];
1845 Eigen::Index bi = IsRM ? out : inner;
1846 Eigen::Index bj = IsRM ? inner : out;
1847 auto dst_seg = dst.template middleCols<BC>(bj * BC);
1848 auto lhs_seg = lhs.template middleCols<BR>(bi * BR);
1849 if (EIGEN_PREDICT_TRUE(a1))
1850 dst_seg.noalias() += lhs_seg * rhs.blockRef(
id);
1852 dst_seg.noalias() -= lhs_seg * rhs.blockRef(
id);
1854 using TmpType = Matrix<Scalar, Lhs::RowsAtCompileTime, BC>;
1855 TmpType tmp(lhs.rows(), BC);
1856 tmp.noalias() = lhs_seg * rhs.blockRef(
id);
1857 dst_seg += alpha * tmp;
General-purpose arrays with easy API for coefficient-wise operations.
Definition Array.h:55
constexpr const Scalar & coeff(Index rowId, Index colId) const
Definition PlainObjectBase.h:187
constexpr Scalar & coeffRef(Index rowId, Index colId)
Definition PlainObjectBase.h:205
Index blockCol() const
Definition BlockSparseMatrix.h:287
Index index() const
Definition BlockSparseMatrix.h:283
BlockMap valueRef()
Definition BlockSparseMatrix.h:292
ConstBlockMap value() const
Definition BlockSparseMatrix.h:290
Index outer() const
Definition BlockSparseMatrix.h:281
Index blockRow() const
Definition BlockSparseMatrix.h:285
A sparse matrix whose stored nonzeros are fixed-size dense blocks.
Definition BlockSparseMatrix.h:160
Index nonZeros() const
Definition BlockSparseMatrix.h:237
Index cols() const noexcept
Definition BlockSparseMatrix.h:217
BlockSparseMatrix operator+(const BlockSparseMatrix &other) const
Definition BlockSparseMatrix.h:476
Matrix< Scalar, Dynamic, 1 > diagonal() const
Definition BlockSparseMatrix.h:435
BlockSparseMatrix()=default
void reserve(Index n)
Definition BlockSparseMatrix.h:324
void setZero()
Definition BlockSparseMatrix.h:317
Index nonZeroBlocks() const
Definition BlockSparseMatrix.h:235
Scalar coeff(Index row, Index col) const
Definition BlockSparseMatrix.h:415
BlockSparseMatrix(Index blockRows, Index blockCols)
Definition BlockSparseMatrix.h:207
Index blockRows() const
Definition BlockSparseMatrix.h:220
Index rows() const noexcept
Definition BlockSparseMatrix.h:215
BlockSparseMatrix cwiseProduct(const BlockSparseMatrix &other) const
Definition BlockSparseMatrix.h:482
void resize(Index blockRows, Index blockCols)
Definition BlockSparseMatrix.h:309
friend BlockSparseMatrix operator*(const Scalar &s, const BlockSparseMatrix &m)
Definition BlockSparseMatrix.h:538
Index allocatedBlocks() const
Definition BlockSparseMatrix.h:239
void setFromOuterInner(Index blockRows, Index blockCols, Index nnzBlocks, const StorageIndex_ *outerPtr, const StorageIndex_ *innerPtr)
Definition BlockSparseMatrix.h:357
Index innerSize() const
Definition BlockSparseMatrix.h:232
Index blockCols() const
Definition BlockSparseMatrix.h:222
BlockSparseMatrix disjunctionExpr(const BlockSparseMatrix &other, ScalarFunc func) const
Definition BlockSparseMatrix.h:502
BlockSparseMatrix< Scalar_, Options_, BlockCols_, BlockRows_, StorageIndex_ > transpose() const
Definition BlockSparseMatrix.h:1769
BlockSparseMatrix conjunctionExpr(const BlockSparseMatrix &other, ScalarFunc func) const
Definition BlockSparseMatrix.h:509
void setFromTriplets(InputIterator begin, InputIterator end)
Definition BlockSparseMatrix.h:929
static BlockSparseMatrix fromSparse(const SparseMatrix< Scalar_, Options_, StorageIndex_ > &sp)
Definition BlockSparseMatrix.h:1060
BlockMap blockRef(Index k)
Definition BlockSparseMatrix.h:258
friend Product< OtherDerived, BlockSparseMatrix, AliasFreeProduct > operator*(const MatrixBase< OtherDerived > &lhs, const BlockSparseMatrix &bsm)
Definition BlockSparseMatrix.h:575
Index blockOuterSize() const
Definition BlockSparseMatrix.h:225
void squeeze()
Definition BlockSparseMatrix.h:329
BlockSparseMatrix operator-(const BlockSparseMatrix &other) const
Definition BlockSparseMatrix.h:479
BlockSparseTriangularView< BlockSparseMatrix, Mode, DiagIsTriangular > triangularView() const
Definition BlockSparseMatrix.h:646
void setIdentity()
Definition BlockSparseMatrix.h:339
BlockSparseSelfAdjointView< BlockSparseMatrix, UpLo, DiagIsSelfAdjoint > selfadjointView() const
Definition BlockSparseMatrix.h:667
Index outerSize() const
Definition BlockSparseMatrix.h:230
BlockSparseMatrix operator-() const
Definition BlockSparseMatrix.h:514
BlockSparseMatrix operator*(const Scalar &s) const
Definition BlockSparseMatrix.h:522
Index blockInnerSize() const
Definition BlockSparseMatrix.h:227
BlockSparseMatrix< Scalar_, Options_, BlockCols_, BlockRows_, StorageIndex_ > adjoint() const
Definition BlockSparseMatrix.h:1775
BlockSparseMatrix unaryExpr(ScalarFunc func) const
Definition BlockSparseMatrix.h:488
Product< BlockSparseMatrix, OtherDerived, AliasFreeProduct > operator*(const MatrixBase< OtherDerived > &rhs) const
Definition BlockSparseMatrix.h:557
ConstBlockMap blockRef(Index k) const
Definition BlockSparseMatrix.h:256
SparseMatrix< Scalar, Options_, StorageIndex_ > toSparse() const
Definition BlockSparseMatrix.h:1012
Lazy block-level self-adjoint (Hermitian) view of a BlockSparseMatrix.
Definition BlockSparseMatrix.h:1553
BSM eval() const
Definition BlockSparseMatrix.h:1579
friend Matrix< Scalar, OtherDerived::RowsAtCompileTime, Dynamic > operator*(const MatrixBase< OtherDerived > &lhs, const BlockSparseSelfAdjointView &view)
Definition BlockSparseMatrix.h:1719
SparseMatrix< Scalar, BSM::Options, StorageIndex > toSparse() const
Definition BlockSparseMatrix.h:1651
Matrix< Scalar, Dynamic, OtherDerived::ColsAtCompileTime > operator*(const MatrixBase< OtherDerived > &rhs) const
Definition BlockSparseMatrix.h:1679
BlockSparseMatrix< Scalar, BSM::Options, BlockRows, RhsBlockCols, StorageIndex > operator*(const BlockSparseMatrix< Scalar, BSM::Options, BlockCols, RhsBlockCols, StorageIndex > &rhs) const
Definition BlockSparseMatrix.h:1660
Lazy block-level triangular view of a BlockSparseMatrix.
Definition BlockSparseMatrix.h:1257
BlockSparseMatrix< Scalar, BSM::Options, BlockRows, RhsBlockCols, StorageIndex > operator*(const BlockSparseMatrix< Scalar, BSM::Options, BlockCols, RhsBlockCols, StorageIndex > &rhs) const
Definition BlockSparseMatrix.h:1319
SparseMatrix< Scalar, BSM::Options, StorageIndex > toSparse() const
Definition BlockSparseMatrix.h:1310
void solveInPlace(MatrixBase< Derived > &x) const
Definition BlockSparseMatrix.h:1396
BSM eval() const
Definition BlockSparseMatrix.h:1280
A (blockRow, blockCol, blockValue) triplet for assembling a BlockSparseMatrix.
Definition BlockSparseMatrix.h:89
A matrix or vector expression mapping an existing array of data.
Definition Map.h:97
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
constexpr const Scalar * data() const
Definition PlainObjectBase.h:261
Derived & setZero(Index size)
Definition CwiseNullaryOp.h:536
Expression of the product of two arbitrary matrices or vectors.
Definition Product.h:211
A versatile sparse matrix representation.
Definition SparseMatrix.h:121
bool isCompressed() const
Definition SparseCompressedBase.h:115
Index cols() const
Definition SparseMatrix.h:162
Index rows() const
Definition SparseMatrix.h:160
void reserve(Index reserveSize)
Definition SparseMatrix.h:317
@ StrictlyLower
Definition Constants.h:224
@ StrictlyUpper
Definition Constants.h:226
@ Lower
Definition Constants.h:212
@ Upper
Definition Constants.h:214
constexpr unsigned int LvalueBit
Definition Constants.h:149
constexpr unsigned int RowMajorBit
Definition Constants.h:71
Definition BlockSparseMatrix.h:34
Definition BlockSparseMatrix.h:1410
Definition BlockSparseMatrix.h:1401
Definition BlockSparseMatrix.h:31
Definition EigenBase.h:34
Eigen::Index Index
Definition EigenBase.h:44