/src/Geometry_Eigen/Eigen/src/Core/NoAlias.h

http://github.com/Akranar/daguerreo · C Header · 136 lines · 54 code · 12 blank · 70 comment · 0 complexity · 841fccf95f9a8590d2274d5cdaaca492 MD5 · raw file

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // Eigen is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU Lesser General Public
  8. // License as published by the Free Software Foundation; either
  9. // version 3 of the License, or (at your option) any later version.
  10. //
  11. // Alternatively, you can redistribute it and/or
  12. // modify it under the terms of the GNU General Public License as
  13. // published by the Free Software Foundation; either version 2 of
  14. // the License, or (at your option) any later version.
  15. //
  16. // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
  17. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  18. // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU Lesser General Public
  22. // License and a copy of the GNU General Public License along with
  23. // Eigen. If not, see <http://www.gnu.org/licenses/>.
  24. #ifndef EIGEN_NOALIAS_H
  25. #define EIGEN_NOALIAS_H
  26. /** \class NoAlias
  27. * \ingroup Core_Module
  28. *
  29. * \brief Pseudo expression providing an operator = assuming no aliasing
  30. *
  31. * \param ExpressionType the type of the object on which to do the lazy assignment
  32. *
  33. * This class represents an expression with special assignment operators
  34. * assuming no aliasing between the target expression and the source expression.
  35. * More precisely it alloas to bypass the EvalBeforeAssignBit flag of the source expression.
  36. * It is the return type of MatrixBase::noalias()
  37. * and most of the time this is the only way it is used.
  38. *
  39. * \sa MatrixBase::noalias()
  40. */
  41. template<typename ExpressionType, template <typename> class StorageBase>
  42. class NoAlias
  43. {
  44. typedef typename ExpressionType::Scalar Scalar;
  45. public:
  46. NoAlias(ExpressionType& expression) : m_expression(expression) {}
  47. /** Behaves like MatrixBase::lazyAssign(other)
  48. * \sa MatrixBase::lazyAssign() */
  49. template<typename OtherDerived>
  50. EIGEN_STRONG_INLINE ExpressionType& operator=(const StorageBase<OtherDerived>& other)
  51. { return internal::assign_selector<ExpressionType,OtherDerived,false>::run(m_expression,other.derived()); }
  52. /** \sa MatrixBase::operator+= */
  53. template<typename OtherDerived>
  54. EIGEN_STRONG_INLINE ExpressionType& operator+=(const StorageBase<OtherDerived>& other)
  55. {
  56. typedef SelfCwiseBinaryOp<internal::scalar_sum_op<Scalar>, ExpressionType, OtherDerived> SelfAdder;
  57. SelfAdder tmp(m_expression);
  58. typedef typename internal::nested<OtherDerived>::type OtherDerivedNested;
  59. typedef typename internal::remove_all<OtherDerivedNested>::type _OtherDerivedNested;
  60. internal::assign_selector<SelfAdder,_OtherDerivedNested,false>::run(tmp,OtherDerivedNested(other.derived()));
  61. return m_expression;
  62. }
  63. /** \sa MatrixBase::operator-= */
  64. template<typename OtherDerived>
  65. EIGEN_STRONG_INLINE ExpressionType& operator-=(const StorageBase<OtherDerived>& other)
  66. {
  67. typedef SelfCwiseBinaryOp<internal::scalar_difference_op<Scalar>, ExpressionType, OtherDerived> SelfAdder;
  68. SelfAdder tmp(m_expression);
  69. typedef typename internal::nested<OtherDerived>::type OtherDerivedNested;
  70. typedef typename internal::remove_all<OtherDerivedNested>::type _OtherDerivedNested;
  71. internal::assign_selector<SelfAdder,_OtherDerivedNested,false>::run(tmp,OtherDerivedNested(other.derived()));
  72. return m_expression;
  73. }
  74. #ifndef EIGEN_PARSED_BY_DOXYGEN
  75. template<typename ProductDerived, typename Lhs, typename Rhs>
  76. EIGEN_STRONG_INLINE ExpressionType& operator+=(const ProductBase<ProductDerived, Lhs,Rhs>& other)
  77. { other.derived().addTo(m_expression); return m_expression; }
  78. template<typename ProductDerived, typename Lhs, typename Rhs>
  79. EIGEN_STRONG_INLINE ExpressionType& operator-=(const ProductBase<ProductDerived, Lhs,Rhs>& other)
  80. { other.derived().subTo(m_expression); return m_expression; }
  81. template<typename Lhs, typename Rhs, int NestingFlags>
  82. EIGEN_STRONG_INLINE ExpressionType& operator+=(const CoeffBasedProduct<Lhs,Rhs,NestingFlags>& other)
  83. { return m_expression.derived() += CoeffBasedProduct<Lhs,Rhs,NestByRefBit>(other.lhs(), other.rhs()); }
  84. template<typename Lhs, typename Rhs, int NestingFlags>
  85. EIGEN_STRONG_INLINE ExpressionType& operator-=(const CoeffBasedProduct<Lhs,Rhs,NestingFlags>& other)
  86. { return m_expression.derived() -= CoeffBasedProduct<Lhs,Rhs,NestByRefBit>(other.lhs(), other.rhs()); }
  87. #endif
  88. protected:
  89. ExpressionType& m_expression;
  90. };
  91. /** \returns a pseudo expression of \c *this with an operator= assuming
  92. * no aliasing between \c *this and the source expression.
  93. *
  94. * More precisely, noalias() allows to bypass the EvalBeforeAssignBit flag.
  95. * Currently, even though several expressions may alias, only product
  96. * expressions have this flag. Therefore, noalias() is only usefull when
  97. * the source expression contains a matrix product.
  98. *
  99. * Here are some examples where noalias is usefull:
  100. * \code
  101. * D.noalias() = A * B;
  102. * D.noalias() += A.transpose() * B;
  103. * D.noalias() -= 2 * A * B.adjoint();
  104. * \endcode
  105. *
  106. * On the other hand the following example will lead to a \b wrong result:
  107. * \code
  108. * A.noalias() = A * B;
  109. * \endcode
  110. * because the result matrix A is also an operand of the matrix product. Therefore,
  111. * there is no alternative than evaluating A * B in a temporary, that is the default
  112. * behavior when you write:
  113. * \code
  114. * A = A * B;
  115. * \endcode
  116. *
  117. * \sa class NoAlias
  118. */
  119. template<typename Derived>
  120. NoAlias<Derived,MatrixBase> MatrixBase<Derived>::noalias()
  121. {
  122. return derived();
  123. }
  124. #endif // EIGEN_NOALIAS_H