10 #ifndef EIGEN_SPARSEMATRIXBASE_H
11 #define EIGEN_SPARSEMATRIXBASE_H
26 template<
typename Derived>
class SparseMatrixBase
27 #ifndef EIGEN_PARSED_BY_DOXYGEN
28 :
public internal::special_scalar_op_base<Derived,typename internal::traits<Derived>::Scalar,
29 typename NumTraits<typename internal::traits<Derived>::Scalar>::Real,
33 #endif // not EIGEN_PARSED_BY_DOXYGEN
37 typedef typename internal::traits<Derived>::Scalar Scalar;
38 typedef typename internal::packet_traits<Scalar>::type PacketScalar;
39 typedef typename internal::traits<Derived>::StorageKind StorageKind;
40 typedef typename internal::traits<Derived>::Index Index;
41 typedef typename internal::add_const_on_value_type_if_arithmetic<
42 typename internal::packet_traits<Scalar>::type
43 >::type PacketReturnType;
47 template<
typename OtherDerived>
69 SizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::RowsAtCompileTime,
70 internal::traits<Derived>::ColsAtCompileTime>::ret),
78 MaxSizeAtCompileTime = (internal::size_at_compile_time<MaxRowsAtCompileTime,
79 MaxColsAtCompileTime>::ret),
87 Flags = internal::traits<Derived>::Flags,
102 #ifndef EIGEN_PARSED_BY_DOXYGEN
108 typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
111 >::type AdjointReturnType;
117 #ifndef EIGEN_PARSED_BY_DOXYGEN
128 typedef typename internal::conditional<_HasDirectAccess, const Scalar&, Scalar>::type CoeffReturnType;
137 inline const Derived&
derived()
const {
return *
static_cast<const Derived*
>(
this); }
138 inline Derived&
derived() {
return *
static_cast<Derived*
>(
this); }
139 inline Derived& const_cast_derived()
const
142 typedef internal::special_scalar_op_base<Derived, Scalar, RealScalar, EigenBase<Derived> > Base;
143 using Base::operator*;
144 #endif // not EIGEN_PARSED_BY_DOXYGEN
146 #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::SparseMatrixBase
147 # include "../plugins/CommonCwiseUnaryOps.h"
148 # include "../plugins/CommonCwiseBinaryOps.h"
149 # include "../plugins/MatrixCwiseUnaryOps.h"
150 # include "../plugins/MatrixCwiseBinaryOps.h"
151 # include "../plugins/BlockMethods.h"
152 # ifdef EIGEN_SPARSEMATRIXBASE_PLUGIN
153 # include EIGEN_SPARSEMATRIXBASE_PLUGIN
155 # undef EIGEN_CURRENT_STORAGE_BASE_CLASS
156 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
180 bool isRValue()
const {
return m_isRValue; }
181 Derived& markAsRValue() { m_isRValue =
true;
return derived(); }
183 SparseMatrixBase() : m_isRValue(false) { }
186 template<
typename OtherDerived>
187 Derived& operator=(
const ReturnByValue<OtherDerived>& other)
194 template<
typename OtherDerived>
197 return assign(other.
derived());
200 inline Derived& operator=(
const Derived& other)
205 return assign(other.derived());
210 template<
typename OtherDerived>
211 inline Derived& assign(
const OtherDerived& other)
215 if ((!transpose) && other.isRValue())
218 derived().resize(other.rows(), other.cols());
224 for (
typename OtherDerived::InnerIterator it(other, j); it; ++it)
226 Scalar v = it.value();
227 derived().insertBackByOuterInner(j,it.index()) = v;
234 assignGeneric(other);
239 template<
typename OtherDerived>
240 inline void assignGeneric(
const OtherDerived& other)
243 eigen_assert(( ((internal::traits<Derived>::SupportedAccessPatterns&OuterRandomAccessPattern)==OuterRandomAccessPattern) ||
244 (!((
Flags & RowMajorBit) != (OtherDerived::Flags & RowMajorBit)))) &&
245 "the transpose operation is supposed to be handled in SparseMatrix::operator=");
247 enum { Flip = (
Flags &
RowMajorBit) != (OtherDerived::Flags & RowMajorBit) };
249 const Index outerSize = other.outerSize();
252 Derived temp(other.rows(), other.cols());
254 temp.reserve((std::max)(this->
rows(),this->
cols())*2);
258 for (
typename OtherDerived::InnerIterator it(other.derived(), j); it; ++it)
260 Scalar v = it.value();
261 temp.insertBackByOuterInner(Flip?it.index():j,Flip?j:it.index()) = v;
266 derived() = temp.markAsRValue();
271 template<
typename Lhs,
typename Rhs>
272 inline Derived& operator=(
const SparseSparseProduct<Lhs,Rhs>& product);
274 friend std::ostream & operator << (std::ostream & s,
const SparseMatrixBase& m)
276 typedef typename Derived::Nested Nested;
277 typedef typename internal::remove_all<Nested>::type NestedCleaned;
279 if (
Flags&RowMajorBit)
281 const Nested nm(m.derived());
282 for (Index
row=0;
row<nm.outerSize(); ++
row)
285 for (
typename NestedCleaned::InnerIterator it(nm.derived(),
row); it; ++it)
287 for ( ; col<it.index(); ++
col)
289 s << it.value() <<
" ";
292 for ( ; col<m.cols(); ++
col)
299 const Nested nm(m.derived());
302 for (
typename NestedCleaned::InnerIterator it(nm.derived(), 0); it; ++it)
304 for ( ; row<it.index(); ++
row)
305 s <<
"0" << std::endl;
306 s << it.value() << std::endl;
309 for ( ; row<m.rows(); ++
row)
310 s <<
"0" << std::endl;
315 s << static_cast<const SparseMatrixBase<SparseMatrix<Scalar, RowMajorBit, Index> >&>(trans);
321 template<
typename OtherDerived>
322 Derived& operator+=(
const SparseMatrixBase<OtherDerived>& other);
323 template<
typename OtherDerived>
324 Derived& operator-=(
const SparseMatrixBase<OtherDerived>& other);
326 Derived& operator*=(
const Scalar& other);
327 Derived& operator/=(
const Scalar& other);
329 template<
typename OtherDerived>
struct CwiseProductDenseReturnType {
330 typedef CwiseBinaryOp<internal::scalar_product_op<
typename internal::scalar_product_traits<
331 typename internal::traits<Derived>::Scalar,
332 typename internal::traits<OtherDerived>::Scalar
339 template<
typename OtherDerived>
340 EIGEN_STRONG_INLINE
const typename CwiseProductDenseReturnType<OtherDerived>::Type
344 template<
typename OtherDerived>
345 const typename SparseSparseProductReturnType<Derived,OtherDerived>::Type
349 template<
typename OtherDerived>
350 const SparseDiagonalProduct<Derived,OtherDerived>
351 operator*(
const DiagonalBase<OtherDerived> &other)
const;
354 template<
typename OtherDerived>
friend
355 const SparseDiagonalProduct<OtherDerived,Derived>
357 {
return SparseDiagonalProduct<OtherDerived,Derived>(lhs.derived(), rhs.
derived()); }
360 template<
typename OtherDerived>
friend
361 const typename DenseSparseProductReturnType<OtherDerived,Derived>::Type
363 {
return typename DenseSparseProductReturnType<OtherDerived,Derived>::Type(lhs.derived(),rhs); }
366 template<
typename OtherDerived>
367 const typename SparseDenseProductReturnType<Derived,OtherDerived>::Type
369 {
return typename SparseDenseProductReturnType<Derived,OtherDerived>::Type(
derived(), other.derived()); }
374 return SparseSymmetricPermutationProduct<Derived,Upper|Lower>(
derived(), perm);
377 template<
typename OtherDerived>
380 #ifdef EIGEN2_SUPPORT
382 template<
typename OtherDerived>
387 template<
typename OtherDerived>
389 #endif // EIGEN2_SUPPORT
392 inline const SparseTriangularView<Derived, Mode> triangularView()
const;
399 RealScalar squaredNorm()
const;
400 RealScalar norm()
const;
401 RealScalar blueNorm()
const;
405 const AdjointReturnType adjoint()
const {
return transpose(); }
411 const ConstInnerVectorReturnType
innerVector(Index outer)
const;
416 InnerVectorsReturnType
innerVectors(Index outerStart, Index outerSize);
417 const ConstInnerVectorsReturnType
innerVectors(Index outerStart, Index outerSize)
const;
420 template<
typename DenseDerived>
425 for (
typename Derived::InnerIterator i(
derived(),j); i; ++i)
426 dst.coeffRef(i.row(),i.col()) = i.
value();
434 template<
typename OtherDerived>
437 {
return toDense().isApprox(other.toDense(),prec); }
439 template<
typename OtherDerived>
442 {
return toDense().isApprox(other,prec); }
449 inline const typename internal::eval<Derived>::type
eval()
const
450 {
return typename internal::eval<Derived>::type(
derived()); }
461 #endif // EIGEN_SPARSEMATRIXBASE_H
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:49
friend const DenseSparseProductReturnType< OtherDerived, Derived >::Type operator*(const MatrixBase< OtherDerived > &lhs, const Derived &rhs)
Definition: SparseMatrixBase.h:362
const internal::eval< Derived >::type eval() const
Definition: SparseMatrixBase.h:449
Definition: SparseMatrixBase.h:81
A versatible sparse matrix representation.
Definition: SparseMatrix.h:85
RowXpr row(Index i)
Definition: SparseMatrixBase.h:750
Expression of the transpose of a matrix.
Definition: Transpose.h:57
Derived & setZero()
Definition: CwiseNullaryOp.h:499
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
Definition: SparseSelfAdjointView.h:49
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:88
SparseSymmetricPermutationProduct< Derived, Upper|Lower > twistedBy(const PermutationMatrix< Dynamic, Dynamic, Index > &perm) const
Definition: SparseMatrixBase.h:372
const CwiseBinaryOp< internal::scalar_product_op< typename Derived::Scalar, typename OtherDerived::Scalar >, const Derived, const OtherDerived > cwiseProduct(const Eigen::SparseMatrixBase< OtherDerived > &other) const
Definition: SparseMatrixBase.h:23
Index outerSize() const
Definition: SparseMatrixBase.h:175
Definition: EigenBase.h:26
Definition: SparseMatrixBase.h:62
Index cols() const
Definition: SparseMatrixBase.h:161
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:107
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:239
Derived & derived()
Definition: EigenBase.h:34
Definition: SparseMatrixBase.h:87
Index size() const
Definition: SparseMatrixBase.h:164
const ScalarMultipleReturnType operator*(const Scalar &scalar) const
Definition: SparseMatrixBase.h:50
Index innerSize() const
Definition: SparseMatrixBase.h:178
Definition: SparseMatrixBase.h:69
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:103
bool isVector() const
Definition: SparseMatrixBase.h:172
Index nonZeros() const
Definition: SparseMatrixBase.h:167
const unsigned int RowMajorBit
Definition: Constants.h:53
InnerVectorReturnType innerVector(Index outer)
Definition: SparseBlock.h:380
Definition: SparseMatrixBase.h:56
const unsigned int DirectAccessBit
Definition: Constants.h:142
CoeffReturnType value() const
Definition: DenseBase.h:422
Definition: SparseMatrixBase.h:92
InnerVectorsReturnType innerVectors(Index outerStart, Index outerSize)
Definition: SparseBlock.h:395
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
Index rows() const
Definition: SparseMatrixBase.h:159
const SparseDenseProductReturnType< Derived, OtherDerived >::Type operator*(const MatrixBase< OtherDerived > &other) const
Definition: SparseMatrixBase.h:368
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
ColXpr col(Index i)
Definition: SparseMatrixBase.h:733