Dot.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2006-2008, 2010 Benoit Jacob <jacob.benoit.1@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_DOT_H
11 #define EIGEN_DOT_H
12 
13 namespace Eigen {
14 
15 namespace internal {
16 
17 // helper function for dot(). The problem is that if we put that in the body of dot(), then upon calling dot
18 // with mismatched types, the compiler emits errors about failing to instantiate cwiseProduct BEFORE
19 // looking at the static assertions. Thus this is a trick to get better compile errors.
20 template<typename T, typename U,
21 // the NeedToTranspose condition here is taken straight from Assign.h
22  bool NeedToTranspose = T::IsVectorAtCompileTime
23  && U::IsVectorAtCompileTime
24  && ((int(T::RowsAtCompileTime) == 1 && int(U::ColsAtCompileTime) == 1)
25  | // FIXME | instead of || to please GCC 4.4.0 stupid warning "suggest parentheses around &&".
26  // revert to || as soon as not needed anymore.
27  (int(T::ColsAtCompileTime) == 1 && int(U::RowsAtCompileTime) == 1))
28 >
30 {
33  EIGEN_DEVICE_FUNC
34  static inline ResScalar run(const MatrixBase<T>& a, const MatrixBase<U>& b)
35  {
36  return a.template binaryExpr<conj_prod>(b).sum();
37  }
38 };
39 
40 template<typename T, typename U>
41 struct dot_nocheck<T, U, true>
42 {
45  EIGEN_DEVICE_FUNC
46  static inline ResScalar run(const MatrixBase<T>& a, const MatrixBase<U>& b)
47  {
48  return a.transpose().template binaryExpr<conj_prod>(b).sum();
49  }
50 };
51 
52 } // end namespace internal
53 
65 template<typename Derived>
66 template<typename OtherDerived>
67 EIGEN_DEVICE_FUNC
70 {
73  EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(Derived,OtherDerived)
74 #if !(defined(EIGEN_NO_STATIC_ASSERT) && defined(EIGEN_NO_DEBUG))
76  EIGEN_CHECK_BINARY_COMPATIBILIY(func,Scalar,typename OtherDerived::Scalar);
77 #endif
78 
79  eigen_assert(size() == other.size());
80 
82 }
83 
84 //---------- implementation of L2 norm and related functions ----------
85 
92 template<typename Derived>
94 {
95  return numext::real((*this).cwiseAbs2().sum());
96 }
97 
104 template<typename Derived>
106 {
107  return numext::sqrt(squaredNorm());
108 }
109 
119 template<typename Derived>
120 inline const typename MatrixBase<Derived>::PlainObject
122 {
123  typedef typename internal::nested_eval<Derived,2>::type _Nested;
124  _Nested n(derived());
125  RealScalar z = n.squaredNorm();
126  // NOTE: after extensive benchmarking, this conditional does not impact performance, at least on recent x86 CPU
127  if(z>RealScalar(0))
128  return n / numext::sqrt(z);
129  else
130  return n;
131 }
132 
141 template<typename Derived>
143 {
144  RealScalar z = squaredNorm();
145  // NOTE: after extensive benchmarking, this conditional does not impact performance, at least on recent x86 CPU
146  if(z>RealScalar(0))
147  derived() /= numext::sqrt(z);
148 }
149 
162 template<typename Derived>
163 inline const typename MatrixBase<Derived>::PlainObject
165 {
166  typedef typename internal::nested_eval<Derived,3>::type _Nested;
167  _Nested n(derived());
168  RealScalar w = n.cwiseAbs().maxCoeff();
169  RealScalar z = (n/w).squaredNorm();
170  if(z>RealScalar(0))
171  return n / (numext::sqrt(z)*w);
172  else
173  return n;
174 }
175 
187 template<typename Derived>
189 {
190  RealScalar w = cwiseAbs().maxCoeff();
191  RealScalar z = (derived()/w).squaredNorm();
192  if(z>RealScalar(0))
193  derived() /= numext::sqrt(z)*w;
194 }
195 
196 //---------- implementation of other norms ----------
197 
198 namespace internal {
199 
200 template<typename Derived, int p>
202 {
204  EIGEN_DEVICE_FUNC
205  static inline RealScalar run(const MatrixBase<Derived>& m)
206  {
207  EIGEN_USING_STD_MATH(pow)
208  return pow(m.cwiseAbs().array().pow(p).sum(), RealScalar(1)/p);
209  }
210 };
211 
212 template<typename Derived>
213 struct lpNorm_selector<Derived, 1>
214 {
215  EIGEN_DEVICE_FUNC
217  {
218  return m.cwiseAbs().sum();
219  }
220 };
221 
222 template<typename Derived>
223 struct lpNorm_selector<Derived, 2>
224 {
225  EIGEN_DEVICE_FUNC
227  {
228  return m.norm();
229  }
230 };
231 
232 template<typename Derived>
233 struct lpNorm_selector<Derived, Infinity>
234 {
236  EIGEN_DEVICE_FUNC
237  static inline RealScalar run(const MatrixBase<Derived>& m)
238  {
239  if(Derived::SizeAtCompileTime==0 || (Derived::SizeAtCompileTime==Dynamic && m.size()==0))
240  return RealScalar(0);
241  return m.cwiseAbs().maxCoeff();
242  }
243 };
244 
245 } // end namespace internal
246 
257 template<typename Derived>
258 template<int p>
259 #ifndef EIGEN_PARSED_BY_DOXYGEN
261 #else
263 #endif
265 {
267 }
268 
269 //---------- implementation of isOrthogonal / isUnitary ----------
270 
277 template<typename Derived>
278 template<typename OtherDerived>
280 (const MatrixBase<OtherDerived>& other, const RealScalar& prec) const
281 {
282  typename internal::nested_eval<Derived,2>::type nested(derived());
283  typename internal::nested_eval<OtherDerived,2>::type otherNested(other.derived());
284  return numext::abs2(nested.dot(otherNested)) <= prec * prec * nested.squaredNorm() * otherNested.squaredNorm();
285 }
286 
298 template<typename Derived>
300 {
301  typename internal::nested_eval<Derived,1>::type self(derived());
302  for(Index i = 0; i < cols(); ++i)
303  {
304  if(!internal::isApprox(self.col(i).squaredNorm(), static_cast<RealScalar>(1), prec))
305  return false;
306  for(Index j = 0; j < i; ++j)
307  if(!internal::isMuchSmallerThan(self.col(i).dot(self.col(j)), static_cast<Scalar>(1), prec))
308  return false;
309  }
310  return true;
311 }
312 
313 } // end namespace Eigen
314 
315 #endif // EIGEN_DOT_H
EIGEN_DEVICE_FUNC ScalarBinaryOpTraits< typename internal::traits< Derived >::Scalar, typename internal::traits< OtherDerived >::Scalar >::ReturnType dot(const MatrixBase< OtherDerived > &other) const
Definition: Dot.h:69
conj_prod::result_type ResScalar
Definition: Dot.h:44
static EIGEN_DEVICE_FUNC NumTraits< typename traits< Derived >::Scalar >::Real run(const MatrixBase< Derived > &m)
Definition: Dot.h:226
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half pow(const half &a, const half &b)
Definition: Half.h:407
#define EIGEN_STRONG_INLINE
Definition: Macros.h:493
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:66
EIGEN_DEVICE_FUNC RealReturnType real() const
#define EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(TYPE0, TYPE1)
Definition: StaticAssert.h:162
static EIGEN_DEVICE_FUNC ResScalar run(const MatrixBase< T > &a, const MatrixBase< U > &b)
Definition: Dot.h:46
static EIGEN_DEVICE_FUNC NumTraits< typename traits< Derived >::Scalar >::Real run(const MatrixBase< Derived > &m)
Definition: Dot.h:216
static EIGEN_DEVICE_FUNC RealScalar run(const MatrixBase< Derived > &m)
Definition: Dot.h:205
Definition: LDLT.h:16
static constexpr size_t size(Tuple< Args... > &)
Provides access to the number of elements in a tuple as a compile-time constant expression.
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:150
EIGEN_DEVICE_FUNC const PlainObject stableNormalized() const
Definition: Dot.h:164
EIGEN_DEVICE_FUNC TransposeReturnType transpose()
Definition: Transpose.h:172
NumTraits< typename traits< Derived >::Scalar >::Real RealScalar
Definition: Dot.h:235
EIGEN_DEVICE_FUNC void normalize()
Definition: Dot.h:142
conj_prod::result_type ResScalar
Definition: Dot.h:32
EIGEN_DEVICE_FUNC void stableNormalize()
Definition: Dot.h:188
EIGEN_DEVICE_FUNC ColXpr col(Index i)
This is the const version of col().
Definition: BlockMethods.h:838
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const CwiseAbsReturnType cwiseAbs() const
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
#define eigen_assert(x)
Definition: Macros.h:577
NumTraits< Scalar >::Real RealScalar
Definition: MatrixBase.h:58
EIGEN_DEVICE_FUNC const PlainObject normalized() const
Definition: Dot.h:121
Base::PlainObject PlainObject
Definition: MatrixBase.h:103
NumTraits< typename traits< Derived >::Scalar >::Real RealScalar
Definition: Dot.h:203
const mpreal sum(const mpreal tab[], const unsigned long int n, int &status, mp_rnd_t mode=mpreal::get_default_rnd())
Definition: mpreal.h:2381
scalar_conj_product_op< typename traits< T >::Scalar, typename traits< U >::Scalar > conj_prod
Definition: Dot.h:31
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Abs2ReturnType abs2() const
EIGEN_DEVICE_FUNC RealScalar squaredNorm() const
Definition: Dot.h:93
TFSIMD_FORCE_INLINE tfScalar dot(const Quaternion &q1, const Quaternion &q2)
TFSIMD_FORCE_INLINE const tfScalar & z() const
TFSIMD_FORCE_INLINE const tfScalar & w() const
NumTraits< Scalar >::Real RealScalar
Definition: DenseBase.h:73
bool isUnitary(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: Dot.h:299
EIGEN_DEVICE_FUNC RealScalar lpNorm() const
ScalarBinaryOpTraits< LhsScalar, RhsScalar, scalar_conj_product_op >::ReturnType result_type
static EIGEN_DEVICE_FUNC ResScalar run(const MatrixBase< T > &a, const MatrixBase< U > &b)
Definition: Dot.h:34
#define EIGEN_CHECK_BINARY_COMPATIBILIY(BINOP, LHS, RHS)
Definition: XprHelper.h:815
static EIGEN_DEVICE_FUNC RealScalar run(const MatrixBase< Derived > &m)
Definition: Dot.h:237
Determines whether the given binary operation of two numeric types is allowed and what the scalar ret...
Definition: XprHelper.h:766
bool isOrthogonal(const MatrixBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: Dot.h:280
const int Dynamic
Definition: Constants.h:21
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float sqrt(const float &x)
#define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE)
Definition: StaticAssert.h:137
EIGEN_DEVICE_FUNC const Scalar & b
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const CwiseAbsReturnType cwiseAbs() const
Definition: MatrixBase.h:33
EIGEN_DEVICE_FUNC RealScalar norm() const
Definition: Dot.h:105
scalar_conj_product_op< typename traits< T >::Scalar, typename traits< U >::Scalar > conj_prod
Definition: Dot.h:43
const int Infinity
Definition: Constants.h:31


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:08:10