10 #ifndef EIGEN_SPARSEMATRIXBASE_H 11 #define EIGEN_SPARSEMATRIXBASE_H 30 typedef typename internal::traits<Derived>::Scalar Scalar;
31 typedef typename internal::packet_traits<Scalar>::type PacketScalar;
32 typedef typename internal::traits<Derived>::StorageKind StorageKind;
33 typedef typename internal::traits<Derived>::Index Index;
34 typedef typename internal::add_const_on_value_type_if_arithmetic<
35 typename internal::packet_traits<Scalar>::type
36 >::type PacketReturnType;
41 template<
typename OtherDerived>
63 SizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::RowsAtCompileTime,
64 internal::traits<Derived>::ColsAtCompileTime>::ret),
72 MaxSizeAtCompileTime = (internal::size_at_compile_time<MaxRowsAtCompileTime,
73 MaxColsAtCompileTime>::ret),
81 Flags = internal::traits<Derived>::Flags,
93 #ifndef EIGEN_PARSED_BY_DOXYGEN 99 typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
102 >::type AdjointReturnType;
108 #ifndef EIGEN_PARSED_BY_DOXYGEN 119 typedef typename internal::conditional<_HasDirectAccess, const Scalar&, Scalar>::type CoeffReturnType;
128 inline const Derived&
derived()
const {
return *static_cast<const Derived*>(
this); }
129 inline Derived&
derived() {
return *static_cast<Derived*>(
this); }
130 inline Derived& const_cast_derived()
const 131 {
return *static_cast<Derived*>(const_cast<SparseMatrixBase*>(
this)); }
132 #endif // not EIGEN_PARSED_BY_DOXYGEN 134 #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::SparseMatrixBase 135 # include "../plugins/CommonCwiseUnaryOps.h" 136 # include "../plugins/CommonCwiseBinaryOps.h" 137 # include "../plugins/MatrixCwiseUnaryOps.h" 138 # include "../plugins/MatrixCwiseBinaryOps.h" 139 # ifdef EIGEN_SPARSEMATRIXBASE_PLUGIN 140 # include EIGEN_SPARSEMATRIXBASE_PLUGIN 142 # undef EIGEN_CURRENT_STORAGE_BASE_CLASS 143 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS 147 inline Index
rows()
const {
return derived().rows(); }
149 inline Index
cols()
const {
return derived().cols(); }
155 inline Index nonZeros()
const {
return derived().nonZeros(); }
160 inline bool isVector()
const {
return rows()==1 ||
cols()==1; }
168 bool isRValue()
const {
return m_isRValue; }
169 Derived& markAsRValue() { m_isRValue =
true;
return derived(); }
171 SparseMatrixBase() : m_isRValue(false) { }
174 template<
typename OtherDerived>
175 Derived& operator=(
const ReturnByValue<OtherDerived>& other)
182 template<
typename OtherDerived>
185 return assign(other.
derived());
188 inline Derived& operator=(
const Derived& other)
193 return assign(other.derived());
198 template<
typename OtherDerived>
199 inline Derived& assign(
const OtherDerived& other)
202 const Index outerSize = (int(OtherDerived::Flags) &
RowMajorBit) ? other.rows() : other.cols();
203 if ((!transpose) && other.isRValue())
206 derived().resize(other.rows(), other.cols());
209 for (Index j=0; j<outerSize; ++j)
212 for (
typename OtherDerived::InnerIterator it(other, j); it; ++it)
214 Scalar v = it.value();
215 derived().insertBackByOuterInner(j,it.index()) = v;
222 assignGeneric(other);
227 template<
typename OtherDerived>
228 inline void assignGeneric(
const OtherDerived& other)
231 eigen_assert(( ((internal::traits<Derived>::SupportedAccessPatterns&OuterRandomAccessPattern)==OuterRandomAccessPattern) ||
233 "the transpose operation is supposed to be handled in SparseMatrix::operator=");
237 const Index outerSize = other.outerSize();
240 Derived temp(other.rows(), other.cols());
242 temp.reserve((std::max)(this->
rows(),this->
cols())*2);
243 for (Index j=0; j<outerSize; ++j)
246 for (
typename OtherDerived::InnerIterator it(other.derived(), j); it; ++it)
248 Scalar v = it.value();
249 temp.insertBackByOuterInner(Flip?it.index():j,Flip?j:it.index()) = v;
254 derived() = temp.markAsRValue();
259 template<
typename Lhs,
typename Rhs>
260 inline Derived& operator=(
const SparseSparseProduct<Lhs,Rhs>& product);
262 friend std::ostream & operator << (std::ostream & s,
const SparseMatrixBase& m)
264 typedef typename Derived::Nested Nested;
265 typedef typename internal::remove_all<Nested>::type NestedCleaned;
269 const Nested nm(m.derived());
270 for (Index
row=0;
row<nm.outerSize(); ++
row)
273 for (
typename NestedCleaned::InnerIterator it(nm.derived(),
row); it; ++it)
275 for ( ;
col<it.index(); ++
col)
277 s << it.value() <<
" ";
280 for ( ;
col<m.cols(); ++
col)
287 const Nested nm(m.derived());
290 for (
typename NestedCleaned::InnerIterator it(nm.derived(), 0); it; ++it)
292 for ( ;
row<it.index(); ++
row)
293 s <<
"0" << std::endl;
294 s << it.value() << std::endl;
297 for ( ;
row<m.rows(); ++
row)
298 s <<
"0" << std::endl;
302 SparseMatrix<Scalar, RowMajorBit> trans = m;
303 s << static_cast<const SparseMatrixBase<SparseMatrix<Scalar, RowMajorBit> >&>(trans);
309 template<
typename OtherDerived>
310 Derived& operator+=(
const SparseMatrixBase<OtherDerived>& other);
311 template<
typename OtherDerived>
312 Derived& operator-=(
const SparseMatrixBase<OtherDerived>& other);
314 Derived& operator*=(
const Scalar& other);
315 Derived& operator/=(
const Scalar& other);
317 #define EIGEN_SPARSE_CWISE_PRODUCT_RETURN_TYPE \ 319 internal::scalar_product_op< \ 320 typename internal::scalar_product_traits< \ 321 typename internal::traits<Derived>::Scalar, \ 322 typename internal::traits<OtherDerived>::Scalar \ 329 template<
typename OtherDerived>
330 EIGEN_STRONG_INLINE
const EIGEN_SPARSE_CWISE_PRODUCT_RETURN_TYPE
331 cwiseProduct(
const MatrixBase<OtherDerived> &other)
const;
334 template<
typename OtherDerived>
335 const typename SparseSparseProductReturnType<Derived,OtherDerived>::Type
336 operator*(
const SparseMatrixBase<OtherDerived> &other)
const;
339 template<
typename OtherDerived>
340 const SparseDiagonalProduct<Derived,OtherDerived>
341 operator*(
const DiagonalBase<OtherDerived> &other)
const;
344 template<
typename OtherDerived>
friend 345 const SparseDiagonalProduct<OtherDerived,Derived>
347 {
return SparseDiagonalProduct<OtherDerived,Derived>(lhs.derived(), rhs.
derived()); }
350 template<
typename OtherDerived>
friend 351 const typename DenseSparseProductReturnType<OtherDerived,Derived>::Type
353 {
return typename DenseSparseProductReturnType<OtherDerived,Derived>::Type(lhs.derived(),rhs); }
356 template<
typename OtherDerived>
357 const typename SparseDenseProductReturnType<Derived,OtherDerived>::Type
363 return SparseSymmetricPermutationProduct<Derived,Upper|Lower>(
derived(), perm);
366 template<
typename OtherDerived>
367 Derived& operator*=(
const SparseMatrixBase<OtherDerived>& other);
369 #ifdef EIGEN2_SUPPORT 371 template<
typename OtherDerived>
372 typename internal::plain_matrix_type_column_major<OtherDerived>::type
373 solveTriangular(
const MatrixBase<OtherDerived>& other)
const;
376 template<
typename OtherDerived>
377 void solveTriangularInPlace(MatrixBase<OtherDerived>& other)
const;
378 #endif // EIGEN2_SUPPORT 381 inline const SparseTriangularView<Derived, Mode> triangularView()
const;
383 template<
unsigned int UpLo>
inline const SparseSelfAdjointView<Derived, UpLo> selfadjointView()
const;
384 template<
unsigned int UpLo>
inline SparseSelfAdjointView<Derived, UpLo> selfadjointView();
386 template<
typename OtherDerived> Scalar dot(
const MatrixBase<OtherDerived>& other)
const;
387 template<
typename OtherDerived> Scalar dot(
const SparseMatrixBase<OtherDerived>& other)
const;
388 RealScalar squaredNorm()
const;
389 RealScalar norm()
const;
391 Transpose<Derived> transpose() {
return derived(); }
392 const Transpose<const Derived> transpose()
const {
return derived(); }
393 const AdjointReturnType adjoint()
const {
return transpose(); }
396 SparseInnerVectorSet<Derived,1>
row(Index i);
397 const SparseInnerVectorSet<Derived,1>
row(Index i)
const;
398 SparseInnerVectorSet<Derived,1>
col(Index j);
399 const SparseInnerVectorSet<Derived,1>
col(Index j)
const;
400 SparseInnerVectorSet<Derived,1>
innerVector(Index outer);
401 const SparseInnerVectorSet<Derived,1>
innerVector(Index outer)
const;
404 SparseInnerVectorSet<Derived,Dynamic> subrows(Index start, Index
size);
405 const SparseInnerVectorSet<Derived,Dynamic> subrows(Index start, Index
size)
const;
406 SparseInnerVectorSet<Derived,Dynamic> subcols(Index start, Index
size);
407 const SparseInnerVectorSet<Derived,Dynamic> subcols(Index start, Index
size)
const;
409 SparseInnerVectorSet<Derived,Dynamic>
middleRows(Index start, Index
size);
410 const SparseInnerVectorSet<Derived,Dynamic>
middleRows(Index start, Index
size)
const;
411 SparseInnerVectorSet<Derived,Dynamic>
middleCols(Index start, Index
size);
412 const SparseInnerVectorSet<Derived,Dynamic>
middleCols(Index start, Index
size)
const;
413 SparseInnerVectorSet<Derived,Dynamic>
innerVectors(Index outerStart, Index outerSize);
414 const SparseInnerVectorSet<Derived,Dynamic>
innerVectors(Index outerStart, Index outerSize)
const;
417 template<
typename DenseDerived>
418 void evalTo(MatrixBase<DenseDerived>& dst)
const 421 for (Index j=0; j<outerSize(); ++j)
422 for (
typename Derived::InnerIterator i(
derived(),j); i; ++i)
423 dst.coeffRef(i.row(),i.col()) = i.value();
426 Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime> toDense()
const 431 template<
typename OtherDerived>
432 bool isApprox(
const SparseMatrixBase<OtherDerived>& other,
433 RealScalar prec = NumTraits<Scalar>::dummy_precision())
const 434 {
return toDense().isApprox(other.toDense(),prec); }
436 template<
typename OtherDerived>
439 {
return toDense().isApprox(other,prec); }
446 inline const typename internal::eval<Derived>::type
eval()
const 447 {
return typename internal::eval<Derived>::type(
derived()); }
458 #endif // EIGEN_SPARSEMATRIXBASE_H Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:49
Definition: SparseMatrixBase.h:56
Index rows() const
Definition: EigenBase.h:44
A versatible sparse matrix representation.
Definition: SparseMatrix.h:85
SparseInnerVectorSet< Derived, 1 > innerVector(Index outer)
Definition: SparseBlock.h:325
Expression of the transpose of a matrix.
Definition: Transpose.h:57
SparseInnerVectorSet< Derived, 1 > col(Index j)
Definition: SparseBlock.h:306
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:88
Definition: EigenBase.h:26
SparseInnerVectorSet< Derived, Dynamic > innerVectors(Index outerStart, Index outerSize)
Definition: SparseBlock.h:375
SparseInnerVectorSet< Derived, Dynamic > middleRows(Index start, Index size)
Definition: SparseBlock.h:337
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:26
Definition: SparseMatrixBase.h:63
Derived & derived()
Definition: EigenBase.h:34
SparseInnerVectorSet< Derived, 1 > row(Index i)
Definition: SparseBlock.h:289
const internal::eval< Derived >::type eval() const
Definition: SparseMatrixBase.h:436
const ScalarMultipleReturnType operator *(const Scalar &scalar) const
Definition: SparseMatrixBase.h:50
Definition: SparseMatrixBase.h:86
Index size() const
Definition: EigenBase.h:49
Index cols() const
Definition: EigenBase.h:46
const unsigned int RowMajorBit
Definition: Constants.h:48
const unsigned int DirectAccessBit
Definition: Constants.h:137
Definition: SparseMatrixBase.h:81
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:59
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Definition: SparseMatrixBase.h:50
SparseSymmetricPermutationProduct< Derived, Upper|Lower > twistedBy(const PermutationMatrix< Dynamic, Dynamic, Index > &perm) const
Definition: SparseMatrixBase.h:351
SparseInnerVectorSet< Derived, Dynamic > middleCols(Index start, Index size)
Definition: SparseBlock.h:354
Definition: SparseMatrixBase.h:75