diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs index 9605ad269..505cfc295 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs @@ -333,8 +333,13 @@ internal static Set Solve(Entity equation, Variable x) for (var index = 0; index < variables.Length; index++) replacements[variables[index]] = row[index]; var residuals = equations.Select(equation => equation.Substitute(replacements)).ToList(); - if (residuals.Any(residual => !residual.Vars.Any() - && (residual.Evaled is Complex { IsFinite: false } || StatementSolver.IsEvidenceAgainstUnlessEnclosed(residual)))) + var weights = residuals.Select(residual => residual.Vars.Any() ? StatementSolver.ResidualWeight.None : StatementSolver.Weigh(residual)).ToList(); + // A residual rounding reached says nothing of the row either way, and the row may stand for a + // solution that was lost: https://github.com/asc-community/AngouriMath/issues/2627 + if (weights.Contains(StatementSolver.ResidualWeight.CannotTell)) + return null; + if (residuals.Select((residual, index) => !residual.Vars.Any() + && (residual.Evaled is Complex { IsFinite: false } || weights[index] is StatementSolver.ResidualWeight.Against)).Any(dropped => dropped)) continue; if (residuals.Any(residual => residual.Vars.Any() && StatementSolver.IsEvidenceAgainst(AtASamplePoint(residual, own)))) return null; diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs index 21d7641fc..4e35412fe 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs @@ -225,8 +225,14 @@ private static RootVerdict Verdict(Entity equation, Variable x, Entity root) var substituted = equation.Substitute(x, root); if (IsUndefinedExactly(substituted)) return RootVerdict.Spurious; - if (IsEvidenceAgainstUnlessEnclosed(substituted)) - return RootVerdict.Spurious; + switch (Weigh(substituted)) + { + case ResidualWeight.Against: + return RootVerdict.Spurious; + // https://github.com/asc-community/AngouriMath/issues/2627 + case ResidualWeight.CannotTell: + return RootVerdict.ShownNotARoot; + } return !substituted.Vars.Any() && IsAboveTheIntervalFloor(equation, x, root) && IsShownNotZero(substituted) ? RootVerdict.ShownNotARoot : RootVerdict.Kept; @@ -275,20 +281,24 @@ internal static bool IsExact(Entity entity) /// (1 + sqrt(2))^140 in the coefficients the terms are near 1e107, and the /// residual -2 is inside an interval of a hundred digits. /// - internal static bool IsShownNotZero(Entity residual) => ExcludesZero(residual) is true; + internal static bool IsShownNotZero(Entity residual) => ExcludesZero(residual, out _) is true; /// /// Whether an interval enclosing leaves out zero, as /// works it out; where it holds zero, /// and where no interval can be worked out. /// - private static bool? ExcludesZero(Entity residual) + private static bool? ExcludesZero(Entity residual, out Numerics.PreciseComplexInterval? excluding) { + excluding = null; var digits = IntervalDigits(); if (Numerics.PreciseEvaluation.Of(residual, digits) is not { } enclosure) return null; if (Excludes(enclosure)) + { + excluding = 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) @@ -302,7 +312,10 @@ internal static bool IsExact(Entity entity) return false; if (!more.CanFitInInt32() || more.ToInt32Unchecked() > MostMoreDigits) more = MostMoreDigits; - return Numerics.PreciseEvaluation.Of(residual, digits + more.ToInt32Unchecked()) is { } finer && Excludes(finer); + if (Numerics.PreciseEvaluation.Of(residual, digits + more.ToInt32Unchecked()) is not { } finer || !Excludes(finer)) + return false; + excluding = finer; + return true; static bool Excludes(Numerics.PreciseComplexInterval enclosure) => enclosure.Re.Mignitude.Sign > 0 || enclosure.Im.Mignitude.Sign > 0; @@ -467,7 +480,81 @@ internal static bool IsEvidenceAgainst(Entity substituted) /// #2448 /// internal static bool IsEvidenceAgainstUnlessEnclosed(Entity substituted) - => IsEvidenceAgainst(substituted) && !(IsExactAndAlgebraic(substituted) && ExcludesZero(substituted) is false); + => Weigh(substituted) is ResidualWeight.Against; + + /// What a residual says of the root or the row put in. + internal enum ResidualWeight + { + /// Nothing against it. + None, + /// It is no solution. + Against, + /// Rounding reached it, and whether it is one cannot be told. + CannotTell, + } + + /// + /// , weighed against an interval where is exact and + /// algebraic. An enclosure that holds zero says the residual is what rounding left, and is no evidence. One that + /// leaves zero out but is no further from it than rounding leaves, or none at all where twice the digits evaluate + /// the residual to something else, says rounding reached what found the root or the row: whether it is one cannot + /// be told, and the caller declines, since dropping it may lose a solution and keeping it may keep a point that is + /// none. + /// + /// + /// In y^2 - 2, x^2 + (P - Q y) x - 3, with (1 + sqrt(2))^n = P + Q sqrt(2), a system solved for + /// y from the second equation goes through ((x^2 + P x - 3)/(Q x))^2 = 2, a quartic in x with + /// coefficients near Q^2. At n = 90 its small root, exact, left -0.042 against terms of 3 at a + /// hundred digits, with no enclosure, and something else at two hundred; at n = 70, merged with a + /// change that kept P + Q sqrt(2) exact, the row of that root left -7.2e-21 where its enclosure is + /// near 6e-53, exact and not zero. Each was dropped as no solution, and three rows of four were answered. + /// https://github.com/asc-community/AngouriMath/issues/2627 + /// + internal static ResidualWeight Weigh(Entity substituted) + { + if (!IsEvidenceAgainst(substituted)) + return ResidualWeight.None; + if (!IsExactAndAlgebraic(substituted)) + return ResidualWeight.Against; + return ExcludesZero(substituted, out var excluding) switch + { + false => ResidualWeight.None, + true => IsWithinRounding(substituted, excluding!.Value) ? ResidualWeight.CannotTell : ResidualWeight.Against, + // No interval: an enclosure divides by one that holds zero where the terms cancel past the digits it + // is worked to. A residual that twice the digits evaluate to about the same is a whole quantity; one + // they move is what rounding left. + null => IsEvidenceAgainstWithMoreDigits(substituted) ? ResidualWeight.Against : ResidualWeight.CannotTell, + }; + } + + /// + /// Whether evaluates to about the same residual with twice the working digits, + /// to within a half of it: rounding's residual moves with the digits, and a whole quantity does not. + /// + private static bool IsEvidenceAgainstWithMoreDigits(Entity substituted) + { + var context = MathS.Settings.DecimalPrecisionContext.Value; + if (context.Precision.IsZero || !context.Precision.CanFitInInt32() || substituted.Evaled is not Number.Complex atTheDigits) + return true; + Entity atMoreDigits; + using (MathS.Settings.DecimalPrecisionContext.Set(context.WithPrecision(2 * context.Precision.ToInt32Unchecked()))) + atMoreDigits = substituted.Evaled; + return atMoreDigits is Number.Complex more && more.IsFinite + && ((Number.Complex)(more - atTheDigits)).Abs().EDecimal.CompareTo(atTheDigits.Abs().EDecimal.Divide(2)) <= 0; + } + + /// + /// Whether of , exact and algebraic, is no + /// further from zero than takes rounding to leave: a share of the + /// 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) + { + 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; + } /// /// Whether is exact numbers in sums, products, quotients and diff --git a/Sources/Tests/UnitTests/Algebra/SolveTest/ARowIsNotDroppedOnARoundedResidualTest.cs b/Sources/Tests/UnitTests/Algebra/SolveTest/ARowIsNotDroppedOnARoundedResidualTest.cs new file mode 100644 index 000000000..7565dd3dc --- /dev/null +++ b/Sources/Tests/UnitTests/Algebra/SolveTest/ARowIsNotDroppedOnARoundedResidualTest.cs @@ -0,0 +1,55 @@ +// +// 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 AngouriMath; +using AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Algebra.SolveTest +{ + /// + /// y^2 - 2 = 0, x^2 + (P - Q y) x - c = 0, with (1 + sqrt(2))^n = P + Q sqrt(2), has four solutions. Solved + /// for y from the second equation, the small root at y = -sqrt(2) is a root of + /// ((x^2 + P x - c)/(Q x))^2 = 2 near c / (2P), which a hundred digits evaluate to a residual rounding made, + /// with no interval to say so; it was dropped as no root, and three rows were answered. The answer must be all four or + /// none. https://github.com/asc-community/AngouriMath/issues/2627 + /// + [Trait("Area", "Algebra")] + public sealed class ARowIsNotDroppedOnARoundedResidualTest + { + [Theory] + [InlineData(70, 3)] + [InlineData(80, 5)] + [InlineData(90, 2)] + [InlineData(90, 5)] + [InlineData(100, 2)] + [InlineData(120, 2)] + [InlineData(120, 3)] + [InlineData(120, 5)] + public void IsAnsweredWithEveryRowOrNone(int n, int c) + { + 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 - {c}".ToEntity() }; + var solutions = MathS.Equations(equations).Solve("x", "y"); + if (solutions is null) + 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, 300); + 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}"); + } + } + } +}