CommonCwiseUnaryOps.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008-2009 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. #ifndef EIGEN_PARSED_BY_DOXYGEN
  12. /** \internal the return type of conjugate() */
  13. typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
  14. const CwiseUnaryOp<internal::scalar_conjugate_op<Scalar>, const Derived>,
  15. const Derived&
  16. >::type ConjugateReturnType;
  17. /** \internal the return type of real() const */
  18. typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
  19. const CwiseUnaryOp<internal::scalar_real_op<Scalar>, const Derived>,
  20. const Derived&
  21. >::type RealReturnType;
  22. /** \internal the return type of real() */
  23. typedef typename internal::conditional<NumTraits<Scalar>::IsComplex,
  24. CwiseUnaryView<internal::scalar_real_ref_op<Scalar>, Derived>,
  25. Derived&
  26. >::type NonConstRealReturnType;
  27. /** \internal the return type of imag() const */
  28. typedef CwiseUnaryOp<internal::scalar_imag_op<Scalar>, const Derived> ImagReturnType;
  29. /** \internal the return type of imag() */
  30. typedef CwiseUnaryView<internal::scalar_imag_ref_op<Scalar>, Derived> NonConstImagReturnType;
  31. typedef CwiseUnaryOp<internal::scalar_opposite_op<Scalar>, const Derived> NegativeReturnType;
  32. #endif // not EIGEN_PARSED_BY_DOXYGEN
  33. /// \returns an expression of the opposite of \c *this
  34. ///
  35. EIGEN_DOC_UNARY_ADDONS(operator-,opposite)
  36. ///
  37. EIGEN_DEVICE_FUNC
  38. inline const NegativeReturnType
  39. operator-() const { return NegativeReturnType(derived()); }
  40. template<class NewType> struct CastXpr { typedef typename internal::cast_return_type<Derived,const CwiseUnaryOp<internal::scalar_cast_op<Scalar, NewType>, const Derived> >::type Type; };
  41. /// \returns an expression of \c *this with the \a Scalar type casted to
  42. /// \a NewScalar.
  43. ///
  44. /// The template parameter \a NewScalar is the type we are casting the scalars to.
  45. ///
  46. EIGEN_DOC_UNARY_ADDONS(cast,conversion function)
  47. ///
  48. /// \sa class CwiseUnaryOp
  49. ///
  50. template<typename NewType>
  51. EIGEN_DEVICE_FUNC
  52. typename CastXpr<NewType>::Type
  53. cast() const
  54. {
  55. return typename CastXpr<NewType>::Type(derived());
  56. }
  57. /// \returns an expression of the complex conjugate of \c *this.
  58. ///
  59. EIGEN_DOC_UNARY_ADDONS(conjugate,complex conjugate)
  60. ///
  61. /// \sa <a href="group__CoeffwiseMathFunctions.html#cwisetable_conj">Math functions</a>, MatrixBase::adjoint()
  62. EIGEN_DEVICE_FUNC
  63. inline ConjugateReturnType
  64. conjugate() const
  65. {
  66. return ConjugateReturnType(derived());
  67. }
  68. /// \returns an expression of the complex conjugate of \c *this if Cond==true, returns derived() otherwise.
  69. ///
  70. EIGEN_DOC_UNARY_ADDONS(conjugate,complex conjugate)
  71. ///
  72. /// \sa conjugate()
  73. template<bool Cond>
  74. EIGEN_DEVICE_FUNC
  75. inline typename internal::conditional<Cond,ConjugateReturnType,const Derived&>::type
  76. conjugateIf() const
  77. {
  78. typedef typename internal::conditional<Cond,ConjugateReturnType,const Derived&>::type ReturnType;
  79. return ReturnType(derived());
  80. }
  81. /// \returns a read-only expression of the real part of \c *this.
  82. ///
  83. EIGEN_DOC_UNARY_ADDONS(real,real part function)
  84. ///
  85. /// \sa imag()
  86. EIGEN_DEVICE_FUNC
  87. inline RealReturnType
  88. real() const { return RealReturnType(derived()); }
  89. /// \returns an read-only expression of the imaginary part of \c *this.
  90. ///
  91. EIGEN_DOC_UNARY_ADDONS(imag,imaginary part function)
  92. ///
  93. /// \sa real()
  94. EIGEN_DEVICE_FUNC
  95. inline const ImagReturnType
  96. imag() const { return ImagReturnType(derived()); }
  97. /// \brief Apply a unary operator coefficient-wise
  98. /// \param[in] func Functor implementing the unary operator
  99. /// \tparam CustomUnaryOp Type of \a func
  100. /// \returns An expression of a custom coefficient-wise unary operator \a func of *this
  101. ///
  102. /// The function \c ptr_fun() from the C++ standard library can be used to make functors out of normal functions.
  103. ///
  104. /// Example:
  105. /// \include class_CwiseUnaryOp_ptrfun.cpp
  106. /// Output: \verbinclude class_CwiseUnaryOp_ptrfun.out
  107. ///
  108. /// Genuine functors allow for more possibilities, for instance it may contain a state.
  109. ///
  110. /// Example:
  111. /// \include class_CwiseUnaryOp.cpp
  112. /// Output: \verbinclude class_CwiseUnaryOp.out
  113. ///
  114. EIGEN_DOC_UNARY_ADDONS(unaryExpr,unary function)
  115. ///
  116. /// \sa unaryViewExpr, binaryExpr, class CwiseUnaryOp
  117. ///
  118. template<typename CustomUnaryOp>
  119. EIGEN_DEVICE_FUNC
  120. inline const CwiseUnaryOp<CustomUnaryOp, const Derived>
  121. unaryExpr(const CustomUnaryOp& func = CustomUnaryOp()) const
  122. {
  123. return CwiseUnaryOp<CustomUnaryOp, const Derived>(derived(), func);
  124. }
  125. /// \returns an expression of a custom coefficient-wise unary operator \a func of *this
  126. ///
  127. /// The template parameter \a CustomUnaryOp is the type of the functor
  128. /// of the custom unary operator.
  129. ///
  130. /// Example:
  131. /// \include class_CwiseUnaryOp.cpp
  132. /// Output: \verbinclude class_CwiseUnaryOp.out
  133. ///
  134. EIGEN_DOC_UNARY_ADDONS(unaryViewExpr,unary function)
  135. ///
  136. /// \sa unaryExpr, binaryExpr class CwiseUnaryOp
  137. ///
  138. template<typename CustomViewOp>
  139. EIGEN_DEVICE_FUNC
  140. inline const CwiseUnaryView<CustomViewOp, const Derived>
  141. unaryViewExpr(const CustomViewOp& func = CustomViewOp()) const
  142. {
  143. return CwiseUnaryView<CustomViewOp, const Derived>(derived(), func);
  144. }
  145. /// \returns a non const expression of the real part of \c *this.
  146. ///
  147. EIGEN_DOC_UNARY_ADDONS(real,real part function)
  148. ///
  149. /// \sa imag()
  150. EIGEN_DEVICE_FUNC
  151. inline NonConstRealReturnType
  152. real() { return NonConstRealReturnType(derived()); }
  153. /// \returns a non const expression of the imaginary part of \c *this.
  154. ///
  155. EIGEN_DOC_UNARY_ADDONS(imag,imaginary part function)
  156. ///
  157. /// \sa real()
  158. EIGEN_DEVICE_FUNC
  159. inline NonConstImagReturnType
  160. imag() { return NonConstImagReturnType(derived()); }