| // This file is part of Eigen, a lightweight C++ template library | |
| // for linear algebra. | |
| // | |
| // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr> | |
| // Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com> | |
| // | |
| // This Source Code Form is subject to the terms of the Mozilla | |
| // Public License v. 2.0. If a copy of the MPL was not distributed | |
| // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
| // SPDX-License-Identifier: MPL-2.0 | |
| #include "main.h" | |
| #include "fp_control.h" | |
| #include <Eigen/SVD> | |
| #include <cfenv> | |
| template <typename MatrixType, typename JacobiScalar> | |
| void jacobi(const MatrixType& m = MatrixType()) { | |
| Index rows = m.rows(); | |
| Index cols = m.cols(); | |
| enum { RowsAtCompileTime = MatrixType::RowsAtCompileTime, ColsAtCompileTime = MatrixType::ColsAtCompileTime }; | |
| using JacobiVector = Matrix<JacobiScalar, 2, 1>; | |
| const MatrixType a(MatrixType::Random(rows, cols)); | |
| JacobiVector v = JacobiVector::Random().normalized(); | |
| JacobiScalar c = v.x(), s = v.y(); | |
| JacobiRotation<JacobiScalar> rot(c, s); | |
| { | |
| Index p = internal::random<Index>(0, rows - 1); | |
| Index q; | |
| do { | |
| q = internal::random<Index>(0, rows - 1); | |
| } while (q == p); | |
| MatrixType b = a; | |
| b.applyOnTheLeft(p, q, rot); | |
| VERIFY_IS_APPROX(b.row(p), c * a.row(p) + numext::conj(s) * a.row(q)); | |
| VERIFY_IS_APPROX(b.row(q), -s * a.row(p) + numext::conj(c) * a.row(q)); | |
| } | |
| { | |
| Index p = internal::random<Index>(0, cols - 1); | |
| Index q; | |
| do { | |
| q = internal::random<Index>(0, cols - 1); | |
| } while (q == p); | |
| MatrixType b = a; | |
| b.applyOnTheRight(p, q, rot); | |
| VERIFY_IS_APPROX(b.col(p), c * a.col(p) - s * a.col(q)); | |
| VERIFY_IS_APPROX(b.col(q), numext::conj(s) * a.col(p) + numext::conj(c) * a.col(q)); | |
| EIGEN_IF_CONSTEXPR (RowsAtCompileTime == Dynamic) { | |
| using Scalar = typename MatrixType::Scalar; | |
| using RealScalar = typename NumTraits<Scalar>::Real; | |
| using Vec = Matrix<Scalar, Dynamic, 1>; | |
| Matrix<RealScalar, Dynamic, 1> raw_x(2 * rows + 1); | |
| Matrix<RealScalar, Dynamic, 1> raw_y(2 * rows + 1); | |
| Map<Vec, Unaligned> ux(reinterpret_cast<Scalar*>(raw_x.data() + 1), rows); | |
| Map<Vec, Unaligned> uy(reinterpret_cast<Scalar*>(raw_y.data() + 1), rows); | |
| ux = a.col(p); | |
| uy = a.col(q); | |
| internal::apply_rotation_in_the_plane(ux, uy, rot.transpose()); | |
| VERIFY_IS_APPROX(ux, b.col(p)); | |
| VERIFY_IS_APPROX(uy, b.col(q)); | |
| ux = a.col(p); | |
| Vec aligned_y = a.col(q); | |
| internal::apply_rotation_in_the_plane(ux, aligned_y, rot.transpose()); | |
| VERIFY_IS_APPROX(ux, b.col(p)); | |
| VERIFY_IS_APPROX(aligned_y, b.col(q)); | |
| } | |
| } | |
| } | |
| // Verify that JacobiRotation::makeGivens(p, q, &r) produces a rotation that | |
| // zeros out q, even when (p, q) straddle the over-/underflow thresholds | |
| // where the direct formula r = p * sqrt(1 + (q/p)^2) would over- or | |
| // underflow. Eigen's convention is r >= 0 with sign carried in c. | |
| template <typename Scalar> | |
| void verify_makeGivens(const Scalar& p, const Scalar& q) { | |
| using std::abs; | |
| Scalar r; | |
| JacobiRotation<Scalar> rot; | |
| rot.makeGivens(p, q, &r); | |
| // Eigen's J^T * [p; q] = [r; 0] with J = [c s; -s c]. Verify the homogeneous relation after scaling the inputs so | |
| // the check itself does not overflow or flush intermediate products to zero. | |
| const Scalar scale = numext::maxi(numext::maxi(abs(p), abs(q)), (std::numeric_limits<Scalar>::min)()); | |
| const Scalar scaledP = p / scale; | |
| const Scalar scaledQ = q / scale; | |
| const Scalar scaledR = r / scale; | |
| const Scalar rotated0 = rot.c() * scaledP - rot.s() * scaledQ; | |
| const Scalar rotated1 = rot.s() * scaledP + rot.c() * scaledQ; | |
| const Scalar tol = NumTraits<Scalar>::epsilon() * (abs(scaledR) + (std::numeric_limits<Scalar>::min)()) * Scalar(64); | |
| VERIFY(abs(rotated0 - scaledR) <= tol); | |
| VERIFY(abs(rotated1) <= tol); | |
| VERIFY(r >= Scalar(0)); | |
| VERIFY_IS_APPROX(numext::abs2(rot.c()) + numext::abs2(rot.s()), Scalar(1)); | |
| } | |
| template <typename Scalar> | |
| void jacobi_makegivens_safe_scaling() { | |
| using std::sqrt; | |
| const Scalar safmin = (std::numeric_limits<Scalar>::min)(); | |
| const Scalar safmax = Scalar(1) / safmin; | |
| const Scalar rtmin = sqrt(safmin); | |
| const Scalar rtmax = sqrt(safmax / Scalar(2)); | |
| const Scalar one(1); | |
| const Scalar two(2); | |
| const Scalar half(0.5L); | |
| // Safe-range cases (regression — must keep existing fast path working). | |
| verify_makeGivens<Scalar>(Scalar(0), Scalar(0)); | |
| verify_makeGivens<Scalar>(Scalar(3), Scalar(4)); | |
| verify_makeGivens<Scalar>(Scalar(-3), Scalar(4)); | |
| verify_makeGivens<Scalar>(Scalar(3), Scalar(-4)); | |
| verify_makeGivens<Scalar>(Scalar(-3), Scalar(-4)); | |
| // Both inputs near overflow: direct formula r = p * sqrt(1+(q/p)^2) would | |
| // overflow because sqrt(1+1) > 1. Prescaling avoids this. | |
| verify_makeGivens<Scalar>(rtmax * two, rtmax); | |
| verify_makeGivens<Scalar>(-rtmax * two, rtmax); | |
| verify_makeGivens<Scalar>(rtmax, rtmax); | |
| verify_makeGivens<Scalar>(rtmax * Scalar(1.5), rtmax * Scalar(1.5)); | |
| // Both inputs near underflow / subnormal: direct (q/p)^2 underflows to 0. | |
| verify_makeGivens<Scalar>(rtmin * half, rtmin * half); | |
| verify_makeGivens<Scalar>(safmin, safmin); | |
| verify_makeGivens<Scalar>(-safmin, safmin); | |
| // Mixed: one near overflow, one normal. | |
| verify_makeGivens<Scalar>(rtmax * Scalar(1.5), one); | |
| verify_makeGivens<Scalar>(one, rtmax * Scalar(1.5)); | |
| verify_makeGivens<Scalar>(-rtmax * Scalar(1.5), one); | |
| // Mixed: one near underflow, one normal. | |
| verify_makeGivens<Scalar>(safmin, one); | |
| verify_makeGivens<Scalar>(one, safmin); | |
| // Mixed: subnormal and near-overflow simultaneously. | |
| verify_makeGivens<Scalar>(safmin, rtmax); | |
| verify_makeGivens<Scalar>(rtmax, safmin); | |
| } | |
| // Extra properties of a real rotation: r is positive, to within the subnormal grid, and exactly |p| or |q| where the | |
| // other is zero. | |
| template <typename Scalar> | |
| void check_real_givens_result(const Scalar& p, const Scalar& q, typename NumTraits<Scalar>::Real scale, | |
| long double expected, long double tolerance, const Scalar& r, std::false_type) { | |
| const long double normalizedR = static_cast<long double>(r) / static_cast<long double>(scale); | |
| const long double quantum = | |
| static_cast<long double>(std::numeric_limits<Scalar>::denorm_min()) / static_cast<long double>(scale); | |
| VERIFY(normalizedR > 0); | |
| VERIFY(numext::abs(normalizedR - expected) <= tolerance * expected + quantum / 2); | |
| if (numext::is_exactly_zero(p) || numext::is_exactly_zero(q)) { | |
| VERIFY_IS_EQUAL(r, numext::abs(numext::is_exactly_zero(p) ? q : p)); | |
| } | |
| } | |
| template <typename Scalar> | |
| void check_real_givens_result(const Scalar&, const Scalar&, typename NumTraits<Scalar>::Real, long double, long double, | |
| const Scalar&, std::true_type) {} | |
| template <typename Scalar> | |
| void check_givens_result(const Scalar& p, const Scalar& q, typename NumTraits<Scalar>::Real scale, bool checkR, | |
| const JacobiRotation<Scalar>& rotation, const JacobiRotation<Scalar>& withoutR, | |
| const Scalar& r) { | |
| using RealScalar = typename NumTraits<Scalar>::Real; | |
| using Wide = std::complex<long double>; | |
| const long double tolerance = 16 * static_cast<long double>(NumTraits<RealScalar>::epsilon()); | |
| const auto widen = [](const Scalar& x) { | |
| return Wide(static_cast<long double>(numext::real(x)), static_cast<long double>(numext::imag(x))); | |
| }; | |
| VERIFY_IS_EQUAL(rotation.c(), withoutR.c()); | |
| VERIFY_IS_EQUAL(rotation.s(), withoutR.s()); | |
| VERIFY_IS_EQUAL(numext::imag(rotation.c()), RealScalar(0)); | |
| // J^* (P, Q) = (c P - conj(s) Q, s P + c Q) = (r / scale, 0). | |
| const long double c = static_cast<long double>(numext::real(rotation.c())); | |
| const Wide s = widen(rotation.s()); | |
| const Wide P = widen(p) / static_cast<long double>(scale), Q = widen(q) / static_cast<long double>(scale); | |
| const long double expected = std::sqrt(std::norm(P) + std::norm(Q)); | |
| VERIFY(numext::abs(c * c + std::norm(s) - 1) <= tolerance); | |
| VERIFY(std::abs(s * P + c * Q) <= tolerance * expected); | |
| const Wide rotated = c * P - std::conj(s) * Q; | |
| VERIFY(numext::abs(std::abs(rotated) - expected) <= tolerance * expected); | |
| if (checkR) { | |
| // r = phase * (q1 * u) is rounded twice, so allow two steps of the subnormal grid. | |
| const long double quantum = | |
| static_cast<long double>(std::numeric_limits<RealScalar>::denorm_min()) / static_cast<long double>(scale); | |
| VERIFY(std::abs(widen(r) / static_cast<long double>(scale) - rotated) <= tolerance * expected + 2 * quantum); | |
| check_real_givens_result(p, q, scale, expected, tolerance, r, | |
| internal::bool_constant<NumTraits<Scalar>::IsComplex>()); | |
| } | |
| } | |
| // Checks that makeGivens(p, q) is unitary and maps (p, q) to (r, 0), in long double with (P, Q) = (p, q) / scale exact, | |
| // for each flush-to-zero mode the hardware supports. The rotation is computed in that mode and checked outside it. | |
| template <typename Scalar> | |
| void check_givens(const Scalar& p, const Scalar& q, typename NumTraits<Scalar>::Real scale, bool checkR = true) { | |
| for (const FlushToZeroMode mode : | |
| {FlushToZeroMode::None, FlushToZeroMode::Results, FlushToZeroMode::Inputs, FlushToZeroMode::Both}) { | |
| JacobiRotation<Scalar> rotation, withoutR; | |
| Scalar r; | |
| { | |
| const ScopedFlushToZero scope(mode); | |
| if (!scope.isSupported() || scope.mode() != mode) continue; | |
| rotation.makeGivens(p, q, &r); | |
| withoutR.makeGivens(p, q); | |
| // makeGivens leaves the flushing it ran under as it was. | |
| VERIFY(ScopedFlushToZero::hardwareMode() == scope.mode()); | |
| } | |
| check_givens_result(p, q, scale, checkR, rotation, withoutR, r); | |
| } | |
| } | |
| template <typename Scalar> | |
| void jacobi_makegivens_subnormal() { | |
| const Scalar minimum = (std::numeric_limits<Scalar>::min)(); | |
| const Scalar tiny = std::numeric_limits<Scalar>::denorm_min(); | |
| JacobiRotation<Scalar> zero; | |
| zero.makeGivens(-Scalar(0), Scalar(0)); | |
| VERIFY_IS_EQUAL(zero.c(), Scalar(1)); | |
| VERIFY_IS_EQUAL(zero.s(), Scalar(0)); | |
| for (Scalar scale : {minimum, Scalar(minimum / Scalar(16)), tiny}) { | |
| for (int i = -3; i <= 3; ++i) { | |
| for (int j = -3; j <= 3; ++j) { | |
| if (i == 0 && j == 0) continue; | |
| const Scalar p = i == 0 ? -Scalar(0) : Scalar(i) * scale; | |
| const Scalar q = Scalar(j) * scale; | |
| check_givens(p, q, scale); | |
| } | |
| } | |
| } | |
| const Scalar maximum = NumTraits<Scalar>::highest(); | |
| verify_makeGivens(Scalar(maximum / Scalar(2)), Scalar(maximum / Scalar(3))); | |
| JacobiRotation<Scalar> overflow, withoutR; | |
| Scalar r; | |
| overflow.makeGivens(maximum, maximum, &r); | |
| withoutR.makeGivens(maximum, maximum); | |
| VERIFY((numext::isinf)(r)); | |
| VERIFY_IS_EQUAL(overflow.c(), withoutR.c()); | |
| VERIFY_IS_EQUAL(overflow.s(), withoutR.s()); | |
| VERIFY(numext::abs(overflow.c() + overflow.s()) <= NumTraits<Scalar>::epsilon()); | |
| VERIFY(numext::abs(overflow.c() * overflow.c() + overflow.s() * overflow.s() - Scalar(1)) <= | |
| Scalar(8) * NumTraits<Scalar>::epsilon()); | |
| } | |
| // Complex inputs whose components are small integers times a subnormal scale, e.g. p = -4.9e-324i, q = 2e-323i. The | |
| // rotation must stay unitary and annihilate q although |p| and |q| carry only a few bits. | |
| template <typename Scalar> | |
| void jacobi_makegivens_complex_subnormal() { | |
| using RealScalar = typename NumTraits<Scalar>::Real; | |
| const RealScalar minimum = (std::numeric_limits<RealScalar>::min)(); | |
| const RealScalar maximum = (std::numeric_limits<RealScalar>::max)(); | |
| const RealScalar tiny = std::numeric_limits<RealScalar>::denorm_min(); | |
| const int qValues[] = {-4, -1, 0, 1, 4}; | |
| for (RealScalar scale : {minimum, RealScalar(minimum / RealScalar(16)), tiny}) { | |
| for (int pr = -2; pr <= 2; ++pr) { | |
| for (int pi = -2; pi <= 2; ++pi) { | |
| for (int qr : qValues) { | |
| for (int qi : qValues) { | |
| if (pr == 0 && pi == 0 && qr == 0 && qi == 0) continue; | |
| check_givens(Scalar(RealScalar(pr) * scale, RealScalar(pi) * scale), | |
| Scalar(RealScalar(qr) * scale, RealScalar(qi) * scale), scale); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| // Mixed scales: a subnormal p with a normal q, and |p| < |q| with |r| > max, where r overflows. | |
| const int units[][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; | |
| for (const auto& up : units) { | |
| for (const auto& uq : units) { | |
| const Scalar q = Scalar(RealScalar(uq[0]), RealScalar(uq[1])); | |
| check_givens(Scalar(RealScalar(up[0] - up[1]) * tiny, RealScalar(up[0] + up[1]) * tiny), q, RealScalar(1)); | |
| check_givens(Scalar(RealScalar(up[0]) * (maximum / 2), RealScalar(up[1]) * (maximum / 2)), q * maximum, maximum, | |
| false); | |
| } | |
| } | |
| // p = 0 where |Re q| + |Im q| overflows although |q| is finite. | |
| const RealScalar big = maximum / RealScalar(1.5); | |
| for (const Scalar q : {Scalar(big, big), Scalar(-big, big)}) check_givens(Scalar(0), q, big); | |
| // p != 0 where |Re p| + |Im p| or |Re q| + |Im q| overflows. half is a power of two, so (P, Q) stays exact. | |
| const RealScalar half = std::ldexp(RealScalar(1), std::numeric_limits<RealScalar>::max_exponent - 1); | |
| const auto turn = [](RealScalar re, RealScalar im, const int* u) { | |
| return Scalar(re * RealScalar(u[0]) - im * RealScalar(u[1]), re * RealScalar(u[1]) + im * RealScalar(u[0])); | |
| }; | |
| for (const auto& up : units) { | |
| for (const auto& uq : units) { | |
| check_givens(turn(1, 0, up), turn(half, half, uq), half); | |
| check_givens(turn(half, half, up), turn(1, 0, uq), half); | |
| check_givens(turn(half, -half, up), turn(half, half, uq), half, false); | |
| } | |
| } | |
| // q = 0 with p = 0 or p infinite gives c = 1, s = 0, r = p, as xLARTG does. | |
| for (const Scalar p : {Scalar(0), Scalar(NumTraits<RealScalar>::infinity(), RealScalar(1))}) { | |
| JacobiRotation<Scalar> rotation; | |
| Scalar r; | |
| rotation.makeGivens(p, Scalar(0), &r); | |
| VERIFY_IS_EQUAL(rotation.c(), Scalar(1)); | |
| VERIFY_IS_EQUAL(rotation.s(), Scalar(0)); | |
| VERIFY_IS_EQUAL(r, p); | |
| } | |
| } | |
| template <typename Scalar> | |
| EIGEN_DONT_INLINE void jacobi_makejacobi_bounded_ratio_impl(Scalar x, Scalar y, JacobiRotation<Scalar>& rotation) { | |
| rotation.makeJacobi(x, y, Scalar(0)); | |
| } | |
| template <typename Scalar> | |
| void jacobi_makejacobi_bounded_ratio() { | |
| #if defined(FE_OVERFLOW) && defined(FE_DIVBYZERO) && defined(FE_INVALID) | |
| const Scalar small = (std::numeric_limits<Scalar>::min)(); | |
| // The unused reciprocal either divides by zero, overflows, or overflows when squared. | |
| for (const Scalar x : {Scalar(0), small, Scalar(1)}) { | |
| for (const Scalar y : {small, Scalar(0.5)}) { | |
| JacobiRotation<Scalar> rotation; | |
| const int exceptions = fp_exceptions_raised_by(FE_OVERFLOW | FE_DIVBYZERO | FE_INVALID, | |
| [&] { jacobi_makejacobi_bounded_ratio_impl(x, y, rotation); }); | |
| if (exceptions >= 0) VERIFY_IS_EQUAL(exceptions, 0); | |
| const Scalar c = rotation.c(); | |
| const Scalar s = rotation.s(); | |
| VERIFY(numext::abs(c * c + s * s - Scalar(1)) <= Scalar(8) * NumTraits<Scalar>::epsilon()); | |
| } | |
| } | |
| #endif | |
| } | |
| template <typename Scalar> | |
| void jacobi_makejacobi_large_tau() { | |
| using std::abs; | |
| using std::sqrt; | |
| const Scalar rtmax = sqrt((std::numeric_limits<Scalar>::max)()); | |
| for (int factor = 1; factor <= 2; ++factor) { | |
| const Scalar deno = Scalar(1) / (Scalar(factor) * rtmax); | |
| const Scalar y = deno * Scalar(0.5); | |
| for (int delta_sign = -1; delta_sign <= 1; delta_sign += 2) { | |
| const Scalar x = delta_sign > 0 ? Scalar(1) : Scalar(0); | |
| const Scalar z = delta_sign > 0 ? Scalar(0) : Scalar(1); | |
| JacobiRotation<Scalar> rotation; | |
| rotation.makeJacobi(x, y, z); | |
| const Scalar offdiag = | |
| rotation.c() * rotation.s() * (x - z) + (rotation.c() * rotation.c() - rotation.s() * rotation.s()) * y; | |
| VERIFY(!numext::is_exactly_zero(rotation.s())); | |
| VERIFY(abs(offdiag) <= NumTraits<Scalar>::epsilon() * abs(y)); | |
| } | |
| } | |
| } | |
| template <typename Scalar> | |
| void jacobi_makejacobi_complex() { | |
| using RealScalar = typename NumTraits<Scalar>::Real; | |
| using std::abs; | |
| using std::sqrt; | |
| const RealScalar rtmax = sqrt((std::numeric_limits<RealScalar>::max)()); | |
| // Allow rounding in the rotation and in the complex off-diagonal residual. | |
| const RealScalar tolerance = RealScalar(16) * NumTraits<RealScalar>::epsilon(); | |
| for (const RealScalar magnitude : | |
| {RealScalar(0.5) / rtmax, RealScalar(0.25) / rtmax, RealScalar(0.25), RealScalar(0.5), RealScalar(1)}) { | |
| for (int real_sign : {-1, 1}) { | |
| for (int imag_sign : {-1, 1}) { | |
| const Scalar y(RealScalar(real_sign) * RealScalar(0.6) * magnitude, | |
| RealScalar(imag_sign) * RealScalar(0.8) * magnitude); | |
| for (int delta_sign : {-1, 0, 1}) { | |
| const RealScalar x = RealScalar(delta_sign); | |
| const RealScalar z = RealScalar(0); | |
| JacobiRotation<Scalar> rotation; | |
| VERIFY(rotation.makeJacobi(x, y, z)); | |
| const Scalar c = rotation.c(); | |
| const Scalar s = rotation.s(); | |
| VERIFY_IS_EQUAL(numext::imag(c), RealScalar(0)); | |
| VERIFY(numext::real(c) > RealScalar(0)); | |
| VERIFY(abs(s) > RealScalar(0)); | |
| VERIFY(abs(numext::abs2(c) + numext::abs2(s) - RealScalar(1)) <= tolerance); | |
| // (J* B J)(0,1), with J = [c conj(s); -s c] and B = [x y; conj(y) z]. | |
| const Scalar conjugate_s = numext::conj(s); | |
| const Scalar offdiag = c * conjugate_s * (x - z) + c * c * y - conjugate_s * conjugate_s * numext::conj(y); | |
| VERIFY(abs(offdiag) <= tolerance * abs(y)); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| template <typename Scalar> | |
| void jacobi_makejacobi_extreme_phase() { | |
| const Scalar tolerance = Scalar(8) * NumTraits<Scalar>::epsilon(); | |
| const Scalar expected = numext::sqrt(Scalar(0.5)); | |
| ScopedFlushToZero flushToZero; | |
| for (const Scalar magnitude : | |
| {(std::numeric_limits<Scalar>::max)() / Scalar(4), (std::numeric_limits<Scalar>::min)() * Scalar(4)}) { | |
| for (const Scalar sign : {Scalar(-1), Scalar(1)}) { | |
| JacobiRotation<Scalar> rotation; | |
| rotation.makeJacobi(Scalar(0), sign * magnitude, Scalar(0)); | |
| VERIFY(numext::abs(rotation.c() - expected) <= tolerance); | |
| VERIFY(numext::abs(rotation.s() - sign * expected) <= tolerance); | |
| } | |
| } | |
| } | |
| template <typename Scalar> | |
| void jacobi_makejacobi_ratio_boundaries() { | |
| const Scalar eps = NumTraits<Scalar>::epsilon(); | |
| // Allow rounding in the rotation and the scaled 2x2 residual. | |
| const Scalar tolerance = Scalar(8) * eps; | |
| for (const Scalar scale : {(std::numeric_limits<Scalar>::min)() * Scalar(4), Scalar(1), | |
| (std::numeric_limits<Scalar>::max)() / Scalar(8)}) { | |
| for (const Scalar ratio : {Scalar(0), eps, Scalar(0.5), Scalar(1) - eps, Scalar(1), Scalar(1) + eps, Scalar(2)}) { | |
| for (const Scalar sign : {Scalar(-1), Scalar(1)}) { | |
| JacobiRotation<Scalar> rotation; | |
| VERIFY(rotation.makeJacobi(scale * ratio, sign * scale * Scalar(0.5), Scalar(0))); | |
| const Scalar c = rotation.c(); | |
| const Scalar s = rotation.s(); | |
| VERIFY(c > Scalar(0)); | |
| VERIFY(numext::abs(c * c + s * s - Scalar(1)) <= tolerance); | |
| const Scalar residual = c * s * ratio + (c * c - s * s) * sign * Scalar(0.5); | |
| VERIFY(numext::abs(residual) <= tolerance); | |
| } | |
| } | |
| } | |
| } | |
| // The symmetrizing rotation of real_2x2_jacobi_svd uses t/d with t = m00 + m11 and d = m10 - m01. Its square | |
| // overflows for |d| << |t|, which used to yield the null rotation (c, s) = (0, 0). | |
| template <typename Scalar> | |
| void jacobi_real_2x2_svd_extreme_ratio() { | |
| const Scalar eps = NumTraits<Scalar>::epsilon(); | |
| const Scalar rtmax = numext::sqrt((std::numeric_limits<Scalar>::max)()); | |
| // With the smallest normal d and a diagonal of 100, t/d is infinite. | |
| for (const Scalar tiny : {(std::numeric_limits<Scalar>::min)(), (std::numeric_limits<Scalar>::min)() * Scalar(4), | |
| Scalar(0.25) / rtmax, Scalar(4) / rtmax, Scalar(0.125)}) { | |
| for (const Scalar sign : {Scalar(-1), Scalar(1)}) { | |
| for (const Scalar diagonal : {Scalar(-1), Scalar(1), Scalar(100)}) { | |
| Matrix<Scalar, 2, 2> m, a; | |
| m << diagonal, Scalar(0), sign * tiny, Scalar(2) * diagonal; | |
| JacobiRotation<Scalar> j_left, j_right; | |
| internal::real_2x2_jacobi_svd(m, Index(0), Index(1), &j_left, &j_right); | |
| VERIFY(numext::abs(j_left.c() * j_left.c() + j_left.s() * j_left.s() - Scalar(1)) <= Scalar(8) * eps); | |
| VERIFY(numext::abs(j_right.c() * j_right.c() + j_right.s() * j_right.s() - Scalar(1)) <= Scalar(8) * eps); | |
| a = m; | |
| a.applyOnTheLeft(0, 1, j_left); | |
| a.applyOnTheRight(0, 1, j_right); | |
| VERIFY(numext::abs(a(0, 1)) <= Scalar(16) * eps * m.norm()); | |
| VERIFY(numext::abs(a(1, 0)) <= Scalar(16) * eps * m.norm()); | |
| } | |
| } | |
| } | |
| } | |
| // The product of two const rotations must compile. | |
| template <typename Scalar> | |
| void jacobi_rotation_const() { | |
| const JacobiRotation<Scalar> a(Scalar(0.6), Scalar(0.8)), b(Scalar(0.8), Scalar(-0.6)); | |
| const JacobiRotation<Scalar> product = a * b; | |
| VERIFY_IS_APPROX(product.c(), a.c() * b.c() - numext::conj(a.s()) * b.s()); | |
| } | |
| EIGEN_DECLARE_TEST(jacobi) { | |
| for (int i = 0; i < g_repeat; i++) { | |
| CALL_SUBTEST_7((jacobi_makegivens_safe_scaling<float>())); | |
| CALL_SUBTEST_7((jacobi_makegivens_subnormal<float>())); | |
| CALL_SUBTEST_7((jacobi_makegivens_subnormal<double>())); | |
| CALL_SUBTEST_7((jacobi_makegivens_safe_scaling<double>())); | |
| CALL_SUBTEST_7((jacobi_makegivens_safe_scaling<long double>())); | |
| CALL_SUBTEST_7((jacobi_makejacobi_large_tau<float>())); | |
| CALL_SUBTEST_7((jacobi_makejacobi_large_tau<double>())); | |
| CALL_SUBTEST_7((jacobi_makejacobi_bounded_ratio<float>())); | |
| CALL_SUBTEST_7((jacobi_makejacobi_bounded_ratio<double>())); | |
| CALL_SUBTEST_7((jacobi_makejacobi_extreme_phase<float>())); | |
| CALL_SUBTEST_7((jacobi_makejacobi_extreme_phase<double>())); | |
| CALL_SUBTEST_7((jacobi_makejacobi_ratio_boundaries<float>())); | |
| CALL_SUBTEST_7((jacobi_real_2x2_svd_extreme_ratio<float>())); | |
| CALL_SUBTEST_7((jacobi_real_2x2_svd_extreme_ratio<double>())); | |
| CALL_SUBTEST_7((jacobi_real_2x2_svd_extreme_ratio<long double>())); | |
| CALL_SUBTEST_7((jacobi_rotation_const<float>())); | |
| CALL_SUBTEST_7((jacobi_rotation_const<std::complex<double>>())); | |
| CALL_SUBTEST_7((jacobi_makejacobi_ratio_boundaries<double>())); | |
| CALL_SUBTEST_7((jacobi_makejacobi_ratio_boundaries<long double>())); | |
| CALL_SUBTEST_7((jacobi_makejacobi_ratio_boundaries<half>())); | |
| CALL_SUBTEST_7((jacobi_makejacobi_ratio_boundaries<bfloat16>())); | |
| CALL_SUBTEST_8((jacobi_makejacobi_complex<std::complex<float>>())); | |
| CALL_SUBTEST_8((jacobi_makejacobi_complex<std::complex<double>>())); | |
| CALL_SUBTEST_8((jacobi_makegivens_complex_subnormal<std::complex<float>>())); | |
| CALL_SUBTEST_8((jacobi_makegivens_complex_subnormal<std::complex<double>>())); | |
| CALL_SUBTEST_1((jacobi<Matrix3f, float>())); | |
| CALL_SUBTEST_2((jacobi<Matrix4d, double>())); | |
| CALL_SUBTEST_3((jacobi<Matrix4cf, float>())); | |
| CALL_SUBTEST_3((jacobi<Matrix4cf, std::complex<float> >())); | |
| CALL_SUBTEST_1((jacobi<Matrix<float, 3, 3, RowMajor>, float>())); | |
| CALL_SUBTEST_2((jacobi<Matrix<double, 4, 4, RowMajor>, double>())); | |
| CALL_SUBTEST_3((jacobi<Matrix<std::complex<float>, 4, 4, RowMajor>, float>())); | |
| CALL_SUBTEST_3((jacobi<Matrix<std::complex<float>, 4, 4, RowMajor>, std::complex<float> >())); | |
| int r = internal::random<int>(2, internal::random<int>(1, EIGEN_TEST_MAX_SIZE) / 2), | |
| c = internal::random<int>(2, internal::random<int>(1, EIGEN_TEST_MAX_SIZE) / 2); | |
| CALL_SUBTEST_4((jacobi<MatrixXf, float>(MatrixXf(r, c)))); | |
| CALL_SUBTEST_5((jacobi<MatrixXcd, double>(MatrixXcd(r, c)))); | |
| CALL_SUBTEST_5((jacobi<MatrixXcd, std::complex<double> >(MatrixXcd(r, c)))); | |
| // complex<float> is really important to test as it is the only way to cover conjugation issues in certain unaligned | |
| // paths | |
| CALL_SUBTEST_6((jacobi<MatrixXcf, float>(MatrixXcf(r, c)))); | |
| CALL_SUBTEST_6((jacobi<MatrixXcf, std::complex<float> >(MatrixXcf(r, c)))); | |
| TEST_SET_BUT_UNUSED_VARIABLE(r); | |
| TEST_SET_BUT_UNUSED_VARIABLE(c); | |
| } | |
| } |