Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef EIGEN_DOT_H
00026 #define EIGEN_DOT_H
00027
00028 namespace internal {
00029
00030
00031
00032
00033 template<typename T, typename U,
00034
00035 bool NeedToTranspose = T::IsVectorAtCompileTime
00036 && U::IsVectorAtCompileTime
00037 && ((int(T::RowsAtCompileTime) == 1 && int(U::ColsAtCompileTime) == 1)
00038 |
00039
00040 (int(T::ColsAtCompileTime) == 1 && int(U::RowsAtCompileTime) == 1))
00041 >
00042 struct dot_nocheck
00043 {
00044 typedef typename scalar_product_traits<typename traits<T>::Scalar,typename traits<U>::Scalar>::ReturnType ResScalar;
00045 static inline ResScalar run(const MatrixBase<T>& a, const MatrixBase<U>& b)
00046 {
00047 return a.template binaryExpr<scalar_conj_product_op<typename traits<T>::Scalar,typename traits<U>::Scalar> >(b).sum();
00048 }
00049 };
00050
00051 template<typename T, typename U>
00052 struct dot_nocheck<T, U, true>
00053 {
00054 typedef typename scalar_product_traits<typename traits<T>::Scalar,typename traits<U>::Scalar>::ReturnType ResScalar;
00055 static inline ResScalar run(const MatrixBase<T>& a, const MatrixBase<U>& b)
00056 {
00057 return a.transpose().template binaryExpr<scalar_conj_product_op<typename traits<T>::Scalar,typename traits<U>::Scalar> >(b).sum();
00058 }
00059 };
00060
00061 }
00062
00073 template<typename Derived>
00074 template<typename OtherDerived>
00075 typename internal::scalar_product_traits<typename internal::traits<Derived>::Scalar,typename internal::traits<OtherDerived>::Scalar>::ReturnType
00076 MatrixBase<Derived>::dot(const MatrixBase<OtherDerived>& other) const
00077 {
00078 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00079 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00080 EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(Derived,OtherDerived)
00081 typedef internal::scalar_conj_product_op<Scalar,typename OtherDerived::Scalar> func;
00082 EIGEN_CHECK_BINARY_COMPATIBILIY(func,Scalar,typename OtherDerived::Scalar);
00083
00084 eigen_assert(size() == other.size());
00085
00086 return internal::dot_nocheck<Derived,OtherDerived>::run(*this, other);
00087 }
00088
00089 #ifdef EIGEN2_SUPPORT
00090
00099 template<typename Derived>
00100 template<typename OtherDerived>
00101 typename internal::traits<Derived>::Scalar
00102 MatrixBase<Derived>::eigen2_dot(const MatrixBase<OtherDerived>& other) const
00103 {
00104 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00105 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00106 EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(Derived,OtherDerived)
00107 EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename OtherDerived::Scalar>::value),
00108 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
00109
00110 eigen_assert(size() == other.size());
00111
00112 return internal::dot_nocheck<OtherDerived,Derived>::run(other,*this);
00113 }
00114 #endif
00115
00116
00117
00118
00123 template<typename Derived>
00124 EIGEN_STRONG_INLINE typename NumTraits<typename internal::traits<Derived>::Scalar>::Real MatrixBase<Derived>::squaredNorm() const
00125 {
00126 return internal::real((*this).cwiseAbs2().sum());
00127 }
00128
00133 template<typename Derived>
00134 inline typename NumTraits<typename internal::traits<Derived>::Scalar>::Real MatrixBase<Derived>::norm() const
00135 {
00136 return internal::sqrt(squaredNorm());
00137 }
00138
00145 template<typename Derived>
00146 inline const typename MatrixBase<Derived>::PlainObject
00147 MatrixBase<Derived>::normalized() const
00148 {
00149 typedef typename internal::nested<Derived>::type Nested;
00150 typedef typename internal::remove_reference<Nested>::type _Nested;
00151 _Nested n(derived());
00152 return n / n.norm();
00153 }
00154
00161 template<typename Derived>
00162 inline void MatrixBase<Derived>::normalize()
00163 {
00164 *this /= norm();
00165 }
00166
00167
00168
00169 namespace internal {
00170
00171 template<typename Derived, int p>
00172 struct lpNorm_selector
00173 {
00174 typedef typename NumTraits<typename traits<Derived>::Scalar>::Real RealScalar;
00175 inline static RealScalar run(const MatrixBase<Derived>& m)
00176 {
00177 return pow(m.cwiseAbs().array().pow(p).sum(), RealScalar(1)/p);
00178 }
00179 };
00180
00181 template<typename Derived>
00182 struct lpNorm_selector<Derived, 1>
00183 {
00184 inline static typename NumTraits<typename traits<Derived>::Scalar>::Real run(const MatrixBase<Derived>& m)
00185 {
00186 return m.cwiseAbs().sum();
00187 }
00188 };
00189
00190 template<typename Derived>
00191 struct lpNorm_selector<Derived, 2>
00192 {
00193 inline static typename NumTraits<typename traits<Derived>::Scalar>::Real run(const MatrixBase<Derived>& m)
00194 {
00195 return m.norm();
00196 }
00197 };
00198
00199 template<typename Derived>
00200 struct lpNorm_selector<Derived, Infinity>
00201 {
00202 inline static typename NumTraits<typename traits<Derived>::Scalar>::Real run(const MatrixBase<Derived>& m)
00203 {
00204 return m.cwiseAbs().maxCoeff();
00205 }
00206 };
00207
00208 }
00209
00216 template<typename Derived>
00217 template<int p>
00218 inline typename NumTraits<typename internal::traits<Derived>::Scalar>::Real
00219 MatrixBase<Derived>::lpNorm() const
00220 {
00221 return internal::lpNorm_selector<Derived, p>::run(*this);
00222 }
00223
00224
00225
00232 template<typename Derived>
00233 template<typename OtherDerived>
00234 bool MatrixBase<Derived>::isOrthogonal
00235 (const MatrixBase<OtherDerived>& other, RealScalar prec) const
00236 {
00237 typename internal::nested<Derived,2>::type nested(derived());
00238 typename internal::nested<OtherDerived,2>::type otherNested(other.derived());
00239 return internal::abs2(nested.dot(otherNested)) <= prec * prec * nested.squaredNorm() * otherNested.squaredNorm();
00240 }
00241
00253 template<typename Derived>
00254 bool MatrixBase<Derived>::isUnitary(RealScalar prec) const
00255 {
00256 typename Derived::Nested nested(derived());
00257 for(Index i = 0; i < cols(); ++i)
00258 {
00259 if(!internal::isApprox(nested.col(i).squaredNorm(), static_cast<RealScalar>(1), prec))
00260 return false;
00261 for(Index j = 0; j < i; ++j)
00262 if(!internal::isMuchSmallerThan(nested.col(i).dot(nested.col(j)), static_cast<Scalar>(1), prec))
00263 return false;
00264 }
00265 return true;
00266 }
00267
00268 #endif // EIGEN_DOT_H