Eigen  3.4.90 (git rev a4098ac676528a83cfb73d4d26ce1b42ec05f47c)
SparseMatrixBase.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_SPARSEMATRIXBASE_H
11#define EIGEN_SPARSEMATRIXBASE_H
12
13#include "./InternalHeaderCheck.h"
14
15namespace Eigen {
16
28template<typename Derived> class SparseMatrixBase
29 : public EigenBase<Derived>
30{
31 public:
32
33 typedef typename internal::traits<Derived>::Scalar Scalar;
34
38 typedef Scalar value_type;
39
40 typedef typename internal::packet_traits<Scalar>::type PacketScalar;
41 typedef typename internal::traits<Derived>::StorageKind StorageKind;
42
45 typedef typename internal::traits<Derived>::StorageIndex StorageIndex;
46
47 typedef typename internal::add_const_on_value_type_if_arithmetic<
48 typename internal::packet_traits<Scalar>::type
49 >::type PacketReturnType;
50
52
55
56 template<typename OtherDerived>
57 Derived& operator=(const EigenBase<OtherDerived> &other);
58
59 enum {
60
61 RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
67 ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
74 SizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::RowsAtCompileTime,
75 internal::traits<Derived>::ColsAtCompileTime>::ret),
80 MaxRowsAtCompileTime = RowsAtCompileTime,
81 MaxColsAtCompileTime = ColsAtCompileTime,
82
83 MaxSizeAtCompileTime = (internal::size_at_compile_time<MaxRowsAtCompileTime,
84 MaxColsAtCompileTime>::ret),
85
92 NumDimensions = int(MaxSizeAtCompileTime) == 1 ? 0 : bool(IsVectorAtCompileTime) ? 1 : 2,
97 Flags = internal::traits<Derived>::Flags,
102 IsRowMajor = Flags&RowMajorBit ? 1 : 0,
103
104 InnerSizeAtCompileTime = int(IsVectorAtCompileTime) ? int(SizeAtCompileTime)
105 : int(IsRowMajor) ? int(ColsAtCompileTime) : int(RowsAtCompileTime),
106
107 #ifndef EIGEN_PARSED_BY_DOXYGEN
108 _HasDirectAccess = (int(Flags)&DirectAccessBit) ? 1 : 0 // workaround sunCC
109 #endif
110 };
111
113 typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
116 >::type AdjointReturnType;
118 typedef typename internal::add_const<Transpose<const Derived> >::type ConstTransposeReturnType;
119
120 // FIXME storage order do not match evaluator storage order
122
123#ifndef EIGEN_PARSED_BY_DOXYGEN
130 typedef typename NumTraits<Scalar>::Real RealScalar;
131
134 typedef typename internal::conditional<_HasDirectAccess, const Scalar&, Scalar>::type CoeffReturnType;
135
138
142 typedef Matrix<Scalar, internal::max_size_prefer_dynamic(RowsAtCompileTime, ColsAtCompileTime),
143 internal::max_size_prefer_dynamic(RowsAtCompileTime, ColsAtCompileTime)> SquareMatrixType;
144
145 inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
146 inline Derived& derived() { return *static_cast<Derived*>(this); }
147 inline Derived& const_cast_derived() const
148 { return *static_cast<Derived*>(const_cast<SparseMatrixBase*>(this)); }
149
150 typedef EigenBase<Derived> Base;
151
152#endif // not EIGEN_PARSED_BY_DOXYGEN
153
154#define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::SparseMatrixBase
155#ifdef EIGEN_PARSED_BY_DOXYGEN
156#define EIGEN_DOC_UNARY_ADDONS(METHOD,OP)
157#define EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
158#define EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF(COND)
159#else
160#define EIGEN_DOC_UNARY_ADDONS(X,Y)
161#define EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
162#define EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF(COND)
163#endif
164# include "../plugins/CommonCwiseUnaryOps.h"
165# include "../plugins/CommonCwiseBinaryOps.h"
166# include "../plugins/MatrixCwiseUnaryOps.h"
167# include "../plugins/MatrixCwiseBinaryOps.h"
168# include "../plugins/BlockMethods.h"
169# ifdef EIGEN_SPARSEMATRIXBASE_PLUGIN
170# include EIGEN_SPARSEMATRIXBASE_PLUGIN
171# endif
172#undef EIGEN_CURRENT_STORAGE_BASE_CLASS
173#undef EIGEN_DOC_UNARY_ADDONS
174#undef EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
175#undef EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF
176
178 inline Index rows() const { return derived().rows(); }
180 inline Index cols() const { return derived().cols(); }
183 inline Index size() const { return rows() * cols(); }
188 inline bool isVector() const { return rows()==1 || cols()==1; }
191 Index outerSize() const { return (int(Flags)&RowMajorBit) ? this->rows() : this->cols(); }
194 Index innerSize() const { return (int(Flags)&RowMajorBit) ? this->cols() : this->rows(); }
195
196 bool isRValue() const { return m_isRValue; }
197 Derived& markAsRValue() { m_isRValue = true; return derived(); }
198
199 SparseMatrixBase() : m_isRValue(false) { /* TODO check flags */ }
200
201
202 template<typename OtherDerived>
203 Derived& operator=(const ReturnByValue<OtherDerived>& other);
204
205 template<typename OtherDerived>
206 inline Derived& operator=(const SparseMatrixBase<OtherDerived>& other);
207
208 inline Derived& operator=(const Derived& other);
209
210 protected:
211
212 template<typename OtherDerived>
213 inline Derived& assign(const OtherDerived& other);
214
215 template<typename OtherDerived>
216 inline void assignGeneric(const OtherDerived& other);
217
218 public:
219
220 friend std::ostream & operator << (std::ostream & s, const SparseMatrixBase& m)
221 {
222 typedef typename Derived::Nested Nested;
223 typedef typename internal::remove_all<Nested>::type NestedCleaned;
224
225 if (Flags&RowMajorBit)
226 {
227 Nested nm(m.derived());
228 internal::evaluator<NestedCleaned> thisEval(nm);
229 for (Index row=0; row<nm.outerSize(); ++row)
230 {
231 Index col = 0;
232 for (typename internal::evaluator<NestedCleaned>::InnerIterator it(thisEval, row); it; ++it)
233 {
234 for ( ; col<it.index(); ++col)
235 s << "0 ";
236 s << it.value() << " ";
237 ++col;
238 }
239 for ( ; col<m.cols(); ++col)
240 s << "0 ";
241 s << std::endl;
242 }
243 }
244 else
245 {
246 Nested nm(m.derived());
247 internal::evaluator<NestedCleaned> thisEval(nm);
248 if (m.cols() == 1) {
249 Index row = 0;
250 for (typename internal::evaluator<NestedCleaned>::InnerIterator it(thisEval, 0); it; ++it)
251 {
252 for ( ; row<it.index(); ++row)
253 s << "0" << std::endl;
254 s << it.value() << std::endl;
255 ++row;
256 }
257 for ( ; row<m.rows(); ++row)
258 s << "0" << std::endl;
259 }
260 else
261 {
262 SparseMatrix<Scalar, RowMajorBit, StorageIndex> trans = m;
263 s << static_cast<const SparseMatrixBase<SparseMatrix<Scalar, RowMajorBit, StorageIndex> >&>(trans);
264 }
265 }
266 return s;
267 }
268
269 template<typename OtherDerived>
270 Derived& operator+=(const SparseMatrixBase<OtherDerived>& other);
271 template<typename OtherDerived>
272 Derived& operator-=(const SparseMatrixBase<OtherDerived>& other);
273
274 template<typename OtherDerived>
275 Derived& operator+=(const DiagonalBase<OtherDerived>& other);
276 template<typename OtherDerived>
277 Derived& operator-=(const DiagonalBase<OtherDerived>& other);
278
279 template<typename OtherDerived>
280 Derived& operator+=(const EigenBase<OtherDerived> &other);
281 template<typename OtherDerived>
282 Derived& operator-=(const EigenBase<OtherDerived> &other);
283
284 Derived& operator*=(const Scalar& other);
285 Derived& operator/=(const Scalar& other);
286
287 template<typename OtherDerived> struct CwiseProductDenseReturnType {
288 typedef CwiseBinaryOp<internal::scalar_product_op<typename ScalarBinaryOpTraits<
289 typename internal::traits<Derived>::Scalar,
290 typename internal::traits<OtherDerived>::Scalar
291 >::ReturnType>,
292 const Derived,
293 const OtherDerived
294 > Type;
295 };
296
297 template<typename OtherDerived>
298 EIGEN_STRONG_INLINE const typename CwiseProductDenseReturnType<OtherDerived>::Type
299 cwiseProduct(const MatrixBase<OtherDerived> &other) const;
300
301 // sparse * diagonal
302 template<typename OtherDerived>
303 const Product<Derived,OtherDerived>
304 operator*(const DiagonalBase<OtherDerived> &other) const
305 { return Product<Derived,OtherDerived>(derived(), other.derived()); }
306
307 // diagonal * sparse
308 template<typename OtherDerived> friend
309 const Product<OtherDerived,Derived>
310 operator*(const DiagonalBase<OtherDerived> &lhs, const SparseMatrixBase& rhs)
311 { return Product<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
312
313 // sparse * sparse
314 template<typename OtherDerived>
315 const Product<Derived,OtherDerived,AliasFreeProduct>
316 operator*(const SparseMatrixBase<OtherDerived> &other) const;
317
318 // sparse * dense
319 template<typename OtherDerived>
320 const Product<Derived,OtherDerived>
321 operator*(const MatrixBase<OtherDerived> &other) const
322 { return Product<Derived,OtherDerived>(derived(), other.derived()); }
323
324 // dense * sparse
325 template<typename OtherDerived> friend
326 const Product<OtherDerived,Derived>
327 operator*(const MatrixBase<OtherDerived> &lhs, const SparseMatrixBase& rhs)
328 { return Product<OtherDerived,Derived>(lhs.derived(), rhs.derived()); }
329
331 SparseSymmetricPermutationProduct<Derived,Upper|Lower> twistedBy(const PermutationMatrix<Dynamic,Dynamic,StorageIndex>& perm) const
332 {
333 return SparseSymmetricPermutationProduct<Derived,Upper|Lower>(derived(), perm);
334 }
335
336 template<typename OtherDerived>
337 Derived& operator*=(const SparseMatrixBase<OtherDerived>& other);
338
339 template<int Mode>
340 inline const TriangularView<const Derived, Mode> triangularView() const;
341
342 template<unsigned int UpLo> struct SelfAdjointViewReturnType { typedef SparseSelfAdjointView<Derived, UpLo> Type; };
343 template<unsigned int UpLo> struct ConstSelfAdjointViewReturnType { typedef const SparseSelfAdjointView<const Derived, UpLo> Type; };
344
345 template<unsigned int UpLo> inline
346 typename ConstSelfAdjointViewReturnType<UpLo>::Type selfadjointView() const;
347 template<unsigned int UpLo> inline
348 typename SelfAdjointViewReturnType<UpLo>::Type selfadjointView();
349
350 template<typename OtherDerived> Scalar dot(const MatrixBase<OtherDerived>& other) const;
351 template<typename OtherDerived> Scalar dot(const SparseMatrixBase<OtherDerived>& other) const;
352 RealScalar squaredNorm() const;
353 RealScalar norm() const;
354 RealScalar blueNorm() const;
355
356 TransposeReturnType transpose() { return TransposeReturnType(derived()); }
357 const ConstTransposeReturnType transpose() const { return ConstTransposeReturnType(derived()); }
358 const AdjointReturnType adjoint() const { return AdjointReturnType(transpose()); }
359
360 DenseMatrixType toDense() const
361 {
362 return DenseMatrixType(derived());
363 }
364
365 template<typename OtherDerived>
366 bool isApprox(const SparseMatrixBase<OtherDerived>& other,
367 const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
368
369 template<typename OtherDerived>
370 bool isApprox(const MatrixBase<OtherDerived>& other,
371 const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const
372 { return toDense().isApprox(other,prec); }
373
379 inline const typename internal::eval<Derived>::type eval() const
380 { return typename internal::eval<Derived>::type(derived()); }
381
382 Scalar sum() const;
383
384 inline const SparseView<Derived>
385 pruned(const Scalar& reference = Scalar(0), const RealScalar& epsilon = NumTraits<Scalar>::dummy_precision()) const;
386
387 protected:
388
389 bool m_isRValue;
390
391 static inline StorageIndex convert_index(const Index idx) {
392 return internal::convert_index<StorageIndex>(idx);
393 }
394 private:
395 template<typename Dest> void evalTo(Dest &) const;
396};
397
398} // end namespace Eigen
399
400#endif // EIGEN_SPARSEMATRIXBASE_H
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:63
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:58
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition: Diagonal.h:67
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:30
internal::traits< Derived >::StorageIndex StorageIndex
Definition: SparseMatrixBase.h:45
Index size() const
Definition: SparseMatrixBase.h:183
Index innerSize() const
Definition: SparseMatrixBase.h:194
Index rows() const
Definition: SparseMatrixBase.h:178
bool isVector() const
Definition: SparseMatrixBase.h:188
@ IsVectorAtCompileTime
Definition: SparseMatrixBase.h:86
@ NumDimensions
Definition: SparseMatrixBase.h:92
@ ColsAtCompileTime
Definition: SparseMatrixBase.h:67
@ Flags
Definition: SparseMatrixBase.h:97
@ RowsAtCompileTime
Definition: SparseMatrixBase.h:61
@ SizeAtCompileTime
Definition: SparseMatrixBase.h:74
Scalar value_type
Definition: SparseMatrixBase.h:38
Index outerSize() const
Definition: SparseMatrixBase.h:191
const SparseView< Derived > pruned(const Scalar &reference=Scalar(0), const RealScalar &epsilon=NumTraits< Scalar >::dummy_precision()) const
Definition: SparseView.h:248
Index cols() const
Definition: SparseMatrixBase.h:180
SparseSymmetricPermutationProduct< Derived, Upper|Lower > twistedBy(const PermutationMatrix< Dynamic, Dynamic, StorageIndex > &perm) const
Definition: SparseMatrixBase.h:331
const internal::eval< Derived >::type eval() const
Definition: SparseMatrixBase.h:379
A versatible sparse matrix representation.
Definition: SparseMatrix.h:100
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
Definition: SparseSelfAdjointView.h:47
Expression of a dense or sparse matrix with zero or too small values removed.
Definition: SparseView.h:48
Expression of the transpose of a matrix.
Definition: Transpose.h:56
Expression of a triangular part in a matrix.
Definition: TriangularMatrix.h:191
const unsigned int DirectAccessBit
Definition: Constants.h:157
const unsigned int RowMajorBit
Definition: Constants.h:68
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
Definition: EigenBase.h:32
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41
Derived & derived()
Definition: EigenBase.h:48
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:235