458template <
typename Scalar,
typename OtherScalar,
int SizeAtCompileTime,
int MinAlignment>
459struct apply_rotation_in_the_plane_selector<Scalar, OtherScalar, SizeAtCompileTime, MinAlignment,
461 EIGEN_DEVICE_FUNC
static void run(Scalar* x, Index incrx, Scalar* y, Index incry, Index size, OtherScalar c,
463 using Packet =
typename packet_traits<Scalar>::type;
464 using OtherPacket =
typename packet_traits<OtherScalar>::type;
466 constexpr int RequiredAlignment =
467 (std::max<int>)(unpacket_traits<Packet>::alignment, unpacket_traits<OtherPacket>::alignment);
468 constexpr Index PacketSize = packet_traits<Scalar>::size;
470 EIGEN_IF_CONSTEXPR (SizeAtCompileTime == Dynamic) {
472 if (size >= 2 * PacketSize && ((incrx == 1 && incry == 1) || PacketSize == 1)) {
474 constexpr Index Peeling = 2;
476 Index alignedStart = internal::first_default_aligned(y, size);
477 Index alignedEnd = alignedStart + ((size - alignedStart) / PacketSize) * PacketSize;
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;
484 for (Index i = 0; i < alignedStart; ++i) {
487 x[i] = c * xi + numext::conj(s) * yi;
488 y[i] = -s * xi + numext::conj(c) * yi;
491 Scalar* EIGEN_RESTRICT px = x + alignedStart;
492 Scalar* EIGEN_RESTRICT py = y + alignedStart;
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)));
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;
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)));
525 for (Index i = alignedEnd; i < size; ++i) {
528 x[i] = c * xi + numext::conj(s) * yi;
529 y[i] = -s * xi + numext::conj(c) * yi;
535 EIGEN_IF_CONSTEXPR (SizeAtCompileTime != Dynamic && MinAlignment >= RequiredAlignment) {
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)));
555 apply_rotation_in_the_plane_selector<Scalar, OtherScalar, SizeAtCompileTime, MinAlignment, false>::run(
556 x, incrx, y, incry, size, c, s);
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));
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();
572 Scalar* EIGEN_RESTRICT x = &xpr_x.derived().coeffRef(0);
573 Scalar* EIGEN_RESTRICT y = &xpr_y.derived().coeffRef(0);
575 OtherScalar c = j.c();
576 OtherScalar s = j.s();
577 if (numext::is_exactly_one(c) && numext::is_exactly_zero(s))
return;
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);
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) {
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));
594 const RealScalar t = m00 + m11;
595 const RealScalar d = m10 - m01;
598 if (numext::abs(d) < (std::numeric_limits<RealScalar>::min)()) {
604 RealScalar u = t / d;
605 s1 = RealScalar(1) / numext::sqrt<RealScalar>(RealScalar(1) + numext::abs2(u));
607 if (EIGEN_PREDICT_FALSE(numext::is_exactly_zero(s1))) {
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;
618 const RealScalar a00 = c1 * m00 + s1 * m10;
619 const RealScalar a01 = c1 * m01 + s1 * m11;
620 const RealScalar a11 = -s1 * m01 + c1 * m11;
623 j_right->makeJacobi(a00, a01, a11);
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;