11#ifndef EIGEN_SPARSEMATRIX_H
12#define EIGEN_SPARSEMATRIX_H
15#include "./InternalHeaderCheck.h"
51template <
typename Scalar_,
int Options_,
typename StorageIndex_>
52struct traits<SparseMatrix<Scalar_, Options_, StorageIndex_>> {
53 using Scalar = Scalar_;
54 using StorageIndex = StorageIndex_;
55 using StorageKind = Sparse;
56 using XprKind = MatrixXpr;
58 RowsAtCompileTime = Dynamic,
59 ColsAtCompileTime = Dynamic,
60 MaxRowsAtCompileTime = Dynamic,
61 MaxColsAtCompileTime = Dynamic,
64 SupportedAccessPatterns = InnerRandomAccessPattern
68template <
typename Scalar_,
int Options_,
typename StorageIndex_,
int DiagIndex>
69struct traits<Diagonal<SparseMatrix<Scalar_, Options_, StorageIndex_>, DiagIndex>> {
73 using Scalar = Scalar_;
74 using StorageKind = Dense;
75 using StorageIndex = StorageIndex_;
76 using XprKind = MatrixXpr;
79 RowsAtCompileTime = Dynamic,
80 ColsAtCompileTime = 1,
81 MaxRowsAtCompileTime = Dynamic,
82 MaxColsAtCompileTime = 1,
87template <
typename Scalar_,
int Options_,
typename StorageIndex_,
int DiagIndex>
88struct traits<Diagonal<const SparseMatrix<Scalar_, Options_, StorageIndex_>, DiagIndex>>
89 :
public traits<Diagonal<SparseMatrix<Scalar_, Options_, StorageIndex_>, DiagIndex>> {
93template <
typename StorageIndex>
94struct sparse_reserve_op {
95 EIGEN_DEVICE_FUNC sparse_reserve_op(Index begin, Index end, Index size) {
96 Index range = numext::mini(end - begin, size);
98 m_end = begin + range;
99 m_val = StorageIndex(size / range);
100 m_remainder = StorageIndex(size % range);
102 template <
typename IndexType>
103 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE StorageIndex operator()(IndexType i)
const {
104 if ((i >= m_begin) && (i < m_end))
105 return m_val + ((i - m_begin) < m_remainder ? 1 : 0);
109 StorageIndex m_val, m_remainder;
110 Index m_begin, m_end;
113template <
typename Scalar>
114struct functor_traits<sparse_reserve_op<Scalar>> {
115 enum { Cost = 1, PacketAccess =
false, IsRepeatable =
true };
120template <
typename Scalar_,
int Options_,
typename StorageIndex_>
123 using Base::convert_index;
126 template <
typename,
typename,
typename,
typename,
typename>
127 friend struct internal::Assignment;
133 using Base::operator+=;
134 using Base::operator-=;
139 using InnerIterator =
typename Base::InnerIterator;
140 using ReverseInnerIterator =
typename Base::ReverseInnerIterator;
142 using Base::IsRowMajor;
143 using Storage = internal::CompressedStorage<Scalar, StorageIndex>;
144 enum { Options = Options_ };
146 using IndexVector =
typename Base::IndexVector;
147 using ScalarVector =
typename Base::ScalarVector;
160 inline Index
rows()
const {
return IsRowMajor ? m_outerSize : m_innerSize; }
162 inline Index
cols()
const {
return IsRowMajor ? m_innerSize : m_outerSize; }
172 inline const Scalar*
valuePtr()
const {
return m_data.valuePtr(); }
176 inline Scalar*
valuePtr() {
return m_data.valuePtr(); }
206 constexpr Storage& data() {
return m_data; }
208 constexpr const Storage& data()
const {
return m_data; }
215 const Index outer = IsRowMajor ?
row :
col;
216 const Index inner = IsRowMajor ?
col :
row;
217 Index end = m_innerNonZeros ? m_outerIndex[outer] + m_innerNonZeros[outer] : m_outerIndex[outer + 1];
218 return m_data.atInRange(m_outerIndex[outer], end, inner);
234 const Index outer = IsRowMajor ?
row :
col;
235 const Index inner = IsRowMajor ?
col :
row;
236 Index start = m_outerIndex[outer];
237 Index end =
isCompressed() ? m_outerIndex[outer + 1] : m_outerIndex[outer] + m_innerNonZeros[outer];
238 eigen_assert(end >= start &&
"you probably called coeffRef on a non finalized matrix");
239 Index dst = start == end ? end : m_data.searchLowerIndex(start, end, inner);
241 Index capacity = m_outerIndex[outer + 1] - end;
244 m_innerNonZeros[outer]++;
246 m_data.value(end) = Scalar(0);
247 if (inserted !=
nullptr) {
250 return m_data.value(end);
253 if ((dst < end) && (m_data.index(dst) == inner)) {
255 if (inserted !=
nullptr) {
258 return m_data.value(dst);
260 if (inserted !=
nullptr) {
264 return insertAtByOuterInner(outer, inner, dst);
309 if (m_innerNonZeros) {
318 eigen_assert(
isCompressed() &&
"This function does not make sense in non compressed mode.");
319 m_data.reserve(reserveSize);
322#ifdef EIGEN_PARSED_BY_DOXYGEN
335 template <
class SizesType>
336 inline void reserve(
const SizesType& reserveSizes);
338 template <
class SizesType>
339 inline void reserve(
const SizesType& reserveSizes,
340 const typename SizesType::value_type& enableif =
typename SizesType::value_type()) {
341 EIGEN_UNUSED_VARIABLE(enableif);
342 reserveInnerVectors(reserveSizes);
346 template <
class SizesType>
347 inline void reserveInnerVectors(
const SizesType& reserveSizes) {
349 Index totalReserveSize = 0;
350 for (Index j = 0; j < m_outerSize; ++j) totalReserveSize += internal::convert_index<Index>(reserveSizes[j]);
353 if (totalReserveSize == 0)
return;
356 m_innerNonZeros = internal::conditional_aligned_new_auto<StorageIndex, true>(m_outerSize);
359 StorageIndex* newOuterIndex = m_innerNonZeros;
362 for (Index j = 0; j < m_outerSize; ++j) {
363 newOuterIndex[j] = internal::convert_index<StorageIndex>(count);
364 Index reserveSize = internal::convert_index<Index>(reserveSizes[j]);
365 count += reserveSize + internal::convert_index<Index>(m_outerIndex[j + 1] - m_outerIndex[j]);
368 m_data.reserve(totalReserveSize);
369 StorageIndex previousOuterIndex = m_outerIndex[m_outerSize];
370 for (Index j = m_outerSize - 1; j >= 0; --j) {
371 StorageIndex innerNNZ = previousOuterIndex - m_outerIndex[j];
372 StorageIndex begin = m_outerIndex[j];
373 StorageIndex end = begin + innerNNZ;
374 StorageIndex target = newOuterIndex[j];
378 eigen_internal_assert(target >= begin);
379 if (target != begin) {
383 previousOuterIndex = m_outerIndex[j];
384 m_outerIndex[j] = newOuterIndex[j];
385 m_innerNonZeros[j] = innerNNZ;
388 m_outerIndex[m_outerSize] = m_outerIndex[m_outerSize - 1] + m_innerNonZeros[m_outerSize - 1] +
389 internal::convert_index<StorageIndex>(reserveSizes[m_outerSize - 1]);
391 m_data.resize(internal::convert_index<Index>(m_outerIndex[m_outerSize]));
393 StorageIndex* newOuterIndex = internal::conditional_aligned_new_auto<StorageIndex, true>(m_outerSize + 1);
396 for (
Index j = 0; j < m_outerSize; ++j) {
397 newOuterIndex[j] = internal::convert_index<StorageIndex>(count);
398 Index alreadyReserved =
399 internal::convert_index<Index>(m_outerIndex[j + 1] - m_outerIndex[j] - m_innerNonZeros[j]);
400 Index reserveSize = internal::convert_index<Index>(reserveSizes[j]);
401 Index toReserve = numext::maxi(reserveSize, alreadyReserved);
402 count += toReserve + internal::convert_index<Index>(m_innerNonZeros[j]);
404 newOuterIndex[m_outerSize] = internal::convert_index<StorageIndex>(count);
406 m_data.resize(count);
407 for (
Index j = m_outerSize - 1; j >= 0; --j) {
414 eigen_internal_assert(target >= begin);
415 if (target != begin) {
421 std::swap(m_outerIndex, newOuterIndex);
422 internal::conditional_aligned_delete_auto<StorageIndex, true>(newOuterIndex, m_outerSize + 1);
440 return insertBackByOuterInner(IsRowMajor ?
row :
col, IsRowMajor ?
col :
row);
445 inline Scalar& insertBackByOuterInner(
Index outer,
Index inner) {
446 eigen_assert(
Index(m_outerIndex[outer + 1]) == m_data.size() &&
"Invalid ordered insertion (invalid outer index)");
447 eigen_assert((m_outerIndex[outer + 1] - m_outerIndex[outer] == 0 || m_data.index(m_data.size() - 1) < inner) &&
448 "Invalid ordered insertion (invalid inner index)");
450 ++m_outerIndex[outer + 1];
451 m_data.append(Scalar(0), inner);
452 return m_data.value(internal::convert_index<Index>(p));
457 inline Scalar& insertBackByOuterInnerUnordered(
Index outer,
Index inner) {
459 ++m_outerIndex[outer + 1];
460 m_data.append(Scalar(0), inner);
461 return m_data.value(p);
466 inline void startVec(
Index outer) {
467 eigen_assert(m_outerIndex[outer] ==
Index(m_data.size()) &&
468 "You must call startVec for each inner vector sequentially");
469 eigen_assert(m_outerIndex[outer + 1] == 0 &&
"You must call startVec for each inner vector sequentially");
470 m_outerIndex[outer + 1] = m_outerIndex[outer];
476 inline void finalize() {
479 Index i = m_outerSize;
481 while (i >= 0 && m_outerIndex[i] == 0) --i;
483 while (i <= m_outerSize) {
484 m_outerIndex[i] =
size;
491 void removeOuterVectors(
Index j,
Index num = 1) {
492 eigen_assert(num >= 0 && j >= 0 && j + num <= m_outerSize &&
"Invalid parameters");
494 const Index newRows = IsRowMajor ? m_outerSize - num :
rows();
495 const Index newCols = IsRowMajor ?
cols() : m_outerSize - num;
497 const Index begin = j + num;
498 const Index end = m_outerSize;
499 const Index target = j;
502 if (m_outerIndex[j + num] > m_outerIndex[j])
uncompress();
506 std::copy(m_outerIndex + begin, m_outerIndex + end + 1, m_outerIndex + target);
507 if (!
isCompressed()) std::copy(m_innerNonZeros + begin, m_innerNonZeros + end, m_innerNonZeros + target);
511 if (num < m_outerSize && m_outerIndex[0] >
StorageIndex(0)) {
513 const Index from = internal::convert_index<Index>(m_outerIndex[0]);
514 const Index chunkSize = internal::convert_index<Index>(m_innerNonZeros[0]);
525 void insertEmptyOuterVectors(
Index j,
Index num = 1) {
527 eigen_assert(num >= 0 && j >= 0 && j < m_outerSize &&
"Invalid parameters");
529 const Index newRows = IsRowMajor ? m_outerSize + num :
rows();
530 const Index newCols = IsRowMajor ?
cols() : m_outerSize + num;
532 const Index begin = j;
533 const Index end = m_outerSize;
538 if (num == 0)
return;
541 std::copy_backward(m_outerIndex + begin, m_outerIndex + end + 1, m_outerIndex + end + 1 + num);
543 fill_n(m_outerIndex + begin, num, m_outerIndex[begin]);
546 std::copy_backward(m_innerNonZeros + begin, m_innerNonZeros + end, m_innerNonZeros + end + num);
552 template <
typename InputIterators>
555 template <
typename InputIterators,
typename DupFunctor>
556 void setFromTriplets(
const InputIterators& begin,
const InputIterators& end, DupFunctor dup_func);
558 template <
typename Derived,
typename DupFunctor>
561 template <
typename InputIterators>
564 template <
typename InputIterators,
typename DupFunctor>
567 template <
typename InputIterators>
570 template <
typename InputIterators,
typename DupFunctor>
571 void insertFromTriplets(
const InputIterators& begin,
const InputIterators& end, DupFunctor dup_func);
573 template <
typename InputIterators>
576 template <
typename InputIterators,
typename DupFunctor>
583 Scalar& insertByOuterInner(Index j, Index i) {
584 eigen_assert(j >= 0 && j < m_outerSize &&
"invalid outer index");
585 eigen_assert(i >= 0 && i < m_innerSize &&
"invalid inner index");
586 Index start = internal::convert_index<Index>(m_outerIndex[j]);
587 Index end =
isCompressed() ? internal::convert_index<Index>(m_outerIndex[j + 1])
588 : start + internal::convert_index<Index>(m_innerNonZeros[j]);
589 Index dst = start == end ? end : m_data.searchLowerIndex(start, end, i);
591 Index capacity = internal::convert_index<Index>(m_outerIndex[j + 1]) - end;
594 m_innerNonZeros[j]++;
596 m_data.value(end) = Scalar(0);
597 return m_data.value(end);
600 eigen_assert((dst == end || m_data.index(dst) != i) &&
601 "you cannot insert an element that already exists, you must call coeffRef to this end");
602 return insertAtByOuterInner(j, i, dst);
610 eigen_internal_assert(m_outerIndex != 0 && m_outerSize > 0);
613 m_outerIndex[1] = m_innerNonZeros[0];
615 Index copyStart = internal::convert_index<Index>(start);
616 Index copyTarget = internal::convert_index<Index>(m_innerNonZeros[0]);
617 for (Index j = 1; j < m_outerSize; j++) {
621 bool breakUpCopy = (end != nextStart) || (j == m_outerSize - 1);
623 Index chunkSize = internal::convert_index<Index>(end) - copyStart;
625 if (copyTarget != copyStart) {
629 copyStart = internal::convert_index<Index>(nextStart);
630 copyTarget += chunkSize;
633 m_outerIndex[j + 1] = m_outerIndex[j] + m_innerNonZeros[j];
635 m_data.resize(internal::convert_index<Index>(m_outerIndex[m_outerSize]));
638 internal::conditional_aligned_delete_auto<StorageIndex, true>(m_innerNonZeros, m_outerSize);
646 m_innerNonZeros = internal::conditional_aligned_new_auto<StorageIndex, true>(m_outerSize);
647 if (m_outerIndex[m_outerSize] == 0) {
651 for (Index j = 0; j < m_outerSize; j++) m_innerNonZeros[j] = m_outerIndex[j + 1] - m_outerIndex[j];
656 void prune(
const Scalar& reference,
const RealScalar& epsilon = NumTraits<RealScalar>::dummy_precision()) {
657 prune(default_prunning_func(reference, epsilon));
668 template <
typename KeepFunc>
669 void prune(
const KeepFunc& keep = KeepFunc()) {
671 for (Index j = 0; j < m_outerSize; ++j) {
681 bool keepEntry = keep(
row,
col, m_data.value(i));
685 if (std::is_trivially_copyable<Scalar>::value || k != i) {
686 m_data.value(k) = internal::move_if_assignable(m_data.value(i));
687 m_data.index(k) = m_data.index(i);
691 m_innerNonZeros[j]--;
695 m_outerIndex[m_outerSize] = k;
717 Index newOuterSize = IsRowMajor ?
rows :
cols;
718 Index newInnerSize = IsRowMajor ?
cols :
rows;
720 Index innerChange = newInnerSize - m_innerSize;
721 Index outerChange = newOuterSize - m_outerSize;
723 if (outerChange != 0) {
724 m_outerIndex = internal::conditional_aligned_realloc_new_auto<StorageIndex, true>(m_outerIndex, newOuterSize + 1,
728 m_innerNonZeros = internal::conditional_aligned_realloc_new_auto<StorageIndex, true>(m_innerNonZeros,
729 newOuterSize, m_outerSize);
731 if (outerChange > 0) {
734 fill_n(m_outerIndex + m_outerSize, outerChange + 1, lastIdx);
739 m_outerSize = newOuterSize;
741 if (innerChange < 0) {
742 for (Index j = 0; j < m_outerSize; j++) {
743 Index start = m_outerIndex[j];
744 Index end =
isCompressed() ? m_outerIndex[j + 1] : start + m_innerNonZeros[j];
745 Index lb = m_data.searchLowerIndex(start, end, newInnerSize);
752 m_innerSize = newInnerSize;
754 Index newSize = m_outerIndex[m_outerSize];
755 eigen_assert(newSize <= m_data.size());
756 m_data.resize(newSize);
774 m_innerSize = IsRowMajor ?
cols :
rows;
777 if ((m_outerIndex == 0) || (m_outerSize !=
outerSize)) {
778 m_outerIndex = internal::conditional_aligned_realloc_new_auto<StorageIndex, true>(m_outerIndex,
outerSize + 1,
783 internal::conditional_aligned_delete_auto<StorageIndex, true>(m_innerNonZeros, m_outerSize);
798 void resizeNonZeros(Index
size) { m_data.resize(
size); }
801 const ConstDiagonalReturnType
diagonal()
const {
return ConstDiagonalReturnType(*
this); }
807 DiagonalReturnType
diagonal() {
return DiagonalReturnType(*
this); }
810 inline SparseMatrix() : m_outerSize(0), m_innerSize(0), m_outerIndex(0), m_innerNonZeros(0) {
resize(0, 0); }
813 inline SparseMatrix(Index
rows, Index
cols) : m_outerSize(0), m_innerSize(0), m_outerIndex(0), m_innerNonZeros(0) {
818 template <
typename OtherDerived>
820 : m_outerSize(0), m_innerSize(0), m_outerIndex(0), m_innerNonZeros(0) {
822 (std::is_same<Scalar, typename OtherDerived::Scalar>::value),
823 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
828#ifdef EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
829 EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
831 internal::call_assignment_no_alias(*
this, other.
derived());
836 template <
typename OtherDerived,
unsigned int UpLo>
838 : m_outerSize(0), m_innerSize(0), m_outerIndex(0), m_innerNonZeros(0) {
839 Base::operator=(other);
845 template <
typename OtherDerived>
847 *
this = other.
derived().markAsRValue();
852 : Base(), m_outerSize(0), m_innerSize(0), m_outerIndex(0), m_innerNonZeros(0) {
857 template <
typename OtherDerived>
859 : Base(), m_outerSize(0), m_innerSize(0), m_outerIndex(0), m_innerNonZeros(0) {
860 initAssignment(other);
865 template <
typename OtherDerived>
867 : Base(), m_outerSize(0), m_innerSize(0), m_outerIndex(0), m_innerNonZeros(0) {
874 std::swap(m_outerIndex, other.m_outerIndex);
875 std::swap(m_innerSize, other.m_innerSize);
876 std::swap(m_outerSize, other.m_outerSize);
877 std::swap(m_innerNonZeros, other.m_innerNonZeros);
878 m_data.swap(other.m_data);
886 eigen_assert(m_outerSize == m_innerSize &&
"ONLY FOR SQUARED MATRICES");
887 internal::conditional_aligned_delete_auto<StorageIndex, true>(m_innerNonZeros, m_outerSize);
889 m_data.resize(m_outerSize);
892 std::iota(m_outerIndex, m_outerIndex + m_outerSize + 1,
StorageIndex(0));
895 fill_n(
valuePtr(), m_outerSize, Scalar(1));
899 if (other.isRValue()) {
900 swap(other.const_cast_derived());
901 }
else if (
this != &other) {
902#ifdef EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
903 EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
905 initAssignment(other);
907 internal::smart_copy(other.m_outerIndex, other.m_outerIndex + m_outerSize + 1, m_outerIndex);
908 m_data = other.m_data;
910 Base::operator=(other);
921 template <
typename OtherDerived>
922 inline SparseMatrix& operator=(
const EigenBase<OtherDerived>& other) {
923 return Base::operator=(other.derived());
926 template <
typename Lhs,
typename Rhs>
927 inline SparseMatrix& operator=(
const Product<Lhs, Rhs, AliasFreeProduct>& other);
929 template <
typename OtherDerived>
930 EIGEN_DONT_INLINE
SparseMatrix& operator=(
const SparseMatrixBase<OtherDerived>& other);
932 template <
typename OtherDerived>
934 *
this = other.
derived().markAsRValue();
939 friend std::ostream& operator<<(std::ostream& s,
const SparseMatrix& m) {
941 s <<
"Nonzero entries:\n";
if (m.isCompressed()) {
942 for (Index i = 0; i < m.nonZeros(); ++i) s <<
"(" << m.m_data.value(i) <<
"," << m.m_data.index(i) <<
") ";
944 for (Index i = 0; i < m.outerSize(); ++i) {
945 Index p = m.m_outerIndex[i];
946 Index pe = m.m_outerIndex[i] + m.m_innerNonZeros[i];
948 for (; k < pe; ++k) {
949 s <<
"(" << m.m_data.value(k) <<
"," << m.m_data.index(k) <<
") ";
951 for (; k < m.m_outerIndex[i + 1]; ++k) {
956 s << std::endl; s <<
"Outer pointers:\n";
957 for (
Index i = 0; i < m.outerSize(); ++i) { s << m.m_outerIndex[i] <<
" "; } s <<
" $" << std::endl;
958 if (!m.isCompressed()) {
959 s <<
"Inner non zeros:\n";
960 for (Index i = 0; i < m.outerSize(); ++i) {
961 s << m.m_innerNonZeros[i] <<
" ";
963 s <<
" $" << std::endl;
966 s << static_cast<const SparseMatrixBase<SparseMatrix>&>(m);
973 internal::conditional_aligned_delete_auto<StorageIndex, true>(m_outerIndex, m_outerSize + 1);
974 internal::conditional_aligned_delete_auto<StorageIndex, true>(m_innerNonZeros, m_outerSize);
980#ifdef EIGEN_SPARSEMATRIX_PLUGIN
981#include EIGEN_SPARSEMATRIX_PLUGIN
985 template <
typename Other>
986 void initAssignment(
const Other& other) {
987 resize(other.rows(), other.cols());
988 internal::conditional_aligned_delete_auto<StorageIndex, true>(m_innerNonZeros, m_outerSize);
994 EIGEN_DEPRECATED EIGEN_DONT_INLINE Scalar& insertCompressed(Index row, Index col);
998 EIGEN_DEPRECATED EIGEN_DONT_INLINE Scalar& insertUncompressed(Index row, Index col);
1003 EIGEN_STRONG_INLINE Scalar& insertBackUncompressed(Index row, Index col) {
1004 const Index outer = IsRowMajor ? row : col;
1005 const Index inner = IsRowMajor ? col : row;
1007 eigen_assert(!isCompressed());
1008 eigen_assert(m_innerNonZeros[outer] <= (m_outerIndex[outer + 1] - m_outerIndex[outer]));
1010 Index p = m_outerIndex[outer] + m_innerNonZeros[outer]++;
1011 m_data.index(p) = StorageIndex(inner);
1012 m_data.value(p) = Scalar(0);
1013 return m_data.value(p);
1017 struct IndexPosPair {
1018 IndexPosPair(Index a_i, Index a_p) : i(a_i), p(a_p) {}
1032 template <
typename DiagXpr,
typename Func>
1033 void assignDiagonal(
const DiagXpr diagXpr,
const Func& assignFunc) {
1034 constexpr StorageIndex kEmptyIndexVal(-1);
1035 using ValueMap =
typename ScalarVector::AlignedMapType;
1037 Index n = diagXpr.size();
1039 const bool overwrite = std::is_same<Func, internal::assign_op<Scalar, Scalar>>::value;
1041 if ((m_outerSize != n) || (m_innerSize != n) || (n == 0)) resize(n, n);
1044 if (m_data.size() == 0 || overwrite) {
1045 internal::conditional_aligned_delete_auto<StorageIndex, true>(m_innerNonZeros, m_outerSize);
1046 m_innerNonZeros = 0;
1048 ValueMap valueMap(valuePtr(), n);
1049 std::iota(m_outerIndex, m_outerIndex + n + 1, StorageIndex(0));
1050 std::iota(innerIndexPtr(), innerIndexPtr() + n, StorageIndex(0));
1052 internal::call_assignment_no_alias(valueMap, diagXpr, assignFunc);
1054 internal::evaluator<DiagXpr> diaEval(diagXpr);
1056 ei_declare_aligned_stack_constructed_variable(StorageIndex, tmp, n, 0);
1057 typename IndexVector::AlignedMapType insertionLocations(tmp, n);
1058 insertionLocations.setConstant(kEmptyIndexVal);
1060 Index deferredInsertions = 0;
1063 for (Index j = 0; j < n; j++) {
1064 Index begin = m_outerIndex[j];
1065 Index end = isCompressed() ? m_outerIndex[j + 1] : begin + m_innerNonZeros[j];
1066 Index capacity = m_outerIndex[j + 1] - end;
1067 Index dst = m_data.searchLowerIndex(begin, end, j);
1069 if (dst != end && m_data.index(dst) == StorageIndex(j))
1070 assignFunc.assignCoeff(m_data.value(dst), diaEval.coeff(j));
1072 else if (dst == end && capacity > 0)
1073 assignFunc.assignCoeff(insertBackUncompressed(j, j), diaEval.coeff(j));
1076 insertionLocations.coeffRef(j) = StorageIndex(dst);
1077 deferredInsertions++;
1079 if (capacity == 0) shift++;
1083 if (deferredInsertions > 0) {
1084 m_data.resize(m_data.size() + shift);
1085 Index copyEnd = isCompressed() ? m_outerIndex[m_outerSize]
1086 : m_outerIndex[m_outerSize - 1] + m_innerNonZeros[m_outerSize - 1];
1087 for (Index j = m_outerSize - 1; deferredInsertions > 0; j--) {
1088 Index begin = m_outerIndex[j];
1089 Index end = isCompressed() ? m_outerIndex[j + 1] : begin + m_innerNonZeros[j];
1090 Index capacity = m_outerIndex[j + 1] - end;
1092 bool doInsertion = insertionLocations(j) >= 0;
1093 bool breakUpCopy = doInsertion && (capacity > 0);
1099 Index copyBegin = m_outerIndex[j + 1];
1102 internal::move_or_copy_backward(valuePtr() + copyBegin, valuePtr() + copyEnd,
1103 valuePtr() + copyEnd + shift);
1104 std::move_backward(innerIndexPtr() + copyBegin, innerIndexPtr() + copyEnd,
1105 innerIndexPtr() + copyEnd + shift);
1110 m_outerIndex[j + 1] += shift;
1114 if (capacity > 0) shift++;
1116 Index copyBegin = insertionLocations(j);
1117 internal::move_or_copy_backward(valuePtr() + copyBegin, valuePtr() + copyEnd, valuePtr() + copyEnd + shift);
1118 std::move_backward(innerIndexPtr() + copyBegin, innerIndexPtr() + copyEnd,
1119 innerIndexPtr() + copyEnd + shift);
1120 Index dst = copyBegin + shift - 1;
1121 m_data.index(dst) = StorageIndex(j);
1122 m_data.value(dst) = Scalar(0);
1123 assignFunc.assignCoeff(m_data.value(dst), diaEval.coeff(j));
1124 if (!isCompressed()) m_innerNonZeros[j]++;
1126 deferredInsertions--;
1127 copyEnd = copyBegin;
1131 eigen_assert((shift == 0) && (deferredInsertions == 0));
1137 EIGEN_STRONG_INLINE Scalar& insertAtByOuterInner(Index outer, Index inner, Index dst);
1138 Scalar& insertCompressedAtByOuterInner(Index outer, Index inner, Index dst);
1139 Scalar& insertUncompressedAtByOuterInner(Index outer, Index inner, Index dst);
1142 EIGEN_STATIC_ASSERT(NumTraits<StorageIndex>::IsSigned, THE_INDEX_TYPE_MUST_BE_A_SIGNED_TYPE)
1143 EIGEN_STATIC_ASSERT((Options & (ColMajor | RowMajor)) == Options, INVALID_MATRIX_TEMPLATE_PARAMETERS)
1145 struct default_prunning_func {
1146 default_prunning_func(
const Scalar& ref,
const RealScalar& eps) : reference(ref), epsilon(eps) {}
1147 inline bool operator()(
const Index&,
const Index&,
const Scalar& value)
const {
1148 return !internal::isMuchSmallerThan(value, reference, epsilon);
1159template <
typename InputIterator,
typename SparseMatrixType,
typename DupFunctor>
1160void set_from_triplets(
const InputIterator& begin,
const InputIterator& end, SparseMatrixType& mat,
1161 DupFunctor dup_func) {
1162 constexpr bool IsRowMajor = SparseMatrixType::IsRowMajor;
1163 using StorageIndex =
typename SparseMatrixType::StorageIndex;
1164 using IndexMap =
typename VectorX<StorageIndex>::AlignedMapType;
1165 using TransposedSparseMatrix =
1166 SparseMatrix<typename SparseMatrixType::Scalar, IsRowMajor ? ColMajor : RowMajor, StorageIndex>;
1171 mat.makeCompressed();
1179 TransposedSparseMatrix trmat(mat.rows(), mat.cols());
1183 for (InputIterator it(begin); it != end; ++it) {
1184 eigen_assert(it->row() >= 0 && it->row() < mat.rows() && it->col() >= 0 && it->col() < mat.cols());
1185 StorageIndex j = convert_index<StorageIndex>(IsRowMajor ? it->col() : it->row());
1186 eigen_assert(nonZeros < NumTraits<StorageIndex>::highest() &&
1187 "non-zero count exceeds StorageIndex range, use a wider StorageIndex (e.g. int64_t)");
1188 if (nonZeros >= NumTraits<StorageIndex>::highest()) internal::throw_std_bad_alloc();
1189 trmat.outerIndexPtr()[j + 1]++;
1193 std::partial_sum(trmat.outerIndexPtr(), trmat.outerIndexPtr() + trmat.outerSize() + 1, trmat.outerIndexPtr());
1194 eigen_assert(nonZeros == trmat.outerIndexPtr()[trmat.outerSize()]);
1195 trmat.resizeNonZeros(nonZeros);
1198 ei_declare_aligned_stack_constructed_variable(StorageIndex, tmp, numext::maxi(mat.innerSize(), mat.outerSize()), 0);
1199 smart_copy(trmat.outerIndexPtr(), trmat.outerIndexPtr() + trmat.outerSize(), tmp);
1202 for (InputIterator it(begin); it != end; ++it) {
1203 StorageIndex j = convert_index<StorageIndex>(IsRowMajor ? it->col() : it->row());
1204 StorageIndex i = convert_index<StorageIndex>(IsRowMajor ? it->row() : it->col());
1205 StorageIndex k = tmp[j];
1206 trmat.data().index(k) = i;
1207 trmat.data().value(k) = it->value();
1211 IndexMap wi(tmp, trmat.innerSize());
1212 trmat.collapseDuplicates(wi, dup_func);
1218template <
typename InputIterator,
typename SparseMatrixType,
typename DupFunctor>
1219void set_from_triplets_sorted(
const InputIterator& begin,
const InputIterator& end, SparseMatrixType& mat,
1220 DupFunctor dup_func) {
1221 constexpr bool IsRowMajor = SparseMatrixType::IsRowMajor;
1222 using StorageIndex =
typename SparseMatrixType::StorageIndex;
1227 mat.makeCompressed();
1231 constexpr StorageIndex kEmptyIndexValue(-1);
1233 mat.resize(mat.rows(), mat.cols());
1235 StorageIndex previous_j = kEmptyIndexValue;
1236 StorageIndex previous_i = kEmptyIndexValue;
1239 for (InputIterator it(begin); it != end; ++it) {
1240 eigen_assert(it->row() >= 0 && it->row() < mat.rows() && it->col() >= 0 && it->col() < mat.cols());
1241 StorageIndex j = convert_index<StorageIndex>(IsRowMajor ? it->row() : it->col());
1242 StorageIndex i = convert_index<StorageIndex>(IsRowMajor ? it->col() : it->row());
1243 eigen_assert(j > previous_j || (j == previous_j && i >= previous_i));
1245 bool duplicate = (previous_j == j) && (previous_i == i);
1247 eigen_assert(nonZeros < NumTraits<StorageIndex>::highest() &&
1248 "non-zero count exceeds StorageIndex range, use a wider StorageIndex (e.g. int64_t)");
1249 if (nonZeros >= NumTraits<StorageIndex>::highest()) internal::throw_std_bad_alloc();
1251 mat.outerIndexPtr()[j + 1]++;
1258 std::partial_sum(mat.outerIndexPtr(), mat.outerIndexPtr() + mat.outerSize() + 1, mat.outerIndexPtr());
1259 eigen_assert(nonZeros == mat.outerIndexPtr()[mat.outerSize()]);
1260 mat.resizeNonZeros(nonZeros);
1262 previous_i = kEmptyIndexValue;
1263 previous_j = kEmptyIndexValue;
1265 for (InputIterator it(begin); it != end; ++it) {
1266 StorageIndex j = convert_index<StorageIndex>(IsRowMajor ? it->row() : it->col());
1267 StorageIndex i = convert_index<StorageIndex>(IsRowMajor ? it->col() : it->row());
1268 bool duplicate = (previous_j == j) && (previous_i == i);
1270 mat.data().value(back - 1) = dup_func(mat.data().value(back - 1), it->value());
1273 mat.data().index(back) = i;
1274 mat.data().value(back) = it->value();
1280 eigen_assert(back == nonZeros);
1286template <
typename DupFunctor,
typename LhsScalar,
typename RhsScalar = LhsScalar>
1287struct scalar_disjunction_op {
1288 using result_type =
typename result_of<DupFunctor(LhsScalar, RhsScalar)>::type;
1289 scalar_disjunction_op(
const DupFunctor& op) : m_functor(op) {}
1290 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator()(
const LhsScalar& a,
const RhsScalar& b)
const {
1291 return m_functor(a, b);
1293 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const DupFunctor& functor()
const {
return m_functor; }
1294 const DupFunctor& m_functor;
1297template <
typename DupFunctor,
typename LhsScalar,
typename RhsScalar>
1298struct functor_traits<scalar_disjunction_op<DupFunctor, LhsScalar, RhsScalar>> :
public functor_traits<DupFunctor> {};
1301template <
typename InputIterator,
typename SparseMatrixType,
typename DupFunctor>
1302void insert_from_triplets(
const InputIterator& begin,
const InputIterator& end, SparseMatrixType& mat,
1303 DupFunctor dup_func) {
1304 using Scalar =
typename SparseMatrixType::Scalar;
1306 CwiseBinaryOp<scalar_disjunction_op<DupFunctor, Scalar>,
const SparseMatrixType,
const SparseMatrixType>;
1309 SparseMatrixType trips(mat.rows(), mat.cols());
1310 set_from_triplets(begin, end, trips, dup_func);
1312 SrcXprType src = mat.binaryExpr(trips, scalar_disjunction_op<DupFunctor, Scalar>(dup_func));
1314 assign_sparse_to_sparse<SparseMatrixType, SrcXprType>(mat, src);
1318template <
typename InputIterator,
typename SparseMatrixType,
typename DupFunctor>
1319void insert_from_triplets_sorted(
const InputIterator& begin,
const InputIterator& end, SparseMatrixType& mat,
1320 DupFunctor dup_func) {
1321 using Scalar =
typename SparseMatrixType::Scalar;
1323 CwiseBinaryOp<scalar_disjunction_op<DupFunctor, Scalar>,
const SparseMatrixType,
const SparseMatrixType>;
1327 SparseMatrixType trips(mat.rows(), mat.cols());
1328 set_from_triplets_sorted(begin, end, trips, dup_func);
1330 SrcXprType src = mat.binaryExpr(trips, scalar_disjunction_op<DupFunctor, Scalar>(dup_func));
1332 assign_sparse_to_sparse<SparseMatrixType, SrcXprType>(mat, src);
1374template <
typename Scalar,
int Options_,
typename StorageIndex_>
1375template <
typename InputIterators>
1377 const InputIterators& end) {
1378 internal::set_from_triplets<InputIterators, SparseMatrix<Scalar, Options_, StorageIndex_>>(
1379 begin, end, *
this, internal::scalar_sum_op<Scalar, Scalar>());
1391template <
typename Scalar,
int Options_,
typename StorageIndex_>
1392template <
typename InputIterators,
typename DupFunctor>
1394 const InputIterators& end, DupFunctor dup_func) {
1395 internal::set_from_triplets<InputIterators, SparseMatrix<Scalar, Options_, StorageIndex_>, DupFunctor>(
1396 begin, end, *
this, dup_func);
1403template <
typename Scalar,
int Options_,
typename StorageIndex_>
1404template <
typename InputIterators>
1406 const InputIterators& end) {
1407 internal::set_from_triplets_sorted<InputIterators, SparseMatrix<Scalar, Options_, StorageIndex_>>(
1408 begin, end, *
this, internal::scalar_sum_op<Scalar, Scalar>());
1420template <
typename Scalar,
int Options_,
typename StorageIndex_>
1421template <
typename InputIterators,
typename DupFunctor>
1423 const InputIterators& end,
1424 DupFunctor dup_func) {
1425 internal::set_from_triplets_sorted<InputIterators, SparseMatrix<Scalar, Options_, StorageIndex_>, DupFunctor>(
1426 begin, end, *
this, dup_func);
1467template <
typename Scalar,
int Options_,
typename StorageIndex_>
1468template <
typename InputIterators>
1470 const InputIterators& end) {
1471 internal::insert_from_triplets<InputIterators, SparseMatrix<Scalar, Options_, StorageIndex_>>(
1472 begin, end, *
this, internal::scalar_sum_op<Scalar, Scalar>());
1484template <
typename Scalar,
int Options_,
typename StorageIndex_>
1485template <
typename InputIterators,
typename DupFunctor>
1487 const InputIterators& end, DupFunctor dup_func) {
1488 internal::insert_from_triplets<InputIterators, SparseMatrix<Scalar, Options_, StorageIndex_>, DupFunctor>(
1489 begin, end, *
this, dup_func);
1496template <
typename Scalar,
int Options_,
typename StorageIndex_>
1497template <
typename InputIterators>
1499 const InputIterators& end) {
1500 internal::insert_from_triplets_sorted<InputIterators, SparseMatrix<Scalar, Options_, StorageIndex_>>(
1501 begin, end, *
this, internal::scalar_sum_op<Scalar, Scalar>());
1513template <
typename Scalar,
int Options_,
typename StorageIndex_>
1514template <
typename InputIterators,
typename DupFunctor>
1516 const InputIterators& end,
1517 DupFunctor dup_func) {
1518 internal::insert_from_triplets_sorted<InputIterators, SparseMatrix<Scalar, Options_, StorageIndex_>, DupFunctor>(
1519 begin, end, *
this, dup_func);
1523template <
typename Scalar_,
int Options_,
typename StorageIndex_>
1524template <
typename Derived,
typename DupFunctor>
1525void SparseMatrix<Scalar_, Options_, StorageIndex_>::collapseDuplicates(
DenseBase<Derived>& wi, DupFunctor dup_func) {
1529 eigen_assert(wi.size() == m_innerSize);
1530 constexpr StorageIndex kEmptyIndexValue(-1);
1532 StorageIndex count = 0;
1533 const bool is_compressed = isCompressed();
1535 for (Index j = 0; j < m_outerSize; ++j) {
1536 const StorageIndex newBegin = count;
1537 const StorageIndex end = is_compressed ? m_outerIndex[j + 1] : m_outerIndex[j] + m_innerNonZeros[j];
1538 for (StorageIndex k = m_outerIndex[j]; k < end; ++k) {
1539 StorageIndex i = m_data.index(k);
1540 if (wi(i) >= newBegin) {
1543 m_data.value(wi(i)) = dup_func(m_data.value(wi(i)), m_data.value(k));
1548 if (std::is_trivially_copyable<Scalar>::value || count != k) {
1549 m_data.index(count) = i;
1550 m_data.value(count) = internal::move_if_assignable(m_data.value(k));
1556 m_outerIndex[j] = newBegin;
1558 m_outerIndex[m_outerSize] = count;
1559 m_data.resize(count);
1562 internal::conditional_aligned_delete_auto<StorageIndex, true>(m_innerNonZeros, m_outerSize);
1563 m_innerNonZeros = 0;
1567template <
typename Scalar,
int Options_,
typename StorageIndex_>
1568template <
typename OtherDerived>
1569EIGEN_DONT_INLINE SparseMatrix<Scalar, Options_, StorageIndex_>&
1570SparseMatrix<Scalar, Options_, StorageIndex_>::operator=(
const SparseMatrixBase<OtherDerived>& other) {
1571 EIGEN_STATIC_ASSERT(
1572 (std::is_same<Scalar, typename OtherDerived::Scalar>::value),
1573 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
1575#ifdef EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
1576 EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
1579 const bool needToTranspose = (Flags &
RowMajorBit) != (internal::evaluator<OtherDerived>::Flags & RowMajorBit);
1580 if (needToTranspose) {
1581#ifdef EIGEN_SPARSE_TRANSPOSED_COPY_PLUGIN
1582 EIGEN_SPARSE_TRANSPOSED_COPY_PLUGIN
1589 typename internal::nested_eval<OtherDerived, 2, typename internal::plain_matrix_type<OtherDerived>::type>::type;
1590 using OtherCopy_ = internal::remove_all_t<OtherCopy>;
1591 using OtherCopyEval = internal::evaluator<OtherCopy_>;
1592 OtherCopy otherCopy(other.derived());
1593 OtherCopyEval otherCopyEval(otherCopy);
1595 SparseMatrix dest(other.rows(), other.cols());
1600 for (Index j = 0; j < otherCopy.outerSize(); ++j)
1601 for (
typename OtherCopyEval::InnerIterator it(otherCopyEval, j); it; ++it) ++dest.m_outerIndex[it.index()];
1605 IndexVector positions(dest.outerSize());
1606 for (Index j = 0; j < dest.outerSize(); ++j) {
1607 StorageIndex tmp = dest.m_outerIndex[j];
1608 dest.m_outerIndex[j] = internal::convert_index<StorageIndex>(count);
1609 positions[j] = internal::convert_index<StorageIndex>(count);
1612 dest.m_outerIndex[dest.outerSize()] = internal::convert_index<StorageIndex>(count);
1614 dest.m_data.resize(count);
1616 for (Index j = 0; j < otherCopy.outerSize(); ++j) {
1617 for (
typename OtherCopyEval::InnerIterator it(otherCopyEval, j); it; ++it) {
1618 Index pos = internal::convert_index<Index>(positions.coeff(it.index()));
1619 positions.coeffRef(it.index()) = internal::convert_index<StorageIndex>(pos + 1);
1620 dest.m_data.index(pos) = internal::convert_index<StorageIndex>(j);
1621 dest.m_data.value(pos) = it.value();
1627 if (other.isRValue()) {
1628 initAssignment(other.derived());
1631 return Base::operator=(other.derived());
1635template <
typename Scalar_,
int Options_,
typename StorageIndex_>
1636inline typename SparseMatrix<Scalar_, Options_, StorageIndex_>::Scalar&
1638 return insertByOuterInner(IsRowMajor ?
row :
col, IsRowMajor ?
col :
row);
1641template <
typename Scalar_,
int Options_,
typename StorageIndex_>
1642EIGEN_STRONG_INLINE
typename SparseMatrix<Scalar_, Options_, StorageIndex_>::Scalar&
1643SparseMatrix<Scalar_, Options_, StorageIndex_>::insertAtByOuterInner(Index outer, Index inner, Index dst) {
1646 return insertUncompressedAtByOuterInner(outer, inner, dst);
1649template <
typename Scalar_,
int Options_,
typename StorageIndex_>
1650EIGEN_DEPRECATED EIGEN_DONT_INLINE
typename SparseMatrix<Scalar_, Options_, StorageIndex_>::Scalar&
1651SparseMatrix<Scalar_, Options_, StorageIndex_>::insertUncompressed(Index row, Index col) {
1652 eigen_assert(!isCompressed());
1653 Index outer = IsRowMajor ? row : col;
1654 Index inner = IsRowMajor ? col : row;
1655 Index start = m_outerIndex[outer];
1656 Index end = start + m_innerNonZeros[outer];
1657 Index dst = start == end ? end : m_data.searchLowerIndex(start, end, inner);
1659 Index capacity = m_outerIndex[outer + 1] - end;
1662 m_innerNonZeros[outer]++;
1663 m_data.index(end) = StorageIndex(inner);
1664 m_data.value(end) = Scalar(0);
1665 return m_data.value(end);
1668 eigen_assert((dst == end || m_data.index(dst) != inner) &&
1669 "you cannot insert an element that already exists, you must call coeffRef to this end");
1670 return insertUncompressedAtByOuterInner(outer, inner, dst);
1673template <
typename Scalar_,
int Options_,
typename StorageIndex_>
1674EIGEN_DEPRECATED EIGEN_DONT_INLINE
typename SparseMatrix<Scalar_, Options_, StorageIndex_>::Scalar&
1675SparseMatrix<Scalar_, Options_, StorageIndex_>::insertCompressed(Index row, Index col) {
1676 eigen_assert(isCompressed());
1677 Index outer = IsRowMajor ? row : col;
1678 Index inner = IsRowMajor ? col : row;
1679 Index start = m_outerIndex[outer];
1680 Index end = m_outerIndex[outer + 1];
1681 Index dst = start == end ? end : m_data.searchLowerIndex(start, end, inner);
1682 eigen_assert((dst == end || m_data.index(dst) != inner) &&
1683 "you cannot insert an element that already exists, you must call coeffRef to this end");
1684 return insertCompressedAtByOuterInner(outer, inner, dst);
1687template <
typename Scalar_,
int Options_,
typename StorageIndex_>
1688typename SparseMatrix<Scalar_, Options_, StorageIndex_>::Scalar&
1689SparseMatrix<Scalar_, Options_, StorageIndex_>::insertCompressedAtByOuterInner(Index outer, Index inner, Index dst) {
1690 eigen_assert(isCompressed());
1693 if (m_data.allocatedSize() <= m_data.size()) {
1696 Index minReserve = 32;
1697 Index reserveSize = numext::maxi(minReserve, m_data.allocatedSize());
1698 m_data.reserve(reserveSize);
1700 m_data.resize(m_data.size() + 1);
1701 Index end = m_outerIndex[m_outerSize];
1703 internal::move_or_copy_backward(valuePtr() + dst, valuePtr() + end, valuePtr() + end + 1);
1704 std::move_backward(innerIndexPtr() + dst, innerIndexPtr() + end, innerIndexPtr() + end + 1);
1707 for (Index j = outer; j < m_outerSize; j++) m_outerIndex[j + 1]++;
1709 m_data.index(dst) = StorageIndex(inner);
1710 m_data.value(dst) = Scalar(0);
1712 return m_data.value(dst);
1715template <
typename Scalar_,
int Options_,
typename StorageIndex_>
1716typename SparseMatrix<Scalar_, Options_, StorageIndex_>::Scalar&
1717SparseMatrix<Scalar_, Options_, StorageIndex_>::insertUncompressedAtByOuterInner(Index outer, Index inner, Index dst) {
1718 eigen_assert(!isCompressed());
1720 for (Index leftTarget = outer - 1, rightTarget = outer; (leftTarget >= 0) || (rightTarget < m_outerSize);) {
1721 if (rightTarget < m_outerSize) {
1722 Index start = internal::convert_index<Index>(m_outerIndex[rightTarget]);
1723 Index end = start + internal::convert_index<Index>(m_innerNonZeros[rightTarget]);
1724 Index nextStart = internal::convert_index<Index>(m_outerIndex[rightTarget + 1]);
1725 Index capacity = nextStart - end;
1728 internal::move_or_copy_backward(valuePtr() + dst, valuePtr() + end, valuePtr() + end + 1);
1729 std::move_backward(innerIndexPtr() + dst, innerIndexPtr() + end, innerIndexPtr() + end + 1);
1730 m_innerNonZeros[outer]++;
1731 for (Index j = outer; j < rightTarget; j++) m_outerIndex[j + 1]++;
1732 m_data.index(dst) = StorageIndex(inner);
1733 m_data.value(dst) = Scalar(0);
1734 return m_data.value(dst);
1738 if (leftTarget >= 0) {
1739 Index start = internal::convert_index<Index>(m_outerIndex[leftTarget]);
1740 Index end = start + internal::convert_index<Index>(m_innerNonZeros[leftTarget]);
1741 Index nextStart = internal::convert_index<Index>(m_outerIndex[leftTarget + 1]);
1742 Index capacity = nextStart - end;
1746 internal::move_or_copy(valuePtr() + nextStart, valuePtr() + dst, valuePtr() + nextStart - 1);
1747 std::move(innerIndexPtr() + nextStart, innerIndexPtr() + dst, innerIndexPtr() + nextStart - 1);
1748 m_innerNonZeros[outer]++;
1749 for (Index j = leftTarget; j < outer; j++) m_outerIndex[j + 1]--;
1750 m_data.index(dst - 1) = StorageIndex(inner);
1751 m_data.value(dst - 1) = Scalar(0);
1752 return m_data.value(dst - 1);
1761 Index dst_offset = dst - internal::convert_index<Index>(m_outerIndex[outer]);
1764 using WideIndex =
typename std::common_type<Index, StorageIndex>::type;
1765 const Index maxSize =
1766 static_cast<Index
>(numext::mini<WideIndex>(NumTraits<StorageIndex>::highest(), NumTraits<Index>::highest()));
1767 if (m_data.allocatedSize() == 0 && m_outerSize <= maxSize) {
1769 m_data.resize(m_outerSize);
1770 std::iota(m_outerIndex, m_outerIndex + m_outerSize + 1, StorageIndex(0));
1774 Index maxReserveSize = maxSize - m_data.size();
1775 if (maxReserveSize <= 0) internal::throw_std_bad_alloc();
1776 if (m_outerSize <= maxReserveSize) {
1778 reserveInnerVectors(IndexVector::Constant(m_outerSize, 1));
1783 Index reserveSize = numext::mini(maxReserveSize, numext::maxi<Index>(m_data.size(), 1));
1784 using ReserveSizesOp = internal::sparse_reserve_op<StorageIndex>;
1785 using ReserveSizesXpr = CwiseNullaryOp<ReserveSizesOp, IndexVector>;
1786 ReserveSizesXpr reserveSizesXpr(m_outerSize, 1, ReserveSizesOp(outer, m_outerSize, reserveSize));
1787 reserveInnerVectors(reserveSizesXpr);
1791 Index start = internal::convert_index<Index>(m_outerIndex[outer]);
1792 Index end = start + internal::convert_index<Index>(m_innerNonZeros[outer]);
1793 Index new_dst = start + dst_offset;
1794 internal::move_or_copy_backward(valuePtr() + new_dst, valuePtr() + end, valuePtr() + end + 1);
1795 std::move_backward(innerIndexPtr() + new_dst, innerIndexPtr() + end, innerIndexPtr() + end + 1);
1796 m_innerNonZeros[outer]++;
1797 m_data.index(new_dst) = StorageIndex(inner);
1798 m_data.value(new_dst) = Scalar(0);
1799 return m_data.value(new_dst);
1804template <
typename Scalar_,
int Options_,
typename StorageIndex_>
1805struct evaluator<SparseMatrix<Scalar_, Options_, StorageIndex_>>
1806 : evaluator<SparseCompressedBase<SparseMatrix<Scalar_, Options_, StorageIndex_>>> {
1807 using Base = evaluator<SparseCompressedBase<SparseMatrix<Scalar_, Options_, StorageIndex_>>>;
1808 using SparseMatrixType = SparseMatrix<Scalar_, Options_, StorageIndex_>;
1809 evaluator() =
default;
1810 explicit evaluator(
const SparseMatrixType& mat) : Base(mat) {}
1818template <
typename Scalar,
int Options,
typename StorageIndex>
1819class Serializer<SparseMatrix<Scalar, Options, StorageIndex>, void> {
1821 using SparseMat = SparseMatrix<Scalar, Options, StorageIndex>;
1824 typename SparseMat::Index rows;
1825 typename SparseMat::Index cols;
1828 Index inner_buffer_size;
1831 EIGEN_DEVICE_FUNC
size_t size(
const SparseMat& value)
const {
1833 std::size_t num_storage_indices = value.isCompressed() ? 0 : value.outerSize();
1835 num_storage_indices += value.outerSize() + 1;
1837 const StorageIndex inner_buffer_size = value.outerIndexPtr()[value.outerSize()];
1838 num_storage_indices += inner_buffer_size;
1840 std::size_t num_values = inner_buffer_size;
1841 return sizeof(Header) +
sizeof(Scalar) * num_values +
sizeof(StorageIndex) * num_storage_indices;
1844 EIGEN_DEVICE_FUNC uint8_t* serialize(uint8_t* dest, uint8_t* end,
const SparseMat& value) {
1845 if (EIGEN_PREDICT_FALSE(dest ==
nullptr))
return nullptr;
1846 if (EIGEN_PREDICT_FALSE(dest + size(value) > end))
return nullptr;
1848 const size_t header_bytes =
sizeof(Header);
1849 Header header = {value.rows(), value.cols(), value.isCompressed(), value.outerSize(),
1850 value.outerIndexPtr()[value.outerSize()]};
1851 EIGEN_USING_STD(memcpy)
1852 memcpy(dest, &header, header_bytes);
1853 dest += header_bytes;
1857 if (!header.compressed) {
1858 std::size_t data_bytes =
sizeof(StorageIndex) * header.outer_size;
1859 if (data_bytes != 0) memcpy(dest, value.innerNonZeroPtr(), data_bytes);
1864 std::size_t data_bytes =
sizeof(StorageIndex) * (header.outer_size + 1);
1865 memcpy(dest, value.outerIndexPtr(), data_bytes);
1869 data_bytes =
sizeof(StorageIndex) * header.inner_buffer_size;
1870 if (data_bytes != 0) memcpy(dest, value.innerIndexPtr(), data_bytes);
1874 data_bytes =
sizeof(Scalar) * header.inner_buffer_size;
1875 if (data_bytes != 0) memcpy(dest, value.valuePtr(), data_bytes);
1881 EIGEN_DEVICE_FUNC
const uint8_t* deserialize(
const uint8_t* src,
const uint8_t* end, SparseMat& value)
const {
1882 if (EIGEN_PREDICT_FALSE(src ==
nullptr))
return nullptr;
1883 if (EIGEN_PREDICT_FALSE(src +
sizeof(Header) > end))
return nullptr;
1885 const size_t header_bytes =
sizeof(Header);
1887 EIGEN_USING_STD(memcpy)
1888 memcpy(&header, src, header_bytes);
1889 src += header_bytes;
1893 unsigned char compressed_byte;
1894 memcpy(&compressed_byte, &header.compressed, 1);
1895 const bool compressed = compressed_byte == 1;
1896 numext::uint64_t remaining =
static_cast<numext::uint64_t
>(end - src);
1897 if (EIGEN_PREDICT_FALSE(
1898 compressed_byte > 1 || header.rows < 0 || header.cols < 0 ||
1899 header.outer_size != (SparseMat::IsRowMajor ? header.rows : header.cols) || header.inner_buffer_size < 0 ||
1900 header.inner_buffer_size > NumTraits<StorageIndex>::highest() ||
1901 !take(remaining, compressed ? 0 : header.outer_size,
sizeof(StorageIndex)) ||
1902 !take(remaining,
static_cast<numext::uint64_t
>(header.outer_size) + 1,
sizeof(StorageIndex)) ||
1903 !take(remaining, header.inner_buffer_size,
sizeof(StorageIndex)) ||
1904 !take(remaining, header.inner_buffer_size,
sizeof(Scalar))))
1908 value.resize(header.rows, header.cols);
1910 value.makeCompressed();
1916 value.data().resize(header.inner_buffer_size);
1921 std::size_t data_bytes =
sizeof(StorageIndex) * header.outer_size;
1922 if (data_bytes != 0) {
1923 memcpy(value.innerNonZeroPtr(), src, data_bytes);
1929 std::size_t data_bytes =
sizeof(StorageIndex) * (header.outer_size + 1);
1930 memcpy(value.outerIndexPtr(), src, data_bytes);
1934 data_bytes =
sizeof(StorageIndex) * header.inner_buffer_size;
1935 if (data_bytes != 0) {
1936 memcpy(value.innerIndexPtr(), src, data_bytes);
1941 data_bytes =
sizeof(Scalar) * header.inner_buffer_size;
1942 if (data_bytes != 0) {
1943 memcpy(value.valuePtr(), src, data_bytes);
1947 if (EIGEN_PREDICT_FALSE(!valid_indices(value))) {
1956 EIGEN_DEVICE_FUNC
static bool take(numext::uint64_t& remaining, numext::uint64_t count, std::size_t elem_bytes) {
1957 if (count > remaining / elem_bytes)
return false;
1958 remaining -= count * elem_bytes;
1965 EIGEN_DEVICE_FUNC
static bool valid_indices(
const SparseMat& value) {
1966 const StorageIndex* outer = value.outerIndexPtr();
1967 const StorageIndex* inner_non_zeros = value.innerNonZeroPtr();
1968 const StorageIndex* inner = value.innerIndexPtr();
1969 const Index buffer_size = value.data().size();
1970 if (outer[0] != 0 || outer[value.outerSize()] != buffer_size)
return false;
1971 for (Index j = 0; j < value.outerSize(); ++j) {
1972 if (outer[j + 1] < outer[j] || outer[j + 1] > buffer_size)
return false;
1973 Index stop = outer[j + 1];
1974 if (inner_non_zeros !=
nullptr) {
1975 if (inner_non_zeros[j] < 0 || inner_non_zeros[j] > outer[j + 1] - outer[j])
return false;
1976 stop = outer[j] + inner_non_zeros[j];
1978 for (Index k = outer[j]; k < stop; ++k) {
1979 if (inner[k] < 0 || inner[k] >= value.innerSize())
return false;
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:45
Derived & setConstant(const Scalar &value)
Definition CwiseNullaryOp.h:333
Base class for diagonal matrices and expressions.
Definition DiagonalMatrix.h:34
const Derived & derived() const
Definition DiagonalMatrix.h:60
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition Diagonal.h:78
A matrix or vector expression mapping an existing array of data.
Definition Map.h:97
Common base class for sparse [compressed]-{row|column}-storage format.
Definition SparseCompressedBase.h:44
Index nonZeros() const
Definition SparseCompressedBase.h:65
SparseCompressedBase()=default
bool isCompressed() const
Definition SparseCompressedBase.h:115
Base class of any sparse matrices or sparse expressions.
Definition SparseMatrixBase.h:31
Index size() const
Definition SparseMatrixBase.h:187
typename NumTraits< Scalar >::Real RealScalar
Definition SparseMatrixBase.h:128
constexpr RowXpr row(Index i)
Definition SparseMatrixBase.h:1095
typename internal::traits< SparseMatrix< Scalar_, Options_, StorageIndex_ > >::StorageIndex StorageIndex
Definition SparseMatrixBase.h:45
constexpr ColXpr col(Index i)
Definition SparseMatrixBase.h:1082
@ Flags
Definition SparseMatrixBase.h:95
A versatile sparse matrix representation.
Definition SparseMatrix.h:121
Scalar coeff(Index row, Index col) const
Definition SparseMatrix.h:212
void resize(Index rows, NoChange_t)
Definition SparseMatrix.h:794
const ConstDiagonalReturnType diagonal() const
Definition SparseMatrix.h:801
void swap(SparseMatrix &other)
Definition SparseMatrix.h:873
void insertFromSortedTriplets(const InputIterators &begin, const InputIterators &end, DupFunctor dup_func)
Definition SparseMatrix.h:1515
const StorageIndex * innerIndexPtr() const
Definition SparseMatrix.h:181
void setZero()
Definition SparseMatrix.h:305
bool isCompressed() const
Definition SparseCompressedBase.h:115
Index cols() const
Definition SparseMatrix.h:162
StorageIndex * innerIndexPtr()
Definition SparseMatrix.h:185
SparseMatrix(const ReturnByValue< OtherDerived > &other)
Copy constructor with in-place evaluation.
Definition SparseMatrix.h:858
void setFromSortedTriplets(const InputIterators &begin, const InputIterators &end)
Definition SparseMatrix.h:1405
void setFromTriplets(const InputIterators &begin, const InputIterators &end, DupFunctor dup_func)
Definition SparseMatrix.h:1393
Index outerSize() const
Definition SparseMatrix.h:167
SparseMatrix()
Definition SparseMatrix.h:810
void uncompress()
Definition SparseMatrix.h:644
void insertFromSortedTriplets(const InputIterators &begin, const InputIterators &end)
Definition SparseMatrix.h:1498
const Scalar * valuePtr() const
Definition SparseMatrix.h:172
void makeCompressed()
Definition SparseMatrix.h:607
void setFromSortedTriplets(const InputIterators &begin, const InputIterators &end, DupFunctor dup_func)
Definition SparseMatrix.h:1422
SparseMatrix(const SparseSelfAdjointView< OtherDerived, UpLo > &other)
Definition SparseMatrix.h:837
void resize(Index rows, Index cols)
Definition SparseMatrix.h:772
void conservativeResize(Index rows, NoChange_t)
Definition SparseMatrix.h:763
Index rows() const
Definition SparseMatrix.h:160
SparseMatrix(const SparseMatrix &other)
Definition SparseMatrix.h:851
StorageIndex * innerNonZeroPtr()
Definition SparseMatrix.h:203
void setFromTriplets(const InputIterators &begin, const InputIterators &end)
Definition SparseMatrix.h:1376
SparseMatrix(const DiagonalBase< OtherDerived > &other)
Copy constructor with in-place evaluation.
Definition SparseMatrix.h:866
void setIdentity()
Definition SparseMatrix.h:885
Index innerSize() const
Definition SparseMatrix.h:165
SparseMatrix(Index rows, Index cols)
Definition SparseMatrix.h:813
StorageIndex * outerIndexPtr()
Definition SparseMatrix.h:194
const StorageIndex * innerNonZeroPtr() const
Definition SparseMatrix.h:199
void conservativeResize(Index rows, Index cols)
Definition SparseMatrix.h:713
void insertFromTriplets(const InputIterators &begin, const InputIterators &end, DupFunctor dup_func)
Definition SparseMatrix.h:1486
void prune(const Scalar &reference, const RealScalar &epsilon=NumTraits< RealScalar >::dummy_precision())
Definition SparseMatrix.h:656
const StorageIndex * outerIndexPtr() const
Definition SparseMatrix.h:190
SparseMatrix(const SparseMatrixBase< OtherDerived > &other)
Definition SparseMatrix.h:819
friend void swap(SparseMatrix &a, SparseMatrix &b)
Definition SparseMatrix.h:881
~SparseMatrix()
Definition SparseMatrix.h:972
Scalar * valuePtr()
Definition SparseMatrix.h:176
void prune(const KeepFunc &keep=KeepFunc())
Definition SparseMatrix.h:669
void conservativeResize(NoChange_t, Index cols)
Definition SparseMatrix.h:760
Scalar sum() const
Definition SparseRedux.h:32
void reserve(Index reserveSize)
Definition SparseMatrix.h:317
Scalar & coeffRef(Index row, Index col)
Definition SparseMatrix.h:276
Scalar & insert(Index row, Index col)
Definition SparseMatrix.h:1637
void reserve(const SizesType &reserveSizes)
DiagonalReturnType diagonal()
Definition SparseMatrix.h:807
void resize(NoChange_t, Index cols)
Definition SparseMatrix.h:791
Scalar & findOrInsertCoeff(Index row, Index col, bool *inserted)
Definition SparseMatrix.h:232
void insertFromTriplets(const InputIterators &begin, const InputIterators &end)
Definition SparseMatrix.h:1469
SparseMatrix(SparseMatrix &&other)
Definition SparseMatrix.h:843
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
Definition SparseSelfAdjointView.h:53
a sparse vector class
Definition SparseVector.h:63
@ RowMajor
Definition Constants.h:321
constexpr unsigned int LvalueBit
Definition Constants.h:149
constexpr unsigned int RowMajorBit
Definition Constants.h:71
constexpr unsigned int CompressedAccessBit
Definition Constants.h:196
constexpr Derived & derived()
Definition EigenBase.h:50
constexpr Index size() const noexcept
Definition EigenBase.h:65
Eigen::Index Index
Definition EigenBase.h:44