Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
Jacobi.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
5// Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
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// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_JACOBI_H
13#define EIGEN_JACOBI_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
38template <typename Scalar>
40 public:
41 using RealScalar = typename NumTraits<Scalar>::Real;
42
44 EIGEN_DEVICE_FUNC JacobiRotation() {}
45
47 EIGEN_DEVICE_FUNC JacobiRotation(const Scalar& c, const Scalar& s) : m_c(c), m_s(s) {}
48
49 EIGEN_DEVICE_FUNC Scalar& c() { return m_c; }
50 EIGEN_DEVICE_FUNC Scalar c() const { return m_c; }
51 EIGEN_DEVICE_FUNC Scalar& s() { return m_s; }
52 EIGEN_DEVICE_FUNC Scalar s() const { return m_s; }
53
55 EIGEN_DEVICE_FUNC JacobiRotation operator*(const JacobiRotation& other) const {
56 return JacobiRotation(m_c * other.m_c - numext::conj(m_s) * other.m_s,
57 numext::conj(m_c * numext::conj(other.m_s) + numext::conj(m_s) * numext::conj(other.m_c)));
58 }
59
61 EIGEN_DEVICE_FUNC JacobiRotation transpose() const { return JacobiRotation(m_c, -numext::conj(m_s)); }
62
64 EIGEN_DEVICE_FUNC JacobiRotation adjoint() const { return JacobiRotation(numext::conj(m_c), -m_s); }
65
66 template <typename Derived>
67 EIGEN_DEVICE_FUNC bool makeJacobi(const MatrixBase<Derived>&, Index p, Index q);
68 EIGEN_DEVICE_FUNC bool makeJacobi(const RealScalar& x, const Scalar& y, const RealScalar& z);
69
70 EIGEN_DEVICE_FUNC void makeGivens(const Scalar& p, const Scalar& q, Scalar* r = 0);
71
72 protected:
73 EIGEN_DEVICE_FUNC void makeGivens(const Scalar& p, const Scalar& q, Scalar* r, std::true_type);
74 EIGEN_DEVICE_FUNC void makeGivens(const Scalar& p, const Scalar& q, Scalar* r, std::false_type);
75 EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE void makeGivensScaled(const Scalar& p, const Scalar& q, Scalar* r);
76 EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE void makeGivensComplexScaled(const Scalar& p, const Scalar& q, Scalar* r);
77
78 Scalar m_c, m_s;
79};
80
88template <typename Scalar>
89EIGEN_DEVICE_FUNC bool JacobiRotation<Scalar>::makeJacobi(const RealScalar& x, const Scalar& y, const RealScalar& z) {
90 const RealScalar abs_y = numext::abs(y);
91 const RealScalar deno = RealScalar(2) * abs_y;
92 if (deno < (std::numeric_limits<RealScalar>::min)()) {
93 m_c = Scalar(1);
94 m_s = Scalar(0);
95 return false;
96 } else {
97 const RealScalar delta = x - z;
98 const RealScalar abs_delta = numext::abs(delta);
99 // |t| = 1 / (|tau| + sqrt(1 + tau^2)), where tau = (x - z) / (2 * |y|).
100 // Scale this numerator/denominator pair by min(1, 1/|tau|), taking scale = 1 at tau = 0.
101 // Then numerator <= 1 and denominator <= 1 + sqrt(2); only a ratio <= 1 is squared.
102 // Normalize the pair directly to save the division forming |t|.
103 // Select operands before dividing: speculative evaluation of both reciprocal ratios can overflow.
104 const bool large_delta = abs_delta > deno;
105 const RealScalar ratio = (large_delta ? deno : abs_delta) / (large_delta ? abs_delta : deno);
106 const RealScalar numerator = large_delta ? ratio : RealScalar(1);
107 const RealScalar denominator =
108 numext::sqrt<RealScalar>(RealScalar(1) + numext::abs2(ratio)) + (large_delta ? RealScalar(1) : ratio);
109 const RealScalar n = RealScalar(1) / numext::sqrt<RealScalar>(numext::abs2(numerator) + numext::abs2(denominator));
110 const RealScalar sine = numerator * n;
111 const RealScalar sign_t = delta > RealScalar(0) ? RealScalar(1) : RealScalar(-1);
112 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::IsComplex) {
113 m_s = -sign_t * (numext::conj(y) / abs_y) * sine;
114 } else {
115 // For real y, conj(y) / abs(y) is just its sign.
116 const RealScalar signed_sine = -sign_t * sine;
117 m_s = numext::real(y) < RealScalar(0) ? -signed_sine : signed_sine;
118 }
119 m_c = denominator * n;
120 return true;
121 }
122}
123
134template <typename Scalar>
135template <typename Derived>
136EIGEN_DEVICE_FUNC inline bool JacobiRotation<Scalar>::makeJacobi(const MatrixBase<Derived>& m, Index p, Index q) {
137 return makeJacobi(numext::real(m.coeff(p, p)), m.coeff(p, q), numext::real(m.coeff(q, q)));
138}
139
156template <typename Scalar>
157EIGEN_DEVICE_FUNC void JacobiRotation<Scalar>::makeGivens(const Scalar& p, const Scalar& q, Scalar* r) {
158 makeGivens(p, q, r, internal::bool_constant<NumTraits<Scalar>::IsComplex>());
159}
160
161// specialization for complexes
162template <typename Scalar>
163EIGEN_DEVICE_FUNC void JacobiRotation<Scalar>::makeGivens(const Scalar& p, const Scalar& q, Scalar* r, std::true_type) {
164 // The formulas below need |p|_1 and |q|_1 to be normal and finite. A 1-norm that compares equal to zero is an exact
165 // zero or, under FTZ/DAZ, below 2 min, which is below epsilon / 4 times the other 1-norm once that is at least
166 // `negligible`; so the zero branches need no look at the representation. All other inputs, including an exact zero
167 // next to a 1-norm below `negligible`, go out of line to makeGivensComplexScaled().
168 const RealScalar p1 = numext::norm1(p);
169 const RealScalar q1 = numext::norm1(q);
170 const RealScalar normalMin = (std::numeric_limits<RealScalar>::min)();
171 const RealScalar highest = NumTraits<RealScalar>::highest();
172 const RealScalar negligible = RealScalar(8) * normalMin / NumTraits<RealScalar>::epsilon();
173 if (EIGEN_PREDICT_TRUE(p1 >= normalMin && p1 <= highest && q1 >= normalMin && q1 <= highest)) {
174 if (p1 >= q1) {
175 Scalar ps = p / p1;
176 RealScalar p2 = numext::abs2(ps);
177 Scalar qs = q / p1;
178 RealScalar q2 = numext::abs2(qs);
179
180 RealScalar u = numext::sqrt<RealScalar>(RealScalar(1) + q2 / p2);
181 if (numext::real(p) < RealScalar(0)) u = -u;
182
183 m_c = Scalar(1) / u;
184 m_s = -qs * numext::conj(ps) * (m_c / p2);
185 if (r) *r = p * u;
186 } else {
187 // Divide by u = sqrt(p2 + q2), i.e. in the quantities scaled by 1/q1: dividing p and q by q1 u loses all but a
188 // few bits when q1 is subnormal, and the rotation is no longer unitary. The phase of p comes from p / p1, which
189 // is O(1) even when p is subnormal or p / q1 underflows.
190 Scalar ps = p / q1;
191 RealScalar p2 = numext::abs2(ps);
192 Scalar qs = q / q1;
193 RealScalar q2 = numext::abs2(qs);
194
195 RealScalar u = numext::sqrt<RealScalar>(p2 + q2);
196 if (numext::real(p) < RealScalar(0)) u = -u;
197
198 const Scalar pn = p / p1;
199 const Scalar phase = pn / numext::abs(pn);
200 m_c = numext::abs(ps) / u;
201 m_s = -numext::conj(phase) * (qs / u);
202 if (r) *r = phase * (q1 * u);
203 }
204 } else if (p1 == RealScalar(0) && q1 >= negligible && q1 <= highest) {
205 // One component of qn = q / max(|Re q|, |Im q|) is +-1, so |qn|^2 = 1 + t^2 with |t| <= 1 is safe to form, and
206 // |q| = qMax |qn|.
207 const RealScalar qMax = numext::maxi(numext::abs(numext::real(q)), numext::abs(numext::imag(q)));
208 const Scalar qn = q / qMax;
209 const RealScalar qnAbs = numext::sqrt(numext::abs2(qn));
210 m_c = 0;
211 m_s = -qn / qnAbs;
212 if (r) *r = qMax * qnAbs;
213 } else if (q1 == RealScalar(0) && p1 >= negligible) {
214 const bool negative = numext::real(p) < RealScalar(0);
215 m_c = negative ? Scalar(-1) : Scalar(1);
216 m_s = 0;
217 if (r) *r = negative ? Scalar(-p) : p;
218 } else {
219 makeGivensComplexScaled(p, q, r);
220 }
221}
222
223template <typename Scalar>
224EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE void JacobiRotation<Scalar>::makeGivensComplexScaled(const Scalar& p,
225 const Scalar& q, Scalar* r) {
226 // Scale p and q by a common power of two such that their largest real or imaginary magnitude is about one; for
227 // binary scalars, the scaling goes through integer significands, which FTZ/DAZ cannot flush. Then the larger of
228 // |p|_1 and |q|_1 is normal and finite, and the smaller one is negligible wherever it is subnormal or zero. Only
229 // p = q = 0 needs its own branch; it is read from the representation, since under DAZ a subnormal compares equal to
230 // zero.
231 if (numext::is_exactly_zero_no_flush(numext::real(p)) && numext::is_exactly_zero_no_flush(numext::imag(p)) &&
232 numext::is_exactly_zero_no_flush(numext::real(q)) && numext::is_exactly_zero_no_flush(numext::imag(q))) {
233 m_c = Scalar(1);
234 m_s = Scalar(0);
235 if (r) *r = p;
236 return;
237 }
238 using Scaling = internal::safe_scaling<RealScalar>;
239 Matrix<Scalar, 2, 1> scaled(p, q);
240 const RealScalar largest = numext::maxi(numext::maxi(numext::abs(numext::real(p)), numext::abs(numext::imag(p))),
241 numext::maxi(numext::abs(numext::real(q)), numext::abs(numext::imag(q))));
242 const RealScalar maxCoeff = Scaling::recover_flushed_max_coeff(scaled, largest);
243 const internal::safe_scaling_factors<RealScalar> factors = Scaling::scale_to(scaled, scaled, maxCoeff);
244 const Scalar ps = scaled.x(), qs = scaled.y();
245 const RealScalar p1 = numext::norm1(ps);
246 const RealScalar q1 = numext::norm1(qs);
248 if (p1 >= q1) {
249 const Scalar pn = ps / p1;
250 const RealScalar p2 = numext::abs2(pn);
251 const Scalar qn = qs / p1;
252 const RealScalar q2 = numext::abs2(qn);
253
254 RealScalar u = numext::sqrt<RealScalar>(RealScalar(1) + q2 / p2);
255 if (numext::real(ps) < RealScalar(0)) u = -u;
256
257 m_c = Scalar(1) / u;
258 m_s = -qn * numext::conj(pn) * (m_c / p2);
259 rs(0) = ps * u;
260 } else {
261 const Scalar pn = ps / q1;
262 const RealScalar p2 = numext::abs2(pn);
263 const Scalar qn = qs / q1;
264 const RealScalar q2 = numext::abs2(qn);
265
266 RealScalar u = numext::sqrt<RealScalar>(p2 + q2);
267 if (numext::real(ps) < RealScalar(0)) u = -u;
268
269 // The phase of p comes from p scaled on its own, since p can be negligible next to q.
270 Scalar phase(1);
271 if (!numext::is_exactly_zero_no_flush(numext::real(p)) || !numext::is_exactly_zero_no_flush(numext::imag(p))) {
273 const RealScalar pMax = Scaling::recover_flushed_max_coeff(
274 pv, numext::maxi(numext::abs(numext::real(p)), numext::abs(numext::imag(p))));
275 Scaling::scale_to(pv, pv, pMax);
276 phase = pv(0) / numext::abs(pv(0));
277 }
278 m_c = numext::abs(pn) / u;
279 m_s = -numext::conj(phase) * (qn / u);
280 rs(0) = phase * (q1 * u);
281 }
282 if (r) {
283 Scaling::unscale_in_place(rs, maxCoeff, factors);
284 *r = rs(0);
285 }
286}
287
288// specialization for reals
289template <typename Scalar>
290EIGEN_DEVICE_FUNC void JacobiRotation<Scalar>::makeGivens(const Scalar& p, const Scalar& q, Scalar* r,
291 std::false_type) {
292 if (numext::is_exactly_zero_no_flush(q)) {
293 // DAZ can hide the sign; MSVC's non-intrinsic abs(float) can flush the
294 // result through a float->double->float conversion even with FTZ alone.
295 if (numext::abs(p) < (std::numeric_limits<Scalar>::min)() && !numext::is_exactly_zero_no_flush(p)) {
296 makeGivensScaled(p, q, r);
297 return;
298 }
299 m_c = p < Scalar(0) ? Scalar(-1) : Scalar(1);
300 m_s = Scalar(0);
301 if (r) *r = numext::abs(p);
302 return;
303 }
304 if (numext::is_exactly_zero_no_flush(p)) {
305 if (numext::abs(q) < (std::numeric_limits<Scalar>::min)()) {
306 makeGivensScaled(p, q, r);
307 return;
308 }
309 m_c = Scalar(0);
310 m_s = q < Scalar(0) ? Scalar(1) : Scalar(-1);
311 if (r) *r = numext::abs(q);
312 return;
313 }
314
315 // Safe-range thresholds following Anderson, "Algorithm 978: Safe Scaling
316 // in the Level 1 BLAS", ACM TOMS 44(1), 2017. When both |p| and |q| lie
317 // in (rtmin, rtmax), the direct formula r = p * sqrt(1 + (q/p)^2) cannot
318 // over- or underflow before the true result would. Outside that range
319 // we prescale with safe_scaling (normal powers of two for supported binary
320 // scalars), preserving significant float/double subnormal inputs under FTZ/DAZ. This preserves the
321 // existing Eigen sign convention (r >= 0, sign carried in c).
322 const Scalar safmin = (std::numeric_limits<Scalar>::min)();
323 const Scalar safmax = Scalar(1) / safmin;
324#if defined(EIGEN_SYCL_DEVICE_ONLY)
325 const Scalar rtmin = numext::sqrt(safmin);
326 const Scalar rtmax = numext::sqrt<Scalar>(safmax / Scalar(2));
327#else
328 // GCC does not fold the SSE numext::sqrt intrinsic for the constant range thresholds below.
329 EIGEN_USING_STD(sqrt);
330 const Scalar rtmin = sqrt(safmin);
331 const Scalar rtmax = sqrt(safmax / Scalar(2));
332#endif
333 const Scalar abs_p = numext::abs(p);
334 const Scalar abs_q = numext::abs(q);
335 const Scalar mx = numext::maxi(abs_p, abs_q);
336 const Scalar mn = numext::mini(abs_p, abs_q);
337
338 if (EIGEN_PREDICT_TRUE(mx < rtmax && mn > rtmin)) {
339 // Safe range: existing direct formulas are stable.
340 if (abs_p > abs_q) {
341 Scalar t = q / p;
342 Scalar u = numext::sqrt<Scalar>(Scalar(1) + numext::abs2(t));
343 if (p < Scalar(0)) u = -u;
344 m_c = Scalar(1) / u;
345 m_s = -t * m_c;
346 if (r) *r = p * u;
347 } else {
348 Scalar t = p / q;
349 Scalar u = numext::sqrt<Scalar>(Scalar(1) + numext::abs2(t));
350 if (q < Scalar(0)) u = -u;
351 m_s = -Scalar(1) / u;
352 m_c = -t * m_s;
353 if (r) *r = q * u;
354 }
355 } else {
356 makeGivensScaled(p, q, r);
357 }
358}
359
360template <typename Scalar>
361EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE void JacobiRotation<Scalar>::makeGivensScaled(const Scalar& p, const Scalar& q,
362 Scalar* r) {
363 using Scaling = internal::safe_scaling<Scalar>;
365 scaled << p, q;
366 const Scalar maximum = Scaling::recover_flushed_max_coeff(scaled, scaled.cwiseAbs().maxCoeff());
367 const auto factors = Scaling::scale_to(scaled, scaled, maximum);
368 const Scalar ps = scaled.x(), qs = scaled.y();
369 Scalar norm;
370 if (numext::abs(ps) > numext::abs(qs)) {
371 const Scalar t = qs / ps;
372 Scalar u = numext::sqrt<Scalar>(Scalar(1) + numext::abs2(t));
373 if (ps < Scalar(0)) u = -u;
374 m_c = Scalar(1) / u;
375 m_s = -t * m_c;
376 if (r) norm = ps * u;
377 } else {
378 const Scalar t = ps / qs;
379 Scalar u = numext::sqrt<Scalar>(Scalar(1) + numext::abs2(t));
380 if (qs < Scalar(0)) u = -u;
381 m_s = -Scalar(1) / u;
382 m_c = -t * m_s;
383 if (r) norm = qs * u;
384 }
385 if (r) {
386 // Restore subnormal r through the same FTZ/DAZ-safe path as the inputs.
388 result[0] = norm;
389 Scaling::unscale_in_place(result, maximum, factors);
390 *r = result[0];
391 }
392}
393
394/****************************************************************************************
395 * Implementation of MatrixBase methods
396 ****************************************************************************************/
397
398namespace internal {
406template <typename VectorX, typename VectorY, typename OtherScalar>
407EIGEN_DEVICE_FUNC void apply_rotation_in_the_plane(DenseBase<VectorX>& xpr_x, DenseBase<VectorY>& xpr_y,
408 const JacobiRotation<OtherScalar>& j);
409} // namespace internal
410
417template <typename Derived>
418template <typename OtherScalar>
419EIGEN_DEVICE_FUNC inline void MatrixBase<Derived>::applyOnTheLeft(Index p, Index q,
421 RowXpr x(this->row(p));
422 RowXpr y(this->row(q));
423 internal::apply_rotation_in_the_plane(x, y, j);
424}
425
432template <typename Derived>
433template <typename OtherScalar>
434EIGEN_DEVICE_FUNC inline void MatrixBase<Derived>::applyOnTheRight(Index p, Index q,
436 ColXpr x(this->col(p));
437 ColXpr y(this->col(q));
438 internal::apply_rotation_in_the_plane(x, y, j.transpose());
439}
440
441namespace internal {
442
443template <typename Scalar, typename OtherScalar, int SizeAtCompileTime, int MinAlignment, bool Vectorizable>
444struct apply_rotation_in_the_plane_selector {
445 static EIGEN_DEVICE_FUNC inline void run(Scalar* x, Index incrx, Scalar* y, Index incry, Index size, OtherScalar c,
446 OtherScalar s) {
447 for (Index i = 0; i < size; ++i) {
448 Scalar xi = *x;
449 Scalar yi = *y;
450 *x = c * xi + numext::conj(s) * yi;
451 *y = -s * xi + numext::conj(c) * yi;
452 x += incrx;
453 y += incry;
454 }
455 }
456};
457
458template <typename Scalar, typename OtherScalar, int SizeAtCompileTime, int MinAlignment>
459struct apply_rotation_in_the_plane_selector<Scalar, OtherScalar, SizeAtCompileTime, MinAlignment,
460 true /* vectorizable */> {
461 EIGEN_DEVICE_FUNC static void run(Scalar* x, Index incrx, Scalar* y, Index incry, Index size, OtherScalar c,
462 OtherScalar s) {
463 using Packet = typename packet_traits<Scalar>::type;
464 using OtherPacket = typename packet_traits<OtherScalar>::type;
465
466 constexpr int RequiredAlignment =
467 (std::max<int>)(unpacket_traits<Packet>::alignment, unpacket_traits<OtherPacket>::alignment);
468 constexpr Index PacketSize = packet_traits<Scalar>::size;
469
470 EIGEN_IF_CONSTEXPR (SizeAtCompileTime == Dynamic) {
471 /*** dynamic-size vectorized paths ***/
472 if (size >= 2 * PacketSize && ((incrx == 1 && incry == 1) || PacketSize == 1)) {
473 // both vectors are sequentially stored in memory => vectorization
474 constexpr Index Peeling = 2;
475
476 Index alignedStart = internal::first_default_aligned(y, size);
477 Index alignedEnd = alignedStart + ((size - alignedStart) / PacketSize) * PacketSize;
478
479 const OtherPacket pc = pset1<OtherPacket>(c);
480 const OtherPacket ps = pset1<OtherPacket>(s);
481 conj_helper<OtherPacket, Packet, NumTraits<OtherScalar>::IsComplex, false> pcj;
482 conj_helper<OtherPacket, Packet, false, false> pm;
483
484 for (Index i = 0; i < alignedStart; ++i) {
485 Scalar xi = x[i];
486 Scalar yi = y[i];
487 x[i] = c * xi + numext::conj(s) * yi;
488 y[i] = -s * xi + numext::conj(c) * yi;
489 }
490
491 Scalar* EIGEN_RESTRICT px = x + alignedStart;
492 Scalar* EIGEN_RESTRICT py = y + alignedStart;
493
494 if (internal::first_default_aligned(x, size) == alignedStart) {
495 for (Index i = alignedStart; i < alignedEnd; i += PacketSize) {
496 Packet xi = pload<Packet>(px);
497 Packet yi = pload<Packet>(py);
498 pstore(px, pm.pmadd(pc, xi, pcj.pmul(ps, yi)));
499 pstore(py, pcj.pmsub(pc, yi, pm.pmul(ps, xi)));
500 px += PacketSize;
501 py += PacketSize;
502 }
503 } else {
504 Index peelingEnd = alignedStart + ((size - alignedStart) / (Peeling * PacketSize)) * (Peeling * PacketSize);
505 for (Index i = alignedStart; i < peelingEnd; i += Peeling * PacketSize) {
506 Packet xi = ploadu<Packet>(px);
507 Packet xi1 = ploadu<Packet>(px + PacketSize);
508 Packet yi = pload<Packet>(py);
509 Packet yi1 = pload<Packet>(py + PacketSize);
510 pstoreu(px, pm.pmadd(pc, xi, pcj.pmul(ps, yi)));
511 pstoreu(px + PacketSize, pm.pmadd(pc, xi1, pcj.pmul(ps, yi1)));
512 pstore(py, pcj.pmsub(pc, yi, pm.pmul(ps, xi)));
513 pstore(py + PacketSize, pcj.pmsub(pc, yi1, pm.pmul(ps, xi1)));
514 px += Peeling * PacketSize;
515 py += Peeling * PacketSize;
516 }
517 if (alignedEnd != peelingEnd) {
518 Packet xi = ploadu<Packet>(x + peelingEnd);
519 Packet yi = pload<Packet>(y + peelingEnd);
520 pstoreu(x + peelingEnd, pm.pmadd(pc, xi, pcj.pmul(ps, yi)));
521 pstore(y + peelingEnd, pcj.pmsub(pc, yi, pm.pmul(ps, xi)));
522 }
523 }
524
525 for (Index i = alignedEnd; i < size; ++i) {
526 Scalar xi = x[i];
527 Scalar yi = y[i];
528 x[i] = c * xi + numext::conj(s) * yi;
529 y[i] = -s * xi + numext::conj(c) * yi;
530 }
531 return;
532 }
533 }
534
535 EIGEN_IF_CONSTEXPR (SizeAtCompileTime != Dynamic && MinAlignment >= RequiredAlignment) {
536 /*** fixed-size vectorized path ***/
537 const OtherPacket pc = pset1<OtherPacket>(c);
538 const OtherPacket ps = pset1<OtherPacket>(s);
539 conj_helper<OtherPacket, Packet, NumTraits<OtherScalar>::IsComplex, false> pcj;
540 conj_helper<OtherPacket, Packet, false, false> pm;
541 Scalar* EIGEN_RESTRICT px = x;
542 Scalar* EIGEN_RESTRICT py = y;
543 for (Index i = 0; i < size; i += PacketSize) {
544 Packet xi = pload<Packet>(px);
545 Packet yi = pload<Packet>(py);
546 pstore(px, pm.pmadd(pc, xi, pcj.pmul(ps, yi)));
547 pstore(py, pcj.pmsub(pc, yi, pm.pmul(ps, xi)));
548 px += PacketSize;
549 py += PacketSize;
550 }
551 return;
552 }
553
554 /*** non-vectorized path ***/
555 apply_rotation_in_the_plane_selector<Scalar, OtherScalar, SizeAtCompileTime, MinAlignment, false>::run(
556 x, incrx, y, incry, size, c, s);
557 }
558};
559
560template <typename VectorX, typename VectorY, typename OtherScalar>
561EIGEN_DEVICE_FUNC void inline apply_rotation_in_the_plane(DenseBase<VectorX>& xpr_x, DenseBase<VectorY>& xpr_y,
562 const JacobiRotation<OtherScalar>& j) {
563 using Scalar = typename VectorX::Scalar;
564 constexpr bool Vectorizable = (int(evaluator<VectorX>::Flags) & int(evaluator<VectorY>::Flags) & PacketAccessBit) &&
565 (int(packet_traits<Scalar>::size) == int(packet_traits<OtherScalar>::size));
566
567 EIGEN_INTERNAL_ASSERT_SAME_SIZE(xpr_x, xpr_y);
568 Index size = xpr_x.size();
569 Index incrx = xpr_x.derived().innerStride();
570 Index incry = xpr_y.derived().innerStride();
571
572 Scalar* EIGEN_RESTRICT x = &xpr_x.derived().coeffRef(0);
573 Scalar* EIGEN_RESTRICT y = &xpr_y.derived().coeffRef(0);
574
575 OtherScalar c = j.c();
576 OtherScalar s = j.s();
577 if (numext::is_exactly_one(c) && numext::is_exactly_zero(s)) return;
578
579 constexpr int Alignment = (std::min)(int(evaluator<VectorX>::Alignment), int(evaluator<VectorY>::Alignment));
580 apply_rotation_in_the_plane_selector<Scalar, OtherScalar, VectorX::SizeAtCompileTime, Alignment, Vectorizable>::run(
581 x, incrx, y, incry, size, c, s);
582}
583
584template <typename MatrixType, typename RealScalar, typename Index>
585EIGEN_DONT_INLINE void real_2x2_jacobi_svd(const MatrixType& matrix, Index p, Index q,
586 JacobiRotation<RealScalar>* j_left, JacobiRotation<RealScalar>* j_right) {
587 // Extract 2x2 submatrix into scalars (avoids Matrix construction on stack).
588 const RealScalar m00 = numext::real(matrix.coeff(p, p));
589 const RealScalar m01 = numext::real(matrix.coeff(p, q));
590 const RealScalar m10 = numext::real(matrix.coeff(q, p));
591 const RealScalar m11 = numext::real(matrix.coeff(q, q));
592
593 // Compute the symmetrizing rotation rot1 such that rot1 * [m] is symmetric.
594 const RealScalar t = m00 + m11;
595 const RealScalar d = m10 - m01;
596
597 RealScalar c1, s1;
598 if (numext::abs(d) < (std::numeric_limits<RealScalar>::min)()) {
599 c1 = RealScalar(1);
600 s1 = RealScalar(0);
601 } else {
602 // u = t/d is huge, even infinite, when |d| is tiny compared to |t|. This assumes that t and d are finite, which
603 // JacobiSVD and RealQZ guarantee by scaling their input; u = +-inf is then the limit d/t -> 0.
604 RealScalar u = t / d;
605 s1 = RealScalar(1) / numext::sqrt<RealScalar>(RealScalar(1) + numext::abs2(u));
606 c1 = u * s1;
607 if (EIGEN_PREDICT_FALSE(numext::is_exactly_zero(s1))) {
608 // abs2(u) overflowed (u may be infinite). With v = 1/u: s1 = |v| / sqrt(1 + v^2), c1 = sign(u) / sqrt(1 + v^2).
609 const RealScalar v = RealScalar(1) / u;
610 const RealScalar r = numext::sqrt<RealScalar>(RealScalar(1) + numext::abs2(v));
611 s1 = numext::abs(v) / r;
612 c1 = (u < RealScalar(0) ? RealScalar(-1) : RealScalar(1)) / r;
613 }
614 }
615
616 // Apply rot1 to the 2x2 submatrix inline (avoids rotation dispatch overhead).
617 // Result is symmetric, so we only need 3 values: a00, a01 (== a10), a11.
618 const RealScalar a00 = c1 * m00 + s1 * m10;
619 const RealScalar a01 = c1 * m01 + s1 * m11;
620 const RealScalar a11 = -s1 * m01 + c1 * m11;
621
622 // Compute the diagonalizing rotation j_right from the symmetrized matrix.
623 j_right->makeJacobi(a00, a01, a11);
624
625 // Compose j_left = rot1 * j_right^T inline (avoids template machinery overhead).
626 const RealScalar jr_c = j_right->c();
627 const RealScalar jr_s = j_right->s();
628 j_left->c() = c1 * jr_c + s1 * jr_s;
629 j_left->s() = s1 * jr_c - c1 * jr_s;
630}
631
632} // end namespace internal
633
634} // end namespace Eigen
635
636#endif // EIGEN_JACOBI_H
constexpr ColXpr col(Index i)
Definition DenseBase.h:1082
constexpr RowXpr row(Index i)
Definition DenseBase.h:1095
Rotation given by a cosine-sine pair.
Definition Jacobi.h:39
JacobiRotation operator*(const JacobiRotation &other) const
Definition Jacobi.h:55
JacobiRotation()
Definition Jacobi.h:44
JacobiRotation(const Scalar &c, const Scalar &s)
Definition Jacobi.h:47
bool makeJacobi(const MatrixBase< Derived > &, Index p, Index q)
Definition Jacobi.h:136
JacobiRotation adjoint() const
Definition Jacobi.h:64
JacobiRotation transpose() const
Definition Jacobi.h:61
void makeGivens(const Scalar &p, const Scalar &q, Scalar *r=0)
Definition Jacobi.h:157
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
void applyOnTheLeft(const EigenBase< OtherDerived > &other)
Definition MatrixBase.h:554
void applyOnTheRight(const EigenBase< OtherDerived > &other)
Definition MatrixBase.h:543
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
constexpr unsigned int PacketAccessBit
Definition Constants.h:98