Dot.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2006-2008, 2010 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_DOT_H
00011 #define EIGEN_DOT_H
00012 
00013 namespace Eigen { 
00014 
00015 namespace internal {
00016 
00017 // helper function for dot(). The problem is that if we put that in the body of dot(), then upon calling dot
00018 // with mismatched types, the compiler emits errors about failing to instantiate cwiseProduct BEFORE
00019 // looking at the static assertions. Thus this is a trick to get better compile errors.
00020 template<typename T, typename U,
00021 // the NeedToTranspose condition here is taken straight from Assign.h
00022          bool NeedToTranspose = T::IsVectorAtCompileTime
00023                 && U::IsVectorAtCompileTime
00024                 && ((int(T::RowsAtCompileTime) == 1 && int(U::ColsAtCompileTime) == 1)
00025                       |  // FIXME | instead of || to please GCC 4.4.0 stupid warning "suggest parentheses around &&".
00026                          // revert to || as soon as not needed anymore.
00027                     (int(T::ColsAtCompileTime) == 1 && int(U::RowsAtCompileTime) == 1))
00028 >
00029 struct dot_nocheck
00030 {
00031   typedef typename scalar_product_traits<typename traits<T>::Scalar,typename traits<U>::Scalar>::ReturnType ResScalar;
00032   static inline ResScalar run(const MatrixBase<T>& a, const MatrixBase<U>& b)
00033   {
00034     return a.template binaryExpr<scalar_conj_product_op<typename traits<T>::Scalar,typename traits<U>::Scalar> >(b).sum();
00035   }
00036 };
00037 
00038 template<typename T, typename U>
00039 struct dot_nocheck<T, U, true>
00040 {
00041   typedef typename scalar_product_traits<typename traits<T>::Scalar,typename traits<U>::Scalar>::ReturnType ResScalar;
00042   static inline ResScalar run(const MatrixBase<T>& a, const MatrixBase<U>& b)
00043   {
00044     return a.transpose().template binaryExpr<scalar_conj_product_op<typename traits<T>::Scalar,typename traits<U>::Scalar> >(b).sum();
00045   }
00046 };
00047 
00048 } // end namespace internal
00049 
00060 template<typename Derived>
00061 template<typename OtherDerived>
00062 typename internal::scalar_product_traits<typename internal::traits<Derived>::Scalar,typename internal::traits<OtherDerived>::Scalar>::ReturnType
00063 MatrixBase<Derived>::dot(const MatrixBase<OtherDerived>& other) const
00064 {
00065   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00066   EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00067   EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(Derived,OtherDerived)
00068   typedef internal::scalar_conj_product_op<Scalar,typename OtherDerived::Scalar> func;
00069   EIGEN_CHECK_BINARY_COMPATIBILIY(func,Scalar,typename OtherDerived::Scalar);
00070 
00071   eigen_assert(size() == other.size());
00072 
00073   return internal::dot_nocheck<Derived,OtherDerived>::run(*this, other);
00074 }
00075 
00076 #ifdef EIGEN2_SUPPORT
00077 
00086 template<typename Derived>
00087 template<typename OtherDerived>
00088 typename internal::traits<Derived>::Scalar
00089 MatrixBase<Derived>::eigen2_dot(const MatrixBase<OtherDerived>& other) const
00090 {
00091   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00092   EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00093   EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(Derived,OtherDerived)
00094   EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename OtherDerived::Scalar>::value),
00095     YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
00096 
00097   eigen_assert(size() == other.size());
00098 
00099   return internal::dot_nocheck<OtherDerived,Derived>::run(other,*this);
00100 }
00101 #endif
00102 
00103 
00104 //---------- implementation of L2 norm and related functions ----------
00105 
00112 template<typename Derived>
00113 EIGEN_STRONG_INLINE typename NumTraits<typename internal::traits<Derived>::Scalar>::Real MatrixBase<Derived>::squaredNorm() const
00114 {
00115   return internal::real((*this).cwiseAbs2().sum());
00116 }
00117 
00124 template<typename Derived>
00125 inline typename NumTraits<typename internal::traits<Derived>::Scalar>::Real MatrixBase<Derived>::norm() const
00126 {
00127   return internal::sqrt(squaredNorm());
00128 }
00129 
00136 template<typename Derived>
00137 inline const typename MatrixBase<Derived>::PlainObject
00138 MatrixBase<Derived>::normalized() const
00139 {
00140   typedef typename internal::nested<Derived>::type Nested;
00141   typedef typename internal::remove_reference<Nested>::type _Nested;
00142   _Nested n(derived());
00143   return n / n.norm();
00144 }
00145 
00152 template<typename Derived>
00153 inline void MatrixBase<Derived>::normalize()
00154 {
00155   *this /= norm();
00156 }
00157 
00158 //---------- implementation of other norms ----------
00159 
00160 namespace internal {
00161 
00162 template<typename Derived, int p>
00163 struct lpNorm_selector
00164 {
00165   typedef typename NumTraits<typename traits<Derived>::Scalar>::Real RealScalar;
00166   static inline RealScalar run(const MatrixBase<Derived>& m)
00167   {
00168     return pow(m.cwiseAbs().array().pow(p).sum(), RealScalar(1)/p);
00169   }
00170 };
00171 
00172 template<typename Derived>
00173 struct lpNorm_selector<Derived, 1>
00174 {
00175   static inline typename NumTraits<typename traits<Derived>::Scalar>::Real run(const MatrixBase<Derived>& m)
00176   {
00177     return m.cwiseAbs().sum();
00178   }
00179 };
00180 
00181 template<typename Derived>
00182 struct lpNorm_selector<Derived, 2>
00183 {
00184   static inline typename NumTraits<typename traits<Derived>::Scalar>::Real run(const MatrixBase<Derived>& m)
00185   {
00186     return m.norm();
00187   }
00188 };
00189 
00190 template<typename Derived>
00191 struct lpNorm_selector<Derived, Infinity>
00192 {
00193   static inline typename NumTraits<typename traits<Derived>::Scalar>::Real run(const MatrixBase<Derived>& m)
00194   {
00195     return m.cwiseAbs().maxCoeff();
00196   }
00197 };
00198 
00199 } // end namespace internal
00200 
00207 template<typename Derived>
00208 template<int p>
00209 inline typename NumTraits<typename internal::traits<Derived>::Scalar>::Real
00210 MatrixBase<Derived>::lpNorm() const
00211 {
00212   return internal::lpNorm_selector<Derived, p>::run(*this);
00213 }
00214 
00215 //---------- implementation of isOrthogonal / isUnitary ----------
00216 
00223 template<typename Derived>
00224 template<typename OtherDerived>
00225 bool MatrixBase<Derived>::isOrthogonal
00226 (const MatrixBase<OtherDerived>& other, RealScalar prec) const
00227 {
00228   typename internal::nested<Derived,2>::type nested(derived());
00229   typename internal::nested<OtherDerived,2>::type otherNested(other.derived());
00230   return internal::abs2(nested.dot(otherNested)) <= prec * prec * nested.squaredNorm() * otherNested.squaredNorm();
00231 }
00232 
00244 template<typename Derived>
00245 bool MatrixBase<Derived>::isUnitary(RealScalar prec) const
00246 {
00247   typename Derived::Nested nested(derived());
00248   for(Index i = 0; i < cols(); ++i)
00249   {
00250     if(!internal::isApprox(nested.col(i).squaredNorm(), static_cast<RealScalar>(1), prec))
00251       return false;
00252     for(Index j = 0; j < i; ++j)
00253       if(!internal::isMuchSmallerThan(nested.col(i).dot(nested.col(j)), static_cast<Scalar>(1), prec))
00254         return false;
00255   }
00256   return true;
00257 }
00258 
00259 } // end namespace Eigen
00260 
00261 #endif // EIGEN_DOT_H


win_eigen
Author(s): Daniel Stonier
autogenerated on Wed Sep 16 2015 07:10:30