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::traits<Derived>::Index StorageIndex;
42 typedef typename internal::add_const_on_value_type_if_arithmetic<
43 typename internal::packet_traits<Scalar>::type
44 >::type PacketReturnType;
48 template<
typename OtherDerived>
70 SizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::RowsAtCompileTime,
71 internal::traits<Derived>::ColsAtCompileTime>::ret),
79 MaxSizeAtCompileTime = (internal::size_at_compile_time<MaxRowsAtCompileTime,
80 MaxColsAtCompileTime>::ret),
88 Flags = internal::traits<Derived>::Flags,
103 #ifndef EIGEN_PARSED_BY_DOXYGEN
109 typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
112 >::type AdjointReturnType;
118 #ifndef EIGEN_PARSED_BY_DOXYGEN
129 typedef typename internal::conditional<_HasDirectAccess, const Scalar&, Scalar>::type CoeffReturnType;
138 inline const Derived&
derived()
const {
return *
static_cast<const Derived*
>(
this); }
139 inline Derived&
derived() {
return *
static_cast<Derived*
>(
this); }
140 inline Derived& const_cast_derived()
const
143 typedef internal::special_scalar_op_base<Derived, Scalar, RealScalar, EigenBase<Derived> > Base;
144 using Base::operator*;
145 #endif // not EIGEN_PARSED_BY_DOXYGEN
147 #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::SparseMatrixBase
148 # include "../plugins/CommonCwiseUnaryOps.h"
149 # include "../plugins/CommonCwiseBinaryOps.h"
150 # include "../plugins/MatrixCwiseUnaryOps.h"
151 # include "../plugins/MatrixCwiseBinaryOps.h"
152 # include "../plugins/BlockMethods.h"
153 # ifdef EIGEN_SPARSEMATRIXBASE_PLUGIN
154 # include EIGEN_SPARSEMATRIXBASE_PLUGIN
156 # undef EIGEN_CURRENT_STORAGE_BASE_CLASS
157 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
181 bool isRValue()
const {
return m_isRValue; }
182 Derived& markAsRValue() { m_isRValue =
true;
return derived(); }
184 SparseMatrixBase() : m_isRValue(false) { }
187 template<
typename OtherDerived>
188 Derived& operator=(
const ReturnByValue<OtherDerived>& other)
195 template<
typename OtherDerived>
198 return assign(other.
derived());
201 inline Derived& operator=(
const Derived& other)
206 return assign(other.derived());
211 template<
typename OtherDerived>
212 inline Derived& assign(
const OtherDerived& other)
216 if ((!transpose) && other.isRValue())
219 derived().resize(other.rows(), other.cols());
225 for (
typename OtherDerived::InnerIterator it(other, j); it; ++it)
227 Scalar v = it.value();
228 derived().insertBackByOuterInner(j,it.index()) = v;
235 assignGeneric(other);
240 template<
typename OtherDerived>
241 inline void assignGeneric(
const OtherDerived& other)
244 eigen_assert(( ((internal::traits<Derived>::SupportedAccessPatterns&OuterRandomAccessPattern)==OuterRandomAccessPattern) ||
245 (!((
Flags & RowMajorBit) != (OtherDerived::Flags & RowMajorBit)))) &&
246 "the transpose operation is supposed to be handled in SparseMatrix::operator=");
248 enum { Flip = (
Flags &
RowMajorBit) != (OtherDerived::Flags & RowMajorBit) };
250 const Index outerSize = other.outerSize();
253 Derived temp(other.rows(), other.cols());
255 temp.reserve((std::max)(this->
rows(),this->
cols())*2);
259 for (
typename OtherDerived::InnerIterator it(other.derived(), j); it; ++it)
261 Scalar v = it.value();
262 temp.insertBackByOuterInner(Flip?it.index():j,Flip?j:it.index()) = v;
267 derived() = temp.markAsRValue();
272 template<
typename Lhs,
typename Rhs>
273 inline Derived& operator=(
const SparseSparseProduct<Lhs,Rhs>& product);
275 friend std::ostream & operator << (std::ostream & s,
const SparseMatrixBase& m)
277 typedef typename Derived::Nested Nested;
278 typedef typename internal::remove_all<Nested>::type NestedCleaned;
280 if (
Flags&RowMajorBit)
282 const Nested nm(m.derived());
286 for (
typename NestedCleaned::InnerIterator it(nm.derived(),
row); it; ++it)
288 for ( ; col<it.index(); ++
col)
290 s << it.value() <<
" ";
293 for ( ; col<m.cols(); ++
col)
300 const Nested nm(m.derived());
303 for (
typename NestedCleaned::InnerIterator it(nm.derived(), 0); it; ++it)
305 for ( ; row<it.index(); ++
row)
306 s <<
"0" << std::endl;
307 s << it.value() << std::endl;
310 for ( ; row<m.rows(); ++
row)
311 s <<
"0" << std::endl;
316 s << static_cast<const SparseMatrixBase<SparseMatrix<Scalar, RowMajorBit, Index> >&>(trans);
322 template<
typename OtherDerived>
323 Derived& operator+=(
const SparseMatrixBase<OtherDerived>& other);
324 template<
typename OtherDerived>
325 Derived& operator-=(
const SparseMatrixBase<OtherDerived>& other);
327 Derived& operator*=(
const Scalar& other);
328 Derived& operator/=(
const Scalar& other);
330 template<
typename OtherDerived>
struct CwiseProductDenseReturnType {
331 typedef CwiseBinaryOp<internal::scalar_product_op<
typename internal::scalar_product_traits<
332 typename internal::traits<Derived>::Scalar,
333 typename internal::traits<OtherDerived>::Scalar
340 template<
typename OtherDerived>
341 EIGEN_STRONG_INLINE
const typename CwiseProductDenseReturnType<OtherDerived>::Type
345 template<
typename OtherDerived>
346 const typename SparseSparseProductReturnType<Derived,OtherDerived>::Type
350 template<
typename OtherDerived>
351 const SparseDiagonalProduct<Derived,OtherDerived>
352 operator*(
const DiagonalBase<OtherDerived> &other)
const;
355 template<
typename OtherDerived>
friend
356 const SparseDiagonalProduct<OtherDerived,Derived>
358 {
return SparseDiagonalProduct<OtherDerived,Derived>(lhs.derived(), rhs.
derived()); }
361 template<
typename OtherDerived>
friend
362 const typename DenseSparseProductReturnType<OtherDerived,Derived>::Type
364 {
return typename DenseSparseProductReturnType<OtherDerived,Derived>::Type(lhs.derived(),rhs); }
367 template<
typename OtherDerived>
368 const typename SparseDenseProductReturnType<Derived,OtherDerived>::Type
370 {
return typename SparseDenseProductReturnType<Derived,OtherDerived>::Type(
derived(), other.derived()); }
375 return SparseSymmetricPermutationProduct<Derived,Upper|Lower>(
derived(), perm);
378 template<
typename OtherDerived>
381 #ifdef EIGEN2_SUPPORT
383 template<
typename OtherDerived>
388 template<
typename OtherDerived>
390 #endif // EIGEN2_SUPPORT
393 inline const SparseTriangularView<Derived, Mode> triangularView()
const;
400 RealScalar squaredNorm()
const;
401 RealScalar norm()
const;
402 RealScalar blueNorm()
const;
406 const AdjointReturnType adjoint()
const {
return transpose(); }
412 const ConstInnerVectorReturnType
innerVector(Index outer)
const;
417 InnerVectorsReturnType
innerVectors(Index outerStart, Index outerSize);
418 const ConstInnerVectorsReturnType
innerVectors(Index outerStart, Index outerSize)
const;
421 template<
typename DenseDerived>
426 for (
typename Derived::InnerIterator i(
derived(),j); i; ++i)
427 dst.coeffRef(i.row(),i.col()) = i.
value();
435 template<
typename OtherDerived>
438 {
return toDense().isApprox(other.toDense(),prec); }
440 template<
typename OtherDerived>
443 {
return toDense().isApprox(other,prec); }
450 inline const typename internal::eval<Derived>::type
eval()
const
451 {
return typename internal::eval<Derived>::type(
derived()); }
462 #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:363
const internal::eval< Derived >::type eval() const
Definition: SparseMatrixBase.h:450
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
Definition: SparseMatrixBase.h:63
Definition: SparseMatrixBase.h:82
Definition: SparseMatrixBase.h:93
Definition: SparseMatrixBase.h:70
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:373
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:176
Definition: EigenBase.h:26
Index cols() const
Definition: SparseMatrixBase.h:162
Definition: SparseMatrixBase.h:57
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
Index size() const
Definition: SparseMatrixBase.h:165
const ScalarMultipleReturnType operator*(const Scalar &scalar) const
Definition: SparseMatrixBase.h:50
Index innerSize() const
Definition: SparseMatrixBase.h:179
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:103
bool isVector() const
Definition: SparseMatrixBase.h:173
Index nonZeros() const
Definition: SparseMatrixBase.h:168
const unsigned int RowMajorBit
Definition: Constants.h:53
InnerVectorReturnType innerVector(Index outer)
Definition: SparseBlock.h:380
const unsigned int DirectAccessBit
Definition: Constants.h:142
CoeffReturnType value() const
Definition: DenseBase.h:422
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:160
const SparseDenseProductReturnType< Derived, OtherDerived >::Type operator*(const MatrixBase< OtherDerived > &other) const
Definition: SparseMatrixBase.h:369
Definition: SparseMatrixBase.h:88
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
ColXpr col(Index i)
Definition: SparseMatrixBase.h:733