MatrixBase.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2006-2009 Benoit Jacob <jacob.benoit.1@gmail.com>
5 // Copyright (C) 2008 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_MATRIXBASE_H
12 #define EIGEN_MATRIXBASE_H
13 
14 namespace Eigen {
15 
48 template<typename Derived> class MatrixBase
49  : public DenseBase<Derived>
50 {
51  public:
52 #ifndef EIGEN_PARSED_BY_DOXYGEN
53  typedef MatrixBase StorageBaseType;
54  typedef typename internal::traits<Derived>::StorageKind StorageKind;
55  typedef typename internal::traits<Derived>::Index Index;
56  typedef typename internal::traits<Derived>::Scalar Scalar;
57  typedef typename internal::packet_traits<Scalar>::type PacketScalar;
58  typedef typename NumTraits<Scalar>::Real RealScalar;
59 
60  typedef DenseBase<Derived> Base;
61  using Base::RowsAtCompileTime;
62  using Base::ColsAtCompileTime;
63  using Base::SizeAtCompileTime;
64  using Base::MaxRowsAtCompileTime;
65  using Base::MaxColsAtCompileTime;
66  using Base::MaxSizeAtCompileTime;
67  using Base::IsVectorAtCompileTime;
68  using Base::Flags;
69  using Base::CoeffReadCost;
70 
71  using Base::derived;
72  using Base::const_cast_derived;
73  using Base::rows;
74  using Base::cols;
75  using Base::size;
76  using Base::coeff;
77  using Base::coeffRef;
78  using Base::lazyAssign;
79  using Base::eval;
80  using Base::operator+=;
81  using Base::operator-=;
82  using Base::operator*=;
83  using Base::operator/=;
84 
85  typedef typename Base::CoeffReturnType CoeffReturnType;
86  typedef typename Base::ConstTransposeReturnType ConstTransposeReturnType;
87  typedef typename Base::RowXpr RowXpr;
88  typedef typename Base::ColXpr ColXpr;
89 #endif // not EIGEN_PARSED_BY_DOXYGEN
90 
91 
92 
93 #ifndef EIGEN_PARSED_BY_DOXYGEN
94 
95  typedef Matrix<Scalar,EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime),
96  EIGEN_SIZE_MAX(RowsAtCompileTime,ColsAtCompileTime)> SquareMatrixType;
97 #endif // not EIGEN_PARSED_BY_DOXYGEN
98 
101  inline Index diagonalSize() const { return (std::min)(rows(),cols()); }
110  internal::traits<Derived>::RowsAtCompileTime,
111  internal::traits<Derived>::ColsAtCompileTime,
112  AutoAlign | (internal::traits<Derived>::Flags&RowMajorBit ? RowMajor : ColMajor),
113  internal::traits<Derived>::MaxRowsAtCompileTime,
114  internal::traits<Derived>::MaxColsAtCompileTime
116 
117 #ifndef EIGEN_PARSED_BY_DOXYGEN
118 
119  typedef CwiseNullaryOp<internal::scalar_constant_op<Scalar>,Derived> ConstantReturnType;
121  typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
124  >::type AdjointReturnType;
126  typedef Matrix<std::complex<RealScalar>, internal::traits<Derived>::ColsAtCompileTime, 1, ColMajor> EigenvaluesReturnType;
128  typedef CwiseNullaryOp<internal::scalar_identity_op<Scalar>,Derived> IdentityReturnType;
131  internal::traits<Derived>::RowsAtCompileTime,
132  internal::traits<Derived>::ColsAtCompileTime> BasisReturnType;
133 #endif // not EIGEN_PARSED_BY_DOXYGEN
134 
135 #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::MatrixBase
136 # include "../plugins/CommonCwiseUnaryOps.h"
137 # include "../plugins/CommonCwiseBinaryOps.h"
138 # include "../plugins/MatrixCwiseUnaryOps.h"
139 # include "../plugins/MatrixCwiseBinaryOps.h"
140 # ifdef EIGEN_MATRIXBASE_PLUGIN
141 # include EIGEN_MATRIXBASE_PLUGIN
142 # endif
143 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
144 
148  Derived& operator=(const MatrixBase& other);
149 
150  // We cannot inherit here via Base::operator= since it is causing
151  // trouble with MSVC.
152 
153  template <typename OtherDerived>
154  Derived& operator=(const DenseBase<OtherDerived>& other);
155 
156  template <typename OtherDerived>
157  Derived& operator=(const EigenBase<OtherDerived>& other);
159  template<typename OtherDerived>
160  Derived& operator=(const ReturnByValue<OtherDerived>& other);
161 
162 #ifndef EIGEN_PARSED_BY_DOXYGEN
163  template<typename ProductDerived, typename Lhs, typename Rhs>
164  Derived& lazyAssign(const ProductBase<ProductDerived, Lhs,Rhs>& other);
165 #endif // not EIGEN_PARSED_BY_DOXYGEN
166 
167  template<typename OtherDerived>
168  Derived& operator+=(const MatrixBase<OtherDerived>& other);
169  template<typename OtherDerived>
170  Derived& operator-=(const MatrixBase<OtherDerived>& other);
171 
172  template<typename OtherDerived>
174  operator*(const MatrixBase<OtherDerived> &other) const;
175 
176  template<typename OtherDerived>
178  lazyProduct(const MatrixBase<OtherDerived> &other) const;
179 
180  template<typename OtherDerived>
181  Derived& operator*=(const EigenBase<OtherDerived>& other);
182 
183  template<typename OtherDerived>
184  void applyOnTheLeft(const EigenBase<OtherDerived>& other);
185 
186  template<typename OtherDerived>
187  void applyOnTheRight(const EigenBase<OtherDerived>& other);
188 
189  template<typename DiagonalDerived>
190  const DiagonalProduct<Derived, DiagonalDerived, OnTheRight>
191  operator*(const DiagonalBase<DiagonalDerived> &diagonal) const;
192 
193  template<typename OtherDerived>
194  typename internal::scalar_product_traits<typename internal::traits<Derived>::Scalar,typename internal::traits<OtherDerived>::Scalar>::ReturnType
195  dot(const MatrixBase<OtherDerived>& other) const;
196 
197  #ifdef EIGEN2_SUPPORT
198  template<typename OtherDerived>
199  Scalar eigen2_dot(const MatrixBase<OtherDerived>& other) const;
200  #endif
201 
202  RealScalar squaredNorm() const;
203  RealScalar norm() const;
204  RealScalar stableNorm() const;
205  RealScalar blueNorm() const;
206  RealScalar hypotNorm() const;
207  const PlainObject normalized() const;
208  void normalize();
209 
210  const AdjointReturnType adjoint() const;
211  void adjointInPlace();
212 
216  const ConstDiagonalReturnType diagonal() const;
217 
218  template<int Index> struct DiagonalIndexReturnType { typedef Diagonal<Derived,Index> Type; };
219  template<int Index> struct ConstDiagonalIndexReturnType { typedef const Diagonal<const Derived,Index> Type; };
220 
221  template<int Index> typename DiagonalIndexReturnType<Index>::Type diagonal();
222  template<int Index> typename ConstDiagonalIndexReturnType<Index>::Type diagonal() const;
223 
224  // Note: The "MatrixBase::" prefixes are added to help MSVC9 to match these declarations with the later implementations.
225  // On the other hand they confuse MSVC8...
226  #if (defined _MSC_VER) && (_MSC_VER >= 1500) // 2008 or later
227  typename MatrixBase::template DiagonalIndexReturnType<Dynamic>::Type diagonal(Index index);
228  typename MatrixBase::template ConstDiagonalIndexReturnType<Dynamic>::Type diagonal(Index index) const;
229  #else
230  typename DiagonalIndexReturnType<Dynamic>::Type diagonal(Index index);
231  typename ConstDiagonalIndexReturnType<Dynamic>::Type diagonal(Index index) const;
232  #endif
233 
234  #ifdef EIGEN2_SUPPORT
235  template<unsigned int Mode> typename internal::eigen2_part_return_type<Derived, Mode>::type part();
236  template<unsigned int Mode> const typename internal::eigen2_part_return_type<Derived, Mode>::type part() const;
237 
238  // huuuge hack. make Eigen2's matrix.part<Diagonal>() work in eigen3. Problem: Diagonal is now a class template instead
239  // of an integer constant. Solution: overload the part() method template wrt template parameters list.
240  template<template<typename T, int N> class U>
241  const DiagonalWrapper<ConstDiagonalReturnType> part() const
242  { return diagonal().asDiagonal(); }
243  #endif // EIGEN2_SUPPORT
244 
245  template<unsigned int Mode> struct TriangularViewReturnType { typedef TriangularView<Derived, Mode> Type; };
246  template<unsigned int Mode> struct ConstTriangularViewReturnType { typedef const TriangularView<const Derived, Mode> Type; };
247 
248  template<unsigned int Mode> typename TriangularViewReturnType<Mode>::Type triangularView();
249  template<unsigned int Mode> typename ConstTriangularViewReturnType<Mode>::Type triangularView() const;
250 
251  template<unsigned int UpLo> struct SelfAdjointViewReturnType { typedef SelfAdjointView<Derived, UpLo> Type; };
252  template<unsigned int UpLo> struct ConstSelfAdjointViewReturnType { typedef const SelfAdjointView<const Derived, UpLo> Type; };
253 
254  template<unsigned int UpLo> typename SelfAdjointViewReturnType<UpLo>::Type selfadjointView();
255  template<unsigned int UpLo> typename ConstSelfAdjointViewReturnType<UpLo>::Type selfadjointView() const;
256 
257  const SparseView<Derived> sparseView(const Scalar& m_reference = Scalar(0),
258  typename NumTraits<Scalar>::Real m_epsilon = NumTraits<Scalar>::dummy_precision()) const;
259  static const IdentityReturnType Identity();
260  static const IdentityReturnType Identity(Index rows, Index cols);
261  static const BasisReturnType Unit(Index size, Index i);
262  static const BasisReturnType Unit(Index i);
263  static const BasisReturnType UnitX();
264  static const BasisReturnType UnitY();
265  static const BasisReturnType UnitZ();
266  static const BasisReturnType UnitW();
267 
268  const DiagonalWrapper<const Derived> asDiagonal() const;
269  const PermutationWrapper<const Derived> asPermutation() const;
270 
271  Derived& setIdentity();
272  Derived& setIdentity(Index rows, Index cols);
273 
274  bool isIdentity(RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
275  bool isDiagonal(RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
276 
277  bool isUpperTriangular(RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
278  bool isLowerTriangular(RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
279 
280  template<typename OtherDerived>
281  bool isOrthogonal(const MatrixBase<OtherDerived>& other,
282  RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
283  bool isUnitary(RealScalar prec = NumTraits<Scalar>::dummy_precision()) const;
284 
289  template<typename OtherDerived>
290  inline bool operator==(const MatrixBase<OtherDerived>& other) const
291  { return cwiseEqual(other).all(); }
292 
297  template<typename OtherDerived>
298  inline bool operator!=(const MatrixBase<OtherDerived>& other) const
299  { return cwiseNotEqual(other).any(); }
300 
302 
303  inline const ForceAlignedAccess<Derived> forceAlignedAccess() const;
305  template<bool Enable> inline typename internal::add_const_on_value_type<typename internal::conditional<Enable,ForceAlignedAccess<Derived>,Derived&>::type>::type forceAlignedAccessIf() const;
306  template<bool Enable> inline typename internal::conditional<Enable,ForceAlignedAccess<Derived>,Derived&>::type forceAlignedAccessIf();
307 
308  Scalar trace() const;
309 
311 
312  template<int p> RealScalar lpNorm() const;
313 
314  MatrixBase<Derived>& matrix() { return *this; }
315  const MatrixBase<Derived>& matrix() const { return *this; }
316 
319  ArrayWrapper<Derived> array() { return derived(); }
320  const ArrayWrapper<const Derived> array() const { return derived(); }
321 
323 
324  const FullPivLU<PlainObject> fullPivLu() const;
325  const PartialPivLU<PlainObject> partialPivLu() const;
326 
327  #if EIGEN2_SUPPORT_STAGE < STAGE20_RESOLVE_API_CONFLICTS
328  const LU<PlainObject> lu() const;
329  #endif
330 
331  #ifdef EIGEN2_SUPPORT
332  const LU<PlainObject> eigen2_lu() const;
333  #endif
334 
335  #if EIGEN2_SUPPORT_STAGE > STAGE20_RESOLVE_API_CONFLICTS
336  const PartialPivLU<PlainObject> lu() const;
337  #endif
338 
339  #ifdef EIGEN2_SUPPORT
340  template<typename ResultType>
341  void computeInverse(MatrixBase<ResultType> *result) const {
342  *result = this->inverse();
343  }
344  #endif
345 
346  const internal::inverse_impl<Derived> inverse() const;
347  template<typename ResultType>
349  ResultType& inverse,
350  typename ResultType::Scalar& determinant,
351  bool& invertible,
352  const RealScalar& absDeterminantThreshold = NumTraits<Scalar>::dummy_precision()
353  ) const;
354  template<typename ResultType>
356  ResultType& inverse,
357  bool& invertible,
358  const RealScalar& absDeterminantThreshold = NumTraits<Scalar>::dummy_precision()
359  ) const;
360  Scalar determinant() const;
361 
363 
364  const LLT<PlainObject> llt() const;
365  const LDLT<PlainObject> ldlt() const;
366 
368 
369  const HouseholderQR<PlainObject> householderQr() const;
370  const ColPivHouseholderQR<PlainObject> colPivHouseholderQr() const;
371  const FullPivHouseholderQR<PlainObject> fullPivHouseholderQr() const;
372 
373  #ifdef EIGEN2_SUPPORT
374  const QR<PlainObject> qr() const;
375  #endif
376 
377  EigenvaluesReturnType eigenvalues() const;
378  RealScalar operatorNorm() const;
379 
381 
382  JacobiSVD<PlainObject> jacobiSvd(unsigned int computationOptions = 0) const;
383 
384  #ifdef EIGEN2_SUPPORT
385  SVD<PlainObject> svd() const;
386  #endif
387 
389 
390  #ifndef EIGEN_PARSED_BY_DOXYGEN
391 
392  template<typename OtherDerived> struct cross_product_return_type {
393  typedef typename internal::scalar_product_traits<typename internal::traits<Derived>::Scalar,typename internal::traits<OtherDerived>::Scalar>::ReturnType Scalar;
394  typedef Matrix<Scalar,MatrixBase::RowsAtCompileTime,MatrixBase::ColsAtCompileTime> type;
395  };
396  #endif // EIGEN_PARSED_BY_DOXYGEN
397  template<typename OtherDerived>
398  typename cross_product_return_type<OtherDerived>::type
399  cross(const MatrixBase<OtherDerived>& other) const;
400  template<typename OtherDerived>
401  PlainObject cross3(const MatrixBase<OtherDerived>& other) const;
402  PlainObject unitOrthogonal(void) const;
403  Matrix<Scalar,3,1> eulerAngles(Index a0, Index a1, Index a2) const;
404 
405  #if EIGEN2_SUPPORT_STAGE > STAGE20_RESOLVE_API_CONFLICTS
406  ScalarMultipleReturnType operator*(const UniformScaling<Scalar>& s) const;
407  // put this as separate enum value to work around possible GCC 4.3 bug (?)
408  enum { HomogeneousReturnTypeDirection = ColsAtCompileTime==1?Vertical:Horizontal };
409  typedef Homogeneous<Derived, HomogeneousReturnTypeDirection> HomogeneousReturnType;
410  HomogeneousReturnType homogeneous() const;
411  #endif
412 
413  enum {
414  SizeMinusOne = SizeAtCompileTime==Dynamic ? Dynamic : SizeAtCompileTime-1
415  };
416  typedef Block<const Derived,
417  internal::traits<Derived>::ColsAtCompileTime==1 ? SizeMinusOne : 1,
418  internal::traits<Derived>::ColsAtCompileTime==1 ? 1 : SizeMinusOne> ConstStartMinusOne;
419  typedef CwiseUnaryOp<internal::scalar_quotient1_op<typename internal::traits<Derived>::Scalar>,
420  const ConstStartMinusOne > HNormalizedReturnType;
421 
422  const HNormalizedReturnType hnormalized() const;
423 
425 
426  void makeHouseholderInPlace(Scalar& tau, RealScalar& beta);
427  template<typename EssentialPart>
428  void makeHouseholder(EssentialPart& essential,
429  Scalar& tau, RealScalar& beta) const;
430  template<typename EssentialPart>
431  void applyHouseholderOnTheLeft(const EssentialPart& essential,
432  const Scalar& tau,
433  Scalar* workspace);
434  template<typename EssentialPart>
435  void applyHouseholderOnTheRight(const EssentialPart& essential,
436  const Scalar& tau,
437  Scalar* workspace);
438 
440 
441  template<typename OtherScalar>
442  void applyOnTheLeft(Index p, Index q, const JacobiRotation<OtherScalar>& j);
443  template<typename OtherScalar>
444  void applyOnTheRight(Index p, Index q, const JacobiRotation<OtherScalar>& j);
445 
447 
448  typedef typename internal::stem_function<Scalar>::type StemFunction;
449  const MatrixExponentialReturnValue<Derived> exp() const;
450  const MatrixFunctionReturnValue<Derived> matrixFunction(StemFunction f) const;
451  const MatrixFunctionReturnValue<Derived> cosh() const;
452  const MatrixFunctionReturnValue<Derived> sinh() const;
453  const MatrixFunctionReturnValue<Derived> cos() const;
454  const MatrixFunctionReturnValue<Derived> sin() const;
455  const MatrixSquareRootReturnValue<Derived> sqrt() const;
456  const MatrixLogarithmReturnValue<Derived> log() const;
457 
458 #ifdef EIGEN2_SUPPORT
459  template<typename ProductDerived, typename Lhs, typename Rhs>
460  Derived& operator+=(const Flagged<ProductBase<ProductDerived, Lhs,Rhs>, 0,
461  EvalBeforeAssigningBit>& other);
462 
463  template<typename ProductDerived, typename Lhs, typename Rhs>
464  Derived& operator-=(const Flagged<ProductBase<ProductDerived, Lhs,Rhs>, 0,
465  EvalBeforeAssigningBit>& other);
466 
469  template<typename OtherDerived>
470  Derived& lazyAssign(const Flagged<OtherDerived, 0, EvalBeforeAssigningBit>& other)
471  { return lazyAssign(other._expression()); }
472 
473  template<unsigned int Added>
474  const Flagged<Derived, Added, 0> marked() const;
475  const Flagged<Derived, 0, EvalBeforeAssigningBit> lazy() const;
476 
477  inline const Cwise<Derived> cwise() const;
478  inline Cwise<Derived> cwise();
479 
480  VectorBlock<Derived> start(Index size);
481  const VectorBlock<const Derived> start(Index size) const;
482  VectorBlock<Derived> end(Index size);
483  const VectorBlock<const Derived> end(Index size) const;
484  template<int Size> VectorBlock<Derived,Size> start();
485  template<int Size> const VectorBlock<const Derived,Size> start() const;
486  template<int Size> VectorBlock<Derived,Size> end();
487  template<int Size> const VectorBlock<const Derived,Size> end() const;
488 
489  Minor<Derived> minor(Index row, Index col);
490  const Minor<Derived> minor(Index row, Index col) const;
491 #endif
492 
493  protected:
494  MatrixBase() : Base() {}
495 
496  private:
497  explicit MatrixBase(int);
498  MatrixBase(int,int);
499  template<typename OtherDerived> explicit MatrixBase(const MatrixBase<OtherDerived>&);
500  protected:
501  // mixing arrays and matrices is not legal
502  template<typename OtherDerived> Derived& operator+=(const ArrayBase<OtherDerived>& )
503  {EIGEN_STATIC_ASSERT(std::ptrdiff_t(sizeof(typename OtherDerived::Scalar))==-1,YOU_CANNOT_MIX_ARRAYS_AND_MATRICES); return *this;}
504  // mixing arrays and matrices is not legal
505  template<typename OtherDerived> Derived& operator-=(const ArrayBase<OtherDerived>& )
506  {EIGEN_STATIC_ASSERT(std::ptrdiff_t(sizeof(typename OtherDerived::Scalar))==-1,YOU_CANNOT_MIX_ARRAYS_AND_MATRICES); return *this;}
507 };
508 
509 } // end namespace Eigen
510 
511 #endif // EIGEN_MATRIXBASE_H