DenseBase.h
Go to the documentation of this file.
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 // Eigen is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 3 of the License, or (at your option) any later version.
11 //
12 // Alternatively, you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of
15 // the License, or (at your option) any later version.
16 //
17 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License and a copy of the GNU General Public License along with
24 // Eigen. If not, see <http://www.gnu.org/licenses/>.
25 
26 #ifndef EIGEN_DENSEBASE_H
27 #define EIGEN_DENSEBASE_H
28 
29 namespace Eigen {
30 
46 template<typename Derived> class DenseBase
47 #ifndef EIGEN_PARSED_BY_DOXYGEN
48  : public internal::special_scalar_op_base<Derived,typename internal::traits<Derived>::Scalar,
49  typename NumTraits<typename internal::traits<Derived>::Scalar>::Real>
50 #else
51  : public DenseCoeffsBase<Derived>
52 #endif // not EIGEN_PARSED_BY_DOXYGEN
53 {
54  public:
55  using internal::special_scalar_op_base<Derived,typename internal::traits<Derived>::Scalar,
56  typename NumTraits<typename internal::traits<Derived>::Scalar>::Real>::operator*;
57 
58  class InnerIterator;
59 
60  typedef typename internal::traits<Derived>::StorageKind StorageKind;
61 
66  typedef typename internal::traits<Derived>::Index Index;
67 
68  typedef typename internal::traits<Derived>::Scalar Scalar;
69  typedef typename internal::packet_traits<Scalar>::type PacketScalar;
71 
73  using Base::derived;
74  using Base::const_cast_derived;
75  using Base::rows;
76  using Base::cols;
77  using Base::size;
78  using Base::rowIndexByOuterInner;
79  using Base::colIndexByOuterInner;
80  using Base::coeff;
81  using Base::coeffByOuterInner;
82  using Base::packet;
83  using Base::packetByOuterInner;
84  using Base::writePacket;
85  using Base::writePacketByOuterInner;
86  using Base::coeffRef;
87  using Base::coeffRefByOuterInner;
88  using Base::copyCoeff;
89  using Base::copyCoeffByOuterInner;
90  using Base::copyPacket;
91  using Base::copyPacketByOuterInner;
92  using Base::operator();
93  using Base::operator[];
94  using Base::x;
95  using Base::y;
96  using Base::z;
97  using Base::w;
98  using Base::stride;
99  using Base::innerStride;
100  using Base::outerStride;
101  using Base::rowStride;
102  using Base::colStride;
103  typedef typename Base::CoeffReturnType CoeffReturnType;
104 
105  enum {
106 
107  RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
113  ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
120  SizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::RowsAtCompileTime,
121  internal::traits<Derived>::ColsAtCompileTime>::ret),
126  MaxRowsAtCompileTime = internal::traits<Derived>::MaxRowsAtCompileTime,
137  MaxColsAtCompileTime = internal::traits<Derived>::MaxColsAtCompileTime,
148  MaxSizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::MaxRowsAtCompileTime,
149  internal::traits<Derived>::MaxColsAtCompileTime>::ret),
160  IsVectorAtCompileTime = internal::traits<Derived>::MaxRowsAtCompileTime == 1
161  || internal::traits<Derived>::MaxColsAtCompileTime == 1,
167  Flags = internal::traits<Derived>::Flags,
176 
177  CoeffReadCost = internal::traits<Derived>::CoeffReadCost,
182  InnerStrideAtCompileTime = internal::inner_stride_at_compile_time<Derived>::ret,
183  OuterStrideAtCompileTime = internal::outer_stride_at_compile_time<Derived>::ret
184  };
187 
190  inline Index nonZeros() const { return size(); }
201  Index outerSize() const
202  {
203  return IsVectorAtCompileTime ? 1
204  : int(IsRowMajor) ? this->rows() : this->cols();
205  }
212  Index innerSize() const
213  {
214  return IsVectorAtCompileTime ? this->size()
215  : int(IsRowMajor) ? this->cols() : this->rows();
216  }
217 
222  void resize(Index size)
223  {
225  eigen_assert(size == this->size()
226  && "DenseBase::resize() does not actually allow to resize.");
227  }
232  void resize(Index rows, Index cols)
233  {
236  eigen_assert(rows == this->rows() && cols == this->cols()
237  && "DenseBase::resize() does not actually allow to resize.");
238  }
239 
240 #ifndef EIGEN_PARSED_BY_DOXYGEN
241 
243  typedef CwiseNullaryOp<internal::scalar_constant_op<Scalar>,Derived> ConstantReturnType;
245  typedef CwiseNullaryOp<internal::linspaced_op<Scalar,false>,Derived> SequentialLinSpacedReturnType;
247  typedef CwiseNullaryOp<internal::linspaced_op<Scalar,true>,Derived> RandomAccessLinSpacedReturnType;
249  typedef Matrix<typename NumTraits<typename internal::traits<Derived>::Scalar>::Real, internal::traits<Derived>::ColsAtCompileTime, 1> EigenvaluesReturnType;
250 
251 #endif // not EIGEN_PARSED_BY_DOXYGEN
252 
254  template<typename OtherDerived>
255  Derived& operator=(const DenseBase<OtherDerived>& other);
256 
260  Derived& operator=(const DenseBase& other);
261 
262  template<typename OtherDerived>
263  Derived& operator=(const EigenBase<OtherDerived> &other);
264 
265  template<typename OtherDerived>
266  Derived& operator+=(const EigenBase<OtherDerived> &other);
267 
268  template<typename OtherDerived>
269  Derived& operator-=(const EigenBase<OtherDerived> &other);
270 
271  template<typename OtherDerived>
272  Derived& operator=(const ReturnByValue<OtherDerived>& func);
273 
274 #ifndef EIGEN_PARSED_BY_DOXYGEN
275 
276  template<typename OtherDerived>
277  Derived& lazyAssign(const DenseBase<OtherDerived>& other);
278 #endif // not EIGEN_PARSED_BY_DOXYGEN
279 
281 
282  template<unsigned int Added,unsigned int Removed>
283  const Flagged<Derived, Added, Removed> flagged() const;
284 
285  template<typename OtherDerived>
286  CommaInitializer<Derived> operator<< (const DenseBase<OtherDerived>& other);
287 
290  ConstTransposeReturnType transpose() const;
291  void transposeInPlace();
292 #ifndef EIGEN_NO_DEBUG
293  protected:
294  template<typename OtherDerived>
295  void checkTransposeAliasing(const OtherDerived& other) const;
296  public:
297 #endif
298 
301  template<int Size> struct FixedSegmentReturnType { typedef VectorBlock<Derived, Size> Type; };
302  template<int Size> struct ConstFixedSegmentReturnType { typedef const VectorBlock<const Derived, Size> Type; };
303 
304  // Note: The "DenseBase::" prefixes are added to help MSVC9 to match these declarations with the later implementations.
305  SegmentReturnType segment(Index start, Index size);
306  typename DenseBase::ConstSegmentReturnType segment(Index start, Index size) const;
307 
308  SegmentReturnType head(Index size);
309  typename DenseBase::ConstSegmentReturnType head(Index size) const;
310 
311  SegmentReturnType tail(Index size);
312  typename DenseBase::ConstSegmentReturnType tail(Index size) const;
313 
314  template<int Size> typename FixedSegmentReturnType<Size>::Type head();
315  template<int Size> typename ConstFixedSegmentReturnType<Size>::Type head() const;
316 
317  template<int Size> typename FixedSegmentReturnType<Size>::Type tail();
318  template<int Size> typename ConstFixedSegmentReturnType<Size>::Type tail() const;
319 
320  template<int Size> typename FixedSegmentReturnType<Size>::Type segment(Index start);
321  template<int Size> typename ConstFixedSegmentReturnType<Size>::Type segment(Index start) const;
322 
323  static const ConstantReturnType
324  Constant(Index rows, Index cols, const Scalar& value);
325  static const ConstantReturnType
326  Constant(Index size, const Scalar& value);
327  static const ConstantReturnType
328  Constant(const Scalar& value);
329 
330  static const SequentialLinSpacedReturnType
331  LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high);
332  static const RandomAccessLinSpacedReturnType
333  LinSpaced(Index size, const Scalar& low, const Scalar& high);
334  static const SequentialLinSpacedReturnType
335  LinSpaced(Sequential_t, const Scalar& low, const Scalar& high);
336  static const RandomAccessLinSpacedReturnType
337  LinSpaced(const Scalar& low, const Scalar& high);
338 
339  template<typename CustomNullaryOp>
341  NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func);
342  template<typename CustomNullaryOp>
344  NullaryExpr(Index size, const CustomNullaryOp& func);
345  template<typename CustomNullaryOp>
347  NullaryExpr(const CustomNullaryOp& func);
348 
349  static const ConstantReturnType Zero(Index rows, Index cols);
350  static const ConstantReturnType Zero(Index size);
351  static const ConstantReturnType Zero();
352  static const ConstantReturnType Ones(Index rows, Index cols);
353  static const ConstantReturnType Ones(Index size);
354  static const ConstantReturnType Ones();
355 
356  void fill(const Scalar& value);
357  Derived& setConstant(const Scalar& value);
358  Derived& setLinSpaced(Index size, const Scalar& low, const Scalar& high);
359  Derived& setLinSpaced(const Scalar& low, const Scalar& high);
360  Derived& setZero();
361  Derived& setOnes();
362  Derived& setRandom();
364  template<typename OtherDerived>
365  bool isApprox(const DenseBase<OtherDerived>& other,
367  bool isMuchSmallerThan(const RealScalar& other,
369  template<typename OtherDerived>
370  bool isMuchSmallerThan(const DenseBase<OtherDerived>& other,
372 
374  bool isConstant(const Scalar& value, RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
377 
378  inline Derived& operator*=(const Scalar& other);
379  inline Derived& operator/=(const Scalar& other);
380 
381  typedef typename internal::add_const_on_value_type<typename internal::eval<Derived>::type>::type EvalReturnType;
387  EIGEN_STRONG_INLINE EvalReturnType eval() const
388  {
389  // Even though MSVC does not honor strong inlining when the return type
390  // is a dynamic matrix, we desperately need strong inlining for fixed
391  // size types on MSVC.
392  return typename internal::eval<Derived>::type(derived());
393  }
394 
398  template<typename OtherDerived>
399  void swap(const DenseBase<OtherDerived>& other,
400  int = OtherDerived::ThisConstantIsPrivateInPlainObjectBase)
401  {
402  SwapWrapper<Derived>(derived()).lazyAssign(other.derived());
403  }
404 
408  template<typename OtherDerived>
410  {
411  SwapWrapper<Derived>(derived()).lazyAssign(other.derived());
412  }
413 
415  inline const NestByValue<Derived> nestByValue() const;
416  inline const ForceAlignedAccess<Derived> forceAlignedAccess() const;
418  template<bool Enable> inline const typename internal::conditional<Enable,ForceAlignedAccess<Derived>,Derived&>::type forceAlignedAccessIf() const;
419  template<bool Enable> inline typename internal::conditional<Enable,ForceAlignedAccess<Derived>,Derived&>::type forceAlignedAccessIf();
420 
421  Scalar sum() const;
422  Scalar mean() const;
423  Scalar trace() const;
424 
425  Scalar prod() const;
426 
427  typename internal::traits<Derived>::Scalar minCoeff() const;
428  typename internal::traits<Derived>::Scalar maxCoeff() const;
430  template<typename IndexType>
431  typename internal::traits<Derived>::Scalar minCoeff(IndexType* row, IndexType* col) const;
432  template<typename IndexType>
433  typename internal::traits<Derived>::Scalar maxCoeff(IndexType* row, IndexType* col) const;
434  template<typename IndexType>
435  typename internal::traits<Derived>::Scalar minCoeff(IndexType* index) const;
436  template<typename IndexType>
437  typename internal::traits<Derived>::Scalar maxCoeff(IndexType* index) const;
438 
439  template<typename BinaryOp>
440  typename internal::result_of<BinaryOp(typename internal::traits<Derived>::Scalar)>::type
441  redux(const BinaryOp& func) const;
442 
443  template<typename Visitor>
444  void visit(Visitor& func) const;
445 
446  inline const WithFormat<Derived> format(const IOFormat& fmt) const;
447 
450  {
452  eigen_assert(this->rows() == 1 && this->cols() == 1);
453  return derived().coeff(0,0);
454  }
455 
457 
458  bool all(void) const;
459  bool any(void) const;
460  Index count() const;
461 
463  typedef const VectorwiseOp<const Derived, Horizontal> ConstRowwiseReturnType;
465  typedef const VectorwiseOp<const Derived, Vertical> ConstColwiseReturnType;
466 
467  ConstRowwiseReturnType rowwise() const;
468  RowwiseReturnType rowwise();
469  ConstColwiseReturnType colwise() const;
470  ColwiseReturnType colwise();
471 
472  static const CwiseNullaryOp<internal::scalar_random_op<Scalar>,Derived> Random(Index rows, Index cols);
473  static const CwiseNullaryOp<internal::scalar_random_op<Scalar>,Derived> Random(Index size);
474  static const CwiseNullaryOp<internal::scalar_random_op<Scalar>,Derived> Random();
475 
476  template<typename ThenDerived,typename ElseDerived>
477  const Select<Derived,ThenDerived,ElseDerived>
478  select(const DenseBase<ThenDerived>& thenMatrix,
479  const DenseBase<ElseDerived>& elseMatrix) const;
481  template<typename ThenDerived>
482  inline const Select<Derived,ThenDerived, typename ThenDerived::ConstantReturnType>
483  select(const DenseBase<ThenDerived>& thenMatrix, typename ThenDerived::Scalar elseScalar) const;
484 
485  template<typename ElseDerived>
486  inline const Select<Derived, typename ElseDerived::ConstantReturnType, ElseDerived >
487  select(typename ElseDerived::Scalar thenScalar, const DenseBase<ElseDerived>& elseMatrix) const;
488 
489  template<int p> RealScalar lpNorm() const;
490 
491  template<int RowFactor, int ColFactor>
492  const Replicate<Derived,RowFactor,ColFactor> replicate() const;
493  const Replicate<Derived,Dynamic,Dynamic> replicate(Index rowFacor,Index colFactor) const;
494 
496  typedef const Reverse<const Derived, BothDirections> ConstReverseReturnType;
497  ReverseReturnType reverse();
498  ConstReverseReturnType reverse() const;
499  void reverseInPlace();
500 
501 #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::DenseBase
502 # include "../plugins/BlockMethods.h"
503 # ifdef EIGEN_DENSEBASE_PLUGIN
504 # include EIGEN_DENSEBASE_PLUGIN
505 # endif
506 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
507 
508 #ifdef EIGEN2_SUPPORT
509 
510  Block<Derived> corner(CornerType type, Index cRows, Index cCols);
511  const Block<Derived> corner(CornerType type, Index cRows, Index cCols) const;
512  template<int CRows, int CCols>
514  template<int CRows, int CCols>
515  const Block<Derived, CRows, CCols> corner(CornerType type) const;
516 
517 #endif // EIGEN2_SUPPORT
518 
520  // disable the use of evalTo for dense objects with a nice compilation error
521  template<typename Dest> inline void evalTo(Dest& ) const
522  {
523  EIGEN_STATIC_ASSERT((internal::is_same<Dest,void>::value),THE_EVAL_EVALTO_FUNCTION_SHOULD_NEVER_BE_CALLED_FOR_DENSE_OBJECTS);
524  }
525 
526  protected:
529  {
530  /* Just checks for self-consistency of the flags.
531  * Only do it when debugging Eigen, as this borders on paranoiac and could slow compilation down
532  */
533 #ifdef EIGEN_INTERNAL_DEBUGGING
536  INVALID_STORAGE_ORDER_FOR_THIS_VECTOR_EXPRESSION)
537 #endif
538  }
539 
540  private:
541  explicit DenseBase(int);
542  DenseBase(int,int);
543  template<typename OtherDerived> explicit DenseBase(const DenseBase<OtherDerived>&);
544 };
545 
546 } // end namespace Eigen
547 
548 #endif // EIGEN_DENSEBASE_H