Skip to content

Commit 4b0a97e

Browse files
A polynomial system is not answered with some of its solutions missing (#2127)
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 #2126 Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent c2a75a9 commit 4b0a97e

4 files changed

Lines changed: 126 additions & 2 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ A not-equal written `<>` is read as `not (a = b)`, and printed that way.
4242

4343
## Unreleased — since 2.6.0
4444

45+
### A polynomial system is not answered with some of its solutions missing
46+
47+
**Silent: a polynomial system that lost solutions has all of them.** `EquationSystem.Solve` triangularises a
48+
polynomial system over the rationals with a Gröbner basis, and two defects made that path decline systems it should
49+
have answered. The conversion to lexicographic order recorded each new row of its echelon form without the rows that
50+
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
51+
whose leading coefficient a root makes zero was not read as zero: `x y + 3 x - 5 y - 15` at `y = -3` stayed
52+
`x * (-3) + 3 * x` and was solved for `x` as every number. A declined system went to the eliminator, which solves
53+
`x * y = 0` for `y` as `0` alone and lost solutions. Both are fixed. A system the triangularisation now takes lists
54+
its rows in the order it finds them, which can differ from the eliminator's
55+
([#2126](https://github.com/asc-community/AngouriMath/issues/2126)).
56+
57+
| Input | Was (2.6.0) | Now |
58+
|---|---|---|
59+
| `MathS.Equations("x*y", "x + y^2 - 1").Solve("x", "y")` | `[[1, 0]]` | `[[1, 0], [0, 1], [0, -1]]` |
60+
| `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]]` |
61+
4562
### A square system is not answered with rows that are no solution
4663

4764
**Silent: a row that is no solution is gone, and a system with a row that fails for some values is `null`.**

‎Sources/AngouriMath/Functions/Algebra/Groebner/Fglm.cs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,16 @@ internal static class Fglm
203203
continue;
204204
}
205205

206+
// The row stored is this monomial's normal form less the rows that reduced it,
207+
// so the combination it stands for is the monomial less theirs. Recording the
208+
// monomial alone gave every later element that leans on this row the wrong
209+
// coefficients: x * y, x + y^2 - 1 came back as x + y^2, whose root (0, 0)
210+
// is no solution, and the system fell back to an eliminator that lost two of
211+
// its three solutions.
206212
staircase.Add(monomial);
207213
var unit = new ERational[staircase.Count];
208214
for (var i = 0; i < unit.Length; i++)
209-
unit[i] = ERational.Zero;
215+
unit[i] = i < staircase.Count - 1 && i < combination.Length ? combination[i].Negate() : ERational.Zero;
210216
unit[staircase.Count - 1] = ERational.One;
211217

212218
for (var row = 0; row < rowCombinations.Count; row++)

‎Sources/AngouriMath/Functions/Algebra/Groebner/GroebnerSystemSolver.cs‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,22 +189,49 @@ static bool BackSubstitute(
189189
if (univariate.SolveEquation(variable).InnerSimplified is not Set.FiniteSet roots)
190190
return budget.Allow(false, "unenumerable roots");
191191

192+
var remaining = new Dictionary<Variable, int>(at);
193+
for (var i = 0; i < at; i++)
194+
remaining[variables[i]] = i;
195+
var still = variables.Take(at).ToList();
192196
foreach (var root in roots)
193197
{
194198
assignment[at] = root;
195199
var narrowed = new List<Entity>(equations.Count);
200+
var consistent = true;
196201
foreach (var equation in equations)
197202
{
198-
var substituted = equation.Substitute(variable, root).InnerSimplified;
203+
var substituted = Canonical(equation.Substitute(variable, root).InnerSimplified, remaining, still);
199204
if (substituted.Vars.Any())
200205
narrowed.Add(substituted);
206+
else if (substituted is Number.Rational { IsZero: false })
207+
consistent = false;
201208
}
209+
// A root that leaves an equation a nonzero number extends to no solution.
210+
if (!consistent)
211+
continue;
202212
if (!BackSubstitute(narrowed, variables, at - 1, assignment, found, budget))
203213
return false;
204214
}
205215
return true;
206216
}
207217

218+
/// <summary>
219+
/// <paramref name="substituted"/> as a polynomial over <c>Q</c> in the variables still to
220+
/// be assigned, where it is one, so that a term the root has made worth nothing is gone.
221+
/// </summary>
222+
/// <remarks>
223+
/// A basis that is not in shape position has elements like <c>x y + 3 x - 5 y - 15</c>,
224+
/// whose leading coefficient vanishes at a root: at <c>y = -3</c> it is <c>0</c>, and
225+
/// says nothing about <c>x</c>. One structural pass leaves it <c>x * (-3) + 3 * x</c>,
226+
/// which still mentions <c>x</c>, so it was taken as the equation to solve <c>x</c>
227+
/// from, solved to every number, and the system was declined. Where a root is a
228+
/// radical the coefficients are not rational, and it is left as it is.
229+
/// </remarks>
230+
static Entity Canonical(Entity substituted, IReadOnlyDictionary<Variable, int> remaining, IReadOnlyList<Variable> still)
231+
=> MultivariatePolynomial.TryParse(substituted, remaining) is { } polynomial
232+
? polynomial.ToEntity(still)
233+
: substituted;
234+
208235
/// <summary>
209236
/// Substitutes a candidate into the original equations and insists they come out
210237
/// exactly zero.

‎Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
using AngouriMath;
1111
using AngouriMath.Core;
1212
using AngouriMath.Core.Exceptions;
13+
using AngouriMath.Extensions;
14+
using AngouriMath.Functions;
15+
using AngouriMath.Functions.Algebra.Groebner;
1316
using Xunit;
1417
using static AngouriMath.Entity;
1518

@@ -177,6 +180,77 @@ public void SystemsWithIrrationalSolutionsAreSolved(string first, string second,
177180
AssertEverySolutionSatisfies(equations, variables, solutions);
178181
}
179182

183+
/// <summary>
184+
/// The basis of <c>x * y, x + y^2 - 1</c> in degree-reverse-lexicographic order has
185+
/// <c>1</c>, <c>y</c> and <c>x</c> under its staircase, and converting it reaches
186+
/// <c>y^2 = 1 - x</c> with the row for <c>1</c> already standing. The row kept for
187+
/// <c>y^2</c> is then <c>-x</c>, and recording it as <c>y^2</c> alone made the element
188+
/// for <c>x</c> come out as <c>x + y^2</c>, whose root <c>(0, 0)</c> is no solution.
189+
/// </summary>
190+
[Fact]
191+
public void TheLexicographicBasisKeepsWhatARowWasReducedBy()
192+
{
193+
Variable[] variables = { "x", "y" };
194+
var index = new Dictionary<Variable, int> { [variables[0]] = 0, [variables[1]] = 1 };
195+
var generators = new[] { "x * y", "x + y^2 - 1" }
196+
.Select(text => MultivariatePolynomial.TryParse(text.ToEntity(), index)!)
197+
.ToList();
198+
var budget = new GroebnerBudget();
199+
var basis = Buchberger.Compute(generators, MonomialOrder.DegreeReverseLexicographic, budget);
200+
Assert.NotNull(basis);
201+
var lexicographic = Fglm.ToLexicographic(basis!, variables.Length, budget);
202+
Assert.NotNull(lexicographic);
203+
204+
var elements = lexicographic!.Select(element => element.ToEntity(variables)).ToList();
205+
Assert.Equal(2, elements.Count);
206+
foreach (var expected in new[] { "y^3 - y", "x + y^2 - 1" })
207+
Assert.Contains(elements, element => (element - expected.ToEntity()).Simplify() == 0);
208+
}
209+
210+
/// <summary>
211+
/// The system the basis above belongs to. With the wrong element the triangularisation
212+
/// was declined, and the eliminator that answered instead solved <c>x * y = 0</c> for
213+
/// <c>y</c> as <c>0</c> alone, which kept <c>(1, 0)</c> and lost <c>(0, 1)</c> and
214+
/// <c>(0, -1)</c>.
215+
/// </summary>
216+
[Fact]
217+
public void ASystemWhoseBasisLeansOnAReducedRowIsSolvedWhole()
218+
{
219+
Entity[] equations = { "x * y", "x + y^2 - 1" };
220+
Variable[] variables = { "x", "y" };
221+
222+
var solutions = MathS.Equations(equations).Solve(variables);
223+
224+
Assert.NotNull(solutions);
225+
Assert.Equal(3, solutions!.RowCount);
226+
AssertEverySolutionSatisfies(equations, variables, solutions);
227+
foreach (var (x, y) in new[] { (1, 0), (0, 1), (0, -1) })
228+
Assert.Contains(Enumerable.Range(0, solutions.RowCount),
229+
row => solutions[row, 0] == Number.Integer.Create(x) && solutions[row, 1] == Number.Integer.Create(y));
230+
}
231+
232+
/// <summary>
233+
/// A basis that is not in shape position. Its element <c>x y + 3 x - 5 y - 15</c> is
234+
/// <c>0</c> at <c>y = -3</c> but was left <c>x * (-3) + 3 * x</c>, which still mentions
235+
/// <c>x</c>, so it was the equation <c>x</c> was solved from there. That solved to every
236+
/// number, the triangularisation was declined, and the eliminator lost <c>(5, 0)</c>.
237+
/// </summary>
238+
[Fact]
239+
public void ARootThatZeroesALeadingCoefficientUsesTheNextElement()
240+
{
241+
Entity[] equations = { "x * y + 3 * x - 5 * y - 15", "x^2 - 3 * x - 4 * y - 10" };
242+
Variable[] variables = { "x", "y" };
243+
244+
var solutions = MathS.Equations(equations).Solve(variables);
245+
246+
Assert.NotNull(solutions);
247+
Assert.Equal(3, solutions!.RowCount);
248+
AssertEverySolutionSatisfies(equations, variables, solutions);
249+
foreach (var (x, y) in new[] { (5, 0), (1, -3), (2, -3) })
250+
Assert.Contains(Enumerable.Range(0, solutions.RowCount),
251+
row => solutions[row, 0] == Number.Integer.Create(x) && solutions[row, 1] == Number.Integer.Create(y));
252+
}
253+
180254
/// <summary>
181255
/// The answers stay exact rather than being handed back as decimals — which is the
182256
/// point of triangularising rather than evaluating.

0 commit comments

Comments
 (0)