Medial Code Documentation
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
13namespace Eigen {
14
15namespace 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.
20template<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{
31 typedef typename scalar_product_traits<typename traits<T>::Scalar,typename traits<U>::Scalar>::ReturnType ResScalar;
33 static inline ResScalar run(const MatrixBase<T>& a, const MatrixBase<U>& b)
34 {
36 }
37};
38
39template<typename T, typename U>
40struct dot_nocheck<T, U, true>
41{
42 typedef typename scalar_product_traits<typename traits<T>::Scalar,typename traits<U>::Scalar>::ReturnType ResScalar;
44 static inline ResScalar run(const MatrixBase<T>& a, const MatrixBase<U>& b)
45 {
46 return a.transpose().template binaryExpr<scalar_conj_product_op<typename traits<T>::Scalar,typename traits<U>::Scalar> >(b).sum();
47 }
48};
49
50} // end namespace internal
51
62template<typename Derived>
63template<typename OtherDerived>
67{
68 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
69 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
70 EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(Derived,OtherDerived)
72 EIGEN_CHECK_BINARY_COMPATIBILIY(func,Scalar,typename OtherDerived::Scalar);
73
74 eigen_assert(size() == other.size());
75
77}
78
79//---------- implementation of L2 norm and related functions ----------
80
87template<typename Derived>
89{
90 return numext::real((*this).cwiseAbs2().sum());
91}
92
99template<typename Derived>
101{
103 return sqrt(squaredNorm());
104}
105
112template<typename Derived>
113inline const typename MatrixBase<Derived>::PlainObject
115{
117 _Nested n(derived());
118 return n / n.norm();
119}
120
127template<typename Derived>
129{
130 *this /= norm();
131}
132
133//---------- implementation of other norms ----------
134
135namespace internal {
136
137template<typename Derived, int p>
139{
140 typedef typename NumTraits<typename traits<Derived>::Scalar>::Real RealScalar;
142 static inline RealScalar run(const MatrixBase<Derived>& m)
143 {
145 return pow(m.cwiseAbs().array().pow(p).sum(), RealScalar(1)/p);
146 }
147};
148
149template<typename Derived>
150struct lpNorm_selector<Derived, 1>
151{
153 static inline typename NumTraits<typename traits<Derived>::Scalar>::Real run(const MatrixBase<Derived>& m)
154 {
155 return m.cwiseAbs().sum();
156 }
157};
158
159template<typename Derived>
160struct lpNorm_selector<Derived, 2>
161{
163 static inline typename NumTraits<typename traits<Derived>::Scalar>::Real run(const MatrixBase<Derived>& m)
164 {
165 return m.norm();
166 }
167};
168
169template<typename Derived>
170struct lpNorm_selector<Derived, Infinity>
171{
173 static inline typename NumTraits<typename traits<Derived>::Scalar>::Real run(const MatrixBase<Derived>& m)
174 {
175 return m.cwiseAbs().maxCoeff();
176 }
177};
178
179} // end namespace internal
180
189template<typename Derived>
190template<int p>
196
197//---------- implementation of isOrthogonal / isUnitary ----------
198
205template<typename Derived>
206template<typename OtherDerived>
208(const MatrixBase<OtherDerived>& other, const RealScalar& prec) const
209{
212 return numext::abs2(nested.dot(otherNested)) <= prec * prec * nested.squaredNorm() * otherNested.squaredNorm();
213}
214
226template<typename Derived>
227bool MatrixBase<Derived>::isUnitary(const RealScalar& prec) const
228{
229 typename internal::nested_eval<Derived,1>::type self(derived());
230 for(Index i = 0; i < cols(); ++i)
231 {
232 if(!internal::isApprox(self.col(i).squaredNorm(), static_cast<RealScalar>(1), prec))
233 return false;
234 for(Index j = 0; j < i; ++j)
235 if(!internal::isMuchSmallerThan(self.col(i).dot(self.col(j)), static_cast<Scalar>(1), prec))
236 return false;
237 }
238 return true;
239}
240
241} // end namespace Eigen
242
243#endif // EIGEN_DOT_H
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
Pseudo expression representing a solving operation.
Definition Solve.h:63
Definition Dot.h:30
Definition BinaryFunctors.h:87
Definition ForwardDeclarations.h:17
Definition Meta.h:30