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 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
22  using Real = typename NumTraits<Scalar>::Real;
23  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Real run(const Derived& a) {
24  Scalar result = a.unaryExpr(squared_norm_functor<Scalar>()).sum();
25  return numext::real(result) + numext::imag(result);
26  }
27 };
28 
29 template <typename Derived>
30 struct squared_norm_impl<Derived, bool> {
31  static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool run(const Derived& a) { return a.any(); }
32 };
33 
34 } // end namespace internal
35 
47 template <typename Derived>
48 template <typename OtherDerived>
51  typename internal::traits<OtherDerived>::Scalar>::ReturnType
53  return internal::dot_impl<Derived, OtherDerived>::run(derived(), other.derived());
54 }
55 
56 //---------- implementation of L2 norm and related functions ----------
57 
64 template <typename Derived>
68 }
69 
76 template <typename Derived>
79  return numext::sqrt(squaredNorm());
80 }
81 
91 template <typename Derived>
93  const {
94  typedef typename internal::nested_eval<Derived, 2>::type Nested_;
95  Nested_ n(derived());
96  RealScalar z = n.squaredNorm();
97  // NOTE: after extensive benchmarking, this conditional does not impact performance, at least on recent x86 CPU
98  if (z > RealScalar(0))
99  return n / numext::sqrt(z);
100  else
101  return n;
102 }
103 
112 template <typename Derived>
114  RealScalar z = squaredNorm();
115  // NOTE: after extensive benchmarking, this conditional does not impact performance, at least on recent x86 CPU
116  if (z > RealScalar(0)) derived() /= numext::sqrt(z);
117 }
118 
131 template <typename Derived>
134  typedef typename internal::nested_eval<Derived, 3>::type Nested_;
135  Nested_ n(derived());
136  RealScalar w = n.cwiseAbs().maxCoeff();
137  RealScalar z = (n / w).squaredNorm();
138  if (z > RealScalar(0))
139  return n / (numext::sqrt(z) * w);
140  else
141  return n;
142 }
143 
155 template <typename Derived>
157  RealScalar w = cwiseAbs().maxCoeff();
158  RealScalar z = (derived() / w).squaredNorm();
159  if (z > RealScalar(0)) derived() /= numext::sqrt(z) * w;
160 }
161 
162 //---------- implementation of other norms ----------
163 
164 namespace internal {
165 
166 template <typename Derived, int p>
171  return pow(m.cwiseAbs().array().pow(p).sum(), RealScalar(1) / p);
172  }
173 };
174 
175 template <typename Derived>
176 struct lpNorm_selector<Derived, 1> {
178  const MatrixBase<Derived>& m) {
179  return m.cwiseAbs().sum();
180  }
181 };
182 
183 template <typename Derived>
184 struct lpNorm_selector<Derived, 2> {
186  const MatrixBase<Derived>& m) {
187  return m.norm();
188  }
189 };
190 
191 template <typename Derived>
192 struct lpNorm_selector<Derived, Infinity> {
195  if (Derived::SizeAtCompileTime == 0 || (Derived::SizeAtCompileTime == Dynamic && m.size() == 0))
196  return RealScalar(0);
197  return m.cwiseAbs().maxCoeff();
198  }
199 };
200 
201 } // end namespace internal
202 
217 template <typename Derived>
218 template <int p>
219 #ifndef EIGEN_PARSED_BY_DOXYGEN
221 #else
223 #endif
226 }
227 
228 //---------- implementation of isOrthogonal / isUnitary ----------
229 
236 template <typename Derived>
237 template <typename OtherDerived>
239  typename internal::nested_eval<Derived, 2>::type nested(derived());
240  typename internal::nested_eval<OtherDerived, 2>::type otherNested(other.derived());
241  return numext::abs2(nested.dot(otherNested)) <= prec * prec * nested.squaredNorm() * otherNested.squaredNorm();
242 }
243 
255 template <typename Derived>
257  typename internal::nested_eval<Derived, 1>::type self(derived());
258  for (Index i = 0; i < cols(); ++i) {
259  if (!internal::isApprox(self.col(i).squaredNorm(), static_cast<RealScalar>(1), prec)) return false;
260  for (Index j = 0; j < i; ++j)
261  if (!internal::isMuchSmallerThan(self.col(i).dot(self.col(j)), static_cast<Scalar>(1), prec)) return false;
262  }
263  return true;
264 }
265 
266 } // end namespace Eigen
267 
268 #endif // EIGEN_DOT_H
AnnoyingScalar imag(const AnnoyingScalar &)
Definition: AnnoyingScalar.h:132
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
#define EIGEN_USING_STD(FUNC)
Definition: Macros.h:1090
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
m col(1)
RowVector3d w
Definition: Matrix_resize_int.cpp:3
float * p
Definition: Tutorial_Map_using.cpp:9
int cols
Definition: Tutorial_commainit_02.cpp:1
SCALAR Scalar
Definition: bench_gemm.cpp:45
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
boost::multiprecision::number< boost::multiprecision::cpp_dec_float< 100 >, boost::multiprecision::et_on > Real
Definition: boostmultiprec.cpp:77
NumTraits< Scalar >::Real RealScalar
Definition: DenseBase.h:69
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:62
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
EIGEN_DEVICE_FUNC RealScalar squaredNorm() const
Definition: Dot.h:66
EIGEN_DEVICE_FUNC ScalarBinaryOpTraits< typename internal::traits< Derived >::Scalar, typename internal::traits< OtherDerived >::Scalar >::ReturnType dot(const MatrixBase< OtherDerived > &other) const
EIGEN_DEVICE_FUNC RealScalar lpNorm() const
EIGEN_DEVICE_FUNC void stableNormalize()
Definition: Dot.h:156
EIGEN_DEVICE_FUNC RealScalar norm() const
Definition: Dot.h:78
NumTraits< Scalar >::Real RealScalar
Definition: MatrixBase.h:60
EIGEN_DEVICE_FUNC void normalize()
Definition: Dot.h:113
bool isUnitary(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: Dot.h:256
Base::PlainObject PlainObject
Definition: MatrixBase.h:104
EIGEN_DEVICE_FUNC const PlainObject normalized() const
Definition: Dot.h:92
EIGEN_DEVICE_FUNC const PlainObject stableNormalized() const
Definition: Dot.h:133
bool isOrthogonal(const MatrixBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: Dot.h:238
float real
Definition: datatypes.h:10
Scalar EIGEN_BLAS_FUNC_NAME() dot(int *n, Scalar *px, int *incx, Scalar *py, int *incy)
Definition: level1_real_impl.h:52
const Scalar * a
Definition: level2_cplx_impl.h:32
int * m
Definition: level2_cplx_impl.h:294
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 pow(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:625
EIGEN_DEVICE_FUNC bool isApprox(const Scalar &x, const Scalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: MathFunctions.h:1923
EIGEN_DEVICE_FUNC bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: MathFunctions.h:1916
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float sqrt(const float &x)
Definition: arch/SSE/MathFunctions.h:69
EIGEN_DEVICE_FUNC bool abs2(bool x)
Definition: MathFunctions.h:1102
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
const int Infinity
Definition: Constants.h:39
const int Dynamic
Definition: Constants.h:25
Definition: Eigen_Colamd.h:49
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type run(const MatrixBase< Lhs > &a, const MatrixBase< Rhs > &b)
Definition: InnerProduct.h:238
static EIGEN_DEVICE_FUNC NumTraits< typename traits< Derived >::Scalar >::Real run(const MatrixBase< Derived > &m)
Definition: Dot.h:177
static EIGEN_DEVICE_FUNC NumTraits< typename traits< Derived >::Scalar >::Real run(const MatrixBase< Derived > &m)
Definition: Dot.h:185
NumTraits< typename traits< Derived >::Scalar >::Real RealScalar
Definition: Dot.h:193
static EIGEN_DEVICE_FUNC RealScalar run(const MatrixBase< Derived > &m)
Definition: Dot.h:169
NumTraits< typename traits< Derived >::Scalar >::Real RealScalar
Definition: Dot.h:168
std::conditional_t< Evaluate, PlainObject, typename ref_selector< T >::type > type
Definition: XprHelper.h:549
Definition: functors/UnaryFunctors.h:107
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool run(const Derived &a)
Definition: Dot.h:31
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Real run(const Derived &a)
Definition: Dot.h:23
typename NumTraits< Scalar >::Real Real
Definition: Dot.h:22
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2