From 5831579e97ced3fec0e00350687ec1f8fcbd2e15 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Mon, 26 Oct 2026 16:39:19 +0000 Subject: [PATCH] A polynomial system is not answered with some of its solutions missing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two polynomial systems over Q, each with only rational solutions, lost some of them: x*y; x + y^2 - 1 [[1, 0]] -> [[1, 0], [0, 1], [0, -1]] x*y + 3x - 5y - 15; x^2 - 3x - 4y - 10 [[1, -3], [2, -3]] -> [[5, 0], [1, -3], [2, -3]] The Gröbner path should answer both, but it declined them, and the eliminator answered instead. That solves x*y = 0 for y as 0 alone, and loses solutions. There were two defects. FGLM stored each new row of its echelon form as the monomial's normal form less the rows that reduced it, but recorded the combination as the monomial alone. Every lexicographic element read off through such a row got the wrong coefficients. For the first system the element for x came out x + y^2 instead of x + y^2 - 1, its root (0, 0) failed the check against the system, and the path declined. The combination is now the monomial less the combination of the rows that reduced it. Back-substitution read what was left after a root with one InnerSimplified pass, which does not cancel a term the root has made worth nothing. A basis that is not in shape position has elements whose leading coefficient vanishes at a root. In the second system x y + 3 x - 5 y - 15 at y = -3 stayed x * (-3) + 3 * x, still mentioned x, was taken as the equation for x, and solved to every number. Each equation left is now read as a polynomial over Q in the variables still to be assigned, where its coefficients are rational. A zero drops out, and a nonzero number ends the branch. Measured with a generated check: random systems where each equation is a product of two linear forms, one of them vanishing at a known integer point, sent to the Gröbner path alone. master FGLM fixed both fixed 3 unknowns, 1000 2 185 830 2 unknowns, 1000 174 516 821 No arm answered without the known point. Of the systems still declined in three unknowns, 165 have no univariate equation, and in those read that is a line of solutions. 4 decline on degree and 1 on quotient dimension. Cost: none measured. The five-unknown system with 120 solutions in GroebnerSystemTest takes 190-200 ms on master and 172-192 ms here, and the bounded-solve systems are level. Fixes https://github.com/asc-community/AngouriMath/issues/2126 Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 17 +++++ .../Functions/Algebra/Groebner/Fglm.cs | 8 +- .../Algebra/Groebner/GroebnerSystemSolver.cs | 29 +++++++- .../UnitTests/Algebra/GroebnerSystemTest.cs | 74 +++++++++++++++++++ 4 files changed, 126 insertions(+), 2 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 74fe3f736..fa097778d 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -42,6 +42,23 @@ A not-equal written `<>` is read as `not (a = b)`, and printed that way. ## Unreleased — since 2.6.0 +### A polynomial system is not answered with some of its solutions missing + +**Silent: a polynomial system that lost solutions has all of them.** `EquationSystem.Solve` triangularises a +polynomial system over the rationals with a Gröbner basis, and two defects made that path decline systems it should +have answered. The conversion to lexicographic order recorded each new row of its echelon form without the rows that +had reduced it, so `x * y, x + y^2 - 1` was triangularised with `x + y^2` in place of `x + y^2 - 1`. And an element +whose leading coefficient a root makes zero was not read as zero: `x y + 3 x - 5 y - 15` at `y = -3` stayed +`x * (-3) + 3 * x` and was solved for `x` as every number. A declined system went to the eliminator, which solves +`x * y = 0` for `y` as `0` alone and lost solutions. Both are fixed. A system the triangularisation now takes lists +its rows in the order it finds them, which can differ from the eliminator's +([#2126](https://github.com/asc-community/AngouriMath/issues/2126)). + +| Input | Was (2.6.0) | Now | +|---|---|---| +| `MathS.Equations("x*y", "x + y^2 - 1").Solve("x", "y")` | `[[1, 0]]` | `[[1, 0], [0, 1], [0, -1]]` | +| `MathS.Equations("x*y + 3*x - 5*y - 15", "x^2 - 3*x - 4*y - 10").Solve("x", "y")` | `[[1, -3], [2, -3]]` | `[[5, 0], [1, -3], [2, -3]]` | + ### A square system is not answered with rows that are no solution **Silent: a row that is no solution is gone, and a system with a row that fails for some values is `null`.** diff --git a/Sources/AngouriMath/Functions/Algebra/Groebner/Fglm.cs b/Sources/AngouriMath/Functions/Algebra/Groebner/Fglm.cs index f3c99b250..369b98324 100644 --- a/Sources/AngouriMath/Functions/Algebra/Groebner/Fglm.cs +++ b/Sources/AngouriMath/Functions/Algebra/Groebner/Fglm.cs @@ -203,10 +203,16 @@ internal static class Fglm continue; } + // The row stored is this monomial's normal form less the rows that reduced it, + // so the combination it stands for is the monomial less theirs. Recording the + // monomial alone gave every later element that leans on this row the wrong + // coefficients: x * y, x + y^2 - 1 came back as x + y^2, whose root (0, 0) + // is no solution, and the system fell back to an eliminator that lost two of + // its three solutions. staircase.Add(monomial); var unit = new ERational[staircase.Count]; for (var i = 0; i < unit.Length; i++) - unit[i] = ERational.Zero; + unit[i] = i < staircase.Count - 1 && i < combination.Length ? combination[i].Negate() : ERational.Zero; unit[staircase.Count - 1] = ERational.One; for (var row = 0; row < rowCombinations.Count; row++) diff --git a/Sources/AngouriMath/Functions/Algebra/Groebner/GroebnerSystemSolver.cs b/Sources/AngouriMath/Functions/Algebra/Groebner/GroebnerSystemSolver.cs index 44044cf00..a836c2d45 100644 --- a/Sources/AngouriMath/Functions/Algebra/Groebner/GroebnerSystemSolver.cs +++ b/Sources/AngouriMath/Functions/Algebra/Groebner/GroebnerSystemSolver.cs @@ -189,22 +189,49 @@ static bool BackSubstitute( if (univariate.SolveEquation(variable).InnerSimplified is not Set.FiniteSet roots) return budget.Allow(false, "unenumerable roots"); + var remaining = new Dictionary(at); + for (var i = 0; i < at; i++) + remaining[variables[i]] = i; + var still = variables.Take(at).ToList(); foreach (var root in roots) { assignment[at] = root; var narrowed = new List(equations.Count); + var consistent = true; foreach (var equation in equations) { - var substituted = equation.Substitute(variable, root).InnerSimplified; + var substituted = Canonical(equation.Substitute(variable, root).InnerSimplified, remaining, still); if (substituted.Vars.Any()) narrowed.Add(substituted); + else if (substituted is Number.Rational { IsZero: false }) + consistent = false; } + // A root that leaves an equation a nonzero number extends to no solution. + if (!consistent) + continue; if (!BackSubstitute(narrowed, variables, at - 1, assignment, found, budget)) return false; } return true; } + /// + /// as a polynomial over Q in the variables still to + /// be assigned, where it is one, so that a term the root has made worth nothing is gone. + /// + /// + /// A basis that is not in shape position has elements like x y + 3 x - 5 y - 15, + /// whose leading coefficient vanishes at a root: at y = -3 it is 0, and + /// says nothing about x. One structural pass leaves it x * (-3) + 3 * x, + /// which still mentions x, so it was taken as the equation to solve x + /// from, solved to every number, and the system was declined. Where a root is a + /// radical the coefficients are not rational, and it is left as it is. + /// + static Entity Canonical(Entity substituted, IReadOnlyDictionary remaining, IReadOnlyList still) + => MultivariatePolynomial.TryParse(substituted, remaining) is { } polynomial + ? polynomial.ToEntity(still) + : substituted; + /// /// Substitutes a candidate into the original equations and insists they come out /// exactly zero. diff --git a/Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs b/Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs index d04792acc..f241d6f95 100644 --- a/Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs +++ b/Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs @@ -10,6 +10,9 @@ using AngouriMath; using AngouriMath.Core; using AngouriMath.Core.Exceptions; +using AngouriMath.Extensions; +using AngouriMath.Functions; +using AngouriMath.Functions.Algebra.Groebner; using Xunit; using static AngouriMath.Entity; @@ -177,6 +180,77 @@ public void SystemsWithIrrationalSolutionsAreSolved(string first, string second, AssertEverySolutionSatisfies(equations, variables, solutions); } + /// + /// The basis of x * y, x + y^2 - 1 in degree-reverse-lexicographic order has + /// 1, y and x under its staircase, and converting it reaches + /// y^2 = 1 - x with the row for 1 already standing. The row kept for + /// y^2 is then -x, and recording it as y^2 alone made the element + /// for x come out as x + y^2, whose root (0, 0) is no solution. + /// + [Fact] + public void TheLexicographicBasisKeepsWhatARowWasReducedBy() + { + Variable[] variables = { "x", "y" }; + var index = new Dictionary { [variables[0]] = 0, [variables[1]] = 1 }; + var generators = new[] { "x * y", "x + y^2 - 1" } + .Select(text => MultivariatePolynomial.TryParse(text.ToEntity(), index)!) + .ToList(); + var budget = new GroebnerBudget(); + var basis = Buchberger.Compute(generators, MonomialOrder.DegreeReverseLexicographic, budget); + Assert.NotNull(basis); + var lexicographic = Fglm.ToLexicographic(basis!, variables.Length, budget); + Assert.NotNull(lexicographic); + + var elements = lexicographic!.Select(element => element.ToEntity(variables)).ToList(); + Assert.Equal(2, elements.Count); + foreach (var expected in new[] { "y^3 - y", "x + y^2 - 1" }) + Assert.Contains(elements, element => (element - expected.ToEntity()).Simplify() == 0); + } + + /// + /// The system the basis above belongs to. With the wrong element the triangularisation + /// was declined, and the eliminator that answered instead solved x * y = 0 for + /// y as 0 alone, which kept (1, 0) and lost (0, 1) and + /// (0, -1). + /// + [Fact] + public void ASystemWhoseBasisLeansOnAReducedRowIsSolvedWhole() + { + Entity[] equations = { "x * y", "x + y^2 - 1" }; + Variable[] variables = { "x", "y" }; + + var solutions = MathS.Equations(equations).Solve(variables); + + Assert.NotNull(solutions); + Assert.Equal(3, solutions!.RowCount); + AssertEverySolutionSatisfies(equations, variables, solutions); + foreach (var (x, y) in new[] { (1, 0), (0, 1), (0, -1) }) + Assert.Contains(Enumerable.Range(0, solutions.RowCount), + row => solutions[row, 0] == Number.Integer.Create(x) && solutions[row, 1] == Number.Integer.Create(y)); + } + + /// + /// A basis that is not in shape position. Its element x y + 3 x - 5 y - 15 is + /// 0 at y = -3 but was left x * (-3) + 3 * x, which still mentions + /// x, so it was the equation x was solved from there. That solved to every + /// number, the triangularisation was declined, and the eliminator lost (5, 0). + /// + [Fact] + public void ARootThatZeroesALeadingCoefficientUsesTheNextElement() + { + Entity[] equations = { "x * y + 3 * x - 5 * y - 15", "x^2 - 3 * x - 4 * y - 10" }; + Variable[] variables = { "x", "y" }; + + var solutions = MathS.Equations(equations).Solve(variables); + + Assert.NotNull(solutions); + Assert.Equal(3, solutions!.RowCount); + AssertEverySolutionSatisfies(equations, variables, solutions); + foreach (var (x, y) in new[] { (5, 0), (1, -3), (2, -3) }) + Assert.Contains(Enumerable.Range(0, solutions.RowCount), + row => solutions[row, 0] == Number.Integer.Create(x) && solutions[row, 1] == Number.Integer.Create(y)); + } + /// /// The answers stay exact rather than being handed back as decimals — which is the /// point of triangularising rather than evaluating.