ArrayCwiseBinaryOps.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /** \returns an expression of the coefficient wise product of \c *this and \a other
  2. *
  3. * \sa MatrixBase::cwiseProduct
  4. */
  5. template<typename OtherDerived>
  6. EIGEN_DEVICE_FUNC
  7. EIGEN_STRONG_INLINE const EIGEN_CWISE_BINARY_RETURN_TYPE(Derived,OtherDerived,product)
  8. operator*(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> &other) const
  9. {
  10. return EIGEN_CWISE_BINARY_RETURN_TYPE(Derived,OtherDerived,product)(derived(), other.derived());
  11. }
  12. /** \returns an expression of the coefficient wise quotient of \c *this and \a other
  13. *
  14. * \sa MatrixBase::cwiseQuotient
  15. */
  16. template<typename OtherDerived>
  17. EIGEN_DEVICE_FUNC
  18. EIGEN_STRONG_INLINE const CwiseBinaryOp<internal::scalar_quotient_op<Scalar,typename OtherDerived::Scalar>, const Derived, const OtherDerived>
  19. operator/(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> &other) const
  20. {
  21. return CwiseBinaryOp<internal::scalar_quotient_op<Scalar,typename OtherDerived::Scalar>, const Derived, const OtherDerived>(derived(), other.derived());
  22. }
  23. /** \returns an expression of the coefficient-wise min of \c *this and \a other
  24. *
  25. * Example: \include Cwise_min.cpp
  26. * Output: \verbinclude Cwise_min.out
  27. *
  28. * \sa max()
  29. */
  30. EIGEN_MAKE_CWISE_BINARY_OP(min,min)
  31. /** \returns an expression of the coefficient-wise min of \c *this and scalar \a other
  32. *
  33. * \sa max()
  34. */
  35. EIGEN_DEVICE_FUNC
  36. EIGEN_STRONG_INLINE const CwiseBinaryOp<internal::scalar_min_op<Scalar,Scalar>, const Derived,
  37. const CwiseNullaryOp<internal::scalar_constant_op<Scalar>, PlainObject> >
  38. #ifdef EIGEN_PARSED_BY_DOXYGEN
  39. min
  40. #else
  41. (min)
  42. #endif
  43. (const Scalar &other) const
  44. {
  45. return (min)(Derived::PlainObject::Constant(rows(), cols(), other));
  46. }
  47. /** \returns an expression of the coefficient-wise max of \c *this and \a other
  48. *
  49. * Example: \include Cwise_max.cpp
  50. * Output: \verbinclude Cwise_max.out
  51. *
  52. * \sa min()
  53. */
  54. EIGEN_MAKE_CWISE_BINARY_OP(max,max)
  55. /** \returns an expression of the coefficient-wise max of \c *this and scalar \a other
  56. *
  57. * \sa min()
  58. */
  59. EIGEN_DEVICE_FUNC
  60. EIGEN_STRONG_INLINE const CwiseBinaryOp<internal::scalar_max_op<Scalar,Scalar>, const Derived,
  61. const CwiseNullaryOp<internal::scalar_constant_op<Scalar>, PlainObject> >
  62. #ifdef EIGEN_PARSED_BY_DOXYGEN
  63. max
  64. #else
  65. (max)
  66. #endif
  67. (const Scalar &other) const
  68. {
  69. return (max)(Derived::PlainObject::Constant(rows(), cols(), other));
  70. }
  71. /** \returns an expression of the coefficient-wise power of \c *this to the given array of \a exponents.
  72. *
  73. * This function computes the coefficient-wise power.
  74. *
  75. * Example: \include Cwise_array_power_array.cpp
  76. * Output: \verbinclude Cwise_array_power_array.out
  77. */
  78. EIGEN_MAKE_CWISE_BINARY_OP(pow,pow)
  79. #ifndef EIGEN_PARSED_BY_DOXYGEN
  80. EIGEN_MAKE_SCALAR_BINARY_OP_ONTHERIGHT(pow,pow)
  81. #else
  82. /** \returns an expression of the coefficients of \c *this rasied to the constant power \a exponent
  83. *
  84. * \tparam T is the scalar type of \a exponent. It must be compatible with the scalar type of the given expression.
  85. *
  86. * This function computes the coefficient-wise power. The function MatrixBase::pow() in the
  87. * unsupported module MatrixFunctions computes the matrix power.
  88. *
  89. * Example: \include Cwise_pow.cpp
  90. * Output: \verbinclude Cwise_pow.out
  91. *
  92. * \sa ArrayBase::pow(ArrayBase), square(), cube(), exp(), log()
  93. */
  94. template<typename T>
  95. const CwiseBinaryOp<internal::scalar_pow_op<Scalar,T>,Derived,Constant<T> > pow(const T& exponent) const;
  96. #endif
  97. // TODO code generating macros could be moved to Macros.h and could include generation of documentation
  98. #define EIGEN_MAKE_CWISE_COMP_OP(OP, COMPARATOR) \
  99. template<typename OtherDerived> \
  100. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const CwiseBinaryOp<internal::scalar_cmp_op<Scalar, typename OtherDerived::Scalar, internal::cmp_ ## COMPARATOR>, const Derived, const OtherDerived> \
  101. OP(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> &other) const \
  102. { \
  103. return CwiseBinaryOp<internal::scalar_cmp_op<Scalar, typename OtherDerived::Scalar, internal::cmp_ ## COMPARATOR>, const Derived, const OtherDerived>(derived(), other.derived()); \
  104. }\
  105. typedef CwiseBinaryOp<internal::scalar_cmp_op<Scalar,Scalar, internal::cmp_ ## COMPARATOR>, const Derived, const CwiseNullaryOp<internal::scalar_constant_op<Scalar>, PlainObject> > Cmp ## COMPARATOR ## ReturnType; \
  106. typedef CwiseBinaryOp<internal::scalar_cmp_op<Scalar,Scalar, internal::cmp_ ## COMPARATOR>, const CwiseNullaryOp<internal::scalar_constant_op<Scalar>, PlainObject>, const Derived > RCmp ## COMPARATOR ## ReturnType; \
  107. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Cmp ## COMPARATOR ## ReturnType \
  108. OP(const Scalar& s) const { \
  109. return this->OP(Derived::PlainObject::Constant(rows(), cols(), s)); \
  110. } \
  111. EIGEN_DEVICE_FUNC friend EIGEN_STRONG_INLINE const RCmp ## COMPARATOR ## ReturnType \
  112. OP(const Scalar& s, const EIGEN_CURRENT_STORAGE_BASE_CLASS<Derived>& d) { \
  113. return Derived::PlainObject::Constant(d.rows(), d.cols(), s).OP(d); \
  114. }
  115. #define EIGEN_MAKE_CWISE_COMP_R_OP(OP, R_OP, RCOMPARATOR) \
  116. template<typename OtherDerived> \
  117. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const CwiseBinaryOp<internal::scalar_cmp_op<typename OtherDerived::Scalar, Scalar, internal::cmp_##RCOMPARATOR>, const OtherDerived, const Derived> \
  118. OP(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> &other) const \
  119. { \
  120. return CwiseBinaryOp<internal::scalar_cmp_op<typename OtherDerived::Scalar, Scalar, internal::cmp_##RCOMPARATOR>, const OtherDerived, const Derived>(other.derived(), derived()); \
  121. } \
  122. EIGEN_DEVICE_FUNC \
  123. inline const RCmp ## RCOMPARATOR ## ReturnType \
  124. OP(const Scalar& s) const { \
  125. return Derived::PlainObject::Constant(rows(), cols(), s).R_OP(*this); \
  126. } \
  127. friend inline const Cmp ## RCOMPARATOR ## ReturnType \
  128. OP(const Scalar& s, const Derived& d) { \
  129. return d.R_OP(Derived::PlainObject::Constant(d.rows(), d.cols(), s)); \
  130. }
  131. /** \returns an expression of the coefficient-wise \< operator of *this and \a other
  132. *
  133. * Example: \include Cwise_less.cpp
  134. * Output: \verbinclude Cwise_less.out
  135. *
  136. * \sa all(), any(), operator>(), operator<=()
  137. */
  138. EIGEN_MAKE_CWISE_COMP_OP(operator<, LT)
  139. /** \returns an expression of the coefficient-wise \<= operator of *this and \a other
  140. *
  141. * Example: \include Cwise_less_equal.cpp
  142. * Output: \verbinclude Cwise_less_equal.out
  143. *
  144. * \sa all(), any(), operator>=(), operator<()
  145. */
  146. EIGEN_MAKE_CWISE_COMP_OP(operator<=, LE)
  147. /** \returns an expression of the coefficient-wise \> operator of *this and \a other
  148. *
  149. * Example: \include Cwise_greater.cpp
  150. * Output: \verbinclude Cwise_greater.out
  151. *
  152. * \sa all(), any(), operator>=(), operator<()
  153. */
  154. EIGEN_MAKE_CWISE_COMP_R_OP(operator>, operator<, LT)
  155. /** \returns an expression of the coefficient-wise \>= operator of *this and \a other
  156. *
  157. * Example: \include Cwise_greater_equal.cpp
  158. * Output: \verbinclude Cwise_greater_equal.out
  159. *
  160. * \sa all(), any(), operator>(), operator<=()
  161. */
  162. EIGEN_MAKE_CWISE_COMP_R_OP(operator>=, operator<=, LE)
  163. /** \returns an expression of the coefficient-wise == operator of *this and \a other
  164. *
  165. * \warning this performs an exact comparison, which is generally a bad idea with floating-point types.
  166. * In order to check for equality between two vectors or matrices with floating-point coefficients, it is
  167. * generally a far better idea to use a fuzzy comparison as provided by isApprox() and
  168. * isMuchSmallerThan().
  169. *
  170. * Example: \include Cwise_equal_equal.cpp
  171. * Output: \verbinclude Cwise_equal_equal.out
  172. *
  173. * \sa all(), any(), isApprox(), isMuchSmallerThan()
  174. */
  175. EIGEN_MAKE_CWISE_COMP_OP(operator==, EQ)
  176. /** \returns an expression of the coefficient-wise != operator of *this and \a other
  177. *
  178. * \warning this performs an exact comparison, which is generally a bad idea with floating-point types.
  179. * In order to check for equality between two vectors or matrices with floating-point coefficients, it is
  180. * generally a far better idea to use a fuzzy comparison as provided by isApprox() and
  181. * isMuchSmallerThan().
  182. *
  183. * Example: \include Cwise_not_equal.cpp
  184. * Output: \verbinclude Cwise_not_equal.out
  185. *
  186. * \sa all(), any(), isApprox(), isMuchSmallerThan()
  187. */
  188. EIGEN_MAKE_CWISE_COMP_OP(operator!=, NEQ)
  189. #undef EIGEN_MAKE_CWISE_COMP_OP
  190. #undef EIGEN_MAKE_CWISE_COMP_R_OP
  191. // scalar addition
  192. #ifndef EIGEN_PARSED_BY_DOXYGEN
  193. EIGEN_MAKE_SCALAR_BINARY_OP(operator+,sum)
  194. #else
  195. /** \returns an expression of \c *this with each coeff incremented by the constant \a scalar
  196. *
  197. * \tparam T is the scalar type of \a scalar. It must be compatible with the scalar type of the given expression.
  198. *
  199. * Example: \include Cwise_plus.cpp
  200. * Output: \verbinclude Cwise_plus.out
  201. *
  202. * \sa operator+=(), operator-()
  203. */
  204. template<typename T>
  205. const CwiseBinaryOp<internal::scalar_sum_op<Scalar,T>,Derived,Constant<T> > operator+(const T& scalar) const;
  206. /** \returns an expression of \a expr with each coeff incremented by the constant \a scalar
  207. *
  208. * \tparam T is the scalar type of \a scalar. It must be compatible with the scalar type of the given expression.
  209. */
  210. template<typename T> friend
  211. const CwiseBinaryOp<internal::scalar_sum_op<T,Scalar>,Constant<T>,Derived> operator+(const T& scalar, const StorageBaseType& expr);
  212. #endif
  213. #ifndef EIGEN_PARSED_BY_DOXYGEN
  214. EIGEN_MAKE_SCALAR_BINARY_OP(operator-,difference)
  215. #else
  216. /** \returns an expression of \c *this with each coeff decremented by the constant \a scalar
  217. *
  218. * \tparam T is the scalar type of \a scalar. It must be compatible with the scalar type of the given expression.
  219. *
  220. * Example: \include Cwise_minus.cpp
  221. * Output: \verbinclude Cwise_minus.out
  222. *
  223. * \sa operator+=(), operator-()
  224. */
  225. template<typename T>
  226. const CwiseBinaryOp<internal::scalar_difference_op<Scalar,T>,Derived,Constant<T> > operator-(const T& scalar) const;
  227. /** \returns an expression of the constant matrix of value \a scalar decremented by the coefficients of \a expr
  228. *
  229. * \tparam T is the scalar type of \a scalar. It must be compatible with the scalar type of the given expression.
  230. */
  231. template<typename T> friend
  232. const CwiseBinaryOp<internal::scalar_difference_op<T,Scalar>,Constant<T>,Derived> operator-(const T& scalar, const StorageBaseType& expr);
  233. #endif
  234. #ifndef EIGEN_PARSED_BY_DOXYGEN
  235. EIGEN_MAKE_SCALAR_BINARY_OP_ONTHELEFT(operator/,quotient)
  236. #else
  237. /**
  238. * \brief Component-wise division of the scalar \a s by array elements of \a a.
  239. *
  240. * \tparam Scalar is the scalar type of \a x. It must be compatible with the scalar type of the given array expression (\c Derived::Scalar).
  241. */
  242. template<typename T> friend
  243. inline const CwiseBinaryOp<internal::scalar_quotient_op<T,Scalar>,Constant<T>,Derived>
  244. operator/(const T& s,const StorageBaseType& a);
  245. #endif
  246. /** \returns an expression of the coefficient-wise ^ operator of *this and \a other
  247. *
  248. * \warning this operator is for expression of bool only.
  249. *
  250. * Example: \include Cwise_boolean_xor.cpp
  251. * Output: \verbinclude Cwise_boolean_xor.out
  252. *
  253. * \sa operator&&(), select()
  254. */
  255. template<typename OtherDerived>
  256. EIGEN_DEVICE_FUNC
  257. inline const CwiseBinaryOp<internal::scalar_boolean_xor_op, const Derived, const OtherDerived>
  258. operator^(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> &other) const
  259. {
  260. EIGEN_STATIC_ASSERT((internal::is_same<bool,Scalar>::value && internal::is_same<bool,typename OtherDerived::Scalar>::value),
  261. THIS_METHOD_IS_ONLY_FOR_EXPRESSIONS_OF_BOOL);
  262. return CwiseBinaryOp<internal::scalar_boolean_xor_op, const Derived, const OtherDerived>(derived(),other.derived());
  263. }
  264. // NOTE disabled until we agree on argument order
  265. #if 0
  266. /** \cpp11 \returns an expression of the coefficient-wise polygamma function.
  267. *
  268. * \specialfunctions_module
  269. *
  270. * It returns the \a n -th derivative of the digamma(psi) evaluated at \c *this.
  271. *
  272. * \warning Be careful with the order of the parameters: x.polygamma(n) is equivalent to polygamma(n,x)
  273. *
  274. * \sa Eigen::polygamma()
  275. */
  276. template<typename DerivedN>
  277. inline const CwiseBinaryOp<internal::scalar_polygamma_op<Scalar>, const DerivedN, const Derived>
  278. polygamma(const EIGEN_CURRENT_STORAGE_BASE_CLASS<DerivedN> &n) const
  279. {
  280. return CwiseBinaryOp<internal::scalar_polygamma_op<Scalar>, const DerivedN, const Derived>(n.derived(), this->derived());
  281. }
  282. #endif
  283. /** \returns an expression of the coefficient-wise zeta function.
  284. *
  285. * \specialfunctions_module
  286. *
  287. * It returns the Riemann zeta function of two arguments \c *this and \a q:
  288. *
  289. * \param *this is the exposent, it must be > 1
  290. * \param q is the shift, it must be > 0
  291. *
  292. * \note This function supports only float and double scalar types. To support other scalar types, the user has
  293. * to provide implementations of zeta(T,T) for any scalar type T to be supported.
  294. *
  295. * This method is an alias for zeta(*this,q);
  296. *
  297. * \sa Eigen::zeta()
  298. */
  299. template<typename DerivedQ>
  300. inline const CwiseBinaryOp<internal::scalar_zeta_op<Scalar>, const Derived, const DerivedQ>
  301. zeta(const EIGEN_CURRENT_STORAGE_BASE_CLASS<DerivedQ> &q) const
  302. {
  303. return CwiseBinaryOp<internal::scalar_zeta_op<Scalar>, const Derived, const DerivedQ>(this->derived(), q.derived());
  304. }