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 Scalar =
typename SparseMatrixType::Scalar;
1164 using StorageIndex =
typename SparseMatrixType::StorageIndex;
1165 using IndexMap =
typename VectorX<StorageIndex>::AlignedMapType;
1166 using TransposedSparseMatrix = SparseMatrix<Scalar, IsRowMajor ? ColMajor : RowMajor, StorageIndex>;
1171 mat.makeCompressed();
1179 TransposedSparseMatrix trmat(mat.rows(), mat.cols());
1180 StorageIndex* outer = trmat.outerIndexPtr();
1184 for (InputIterator it(begin); it != end; ++it) {
1185 eigen_assert(it->row() >= 0 && it->row() < mat.rows() && it->col() >= 0 && it->col() < mat.cols());
1186 StorageIndex j = convert_index<StorageIndex>(IsRowMajor ? it->col() : it->row());
1187 eigen_assert(nonZeros < NumTraits<StorageIndex>::highest() &&
1188 "non-zero count exceeds StorageIndex range, use a wider StorageIndex (e.g. int64_t)");
1189 if (nonZeros >= NumTraits<StorageIndex>::highest()) internal::throw_std_bad_alloc();
1195 StorageIndex start = 0;
1196 for (Index j = 0; j < trmat.outerSize(); ++j) {
1197 StorageIndex size = outer[j + 1];
1198 outer[j + 1] = start;
1201 eigen_assert(start == nonZeros);
1202 trmat.resizeNonZeros(nonZeros);
1204 StorageIndex* cursor = outer + 1;
1205 StorageIndex* indices = trmat.innerIndexPtr();
1206 Scalar* values = trmat.valuePtr();
1207 for (InputIterator it(begin); it != end; ++it) {
1208 StorageIndex j = convert_index<StorageIndex>(IsRowMajor ? it->col() : it->row());
1209 StorageIndex pos = cursor[j]++;
1210 indices[pos] = convert_index<StorageIndex>(IsRowMajor ? it->row() : it->col());
1211 values[pos] = it->value();
1214 ei_declare_aligned_stack_constructed_variable(StorageIndex, tmp, trmat.innerSize(), 0);
1215 IndexMap wi(tmp, trmat.innerSize());
1216 trmat.collapseDuplicates(wi, dup_func);
1222template <
typename InputIterator,
typename SparseMatrixType,
typename DupFunctor>
1223void set_from_triplets_sorted(
const InputIterator& begin,
const InputIterator& end, SparseMatrixType& mat,
1224 DupFunctor dup_func) {
1225 constexpr bool IsRowMajor = SparseMatrixType::IsRowMajor;
1226 using StorageIndex =
typename SparseMatrixType::StorageIndex;
1231 mat.makeCompressed();
1235 constexpr StorageIndex kEmptyIndexValue(-1);
1237 mat.resize(mat.rows(), mat.cols());
1239 StorageIndex previous_j = kEmptyIndexValue;
1240 StorageIndex previous_i = kEmptyIndexValue;
1243 for (InputIterator it(begin); it != end; ++it) {
1244 eigen_assert(it->row() >= 0 && it->row() < mat.rows() && it->col() >= 0 && it->col() < mat.cols());
1245 StorageIndex j = convert_index<StorageIndex>(IsRowMajor ? it->row() : it->col());
1246 StorageIndex i = convert_index<StorageIndex>(IsRowMajor ? it->col() : it->row());
1247 eigen_assert(j > previous_j || (j == previous_j && i >= previous_i));
1249 bool duplicate = (previous_j == j) && (previous_i == i);
1251 eigen_assert(nonZeros < NumTraits<StorageIndex>::highest() &&
1252 "non-zero count exceeds StorageIndex range, use a wider StorageIndex (e.g. int64_t)");
1253 if (nonZeros >= NumTraits<StorageIndex>::highest()) internal::throw_std_bad_alloc();
1255 mat.outerIndexPtr()[j + 1]++;
1262 std::partial_sum(mat.outerIndexPtr(), mat.outerIndexPtr() + mat.outerSize() + 1, mat.outerIndexPtr());
1263 eigen_assert(nonZeros == mat.outerIndexPtr()[mat.outerSize()]);
1264 mat.resizeNonZeros(nonZeros);
1266 previous_i = kEmptyIndexValue;
1267 previous_j = kEmptyIndexValue;
1269 for (InputIterator it(begin); it != end; ++it) {
1270 StorageIndex j = convert_index<StorageIndex>(IsRowMajor ? it->row() : it->col());
1271 StorageIndex i = convert_index<StorageIndex>(IsRowMajor ? it->col() : it->row());
1272 bool duplicate = (previous_j == j) && (previous_i == i);
1274 mat.data().value(back - 1) = dup_func(mat.data().value(back - 1), it->value());
1277 mat.data().index(back) = i;
1278 mat.data().value(back) = it->value();
1284 eigen_assert(back == nonZeros);
1290template <
typename DupFunctor,
typename LhsScalar,
typename RhsScalar = LhsScalar>
1291struct scalar_disjunction_op {
1292 using result_type =
typename result_of<DupFunctor(LhsScalar, RhsScalar)>::type;
1293 scalar_disjunction_op(
const DupFunctor& op) : m_functor(op) {}
1294 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator()(
const LhsScalar& a,
const RhsScalar& b)
const {
1295 return m_functor(a, b);
1297 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const DupFunctor& functor()
const {
return m_functor; }
1298 const DupFunctor& m_functor;
1301template <
typename DupFunctor,
typename LhsScalar,
typename RhsScalar>
1302struct functor_traits<scalar_disjunction_op<DupFunctor, LhsScalar, RhsScalar>> :
public functor_traits<DupFunctor> {};
1305template <
typename InputIterator,
typename SparseMatrixType,
typename DupFunctor>
1306void insert_from_triplets(
const InputIterator& begin,
const InputIterator& end, SparseMatrixType& mat,
1307 DupFunctor dup_func) {
1308 using Scalar =
typename SparseMatrixType::Scalar;
1310 CwiseBinaryOp<scalar_disjunction_op<DupFunctor, Scalar>,
const SparseMatrixType,
const SparseMatrixType>;
1313 SparseMatrixType trips(mat.rows(), mat.cols());
1314 set_from_triplets(begin, end, trips, dup_func);
1316 SrcXprType src = mat.binaryExpr(trips, scalar_disjunction_op<DupFunctor, Scalar>(dup_func));
1318 assign_sparse_to_sparse<SparseMatrixType, SrcXprType>(mat, src);
1322template <
typename InputIterator,
typename SparseMatrixType,
typename DupFunctor>
1323void insert_from_triplets_sorted(
const InputIterator& begin,
const InputIterator& end, SparseMatrixType& mat,
1324 DupFunctor dup_func) {
1325 using Scalar =
typename SparseMatrixType::Scalar;
1327 CwiseBinaryOp<scalar_disjunction_op<DupFunctor, Scalar>,
const SparseMatrixType,
const SparseMatrixType>;
1331 SparseMatrixType trips(mat.rows(), mat.cols());
1332 set_from_triplets_sorted(begin, end, trips, dup_func);
1334 SrcXprType src = mat.binaryExpr(trips, scalar_disjunction_op<DupFunctor, Scalar>(dup_func));
1336 assign_sparse_to_sparse<SparseMatrixType, SrcXprType>(mat, src);
1342template <typename Src, bool HasStorage = has_compressed_access<Src>::value>
1343class sparse_transposed_copy_source {
1345 explicit sparse_transposed_copy_source(
const Src& src) : m_src(src), m_eval(src) {}
1347 template <
typename DstIndex>
1348 void count(DstIndex* outer)
const {
1349 for (Index j = 0; j < m_src.outerSize(); ++j)
1350 for (
typename evaluator<Src>::InnerIterator it(m_eval, j); it; ++it) ++outer[it.index() + 1];
1353 template <
typename DstIndex,
typename Scalar>
1354 void scatter(DstIndex* cursor, DstIndex* indices, Scalar* values)
const {
1355 for (Index j = 0; j < m_src.outerSize(); ++j) {
1356 for (
typename evaluator<Src>::InnerIterator it(m_eval, j); it; ++it) {
1357 DstIndex pos = cursor[it.index()]++;
1359 indices[pos] = convert_index<DstIndex>(j);
1360 values[pos] = it.value();
1367 evaluator<Src> m_eval;
1373template <
typename Src>
1374class sparse_transposed_copy_source<Src, true> {
1376 explicit sparse_transposed_copy_source(
const Src& src)
1377 : m_outerSize(src.outerSize()),
1378 m_outer(src.outerIndexPtr()),
1379 m_innerNonZeros(src.innerNonZeroPtr()),
1380 m_inner(src.innerIndexPtr()),
1381 m_values(src.valuePtr()),
1382 m_vectorNonZeros(m_outer == nullptr ? src.nonZeros() : 0) {}
1384 template <
typename DstIndex>
1385 void count(DstIndex* outer)
const {
1386 if (m_outer ==
nullptr) {
1387 count(outer, 0, m_vectorNonZeros);
1388 }
else if (m_innerNonZeros ==
nullptr) {
1389 count(outer, m_outer[0], m_outer[m_outerSize]);
1391 for (Index j = 0; j < m_outerSize; ++j) count(outer, m_outer[j], m_outer[j] + m_innerNonZeros[j]);
1395 template <
typename DstIndex,
typename Scalar>
1396 void scatter(DstIndex* cursor, DstIndex* indices, Scalar* values)
const {
1397 if (m_outer ==
nullptr) {
1398 scatter(0, 0, m_vectorNonZeros, cursor, indices, values);
1401 for (Index j = 0; j < m_outerSize; ++j) {
1402 Index end = m_innerNonZeros ==
nullptr ? m_outer[j + 1] : m_outer[j] + m_innerNonZeros[j];
1403 scatter(j, m_outer[j], end, cursor, indices, values);
1408 template <
typename DstIndex>
1409 void count(DstIndex* outer, Index begin, Index end)
const {
1410 for (Index p = begin; p < end; ++p) ++outer[m_inner[p] + 1];
1414 template <
typename DstIndex,
typename Scalar>
1415 void scatter(Index j, Index begin, Index end, DstIndex* cursor, DstIndex* indices, Scalar* values)
const {
1417 for (Index p = begin; p < end; ++p) {
1418 DstIndex pos = cursor[m_inner[p]]++;
1419 indices[pos] = convert_index<DstIndex>(j);
1420 values[pos] = m_values[p];
1424 using SrcIndex =
typename Src::StorageIndex;
1426 const SrcIndex* m_outer;
1427 const SrcIndex* m_innerNonZeros;
1428 const SrcIndex* m_inner;
1429 const typename Src::Scalar* m_values;
1430 Index m_vectorNonZeros;
1472template <
typename Scalar,
int Options_,
typename StorageIndex_>
1473template <
typename InputIterators>
1475 const InputIterators& end) {
1476 internal::set_from_triplets<InputIterators, SparseMatrix<Scalar, Options_, StorageIndex_>>(
1477 begin, end, *
this, internal::scalar_sum_op<Scalar, Scalar>());
1489template <
typename Scalar,
int Options_,
typename StorageIndex_>
1490template <
typename InputIterators,
typename DupFunctor>
1492 const InputIterators& end, DupFunctor dup_func) {
1493 internal::set_from_triplets<InputIterators, SparseMatrix<Scalar, Options_, StorageIndex_>, DupFunctor>(
1494 begin, end, *
this, dup_func);
1501template <
typename Scalar,
int Options_,
typename StorageIndex_>
1502template <
typename InputIterators>
1504 const InputIterators& end) {
1505 internal::set_from_triplets_sorted<InputIterators, SparseMatrix<Scalar, Options_, StorageIndex_>>(
1506 begin, end, *
this, internal::scalar_sum_op<Scalar, Scalar>());
1518template <
typename Scalar,
int Options_,
typename StorageIndex_>
1519template <
typename InputIterators,
typename DupFunctor>
1521 const InputIterators& end,
1522 DupFunctor dup_func) {
1523 internal::set_from_triplets_sorted<InputIterators, SparseMatrix<Scalar, Options_, StorageIndex_>, DupFunctor>(
1524 begin, end, *
this, dup_func);
1565template <
typename Scalar,
int Options_,
typename StorageIndex_>
1566template <
typename InputIterators>
1568 const InputIterators& end) {
1569 internal::insert_from_triplets<InputIterators, SparseMatrix<Scalar, Options_, StorageIndex_>>(
1570 begin, end, *
this, internal::scalar_sum_op<Scalar, Scalar>());
1582template <
typename Scalar,
int Options_,
typename StorageIndex_>
1583template <
typename InputIterators,
typename DupFunctor>
1585 const InputIterators& end, DupFunctor dup_func) {
1586 internal::insert_from_triplets<InputIterators, SparseMatrix<Scalar, Options_, StorageIndex_>, DupFunctor>(
1587 begin, end, *
this, dup_func);
1594template <
typename Scalar,
int Options_,
typename StorageIndex_>
1595template <
typename InputIterators>
1597 const InputIterators& end) {
1598 internal::insert_from_triplets_sorted<InputIterators, SparseMatrix<Scalar, Options_, StorageIndex_>>(
1599 begin, end, *
this, internal::scalar_sum_op<Scalar, Scalar>());
1611template <
typename Scalar,
int Options_,
typename StorageIndex_>
1612template <
typename InputIterators,
typename DupFunctor>
1614 const InputIterators& end,
1615 DupFunctor dup_func) {
1616 internal::insert_from_triplets_sorted<InputIterators, SparseMatrix<Scalar, Options_, StorageIndex_>, DupFunctor>(
1617 begin, end, *
this, dup_func);
1621template <
typename Scalar_,
int Options_,
typename StorageIndex_>
1622template <
typename Derived,
typename DupFunctor>
1623void SparseMatrix<Scalar_, Options_, StorageIndex_>::collapseDuplicates(
DenseBase<Derived>& wi, DupFunctor dup_func) {
1627 eigen_assert(wi.size() == m_innerSize);
1628 constexpr StorageIndex kEmptyIndexValue(-1);
1630 StorageIndex count = 0;
1631 const bool is_compressed = isCompressed();
1633 for (Index j = 0; j < m_outerSize; ++j) {
1634 const StorageIndex newBegin = count;
1635 const StorageIndex end = is_compressed ? m_outerIndex[j + 1] : m_outerIndex[j] + m_innerNonZeros[j];
1636 for (StorageIndex k = m_outerIndex[j]; k < end; ++k) {
1637 StorageIndex i = m_data.index(k);
1638 if (wi(i) >= newBegin) {
1641 m_data.value(wi(i)) = dup_func(m_data.value(wi(i)), m_data.value(k));
1646 if (std::is_trivially_copyable<Scalar>::value || count != k) {
1647 m_data.index(count) = i;
1648 m_data.value(count) = internal::move_if_assignable(m_data.value(k));
1654 m_outerIndex[j] = newBegin;
1656 m_outerIndex[m_outerSize] = count;
1657 m_data.resize(count);
1660 internal::conditional_aligned_delete_auto<StorageIndex, true>(m_innerNonZeros, m_outerSize);
1661 m_innerNonZeros = 0;
1665template <
typename Scalar,
int Options_,
typename StorageIndex_>
1666template <
typename OtherDerived>
1667EIGEN_DONT_INLINE SparseMatrix<Scalar, Options_, StorageIndex_>&
1668SparseMatrix<Scalar, Options_, StorageIndex_>::operator=(
const SparseMatrixBase<OtherDerived>& other) {
1669 EIGEN_STATIC_ASSERT(
1670 (std::is_same<Scalar, typename OtherDerived::Scalar>::value),
1671 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
1673#ifdef EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
1674 EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
1677 const bool needToTranspose = (Flags &
RowMajorBit) != (internal::evaluator<OtherDerived>::Flags & RowMajorBit);
1678 if (needToTranspose) {
1679#ifdef EIGEN_SPARSE_TRANSPOSED_COPY_PLUGIN
1680 EIGEN_SPARSE_TRANSPOSED_COPY_PLUGIN
1687 typename internal::nested_eval<OtherDerived, 2, typename internal::plain_matrix_type<OtherDerived>::type>::type;
1688 using OtherCopy_ = internal::remove_all_t<OtherCopy>;
1689 OtherCopy otherCopy(other.derived());
1690 internal::sparse_transposed_copy_source<OtherCopy_> source(otherCopy);
1692 SparseMatrix dest(other.rows(), other.cols());
1693 StorageIndex* outer = dest.m_outerIndex;
1694 Index destOuterSize = dest.outerSize();
1699 source.count(outer);
1703 for (Index i = 0; i < destOuterSize; ++i) {
1704 Index size = outer[i + 1];
1705 outer[i + 1] = internal::convert_index<StorageIndex>(count);
1708 dest.m_data.resize(count);
1709 source.scatter(outer + 1, dest.m_data.indexPtr(), dest.m_data.valuePtr());
1713 if (other.isRValue()) {
1714 initAssignment(other.derived());
1717 return Base::operator=(other.derived());
1721template <
typename Scalar_,
int Options_,
typename StorageIndex_>
1722inline typename SparseMatrix<Scalar_, Options_, StorageIndex_>::Scalar&
1724 return insertByOuterInner(IsRowMajor ?
row :
col, IsRowMajor ?
col :
row);
1727template <
typename Scalar_,
int Options_,
typename StorageIndex_>
1728EIGEN_STRONG_INLINE
typename SparseMatrix<Scalar_, Options_, StorageIndex_>::Scalar&
1729SparseMatrix<Scalar_, Options_, StorageIndex_>::insertAtByOuterInner(Index outer, Index inner, Index dst) {
1732 return insertUncompressedAtByOuterInner(outer, inner, dst);
1735template <
typename Scalar_,
int Options_,
typename StorageIndex_>
1736EIGEN_DEPRECATED EIGEN_DONT_INLINE
typename SparseMatrix<Scalar_, Options_, StorageIndex_>::Scalar&
1737SparseMatrix<Scalar_, Options_, StorageIndex_>::insertUncompressed(Index row, Index col) {
1738 eigen_assert(!isCompressed());
1739 Index outer = IsRowMajor ? row : col;
1740 Index inner = IsRowMajor ? col : row;
1741 Index start = m_outerIndex[outer];
1742 Index end = start + m_innerNonZeros[outer];
1743 Index dst = start == end ? end : m_data.searchLowerIndex(start, end, inner);
1745 Index capacity = m_outerIndex[outer + 1] - end;
1748 m_innerNonZeros[outer]++;
1749 m_data.index(end) = StorageIndex(inner);
1750 m_data.value(end) = Scalar(0);
1751 return m_data.value(end);
1754 eigen_assert((dst == end || m_data.index(dst) != inner) &&
1755 "you cannot insert an element that already exists, you must call coeffRef to this end");
1756 return insertUncompressedAtByOuterInner(outer, inner, dst);
1759template <
typename Scalar_,
int Options_,
typename StorageIndex_>
1760EIGEN_DEPRECATED EIGEN_DONT_INLINE
typename SparseMatrix<Scalar_, Options_, StorageIndex_>::Scalar&
1761SparseMatrix<Scalar_, Options_, StorageIndex_>::insertCompressed(Index row, Index col) {
1762 eigen_assert(isCompressed());
1763 Index outer = IsRowMajor ? row : col;
1764 Index inner = IsRowMajor ? col : row;
1765 Index start = m_outerIndex[outer];
1766 Index end = m_outerIndex[outer + 1];
1767 Index dst = start == end ? end : m_data.searchLowerIndex(start, end, inner);
1768 eigen_assert((dst == end || m_data.index(dst) != inner) &&
1769 "you cannot insert an element that already exists, you must call coeffRef to this end");
1770 return insertCompressedAtByOuterInner(outer, inner, dst);
1773template <
typename Scalar_,
int Options_,
typename StorageIndex_>
1774typename SparseMatrix<Scalar_, Options_, StorageIndex_>::Scalar&
1775SparseMatrix<Scalar_, Options_, StorageIndex_>::insertCompressedAtByOuterInner(Index outer, Index inner, Index dst) {
1776 eigen_assert(isCompressed());
1779 if (m_data.allocatedSize() <= m_data.size()) {
1782 Index minReserve = 32;
1783 Index reserveSize = numext::maxi(minReserve, m_data.allocatedSize());
1784 m_data.reserve(reserveSize);
1786 m_data.resize(m_data.size() + 1);
1787 Index end = m_outerIndex[m_outerSize];
1789 internal::move_or_copy_backward(valuePtr() + dst, valuePtr() + end, valuePtr() + end + 1);
1790 std::move_backward(innerIndexPtr() + dst, innerIndexPtr() + end, innerIndexPtr() + end + 1);
1793 for (Index j = outer; j < m_outerSize; j++) m_outerIndex[j + 1]++;
1795 m_data.index(dst) = StorageIndex(inner);
1796 m_data.value(dst) = Scalar(0);
1798 return m_data.value(dst);
1801template <
typename Scalar_,
int Options_,
typename StorageIndex_>
1802typename SparseMatrix<Scalar_, Options_, StorageIndex_>::Scalar&
1803SparseMatrix<Scalar_, Options_, StorageIndex_>::insertUncompressedAtByOuterInner(Index outer, Index inner, Index dst) {
1804 eigen_assert(!isCompressed());
1806 for (Index leftTarget = outer - 1, rightTarget = outer; (leftTarget >= 0) || (rightTarget < m_outerSize);) {
1807 if (rightTarget < m_outerSize) {
1808 Index start = internal::convert_index<Index>(m_outerIndex[rightTarget]);
1809 Index end = start + internal::convert_index<Index>(m_innerNonZeros[rightTarget]);
1810 Index nextStart = internal::convert_index<Index>(m_outerIndex[rightTarget + 1]);
1811 Index capacity = nextStart - end;
1814 internal::move_or_copy_backward(valuePtr() + dst, valuePtr() + end, valuePtr() + end + 1);
1815 std::move_backward(innerIndexPtr() + dst, innerIndexPtr() + end, innerIndexPtr() + end + 1);
1816 m_innerNonZeros[outer]++;
1817 for (Index j = outer; j < rightTarget; j++) m_outerIndex[j + 1]++;
1818 m_data.index(dst) = StorageIndex(inner);
1819 m_data.value(dst) = Scalar(0);
1820 return m_data.value(dst);
1824 if (leftTarget >= 0) {
1825 Index start = internal::convert_index<Index>(m_outerIndex[leftTarget]);
1826 Index end = start + internal::convert_index<Index>(m_innerNonZeros[leftTarget]);
1827 Index nextStart = internal::convert_index<Index>(m_outerIndex[leftTarget + 1]);
1828 Index capacity = nextStart - end;
1832 internal::move_or_copy(valuePtr() + nextStart, valuePtr() + dst, valuePtr() + nextStart - 1);
1833 std::move(innerIndexPtr() + nextStart, innerIndexPtr() + dst, innerIndexPtr() + nextStart - 1);
1834 m_innerNonZeros[outer]++;
1835 for (Index j = leftTarget; j < outer; j++) m_outerIndex[j + 1]--;
1836 m_data.index(dst - 1) = StorageIndex(inner);
1837 m_data.value(dst - 1) = Scalar(0);
1838 return m_data.value(dst - 1);
1847 Index dst_offset = dst - internal::convert_index<Index>(m_outerIndex[outer]);
1850 using WideIndex =
typename std::common_type<Index, StorageIndex>::type;
1851 const Index maxSize =
1852 static_cast<Index
>(numext::mini<WideIndex>(NumTraits<StorageIndex>::highest(), NumTraits<Index>::highest()));
1853 if (m_data.allocatedSize() == 0 && m_outerSize <= maxSize) {
1855 m_data.resize(m_outerSize);
1856 std::iota(m_outerIndex, m_outerIndex + m_outerSize + 1, StorageIndex(0));
1860 Index maxReserveSize = maxSize - m_data.size();
1861 if (maxReserveSize <= 0) internal::throw_std_bad_alloc();
1862 if (m_outerSize <= maxReserveSize) {
1864 reserveInnerVectors(IndexVector::Constant(m_outerSize, 1));
1869 Index reserveSize = numext::mini(maxReserveSize, numext::maxi<Index>(m_data.size(), 1));
1870 using ReserveSizesOp = internal::sparse_reserve_op<StorageIndex>;
1871 using ReserveSizesXpr = CwiseNullaryOp<ReserveSizesOp, IndexVector>;
1872 ReserveSizesXpr reserveSizesXpr(m_outerSize, 1, ReserveSizesOp(outer, m_outerSize, reserveSize));
1873 reserveInnerVectors(reserveSizesXpr);
1877 Index start = internal::convert_index<Index>(m_outerIndex[outer]);
1878 Index end = start + internal::convert_index<Index>(m_innerNonZeros[outer]);
1879 Index new_dst = start + dst_offset;
1880 internal::move_or_copy_backward(valuePtr() + new_dst, valuePtr() + end, valuePtr() + end + 1);
1881 std::move_backward(innerIndexPtr() + new_dst, innerIndexPtr() + end, innerIndexPtr() + end + 1);
1882 m_innerNonZeros[outer]++;
1883 m_data.index(new_dst) = StorageIndex(inner);
1884 m_data.value(new_dst) = Scalar(0);
1885 return m_data.value(new_dst);
1890template <
typename Scalar_,
int Options_,
typename StorageIndex_>
1891struct evaluator<SparseMatrix<Scalar_, Options_, StorageIndex_>>
1892 : evaluator<SparseCompressedBase<SparseMatrix<Scalar_, Options_, StorageIndex_>>> {
1893 using Base = evaluator<SparseCompressedBase<SparseMatrix<Scalar_, Options_, StorageIndex_>>>;
1894 using SparseMatrixType = SparseMatrix<Scalar_, Options_, StorageIndex_>;
1895 evaluator() =
default;
1896 explicit evaluator(
const SparseMatrixType& mat) : Base(mat) {}
1904template <
typename Scalar,
int Options,
typename StorageIndex>
1905class Serializer<SparseMatrix<Scalar, Options, StorageIndex>, void> {
1907 using SparseMat = SparseMatrix<Scalar, Options, StorageIndex>;
1910 typename SparseMat::Index rows;
1911 typename SparseMat::Index cols;
1914 Index inner_buffer_size;
1917 EIGEN_DEVICE_FUNC
size_t size(
const SparseMat& value)
const {
1919 std::size_t num_storage_indices = value.isCompressed() ? 0 : value.outerSize();
1921 num_storage_indices += value.outerSize() + 1;
1923 const StorageIndex inner_buffer_size = value.outerIndexPtr()[value.outerSize()];
1924 num_storage_indices += inner_buffer_size;
1926 std::size_t num_values = inner_buffer_size;
1927 return sizeof(Header) +
sizeof(Scalar) * num_values +
sizeof(StorageIndex) * num_storage_indices;
1930 EIGEN_DEVICE_FUNC uint8_t* serialize(uint8_t* dest, uint8_t* end,
const SparseMat& value) {
1931 if (EIGEN_PREDICT_FALSE(dest ==
nullptr))
return nullptr;
1932 if (EIGEN_PREDICT_FALSE(dest + size(value) > end))
return nullptr;
1934 const size_t header_bytes =
sizeof(Header);
1935 Header header = {value.rows(), value.cols(), value.isCompressed(), value.outerSize(),
1936 value.outerIndexPtr()[value.outerSize()]};
1937 EIGEN_USING_STD(memcpy)
1938 memcpy(dest, &header, header_bytes);
1939 dest += header_bytes;
1943 if (!header.compressed) {
1944 std::size_t data_bytes =
sizeof(StorageIndex) * header.outer_size;
1945 if (data_bytes != 0) memcpy(dest, value.innerNonZeroPtr(), data_bytes);
1950 std::size_t data_bytes =
sizeof(StorageIndex) * (header.outer_size + 1);
1951 memcpy(dest, value.outerIndexPtr(), data_bytes);
1955 data_bytes =
sizeof(StorageIndex) * header.inner_buffer_size;
1956 if (data_bytes != 0) memcpy(dest, value.innerIndexPtr(), data_bytes);
1960 data_bytes =
sizeof(Scalar) * header.inner_buffer_size;
1961 if (data_bytes != 0) memcpy(dest, value.valuePtr(), data_bytes);
1967 EIGEN_DEVICE_FUNC
const uint8_t* deserialize(
const uint8_t* src,
const uint8_t* end, SparseMat& value)
const {
1968 if (EIGEN_PREDICT_FALSE(src ==
nullptr))
return nullptr;
1969 if (EIGEN_PREDICT_FALSE(src +
sizeof(Header) > end))
return nullptr;
1971 const size_t header_bytes =
sizeof(Header);
1973 EIGEN_USING_STD(memcpy)
1974 memcpy(&header, src, header_bytes);
1975 src += header_bytes;
1979 unsigned char compressed_byte;
1980 memcpy(&compressed_byte, &header.compressed, 1);
1981 const bool compressed = compressed_byte == 1;
1982 numext::uint64_t remaining =
static_cast<numext::uint64_t
>(end - src);
1983 if (EIGEN_PREDICT_FALSE(
1984 compressed_byte > 1 || header.rows < 0 || header.cols < 0 ||
1985 header.outer_size != (SparseMat::IsRowMajor ? header.rows : header.cols) || header.inner_buffer_size < 0 ||
1986 header.inner_buffer_size > NumTraits<StorageIndex>::highest() ||
1987 !take(remaining, compressed ? 0 : header.outer_size,
sizeof(StorageIndex)) ||
1988 !take(remaining,
static_cast<numext::uint64_t
>(header.outer_size) + 1,
sizeof(StorageIndex)) ||
1989 !take(remaining, header.inner_buffer_size,
sizeof(StorageIndex)) ||
1990 !take(remaining, header.inner_buffer_size,
sizeof(Scalar))))
1994 value.resize(header.rows, header.cols);
1996 value.makeCompressed();
2002 value.data().resize(header.inner_buffer_size);
2007 std::size_t data_bytes =
sizeof(StorageIndex) * header.outer_size;
2008 if (data_bytes != 0) {
2009 memcpy(value.innerNonZeroPtr(), src, data_bytes);
2015 std::size_t data_bytes =
sizeof(StorageIndex) * (header.outer_size + 1);
2016 memcpy(value.outerIndexPtr(), src, data_bytes);
2020 data_bytes =
sizeof(StorageIndex) * header.inner_buffer_size;
2021 if (data_bytes != 0) {
2022 memcpy(value.innerIndexPtr(), src, data_bytes);
2027 data_bytes =
sizeof(Scalar) * header.inner_buffer_size;
2028 if (data_bytes != 0) {
2029 memcpy(value.valuePtr(), src, data_bytes);
2033 if (EIGEN_PREDICT_FALSE(!valid_indices(value))) {
2042 EIGEN_DEVICE_FUNC
static bool take(numext::uint64_t& remaining, numext::uint64_t count, std::size_t elem_bytes) {
2043 if (count > remaining / elem_bytes)
return false;
2044 remaining -= count * elem_bytes;
2051 EIGEN_DEVICE_FUNC
static bool valid_indices(
const SparseMat& value) {
2052 const StorageIndex* outer = value.outerIndexPtr();
2053 const StorageIndex* inner_non_zeros = value.innerNonZeroPtr();
2054 const StorageIndex* inner = value.innerIndexPtr();
2055 const Index buffer_size = value.data().size();
2056 if (outer[0] != 0 || outer[value.outerSize()] != buffer_size)
return false;
2057 for (Index j = 0; j < value.outerSize(); ++j) {
2058 if (outer[j + 1] < outer[j] || outer[j + 1] > buffer_size)
return false;
2059 Index stop = outer[j + 1];
2060 if (inner_non_zeros !=
nullptr) {
2061 if (inner_non_zeros[j] < 0 || inner_non_zeros[j] > outer[j + 1] - outer[j])
return false;
2062 stop = outer[j] + inner_non_zeros[j];
2064 for (Index k = outer[j]; k < stop; ++k) {
2065 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:1613
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:1503
void setFromTriplets(const InputIterators &begin, const InputIterators &end, DupFunctor dup_func)
Definition SparseMatrix.h:1491
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:1596
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:1520
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:1474
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:1584
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:1723
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:1567
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