Skip to content

Commit d6e8427

Browse files
A Gröbner root in decimals is checked to its precision (#2322)
The Gröbner triangularisation kept a tuple only where every equation came back exactly zero with the tuple put in. A root of a univariate factor with no closed form is found numerically, and no structural pass makes its residual zero, so one such root declined the whole system. The eliminator it fell back to answered in decimals and could lose solutions: x^2 + y^2 = 2, x^3 - y = -2 eliminates to (x + 1) times a quintic with no roots in radicals, and 2.6.0 and master both answered its four complex solutions without either real one, (-1, 1) and (-1.3455, -0.4356). A tuple with a decimal in it is now kept where each equation's residual is under 1E-10 of the largest term the equation adds up to. A tuple that is no solution, as a basis out of shape position can give, misses by whole quantities and fails this as it fails the exact test. An exact tuple is still checked exactly, and the decimal test is made only once the exact one has failed. An entry in decimals is listed as the number it evaluates to, rather than as the polynomial in decimals back-substitution wrote it as. Generated systems with known solutions (e387 syscheck, 1190 systems): known solutions missing 10 -> 7, wrong rows 0 -> 0. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 43ed2f7 commit d6e8427

3 files changed

Lines changed: 90 additions & 8 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,21 @@ its rows in the order it finds them, which can differ from the eliminator's
484484
| `MathS.Equations("x*y", "x + y^2 - 1").Solve("x", "y")` | `[[1, 0]]` | `[[1, 0], [0, 1], [0, -1]]` |
485485
| `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]]` |
486486

487+
### A polynomial system with roots in decimals keeps its exact solutions
488+
489+
**Silent: a polynomial system whose elimination has roots with no closed form has all its solutions, the rational ones
490+
exactly.** The Gröbner triangularisation puts every tuple it finds back into the system and keeps it only where each
491+
equation is exactly zero, which a root found numerically never is. One such root sent the whole system to the
492+
eliminator, which answered in decimals and could lose solutions: `x^2 + y^2 = 2, x^3 - y = -2` eliminates to
493+
`(x + 1)` times a quintic with no roots in radicals, and both of its real solutions were missing. A tuple in decimals is
494+
now kept where each equation's residual is under `1E-10` of the largest term it adds up to. A tuple that is no solution
495+
misses by whole quantities. An exact tuple is still checked exactly. No issue was filed; a check of systems built from
496+
known solutions found it.
497+
498+
| Input | Was (2.6.0) | Now |
499+
|---|---|---|
500+
| `MathS.Equations("x^2 + y^2 - 2", "x^3 - y + 2").Solve("x", "y")` | the four complex solutions, in decimals | `[-1, 1]`, `[-1.3455, -0.4356]` and the four complex solutions |
501+
487502
### `round` is exact, and an infinity is its own nearest integer
488503

489504
**Silent for a long rational, not silent for an infinity.** `round` of a rational with more digits than the precision was

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

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Linq;
1010
using AngouriMath.Core;
1111
using AngouriMath.Core.Budgets;
12+
using PeterO.Numbers;
1213
using static AngouriMath.Entity;
1314

1415
namespace AngouriMath.Functions.Algebra.Groebner
@@ -41,9 +42,11 @@ namespace AngouriMath.Functions.Algebra.Groebner
4142
/// <c>(sqrt(2), sqrt(2))</c> and <c>(-sqrt(2), -sqrt(2))</c> — because one structural
4243
/// pass is enough to prove a radical identity. Where a root is a decimal the check cannot
4344
/// be made at all, and rather than accept a tuple on a tolerance, which is how a root
44-
/// that is merely close becomes a reported solution, the whole system goes back to the
45-
/// existing solver. So this takes what it can prove and declines the rest without
46-
/// changing what those did before.
45+
/// that is merely close becomes a reported solution, the whole system went back to the
46+
/// existing solver, which answers it in decimals too and can leave out what this would have
47+
/// found exactly: x^2 + y^2 = 2, x^3 - y = -2 lost (-1, 1). So a candidate in decimals is
48+
/// checked to the precision it has instead, a negligible share of the largest term, which a
49+
/// tuple that is not a solution misses by whole quantities.
4750
/// </para>
4851
/// </remarks>
4952
internal static class GroebnerSystemSolver
@@ -151,7 +154,9 @@ static bool Attempt(
151154

152155
var builder = new MatrixBuilder(variables.Count);
153156
foreach (var candidate in found)
154-
builder.Add(candidate);
157+
// An entry already in decimals is the number it evaluates to, rather than the
158+
// polynomial in decimals that back-substitution wrote it as.
159+
builder.Add(candidate.Select(entry => entry.Nodes.Any(IsADecimal) && entry.Evaled is Number.Complex value ? value : entry).ToArray());
155160
solutions = builder.ToMatrix();
156161
return true;
157162
}
@@ -274,14 +279,48 @@ static bool Satisfies(
274279
for (var i = 0; i < variables.Count; i++)
275280
substitutions[variables[i]] = candidate[i];
276281

282+
// A candidate with a decimal in it is a root of a factor of a univariate polynomial whose
283+
// roots have no closed form, found numerically, and no structural pass makes its residual
284+
// exactly zero. Such a candidate is checked to the precision it has: each equation's
285+
// residual is to be a negligible part of the largest term the equation adds up to. A
286+
// tuple that does not satisfy the system, as a basis out of shape position can give, is
287+
// off by whole quantities and fails this as it fails the exact test. Declining every
288+
// decimal sent the system to elimination, which answered x^2 + y^2 = 2, x^3 - y = -2 in
289+
// decimals and without its rational solution (-1, 1).
290+
bool? inDecimals = null;
277291
foreach (var equation in equations)
278292
{
279293
if (!budget.Spend())
280294
return false;
281-
if (equation.Substitute(substitutions).InnerSimplified is not Number.Integer { IsZero: true })
295+
var substituted = equation.Substitute(substitutions);
296+
if (substituted.InnerSimplified is Number.Integer { IsZero: true })
297+
continue;
298+
inDecimals ??= candidate.Any(entry => entry.Nodes.Any(IsADecimal));
299+
if (inDecimals is false || !IsNegligible(substituted))
282300
return budget.Allow(false, "unverifiable root");
283301
}
284302
return true;
285303
}
304+
305+
private static bool IsADecimal(Entity node)
306+
=> node is Number.Complex number && (number.RealPart is not Number.Rational || number.ImaginaryPart is not Number.Rational);
307+
308+
/// <summary>How small a residual is next to the largest term, to be what rounding leaves of a zero.</summary>
309+
[ConstantField] private static readonly EDecimal NegligibleShare = EDecimal.FromString("1E-10");
310+
311+
/// <summary>
312+
/// Whether <paramref name="substituted"/>, an equation with numbers for its unknowns,
313+
/// evaluates to a residual negligible next to the largest of the terms it adds up to.
314+
/// </summary>
315+
private static bool IsNegligible(Entity substituted)
316+
{
317+
if (substituted.Evaled is not Number.Complex { IsFinite: true } residual)
318+
return false;
319+
var largest = EDecimal.One;
320+
foreach (var term in Sumf.LinearChildren(substituted))
321+
if (term.Evaled is Number.Complex { IsFinite: true } value)
322+
largest = EDecimal.Max(largest, value.Abs().EDecimal);
323+
return residual.Abs().EDecimal.CompareTo(largest.Multiply(NegligibleShare)) <= 0;
324+
}
286325
}
287326
}

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,9 @@ public void AnIrrationalSolutionComesBackInRadicals()
268268
}
269269

270270
/// <summary>
271-
/// Where the univariate's roots are decimals there is nothing to prove an identity
272-
/// with, so the system is declined — and the declining has to be quick, because an
273-
/// earlier version proved nothing slowly and cost more than it saved.
271+
/// Where the univariate's roots are decimals there is nothing to prove an identity with,
272+
/// so a candidate in decimals is checked to the precision it has, its residual a
273+
/// negligible share of the largest term of each equation.
274274
/// </summary>
275275
[Fact]
276276
public void APolynomialSystemWithDecimalRootsIsStillAnswered()
@@ -281,6 +281,34 @@ public void APolynomialSystemWithDecimalRootsIsStillAnswered()
281281
Assert.Equal(9, solutions.RowCount);
282282
}
283283

284+
/// <summary>
285+
/// A rational solution beside roots that are decimals is kept exactly. The univariate of
286+
/// x^2 + y^2 = 2, x^3 - y = -2 is (x + 1) times a quintic with no roots in radicals; the
287+
/// path declined every candidate for the quintic's decimals, and the elimination it fell
288+
/// back to answered in decimals without (-1, 1). Found by a check of systems built from
289+
/// known solutions; no issue was filed.
290+
/// </summary>
291+
[Fact]
292+
public void ARationalSolutionBesideDecimalOnesIsKept()
293+
{
294+
var solutions = MathS.Equations("x^2 + y^2 - 2", "x^3 - y + 2").Solve("x", "y");
295+
Assert.NotNull(solutions);
296+
Assert.Equal(6, solutions.RowCount);
297+
var exact = false;
298+
for (var row = 0; row < solutions.RowCount; row++)
299+
{
300+
if (solutions[row, 0] == (Entity)(-1) && solutions[row, 1] == (Entity)1)
301+
exact = true;
302+
foreach (var equation in new Entity[] { "x^2 + y^2 - 2", "x^3 - y + 2" })
303+
{
304+
var residual = equation.Substitute("x", solutions[row, 0]).Substitute("y", solutions[row, 1]).EvalNumerical();
305+
Assert.True(Number.Abs(residual).EDecimal.CompareTo(PeterO.Numbers.EDecimal.FromString("1e-10")) < 0,
306+
$"row {row} leaves {residual} in {equation}");
307+
}
308+
}
309+
Assert.True(exact, $"(-1, 1) is not a row of {solutions}");
310+
}
311+
284312
/// <summary>
285313
/// A free variable means infinitely many solutions, which is not something a
286314
/// triangular basis enumerates — the ideal is not zero-dimensional, so there is no

0 commit comments

Comments
 (0)