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 (at) inria.fr> 5 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1 (at) 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 11 // This file is a base class plugin containing common coefficient wise functions. 12 13 #ifndef EIGEN_PARSED_BY_DOXYGEN 14 15 /** \internal Represents a scalar multiple of an expression */ 16 typedef CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, const Derived> ScalarMultipleReturnType; 17 /** \internal Represents a quotient of an expression by a scalar*/ 18 typedef CwiseUnaryOp<internal::scalar_quotient1_op<Scalar>, const Derived> ScalarQuotient1ReturnType; 19 /** \internal the return type of conjugate() */ 20 typedef typename internal::conditional<NumTraits<Scalar>::IsComplex, 21 const CwiseUnaryOp<internal::scalar_conjugate_op<Scalar>, const Derived>, 22 const Derived& 23 >::type ConjugateReturnType; 24 /** \internal the return type of real() const */ 25 typedef typename internal::conditional<NumTraits<Scalar>::IsComplex, 26 const CwiseUnaryOp<internal::scalar_real_op<Scalar>, const Derived>, 27 const Derived& 28 >::type RealReturnType; 29 /** \internal the return type of real() */ 30 typedef typename internal::conditional<NumTraits<Scalar>::IsComplex, 31 CwiseUnaryView<internal::scalar_real_ref_op<Scalar>, Derived>, 32 Derived& 33 >::type NonConstRealReturnType; 34 /** \internal the return type of imag() const */ 35 typedef CwiseUnaryOp<internal::scalar_imag_op<Scalar>, const Derived> ImagReturnType; 36 /** \internal the return type of imag() */ 37 typedef CwiseUnaryView<internal::scalar_imag_ref_op<Scalar>, Derived> NonConstImagReturnType; 38 39 #endif // not EIGEN_PARSED_BY_DOXYGEN 40 41 /** \returns an expression of the opposite of \c *this 42 */ 43 inline const CwiseUnaryOp<internal::scalar_opposite_op<typename internal::traits<Derived>::Scalar>, const Derived> 44 operator-() const { return derived(); } 45 46 47 /** \returns an expression of \c *this scaled by the scalar factor \a scalar */ 48 inline const ScalarMultipleReturnType 49 operator*(const Scalar& scalar) const 50 { 51 return CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, const Derived> 52 (derived(), internal::scalar_multiple_op<Scalar>(scalar)); 53 } 54 55 #ifdef EIGEN_PARSED_BY_DOXYGEN 56 const ScalarMultipleReturnType operator*(const RealScalar& scalar) const; 57 #endif 58 59 /** \returns an expression of \c *this divided by the scalar value \a scalar */ 60 inline const CwiseUnaryOp<internal::scalar_quotient1_op<typename internal::traits<Derived>::Scalar>, const Derived> 61 operator/(const Scalar& scalar) const 62 { 63 return CwiseUnaryOp<internal::scalar_quotient1_op<Scalar>, const Derived> 64 (derived(), internal::scalar_quotient1_op<Scalar>(scalar)); 65 } 66 67 /** Overloaded for efficient real matrix times complex scalar value */ 68 inline const CwiseUnaryOp<internal::scalar_multiple2_op<Scalar,std::complex<Scalar> >, const Derived> 69 operator*(const std::complex<Scalar>& scalar) const 70 { 71 return CwiseUnaryOp<internal::scalar_multiple2_op<Scalar,std::complex<Scalar> >, const Derived> 72 (*static_cast<const Derived*>(this), internal::scalar_multiple2_op<Scalar,std::complex<Scalar> >(scalar)); 73 } 74 75 inline friend const ScalarMultipleReturnType 76 operator*(const Scalar& scalar, const StorageBaseType& matrix) 77 { return matrix*scalar; } 78 79 inline friend const CwiseUnaryOp<internal::scalar_multiple2_op<Scalar,std::complex<Scalar> >, const Derived> 80 operator*(const std::complex<Scalar>& scalar, const StorageBaseType& matrix) 81 { return matrix*scalar; } 82 83 /** \returns an expression of *this with the \a Scalar type casted to 84 * \a NewScalar. 85 * 86 * The template parameter \a NewScalar is the type we are casting the scalars to. 87 * 88 * \sa class CwiseUnaryOp 89 */ 90 template<typename NewType> 91 typename internal::cast_return_type<Derived,const CwiseUnaryOp<internal::scalar_cast_op<typename internal::traits<Derived>::Scalar, NewType>, const Derived> >::type 92 cast() const 93 { 94 return derived(); 95 } 96 97 /** \returns an expression of the complex conjugate of \c *this. 98 * 99 * \sa adjoint() */ 100 inline ConjugateReturnType 101 conjugate() const 102 { 103 return ConjugateReturnType(derived()); 104 } 105 106 /** \returns a read-only expression of the real part of \c *this. 107 * 108 * \sa imag() */ 109 inline RealReturnType 110 real() const { return derived(); } 111 112 /** \returns an read-only expression of the imaginary part of \c *this. 113 * 114 * \sa real() */ 115 inline const ImagReturnType 116 imag() const { return derived(); } 117 118 /** \brief Apply a unary operator coefficient-wise 119 * \param[in] func Functor implementing the unary operator 120 * \tparam CustomUnaryOp Type of \a func 121 * \returns An expression of a custom coefficient-wise unary operator \a func of *this 122 * 123 * The function \c ptr_fun() from the C++ standard library can be used to make functors out of normal functions. 124 * 125 * Example: 126 * \include class_CwiseUnaryOp_ptrfun.cpp 127 * Output: \verbinclude class_CwiseUnaryOp_ptrfun.out 128 * 129 * Genuine functors allow for more possibilities, for instance it may contain a state. 130 * 131 * Example: 132 * \include class_CwiseUnaryOp.cpp 133 * Output: \verbinclude class_CwiseUnaryOp.out 134 * 135 * \sa class CwiseUnaryOp, class CwiseBinaryOp 136 */ 137 template<typename CustomUnaryOp> 138 inline const CwiseUnaryOp<CustomUnaryOp, const Derived> 139 unaryExpr(const CustomUnaryOp& func = CustomUnaryOp()) const 140 { 141 return CwiseUnaryOp<CustomUnaryOp, const Derived>(derived(), func); 142 } 143 144 /** \returns an expression of a custom coefficient-wise unary operator \a func of *this 145 * 146 * The template parameter \a CustomUnaryOp is the type of the functor 147 * of the custom unary operator. 148 * 149 * Example: 150 * \include class_CwiseUnaryOp.cpp 151 * Output: \verbinclude class_CwiseUnaryOp.out 152 * 153 * \sa class CwiseUnaryOp, class CwiseBinaryOp 154 */ 155 template<typename CustomViewOp> 156 inline const CwiseUnaryView<CustomViewOp, const Derived> 157 unaryViewExpr(const CustomViewOp& func = CustomViewOp()) const 158 { 159 return CwiseUnaryView<CustomViewOp, const Derived>(derived(), func); 160 } 161 162 /** \returns a non const expression of the real part of \c *this. 163 * 164 * \sa imag() */ 165 inline NonConstRealReturnType 166 real() { return derived(); } 167 168 /** \returns a non const expression of the imaginary part of \c *this. 169 * 170 * \sa real() */ 171 inline NonConstImagReturnType 172 imag() { return derived(); } 173