10#ifndef EIGEN_SPARSEMATRIX_H
11#define EIGEN_SPARSEMATRIX_H
46template<
typename _Scalar,
int _Options,
typename _StorageIndex>
47struct traits<SparseMatrix<_Scalar, _Options, _StorageIndex> >
49 typedef _Scalar Scalar;
50 typedef _StorageIndex StorageIndex;
51 typedef Sparse StorageKind;
52 typedef MatrixXpr XprKind;
59 SupportedAccessPatterns = InnerRandomAccessPattern
63template<
typename _Scalar,
int _Options,
typename _StorageIndex,
int DiagIndex>
64struct traits<Diagonal<SparseMatrix<_Scalar, _Options, _StorageIndex>, DiagIndex> >
66 typedef SparseMatrix<_Scalar, _Options, _StorageIndex> MatrixType;
67 typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
68 typedef typename remove_reference<MatrixTypeNested>::type _MatrixTypeNested;
70 typedef _Scalar Scalar;
71 typedef Dense StorageKind;
72 typedef _StorageIndex StorageIndex;
73 typedef MatrixXpr XprKind;
77 ColsAtCompileTime = 1,
79 MaxColsAtCompileTime = 1,
84template<
typename _Scalar,
int _Options,
typename _StorageIndex,
int DiagIndex>
85struct traits<Diagonal<const SparseMatrix<_Scalar, _Options, _StorageIndex>, DiagIndex> >
86 :
public traits<Diagonal<SparseMatrix<_Scalar, _Options, _StorageIndex>, DiagIndex> >
95template<
typename _Scalar,
int _Options,
typename _StorageIndex>
100 using Base::convert_index;
102 template<
typename,
typename,
typename,
typename,
typename>
103 friend struct internal::Assignment;
108 using Base::operator+=;
109 using Base::operator-=;
114 typedef typename Base::InnerIterator InnerIterator;
115 typedef typename Base::ReverseInnerIterator ReverseInnerIterator;
118 using Base::IsRowMajor;
119 typedef internal::CompressedStorage<Scalar,StorageIndex> Storage;
138 inline Index rows()
const {
return IsRowMajor ? m_outerSize : m_innerSize; }
140 inline Index cols()
const {
return IsRowMajor ? m_innerSize : m_outerSize; }
150 inline const Scalar*
valuePtr()
const {
return m_data.valuePtr(); }
154 inline Scalar*
valuePtr() {
return m_data.valuePtr(); }
184 inline Storage& data() {
return m_data; }
186 inline const Storage& data()
const {
return m_data; }
192 eigen_assert(row>=0 && row<
rows() && col>=0 && col<
cols());
194 const Index outer = IsRowMajor ? row : col;
195 const Index inner = IsRowMajor ? col : row;
196 Index end = m_innerNonZeros ? m_outerIndex[outer] + m_innerNonZeros[outer] : m_outerIndex[outer+1];
197 return m_data.atInRange(m_outerIndex[outer], end,
StorageIndex(inner));
210 eigen_assert(row>=0 && row<
rows() && col>=0 && col<
cols());
212 const Index outer = IsRowMajor ? row : col;
213 const Index inner = IsRowMajor ? col : row;
215 Index start = m_outerIndex[outer];
216 Index end = m_innerNonZeros ? m_outerIndex[outer] + m_innerNonZeros[outer] : m_outerIndex[outer+1];
217 eigen_assert(end>=start &&
"you probably called coeffRef on a non finalized matrix");
221 if((p<end) && (m_data.index(p)==inner))
222 return m_data.value(p);
256 memset(m_outerIndex, 0, (m_outerSize+1)*
sizeof(
StorageIndex));
258 memset(m_innerNonZeros, 0, (m_outerSize)*
sizeof(
StorageIndex));
266 eigen_assert(
isCompressed() &&
"This function does not make sense in non compressed mode.");
267 m_data.reserve(reserveSize);
270 #ifdef EIGEN_PARSED_BY_DOXYGEN
283 template<
class SizesType>
284 inline void reserve(
const SizesType& reserveSizes);
286 template<
class SizesType>
287 inline void reserve(
const SizesType& reserveSizes,
const typename SizesType::value_type& enableif =
288 #
if (!EIGEN_COMP_MSVC) || (EIGEN_COMP_MSVC>=1500)
291 SizesType::value_type())
293 EIGEN_UNUSED_VARIABLE(enableif);
294 reserveInnerVectors(reserveSizes);
298 template<
class SizesType>
299 inline void reserveInnerVectors(
const SizesType& reserveSizes)
303 Index totalReserveSize = 0;
306 if (!m_innerNonZeros) internal::throw_std_bad_alloc();
312 for(
Index j=0; j<m_outerSize; ++j)
314 newOuterIndex[j] = count;
315 count += reserveSizes[j] + (m_outerIndex[j+1]-m_outerIndex[j]);
316 totalReserveSize += reserveSizes[j];
318 m_data.reserve(totalReserveSize);
319 StorageIndex previousOuterIndex = m_outerIndex[m_outerSize];
320 for(
Index j=m_outerSize-1; j>=0; --j)
322 StorageIndex innerNNZ = previousOuterIndex - m_outerIndex[j];
323 for(
Index i=innerNNZ-1; i>=0; --i)
325 m_data.index(newOuterIndex[j]+i) = m_data.index(m_outerIndex[j]+i);
326 m_data.value(newOuterIndex[j]+i) = m_data.value(m_outerIndex[j]+i);
328 previousOuterIndex = m_outerIndex[j];
329 m_outerIndex[j] = newOuterIndex[j];
330 m_innerNonZeros[j] = innerNNZ;
333 m_outerIndex[m_outerSize] = m_outerIndex[m_outerSize-1] + m_innerNonZeros[m_outerSize-1] + reserveSizes[m_outerSize-1];
335 m_data.resize(m_outerIndex[m_outerSize]);
340 if (!newOuterIndex) internal::throw_std_bad_alloc();
343 for(
Index j=0; j<m_outerSize; ++j)
345 newOuterIndex[j] = count;
346 StorageIndex alreadyReserved = (m_outerIndex[j+1]-m_outerIndex[j]) - m_innerNonZeros[j];
347 StorageIndex toReserve = std::max<StorageIndex>(reserveSizes[j], alreadyReserved);
348 count += toReserve + m_innerNonZeros[j];
350 newOuterIndex[m_outerSize] = count;
352 m_data.resize(count);
353 for(
Index j=m_outerSize-1; j>=0; --j)
355 Index offset = newOuterIndex[j] - m_outerIndex[j];
359 for(
Index i=innerNNZ-1; i>=0; --i)
361 m_data.index(newOuterIndex[j]+i) = m_data.index(m_outerIndex[j]+i);
362 m_data.value(newOuterIndex[j]+i) = m_data.value(m_outerIndex[j]+i);
367 std::swap(m_outerIndex, newOuterIndex);
368 std::free(newOuterIndex);
386 inline Scalar& insertBack(
Index row,
Index col)
388 return insertBackByOuterInner(IsRowMajor?row:col, IsRowMajor?col:row);
393 inline Scalar& insertBackByOuterInner(
Index outer,
Index inner)
395 eigen_assert(
Index(m_outerIndex[outer+1]) == m_data.size() &&
"Invalid ordered insertion (invalid outer index)");
396 eigen_assert( (m_outerIndex[outer+1]-m_outerIndex[outer]==0 || m_data.index(m_data.size()-1)<inner) &&
"Invalid ordered insertion (invalid inner index)");
397 Index p = m_outerIndex[outer+1];
398 ++m_outerIndex[outer+1];
399 m_data.append(Scalar(0), inner);
400 return m_data.value(p);
405 inline Scalar& insertBackByOuterInnerUnordered(
Index outer,
Index inner)
407 Index p = m_outerIndex[outer+1];
408 ++m_outerIndex[outer+1];
409 m_data.append(Scalar(0), inner);
410 return m_data.value(p);
415 inline void startVec(
Index outer)
417 eigen_assert(m_outerIndex[outer]==
Index(m_data.size()) &&
"You must call startVec for each inner vector sequentially");
418 eigen_assert(m_outerIndex[outer+1]==0 &&
"You must call startVec for each inner vector sequentially");
419 m_outerIndex[outer+1] = m_outerIndex[outer];
425 inline void finalize()
430 Index i = m_outerSize;
432 while (i>=0 && m_outerIndex[i]==0)
435 while (i<=m_outerSize)
437 m_outerIndex[i] =
size;
445 template<
typename InputIterators>
448 template<
typename InputIterators,
typename DupFunctor>
449 void setFromTriplets(
const InputIterators& begin,
const InputIterators& end, DupFunctor dup_func);
451 void sumupDuplicates() { collapseDuplicates(internal::scalar_sum_op<Scalar,Scalar>()); }
453 template<
typename DupFunctor>
454 void collapseDuplicates(DupFunctor dup_func = DupFunctor());
462 return insert(IsRowMajor ? j : i, IsRowMajor ? i : j);
472 eigen_internal_assert(m_outerIndex!=0 && m_outerSize>0);
474 Index oldStart = m_outerIndex[1];
475 m_outerIndex[1] = m_innerNonZeros[0];
476 for(
Index j=1; j<m_outerSize; ++j)
478 Index nextOldStart = m_outerIndex[j+1];
479 Index offset = oldStart - m_outerIndex[j];
482 for(
Index k=0; k<m_innerNonZeros[j]; ++k)
484 m_data.index(m_outerIndex[j]+k) = m_data.index(oldStart+k);
485 m_data.value(m_outerIndex[j]+k) = m_data.value(oldStart+k);
488 m_outerIndex[j+1] = m_outerIndex[j] + m_innerNonZeros[j];
489 oldStart = nextOldStart;
491 std::free(m_innerNonZeros);
493 m_data.resize(m_outerIndex[m_outerSize]);
500 if(m_innerNonZeros != 0)
503 for (
Index i = 0; i < m_outerSize; i++)
505 m_innerNonZeros[i] = m_outerIndex[i+1] - m_outerIndex[i];
512 prune(default_prunning_func(reference,epsilon));
522 template<
typename KeepFunc>
523 void prune(
const KeepFunc& keep = KeepFunc())
529 for(
Index j=0; j<m_outerSize; ++j)
531 Index previousStart = m_outerIndex[j];
533 Index end = m_outerIndex[j+1];
534 for(
Index i=previousStart; i<end; ++i)
536 if(keep(IsRowMajor?j:m_data.index(i), IsRowMajor?m_data.index(i):j, m_data.value(i)))
538 m_data.value(k) = m_data.value(i);
539 m_data.index(k) = m_data.index(i);
544 m_outerIndex[m_outerSize] = k;
559 if (this->
rows() == rows && this->
cols() == cols)
return;
573 if (!newInnerNonZeros) internal::throw_std_bad_alloc();
574 m_innerNonZeros = newInnerNonZeros;
576 for(
Index i=m_outerSize; i<m_outerSize+outerChange; i++)
577 m_innerNonZeros[i] = 0;
579 else if (innerChange < 0)
583 if (!m_innerNonZeros) internal::throw_std_bad_alloc();
584 for(
Index i = 0; i < m_outerSize + (std::min)(outerChange,
Index(0)); i++)
585 m_innerNonZeros[i] = m_outerIndex[i+1] - m_outerIndex[i];
586 for(
Index i = m_outerSize; i < m_outerSize + outerChange; i++)
587 m_innerNonZeros[i] = 0;
591 if (m_innerNonZeros && innerChange < 0)
593 for(
Index i = 0; i < m_outerSize + (std::min)(outerChange,
Index(0)); i++)
597 while (n > 0 && m_data.index(start+n-1) >= newInnerSize) --n;
601 m_innerSize = newInnerSize;
604 if (outerChange == 0)
608 if (!newOuterIndex) internal::throw_std_bad_alloc();
609 m_outerIndex = newOuterIndex;
612 StorageIndex lastIdx = m_outerSize == 0 ? 0 : m_outerIndex[m_outerSize];
613 for(
Index i=m_outerSize; i<m_outerSize+outerChange+1; i++)
614 m_outerIndex[i] = lastIdx;
616 m_outerSize += outerChange;
629 m_innerSize = IsRowMajor ?
cols :
rows;
631 if (m_outerSize !=
outerSize || m_outerSize==0)
633 std::free(m_outerIndex);
635 if (!m_outerIndex) internal::throw_std_bad_alloc();
641 std::free(m_innerNonZeros);
644 memset(m_outerIndex, 0, (m_outerSize+1)*
sizeof(
StorageIndex));
665 : m_outerSize(-1), m_innerSize(0), m_outerIndex(0), m_innerNonZeros(0)
667 check_template_parameters();
673 : m_outerSize(0), m_innerSize(0), m_outerIndex(0), m_innerNonZeros(0)
675 check_template_parameters();
680 template<
typename OtherDerived>
682 : m_outerSize(0), m_innerSize(0), m_outerIndex(0), m_innerNonZeros(0)
684 EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename OtherDerived::Scalar>::value),
685 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
686 check_template_parameters();
692 #ifdef EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
693 EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
695 internal::call_assignment_no_alias(*
this, other.
derived());
700 template<
typename OtherDerived,
unsigned int UpLo>
702 : m_outerSize(0), m_innerSize(0), m_outerIndex(0), m_innerNonZeros(0)
704 check_template_parameters();
705 Base::operator=(other);
710 :
Base(), m_outerSize(0), m_innerSize(0), m_outerIndex(0), m_innerNonZeros(0)
712 check_template_parameters();
717 template<
typename OtherDerived>
719 :
Base(), m_outerSize(0), m_innerSize(0), m_outerIndex(0), m_innerNonZeros(0)
721 check_template_parameters();
722 initAssignment(other);
727 template<
typename OtherDerived>
729 :
Base(), m_outerSize(0), m_innerSize(0), m_outerIndex(0), m_innerNonZeros(0)
731 check_template_parameters();
732 *
this = other.derived();
740 std::swap(m_outerIndex, other.m_outerIndex);
741 std::swap(m_innerSize, other.m_innerSize);
742 std::swap(m_outerSize, other.m_outerSize);
743 std::swap(m_innerNonZeros, other.m_innerNonZeros);
744 m_data.swap(other.m_data);
751 eigen_assert(
rows() ==
cols() &&
"ONLY FOR SQUARED MATRICES");
752 this->m_data.resize(
rows());
756 std::free(m_innerNonZeros);
761 if (other.isRValue())
763 swap(other.const_cast_derived());
765 else if(
this!=&other)
767 #ifdef EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
768 EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
770 initAssignment(other);
773 internal::smart_copy(other.m_outerIndex, other.m_outerIndex + m_outerSize + 1, m_outerIndex);
774 m_data = other.m_data;
778 Base::operator=(other);
784#ifndef EIGEN_PARSED_BY_DOXYGEN
785 template<
typename OtherDerived>
786 inline SparseMatrix& operator=(
const EigenBase<OtherDerived>& other)
787 {
return Base::operator=(other.derived()); }
789 template<
typename Lhs,
typename Rhs>
790 inline SparseMatrix& operator=(
const Product<Lhs,Rhs,AliasFreeProduct>& other);
793 template<
typename OtherDerived>
794 EIGEN_DONT_INLINE
SparseMatrix& operator=(
const SparseMatrixBase<OtherDerived>& other);
796 friend std::ostream & operator << (std::ostream & s,
const SparseMatrix& m)
799 s <<
"Nonzero entries:\n";
802 for (Index i=0; i<m.nonZeros(); ++i)
803 s <<
"(" << m.m_data.value(i) <<
"," << m.m_data.index(i) <<
") ";
807 for (Index i=0; i<m.outerSize(); ++i)
809 Index p = m.m_outerIndex[i];
810 Index pe = m.m_outerIndex[i]+m.m_innerNonZeros[i];
813 s <<
"(" << m.m_data.value(k) <<
"," << m.m_data.index(k) <<
") ";
815 for (; k<m.m_outerIndex[i+1]; ++k) {
822 s <<
"Outer pointers:\n";
823 for (
Index i=0; i<m.outerSize(); ++i) {
824 s << m.m_outerIndex[i] <<
" ";
826 s <<
" $" << std::endl;
827 if(!m.isCompressed())
829 s <<
"Inner non zeros:\n";
830 for (Index i=0; i<m.outerSize(); ++i) {
831 s << m.m_innerNonZeros[i] <<
" ";
833 s <<
" $" << std::endl;
837 s << static_cast<const SparseMatrixBase<SparseMatrix>&>(m);
844 std::free(m_outerIndex);
845 std::free(m_innerNonZeros);
851# ifdef EIGEN_SPARSEMATRIX_PLUGIN
852# include EIGEN_SPARSEMATRIX_PLUGIN
857 template<
typename Other>
858 void initAssignment(
const Other& other)
860 resize(other.rows(), other.cols());
863 std::free(m_innerNonZeros);
870 EIGEN_DONT_INLINE Scalar& insertCompressed(Index row, Index col);
874 class SingletonVector
876 StorageIndex m_index;
877 StorageIndex m_value;
879 typedef StorageIndex value_type;
880 SingletonVector(Index i, Index v)
881 : m_index(convert_index(i)), m_value(convert_index(v))
884 StorageIndex operator[](Index i)
const {
return i==m_index ? m_value : 0; }
889 EIGEN_DONT_INLINE Scalar& insertUncompressed(Index row, Index col);
894 EIGEN_STRONG_INLINE Scalar& insertBackUncompressed(Index row, Index col)
896 const Index outer = IsRowMajor ? row : col;
897 const Index inner = IsRowMajor ? col : row;
899 eigen_assert(!isCompressed());
900 eigen_assert(m_innerNonZeros[outer]<=(m_outerIndex[outer+1] - m_outerIndex[outer]));
902 Index p = m_outerIndex[outer] + m_innerNonZeros[outer]++;
903 m_data.index(p) = convert_index(inner);
904 return (m_data.value(p) = Scalar(0));
907 struct IndexPosPair {
908 IndexPosPair(Index a_i, Index a_p) : i(a_i), p(a_p) {}
926 template<
typename DiagXpr,
typename Func>
927 void assignDiagonal(
const DiagXpr diagXpr,
const Func& assignFunc)
929 Index n = diagXpr.size();
931 const bool overwrite = internal::is_same<Func, internal::assign_op<Scalar,Scalar> >::value;
934 if((this->rows()!=n) || (this->cols()!=n))
938 if(m_data.size()==0 || overwrite)
940 typedef Array<StorageIndex,Dynamic,1> ArrayXI;
941 this->makeCompressed();
942 this->resizeNonZeros(n);
947 internal::call_assignment_no_alias(values, diagXpr, assignFunc);
951 bool isComp = isCompressed();
952 internal::evaluator<DiagXpr> diaEval(diagXpr);
953 std::vector<IndexPosPair> newEntries;
956 for(Index i = 0; i<n; ++i)
958 internal::LowerBoundIndex lb = this->lower_bound(i,i);
963 assignFunc.assignCoeff(m_data.value(p), diaEval.coeff(i));
965 else if((!isComp) && m_innerNonZeros[i] < (m_outerIndex[i+1]-m_outerIndex[i]))
968 m_data.moveChunk(p, p+1, m_outerIndex[i]+m_innerNonZeros[i]-p);
969 m_innerNonZeros[i]++;
970 m_data.value(p) = Scalar(0);
971 m_data.index(p) = StorageIndex(i);
972 assignFunc.assignCoeff(m_data.value(p), diaEval.coeff(i));
977 newEntries.push_back(IndexPosPair(i,p));
984 Storage newData(m_data.size()+n_entries);
987 for(Index k=0; k<n_entries;++k)
989 Index i = newEntries[k].i;
990 Index p = newEntries[k].p;
991 internal::smart_copy(m_data.valuePtr()+prev_p, m_data.valuePtr()+p, newData.valuePtr()+prev_p+k);
992 internal::smart_copy(m_data.indexPtr()+prev_p, m_data.indexPtr()+p, newData.indexPtr()+prev_p+k);
993 for(Index j=prev_i;j<i;++j)
994 m_outerIndex[j+1] += k;
996 m_innerNonZeros[i]++;
999 newData.value(p+k) = Scalar(0);
1000 newData.index(p+k) = StorageIndex(i);
1001 assignFunc.assignCoeff(newData.value(p+k), diaEval.coeff(i));
1004 internal::smart_copy(m_data.valuePtr()+prev_p, m_data.valuePtr()+m_data.size(), newData.valuePtr()+prev_p+n_entries);
1005 internal::smart_copy(m_data.indexPtr()+prev_p, m_data.indexPtr()+m_data.size(), newData.indexPtr()+prev_p+n_entries);
1006 for(Index j=prev_i+1;j<=m_outerSize;++j)
1007 m_outerIndex[j] += n_entries;
1009 m_data.swap(newData);
1015 static void check_template_parameters()
1017 EIGEN_STATIC_ASSERT(NumTraits<StorageIndex>::IsSigned,THE_INDEX_TYPE_MUST_BE_A_SIGNED_TYPE);
1018 EIGEN_STATIC_ASSERT((Options&(ColMajor|RowMajor))==Options,INVALID_MATRIX_TEMPLATE_PARAMETERS);
1021 struct default_prunning_func {
1022 default_prunning_func(
const Scalar& ref,
const RealScalar& eps) : reference(ref), epsilon(eps) {}
1023 inline bool operator() (
const Index&,
const Index&,
const Scalar& value)
const
1025 return !internal::isMuchSmallerThan(value, reference, epsilon);
1034template<
typename InputIterator,
typename SparseMatrixType,
typename DupFunctor>
1035void set_from_triplets(
const InputIterator& begin,
const InputIterator& end, SparseMatrixType& mat, DupFunctor dup_func)
1037 enum { IsRowMajor = SparseMatrixType::IsRowMajor };
1038 typedef typename SparseMatrixType::Scalar Scalar;
1039 typedef typename SparseMatrixType::StorageIndex StorageIndex;
1040 SparseMatrix<Scalar,IsRowMajor?ColMajor:RowMajor,StorageIndex> trMat(mat.rows(),mat.cols());
1045 typename SparseMatrixType::IndexVector wi(trMat.outerSize());
1047 for(InputIterator it(begin); it!=end; ++it)
1049 eigen_assert(it->row()>=0 && it->row()<mat.rows() && it->col()>=0 && it->col()<mat.cols());
1050 wi(IsRowMajor ? it->col() : it->row())++;
1055 for(InputIterator it(begin); it!=end; ++it)
1056 trMat.insertBackUncompressed(it->row(),it->col()) = it->value();
1059 trMat.collapseDuplicates(dup_func);
1106template<
typename Scalar,
int _Options,
typename _StorageIndex>
1107template<
typename InputIterators>
1110 internal::set_from_triplets<InputIterators, SparseMatrix<Scalar,_Options,_StorageIndex> >(begin, end, *
this, internal::scalar_sum_op<Scalar,Scalar>());
1122template<
typename Scalar,
int _Options,
typename _StorageIndex>
1123template<
typename InputIterators,
typename DupFunctor>
1126 internal::set_from_triplets<InputIterators, SparseMatrix<Scalar,_Options,_StorageIndex>, DupFunctor>(begin, end, *
this, dup_func);
1130template<
typename Scalar,
int _Options,
typename _StorageIndex>
1131template<
typename DupFunctor>
1134 eigen_assert(!isCompressed());
1136 IndexVector wi(innerSize());
1138 StorageIndex count = 0;
1140 for(
Index j=0; j<outerSize(); ++j)
1142 StorageIndex start = count;
1143 Index oldEnd = m_outerIndex[j]+m_innerNonZeros[j];
1144 for(
Index k=m_outerIndex[j]; k<oldEnd; ++k)
1146 Index i = m_data.index(k);
1150 m_data.value(wi(i)) = dup_func(m_data.value(wi(i)), m_data.value(k));
1154 m_data.value(count) = m_data.value(k);
1155 m_data.index(count) = m_data.index(k);
1160 m_outerIndex[j] = start;
1162 m_outerIndex[m_outerSize] = count;
1165 std::free(m_innerNonZeros);
1166 m_innerNonZeros = 0;
1167 m_data.resize(m_outerIndex[m_outerSize]);
1170template<
typename Scalar,
int _Options,
typename _StorageIndex>
1171template<
typename OtherDerived>
1172EIGEN_DONT_INLINE SparseMatrix<Scalar,_Options,_StorageIndex>& SparseMatrix<Scalar,_Options,_StorageIndex>::operator=(
const SparseMatrixBase<OtherDerived>& other)
1174 EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename OtherDerived::Scalar>::value),
1175 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
1177 #ifdef EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
1178 EIGEN_SPARSE_CREATE_TEMPORARY_PLUGIN
1181 const bool needToTranspose = (Flags &
RowMajorBit) != (internal::evaluator<OtherDerived>::Flags & RowMajorBit);
1182 if (needToTranspose)
1184 #ifdef EIGEN_SPARSE_TRANSPOSED_COPY_PLUGIN
1185 EIGEN_SPARSE_TRANSPOSED_COPY_PLUGIN
1191 typedef typename internal::nested_eval<OtherDerived,2,typename internal::plain_matrix_type<OtherDerived>::type >::type OtherCopy;
1192 typedef typename internal::remove_all<OtherCopy>::type _OtherCopy;
1193 typedef internal::evaluator<_OtherCopy> OtherCopyEval;
1194 OtherCopy otherCopy(other.derived());
1195 OtherCopyEval otherCopyEval(otherCopy);
1197 SparseMatrix dest(other.rows(),other.cols());
1202 for (Index j=0; j<otherCopy.outerSize(); ++j)
1203 for (
typename OtherCopyEval::InnerIterator it(otherCopyEval, j); it; ++it)
1204 ++dest.m_outerIndex[it.index()];
1207 StorageIndex count = 0;
1208 IndexVector positions(dest.outerSize());
1209 for (Index j=0; j<dest.outerSize(); ++j)
1211 StorageIndex tmp = dest.m_outerIndex[j];
1212 dest.m_outerIndex[j] = count;
1213 positions[j] = count;
1216 dest.m_outerIndex[dest.outerSize()] = count;
1218 dest.m_data.resize(count);
1220 for (StorageIndex j=0; j<otherCopy.outerSize(); ++j)
1222 for (
typename OtherCopyEval::InnerIterator it(otherCopyEval, j); it; ++it)
1224 Index pos = positions[it.index()]++;
1225 dest.m_data.index(pos) = j;
1226 dest.m_data.value(pos) = it.value();
1234 if(other.isRValue())
1236 initAssignment(other.derived());
1239 return Base::operator=(other.derived());
1243template<
typename _Scalar,
int _Options,
typename _StorageIndex>
1246 eigen_assert(row>=0 && row<rows() && col>=0 && col<cols());
1248 const Index outer = IsRowMajor ? row : col;
1249 const Index inner = IsRowMajor ? col : row;
1256 if(m_data.allocatedSize()==0)
1257 m_data.reserve(2*m_innerSize);
1261 if(!m_innerNonZeros) internal::throw_std_bad_alloc();
1263 memset(m_innerNonZeros, 0, (m_outerSize)*
sizeof(
StorageIndex));
1267 StorageIndex end = convert_index(m_data.allocatedSize());
1268 for(
Index j=1; j<=m_outerSize; ++j)
1269 m_outerIndex[j] = end;
1275 if(!m_innerNonZeros) internal::throw_std_bad_alloc();
1276 for(
Index j=0; j<m_outerSize; ++j)
1277 m_innerNonZeros[j] = m_outerIndex[j+1]-m_outerIndex[j];
1282 Index data_end = m_data.allocatedSize();
1286 if(m_outerIndex[outer]==data_end)
1288 eigen_internal_assert(m_innerNonZeros[outer]==0);
1294 while(j>=0 && m_innerNonZeros[j]==0)
1295 m_outerIndex[j--] = p;
1298 ++m_innerNonZeros[outer];
1299 m_data.append(Scalar(0), inner);
1302 if(data_end != m_data.allocatedSize())
1307 eigen_internal_assert(data_end < m_data.allocatedSize());
1308 StorageIndex new_end = convert_index(m_data.allocatedSize());
1309 for(
Index k=outer+1; k<=m_outerSize; ++k)
1310 if(m_outerIndex[k]==data_end)
1311 m_outerIndex[k] = new_end;
1313 return m_data.value(p);
1318 if(m_outerIndex[outer+1]==data_end && m_outerIndex[outer]+m_innerNonZeros[outer]==m_data.size())
1320 eigen_internal_assert(outer+1==m_outerSize || m_innerNonZeros[outer+1]==0);
1323 ++m_innerNonZeros[outer];
1324 m_data.resize(m_data.size()+1);
1327 if(data_end != m_data.allocatedSize())
1332 eigen_internal_assert(data_end < m_data.allocatedSize());
1333 StorageIndex new_end = convert_index(m_data.allocatedSize());
1334 for(
Index k=outer+1; k<=m_outerSize; ++k)
1335 if(m_outerIndex[k]==data_end)
1336 m_outerIndex[k] = new_end;
1340 Index startId = m_outerIndex[outer];
1341 Index p = m_outerIndex[outer]+m_innerNonZeros[outer]-1;
1342 while ( (p > startId) && (m_data.index(p-1) > inner) )
1344 m_data.index(p) = m_data.index(p-1);
1345 m_data.value(p) = m_data.value(p-1);
1349 m_data.index(p) = convert_index(inner);
1350 return (m_data.value(p) = Scalar(0));
1353 if(m_data.size() != m_data.allocatedSize())
1356 m_data.resize(m_data.allocatedSize());
1360 return insertUncompressed(row,col);
1363template<
typename _Scalar,
int _Options,
typename _StorageIndex>
1366 eigen_assert(!isCompressed());
1368 const Index outer = IsRowMajor ? row : col;
1369 const StorageIndex inner = convert_index(IsRowMajor ? col : row);
1371 Index room = m_outerIndex[outer+1] - m_outerIndex[outer];
1372 StorageIndex innerNNZ = m_innerNonZeros[outer];
1376 reserve(SingletonVector(outer,std::max<StorageIndex>(2,innerNNZ)));
1379 Index startId = m_outerIndex[outer];
1380 Index p = startId + m_innerNonZeros[outer];
1381 while ( (p > startId) && (m_data.index(p-1) > inner) )
1383 m_data.index(p) = m_data.index(p-1);
1384 m_data.value(p) = m_data.value(p-1);
1387 eigen_assert((p<=startId || m_data.index(p-1)!=inner) &&
"you cannot insert an element that already exists, you must call coeffRef to this end");
1389 m_innerNonZeros[outer]++;
1391 m_data.index(p) = inner;
1392 return (m_data.value(p) = Scalar(0));
1395template<
typename _Scalar,
int _Options,
typename _StorageIndex>
1396EIGEN_DONT_INLINE
typename SparseMatrix<_Scalar,_Options,_StorageIndex>::Scalar& SparseMatrix<_Scalar,_Options,_StorageIndex>::insertCompressed(Index row, Index col)
1398 eigen_assert(isCompressed());
1400 const Index outer = IsRowMajor ? row : col;
1401 const Index inner = IsRowMajor ? col : row;
1403 Index previousOuter = outer;
1404 if (m_outerIndex[outer+1]==0)
1407 while (previousOuter>=0 && m_outerIndex[previousOuter]==0)
1409 m_outerIndex[previousOuter] = convert_index(m_data.size());
1412 m_outerIndex[outer+1] = m_outerIndex[outer];
1418 bool isLastVec = (!(previousOuter==-1 && m_data.size()!=0))
1419 && (std::size_t(m_outerIndex[outer+1]) == m_data.size());
1421 std::size_t startId = m_outerIndex[outer];
1423 std::size_t p = m_outerIndex[outer+1];
1424 ++m_outerIndex[outer+1];
1426 double reallocRatio = 1;
1427 if (m_data.allocatedSize()<=m_data.size())
1430 if (m_data.size()==0)
1439 double nnzEstimate = double(m_outerIndex[outer])*double(m_outerSize)/double(outer+1);
1440 reallocRatio = (nnzEstimate-double(m_data.size()))/
double(m_data.size());
1444 reallocRatio = (std::min)((std::max)(reallocRatio,1.5),8.);
1447 m_data.resize(m_data.size()+1,reallocRatio);
1451 if (previousOuter==-1)
1455 for (Index k=0; k<=(outer+1); ++k)
1456 m_outerIndex[k] = 0;
1458 while(m_outerIndex[k]==0)
1459 m_outerIndex[k++] = 1;
1460 while (k<=m_outerSize && m_outerIndex[k]!=0)
1461 m_outerIndex[k++]++;
1464 k = m_outerIndex[k]-1;
1467 m_data.index(k) = m_data.index(k-1);
1468 m_data.value(k) = m_data.value(k-1);
1477 while (j<=m_outerSize && m_outerIndex[j]!=0)
1478 m_outerIndex[j++]++;
1481 Index k = m_outerIndex[j]-1;
1484 m_data.index(k) = m_data.index(k-1);
1485 m_data.value(k) = m_data.value(k-1);
1491 while ( (p > startId) && (m_data.index(p-1) > inner) )
1493 m_data.index(p) = m_data.index(p-1);
1494 m_data.value(p) = m_data.value(p-1);
1498 m_data.index(p) = inner;
1499 return (m_data.value(p) = Scalar(0));
1504template<
typename _Scalar,
int _Options,
typename _StorageIndex>
1505struct evaluator<SparseMatrix<_Scalar,_Options,_StorageIndex> >
1506 : evaluator<SparseCompressedBase<SparseMatrix<_Scalar,_Options,_StorageIndex> > >
1508 typedef evaluator<SparseCompressedBase<SparseMatrix<_Scalar,_Options,_StorageIndex> > > Base;
1509 typedef SparseMatrix<_Scalar,_Options,_StorageIndex> SparseMatrixType;
1510 evaluator() : Base() {}
1511 explicit evaluator(
const SparseMatrixType &mat) : Base(mat) {}
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:47
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition: Diagonal.h:65
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:96
Sparse matrix.
Definition: MappedSparseMatrix.h:34
Common base class for sparse [compressed]-{row|column}-storage format.
Definition: SparseCompressedBase.h:38
Index nonZeros() const
Definition: SparseCompressedBase.h:56
bool isCompressed() const
Definition: SparseCompressedBase.h:107
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:28
internal::traits< Derived >::StorageIndex StorageIndex
Definition: SparseMatrixBase.h:43
Index size() const
Definition: SparseMatrixBase.h:181
@ Flags
Definition: SparseMatrixBase.h:95
A versatible sparse matrix representation.
Definition: SparseMatrix.h:98
void prune(const Scalar &reference, const RealScalar &epsilon=NumTraits< RealScalar >::dummy_precision())
Definition: SparseMatrix.h:510
void prune(const KeepFunc &keep=KeepFunc())
Definition: SparseMatrix.h:523
Index innerSize() const
Definition: SparseMatrix.h:143
void reserve(Index reserveSize)
Definition: SparseMatrix.h:264
SparseMatrix(const DiagonalBase< OtherDerived > &other)
Copy constructor with in-place evaluation.
Definition: SparseMatrix.h:728
StorageIndex * outerIndexPtr()
Definition: SparseMatrix.h:172
const StorageIndex * innerNonZeroPtr() const
Definition: SparseMatrix.h:177
~SparseMatrix()
Definition: SparseMatrix.h:842
Scalar * valuePtr()
Definition: SparseMatrix.h:154
const ConstDiagonalReturnType diagonal() const
Definition: SparseMatrix.h:655
SparseMatrix(const ReturnByValue< OtherDerived > &other)
Copy constructor with in-place evaluation.
Definition: SparseMatrix.h:718
Index outerSize() const
Definition: SparseMatrix.h:145
SparseMatrix(const SparseMatrixBase< OtherDerived > &other)
Definition: SparseMatrix.h:681
Scalar coeff(Index row, Index col) const
Definition: SparseMatrix.h:190
void makeCompressed()
Definition: SparseMatrix.h:467
Index rows() const
Definition: SparseMatrix.h:138
SparseMatrix()
Definition: SparseMatrix.h:664
SparseMatrix(Index rows, Index cols)
Definition: SparseMatrix.h:672
void uncompress()
Definition: SparseMatrix.h:498
const StorageIndex * innerIndexPtr() const
Definition: SparseMatrix.h:159
const StorageIndex * outerIndexPtr() const
Definition: SparseMatrix.h:168
bool isCompressed() const
Definition: SparseCompressedBase.h:107
void setIdentity()
Definition: SparseMatrix.h:749
void conservativeResize(Index rows, Index cols)
Definition: SparseMatrix.h:556
Index cols() const
Definition: SparseMatrix.h:140
SparseMatrix(const SparseSelfAdjointView< OtherDerived, UpLo > &other)
Definition: SparseMatrix.h:701
Scalar & insert(Index row, Index col)
Definition: SparseMatrix.h:1244
const Scalar * valuePtr() const
Definition: SparseMatrix.h:150
StorageIndex * innerNonZeroPtr()
Definition: SparseMatrix.h:181
StorageIndex * innerIndexPtr()
Definition: SparseMatrix.h:163
void reserve(const SizesType &reserveSizes)
void setFromTriplets(const InputIterators &begin, const InputIterators &end)
Definition: SparseMatrix.h:1108
void setZero()
Definition: SparseMatrix.h:253
void setFromTriplets(const InputIterators &begin, const InputIterators &end, DupFunctor dup_func)
Definition: SparseMatrix.h:1124
Scalar & coeffRef(Index row, Index col)
Definition: SparseMatrix.h:208
void swap(SparseMatrix &other)
Definition: SparseMatrix.h:737
SparseMatrix(const SparseMatrix &other)
Definition: SparseMatrix.h:709
DiagonalReturnType diagonal()
Definition: SparseMatrix.h:661
void resize(Index rows, Index cols)
Definition: SparseMatrix.h:626
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
Definition: SparseSelfAdjointView.h:45
a sparse vector class
Definition: SparseVector.h:66
const unsigned int LvalueBit
Definition: Constants.h:144
const unsigned int RowMajorBit
Definition: Constants.h:66
const unsigned int CompressedAccessBit
Definition: Constants.h:191
Namespace containing all symbols from the Eigen library.
Definition: Core:141
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
const int Dynamic
Definition: Constants.h:22
Derived & derived()
Definition: EigenBase.h:46
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:233