Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
Loading...
Searching...
No Matches
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 <[email protected]>
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// IWYU pragma: private
14#include "./InternalHeaderCheck.h"
15
16namespace Eigen {
17
29template <typename Derived>
30class SparseMatrixBase : public EigenBase<Derived> {
31 public:
32 typedef typename internal::traits<Derived>::Scalar Scalar;
33
37 typedef Scalar value_type;
38
39 typedef typename internal::packet_traits<Scalar>::type PacketScalar;
40 typedef typename internal::traits<Derived>::StorageKind StorageKind;
41
44 typedef typename internal::traits<Derived>::StorageIndex StorageIndex;
45
46 typedef typename internal::add_const_on_value_type_if_arithmetic<typename internal::packet_traits<Scalar>::type>::type
47 PacketReturnType;
48
50
53
54 template <typename OtherDerived>
55 Derived& operator=(const EigenBase<OtherDerived>& other);
56
57 enum {
58
59 RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
65 ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
71 SizeAtCompileTime = (internal::size_of_xpr_at_compile_time<Derived>::ret),
76 MaxRowsAtCompileTime = RowsAtCompileTime,
77 MaxColsAtCompileTime = ColsAtCompileTime,
78
79 MaxSizeAtCompileTime = internal::size_at_compile_time(MaxRowsAtCompileTime, MaxColsAtCompileTime),
80
87 NumDimensions = int(MaxSizeAtCompileTime) == 1 ? 0
88 : bool(IsVectorAtCompileTime) ? 1
89 : 2,
94 Flags = internal::traits<Derived>::Flags,
99 IsRowMajor = Flags & RowMajorBit ? 1 : 0,
100
101 InnerSizeAtCompileTime = int(IsVectorAtCompileTime) ? int(SizeAtCompileTime)
102 : int(IsRowMajor) ? int(ColsAtCompileTime)
103 : int(RowsAtCompileTime),
104
105#ifndef EIGEN_PARSED_BY_DOXYGEN
106 HasDirectAccess_ = (int(Flags) & DirectAccessBit) ? 1 : 0 // workaround sunCC
107#endif
108 };
109
111 typedef std::conditional_t<NumTraits<Scalar>::IsComplex,
114 AdjointReturnType;
117
118 // FIXME storage order do not match evaluator storage order
120
121#ifndef EIGEN_PARSED_BY_DOXYGEN
128 typedef typename NumTraits<Scalar>::Real RealScalar;
129
132 typedef std::conditional_t<HasDirectAccess_, const Scalar&, Scalar> CoeffReturnType;
133
136
140 typedef Matrix<Scalar, internal::max_size_prefer_dynamic(RowsAtCompileTime, ColsAtCompileTime),
141 internal::max_size_prefer_dynamic(RowsAtCompileTime, ColsAtCompileTime)>
142 SquareMatrixType;
143
144 inline const Derived& derived() const { return *static_cast<const Derived*>(this); }
145 inline Derived& derived() { return *static_cast<Derived*>(this); }
146 inline Derived& const_cast_derived() const { return *static_cast<Derived*>(const_cast<SparseMatrixBase*>(this)); }
147
148 typedef EigenBase<Derived> Base;
149
150#endif // not EIGEN_PARSED_BY_DOXYGEN
151
152#define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::SparseMatrixBase
153#ifdef EIGEN_PARSED_BY_DOXYGEN
154#define EIGEN_DOC_UNARY_ADDONS(METHOD, \
155 OP)
157#define EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
160#define EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF( \
161 COND)
163#else
164#define EIGEN_DOC_UNARY_ADDONS(X, Y)
165#define EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
166#define EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF(COND)
167#endif
168#include "../plugins/CommonCwiseUnaryOps.inc"
169#include "../plugins/CommonCwiseBinaryOps.inc"
170#include "../plugins/MatrixCwiseUnaryOps.inc"
171#include "../plugins/MatrixCwiseBinaryOps.inc"
172#include "../plugins/BlockMethods.inc"
173#ifdef EIGEN_SPARSEMATRIXBASE_PLUGIN
174#include EIGEN_SPARSEMATRIXBASE_PLUGIN
175#endif
176#undef EIGEN_CURRENT_STORAGE_BASE_CLASS
177#undef EIGEN_DOC_UNARY_ADDONS
178#undef EIGEN_DOC_BLOCK_ADDONS_NOT_INNER_PANEL
179#undef EIGEN_DOC_BLOCK_ADDONS_INNER_PANEL_IF
180
182 inline Index rows() const { return derived().rows(); }
184 inline Index cols() const { return derived().cols(); }
187 inline Index size() const { return rows() * cols(); }
192 inline bool isVector() const { return rows() == 1 || cols() == 1; }
195 Index outerSize() const { return (int(Flags) & RowMajorBit) ? this->rows() : this->cols(); }
198 Index innerSize() const { return (int(Flags) & RowMajorBit) ? this->cols() : this->rows(); }
199
200 bool isRValue() const { return m_isRValue; }
201 Derived& markAsRValue() {
202 m_isRValue = true;
203 return derived();
204 }
205
206 SparseMatrixBase() : m_isRValue(false) { /* TODO check flags */
207 }
208
209 template <typename OtherDerived>
210 Derived& operator=(const ReturnByValue<OtherDerived>& other);
211
212 template <typename OtherDerived>
213 inline Derived& operator=(const SparseMatrixBase<OtherDerived>& other);
214
215 inline Derived& operator=(const Derived& other);
216
217 protected:
218 template <typename OtherDerived>
219 inline Derived& assign(const OtherDerived& other);
220
221 template <typename OtherDerived>
222 inline void assignGeneric(const OtherDerived& other);
223
224 public:
225#ifndef EIGEN_NO_IO
226 friend std::ostream& operator<<(std::ostream& s, const SparseMatrixBase& m) {
227 typedef typename Derived::Nested Nested;
228 typedef internal::remove_all_t<Nested> NestedCleaned;
229
230 if (Flags & RowMajorBit) {
231 Nested nm(m.derived());
232 internal::evaluator<NestedCleaned> thisEval(nm);
233 for (Index row = 0; row < nm.outerSize(); ++row) {
234 Index col = 0;
235 for (typename internal::evaluator<NestedCleaned>::InnerIterator it(thisEval, row); it; ++it) {
236 for (; col < it.index(); ++col) s << "0 ";
237 s << it.value() << " ";
238 ++col;
239 }
240 for (; col < m.cols(); ++col) s << "0 ";
241 s << std::endl;
242 }
243 } else {
244 Nested nm(m.derived());
245 internal::evaluator<NestedCleaned> thisEval(nm);
246 if (m.cols() == 1) {
247 Index row = 0;
248 for (typename internal::evaluator<NestedCleaned>::InnerIterator it(thisEval, 0); it; ++it) {
249 for (; row < it.index(); ++row) s << "0" << std::endl;
250 s << it.value() << std::endl;
251 ++row;
252 }
253 for (; row < m.rows(); ++row) s << "0" << std::endl;
254 } else {
255 SparseMatrix<Scalar, RowMajorBit, StorageIndex> trans = m;
256 s << static_cast<const SparseMatrixBase<SparseMatrix<Scalar, RowMajorBit, StorageIndex> >&>(trans);
257 }
258 }
259 return s;
260 }
261#endif
262
263 template <typename OtherDerived>
264 Derived& operator+=(const SparseMatrixBase<OtherDerived>& other);
265 template <typename OtherDerived>
266 Derived& operator-=(const SparseMatrixBase<OtherDerived>& other);
267
268 template <typename OtherDerived>
269 Derived& operator+=(const DiagonalBase<OtherDerived>& other);
270 template <typename OtherDerived>
271 Derived& operator-=(const DiagonalBase<OtherDerived>& other);
272
273 template <typename OtherDerived>
274 Derived& operator+=(const EigenBase<OtherDerived>& other);
275 template <typename OtherDerived>
276 Derived& operator-=(const EigenBase<OtherDerived>& other);
277
278 Derived& operator*=(const Scalar& other);
279 Derived& operator/=(const Scalar& other);
280
281 template <typename OtherDerived>
282 struct CwiseProductDenseReturnType {
283 typedef CwiseBinaryOp<
284 internal::scalar_product_op<typename ScalarBinaryOpTraits<
285 typename internal::traits<Derived>::Scalar, typename internal::traits<OtherDerived>::Scalar>::ReturnType>,
286 const Derived, const OtherDerived>
287 Type;
288 };
289
290 template <typename OtherDerived>
291 EIGEN_STRONG_INLINE const typename CwiseProductDenseReturnType<OtherDerived>::Type cwiseProduct(
292 const MatrixBase<OtherDerived>& other) const;
293
294 // sparse * diagonal
295 template <typename OtherDerived>
296 const Product<Derived, OtherDerived> operator*(const DiagonalBase<OtherDerived>& other) const {
297 return Product<Derived, OtherDerived>(derived(), other.derived());
298 }
299
300 // diagonal * sparse
301 template <typename OtherDerived>
302 friend const Product<OtherDerived, Derived> operator*(const DiagonalBase<OtherDerived>& lhs,
303 const SparseMatrixBase& rhs) {
304 return Product<OtherDerived, Derived>(lhs.derived(), rhs.derived());
305 }
306
307 // sparse * sparse
308 template <typename OtherDerived>
309 const Product<Derived, OtherDerived, AliasFreeProduct> operator*(const SparseMatrixBase<OtherDerived>& other) const;
310
311 // sparse * dense
312 template <typename OtherDerived>
313 const Product<Derived, OtherDerived> operator*(const MatrixBase<OtherDerived>& other) const {
314 return Product<Derived, OtherDerived>(derived(), other.derived());
315 }
316
317 // dense * sparse
318 template <typename OtherDerived>
319 friend const Product<OtherDerived, Derived> operator*(const MatrixBase<OtherDerived>& lhs,
320 const SparseMatrixBase& rhs) {
321 return Product<OtherDerived, Derived>(lhs.derived(), rhs.derived());
322 }
323
325 SparseSymmetricPermutationProduct<Derived, Upper | Lower> twistedBy(
327 return SparseSymmetricPermutationProduct<Derived, Upper | Lower>(derived(), perm);
328 }
329
330 template <typename OtherDerived>
331 Derived& operator*=(const SparseMatrixBase<OtherDerived>& other);
332
333 template <int Mode>
334 inline const TriangularView<const Derived, Mode> triangularView() const;
335
336 template <unsigned int UpLo>
337 struct SelfAdjointViewReturnType {
339 };
340 template <unsigned int UpLo>
341 struct ConstSelfAdjointViewReturnType {
342 typedef const SparseSelfAdjointView<const Derived, UpLo> Type;
343 };
344
345 template <unsigned int UpLo>
346 inline typename ConstSelfAdjointViewReturnType<UpLo>::Type selfadjointView() const;
347 template <unsigned int UpLo>
348 inline typename SelfAdjointViewReturnType<UpLo>::Type selfadjointView();
349
350 template <typename OtherDerived>
351 Scalar dot(const MatrixBase<OtherDerived>& other) const;
352 template <typename OtherDerived>
353 Scalar dot(const SparseMatrixBase<OtherDerived>& other) const;
354 RealScalar squaredNorm() const;
355 RealScalar norm() const;
356 RealScalar blueNorm() const;
357
358 TransposeReturnType transpose() { return TransposeReturnType(derived()); }
359 const ConstTransposeReturnType transpose() const { return ConstTransposeReturnType(derived()); }
360 const AdjointReturnType adjoint() const { return AdjointReturnType(transpose()); }
361
362 DenseMatrixType toDense() const { return DenseMatrixType(derived()); }
363
364 template <typename OtherDerived>
365 bool isApprox(const SparseMatrixBase<OtherDerived>& other,
366 const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
367
368 template <typename OtherDerived>
369 bool isApprox(const MatrixBase<OtherDerived>& other,
370 const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const {
371 return toDense().isApprox(other, prec);
372 }
373
379 inline const typename internal::eval<Derived>::type eval() const {
380 return typename internal::eval<Derived>::type(derived());
381 }
382
383 Scalar sum() const;
384
385 inline const SparseView<Derived> pruned(const Scalar& reference = Scalar(0),
386 const RealScalar& epsilon = NumTraits<Scalar>::dummy_precision()) const;
387
388 protected:
389 bool m_isRValue;
390
391 static inline StorageIndex convert_index(const Index idx) { return internal::convert_index<StorageIndex>(idx); }
392
393 private:
394 template <typename Dest>
395 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:64
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition CwiseUnaryOp.h:53
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:186
Permutation matrix.
Definition PermutationMatrix.h:280
Base class of any sparse matrices or sparse expressions.
Definition SparseMatrixBase.h:30
internal::traits< Derived >::StorageIndex StorageIndex
Definition SparseMatrixBase.h:44
Index size() const
Definition SparseMatrixBase.h:187
Index innerSize() const
Definition SparseMatrixBase.h:198
Index rows() const
Definition SparseMatrixBase.h:182
@ IsVectorAtCompileTime
Definition SparseMatrixBase.h:81
@ NumDimensions
Definition SparseMatrixBase.h:87
@ ColsAtCompileTime
Definition SparseMatrixBase.h:65
@ Flags
Definition SparseMatrixBase.h:94
@ RowsAtCompileTime
Definition SparseMatrixBase.h:59
@ SizeAtCompileTime
Definition SparseMatrixBase.h:71
bool isVector() const
Definition SparseMatrixBase.h:192
Scalar value_type
Definition SparseMatrixBase.h:37
Index outerSize() const
Definition SparseMatrixBase.h:195
const SparseView< Derived > pruned(const Scalar &reference=Scalar(0), const RealScalar &epsilon=NumTraits< Scalar >::dummy_precision()) const
Definition SparseView.h:219
Index cols() const
Definition SparseMatrixBase.h:184
SparseSymmetricPermutationProduct< Derived, Upper|Lower > twistedBy(const PermutationMatrix< Dynamic, Dynamic, StorageIndex > &perm) const
Definition SparseMatrixBase.h:325
const internal::eval< Derived >::type eval() const
Definition SparseMatrixBase.h:379
A versatible sparse matrix representation.
Definition SparseUtil.h:47
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
Definition SparseUtil.h:52
Expression of a dense or sparse matrix with zero or too small values removed.
Definition SparseView.h:45
Expression of the transpose of a matrix.
Definition Transpose.h:56
Expression of a triangular part in a matrix.
Definition TriangularMatrix.h:167
const unsigned int DirectAccessBit
Definition Constants.h:159
const unsigned int RowMajorBit
Definition Constants.h:70
Namespace containing all symbols from the Eigen library.
Definition Core:137
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:83
Definition EigenBase.h:33
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:43
Derived & derived()
Definition EigenBase.h:49
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition Meta.h:523