11 #ifndef EIGEN_JACOBI_H 12 #define EIGEN_JACOBI_H 34 template<
typename Scalar>
class JacobiRotation
37 typedef typename NumTraits<Scalar>::Real RealScalar;
45 Scalar& c() {
return m_c; }
46 Scalar c()
const {
return m_c; }
47 Scalar& s() {
return m_s; }
48 Scalar s()
const {
return m_s; }
55 conj(m_c * conj(other.m_s) + conj(m_s) * conj(other.m_c)));
64 template<
typename Derived>
66 bool makeJacobi(
const RealScalar& x,
const Scalar& y,
const RealScalar& z);
68 void makeGivens(
const Scalar& p,
const Scalar& q, Scalar* z=0);
82 template<
typename Scalar>
88 RealScalar deno = RealScalar(2)*abs(y);
89 if(deno < (std::numeric_limits<RealScalar>::min)())
97 RealScalar tau = (x-z)/deno;
98 RealScalar w = sqrt(numext::abs2(tau) + RealScalar(1));
100 if(tau>RealScalar(0))
102 t = RealScalar(1) / (tau + w);
106 t = RealScalar(1) / (tau - w);
108 RealScalar sign_t = t > RealScalar(0) ? RealScalar(1) : RealScalar(-1);
109 RealScalar n = RealScalar(1) / sqrt(numext::abs2(t)+RealScalar(1));
110 m_s = - sign_t * (numext::conj(y) / abs(y)) * abs(t) * n;
125 template<
typename Scalar>
126 template<
typename Derived>
129 return makeJacobi(numext::real(m.coeff(p,p)), m.coeff(p,q), numext::real(m.coeff(q,q)));
148 template<
typename Scalar>
156 template<
typename Scalar>
165 m_c = numext::real(p)<0 ? Scalar(-1) : Scalar(1);
169 else if(p==Scalar(0))
177 RealScalar p1 = numext::norm1(p);
178 RealScalar q1 = numext::norm1(q);
182 RealScalar p2 = numext::abs2(ps);
184 RealScalar q2 = numext::abs2(qs);
186 RealScalar u = sqrt(RealScalar(1) + q2/p2);
187 if(numext::real(p)<RealScalar(0))
191 m_s = -qs*conj(ps)*(m_c/p2);
197 RealScalar p2 = numext::abs2(ps);
199 RealScalar q2 = numext::abs2(qs);
201 RealScalar u = q1 * sqrt(p2 + q2);
202 if(numext::real(p)<RealScalar(0))
208 m_s = -conj(ps) * (q/u);
215 template<
typename Scalar>
222 m_c = p<Scalar(0) ? Scalar(-1) : Scalar(1);
226 else if(p==Scalar(0))
229 m_s = q<Scalar(0) ? Scalar(1) : Scalar(-1);
232 else if(abs(p) > abs(q))
235 Scalar u = sqrt(Scalar(1) + numext::abs2(t));
245 Scalar u = sqrt(Scalar(1) + numext::abs2(t));
266 template<
typename VectorX,
typename VectorY,
typename OtherScalar>
276 template<
typename Derived>
277 template<
typename OtherScalar>
291 template<
typename Derived>
292 template<
typename OtherScalar>
301 template<
typename VectorX,
typename VectorY,
typename OtherScalar>
304 typedef typename VectorX::Scalar Scalar;
307 eigen_assert(xpr_x.size() == xpr_y.size());
308 Index size = xpr_x.size();
309 Index incrx = xpr_x.derived().innerStride();
310 Index incry = xpr_y.derived().innerStride();
312 Scalar* EIGEN_RESTRICT x = &xpr_x.derived().coeffRef(0);
313 Scalar* EIGEN_RESTRICT y = &xpr_y.derived().coeffRef(0);
315 OtherScalar c = j.c();
316 OtherScalar s = j.s();
317 if (c==OtherScalar(1) && s==OtherScalar(0))
322 if(VectorX::SizeAtCompileTime ==
Dynamic &&
324 ((incrx==1 && incry==1) || PacketSize == 1))
327 enum { Peeling = 2 };
329 Index alignedStart = internal::first_default_aligned(y, size);
330 Index alignedEnd = alignedStart + ((size-alignedStart)/PacketSize)*PacketSize;
332 const Packet pc = pset1<Packet>(c);
333 const Packet ps = pset1<Packet>(s);
336 for(
Index i=0; i<alignedStart; ++i)
340 x[i] = c * xi + numext::conj(s) * yi;
341 y[i] = -s * xi + numext::conj(c) * yi;
344 Scalar* EIGEN_RESTRICT px = x + alignedStart;
345 Scalar* EIGEN_RESTRICT py = y + alignedStart;
347 if(internal::first_default_aligned(x, size)==alignedStart)
349 for(
Index i=alignedStart; i<alignedEnd; i+=PacketSize)
351 Packet xi = pload<Packet>(px);
352 Packet yi = pload<Packet>(py);
353 pstore(px, padd(pmul(pc,xi),pcj.pmul(ps,yi)));
354 pstore(py, psub(pcj.pmul(pc,yi),pmul(ps,xi)));
361 Index peelingEnd = alignedStart + ((size-alignedStart)/(Peeling*PacketSize))*(Peeling*PacketSize);
362 for(
Index i=alignedStart; i<peelingEnd; i+=Peeling*PacketSize)
364 Packet xi = ploadu<Packet>(px);
365 Packet xi1 = ploadu<Packet>(px+PacketSize);
366 Packet yi = pload <Packet>(py);
367 Packet yi1 = pload <Packet>(py+PacketSize);
368 pstoreu(px, padd(pmul(pc,xi),pcj.pmul(ps,yi)));
369 pstoreu(px+PacketSize, padd(pmul(pc,xi1),pcj.pmul(ps,yi1)));
370 pstore (py, psub(pcj.pmul(pc,yi),pmul(ps,xi)));
371 pstore (py+PacketSize, psub(pcj.pmul(pc,yi1),pmul(ps,xi1)));
372 px += Peeling*PacketSize;
373 py += Peeling*PacketSize;
375 if(alignedEnd!=peelingEnd)
377 Packet xi = ploadu<Packet>(x+peelingEnd);
378 Packet yi = pload <Packet>(y+peelingEnd);
379 pstoreu(x+peelingEnd, padd(pmul(pc,xi),pcj.pmul(ps,yi)));
380 pstore (y+peelingEnd, psub(pcj.pmul(pc,yi),pmul(ps,xi)));
384 for(
Index i=alignedEnd; i<size; ++i)
388 x[i] = c * xi + numext::conj(s) * yi;
389 y[i] = -s * xi + numext::conj(c) * yi;
394 else if(VectorX::SizeAtCompileTime !=
Dynamic &&
395 (VectorX::Flags & VectorY::Flags & PacketAccessBit) &&
398 const Packet pc = pset1<Packet>(c);
399 const Packet ps = pset1<Packet>(s);
401 Scalar* EIGEN_RESTRICT px = x;
402 Scalar* EIGEN_RESTRICT py = y;
403 for(
Index i=0; i<size; i+=PacketSize)
405 Packet xi = pload<Packet>(px);
406 Packet yi = pload<Packet>(py);
407 pstore(px, padd(pmul(pc,xi),pcj.pmul(ps,yi)));
408 pstore(py, psub(pcj.pmul(pc,yi),pmul(ps,xi)));
417 for(
Index i=0; i<size; ++i)
421 *x = c * xi + numext::conj(s) * yi;
422 *y = -s * xi + numext::conj(c) * yi;
433 #endif // EIGEN_JACOBI_H JacobiRotation operator*(const JacobiRotation &other)
Concatenates two planar rotation.
Definition: Jacobi.h:51
Definition: BlasUtil.h:61
void applyOnTheLeft(const EigenBase< OtherDerived > &other)
replaces *this by other * *this.
Definition: MatrixBase.h:523
void makeGivens(const Scalar &p, const Scalar &q, Scalar *z=0)
Makes *this as a Givens rotation G such that applying to the left of the vector yields: ...
Definition: Jacobi.h:149
Definition: CoreEvaluators.h:90
JacobiRotation(const Scalar &c, const Scalar &s)
Construct a planar rotation from a cosine-sine pair (c, s).
Definition: Jacobi.h:43
void applyOnTheRight(const EigenBase< OtherDerived > &other)
replaces *this by *this * other.
Definition: MatrixBase.h:511
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:85
Definition: ForwardDeclarations.h:263
Holds information about the various numeric (i.e.
Definition: NumTraits.h:150
bool makeJacobi(const MatrixBase< Derived > &, Index p, Index q)
Makes *this as a Jacobi rotation J such that applying J on both the right and left sides of the 2x2 s...
Definition: Jacobi.h:127
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:41
const unsigned int PacketAccessBit
Short version: means the expression might be vectorized.
Definition: Constants.h:89
Definition: GenericPacketMath.h:96
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Definition: PacketMath.h:48
Definition: BandTriangularSolver.h:13
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:103
JacobiRotation transpose() const
Returns the transposed transformation.
Definition: Jacobi.h:59
void apply_rotation_in_the_plane(DenseBase< VectorX > &xpr_x, DenseBase< VectorY > &xpr_y, const JacobiRotation< OtherScalar > &j)
Applies the clock wise 2D rotation j to the set of 2D vectors of cordinates x and y: ...
Definition: Jacobi.h:302
const int Dynamic
This value means that a positive quantity (e.g., a size) is not known at compile-time, and that instead the value is stored in some runtime variable.
Definition: Constants.h:21
JacobiRotation()
Default constructor without any initialization.
Definition: Jacobi.h:40
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
JacobiRotation adjoint() const
Returns the adjoint transformation.
Definition: Jacobi.h:62