From 527f303ec640dd9ba2ac4b3da5fe17a692364a88 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sat, 28 Nov 2026 21:23:36 +0000 Subject: [PATCH] Decide a rational residual exactly, and decline a row a small part off (#2634, #2635) Two ways rounding that reached an exact system solve became a wrong answer. y^2 - 3 = 0, x^2 + (P - Q y) x - 1 = 0 at the 190th power of 2 + sqrt(3) was answered with false rows, x = -668385287/2 among them and y a rational within 1e-217 of sqrt(3). The quartic that #2612's second candidate leads to has coefficients near 1e217, and its roots came out rational. For any modest x, y = (x^2 + P x - 1)/(Q x) is within 1/Q^2 of sqrt(3). So the residual of y^2 - 3 is a nonzero rational near 1e-217, which reads as zero to the evaluation's digits and inside the enclosure's. A residual of rationals alone is now worked out exactly. x y = 1, x + y = 10^45 + 7 was answered with one of its two rows; 2.10.0 did the same. In the other row, y came out of the back-substitution as 10^45 exactly: downcasting put a value spoiled by a cancellation of 90 digits onto a nearby rational. The row left -7 against terms near 10^45, outside what IsEvidenceAgainst lets rounding leave, and was dropped as no solution. An exact residual that is not zero but is no more than a part in 10^12 of the largest term is now read as rounding having reached the row. A row a rewrite added leaves a residual of the terms' own size. Each case now gives all its rows or declines, through ResidualWeight .CannotTell. A sweep of 320 Pell-shape systems (d = 2, 3; powers 10 to 200; c = 1, 2, 3, 5; both signs), 48 sum-and-product, 148 product-of-lines, 12 circle and 18 Fibonacci-linear systems found 19 wrong answers on master and none here. The answered counts are unchanged and the 19 now decline. 270 checks of the determinant, rank and inverse of integer and rational matrices with entries near 1e30 to 1e60 agree exactly with fraction arithmetic, on master and here. Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 16 ++++ .../Solvers/EquationSolver/SolveStatement.cs | 51 +++++++++++- .../ARoundedRowIsDeclinedNotAnsweredTest.cs | 83 +++++++++++++++++++ 3 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 Sources/Tests/UnitTests/Algebra/SolveTest/ARoundedRowIsDeclinedNotAnsweredTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 5b268de30..560bbe8cc 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -275,6 +275,22 @@ rational function with a linear and an irreducible quadratic factor below the ba | `ln(x + 1) + (-1/2 * ln(x^2 + 1) + arctan(x))`, limit at `+oo` | unevaluated | `pi / 2` | | `ln(x) + (exp(x) - ln(sin(x)))`, limit at 0 from the right | unevaluated | `1` | +### A system that rounding reached is declined, not answered in part + +**Not silent: an incomplete answer is now no answer.** `x y = 1, x + y = 10^45 + 7` was answered with one of its two +rows. In the other, `y` came out of the back-substitution as `10^45` exactly: rounding reached the exact solve through +downcasting, the row left `-7` against terms near `10^45`, and it was dropped as no solution. A row whose exact residual +is not zero but is no more than a part in `10^12` of its terms is now read as rounding having reached it, and the system +is declined. A residual of rationals alone is decided exactly rather than to the working digits +([#2635](https://github.com/asc-community/AngouriMath/issues/2635), +[#2634](https://github.com/asc-community/AngouriMath/issues/2634)). + +| `MathS.Equations(...).Solve("x", "y")` | Was (2.10.0) | Now | +|---|---|---| +| `x y = 1, x + y = 10^45 + 7` | 1 row of 2 | `null` | +| `x y = -3, x + y = 10^45 + 7` | 1 row of 2 | `null` | +| `x y = 1, x + y = 10^20 + 7` | 2 rows | unchanged | + ### A trigonometric root at a rational multiple of pi is one multiple, written once **Silent, in the printed form only, and in how many members a family lists.** The inversion of `sin` and `cos` wrote a diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs index 4e35412fe..56345910d 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs @@ -512,6 +512,14 @@ internal enum ResidualWeight /// internal static ResidualWeight Weigh(Entity substituted) { + // A residual of rational numbers alone is decided exactly. Evaluated, a residual of 3e-217 is zero to the + // working digits, and an enclosure to them holds zero: y^2 - 3 = 0, x^2 + (P - Q y) x - 1 = 0 at the 190th + // power of 2 + sqrt(3) was answered with the rows x = -668385287/2 and y a rational within 1e-217 of + // sqrt(3), which hold every equation to every digit asked and are no solution. + // https://github.com/asc-community/AngouriMath/issues/2634 + if (AsAnExactRational(substituted) is { } exactly) + return exactly.IsZero ? ResidualWeight.None + : IsSmallBesideTheTerms(substituted, exactly.Abs().EDecimal) ? ResidualWeight.CannotTell : ResidualWeight.Against; if (!IsEvidenceAgainst(substituted)) return ResidualWeight.None; if (!IsExactAndAlgebraic(substituted)) @@ -549,11 +557,50 @@ private static bool IsEvidenceAgainstWithMoreDigits(Entity substituted) /// largest term or of one, the share being ten to the minus half the working digits. /// private static bool IsWithinRounding(Entity substituted, Numerics.PreciseComplexInterval enclosure) + => IsSmallBesideTheTerms(substituted, EDecimal.Max(enclosure.Re.Magnitude, enclosure.Im.Magnitude)); + + /// + /// Whether a residual of size , exact and not zero, is no more than + /// of the largest term of , or than the share + /// takes rounding to leave where that is larger. + /// + private static bool IsSmallBesideTheTerms(Entity substituted, EDecimal size) { var share = EDecimal.Create(EInteger.One, MathS.Settings.DecimalPrecisionContext.Value.Precision.Divide(2).Negate()); var unused = false; - var bound = EDecimal.Max(LargestTerm(substituted, ref unused), share).Multiply(share); - return EDecimal.Max(enclosure.Re.Magnitude, enclosure.Im.Magnitude).CompareTo(bound) <= 0; + var largest = LargestTerm(substituted, ref unused); + var bound = EDecimal.Max(EDecimal.Max(largest, share).Multiply(share), largest.Multiply(RoundingReach)); + return size.CompareTo(bound) <= 0; + } + + /// + /// How small beside its largest term an exact residual that is not zero is read as rounding having reached + /// the root or the row, rather than as its being none: a part in 10^12. Rounding reaches an exact solve where + /// downcasting rounds a number it built onto a nearby rational, and that rational is near the right value, so + /// the residual is a small part of the terms; a root that a rewrite added leaves a residual of their size. + /// x y = 1, x + y = 10^45 + 7 was answered with one row of two: the other's y came out as + /// 10^45 exactly, and left -7 against terms near 10^45, read as no solution. + /// https://github.com/asc-community/AngouriMath/issues/2635 + /// + [ConstantField] private static readonly EDecimal RoundingReach = EDecimal.FromString("1E-12"); + + /// + /// as the rational it is, where it is rational numbers in sums, products, quotients + /// and whole powers alone, and so is worked out exactly; elsewhere. + /// + private static Rational? AsAnExactRational(Entity expression) + { + static bool IsRationalArithmetic(Entity e) => e switch + { + Rational => true, + Sumf(var a, var b) => IsRationalArithmetic(a) && IsRationalArithmetic(b), + Minusf(var a, var b) => IsRationalArithmetic(a) && IsRationalArithmetic(b), + Mulf(var a, var b) => IsRationalArithmetic(a) && IsRationalArithmetic(b), + Divf(var a, var b) => IsRationalArithmetic(a) && IsRationalArithmetic(b), + Powf(var a, Integer) => IsRationalArithmetic(a), + _ => false, + }; + return IsRationalArithmetic(expression) && expression.InnerSimplified is Rational value ? value : null; } /// diff --git a/Sources/Tests/UnitTests/Algebra/SolveTest/ARoundedRowIsDeclinedNotAnsweredTest.cs b/Sources/Tests/UnitTests/Algebra/SolveTest/ARoundedRowIsDeclinedNotAnsweredTest.cs new file mode 100644 index 000000000..bb48da1b5 --- /dev/null +++ b/Sources/Tests/UnitTests/Algebra/SolveTest/ARoundedRowIsDeclinedNotAnsweredTest.cs @@ -0,0 +1,83 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System.Linq; +using AngouriMath; +using AngouriMath.Extensions; +using Xunit; +using static AngouriMath.Entity; + +namespace AngouriMath.Tests.Algebra.SolveTest +{ + /// + /// Where rounding reached an exact system solve, a row came out a little off. A row off by a residual of rationals + /// that every digit evaluated reads as zero was answered, and a row off by a small part of its terms was dropped as + /// none. Each must be answered as all of the rows or none. + /// https://github.com/asc-community/AngouriMath/issues/2634 + /// https://github.com/asc-community/AngouriMath/issues/2635 + /// + [Trait("Area", "Algebra")] + public sealed class ARoundedRowIsDeclinedNotAnsweredTest + { + private static void EveryRowHolds(Entity[] equations, Matrix solutions, int count) + { + Assert.Equal(count, solutions.RowCount); + for (var row = 0; row < solutions.RowCount; row++) + foreach (var equation in equations) + { + var residual = equation.Substitute("x", solutions[row, 0]).Substitute("y", solutions[row, 1]); + var enclosure = AngouriMath.Numerics.PreciseEvaluation.Of(residual, 400); + Assert.True(enclosure is { } value + && value.Re.Magnitude.CompareTo(PeterO.Numbers.EDecimal.FromString("1E-60")) <= 0 + && value.Im.Magnitude.CompareTo(PeterO.Numbers.EDecimal.FromString("1E-60")) <= 0, + $"row {row} leaves {residual.Evaled} in {equation}"); + // A row of rationals is checked exactly: within 1e-217 of a solution is not one. + if (solutions[row, 0] is Number.Rational && solutions[row, 1] is Number.Rational) + Assert.Equal(Number.Integer.Zero, residual.InnerSimplified); + } + } + + [Theory] + [InlineData(3, 2, 1, 190, "1", "-")] + [InlineData(3, 2, 1, 190, "3", "+")] + [InlineData(2, 1, 1, 150, "1", "-")] + [InlineData(2, 1, 1, 200, "1", "+")] + public void APellSystemAtAHighPower(int d, int u, int w, int n, string c, string sign) + { + System.Numerics.BigInteger p = 1, q = 0; + for (var i = 0; i < n; i++) + (p, q) = (p * u + q * w * d, p * w + q * u); + var equations = new[] { $"y^2 - {d}".ToEntity(), $"x^2 + ({p} {sign} {q} * y) * x - {c}".ToEntity() }; + if (MathS.Equations(equations).Solve("x", "y") is { } solutions) + EveryRowHolds(equations, solutions, 4); + } + + [Theory] + [InlineData(45, "1")] + [InlineData(45, "-3")] + [InlineData(60, "2")] + public void ASumAndAProduct(int digits, string c) + { + var s = System.Numerics.BigInteger.Pow(10, digits) + 7; + var equations = new[] { $"x * y - ({c})".ToEntity(), $"x + y - {s}".ToEntity() }; + if (MathS.Equations(equations).Solve("x", "y") is { } solutions) + EveryRowHolds(equations, solutions, 2); + } + + // Still answered where nothing rounds. + [Theory] + [InlineData(10, "1")] + [InlineData(20, "-3")] + public void ASumAndAProductOfModestSize(int digits, string c) + { + var s = System.Numerics.BigInteger.Pow(10, digits) + 7; + var equations = new[] { $"x * y - ({c})".ToEntity(), $"x + y - {s}".ToEntity() }; + var solutions = Assert.IsAssignableFrom(MathS.Equations(equations).Solve("x", "y")); + EveryRowHolds(equations, solutions, 2); + } + } +}