EigenBase.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
  5. // Copyright (C) 2009 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. #ifndef EIGEN_EIGENBASE_H
  11. #define EIGEN_EIGENBASE_H
  12. namespace Eigen {
  13. /** \class EigenBase
  14. * \ingroup Core_Module
  15. *
  16. * Common base class for all classes T such that MatrixBase has an operator=(T) and a constructor MatrixBase(T).
  17. *
  18. * In other words, an EigenBase object is an object that can be copied into a MatrixBase.
  19. *
  20. * Besides MatrixBase-derived classes, this also includes special matrix classes such as diagonal matrices, etc.
  21. *
  22. * Notice that this class is trivial, it is only used to disambiguate overloaded functions.
  23. *
  24. * \sa \blank \ref TopicClassHierarchy
  25. */
  26. template<typename Derived> struct EigenBase
  27. {
  28. // typedef typename internal::plain_matrix_type<Derived>::type PlainObject;
  29. /** \brief The interface type of indices
  30. * \details To change this, \c \#define the preprocessor symbol \c EIGEN_DEFAULT_DENSE_INDEX_TYPE.
  31. * \deprecated Since Eigen 3.3, its usage is deprecated. Use Eigen::Index instead.
  32. * \sa StorageIndex, \ref TopicPreprocessorDirectives.
  33. */
  34. typedef Eigen::Index Index;
  35. // FIXME is it needed?
  36. typedef typename internal::traits<Derived>::StorageKind StorageKind;
  37. /** \returns a reference to the derived object */
  38. EIGEN_DEVICE_FUNC
  39. Derived& derived() { return *static_cast<Derived*>(this); }
  40. /** \returns a const reference to the derived object */
  41. EIGEN_DEVICE_FUNC
  42. const Derived& derived() const { return *static_cast<const Derived*>(this); }
  43. EIGEN_DEVICE_FUNC
  44. inline Derived& const_cast_derived() const
  45. { return *static_cast<Derived*>(const_cast<EigenBase*>(this)); }
  46. EIGEN_DEVICE_FUNC
  47. inline const Derived& const_derived() const
  48. { return *static_cast<const Derived*>(this); }
  49. /** \returns the number of rows. \sa cols(), RowsAtCompileTime */
  50. EIGEN_DEVICE_FUNC
  51. inline Index rows() const { return derived().rows(); }
  52. /** \returns the number of columns. \sa rows(), ColsAtCompileTime*/
  53. EIGEN_DEVICE_FUNC
  54. inline Index cols() const { return derived().cols(); }
  55. /** \returns the number of coefficients, which is rows()*cols().
  56. * \sa rows(), cols(), SizeAtCompileTime. */
  57. EIGEN_DEVICE_FUNC
  58. inline Index size() const { return rows() * cols(); }
  59. /** \internal Don't use it, but do the equivalent: \code dst = *this; \endcode */
  60. template<typename Dest>
  61. EIGEN_DEVICE_FUNC
  62. inline void evalTo(Dest& dst) const
  63. { derived().evalTo(dst); }
  64. /** \internal Don't use it, but do the equivalent: \code dst += *this; \endcode */
  65. template<typename Dest>
  66. EIGEN_DEVICE_FUNC
  67. inline void addTo(Dest& dst) const
  68. {
  69. // This is the default implementation,
  70. // derived class can reimplement it in a more optimized way.
  71. typename Dest::PlainObject res(rows(),cols());
  72. evalTo(res);
  73. dst += res;
  74. }
  75. /** \internal Don't use it, but do the equivalent: \code dst -= *this; \endcode */
  76. template<typename Dest>
  77. EIGEN_DEVICE_FUNC
  78. inline void subTo(Dest& dst) const
  79. {
  80. // This is the default implementation,
  81. // derived class can reimplement it in a more optimized way.
  82. typename Dest::PlainObject res(rows(),cols());
  83. evalTo(res);
  84. dst -= res;
  85. }
  86. /** \internal Don't use it, but do the equivalent: \code dst.applyOnTheRight(*this); \endcode */
  87. template<typename Dest>
  88. EIGEN_DEVICE_FUNC inline void applyThisOnTheRight(Dest& dst) const
  89. {
  90. // This is the default implementation,
  91. // derived class can reimplement it in a more optimized way.
  92. dst = dst * this->derived();
  93. }
  94. /** \internal Don't use it, but do the equivalent: \code dst.applyOnTheLeft(*this); \endcode */
  95. template<typename Dest>
  96. EIGEN_DEVICE_FUNC inline void applyThisOnTheLeft(Dest& dst) const
  97. {
  98. // This is the default implementation,
  99. // derived class can reimplement it in a more optimized way.
  100. dst = this->derived() * dst;
  101. }
  102. };
  103. /***************************************************************************
  104. * Implementation of matrix base methods
  105. ***************************************************************************/
  106. /** \brief Copies the generic expression \a other into *this.
  107. *
  108. * \details The expression must provide a (templated) evalTo(Derived& dst) const
  109. * function which does the actual job. In practice, this allows any user to write
  110. * its own special matrix without having to modify MatrixBase
  111. *
  112. * \returns a reference to *this.
  113. */
  114. template<typename Derived>
  115. template<typename OtherDerived>
  116. EIGEN_DEVICE_FUNC
  117. Derived& DenseBase<Derived>::operator=(const EigenBase<OtherDerived> &other)
  118. {
  119. call_assignment(derived(), other.derived());
  120. return derived();
  121. }
  122. template<typename Derived>
  123. template<typename OtherDerived>
  124. EIGEN_DEVICE_FUNC
  125. Derived& DenseBase<Derived>::operator+=(const EigenBase<OtherDerived> &other)
  126. {
  127. call_assignment(derived(), other.derived(), internal::add_assign_op<Scalar,typename OtherDerived::Scalar>());
  128. return derived();
  129. }
  130. template<typename Derived>
  131. template<typename OtherDerived>
  132. EIGEN_DEVICE_FUNC
  133. Derived& DenseBase<Derived>::operator-=(const EigenBase<OtherDerived> &other)
  134. {
  135. call_assignment(derived(), other.derived(), internal::sub_assign_op<Scalar,typename OtherDerived::Scalar>());
  136. return derived();
  137. }
  138. } // end namespace Eigen
  139. #endif // EIGEN_EIGENBASE_H