Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): keep scaled quartics with large roots off solve_quartic's cubic fallback (#4954) (#5334)

Arij Belmabrouk76999b9dac1c
diff --git a/changelog.d/5334.breaking.md b/changelog.d/5334.breaking.md
new file mode 100644
index 000000000..197e39d8f
--- /dev/null
+++ b/changelog.d/5334.breaking.md
@@ -0,0 +1,6 @@
+`solve_quartic` now solves a row as a cubic only when its leading coefficient is below the tolerance and the
+scale-invariant root bound `max(|b/a|, |c/a|^(1/2), |d/a|^(1/3), |e/a|^(1/4))` exceeds `1 / tol`, so a quartic
+whose roots are all below `1 / (4 * tol)` in magnitude is solved as a quartic at every scale. Rows such as
+`[1e-7, 0, 0, 0, -1]` previously returned `[0, 0, 0, 0]` and now return their real roots `Β±56.23`;
+`(x-50)(x-60)(x-70)(x-80)` scaled by `2^-21` in float32 now returns all four roots instead of a single root
+near 31.5. (#4954)
diff --git a/kornia/geometry/solvers/polynomial_solver.py b/kornia/geometry/solvers/polynomial_solver.py
index 35b03faa9..563e8a4c2 100644
--- a/kornia/geometry/solvers/polynomial_solver.py
+++ b/kornia/geometry/solvers/polynomial_solver.py
@@ -491,14 +491,12 @@ def solve_quartic(coeffs: torch.Tensor) -> torch.Tensor:
- Coefficient layout and zero padding as :func:`solve_quadratic`; the roots are unordered.
- Known defects: the solver is not scale-invariant, so a quartic whose roots are all small can lose real
roots and return values that are not roots (`#4833 <https://github.com/kornia/kornia/issues/4833>`_).
- - A row is solved as the cubic of its last four coefficients when its leading coefficient is 0 or smaller in
- magnitude than ``1e-6`` (``1e-12`` in float64) times ``min(1, max_i |coeffs_i|)``. For a row whose largest
- coefficient is at most 1 the test is relative, so scaling the row down does not change how it is solved;
- at unit scale and above it is absolute, as before.
- - Known defects: a quartic with large roots has a leading coefficient that is small next to its largest
- coefficient. Scaled so that the leading coefficient drops below the tolerance while the largest coefficient
- is still above 1, it is solved as a cubic and its roots are lost
- (`#4954 <https://github.com/kornia/kornia/issues/4954>`_).
+ - A row is solved as the cubic of its last four coefficients when its leading coefficient is 0, or when both
+ hold: ``|a|`` is smaller than ``1e-6`` (``1e-12`` in float64) times ``min(1, max_i |coeffs_i|)``, and the
+ scale-invariant root bound ``max(|b/a|, |c/a|^(1/2), |d/a|^(1/3), |e/a|^(1/4))`` exceeds ``1 / tol``
+ (tested as ``|coeffs_k| > |a| / tol^k``, without dividing by ``a``). Since the bound is at most 4 times
+ the largest root's magnitude, a quartic whose roots are all smaller than ``1 / (4 * tol)`` stays a
+ quartic at every scale; the bound can only move a row from the cubic path to the quartic path.
Args:
coeffs : The coefficients quartic equation : `(B, 5)`
@@ -530,6 +528,7 @@ def solve_quartic(coeffs: torch.Tensor) -> torch.Tensor:
# Coefficients
a = coeffs[:, 0]
+ b, c, d, e = coeffs[:, 1], coeffs[:, 2], coeffs[:, 3], coeffs[:, 4]
solutions = torch.zeros((len(coeffs), 4), device=coeffs.device, dtype=coeffs.dtype)
@@ -537,10 +536,19 @@ def solve_quartic(coeffs: torch.Tensor) -> torch.Tensor:
# float32's cubic-fallback tolerance; float64's 1e-12 would also round to 0 in float16.
zero_tol = 1e-6 if coeffs.dtype in (torch.float32, torch.float16, torch.bfloat16) else 1e-12
- # Cubic fallback for a approx 0. Scaling a row does not move its roots, so a row whose largest coefficient is
- # below 1 gets a proportionally smaller tolerance; rows at unit scale or above keep the absolute one.
+ # Cubic fallback for a approx 0. The first test is relative below unit scale and absolute above it, which on
+ # its own sends a scaled quartic whose roots are well below 1 / zero_tol to the cubic. The second is the
+ # scale-invariant root bound max(|b/a|, |c/a|^(1/2), |d/a|^(1/3), |e/a|^(1/4)) > 1 / zero_tol, written as
+ # |coeffs_k| > |a| / zero_tol**k so it never divides by a. ANDed in, it can only move a row to the quartic
+ # path. If |a| / zero_tol**k overflows, the bound exceeds any representable coefficient, so False is correct.
row_scale = coeffs.abs().amax(dim=-1).clamp(max=1.0)
- mask_a_zero = (torch.abs(a) < zero_tol * row_scale) | (a == 0)
+ bound = (
+ (b.abs() > a.abs() / zero_tol)
+ | (c.abs() > a.abs() / zero_tol**2)
+ | (d.abs() > a.abs() / zero_tol**3)
+ | (e.abs() > a.abs() / zero_tol**4)
+ )
+ mask_a_zero = ((torch.abs(a) < zero_tol * row_scale) & bound) | (a == 0)
mask_quartic = ~mask_a_zero
if torch.any(mask_a_zero):
diff --git a/tests/geometry/solvers/test_polynomial_solver.py b/tests/geometry/solvers/test_polynomial_solver.py
index b8664b564..6438a918e 100644
--- a/tests/geometry/solvers/test_polynomial_solver.py
+++ b/tests/geometry/solvers/test_polynomial_solver.py
@@ -1431,19 +1431,67 @@ class TestConventionPolynomialSolvers(BaseTester):
for root in (1e-3, 2e-3):
assert (out - root).abs().min() <= 1e-5 * root, out
- def test_wart_solve_quartic_scaled_large_roots_fall_back_to_cubic_4954(self, device, dtype):
+ def test_convention_solve_quartic_scale_invariant_large_roots_4954(self, device, dtype):
if dtype != torch.float32:
pytest.skip(
"pinned in float32, where this row's 6e-8 ratio of leading to largest coefficient is below 1e-6"
)
- # #4954: (x - 50)(x - 60)(x - 70)(x - 80) times 2^-21 (exact in float32) has a leading coefficient 4.8e-7 and
- # a largest coefficient 8.01. The fallback tolerance is only relative below unit scale, so the row is solved
- # as the cubic of its last four coefficients: one real root near 31.5 and none of 50, 60, 70, 80.
+ # #4954: (x - 50)(x - 60)(x - 70)(x - 80) times 2^-21 has a = 4.8e-7, below 1e-6 (its largest coefficient
+ # is 8.01, so the old test is absolute), but its root bound is 260, so it stays a quartic. Today's error is
+ # at most 1.5e-3; a cubic fallback misses by at least 18.
row = torch.tensor([[1.0, -260.0, 25100.0, -1066000.0, 16800000.0]], device=device, dtype=dtype)
- out = solver.solve_quartic(row * 2.0**-21)
- self.assert_close(out[:, 1:], torch.zeros(1, 3, device=device, dtype=dtype))
- for root in (50.0, 60.0, 70.0, 80.0):
- assert (out - root).abs().min() > 1.0
+ out = solver.solve_quartic(row * 2.0**-21).sort(dim=-1).values
+ expected = torch.tensor([[50.0, 60.0, 70.0, 80.0]], device=device, dtype=dtype)
+ self.assert_close(out, expected, atol=1e-2, rtol=0.0)
+
+ def test_convention_solve_quartic_scale_invariant_large_roots_float64_4954(self, device, dtype):
+ if dtype != torch.float64:
+ pytest.skip("the float32 case is pinned in test_convention_solve_quartic_scale_invariant_large_roots_4954")
+ # Same shape as the float32 pin, at float64's 1e-12 tolerance: (x - 1e4)...(x - 4e4) scaled by 2^-50 has
+ # a = 8.9e-16, below 1e-12 (largest coefficient 213, so the old test is absolute), but its root bound is
+ # 1e5, so it stays a quartic. Today's error is at most 4.4e-11; a cubic fallback misses by at least 946.
+ row = torch.tensor([[1.0, -100000.0, 3500000000.0, -50000000000000.0, 2.4e17]], device=device, dtype=dtype)
+ out = solver.solve_quartic(row * 2.0**-50).sort(dim=-1).values
+ expected = torch.tensor([[10000.0, 20000.0, 30000.0, 40000.0]], device=device, dtype=dtype)
+ self.assert_close(out, expected, atol=1e-6, rtol=1e-9)
+
+ def test_convention_solve_quartic_tiny_leading_coefficient_real_roots_4954(self, device, dtype):
+ if dtype != torch.float32:
+ pytest.skip("pinned in float32, where the old absolute tolerance (1e-6) is what this row crosses")
+ # #4954: x^4 - 1 times 1e-7 has a = 1e-7, below 1e-6, but its root bound is 56.23 (|e/a|^(1/4)), so it
+ # stays a quartic, recovering the real roots +-56.23. Today's error is at most 3.8e-6; a cubic
+ # fallback misses by 56.23 (both roots lost to [0, 0, 0, 0]).
+ coeffs = torch.tensor([[1e-7, 0.0, 0.0, 0.0, -1.0]], device=device, dtype=dtype)
+ out = solver.solve_quartic(coeffs)
+ root = 1e7**0.25
+ for expected_root in (-root, root):
+ assert (out - expected_root).abs().min() <= 1e-4 * abs(expected_root)
+
+ def test_convention_solve_quartic_and_rule_keeps_dominant_root_4954(self, device, dtype):
+ if dtype != torch.float32:
+ pytest.skip("pinned in float32; this row's unit-scale absolute test is what the AND rule must not override")
+ # #4954: this row has a = 1e-5, above 1e-6, so the old test alone already keeps it a quartic, finding
+ # its root near -1e7. Its root bound is 1e7 (b/a): the bound ALONE, not ANDed to the old test, would
+ # wrongly fall back here. Today's error is at most 5.4e-7; a bound-alone regression loses the -1e7 root.
+ coeffs = torch.tensor([[1e-5, 100.0, -50.0, 3.0, -1.0]], device=device, dtype=dtype)
+ out = solver.solve_quartic(coeffs)
+ assert (out - (-10000000.5)).abs().min() <= 10.0
+ assert (out - 0.48086).abs().min() <= 1e-5 * 0.48086
+
+ def test_convention_solve_quartic_roots_below_quarter_inverse_tolerance_any_scale_4954(self, device, dtype):
+ if dtype not in (torch.float32, torch.float64):
+ pytest.skip("the constant term of these rows overflows float16 and keeps 3 digits in bfloat16")
+ # The root bound is at most 4 times the largest root, so roots all below 1 / (4 * tol) (2.5e5 in float32,
+ # 2.5e11 in float64) keep a row on the quartic path at every scale, here down to a leading coefficient of
+ # 8e-25. Each term of the bound crosses 1 / tol here if its power of tol is off by one, and the row then
+ # falls back to the cubic, as the leading-coefficient test alone does once a drops below tol (#4954).
+ unit = 1.0 if dtype == torch.float32 else 1e6
+ roots = [-2.4e5 * unit, -1.5e5 * unit, 1e5 * unit, 2e5 * unit]
+ monic = [1.0, 9e4 * unit, -6.1e10 * unit**2, -3e15 * unit**3, 7.2e20 * unit**4]
+ coeffs = torch.tensor([[c * 2.0**-k for c in monic] for k in (0, 20, 40, 60, 80)], device=device, dtype=dtype)
+ out = solver.solve_quartic(coeffs).sort(dim=-1).values
+ expected = torch.tensor([roots] * 5, device=device, dtype=dtype)
+ self.assert_close(out, expected, rtol=1e-4, atol=0.0)
def test_convention_solve_quartic_relative_leading_tolerance_4905(self, device, dtype):
# (x - 1)(x - 2)(x - 3)(x - 4), and the same row times a power of two (exact in every dtype) that brings the