CommonCwiseBinaryOps.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008-2016 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
  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. // This file is a base class plugin containing common coefficient wise functions.
  11. /** \returns an expression of the difference of \c *this and \a other
  12. *
  13. * \note If you want to substract a given scalar from all coefficients, see Cwise::operator-().
  14. *
  15. * \sa class CwiseBinaryOp, operator-=()
  16. */
  17. EIGEN_MAKE_CWISE_BINARY_OP(operator-,difference)
  18. /** \returns an expression of the sum of \c *this and \a other
  19. *
  20. * \note If you want to add a given scalar to all coefficients, see Cwise::operator+().
  21. *
  22. * \sa class CwiseBinaryOp, operator+=()
  23. */
  24. EIGEN_MAKE_CWISE_BINARY_OP(operator+,sum)
  25. /** \returns an expression of a custom coefficient-wise operator \a func of *this and \a other
  26. *
  27. * The template parameter \a CustomBinaryOp is the type of the functor
  28. * of the custom operator (see class CwiseBinaryOp for an example)
  29. *
  30. * Here is an example illustrating the use of custom functors:
  31. * \include class_CwiseBinaryOp.cpp
  32. * Output: \verbinclude class_CwiseBinaryOp.out
  33. *
  34. * \sa class CwiseBinaryOp, operator+(), operator-(), cwiseProduct()
  35. */
  36. template<typename CustomBinaryOp, typename OtherDerived>
  37. EIGEN_DEVICE_FUNC
  38. EIGEN_STRONG_INLINE const CwiseBinaryOp<CustomBinaryOp, const Derived, const OtherDerived>
  39. binaryExpr(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> &other, const CustomBinaryOp& func = CustomBinaryOp()) const
  40. {
  41. return CwiseBinaryOp<CustomBinaryOp, const Derived, const OtherDerived>(derived(), other.derived(), func);
  42. }
  43. #ifndef EIGEN_PARSED_BY_DOXYGEN
  44. EIGEN_MAKE_SCALAR_BINARY_OP(operator*,product)
  45. #else
  46. /** \returns an expression of \c *this scaled by the scalar factor \a scalar
  47. *
  48. * \tparam T is the scalar type of \a scalar. It must be compatible with the scalar type of the given expression.
  49. */
  50. template<typename T>
  51. const CwiseBinaryOp<internal::scalar_product_op<Scalar,T>,Derived,Constant<T> > operator*(const T& scalar) const;
  52. /** \returns an expression of \a expr scaled by the scalar factor \a scalar
  53. *
  54. * \tparam T is the scalar type of \a scalar. It must be compatible with the scalar type of the given expression.
  55. */
  56. template<typename T> friend
  57. const CwiseBinaryOp<internal::scalar_product_op<T,Scalar>,Constant<T>,Derived> operator*(const T& scalar, const StorageBaseType& expr);
  58. #endif
  59. #ifndef EIGEN_PARSED_BY_DOXYGEN
  60. EIGEN_MAKE_SCALAR_BINARY_OP_ONTHERIGHT(operator/,quotient)
  61. #else
  62. /** \returns an expression of \c *this divided by the scalar value \a scalar
  63. *
  64. * \tparam T is the scalar type of \a scalar. It must be compatible with the scalar type of the given expression.
  65. */
  66. template<typename T>
  67. const CwiseBinaryOp<internal::scalar_quotient_op<Scalar,T>,Derived,Constant<T> > operator/(const T& scalar) const;
  68. #endif
  69. /** \returns an expression of the coefficient-wise boolean \b and operator of \c *this and \a other
  70. *
  71. * \warning this operator is for expression of bool only.
  72. *
  73. * Example: \include Cwise_boolean_and.cpp
  74. * Output: \verbinclude Cwise_boolean_and.out
  75. *
  76. * \sa operator||(), select()
  77. */
  78. template<typename OtherDerived>
  79. EIGEN_DEVICE_FUNC
  80. inline const CwiseBinaryOp<internal::scalar_boolean_and_op, const Derived, const OtherDerived>
  81. operator&&(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> &other) const
  82. {
  83. EIGEN_STATIC_ASSERT((internal::is_same<bool,Scalar>::value && internal::is_same<bool,typename OtherDerived::Scalar>::value),
  84. THIS_METHOD_IS_ONLY_FOR_EXPRESSIONS_OF_BOOL);
  85. return CwiseBinaryOp<internal::scalar_boolean_and_op, const Derived, const OtherDerived>(derived(),other.derived());
  86. }
  87. /** \returns an expression of the coefficient-wise boolean \b or operator of \c *this and \a other
  88. *
  89. * \warning this operator is for expression of bool only.
  90. *
  91. * Example: \include Cwise_boolean_or.cpp
  92. * Output: \verbinclude Cwise_boolean_or.out
  93. *
  94. * \sa operator&&(), select()
  95. */
  96. template<typename OtherDerived>
  97. EIGEN_DEVICE_FUNC
  98. inline const CwiseBinaryOp<internal::scalar_boolean_or_op, const Derived, const OtherDerived>
  99. operator||(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> &other) const
  100. {
  101. EIGEN_STATIC_ASSERT((internal::is_same<bool,Scalar>::value && internal::is_same<bool,typename OtherDerived::Scalar>::value),
  102. THIS_METHOD_IS_ONLY_FOR_EXPRESSIONS_OF_BOOL);
  103. return CwiseBinaryOp<internal::scalar_boolean_or_op, const Derived, const OtherDerived>(derived(),other.derived());
  104. }