Eigen  5.0.1-dev+60122df6
 
Loading...
Searching...
No Matches
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
16namespace Eigen {
17
18namespace internal {
19
20template <typename Derived, typename Scalar = typename traits<Derived>::Scalar>
21struct 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
29template <typename Derived>
30struct 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
47template <typename Derived>
48template <typename OtherDerived>
49EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
51 typename internal::traits<OtherDerived>::Scalar>::ReturnType
52 MatrixBase<Derived>::dot(const MatrixBase<OtherDerived>& other) const {
53 return internal::dot_impl<Derived, OtherDerived>::run(derived(), other.derived());
54}
55
56//---------- implementation of L2 norm and related functions ----------
57
64template <typename Derived>
65EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename NumTraits<typename internal::traits<Derived>::Scalar>::Real
67 return internal::squared_norm_impl<Derived>::run(derived());
68}
69
76template <typename Derived>
77EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename NumTraits<typename internal::traits<Derived>::Scalar>::Real
79 return numext::sqrt(squaredNorm());
80}
81
91template <typename Derived>
92EIGEN_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
112template <typename Derived>
113EIGEN_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
131template <typename Derived>
132EIGEN_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
155template <typename Derived>
156EIGEN_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
164namespace internal {
165
166template <typename Derived, int p>
167struct 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
175template <typename Derived>
176struct 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
183template <typename Derived>
184struct 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();
189};
190
191template <typename Derived>
192struct lpNorm_selector<Derived, Infinity> {
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
217template <typename Derived>
218template <int p>
219#ifndef EIGEN_PARSED_BY_DOXYGEN
220EIGEN_DEVICE_FUNC inline typename NumTraits<typename internal::traits<Derived>::Scalar>::Real
221#else
222EIGEN_DEVICE_FUNC MatrixBase<Derived>::RealScalar
223#endif
225 return internal::lpNorm_selector<Derived, p>::run(*this);
226}
227
228//---------- implementation of isOrthogonal / isUnitary ----------
229
236template <typename Derived>
237template <typename OtherDerived>
238bool 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
255template <typename Derived>
256bool 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
const GlobalUnaryPowReturnType< Derived, ScalarExponent > pow(const Eigen::ArrayBase< Derived > &x, const ScalarExponent &exponent)
internal::traits< Derived >::Scalar Scalar
Definition DenseBase.h:62
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:52
void stableNormalize()
Definition Dot.h:156
const PlainObject stableNormalized() const
Definition Dot.h:133
const PlainObject normalized() const
Definition Dot.h:92
RealScalar lpNorm() const
Definition Dot.h:224
ScalarBinaryOpTraits< typenameinternal::traits< Derived >::Scalar, typenameinternal::traits< OtherDerived >::Scalar >::ReturnType dot(const MatrixBase< OtherDerived > &other) const
Definition Dot.h:52
bool isUnitary(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition Dot.h:256
RealScalar squaredNorm() const
Definition Dot.h:66
void normalize()
Definition Dot.h:113
RealScalar norm() const
Definition Dot.h:78
const CwiseAbsReturnType cwiseAbs() const
Definition MatrixBase.h:34
bool isOrthogonal(const MatrixBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition Dot.h:238
Namespace containing all symbols from the Eigen library.
Definition B01_Experimental.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:82
const int Infinity
Definition Constants.h:39
const int Dynamic
Definition Constants.h:25
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition NumTraits.h:232
Determines whether the given binary operation of two numeric types is allowed and what the scalar ret...
Definition XprHelper.h:1047