$darkmode
Eigen  5.0.1-dev
Dot.h
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 
20 template <typename Derived, typename Scalar = typename traits<Derived>::Scalar>
21 struct squared_norm_impl {
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>
49 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
50  typename ScalarBinaryOpTraits<typename internal::traits<Derived>::Scalar,
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>
65 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename NumTraits<typename internal::traits<Derived>::Scalar>::Real
67  return internal::squared_norm_impl<Derived>::run(derived());
68 }
69 
76 template <typename Derived>
77 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename NumTraits<typename internal::traits<Derived>::Scalar>::Real
79  return numext::sqrt(squaredNorm());
80 }
81 
91 template <typename Derived>
92 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::PlainObject MatrixBase<Derived>::normalized()
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>
113 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void MatrixBase<Derived>::normalize() {
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>
132 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::PlainObject
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>
156 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void MatrixBase<Derived>::stableNormalize() {
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>
167 struct lpNorm_selector {
168  typedef typename NumTraits<typename traits<Derived>::Scalar>::Real RealScalar;
169  EIGEN_DEVICE_FUNC static inline RealScalar run(const MatrixBase<Derived>& m) {
170  EIGEN_USING_STD(pow)
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> {
177  EIGEN_DEVICE_FUNC static inline typename NumTraits<typename traits<Derived>::Scalar>::Real run(
178  const MatrixBase<Derived>& m) {
179  return m.cwiseAbs().sum();
180  }
181 };
182 
183 template <typename Derived>
184 struct lpNorm_selector<Derived, 2> {
185  EIGEN_DEVICE_FUNC static inline typename NumTraits<typename traits<Derived>::Scalar>::Real run(
186  const MatrixBase<Derived>& m) {
187  return m.norm();
188  }
189 };
190 
191 template <typename Derived>
192 struct lpNorm_selector<Derived, Infinity> {
193  typedef typename NumTraits<typename traits<Derived>::Scalar>::Real RealScalar;
194  EIGEN_DEVICE_FUNC static inline RealScalar run(const MatrixBase<Derived>& m) {
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
220 EIGEN_DEVICE_FUNC inline typename NumTraits<typename internal::traits<Derived>::Scalar>::Real
221 #else
222 EIGEN_DEVICE_FUNC MatrixBase<Derived>::RealScalar
223 #endif
225  return internal::lpNorm_selector<Derived, p>::run(*this);
226 }
227 
228 //---------- implementation of isOrthogonal / isUnitary ----------
229 
236 template <typename Derived>
237 template <typename OtherDerived>
238 bool MatrixBase<Derived>::isOrthogonal(const MatrixBase<OtherDerived>& other, const RealScalar& prec) const {
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>
256 bool MatrixBase<Derived>::isUnitary(const RealScalar& prec) const {
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
constexpr Derived & derived()
Definition: EigenBase.h:49
bool isUnitary(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: Dot.h:256
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
const PlainObject normalized() const
Definition: Dot.h:92
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:43
void normalize()
Definition: Dot.h:113
RealScalar norm() const
Definition: Dot.h:78
void stableNormalize()
Definition: Dot.h:156
RealScalar squaredNorm() const
Definition: Dot.h:66
const PlainObject stableNormalized() const
Definition: Dot.h:133
ScalarBinaryOpTraits< typename internal::traits< Derived >::Scalar, typename internal::traits< OtherDerived >::Scalar >::ReturnType dot(const MatrixBase< OtherDerived > &other) const
Definition: Dot.h:52
RealScalar lpNorm() const
Definition: Dot.h:224
bool isOrthogonal(const MatrixBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: Dot.h:238
const int Dynamic
Definition: Constants.h:25
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
const CwiseAbsReturnType cwiseAbs() const
Definition: MatrixBase.h:34
const int Infinity
Definition: Constants.h:39