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 {
35  static ResScalar run(const MatrixBase<T>& a, const MatrixBase<U>& b)
36  {
37  return a.template binaryExpr<conj_prod>(b).sum();
38  }
39 };
40 
41 template<typename T, typename U>
42 struct dot_nocheck<T, U, true>
43 {
48  static ResScalar run(const MatrixBase<T>& a, const MatrixBase<U>& b)
49  {
50  return a.transpose().template binaryExpr<conj_prod>(b).sum();
51  }
52 };
53 
54 } // end namespace internal
55 
67 template<typename Derived>
68 template<typename OtherDerived>
73 {
76  EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(Derived,OtherDerived)
77 #if !(defined(EIGEN_NO_STATIC_ASSERT) && defined(EIGEN_NO_DEBUG))
80 #endif
81 
82  eigen_assert(size() == other.size());
83 
85 }
86 
87 //---------- implementation of L2 norm and related functions ----------
88 
95 template<typename Derived>
97 {
98  return numext::real((*this).cwiseAbs2().sum());
99 }
100 
107 template<typename Derived>
109 {
110  return numext::sqrt(squaredNorm());
111 }
112 
122 template<typename Derived>
125 {
126  typedef typename internal::nested_eval<Derived,2>::type _Nested;
127  _Nested n(derived());
128  RealScalar z = n.squaredNorm();
129  // NOTE: after extensive benchmarking, this conditional does not impact performance, at least on recent x86 CPU
130  if(z>RealScalar(0))
131  return n / numext::sqrt(z);
132  else
133  return n;
134 }
135 
144 template<typename Derived>
146 {
147  RealScalar z = squaredNorm();
148  // NOTE: after extensive benchmarking, this conditional does not impact performance, at least on recent x86 CPU
149  if(z>RealScalar(0))
150  derived() /= numext::sqrt(z);
151 }
152 
165 template<typename Derived>
168 {
169  typedef typename internal::nested_eval<Derived,3>::type _Nested;
170  _Nested n(derived());
171  RealScalar w = n.cwiseAbs().maxCoeff();
172  RealScalar z = (n/w).squaredNorm();
173  if(z>RealScalar(0))
174  return n / (numext::sqrt(z)*w);
175  else
176  return n;
177 }
178 
190 template<typename Derived>
192 {
193  RealScalar w = cwiseAbs().maxCoeff();
194  RealScalar z = (derived()/w).squaredNorm();
195  if(z>RealScalar(0))
196  derived() /= numext::sqrt(z)*w;
197 }
198 
199 //---------- implementation of other norms ----------
200 
201 namespace internal {
202 
203 template<typename Derived, int p>
205 {
208  static inline RealScalar run(const MatrixBase<Derived>& m)
209  {
211  return pow(m.cwiseAbs().array().pow(p).sum(), RealScalar(1)/p);
212  }
213 };
214 
215 template<typename Derived>
216 struct lpNorm_selector<Derived, 1>
217 {
220  {
221  return m.cwiseAbs().sum();
222  }
223 };
224 
225 template<typename Derived>
226 struct lpNorm_selector<Derived, 2>
227 {
230  {
231  return m.norm();
232  }
233 };
234 
235 template<typename Derived>
236 struct lpNorm_selector<Derived, Infinity>
237 {
240  static inline RealScalar run(const MatrixBase<Derived>& m)
241  {
242  if(Derived::SizeAtCompileTime==0 || (Derived::SizeAtCompileTime==Dynamic && m.size()==0))
243  return RealScalar(0);
244  return m.cwiseAbs().maxCoeff();
245  }
246 };
247 
248 } // end namespace internal
249 
260 template<typename Derived>
261 template<int p>
262 #ifndef EIGEN_PARSED_BY_DOXYGEN
264 #else
266 #endif
268 {
270 }
271 
272 //---------- implementation of isOrthogonal / isUnitary ----------
273 
280 template<typename Derived>
281 template<typename OtherDerived>
283 (const MatrixBase<OtherDerived>& other, const RealScalar& prec) const
284 {
285  typename internal::nested_eval<Derived,2>::type nested(derived());
286  typename internal::nested_eval<OtherDerived,2>::type otherNested(other.derived());
287  return numext::abs2(nested.dot(otherNested)) <= prec * prec * nested.squaredNorm() * otherNested.squaredNorm();
288 }
289 
301 template<typename Derived>
303 {
304  typename internal::nested_eval<Derived,1>::type self(derived());
305  for(Index i = 0; i < cols(); ++i)
306  {
307  if(!internal::isApprox(self.col(i).squaredNorm(), static_cast<RealScalar>(1), prec))
308  return false;
309  for(Index j = 0; j < i; ++j)
310  if(!internal::isMuchSmallerThan(self.col(i).dot(self.col(j)), static_cast<Scalar>(1), prec))
311  return false;
312  }
313  return true;
314 }
315 
316 } // end namespace Eigen
317 
318 #endif // EIGEN_DOT_H
w
RowVector3d w
Definition: Matrix_resize_int.cpp:3
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
Eigen::internal::lpNorm_selector
Definition: Dot.h:204
Eigen::internal::dot_nocheck::run
EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE ResScalar run(const MatrixBase< T > &a, const MatrixBase< U > &b)
Definition: Dot.h:35
EIGEN_DEVICE_FUNC
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
EIGEN_CHECK_BINARY_COMPATIBILIY
#define EIGEN_CHECK_BINARY_COMPATIBILIY(BINOP, LHS, RHS)
Definition: XprHelper.h:850
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
col
m col(1)
Eigen::MatrixBase::isOrthogonal
bool isOrthogonal(const MatrixBase< OtherDerived > &other, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: Dot.h:283
EIGEN_USING_STD
#define EIGEN_USING_STD(FUNC)
Definition: Macros.h:1185
Eigen::internal::dot_nocheck< T, U, true >::ResScalar
conj_prod::result_type ResScalar
Definition: Dot.h:45
Eigen::DenseBase< Solve< Decomposition, RhsType > >::Scalar
internal::traits< Solve< Decomposition, RhsType > >::Scalar Scalar
Definition: DenseBase.h:66
Eigen::MatrixBase::normalized
const EIGEN_DEVICE_FUNC PlainObject normalized() const
Definition: Dot.h:124
b
Scalar * b
Definition: benchVecAdd.cpp:17
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:1037
Eigen::internal::lpNorm_selector< Derived, Infinity >::RealScalar
NumTraits< typename traits< Derived >::Scalar >::Real RealScalar
Definition: Dot.h:238
Eigen::internal::dot_nocheck< T, U, true >::run
EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE ResScalar run(const MatrixBase< T > &a, const MatrixBase< U > &b)
Definition: Dot.h:48
Eigen::internal::lpNorm_selector< Derived, 2 >::run
static EIGEN_DEVICE_FUNC NumTraits< typename traits< Derived >::Scalar >::Real run(const MatrixBase< Derived > &m)
Definition: Dot.h:229
Eigen::MatrixBase::lpNorm
EIGEN_DEVICE_FUNC RealScalar lpNorm() const
real
float real
Definition: datatypes.h:10
T
Eigen::Triplet< double > T
Definition: Tutorial_sparse_example.cpp:6
Eigen::internal::isApprox
EIGEN_DEVICE_FUNC bool isApprox(const Scalar &x, const Scalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: Eigen/src/Core/MathFunctions.h:1947
Eigen::internal::lpNorm_selector::RealScalar
NumTraits< typename traits< Derived >::Scalar >::Real RealScalar
Definition: Dot.h:206
EIGEN_STATIC_ASSERT_VECTOR_ONLY
#define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE)
Definition: StaticAssert.h:142
Eigen::internal::isMuchSmallerThan
EIGEN_DEVICE_FUNC bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: Eigen/src/Core/MathFunctions.h:1940
Eigen::internal::dot_nocheck< T, U, true >::conj_prod
scalar_conj_product_op< typename traits< T >::Scalar, typename traits< U >::Scalar > conj_prod
Definition: Dot.h:44
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
dot
Scalar EIGEN_BLAS_FUNC() dot(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_real_impl.h:49
n
int n
Definition: BiCGSTAB_simple.cpp:1
Eigen::internal::true_type
Definition: Meta.h:96
Eigen::DenseBase< Solve< Decomposition, RhsType > >::RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: DenseBase.h:73
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
Eigen::MatrixBase::normalize
EIGEN_DEVICE_FUNC void normalize()
Definition: Dot.h:145
Eigen::internal::dot_nocheck::ResScalar
conj_prod::result_type ResScalar
Definition: Dot.h:32
Eigen::internal::scalar_conj_product_op::result_type
ScalarBinaryOpTraits< LhsScalar, RhsScalar, scalar_conj_product_op >::ReturnType result_type
Definition: BinaryFunctors.h:114
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:22
func
int func(const int &a)
Definition: testDSF.cpp:221
EIGEN_STRONG_INLINE
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
Eigen::MatrixBase::stableNormalized
const EIGEN_DEVICE_FUNC PlainObject stableNormalized() const
Definition: Dot.h:167
pybind_wrapper_test_script.z
z
Definition: pybind_wrapper_test_script.py:61
Eigen::MatrixBase::isUnitary
bool isUnitary(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: Dot.h:302
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
Eigen::Triplet< double >
Eigen::internal::dot_nocheck::conj_prod
scalar_conj_product_op< typename traits< T >::Scalar, typename traits< U >::Scalar > conj_prod
Definition: Dot.h:31
EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE
#define EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(TYPE0, TYPE1)
Definition: StaticAssert.h:167
ceres::pow
Jet< T, N > pow(const Jet< T, N > &f, double g)
Definition: jet.h:570
Eigen::MatrixBase::RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: MatrixBase.h:58
Eigen::MatrixBase::PlainObject
Base::PlainObject PlainObject
Definition: MatrixBase.h:104
RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:47
Eigen::MatrixBase::dot
EIGEN_DEVICE_FUNC ScalarBinaryOpTraits< typename internal::traits< Derived >::Scalar, typename internal::traits< OtherDerived >::Scalar >::ReturnType dot(const MatrixBase< OtherDerived > &other) const
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
Eigen::MatrixBase::stableNormalize
EIGEN_DEVICE_FUNC void stableNormalize()
Definition: Dot.h:191
Eigen::numext::abs2
EIGEN_DEVICE_FUNC bool abs2(bool x)
Definition: Eigen/src/Core/MathFunctions.h:1294
Eigen::internal::lpNorm_selector::run
static EIGEN_DEVICE_FUNC RealScalar run(const MatrixBase< Derived > &m)
Definition: Dot.h:208
Eigen::internal::traits
Definition: ForwardDeclarations.h:17
Eigen::internal::lpNorm_selector< Derived, Infinity >::run
static EIGEN_DEVICE_FUNC RealScalar run(const MatrixBase< Derived > &m)
Definition: Dot.h:240
p
float * p
Definition: Tutorial_Map_using.cpp:9
Real
mp::number< mp::cpp_dec_float< 100 >, mp::et_on > Real
Definition: boostmultiprec.cpp:78
Eigen::internal::lpNorm_selector< Derived, 1 >::run
static EIGEN_DEVICE_FUNC NumTraits< typename traits< Derived >::Scalar >::Real run(const MatrixBase< Derived > &m)
Definition: Dot.h:219
U
@ U
Definition: testDecisionTree.cpp:349
internal
Definition: BandTriangularSolver.h:13
Eigen::MatrixBase
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Eigen::internal::dot_nocheck
Definition: Dot.h:29
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
Eigen::MatrixBase::norm
EIGEN_DEVICE_FUNC RealScalar norm() const
Definition: Dot.h:108
func
Definition: benchGeometry.cpp:23
Eigen::numext::sqrt
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE float sqrt(const float &x)
Definition: Eigen/src/Core/arch/SSE/MathFunctions.h:177
Eigen::Infinity
const int Infinity
Definition: Constants.h:36
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Eigen::internal::scalar_conj_product_op
Definition: BinaryFunctors.h:107
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
cwiseAbs
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE CwiseAbsReturnType cwiseAbs() const
Definition: MatrixCwiseUnaryOps.h:33
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Eigen::MatrixBase::squaredNorm
EIGEN_DEVICE_FUNC RealScalar squaredNorm() const
Definition: Dot.h:96


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:14