diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index ce3ac9c1c..f3e07ef1e 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -473,6 +473,21 @@ its rows in the order it finds them, which can differ from the eliminator's | `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 polynomial system with roots in decimals keeps its exact solutions + +**Silent: a polynomial system whose elimination has roots with no closed form has all its solutions, the rational ones +exactly.** The Gröbner triangularisation puts every tuple it finds back into the system and keeps it only where each +equation is exactly zero, which a root found numerically never is. One such root sent the whole system to the +eliminator, which 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 both of its real solutions were missing. A tuple in decimals is +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 +misses by whole quantities. An exact tuple is still checked exactly. No issue was filed; a check of systems built from +known solutions found it. + +| Input | Was (2.6.0) | Now | +|---|---|---| +| `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 | + ### `round` is exact, and an infinity is its own nearest integer **Silent for a long rational, not silent for an infinity.** `round` of a rational with more digits than the precision was diff --git a/Sources/AngouriMath/Functions/Algebra/Groebner/GroebnerSystemSolver.cs b/Sources/AngouriMath/Functions/Algebra/Groebner/GroebnerSystemSolver.cs index df1e1aa92..a74ce010b 100644 --- a/Sources/AngouriMath/Functions/Algebra/Groebner/GroebnerSystemSolver.cs +++ b/Sources/AngouriMath/Functions/Algebra/Groebner/GroebnerSystemSolver.cs @@ -9,6 +9,7 @@ using System.Linq; using AngouriMath.Core; using AngouriMath.Core.Budgets; +using PeterO.Numbers; using static AngouriMath.Entity; namespace AngouriMath.Functions.Algebra.Groebner @@ -41,9 +42,11 @@ namespace AngouriMath.Functions.Algebra.Groebner /// (sqrt(2), sqrt(2)) and (-sqrt(2), -sqrt(2)) — because one structural /// pass is enough to prove a radical identity. Where a root is a decimal the check cannot /// be made at all, and rather than accept a tuple on a tolerance, which is how a root - /// that is merely close becomes a reported solution, the whole system goes back to the - /// existing solver. So this takes what it can prove and declines the rest without - /// changing what those did before. + /// that is merely close becomes a reported solution, the whole system went back to the + /// existing solver, which answers it in decimals too and can leave out what this would have + /// found exactly: x^2 + y^2 = 2, x^3 - y = -2 lost (-1, 1). So a candidate in decimals is + /// checked to the precision it has instead, a negligible share of the largest term, which a + /// tuple that is not a solution misses by whole quantities. /// /// internal static class GroebnerSystemSolver @@ -151,7 +154,9 @@ static bool Attempt( var builder = new MatrixBuilder(variables.Count); foreach (var candidate in found) - builder.Add(candidate); + // An entry already in decimals is the number it evaluates to, rather than the + // polynomial in decimals that back-substitution wrote it as. + builder.Add(candidate.Select(entry => entry.Nodes.Any(IsADecimal) && entry.Evaled is Number.Complex value ? value : entry).ToArray()); solutions = builder.ToMatrix(); return true; } @@ -274,14 +279,48 @@ static bool Satisfies( for (var i = 0; i < variables.Count; i++) substitutions[variables[i]] = candidate[i]; + // A candidate with a decimal in it is a root of a factor of a univariate polynomial whose + // roots have no closed form, found numerically, and no structural pass makes its residual + // exactly zero. Such a candidate is checked to the precision it has: each equation's + // residual is to be a negligible part of the largest term the equation adds up to. A + // tuple that does not satisfy the system, as a basis out of shape position can give, is + // off by whole quantities and fails this as it fails the exact test. Declining every + // decimal sent the system to elimination, which answered x^2 + y^2 = 2, x^3 - y = -2 in + // decimals and without its rational solution (-1, 1). + bool? inDecimals = null; foreach (var equation in equations) { if (!budget.Spend()) return false; - if (equation.Substitute(substitutions).InnerSimplified is not Number.Integer { IsZero: true }) + var substituted = equation.Substitute(substitutions); + if (substituted.InnerSimplified is Number.Integer { IsZero: true }) + continue; + inDecimals ??= candidate.Any(entry => entry.Nodes.Any(IsADecimal)); + if (inDecimals is false || !IsNegligible(substituted)) return budget.Allow(false, "unverifiable root"); } return true; } + + private static bool IsADecimal(Entity node) + => node is Number.Complex number && (number.RealPart is not Number.Rational || number.ImaginaryPart is not Number.Rational); + + /// How small a residual is next to the largest term, to be what rounding leaves of a zero. + [ConstantField] private static readonly EDecimal NegligibleShare = EDecimal.FromString("1E-10"); + + /// + /// Whether , an equation with numbers for its unknowns, + /// evaluates to a residual negligible next to the largest of the terms it adds up to. + /// + private static bool IsNegligible(Entity substituted) + { + if (substituted.Evaled is not Number.Complex { IsFinite: true } residual) + return false; + var largest = EDecimal.One; + foreach (var term in Sumf.LinearChildren(substituted)) + if (term.Evaled is Number.Complex { IsFinite: true } value) + largest = EDecimal.Max(largest, value.Abs().EDecimal); + return residual.Abs().EDecimal.CompareTo(largest.Multiply(NegligibleShare)) <= 0; + } } } diff --git a/Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs b/Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs index f241d6f95..8338e6f12 100644 --- a/Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs +++ b/Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs @@ -268,9 +268,9 @@ public void AnIrrationalSolutionComesBackInRadicals() } /// - /// Where the univariate's roots are decimals there is nothing to prove an identity - /// with, so the system is declined — and the declining has to be quick, because an - /// earlier version proved nothing slowly and cost more than it saved. + /// Where the univariate's roots are decimals there is nothing to prove an identity with, + /// so a candidate in decimals is checked to the precision it has, its residual a + /// negligible share of the largest term of each equation. /// [Fact] public void APolynomialSystemWithDecimalRootsIsStillAnswered() @@ -281,6 +281,34 @@ public void APolynomialSystemWithDecimalRootsIsStillAnswered() Assert.Equal(9, solutions.RowCount); } + /// + /// A rational solution beside roots that are decimals is kept exactly. The univariate of + /// x^2 + y^2 = 2, x^3 - y = -2 is (x + 1) times a quintic with no roots in radicals; the + /// path declined every candidate for the quintic's decimals, and the elimination it fell + /// back to answered in decimals without (-1, 1). Found by a check of systems built from + /// known solutions; no issue was filed. + /// + [Fact] + public void ARationalSolutionBesideDecimalOnesIsKept() + { + var solutions = MathS.Equations("x^2 + y^2 - 2", "x^3 - y + 2").Solve("x", "y"); + Assert.NotNull(solutions); + Assert.Equal(6, solutions.RowCount); + var exact = false; + for (var row = 0; row < solutions.RowCount; row++) + { + if (solutions[row, 0] == (Entity)(-1) && solutions[row, 1] == (Entity)1) + exact = true; + foreach (var equation in new Entity[] { "x^2 + y^2 - 2", "x^3 - y + 2" }) + { + var residual = equation.Substitute("x", solutions[row, 0]).Substitute("y", solutions[row, 1]).EvalNumerical(); + Assert.True(Number.Abs(residual).EDecimal.CompareTo(PeterO.Numbers.EDecimal.FromString("1e-10")) < 0, + $"row {row} leaves {residual} in {equation}"); + } + } + Assert.True(exact, $"(-1, 1) is not a row of {solutions}"); + } + /// /// A free variable means infinitely many solutions, which is not something a /// triangular basis enumerates — the ideal is not zero-dimensional, so there is no