diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index daba72519..f3cb7248e 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -63,6 +63,24 @@ among the arguments. --- +### A system row that is not a solution declines the system + +**Silent: a wrong row is now no answer.** With `P + Q sqrt(2) = (1 + sqrt(2))^70`, the coefficient `P - Q y` is +`1.6e-27` at `y = sqrt(2)`, and solving one equation reads values that small as zero. The elimination then found the row +`[-2P, -sqrt(2)]`, which leaves `-2` in the second equation, and lost all four solutions. A row with only exact numbers +in it is now put back into the equations by interval arithmetic, and a residual whose interval leaves out zero declines +the system ([#2446](https://github.com/asc-community/AngouriMath/issues/2446)). + +| Input | Was (2.7.0) | Now | +|---|---|---| +| `MathS.Equations("y^2 - 2", "x^2 + (311363698964240484013304163 - 220167382952941249990598278 * y) * x - 3").Solve("x", "y")` | `[[-622727397928480968026608326, -sqrt(2)]]` | `null` | + +The same system for the exponents 20 to 60 was declined already, and for 10 it is still answered with its four rows. +For 80 to 160 it was answered with the one row `[-2P, -sqrt(2)]` too, and for 140 and 200 with `[sqrt(3), sqrt(2)]` and +`[-sqrt(3), sqrt(2)]` besides, which leave `sqrt(3) (P - Q sqrt(2))`; each of those is declined now. + +--- + ## 2.7.0 — since 2.6.0 **Read these first: names that read differently.** Each of these was read as something else on 2.6.0. Code that used diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs index 3d5faf5b9..53b8b0f3a 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs @@ -288,6 +288,21 @@ internal static Set Solve(Entity equation, Variable x) /// Anything else is kept, as a root of one equation is: a residual that cannot be /// evaluated, or one too small next to its terms to be told from rounding. /// + /// + /// Except where the row is exact and an interval enclosing a residual leaves out zero, + /// which declines the system. An exact row is a solution only where every residual is + /// exactly zero, so such a row is none, however small the residual is next to its terms; + /// and that the tolerance took it for one means rounding reached the elimination, which + /// can have lost true rows as well as made this one. In + /// y^2 - 2, x^2 + (P - Q y) x - 3, with (1 + sqrt(2))^70 = P + Q sqrt(2), + /// P - Q sqrt(2) is 1.6e-27, and the zero tolerance of one equation's solve + /// reads it, and terms of that size, as nothing: the row [-2P, -sqrt(2)] leaves + /// -2 against terms of 4e53, under a tolerance relative to them, and all + /// four solutions were gone. Rounding reaches an exact elimination through downcasting, + /// which v3 removes; the decline is to be reconsidered then, and the check kept. + /// #2446, + /// #1019 + /// /// private static List>? RowsThatHold(List equations, Variable[] variables, List> rows) { @@ -305,11 +320,66 @@ internal static Set Solve(Entity equation, Variable x) continue; if (residuals.Any(residual => residual.Vars.Any() && StatementSolver.IsEvidenceAgainst(AtASamplePoint(residual, own)))) return null; + if (IsExact(row) && residuals.Any(residual => !residual.Vars.Any() && IsShownNotZero(residual))) + return null; kept.Add(row); } return WithoutDecimalCopies(kept); } + /// Whether no entry of has a number in it that is not exact. + private static bool IsExact(List row) + => !row.Any(entry => entry.Nodes.Any(node => node is Number { IsExact: false })); + + /// + /// Whether an interval enclosing , a number with no symbol in + /// it, leaves out zero. Interval arithmetic rounds each bound outward, so that is a proof + /// and not an estimate; an interval that holds zero, or that cannot be worked out, proves + /// nothing either way. + /// + /// + /// To as many decimal places as evaluation works to significant digits: a residual under + /// that is what the tolerance takes for zero whatever the terms are, and one that + /// evaluation took for zero above it is under about half of the digits next to its terms. + /// The digits are + /// significant ones, so where the terms are large the first interval is wider than that, + /// and it is worked out once more with the digits the terms take besides: with + /// (1 + sqrt(2))^140 in the coefficients the terms are near 1e107, and the + /// residual -2 is inside an interval of a hundred digits. + /// + private static bool IsShownNotZero(Entity residual) + { + var precision = MathS.Settings.DecimalPrecisionContext.Value.Precision; + var digits = precision.IsZero || !precision.CanFitInInt32() ? 100 : System.Math.Max(precision.ToInt32Unchecked(), 30); + if (Numerics.PreciseEvaluation.Of(residual, digits) is not { } enclosure) + return false; + if (Excludes(enclosure)) + return true; + var width = PeterO.Numbers.EDecimal.Max( + enclosure.Re.High.Subtract(enclosure.Re.Low), enclosure.Im.High.Subtract(enclosure.Im.Low)); + if (width.IsZero) + return false; + // The decimal exponent of the width: how many more digits put it under 10^-digits. + // Ten or fewer are what rounding leaves of terms near one, which is every row whose + // terms are not large, and are not worth a second evaluation. + var magnitude = width.Exponent.Add(width.UnsignedMantissa.GetDigitCountAsEInteger()).Subtract(1); + var more = magnitude.Add(digits); + if (more.CompareTo(10) <= 0) + return false; + if (!more.CanFitInInt32() || more.ToInt32Unchecked() > MostMoreDigits) + more = MostMoreDigits; + return Numerics.PreciseEvaluation.Of(residual, digits + more.ToInt32Unchecked()) is { } finer && Excludes(finer); + + static bool Excludes(Numerics.PreciseComplexInterval enclosure) + => enclosure.Re.Mignitude.Sign > 0 || enclosure.Im.Mignitude.Sign > 0; + } + + /// + /// The most digits adds for large terms, which bounds what one + /// residual costs: a thousand digits is terms past 1e900. + /// + private const int MostMoreDigits = 1000; + /// /// without a row that is a copy of another: one equal to an /// earlier row, and one with a number in it that is not exact, each of whose entries is diff --git a/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs b/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs index 905dd8548..e4465f4c0 100644 --- a/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs +++ b/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs @@ -283,6 +283,54 @@ public void Issue2425_AnExponentialEquationOverTheRealsHasOnlyRealRoots(string e } } + // https://github.com/asc-community/AngouriMath/issues/2446 + // The declines here come of downcasting, which v3 removes: + // https://github.com/asc-community/AngouriMath/issues/1019#issuecomment-5754734225 + // P and Q are the Pell numbers with (1 + sqrt(2))^n = P + Q sqrt(2), so at y = sqrt(2) + // the coefficient P - Q y is (1 - sqrt(2))^n, which is 1.6e-27 for n = 70. The system was + // answered there with the one row [-2P, -sqrt(2)], which leaves exactly -2 + 2P (P - Q sqrt(2)) + // in the second equation, about -2, and its four solutions were lost. Each equation is a + // quadratic in x for each y, so every answer has four rows; each row is put back into + // both equations and enclosed, by interval arithmetic, within 1e-80 of zero. Declining + // is allowed where the coefficients are too close to rounding to settle, and n = 10 is + // answered today and must stay answered. At n = 140 the terms are near 1e107, past the + // hundred digits evaluation works to, and the rows [sqrt(3), sqrt(2)] and + // [-sqrt(3), sqrt(2)] were answered too. + [Theory] + [InlineData(10, true)] + [InlineData(20, false)] + [InlineData(30, false)] + [InlineData(40, false)] + [InlineData(50, false)] + [InlineData(60, false)] + [InlineData(70, false)] + [InlineData(140, false)] + [InlineData(200, false)] + public void Issue2446_EveryRowOfASystemWithACancellingCoefficientIsASolution(int n, bool answered) + { + System.Numerics.BigInteger p = 1, q = 0; + for (var i = 0; i < n; i++) + (p, q) = (p + 2 * q, p + q); + var equations = new[] { "y^2 - 2".ToEntity(), $"x^2 + ({p} - {q} * y) * x - 3".ToEntity() }; + var solutions = MathS.Equations(equations).Solve("x", "y"); + if (solutions is null) + { + Assert.False(answered, "declined a system that is answered"); + return; + } + Assert.Equal(4, 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, 200); + Assert.True(enclosure is { } value + && value.Re.Magnitude.CompareTo(PeterO.Numbers.EDecimal.FromString("1E-80")) <= 0 + && value.Im.Magnitude.CompareTo(PeterO.Numbers.EDecimal.FromString("1E-80")) <= 0, + $"[{solutions[row, 0]}, {solutions[row, 1]}] leaves {residual.Evaled} in {equation}"); + } + } + // The complex reading is untouched: e^x + 1 = 0 has the root i pi there. [Fact] public void Issue2425_OverTheComplexNumbersTheRootIsKept()