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
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)
203 if ((!transpose) && other.isRValue())
206 derived().resize(other.rows(), other.cols());
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) ||
232 (!((
Flags & RowMajorBit) != (OtherDerived::Flags & RowMajorBit)))) &&
233 "the transpose operation is supposed to be handled in SparseMatrix::operator=");
235 enum { Flip = (
Flags &
RowMajorBit) != (OtherDerived::Flags & RowMajorBit) };
237 const Index outerSize = other.outerSize();
240 Derived temp(other.rows(), other.cols());
242 temp.reserve((std::max)(this->
rows(),this->
cols())*2);
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;
267 if (
Flags&RowMajorBit)
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>
346 operator*(
const DiagonalBase<OtherDerived> &lhs,
const SparseMatrixBase& rhs)
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>
369 #ifdef EIGEN2_SUPPORT
371 template<
typename OtherDerived>
376 template<
typename OtherDerived>
378 #endif // EIGEN2_SUPPORT
381 inline const SparseTriangularView<Derived, Mode> triangularView()
const;
388 RealScalar squaredNorm()
const;
389 RealScalar norm()
const;
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
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>
437 bool isApprox(
const MatrixBase<OtherDerived>& other,
438 RealScalar prec = NumTraits<Scalar>::dummy_precision())
const
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