Eigen::JacobiRotation< Scalar > Class Template Reference

Rotation given by a cosine-sine pair. More...

Public Types

typedef NumTraits< Scalar >::Real RealScalar
 

Public Member Functions

JacobiRotation adjoint () const
 
Scalar & c ()
 
Scalar c () const
 
 JacobiRotation ()
 
 JacobiRotation (const Scalar &c, const Scalar &s)
 
void makeGivens (const Scalar &p, const Scalar &q, Scalar *r=0)
 
template<typename Derived >
bool makeJacobi (const MatrixBase< Derived > &, Index p, Index q)
 
bool makeJacobi (const RealScalar &x, const Scalar &y, const RealScalar &z)
 
JacobiRotation operator* (const JacobiRotation &other)
 
Scalar & s ()
 
Scalar s () const
 
JacobiRotation transpose () const
 

Protected Member Functions

void makeGivens (const Scalar &p, const Scalar &q, Scalar *r, internal::false_type)
 
void makeGivens (const Scalar &p, const Scalar &q, Scalar *r, internal::true_type)
 

Protected Attributes

Scalar m_c
 
Scalar m_s
 

Detailed Description

template<typename Scalar>
class Eigen::JacobiRotation< Scalar >

Rotation given by a cosine-sine pair.

This is defined in the Jacobi module.

#include <Eigen/Jacobi>

This class represents a Jacobi or Givens rotation. This is a 2D rotation in the plane J of angle \( \theta \) defined by its cosine c and sine s as follow: \( J = \left ( \begin{array}{cc} c & \overline s \\ -s & \overline c \end{array} \right ) \)

You can apply the respective counter-clockwise rotation to a column vector v by applying its adjoint on the left: \( v = J^* v \) that translates to the following Eigen code:

v.applyOnTheLeft(J.adjoint());
Array< int, Dynamic, 1 > v
JacobiRotation< float > J
See also
MatrixBase::applyOnTheLeft(), MatrixBase::applyOnTheRight()

Definition at line 36 of file Jacobi.h.

Member Typedef Documentation

◆ RealScalar

template<typename Scalar >
typedef NumTraits<Scalar>::Real Eigen::JacobiRotation< Scalar >::RealScalar

Definition at line 39 of file Jacobi.h.

Constructor & Destructor Documentation

◆ JacobiRotation() [1/2]

template<typename Scalar >
Eigen::JacobiRotation< Scalar >::JacobiRotation ( )
inline

Default constructor without any initialization.

Definition at line 43 of file Jacobi.h.

43 {}

◆ JacobiRotation() [2/2]

template<typename Scalar >
Eigen::JacobiRotation< Scalar >::JacobiRotation ( const Scalar &  c,
const Scalar &  s 
)
inline

Construct a planar rotation from a cosine-sine pair (c, s).

Definition at line 47 of file Jacobi.h.

47 : m_c(c), m_s(s) {}
Scalar & s()
Definition: Jacobi.h:51
Scalar & c()
Definition: Jacobi.h:49

Member Function Documentation

◆ adjoint()

template<typename Scalar >
JacobiRotation Eigen::JacobiRotation< Scalar >::adjoint ( ) const
inline

Returns the adjoint transformation

Definition at line 69 of file Jacobi.h.

69 { using numext::conj; return JacobiRotation(conj(m_c), -m_s); }
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_conjugate_op< typename Derived::Scalar >, const Derived > conj(const Eigen::ArrayBase< Derived > &x)

◆ c() [1/2]

template<typename Scalar >
Scalar& Eigen::JacobiRotation< Scalar >::c ( )
inline

Definition at line 49 of file Jacobi.h.

49 { return m_c; }

◆ c() [2/2]

template<typename Scalar >
Scalar Eigen::JacobiRotation< Scalar >::c ( ) const
inline

Definition at line 50 of file Jacobi.h.

50 { return m_c; }

◆ makeGivens() [1/3]

template<typename Scalar >
void Eigen::JacobiRotation< Scalar >::makeGivens ( const Scalar &  p,
const Scalar &  q,
Scalar *  r,
internal::false_type   
)
protected

Definition at line 233 of file Jacobi.h.

234 {
235  using std::sqrt;
236  using std::abs;
238  {
239  m_c = p<Scalar(0) ? Scalar(-1) : Scalar(1);
240  m_s = Scalar(0);
241  if(r) *r = abs(p);
242  }
243  else if(numext::is_exactly_zero(p))
244  {
245  m_c = Scalar(0);
246  m_s = q<Scalar(0) ? Scalar(1) : Scalar(-1);
247  if(r) *r = abs(q);
248  }
249  else if(abs(p) > abs(q))
250  {
251  Scalar t = q/p;
252  Scalar u = sqrt(Scalar(1) + numext::abs2(t));
253  if(p<Scalar(0))
254  u = -u;
255  m_c = Scalar(1)/u;
256  m_s = -t * m_c;
257  if(r) *r = p * u;
258  }
259  else
260  {
261  Scalar t = p/q;
262  Scalar u = sqrt(Scalar(1) + numext::abs2(t));
263  if(q<Scalar(0))
264  u = -u;
265  m_s = -Scalar(1)/u;
266  m_c = -t * m_s;
267  if(r) *r = q * u;
268  }
269 
270 }
const AbsReturnType abs() const
const SqrtReturnType sqrt() const
float * p
bool abs2(bool x)
bool is_exactly_zero(const X &x)
Definition: Meta.h:475
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)

◆ makeGivens() [2/3]

template<typename Scalar >
void Eigen::JacobiRotation< Scalar >::makeGivens ( const Scalar &  p,
const Scalar &  q,
Scalar *  r,
internal::true_type   
)
protected

Definition at line 173 of file Jacobi.h.

174 {
175  using std::sqrt;
176  using std::abs;
177  using numext::conj;
178 
179  if(q==Scalar(0))
180  {
181  m_c = numext::real(p)<0 ? Scalar(-1) : Scalar(1);
182  m_s = 0;
183  if(r) *r = m_c * p;
184  }
185  else if(p==Scalar(0))
186  {
187  m_c = 0;
188  m_s = -q/abs(q);
189  if(r) *r = abs(q);
190  }
191  else
192  {
193  RealScalar p1 = numext::norm1(p);
194  RealScalar q1 = numext::norm1(q);
195  if(p1>=q1)
196  {
197  Scalar ps = p / p1;
198  RealScalar p2 = numext::abs2(ps);
199  Scalar qs = q / p1;
200  RealScalar q2 = numext::abs2(qs);
201 
202  RealScalar u = sqrt(RealScalar(1) + q2/p2);
203  if(numext::real(p)<RealScalar(0))
204  u = -u;
205 
206  m_c = Scalar(1)/u;
207  m_s = -qs*conj(ps)*(m_c/p2);
208  if(r) *r = p * u;
209  }
210  else
211  {
212  Scalar ps = p / q1;
213  RealScalar p2 = numext::abs2(ps);
214  Scalar qs = q / q1;
215  RealScalar q2 = numext::abs2(qs);
216 
217  RealScalar u = q1 * sqrt(p2 + q2);
218  if(numext::real(p)<RealScalar(0))
219  u = -u;
220 
221  p1 = abs(p);
222  ps = p/p1;
223  m_c = p1/u;
224  m_s = -conj(ps) * (q/u);
225  if(r) *r = ps * u;
226  }
227  }
228 }
RealReturnType real() const
Vector3f p1
NumTraits< Scalar >::Real RealScalar
Definition: Jacobi.h:39

◆ makeGivens() [3/3]

template<typename Scalar >
void Eigen::JacobiRotation< Scalar >::makeGivens ( const Scalar &  p,
const Scalar &  q,
Scalar *  r = 0 
)

Makes *this as a Givens rotation G such that applying \( G^* \) to the left of the vector \( V = \left ( \begin{array}{c} p \\ q \end{array} \right )\) yields: \( G^* V = \left ( \begin{array}{c} r \\ 0 \end{array} \right )\).

The value of r is returned if r is not null (the default is null). Also note that G is built such that the cosine is always real.

Example:

JacobiRotation<float> G;
G.makeGivens(v.x(), v.y());
cout << "Here is the vector v:" << endl << v << endl;
v.applyOnTheLeft(0, 1, G.adjoint());
cout << "Here is the vector J' * v:" << endl << v << endl;
JacobiRotation< float > G
static const RandomReturnType Random()
Definition: Random.h:114
Matrix< float, 2, 1 > Vector2f
2×1 vector of type float.
Definition: Matrix.h:501

Output:

Here is the vector v:
  0.68
-0.211
Here is the vector J' * v:
0.712
    0

This function implements the continuous Givens rotation generation algorithm found in Anderson (2000), Discontinuous Plane Rotations and the Symmetric Eigenvalue Problem. LAPACK Working Note 150, University of Tennessee, UT-CS-00-454, December 4, 2000.

See also
MatrixBase::applyOnTheLeft(), MatrixBase::applyOnTheRight()

Definition at line 164 of file Jacobi.h.

165 {
166  makeGivens(p, q, r, std::conditional_t<NumTraits<Scalar>::IsComplex, internal::true_type, internal::false_type>());
167 }
void makeGivens(const Scalar &p, const Scalar &q, Scalar *r=0)
Definition: Jacobi.h:164

◆ makeJacobi() [1/2]

template<typename Scalar >
template<typename Derived >
bool Eigen::JacobiRotation< Scalar >::makeJacobi ( const MatrixBase< Derived > &  m,
Index  p,
Index  q 
)
inline

Makes *this as a Jacobi rotation J such that applying J on both the right and left sides of the 2x2 selfadjoint matrix \( B = \left ( \begin{array}{cc} \text{this}_{pp} & \text{this}_{pq} \\ (\text{this}_{pq})^* & \text{this}_{qq} \end{array} \right )\) yields a diagonal matrix \( A = J^* B J \)

Example:

m = (m + m.adjoint()).eval();
JacobiRotation<float> J;
J.makeJacobi(m, 0, 1);
cout << "Here is the matrix m:" << endl << m << endl;
m.applyOnTheLeft(0, 1, J.adjoint());
m.applyOnTheRight(0, 1, J);
cout << "Here is the matrix J' * m * J:" << endl << m << endl;
Matrix3f m
Matrix< float, 2, 2 > Matrix2f
2×2 matrix of type float.
Definition: Matrix.h:501

Output:

Here is the matrix m:
 1.36 0.355
0.355  1.19
Here is the matrix J' * m * J:
 1.64     0
    0 0.913
See also
JacobiRotation::makeJacobi(RealScalar, Scalar, RealScalar), MatrixBase::applyOnTheLeft(), MatrixBase::applyOnTheRight()

Definition at line 141 of file Jacobi.h.

142 {
143  return makeJacobi(numext::real(m.coeff(p,p)), m.coeff(p,q), numext::real(m.coeff(q,q)));
144 }
bool makeJacobi(const MatrixBase< Derived > &, Index p, Index q)
Definition: Jacobi.h:141

◆ makeJacobi() [2/2]

template<typename Scalar >
bool Eigen::JacobiRotation< Scalar >::makeJacobi ( const RealScalar x,
const Scalar &  y,
const RealScalar z 
)

Makes *this as a Jacobi rotation J such that applying J on both the right and left sides of the selfadjoint 2x2 matrix \( B = \left ( \begin{array}{cc} x & y \\ \overline y & z \end{array} \right )\) yields a diagonal matrix \( A = J^* B J \)

See also
MatrixBase::makeJacobi(const MatrixBase<Derived>&, Index, Index), MatrixBase::applyOnTheLeft(), MatrixBase::applyOnTheRight()

Definition at line 96 of file Jacobi.h.

97 {
98  using std::sqrt;
99  using std::abs;
100 
101  RealScalar deno = RealScalar(2)*abs(y);
103  {
104  m_c = Scalar(1);
105  m_s = Scalar(0);
106  return false;
107  }
108  else
109  {
110  RealScalar tau = (x-z)/deno;
111  RealScalar w = sqrt(numext::abs2(tau) + RealScalar(1));
112  RealScalar t;
113  if(tau>RealScalar(0))
114  {
115  t = RealScalar(1) / (tau + w);
116  }
117  else
118  {
119  t = RealScalar(1) / (tau - w);
120  }
121  RealScalar sign_t = t > RealScalar(0) ? RealScalar(1) : RealScalar(-1);
123  m_s = - sign_t * (numext::conj(y) / abs(y)) * abs(t) * n;
124  m_c = n;
125  return true;
126  }
127 }
int n
RowVector3d w
bfloat16() min(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:684
const Scalar & y

◆ operator*()

template<typename Scalar >
JacobiRotation Eigen::JacobiRotation< Scalar >::operator* ( const JacobiRotation< Scalar > &  other)
inline

Concatenates two planar rotation

Definition at line 56 of file Jacobi.h.

57  {
58  using numext::conj;
59  return JacobiRotation(m_c * other.m_c - conj(m_s) * other.m_s,
60  conj(m_c * conj(other.m_s) + conj(m_s) * conj(other.m_c)));
61  }

◆ s() [1/2]

template<typename Scalar >
Scalar& Eigen::JacobiRotation< Scalar >::s ( )
inline

Definition at line 51 of file Jacobi.h.

51 { return m_s; }

◆ s() [2/2]

template<typename Scalar >
Scalar Eigen::JacobiRotation< Scalar >::s ( ) const
inline

Definition at line 52 of file Jacobi.h.

52 { return m_s; }

◆ transpose()

template<typename Scalar >
JacobiRotation Eigen::JacobiRotation< Scalar >::transpose ( ) const
inline

Returns the transposed transformation

Definition at line 65 of file Jacobi.h.

65 { using numext::conj; return JacobiRotation(m_c, -conj(m_s)); }

Member Data Documentation

◆ m_c

template<typename Scalar >
Scalar Eigen::JacobiRotation< Scalar >::m_c
protected

Definition at line 86 of file Jacobi.h.

◆ m_s

template<typename Scalar >
Scalar Eigen::JacobiRotation< Scalar >::m_s
protected

Definition at line 86 of file Jacobi.h.


The documentation for this class was generated from the following files: