OrthoMethods.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) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_ORTHOMETHODS_H
12 #define EIGEN_ORTHOMETHODS_H
13 
14 // IWYU pragma: private
15 #include "./InternalHeaderCheck.h"
16 
17 namespace Eigen {
18 
19 namespace internal {
20 
21 // Vector3 version (default)
22 template <typename Derived, typename OtherDerived, int Size>
23 struct cross_impl {
27 
29  const MatrixBase<OtherDerived>& second) {
32 
33  // Note that there is no need for an expression here since the compiler
34  // optimize such a small temporary very well (even within a complex expression)
35  typename internal::nested_eval<Derived, 2>::type lhs(first.derived());
36  typename internal::nested_eval<OtherDerived, 2>::type rhs(second.derived());
37  return return_type(numext::conj(lhs.coeff(1) * rhs.coeff(2) - lhs.coeff(2) * rhs.coeff(1)),
38  numext::conj(lhs.coeff(2) * rhs.coeff(0) - lhs.coeff(0) * rhs.coeff(2)),
39  numext::conj(lhs.coeff(0) * rhs.coeff(1) - lhs.coeff(1) * rhs.coeff(0)));
40  }
41 };
42 
43 // Vector2 version
44 template <typename Derived, typename OtherDerived>
45 struct cross_impl<Derived, OtherDerived, 2> {
49 
51  const MatrixBase<OtherDerived>& second) {
54  typename internal::nested_eval<Derived, 2>::type lhs(first.derived());
55  typename internal::nested_eval<OtherDerived, 2>::type rhs(second.derived());
56  return numext::conj(lhs.coeff(0) * rhs.coeff(1) - lhs.coeff(1) * rhs.coeff(0));
57  }
58 };
59 
60 } // end namespace internal
61 
88 template <typename Derived>
89 template <typename OtherDerived>
91 #ifndef EIGEN_PARSED_BY_DOXYGEN
93 #else
94  inline std::conditional_t<SizeAtCompileTime == 2, Scalar, PlainObject>
95 #endif
98 }
99 
100 namespace internal {
101 
102 template <int Arch, typename VectorLhs, typename VectorRhs, typename Scalar = typename VectorLhs::Scalar,
103  bool Vectorizable = bool((evaluator<VectorLhs>::Flags & evaluator<VectorRhs>::Flags) & PacketAccessBit)>
104 struct cross3_impl {
105  EIGEN_DEVICE_FUNC static inline typename internal::plain_matrix_type<VectorLhs>::type run(const VectorLhs& lhs,
106  const VectorRhs& rhs) {
108  numext::conj(lhs.coeff(1) * rhs.coeff(2) - lhs.coeff(2) * rhs.coeff(1)),
109  numext::conj(lhs.coeff(2) * rhs.coeff(0) - lhs.coeff(0) * rhs.coeff(2)),
110  numext::conj(lhs.coeff(0) * rhs.coeff(1) - lhs.coeff(1) * rhs.coeff(0)), 0);
111  }
112 };
113 
114 } // namespace internal
115 
125 template <typename Derived>
126 template <typename OtherDerived>
128  const MatrixBase<OtherDerived>& other) const {
131 
132  typedef typename internal::nested_eval<Derived, 2>::type DerivedNested;
133  typedef typename internal::nested_eval<OtherDerived, 2>::type OtherDerivedNested;
134  DerivedNested lhs(derived());
135  OtherDerivedNested rhs(other.derived());
136 
139 }
140 
150 template <typename ExpressionType, int Direction>
151 template <typename OtherDerived>
157  YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
158 
159  typename internal::nested_eval<ExpressionType, 2>::type mat(_expression());
160  typename internal::nested_eval<OtherDerived, 2>::type vec(other.derived());
161 
162  CrossReturnType res(_expression().rows(), _expression().cols());
163  if (Direction == Vertical) {
164  eigen_assert(CrossReturnType::RowsAtCompileTime == 3 && "the matrix must have exactly 3 rows");
165  res.row(0) = (mat.row(1) * vec.coeff(2) - mat.row(2) * vec.coeff(1)).conjugate();
166  res.row(1) = (mat.row(2) * vec.coeff(0) - mat.row(0) * vec.coeff(2)).conjugate();
167  res.row(2) = (mat.row(0) * vec.coeff(1) - mat.row(1) * vec.coeff(0)).conjugate();
168  } else {
169  eigen_assert(CrossReturnType::ColsAtCompileTime == 3 && "the matrix must have exactly 3 columns");
170  res.col(0) = (mat.col(1) * vec.coeff(2) - mat.col(2) * vec.coeff(1)).conjugate();
171  res.col(1) = (mat.col(2) * vec.coeff(0) - mat.col(0) * vec.coeff(2)).conjugate();
172  res.col(2) = (mat.col(0) * vec.coeff(1) - mat.col(1) * vec.coeff(0)).conjugate();
173  }
174  return res;
175 }
176 
177 namespace internal {
178 
179 template <typename Derived, int Size = Derived::SizeAtCompileTime>
182  typedef typename traits<Derived>::Scalar Scalar;
185  EIGEN_DEVICE_FUNC static inline VectorType run(const Derived& src) {
186  VectorType perp = VectorType::Zero(src.size());
187  Index maxi = 0;
188  Index sndi = 0;
189  src.cwiseAbs().maxCoeff(&maxi);
190  if (maxi == 0) sndi = 1;
191  RealScalar invnm = RealScalar(1) / (Vector2() << src.coeff(sndi), src.coeff(maxi)).finished().norm();
192  perp.coeffRef(maxi) = -numext::conj(src.coeff(sndi)) * invnm;
193  perp.coeffRef(sndi) = numext::conj(src.coeff(maxi)) * invnm;
194 
195  return perp;
196  }
197 };
198 
199 template <typename Derived>
200 struct unitOrthogonal_selector<Derived, 3> {
202  typedef typename traits<Derived>::Scalar Scalar;
204  EIGEN_DEVICE_FUNC static inline VectorType run(const Derived& src) {
205  VectorType perp;
206  /* Let us compute the crossed product of *this with a vector
207  * that is not too close to being colinear to *this.
208  */
209 
210  /* unless the x and y coords are both close to zero, we can
211  * simply take ( -y, x, 0 ) and normalize it.
212  */
213  if ((!isMuchSmallerThan(src.x(), src.z())) || (!isMuchSmallerThan(src.y(), src.z()))) {
214  RealScalar invnm = RealScalar(1) / src.template head<2>().norm();
215  perp.coeffRef(0) = -numext::conj(src.y()) * invnm;
216  perp.coeffRef(1) = numext::conj(src.x()) * invnm;
217  perp.coeffRef(2) = 0;
218  }
219  /* if both x and y are close to zero, then the vector is close
220  * to the z-axis, so it's far from colinear to the x-axis for instance.
221  * So we take the crossed product with (1,0,0) and normalize it.
222  */
223  else {
224  RealScalar invnm = RealScalar(1) / src.template tail<2>().norm();
225  perp.coeffRef(0) = 0;
226  perp.coeffRef(1) = -numext::conj(src.z()) * invnm;
227  perp.coeffRef(2) = numext::conj(src.y()) * invnm;
228  }
229 
230  return perp;
231  }
232 };
233 
234 template <typename Derived>
235 struct unitOrthogonal_selector<Derived, 2> {
237  EIGEN_DEVICE_FUNC static inline VectorType run(const Derived& src) {
238  return VectorType(-numext::conj(src.y()), numext::conj(src.x())).normalized();
239  }
240 };
241 
242 } // end namespace internal
243 
253 template <typename Derived>
257 }
258 
259 } // end namespace Eigen
260 
261 #endif // EIGEN_ORTHOMETHODS_H
AnnoyingScalar conj(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:133
Eigen::SparseMatrix< double > mat
Definition: EigenUnitTest.cpp:10
Direction
An enum that indicates the direction in Cartesian coordinates.
Definition: GeneralDefine.h:56
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define eigen_assert(x)
Definition: Macros.h:910
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
v tail< 2 >().setZero()
v head< 2 >().setZero()
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
#define EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(TYPE, SIZE)
Definition: StaticAssert.h:50
#define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE)
Definition: StaticAssert.h:36
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
SCALAR Scalar
Definition: bench_gemm.cpp:45
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
EIGEN_DEVICE_FUNC internal::cross_impl< Derived, OtherDerived >::return_type cross(const MatrixBase< OtherDerived > &other) const
Base::PlainObject PlainObject
Definition: MatrixBase.h:104
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
ExpressionType::PlainObject CrossReturnType
Definition: VectorwiseOp.h:653
EIGEN_DEVICE_FUNC PlainObject unitOrthogonal(void) const
Definition: OrthoMethods.h:254
EIGEN_DEVICE_FUNC const CrossReturnType cross(const MatrixBase< OtherDerived > &other) const
Definition: OrthoMethods.h:153
EIGEN_DEVICE_FUNC PlainObject cross3(const MatrixBase< OtherDerived > &other) const
Definition: OrthoMethods.h:127
@ Vertical
Definition: Constants.h:266
const unsigned int PacketAccessBit
Definition: Constants.h:97
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
typename remove_all< T >::type remove_all_t
Definition: Meta.h:142
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T maxi(const T &x, const T &y)
Definition: MathFunctions.h:926
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
auto run(Kernel kernel, Args &&... args) -> decltype(kernel(args...))
Definition: gpu_test_helper.h:414
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
bool finished
Definition: MergeRestartFiles.py:79
type
Definition: compute_granudrum_aor.py:141
Definition: Eigen_Colamd.h:49
double Zero
Definition: pseudosolid_node_update_elements.cc:35
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
Determines whether the given binary operation of two numeric types is allowed and what the scalar ret...
Definition: XprHelper.h:1043
Definition: OrthoMethods.h:104
static EIGEN_DEVICE_FUNC internal::plain_matrix_type< VectorLhs >::type run(const VectorLhs &lhs, const VectorRhs &rhs)
Definition: OrthoMethods.h:105
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE return_type run(const MatrixBase< Derived > &first, const MatrixBase< OtherDerived > &second)
Definition: OrthoMethods.h:50
Scalar return_type
Definition: OrthoMethods.h:48
ScalarBinaryOpTraits< typename internal::traits< Derived >::Scalar, typename internal::traits< OtherDerived >::Scalar >::ReturnType Scalar
Definition: OrthoMethods.h:47
Definition: OrthoMethods.h:23
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE return_type run(const MatrixBase< Derived > &first, const MatrixBase< OtherDerived > &second)
Definition: OrthoMethods.h:28
ScalarBinaryOpTraits< typename internal::traits< Derived >::Scalar, typename internal::traits< OtherDerived >::Scalar >::ReturnType Scalar
Definition: OrthoMethods.h:25
Matrix< Scalar, MatrixBase< Derived >::RowsAtCompileTime, MatrixBase< Derived >::ColsAtCompileTime > return_type
Definition: OrthoMethods.h:26
Definition: Meta.h:205
std::conditional_t< Evaluate, PlainObject, typename ref_selector< T >::type > type
Definition: XprHelper.h:549
Definition: XprHelper.h:389
Definition: ForwardDeclarations.h:21
static EIGEN_DEVICE_FUNC VectorType run(const Derived &src)
Definition: OrthoMethods.h:237
plain_matrix_type< Derived >::type VectorType
Definition: OrthoMethods.h:236
NumTraits< Scalar >::Real RealScalar
Definition: OrthoMethods.h:203
plain_matrix_type< Derived >::type VectorType
Definition: OrthoMethods.h:201
traits< Derived >::Scalar Scalar
Definition: OrthoMethods.h:202
static EIGEN_DEVICE_FUNC VectorType run(const Derived &src)
Definition: OrthoMethods.h:204
Definition: OrthoMethods.h:180
traits< Derived >::Scalar Scalar
Definition: OrthoMethods.h:182
NumTraits< Scalar >::Real RealScalar
Definition: OrthoMethods.h:183
Matrix< Scalar, 2, 1 > Vector2
Definition: OrthoMethods.h:184
plain_matrix_type< Derived >::type VectorType
Definition: OrthoMethods.h:181
static EIGEN_DEVICE_FUNC VectorType run(const Derived &src)
Definition: OrthoMethods.h:185
Definition: fft_test_shared.h:66