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