Eigen  3.2.5
DenseBase.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2007-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
5 // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_DENSEBASE_H
12 #define EIGEN_DENSEBASE_H
13 
14 namespace Eigen {
15 
16 namespace internal {
17 
18 // The index type defined by EIGEN_DEFAULT_DENSE_INDEX_TYPE must be a signed type.
19 // This dummy function simply aims at checking that at compile time.
20 static inline void check_DenseIndex_is_signed() {
21  EIGEN_STATIC_ASSERT(NumTraits<DenseIndex>::IsSigned,THE_INDEX_TYPE_MUST_BE_A_SIGNED_TYPE);
22 }
23 
24 } // end namespace internal
25 
41 template<typename Derived> class DenseBase
42 #ifndef EIGEN_PARSED_BY_DOXYGEN
43  : public internal::special_scalar_op_base<Derived,typename internal::traits<Derived>::Scalar,
44  typename NumTraits<typename internal::traits<Derived>::Scalar>::Real>
45 #else
46  : public DenseCoeffsBase<Derived>
47 #endif // not EIGEN_PARSED_BY_DOXYGEN
48 {
49  public:
50  using internal::special_scalar_op_base<Derived,typename internal::traits<Derived>::Scalar,
51  typename NumTraits<typename internal::traits<Derived>::Scalar>::Real>::operator*;
52 
53  class InnerIterator;
54 
55  typedef typename internal::traits<Derived>::StorageKind StorageKind;
56 
61  typedef typename internal::traits<Derived>::Index Index;
62 
63  typedef typename internal::traits<Derived>::Scalar Scalar;
64  typedef typename internal::packet_traits<Scalar>::type PacketScalar;
65  typedef typename NumTraits<Scalar>::Real RealScalar;
66 
67  typedef DenseCoeffsBase<Derived> Base;
68  using Base::derived;
69  using Base::const_cast_derived;
70  using Base::rows;
71  using Base::cols;
72  using Base::size;
73  using Base::rowIndexByOuterInner;
74  using Base::colIndexByOuterInner;
75  using Base::coeff;
76  using Base::coeffByOuterInner;
77  using Base::packet;
78  using Base::packetByOuterInner;
79  using Base::writePacket;
80  using Base::writePacketByOuterInner;
81  using Base::coeffRef;
82  using Base::coeffRefByOuterInner;
83  using Base::copyCoeff;
84  using Base::copyCoeffByOuterInner;
85  using Base::copyPacket;
86  using Base::copyPacketByOuterInner;
87  using Base::operator();
88  using Base::operator[];
89  using Base::x;
90  using Base::y;
91  using Base::z;
92  using Base::w;
93  using Base::stride;
94  using Base::innerStride;
95  using Base::outerStride;
96  using Base::rowStride;
97  using Base::colStride;
98  typedef typename Base::CoeffReturnType CoeffReturnType;
99 
100  enum {
101 
102  RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
108  ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
115  SizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::RowsAtCompileTime,
116  internal::traits<Derived>::ColsAtCompileTime>::ret),
121  MaxRowsAtCompileTime = internal::traits<Derived>::MaxRowsAtCompileTime,
132  MaxColsAtCompileTime = internal::traits<Derived>::MaxColsAtCompileTime,
143  MaxSizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::MaxRowsAtCompileTime,
144  internal::traits<Derived>::MaxColsAtCompileTime>::ret),
155  IsVectorAtCompileTime = internal::traits<Derived>::MaxRowsAtCompileTime == 1
156  || internal::traits<Derived>::MaxColsAtCompileTime == 1,
162  Flags = internal::traits<Derived>::Flags,
167  IsRowMajor = int(Flags) & RowMajorBit,
169  InnerSizeAtCompileTime = int(IsVectorAtCompileTime) ? int(SizeAtCompileTime)
170  : int(IsRowMajor) ? int(ColsAtCompileTime) : int(RowsAtCompileTime),
171 
172  CoeffReadCost = internal::traits<Derived>::CoeffReadCost,
177  InnerStrideAtCompileTime = internal::inner_stride_at_compile_time<Derived>::ret,
178  OuterStrideAtCompileTime = internal::outer_stride_at_compile_time<Derived>::ret
179  };
180 
181  enum { ThisConstantIsPrivateInPlainObjectBase };
182 
185  inline Index nonZeros() const { return size(); }
196  Index outerSize() const
197  {
198  return IsVectorAtCompileTime ? 1
199  : int(IsRowMajor) ? this->rows() : this->cols();
200  }
201 
207  Index innerSize() const
208  {
209  return IsVectorAtCompileTime ? this->size()
210  : int(IsRowMajor) ? this->cols() : this->rows();
211  }
212 
217  void resize(Index newSize)
218  {
219  EIGEN_ONLY_USED_FOR_DEBUG(newSize);
220  eigen_assert(newSize == this->size()
221  && "DenseBase::resize() does not actually allow to resize.");
222  }
227  void resize(Index nbRows, Index nbCols)
228  {
229  EIGEN_ONLY_USED_FOR_DEBUG(nbRows);
230  EIGEN_ONLY_USED_FOR_DEBUG(nbCols);
231  eigen_assert(nbRows == this->rows() && nbCols == this->cols()
232  && "DenseBase::resize() does not actually allow to resize.");
233  }
234 
235 #ifndef EIGEN_PARSED_BY_DOXYGEN
236 
238  typedef CwiseNullaryOp<internal::scalar_constant_op<Scalar>,Derived> ConstantReturnType;
240  typedef CwiseNullaryOp<internal::linspaced_op<Scalar,false>,Derived> SequentialLinSpacedReturnType;
242  typedef CwiseNullaryOp<internal::linspaced_op<Scalar,true>,Derived> RandomAccessLinSpacedReturnType;
244  typedef Matrix<typename NumTraits<typename internal::traits<Derived>::Scalar>::Real, internal::traits<Derived>::ColsAtCompileTime, 1> EigenvaluesReturnType;
245 
246 #endif // not EIGEN_PARSED_BY_DOXYGEN
247 
249  template<typename OtherDerived>
250  Derived& operator=(const DenseBase<OtherDerived>& other);
251 
255  Derived& operator=(const DenseBase& other);
256 
257  template<typename OtherDerived>
258  Derived& operator=(const EigenBase<OtherDerived> &other);
259 
260  template<typename OtherDerived>
261  Derived& operator+=(const EigenBase<OtherDerived> &other);
262 
263  template<typename OtherDerived>
264  Derived& operator-=(const EigenBase<OtherDerived> &other);
265 
266  template<typename OtherDerived>
267  Derived& operator=(const ReturnByValue<OtherDerived>& func);
268 
270  template<typename OtherDerived>
271  Derived& lazyAssign(const DenseBase<OtherDerived>& other);
272 
274  template<typename OtherDerived>
275  Derived& lazyAssign(const ReturnByValue<OtherDerived>& other);
276 
277  CommaInitializer<Derived> operator<< (const Scalar& s);
278 
279  template<unsigned int Added,unsigned int Removed>
280  const Flagged<Derived, Added, Removed> flagged() const;
281 
282  template<typename OtherDerived>
283  CommaInitializer<Derived> operator<< (const DenseBase<OtherDerived>& other);
284 
285  Eigen::Transpose<Derived> transpose();
286  typedef typename internal::add_const<Transpose<const Derived> >::type ConstTransposeReturnType;
287  ConstTransposeReturnType transpose() const;
288  void transposeInPlace();
289 #ifndef EIGEN_NO_DEBUG
290  protected:
291  template<typename OtherDerived>
292  void checkTransposeAliasing(const OtherDerived& other) const;
293  public:
294 #endif
295 
296 
297  static const ConstantReturnType
298  Constant(Index rows, Index cols, const Scalar& value);
299  static const ConstantReturnType
300  Constant(Index size, const Scalar& value);
301  static const ConstantReturnType
302  Constant(const Scalar& value);
303 
304  static const SequentialLinSpacedReturnType
305  LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high);
306  static const RandomAccessLinSpacedReturnType
307  LinSpaced(Index size, const Scalar& low, const Scalar& high);
308  static const SequentialLinSpacedReturnType
309  LinSpaced(Sequential_t, const Scalar& low, const Scalar& high);
310  static const RandomAccessLinSpacedReturnType
311  LinSpaced(const Scalar& low, const Scalar& high);
312 
313  template<typename CustomNullaryOp>
315  NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func);
316  template<typename CustomNullaryOp>
318  NullaryExpr(Index size, const CustomNullaryOp& func);
319  template<typename CustomNullaryOp>
321  NullaryExpr(const CustomNullaryOp& func);
322 
323  static const ConstantReturnType Zero(Index rows, Index cols);
324  static const ConstantReturnType Zero(Index size);
325  static const ConstantReturnType Zero();
326  static const ConstantReturnType Ones(Index rows, Index cols);
327  static const ConstantReturnType Ones(Index size);
328  static const ConstantReturnType Ones();
329 
330  void fill(const Scalar& value);
331  Derived& setConstant(const Scalar& value);
332  Derived& setLinSpaced(Index size, const Scalar& low, const Scalar& high);
333  Derived& setLinSpaced(const Scalar& low, const Scalar& high);
334  Derived& setZero();
335  Derived& setOnes();
336  Derived& setRandom();
337 
338  template<typename OtherDerived>
339  bool isApprox(const DenseBase<OtherDerived>& other,
340  const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
341  bool isMuchSmallerThan(const RealScalar& other,
342  const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
343  template<typename OtherDerived>
344  bool isMuchSmallerThan(const DenseBase<OtherDerived>& other,
345  const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
346 
347  bool isApproxToConstant(const Scalar& value, const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
348  bool isConstant(const Scalar& value, const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
349  bool isZero(const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
350  bool isOnes(const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
351 
352  inline bool hasNaN() const;
353  inline bool allFinite() const;
354 
355  inline Derived& operator*=(const Scalar& other);
356  inline Derived& operator/=(const Scalar& other);
357 
358  typedef typename internal::add_const_on_value_type<typename internal::eval<Derived>::type>::type EvalReturnType;
364  EIGEN_STRONG_INLINE EvalReturnType eval() const
365  {
366  // Even though MSVC does not honor strong inlining when the return type
367  // is a dynamic matrix, we desperately need strong inlining for fixed
368  // size types on MSVC.
369  return typename internal::eval<Derived>::type(derived());
370  }
371 
375  template<typename OtherDerived>
376  void swap(const DenseBase<OtherDerived>& other,
377  int = OtherDerived::ThisConstantIsPrivateInPlainObjectBase)
378  {
379  SwapWrapper<Derived>(derived()).lazyAssign(other.derived());
380  }
385  template<typename OtherDerived>
387  {
388  SwapWrapper<Derived>(derived()).lazyAssign(other.derived());
389  }
390 
391 
392  inline const NestByValue<Derived> nestByValue() const;
393  inline const ForceAlignedAccess<Derived> forceAlignedAccess() const;
394  inline ForceAlignedAccess<Derived> forceAlignedAccess();
395  template<bool Enable> inline const typename internal::conditional<Enable,ForceAlignedAccess<Derived>,Derived&>::type forceAlignedAccessIf() const;
396  template<bool Enable> inline typename internal::conditional<Enable,ForceAlignedAccess<Derived>,Derived&>::type forceAlignedAccessIf();
397 
398  Scalar sum() const;
399  Scalar mean() const;
400  Scalar trace() const;
401 
402  Scalar prod() const;
403 
404  typename internal::traits<Derived>::Scalar minCoeff() const;
405  typename internal::traits<Derived>::Scalar maxCoeff() const;
407  template<typename IndexType>
408  typename internal::traits<Derived>::Scalar minCoeff(IndexType* row, IndexType* col) const;
409  template<typename IndexType>
410  typename internal::traits<Derived>::Scalar maxCoeff(IndexType* row, IndexType* col) const;
411  template<typename IndexType>
412  typename internal::traits<Derived>::Scalar minCoeff(IndexType* index) const;
413  template<typename IndexType>
414  typename internal::traits<Derived>::Scalar maxCoeff(IndexType* index) const;
415 
416  template<typename BinaryOp>
417  typename internal::result_of<BinaryOp(typename internal::traits<Derived>::Scalar)>::type
418  redux(const BinaryOp& func) const;
419 
420  template<typename Visitor>
421  void visit(Visitor& func) const;
422 
423  inline const WithFormat<Derived> format(const IOFormat& fmt) const;
424 
426  CoeffReturnType value() const
427  {
428  EIGEN_STATIC_ASSERT_SIZE_1x1(Derived)
429  eigen_assert(this->rows() == 1 && this->cols() == 1);
430  return derived().coeff(0,0);
431  }
432 
433  bool all(void) const;
434  bool any(void) const;
435  Index count() const;
436 
437  typedef VectorwiseOp<Derived, Horizontal> RowwiseReturnType;
438  typedef const VectorwiseOp<const Derived, Horizontal> ConstRowwiseReturnType;
439  typedef VectorwiseOp<Derived, Vertical> ColwiseReturnType;
440  typedef const VectorwiseOp<const Derived, Vertical> ConstColwiseReturnType;
441 
442  ConstRowwiseReturnType rowwise() const;
443  RowwiseReturnType rowwise();
444  ConstColwiseReturnType colwise() const;
445  ColwiseReturnType colwise();
446 
447  static const CwiseNullaryOp<internal::scalar_random_op<Scalar>,Derived> Random(Index rows, Index cols);
448  static const CwiseNullaryOp<internal::scalar_random_op<Scalar>,Derived> Random(Index size);
449  static const CwiseNullaryOp<internal::scalar_random_op<Scalar>,Derived> Random();
450 
451  template<typename ThenDerived,typename ElseDerived>
452  const Select<Derived,ThenDerived,ElseDerived>
453  select(const DenseBase<ThenDerived>& thenMatrix,
454  const DenseBase<ElseDerived>& elseMatrix) const;
455 
456  template<typename ThenDerived>
457  inline const Select<Derived,ThenDerived, typename ThenDerived::ConstantReturnType>
458  select(const DenseBase<ThenDerived>& thenMatrix, const typename ThenDerived::Scalar& elseScalar) const;
459 
460  template<typename ElseDerived>
461  inline const Select<Derived, typename ElseDerived::ConstantReturnType, ElseDerived >
462  select(const typename ElseDerived::Scalar& thenScalar, const DenseBase<ElseDerived>& elseMatrix) const;
463 
464  template<int p> RealScalar lpNorm() const;
465 
466  template<int RowFactor, int ColFactor>
467  inline const Replicate<Derived,RowFactor,ColFactor> replicate() const;
468 
469  typedef Replicate<Derived,Dynamic,Dynamic> ReplicateReturnType;
470  inline const ReplicateReturnType replicate(Index rowFacor,Index colFactor) const;
471 
472  typedef Reverse<Derived, BothDirections> ReverseReturnType;
473  typedef const Reverse<const Derived, BothDirections> ConstReverseReturnType;
474  ReverseReturnType reverse();
475  ConstReverseReturnType reverse() const;
476  void reverseInPlace();
477 
478 #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::DenseBase
479 # include "../plugins/BlockMethods.h"
480 # ifdef EIGEN_DENSEBASE_PLUGIN
481 # include EIGEN_DENSEBASE_PLUGIN
482 # endif
483 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
485 #ifdef EIGEN2_SUPPORT
486 
487  Block<Derived> corner(CornerType type, Index cRows, Index cCols);
488  const Block<Derived> corner(CornerType type, Index cRows, Index cCols) const;
489  template<int CRows, int CCols>
491  template<int CRows, int CCols>
492  const Block<Derived, CRows, CCols> corner(CornerType type) const;
493 
494 #endif // EIGEN2_SUPPORT
495 
496 
497  // disable the use of evalTo for dense objects with a nice compilation error
498  template<typename Dest> inline void evalTo(Dest& ) const
499  {
500  EIGEN_STATIC_ASSERT((internal::is_same<Dest,void>::value),THE_EVAL_EVALTO_FUNCTION_SHOULD_NEVER_BE_CALLED_FOR_DENSE_OBJECTS);
501  }
502 
503  protected:
506  {
507  /* Just checks for self-consistency of the flags.
508  * Only do it when debugging Eigen, as this borders on paranoiac and could slow compilation down
509  */
510 #ifdef EIGEN_INTERNAL_DEBUGGING
511  EIGEN_STATIC_ASSERT((EIGEN_IMPLIES(MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1, int(IsRowMajor))
512  && EIGEN_IMPLIES(MaxColsAtCompileTime==1 && MaxRowsAtCompileTime!=1, int(!IsRowMajor))),
513  INVALID_STORAGE_ORDER_FOR_THIS_VECTOR_EXPRESSION)
514 #endif
515  }
516 
517  private:
518  explicit DenseBase(int);
519  DenseBase(int,int);
520  template<typename OtherDerived> explicit DenseBase(const DenseBase<OtherDerived>&);
521 };
522 
523 } // end namespace Eigen
524 
525 #endif // EIGEN_DENSEBASE_H
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:49
void resize(Index newSize)
Definition: DenseBase.h:217
Enforce aligned packet loads and stores regardless of what is requested.
Definition: ForceAlignedAccess.h:34
Expression of the transpose of a matrix.
Definition: Transpose.h:57
CornerType
Definition: Constants.h:201
void resize(Index nbRows, Index nbCols)
Definition: DenseBase.h:227
Definition: LDLT.h:16
void swap(const DenseBase< OtherDerived > &other, int=OtherDerived::ThisConstantIsPrivateInPlainObjectBase)
Definition: DenseBase.h:376
DenseBase()
Definition: DenseBase.h:505
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:88
Pseudo expression providing partial reduction operations.
Definition: ForwardDeclarations.h:212
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:41
Definition: Constants.h:212
Helper class used by the comma initializer operator.
Definition: CommaInitializer.h:28
Definition: EigenBase.h:26
void swap(PlainObjectBase< OtherDerived > &other)
Definition: DenseBase.h:386
Dense storage base class for matrices and arrays.
Definition: PlainObjectBase.h:85
Expression of the multiple replication of a matrix or vector.
Definition: Replicate.h:62
Index innerSize() const
Definition: DenseBase.h:207
Expression which must be nested by value.
Definition: NestByValue.h:35
Definition: Constants.h:209
internal::traits< Derived >::Index Index
The type of indices.
Definition: DenseBase.h:61
Definition: Eigen_Colamd.h:54
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:103
Index outerSize() const
Definition: DenseBase.h:196
const unsigned int RowMajorBit
Definition: Constants.h:53
Definition: Constants.h:215
CoeffReturnType value() const
Definition: DenseBase.h:426
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
Pseudo expression providing matrix output with given format.
Definition: IO.h:90
Expression of the reverse of a vector or matrix.
Definition: Reverse.h:70
Stores a set of parameters controlling the way matrices are printed.
Definition: IO.h:50
EvalReturnType eval() const
Definition: DenseBase.h:364
Expression of a coefficient wise version of the C++ ternary operator ?:
Definition: Select.h:55