Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(solvers): drop quartic roots that do not satisfy the quartic (#4669)

Pranjal Mishra9b94a7bb72af
diff --git a/changelog.d/4669.fixed.md b/changelog.d/4669.fixed.md
new file mode 100644
index 000000000..0003ff9a6
--- /dev/null
+++ b/changelog.d/4669.fixed.md
@@ -0,0 +1,15 @@
+`solve_quartic` no longer returns values that do not satisfy the quartic it was given. When the
+resolvent cubic has a near-double root, `solve_cubic` can lose it, and Ferrari then factored a
+different polynomial and reported its roots: `(x^2 + 2x + 5)(x^2 + 2x + 5.01)`, which has no real
+roots, came back as `[1.0012, -3.0012, 1.0012, -3.0012]` with a residual of 64. Each candidate is now
+Newton-polished and checked against the quartic with a `sqrt(eps) / 4` tolerance for its dtype. A
+zero that stood for a complex pair is polished too, but kept only where Newton converged at a simple
+root, and a root reached by two candidates is reported once, from Ferrari's own candidate. Over
+50k-row families built from known roots, rows returning a non-root on the near-square family fall
+from 23,488 to 0 in float32 and from 10,727 to 0 in float64, no family gains a row with a value off
+its root, and the small root of a mixed-scale quartic that `main` misses is recovered (rows missing
+a root fall from 18,495 to 1,851 in float32). The residual is measured against the size of the
+quartic's terms with a floor of 1, as elsewhere in `solve_quartic`, so the check does not reach a
+quartic whose roots are all small: the near-square family scaled to roots of about 0.04 still
+returns non-roots in float32, and scaled to about 0.004 in float64. Affects float64 as well as
+float32. Fixes #4474.
diff --git a/kornia/geometry/solvers/polynomial_solver.py b/kornia/geometry/solvers/polynomial_solver.py
index 51198a8df..df8b361cc 100644
--- a/kornia/geometry/solvers/polynomial_solver.py
+++ b/kornia/geometry/solvers/polynomial_solver.py
@@ -230,6 +230,40 @@ def solve_cubic(coeffs: torch.Tensor) -> torch.Tensor:
return solutions
+def _quartic_root_residual_tol(dtype: torch.dtype) -> float:
+ """Largest scaled Horner residual a polished quartic candidate may leave and still count as a root.
+
+ Relative to the magnitude of the polynomial's terms at the candidate, which is what a Horner
+ evaluation cannot resolve below a few ``eps``. The candidates are Newton-polished before the
+ test, so a genuine root arrives near that floor and a value that is not a root of the quartic
+ at all cannot. ``sqrt(eps) / 4`` is 8.6e-5 in float32 and 3.7e-9 in float64. Measured on
+ 100k-row sweeps per family, genuine simple float32 roots leave at most 5.8e-6 after polishing and
+ float64 roots from an ill-conditioned cluster 3.7e-11. In float32 the margin is narrower at both
+ ends: the values Ferrari's collapsed factorisation produces in #4474 go down to about 5e-4, 6x the
+ tolerance, and a root next to a near-double root or inside a tight cluster, which Newton only
+ approaches linearly, can sit at the tolerance itself. Those margins are for Ferrari's own
+ candidates. A value polished from a complex-pair placeholder can stop inside the tolerance next to
+ two close roots, so ``solve_quartic`` also requires those to have converged.
+ """
+ return math.sqrt(torch.finfo(dtype).eps) / 4
+
+
+# Two accepted quartic candidates are the same root when they sit within each other's error bound:
+# the Newton step |p / p'| of a polished candidate at a simple root is a first-order estimate of its
+# distance to the nearest root, so a spurious repeat that stopped short of the root still reaches it,
+# while two distinct roots, however close, do not (a float64 quartic with roots 0.01 and 0.0109
+# keeps both). A candidate at a multiple root contributes nothing to the bound: p' vanishes there and
+# its Newton step is not an error estimate. The factor covers the first-order truncation and the
+# ulp floor, and was measured against 4, 16 and 64: 4 loses no root that 16 or 64 keep.
+_QUARTIC_COINCIDENCE_FACTOR = 4.0
+
+# A polished candidate whose derivative, relative to the size of the derivative's terms, exceeds this
+# sits at a simple root. Measured over 100k-row families: polished genuine double roots stay below
+# 1.1e-3 at the 99th percentile in float32 and below 1e-7 in float64, while a spurious repeat of a
+# simple root sits above 1e-2 at the 1st percentile in float32 and above 2e-2 in float64.
+_QUARTIC_SIMPLE_ROOT_DERIVATIVE = 1e-2
+
+
def solve_quartic(coeffs: torch.Tensor) -> torch.Tensor:
r"""Solve given quartic equation.
@@ -431,6 +465,107 @@ def solve_quartic(coeffs: torch.Tensor) -> torch.Tensor:
roots1 = solve_quadratic(torch.stack([q1_a, q1_b, q1_c], dim=1))
roots2 = solve_quadratic(torch.stack([q2_a, q2_b, q2_c], dim=1))
+ # Ferrari reports the roots of the factorization it built, not of the quartic it was
+ # given, and in float32 those can differ completely. When solve_cubic loses a near-double
+ # resolvent root -- which is exactly what a near-perfect-square quartic produces -- the
+ # best remaining candidate leaves R^2 < 0, R falls back to 0, E's radicand goes negative
+ # too, and both quadratics collapse to x^2 + (A/2)x + y/2. Its roots are then returned as
+ # the quartic's while leaving a residual of 25 and 64 on the two cases in #4474.
+ #
+ # So polish, then verify. Ferrari's arithmetic also loses digits to cancellation on ordinary
+ # quartics: a genuine, well-conditioned root can arrive thousands of ulps off (1.4e-5 on a root
+ # of 0.024 in float32, a scaled residual of 1.1e-4) and no fixed residual cutoff separates such
+ # a root from the collapsed-factorisation values above. Two Newton steps, each kept only when it
+ # lowers |p|, pull a genuine root to the evaluation floor; a value that is not a root of this
+ # quartic cannot be pulled there, and a step near a multiple root that would overshoot is not
+ # taken, so the polish never makes a candidate worse. The zeros solve_quadratic returns for a
+ # complex pair are polished too: when rounding sends a real pair's discriminant negative, the
+ # pair comes back as placeholders and Newton from zero recovers the root nearest zero (one real
+ # root of 1e-3 next to roots of 1 to 1e3, say). They are judged apart from Ferrari's own
+ # candidates below, because from zero Newton can also stop anywhere |p| is merely small.
+ root_candidates = torch.cat([roots1, roots2], dim=-1)
+ is_candidate = root_candidates != 0
+ A_e, B_e, C_e, D_e = (t.unsqueeze(-1) for t in (A, B, C, D))
+
+ def quartic(x: torch.Tensor) -> torch.Tensor:
+ return (((x + A_e) * x + B_e) * x + C_e) * x + D_e
+
+ def quartic_derivative(x: torch.Tensor) -> torch.Tensor:
+ return ((4.0 * x + 3.0 * A_e) * x + 2.0 * B_e) * x + C_e
+
+ root_residual = quartic(root_candidates)
+ for _ in range(2):
+ derivative = quartic_derivative(root_candidates)
+ can_step = derivative != 0
+ safe_derivative = torch.where(can_step, derivative, torch.ones_like(derivative))
+ stepped = torch.where(can_step, root_candidates - root_residual / safe_derivative, root_candidates)
+ stepped_residual = quartic(stepped)
+ improved = torch.abs(stepped_residual) < torch.abs(root_residual)
+ root_candidates = torch.where(improved, stepped, root_candidates)
+ root_residual = torch.where(improved, stepped_residual, root_residual)
+
+ # The derivative at each polished candidate, relative to the size of the derivative's terms,
+ # tells a simple root (p' clear of zero) from a multiple one; at a simple root |p / p'| is a
+ # first-order estimate of the distance to the root.
+ abs_root = torch.abs(root_candidates)
+ abs_derivative = torch.abs(quartic_derivative(root_candidates))
+ derivative_scale = torch.maximum(
+ torch.ones_like(root_candidates),
+ 4.0 * abs_root**3 + 3.0 * torch.abs(A_e) * abs_root**2 + 2.0 * torch.abs(B_e) * abs_root + torch.abs(C_e),
+ )
+ is_simple = abs_derivative / derivative_scale > _QUARTIC_SIMPLE_ROOT_DERIVATIVE
+ eps = torch.finfo(root_candidates.dtype).eps
+ root_error = torch.where(
+ is_simple,
+ torch.abs(root_residual) / torch.maximum(abs_derivative, eps * derivative_scale),
+ torch.zeros_like(root_candidates),
+ )
+
+ # Then drop the candidates that are not roots of the normalized quartic, using the zero
+ # placeholder this function already returns for a quartic with no real roots. The tolerance is
+ # relative to the size of the terms at the candidate: a cluster of close real roots is
+ # ill-conditioned in float32 and lands a long way from the true values while still satisfying
+ # the polynomial, and those stay.
+ root_residual_scale = torch.maximum(
+ torch.ones_like(root_candidates),
+ abs_root**4
+ + torch.abs(A_e) * abs_root**3
+ + torch.abs(B_e) * abs_root**2
+ + torch.abs(C_e) * abs_root
+ + torch.abs(D_e),
+ )
+ is_root = torch.abs(root_residual) / root_residual_scale <= _quartic_root_residual_tol(root_candidates.dtype)
+ # A polished placeholder is kept only where Newton converged, at a simple root: its remaining step
+ # |p / p'| must be within sqrt(eps) of |x| itself, the reach of a root the dtype can resolve. The
+ # bound is relative with no floor at 1, so a root near zero is held to the same standard; with a
+ # floor it becomes absolute there and admits a value 25% off a root at 1e-3. Two steps from zero
+ # can also stop inside the residual tolerance a tenth away from two close real roots, where |p|
+ # stays small over a wide interval; p' is small there too, and the step still to go is far larger.
+ recovered = is_simple & (root_error <= math.sqrt(eps) * abs_root)
+ is_root = is_root & (is_candidate | recovered)
+ root_candidates = torch.where(is_root, root_candidates, torch.zeros_like(root_candidates))
+
+ # Polishing can also carry a non-root onto a root the other quadratic already reports: when the
+ # quadratic that should have held the complex pair collapses to a double candidate near a real
+ # root, both copies converge onto it and a simple root comes back two or three times. A simple
+ # root cannot repeat, so a candidate that coincides with an earlier accepted one is dropped
+ # unless the root is multiple, which the derivative decides: at a multiple root p' vanishes
+ # with p, at a simple one it does not. Two genuine roots that merely lie close have a small
+ # derivative between them for the same reason and are both kept.
+ ulp_floor = eps * torch.maximum(abs_root.unsqueeze(-1), abs_root.unsqueeze(-2))
+ coincidence = _QUARTIC_COINCIDENCE_FACTOR * (root_error.unsqueeze(-1) + root_error.unsqueeze(-2) + ulp_floor)
+ coincide = torch.abs(root_candidates.unsqueeze(-1) - root_candidates.unsqueeze(-2)) <= coincidence
+ # Of two copies, the one kept is Ferrari's own candidate over a root recovered from a placeholder,
+ # which had only two steps from zero to get there while the other started at the root; between
+ # two of the same kind, the earlier one.
+ slot = torch.arange(4, device=root_candidates.device).expand_as(root_candidates)
+ rank = slot + 4 * (~is_candidate).to(slot.dtype)
+ outranked_by = rank.unsqueeze(-2) < rank.unsqueeze(-1)
+ accepted = root_candidates != 0
+ is_repeat = (coincide & outranked_by & accepted.unsqueeze(-1) & accepted.unsqueeze(-2)).any(dim=-1)
+ root_candidates = torch.where(is_repeat & is_simple, torch.zeros_like(root_candidates), root_candidates)
+ roots1, roots2 = root_candidates[:, :2], root_candidates[:, 2:]
+
solutions[mask_quartic, 0:2] = roots1.to(dtype=solutions.dtype)
solutions[mask_quartic, 2:4] = roots2.to(dtype=solutions.dtype)
diff --git a/tests/geometry/solvers/test_polynomial_solver.py b/tests/geometry/solvers/test_polynomial_solver.py
index 807a59c51..6ed92cb50 100644
--- a/tests/geometry/solvers/test_polynomial_solver.py
+++ b/tests/geometry/solvers/test_polynomial_solver.py
@@ -24,6 +24,17 @@ import kornia.geometry.solvers as solver
from testing.base import BaseTester
+def _monic_from_roots(roots: torch.Tensor) -> torch.Tensor:
+ """Expand prod (x - r_i) row by row into monic coefficients, highest power first, in float64."""
+ roots = roots.to(torch.float64)
+ coeffs = torch.ones(roots.shape[0], 1, dtype=torch.float64)
+ for i in range(roots.shape[1]):
+ r = roots[:, i : i + 1]
+ coeffs = torch.cat([coeffs, torch.zeros(roots.shape[0], 1, dtype=torch.float64)], dim=1)
+ coeffs[:, 1:] = coeffs[:, 1:] - r * coeffs[:, :-1]
+ return coeffs
+
+
class TestQuadraticSolver(BaseTester):
def test_smoke(self, device, dtype):
coeffs = torch.rand(1, 3, device=device, dtype=dtype)
@@ -742,6 +753,218 @@ class TestQuarticSolver(BaseTester):
assert bool(torch.isfinite(mixed.grad[0]).all()), mixed.grad
self.assert_close(mixed.grad[0], alone.grad[0])
+ def test_no_spurious_real_roots_for_near_square_quartic_4474(self, device, dtype):
+ # (x^2 + 2x + 5)(x^2 + 2x + 5.01) has no real roots, but its resolvent cubic has a
+ # near-double root that float32 solve_cubic loses. Ferrari then fell back to R = 0,
+ # both quadratics collapsed to x^2 + (A/2)x + y/2, and its roots came back as the
+ # quartic's while leaving a residual of 25 (#4474).
+ coeffs = torch.tensor([[1.0, 4.0, 14.01, 20.02, 25.05]], device=device, dtype=dtype)
+ roots = solver.solve_quartic(coeffs)
+
+ # Every returned value must satisfy the quartic it was given. Zeros are the
+ # placeholder for "no real root" and are skipped, which is the correct answer here.
+ for root in roots[0]:
+ if root == 0.0:
+ continue
+ residual = (((root + 4.0) * root + 14.01) * root + 20.02) * root + 25.05
+ assert bool(torch.abs(residual) < 1e-2), f"{root} is not a root, residual {residual}"
+
+ def test_near_square_family_returns_no_real_roots_4474(self, device, dtype):
+ # The same failure across the family rather than one literal, so a future change that
+ # reintroduces it on neighbouring coefficients is caught too. Each row is
+ # (x^2 + a x + b)(x^2 + a x + b + eps) with a discriminant that admits no real root.
+ rows, a = [], 2.0
+ for b in (5.0, 6.5, 8.0):
+ for eps in (1e-3, 1e-2, 1e-1):
+ c1 = b + eps
+ # expand (x^2 + a x + b)(x^2 + a x + c1)
+ rows.append([1.0, 2 * a, b + c1 + a * a, a * (b + c1), b * c1])
+ coeffs = torch.tensor(rows, device=device, dtype=dtype)
+ roots = solver.solve_quartic(coeffs)
+
+ assert bool((roots == 0.0).all()), f"expected the no-real-root placeholder, got {roots}"
+
+ def test_a_genuine_root_ferrari_left_off_is_polished_not_dropped_4474(self, device, dtype):
+ if dtype not in (torch.float32, torch.float64):
+ pytest.skip("Root accuracy assertions are limited to float32 and float64.")
+ # Four distinct real roots, nothing pathological. float32 Ferrari returns -0.02384 for the
+ # root at -0.023854, a scaled residual of 1.1e-4, and a fixed 1e-4 cutoff replaced it with
+ # the no-root placeholder (review of #4669). Polishing brings it onto the root instead.
+ coeffs = torch.tensor(
+ [[1.0, -0.3775281906, -71.65801239, -11.11165237, -0.2242867798]], device=device, dtype=dtype
+ )
+ expected = torch.tensor([[-8.19822634, -0.13136028, -0.02385378, 8.7309686]], device=device, dtype=dtype)
+ roots = torch.sort(solver.solve_quartic(coeffs), dim=-1).values
+ self.assert_close(roots, expected, rtol=1e-4, atol=1e-6)
+
+ def test_separated_real_roots_are_never_dropped_4474(self, device, dtype):
+ if dtype not in (torch.float32, torch.float64):
+ pytest.skip("Root accuracy assertions are limited to float32 and float64.")
+ # 2000 quartics with four real roots in [-10, 10] at least 0.5 apart: every root must come
+ # back within the dtype's reach of its true value, and none as the zero placeholder.
+ gen = torch.Generator().manual_seed(4474)
+ gaps = torch.rand(2000, 3, generator=gen, dtype=torch.float64) * 4 + 0.5
+ first = torch.rand(2000, 1, generator=gen, dtype=torch.float64) * (20 - gaps.sum(1, keepdim=True)) - 10
+ true_roots = torch.cat([first, first + gaps.cumsum(1)], dim=1)
+ coeffs = _monic_from_roots(true_roots)
+ roots = torch.sort(solver.solve_quartic(coeffs.to(device=device, dtype=dtype)), dim=-1).values
+
+ assert bool((roots != 0).all()), f"{int((roots == 0).sum())} roots replaced by the placeholder"
+ self.assert_close(roots, true_roots.to(device=device, dtype=dtype), rtol=1e-3, atol=1e-3)
+
+ def test_polish_does_not_repeat_a_simple_root_4474(self, device, dtype):
+ # Two real roots and a complex pair. The quadratic that should hold the pair collapses to a
+ # double candidate near the small real root, and polishing alone carried both copies onto
+ # it: 0.0816 came back three times. A simple root is reported once.
+ coeffs = torch.tensor([[1.0, 0.34461, -0.46098, 2.76681, -0.22301]], device=device, dtype=dtype)
+ roots = solver.solve_quartic(coeffs)
+ nonzero = roots[roots != 0]
+ assert nonzero.numel() == 2, f"expected the two real roots and two placeholders, got {roots}"
+ expected = torch.tensor([-1.666164, 0.081621], device=device, dtype=dtype)
+ self.assert_close(torch.sort(nonzero).values, expected, rtol=1e-3, atol=1e-4)
+
+ def test_close_distinct_simple_roots_are_both_kept_4474(self, device, dtype):
+ # Roots 0.01, 0.0109, 10, 20 (review of #4669): a fixed coincidence window took the two small
+ # roots for one and replaced 0.01 with the placeholder. The window is now each candidate's
+ # own error bound, which two distinct roots do not share however close they are.
+ if dtype not in (torch.float32, torch.float64):
+ pytest.skip("Root accuracy assertions are limited to float32 and float64.")
+ coeffs = torch.tensor([[1.0, -30.0209, 200.627109, -4.18327, 0.0218]], device=device, dtype=dtype)
+ roots = torch.sort(solver.solve_quartic(coeffs), dim=-1).values
+ expected = torch.tensor([[0.01, 0.0109, 10.0, 20.0]], device=device, dtype=dtype)
+ # float32 cannot separate the pair better than ~1%; float64 places both exactly.
+ tol = 2e-2 if dtype == torch.float32 else 1e-6
+ self.assert_close(roots, expected, rtol=tol, atol=0.0)
+
+ def test_double_root_is_still_reported_twice_4474(self, device, dtype):
+ # (x - 2)^2 (x + 1)(x + 3) and (x^2 - 1)^2: the repeat rule must leave a genuine double
+ # root alone, whether it comes from one quadratic or one copy from each.
+ coeffs = torch.tensor([[1.0, 0.0, -9.0, 4.0, 12.0], [1.0, 0.0, -2.0, 0.0, 1.0]], device=device, dtype=dtype)
+ roots = torch.sort(solver.solve_quartic(coeffs), dim=-1).values
+ expected = torch.tensor([[-3.0, -1.0, 2.0, 2.0], [-1.0, -1.0, 1.0, 1.0]], device=device, dtype=dtype)
+ self.assert_close(roots, expected, rtol=1e-3, atol=1e-3)
+
+ @pytest.mark.parametrize(
+ "coeffs, true_roots, dtypes",
+ [
+ # (x + 8.75)(x + 8.74)(x^2 - 2x + 17), and the same with roots -8.75 and -8.748. Polishing a
+ # complex-pair placeholder from zero stopped a tenth away from the close pair, inside the
+ # residual tolerance: -8.8777 twice, or -8.8840 four times.
+ ([1.0, 15.49, 58.495, 144.38, 1300.075], [-8.75, -8.74], (torch.float32, torch.float64)),
+ ([1.0, 15.498, 58.549, 144.376, 1301.265], [-8.75, -8.748], (torch.float32, torch.float64)),
+ # A recovered placeholder must have converged: accepting it at any simple root returns -1.10588
+ # for the near-double root at -1.1189.
+ (
+ [1.0, -2.4284734, -3.84090791, 6.128004724, 6.696065824],
+ [-1.11887, -1.11886, 2.02579, 2.64041],
+ (torch.float32, torch.float64),
+ ),
+ # ...relative to |x| itself: a bound of sqrt(eps) * max(1, |x|) is absolute below 1, and
+ # returns 0.0008457 for the root at 0.00112, 25% off.
+ (
+ [1.0, -6.807518769, -104.0549685, 0.2346873751, -0.0001323169874],
+ [-7.351354433, 0.001122449359, 0.001132718443, 14.15661803],
+ (torch.float32,),
+ ),
+ ],
+ )
+ def test_returned_values_are_roots_4474(self, coeffs, true_roots, dtypes, device, dtype):
+ if dtype not in dtypes:
+ pytest.skip("This case pins behaviour in another dtype.")
+ roots = solver.solve_quartic(torch.tensor([coeffs], device=device, dtype=dtype))[0]
+ want = torch.tensor(true_roots, device=device, dtype=dtype)
+ # Relative to the root itself, with no floor at 1, so a value near zero is held to the same standard.
+ rtol = 1e-2 if dtype == torch.float32 else 1e-6
+ for value in roots[roots != 0]:
+ assert bool(((want - value).abs() <= rtol * want.abs()).any()), f"{value} is not a root: {roots.tolist()}"
+
+ @pytest.mark.parametrize(
+ "coeffs, expected, dtypes",
+ [
+ # Each case pins one rule of the polish/filter/dedupe step: changing that rule makes it fail.
+ # Placeholders are judged apart from Ferrari's candidates: without it -8.8777 is returned twice.
+ ([1.0, 15.49, 58.495, 144.38, 1300.075], [-8.75, -8.74], (torch.float32, torch.float64)),
+ # Placeholders are polished: without it the root at 0.003 is lost next to roots of 1e2 to 1e3.
+ (
+ [1.0, -1088.503, -128841.7345, 329286.535, -986.7],
+ [-110.0, 0.003, 2.5, 1196.0],
+ (torch.float32, torch.float64),
+ ),
+ # The error bound comes only from simple roots: counting the double root drops 1.68.
+ (
+ _monic_from_roots(torch.tensor([[2.3, 2.3, -4.19, 1.68]], dtype=torch.float64))[0].tolist(),
+ [-4.19, 1.68, 2.3, 2.3],
+ (torch.float64,),
+ ),
+ # Coincidence factor 4, not 1: at 1 a second -1.5 survives (roots -2, -1.5, 4 +- 0.5i).
+ ([1.0, -4.5, -8.75, 32.875, 48.75], [-2.0, -1.5], (torch.float32, torch.float64)),
+ # Simple-root threshold 1e-2, not 1e-1: at 1e-1 -4031 is returned twice (roots -4031, -4689,
+ # 231 +- 875i). The smaller case (roots -9, -5, 1 +- 3i) guards the same repeat at -9.
+ (
+ [1.0, 8258.0, 15691705.0, -1590869938.0, 15479948400000.0],
+ [-4689.0, -4031.0],
+ (torch.float32, torch.float64),
+ ),
+ ([1.0, 12.0, 27.0, 50.0, 450.0], [-9.0, -5.0], (torch.float32, torch.float64)),
+ # ...and not 1e-3: at 1e-3 the double root at -2 loses a copy (roots -5, -2, -2, 2.25).
+ ([1.0, 6.75, 3.75, -34.0, -45.0], [-5.0, -2.0, -2.0, 2.25], (torch.float32, torch.float64)),
+ # The ulp floor in the coincidence window: without it 4.75 comes back twice (roots 4.75, -5, 9 +- 3i).
+ ([1.0, -17.75, 61.75, 450.0, -2137.5], [-5.0, 4.75], (torch.float32, torch.float64)),
+ # The residual tolerance, pinned from both sides. At sqrt(eps) instead of sqrt(eps) / 4, this
+ # quartic with two complex pairs returns -7.2066 and -6.7561 twice each...
+ ([1.0, 27.91975997, 297.7351036, 1435.935501, 2645.836994], [], (torch.float32,)),
+ # ...and at sqrt(eps) / 16 both copies of the double root at 0.558935 are dropped.
+ (
+ [1.0, -7.636022673, 0.9114557167, 5.439310611, -2.089195035],
+ [-0.901329, 0.558935, 0.558935, 7.419483],
+ (torch.float32,),
+ ),
+ # A recovered placeholder's step bound, from below: at eps * |x| instead of sqrt(eps) * |x| the
+ # root at 0.0009864 next to roots of 1 to 946 is lost.
+ (
+ [1.0, -1333.67141, 366869.5534, -369281.897, 363.9159878],
+ [0.0009864, 1.009293, 386.1998282, 946.4613022],
+ (torch.float32,),
+ ),
+ ],
+ )
+ def test_root_set_4474(self, coeffs, expected, dtypes, device, dtype):
+ if dtype not in dtypes:
+ pytest.skip("This case pins behaviour in another dtype.")
+ roots = solver.solve_quartic(torch.tensor([coeffs], device=device, dtype=dtype))[0]
+ found = torch.sort(roots[roots != 0]).values
+ assert found.numel() == len(expected), f"expected {expected}, got {roots.tolist()}"
+ # A double root in float32 lands ~sqrt(eps) apart; everything else here is far tighter.
+ tol = 1e-2 if dtype == torch.float32 else 1e-6
+ want = torch.tensor(sorted(expected), device=device, dtype=dtype)
+ self.assert_close(found, want, rtol=tol, atol=tol)
+
+ def test_ferrari_candidate_is_kept_over_a_recovered_copy_4474(self, device, dtype):
+ # A placeholder recovered to 2.288697 and Ferrari's own 2.289372 are copies of the root at
+ # 2.289375. The recovered one had two steps from zero and is 3e-4 off; keeping it by slot order
+ # threw away the accurate one. Ferrari's candidate is kept.
+ if dtype != torch.float32:
+ pytest.skip("The recovered copy arises in float32.")
+ coeffs = torch.tensor([[1.0, -7.111530047, 11.09636462, 16.30030766, -37.6143968]], device=device, dtype=dtype)
+ roots = solver.solve_quartic(coeffs)[0]
+ near = roots[(roots - 2.289375).abs() <= 1e-2 * 2.289375]
+ assert near.numel() == 1, f"expected one copy of 2.289375, got {roots.tolist()}"
+ assert abs(float(near) - 2.289375) <= 1e-5 * 2.289375, f"kept the less accurate copy: {float(near)}"
+
+ def test_four_real_roots_survive_the_residual_filter_4474(self, device, dtype):
+ # The guard rejects non-roots; it must not reject roots. A well-separated
+ # four-real-root quartic and a biquadratic both keep every root.
+ quartics = [
+ [1.0, -10.0, 35.0, -50.0, 24.0], # (x-1)(x-2)(x-3)(x-4)
+ [1.0, 0.0, -5.0, 0.0, 4.0], # (x^2-1)(x^2-4)
+ ]
+ expected = [[1.0, 2.0, 3.0, 4.0], [-2.0, -1.0, 1.0, 2.0]]
+ roots = solver.solve_quartic(torch.tensor(quartics, device=device, dtype=dtype))
+
+ for row, want in zip(roots, expected):
+ got = torch.sort(row).values
+ self.assert_close(got, torch.tensor(want, device=device, dtype=dtype).sort().values, rtol=1e-3, atol=1e-3)
+
@pytest.mark.parametrize("leading", [0.0, 1e-9, 5e-7])
def test_cubic_fallback_for_near_zero_leading_coefficient(self, leading, device, dtype):
# (x - 1)(x - 2)(x - 3) behind a leading coefficient below the 1e-6 cubic-fallback tolerance