From e3b55745bea9e773b8b308fb404d38c0649a9b63 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 1 Nov 2026 23:06:13 +0000 Subject: [PATCH 1/2] A root in exact numbers is put back to the working precision Every root is put back into its equation, and IsEvidenceAgainst dropped one only where it left more than a hundredth of the largest term. That bound was set for roots found numerically, which are only as accurate as the search that found them. A root in exact numbers, put into an equation of exact numbers, evaluates to the working precision, so a true one leaves only rounding and a spurious one a whole quantity, and the bound let a near spurious one through: (3x - 4) sqrt(x) + 3 + 3x - 4x^2 = 0 is solved by squaring into a quartic, and one of the quartic's roots, 1.3233 in radicals, leaves 0.069 against a largest term of 7. 2.6.0 and master answer it. An equation and root that are exact numbers in sums, products, quotients and rational powers are now held to half the digits of DecimalPrecisionContext, 10^-50 of the largest term by default. A function, a constant such as pi, or a decimal keeps the hundredth: a special function may be evaluated in doubles, and a decimal is only as accurate as what found it. The same test reads a system's rows, which it holds the same way. Generated equations against master: the radical cross-check's powers-of-x mode, 1000 at seed 41, goes from 8 findings to 7, the unsound 1.3233 removed; the 7 are roots of polynomials, which #2324 answers. At seed 31 (200), the default mode (400 at seed 21), random polynomials (200 at seed 5) and generated systems (1190) are unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) --- BREAKING-CHANGES.md | 15 +++++++ Sources/.editorconfig | 3 ++ .../Solvers/EquationSolver/SolveStatement.cs | 28 ++++++++++++ ...tRootIsCheckedToTheWorkingPrecisionTest.cs | 43 +++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 Sources/Tests/UnitTests/Algebra/SolveTest/AnExactRootIsCheckedToTheWorkingPrecisionTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 3df4acd42..c6fbc183b 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -212,6 +212,21 @@ carried it once combined: a second derivative of `1/sqrt(x^2 + 13)` stated `not | `"(a provided b > 0) + (c provided b > 0)".ToEntity().InnerSimplified` | `a + c provided b > 0 and b > 0` | `a + c provided b > 0` | | `MathS.Equations("sqrt(y) - x", "y - a").Solve("x", "y")` | `[[sqrt(a), (sqrt(a)) ^ 2], [-sqrt(a), (-sqrt(a)) ^ 2]]` | `[[sqrt(a) provided C(sqrt(a)), (sqrt(a)) ^ 2 provided C(sqrt(a))], [-sqrt(a) provided C(-sqrt(a)), (-sqrt(a)) ^ 2 provided C(-sqrt(a))]]`, `C(v)` being `v = 0 or -1/2 * pi < arg(v) and arg(v) <= 1/2 * pi` | +### A root in exact numbers is put back to the working precision + +**Silent: a number that is not a root, in exact numbers, is no longer answered as one.** Every root is put back into the +equation, and one was dropped only where it left more than a hundredth of the largest term. That bound is for roots +found numerically, which are only as accurate as the search. An exact root in an equation of exact numbers is evaluated +to `MathS.Settings.DecimalPrecisionContext`'s digits, so a true root leaves only rounding, and it is now held to half of +those digits. `(3x - 4) sqrt(x) + 3 + 3x - 4x^2 = 0` is solved by squaring, and one of the roots of the quartic that +gives, 1.3233 in radicals, leaves 0.069 against a largest term of 7. A root with a function, a constant like `pi`, or a +decimal in it is still held to a hundredth. No issue was filed; a check of generated equations in `sqrt(x)` beside +powers of `x` found it. + +| Input | Was (2.6.0) | Now | +|---|---|---| +| `"(3x - 4) * sqrt(x) + 3 + 3x - 4x^2 = 0".Solve("x")` | four roots, one of them 1.3233…, which is not a root | the other three | + ### A limit of a logarithm that is not decided is left unevaluated **Not silent: what overflowed the stack now returns.** A limit of a logarithm whose argument has no limit, as diff --git a/Sources/.editorconfig b/Sources/.editorconfig index b5a9cb974..82b110464 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -69,6 +69,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [Tests/UnitTests/Algebra/SolveTest/ARadicalTimesAPowerTest.cs] file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n +[Tests/UnitTests/Algebra/SolveTest/AnExactRootIsCheckedToTheWorkingPrecisionTest.cs] +file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n + [Tests/UnitTests/Core/FactorialCacheIsThreadSafeTest.cs] file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs index d8a13cc30..ca2dbaa51 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs @@ -118,6 +118,9 @@ internal static Set UnsolvedWhereIndependenceIsDenied(Set roots, Entity conditio /// 1e-15, while every spurious one sits between 1.5 and 2. This is a hundredth: /// twenty times above the worst genuine root and a hundred and fifty times below /// the mildest spurious one. + /// + /// It is a bound for roots found numerically. An equation and a root that are exact numbers + /// are held to the working precision instead, in . /// [ConstantField] private static readonly EDecimal RelativeResidualTolerance = EDecimal.FromString("0.01"); @@ -194,11 +197,36 @@ internal static bool IsEvidenceAgainst(Entity substituted) if (substituted.Evaled is not Number.Complex residual || !residual.IsFinite) return false; var size = residual.Abs().EDecimal; + // Exact numbers in sums, products, quotients and rational powers are evaluated to the + // working precision, so what they leave is either what rounding leaves of zero or a whole + // quantity, and a percent is no bound for it. (3x - 4) sqrt(x) + 3 + 3x - 4x^2 = 0, raised + // to a quartic, has a root of the quartic in radicals at 1.3233 that leaves 0.069 against + // terms of 7, which a percent of them kept. + if (IsExactAndAlgebraic(substituted)) + { + var share = EDecimal.Create(EInteger.One, MathS.Settings.DecimalPrecisionContext.Value.Precision.Divide(2).Negate()); + return size.GreaterThan(EDecimal.Max(LargestTerm(substituted), share).Multiply(share)); + } if (size.LessThan(MathS.Settings.PrecisionErrorCommon)) return false; return size.GreaterThan(LargestTerm(substituted).Multiply(RelativeResidualTolerance)); } + /// + /// Whether is exact numbers in sums, products, quotients and + /// rational powers, which evaluation carries to the working precision. A function, a constant + /// such as pi or a decimal is not: a special function may be evaluated in doubles, and + /// a decimal is only as accurate as what found it. + /// + private static bool IsExactAndAlgebraic(Entity expression) + => expression.Nodes.All(node => node switch + { + Complex number => number.RealPart is Rational && number.ImaginaryPart is Rational, + Sumf or Minusf or Mulf or Divf => true, + Powf(_, Rational) => true, + _ => false, + }); + /// /// The largest of the terms the equation adds up to at the root, which is the scale /// the residual has to be read against. A single term is its own largest, so an diff --git a/Sources/Tests/UnitTests/Algebra/SolveTest/AnExactRootIsCheckedToTheWorkingPrecisionTest.cs b/Sources/Tests/UnitTests/Algebra/SolveTest/AnExactRootIsCheckedToTheWorkingPrecisionTest.cs new file mode 100644 index 000000000..fa3ba0403 --- /dev/null +++ b/Sources/Tests/UnitTests/Algebra/SolveTest/AnExactRootIsCheckedToTheWorkingPrecisionTest.cs @@ -0,0 +1,43 @@ +// +// 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.Extensions; +using PeterO.Numbers; +using Xunit; +using static AngouriMath.Entity; +using static AngouriMath.Entity.Number; + +namespace AngouriMath.Tests.Algebra +{ + /// + /// A root in exact numbers is put back into an equation in exact numbers to the working + /// precision, not to a percent of its largest term. (3x - 4) sqrt(x) + 3 + 3x - 4x^2 = 0, + /// squared, is a quartic whose roots come back in radicals, and one of them, 1.3233, is the + /// quartic's and not the equation's: it leaves 0.069 against a largest term of 7, under the + /// percent that a root found numerically is allowed. Found by a check of generated equations in + /// sqrt(x) beside powers of x; no issue was filed. + /// + [Trait("Area", "Algebra")] + public sealed class AnExactRootIsCheckedToTheWorkingPrecisionTest + { + [Fact] + public void ARootOfTheSquaredEquationOnlyIsNotAnswered() + { + const string residual = "((-4) + 3x) * sqrt(x) + 3 + 3x - 4x^2"; + var roots = Assert.IsType($"{residual} = 0".ToEntity().Solve("x")); + Assert.Equal(3, roots.Count); + foreach (var root in roots) + { + var left = Complex.Abs(residual.ToEntity().Substitute("x", root).EvalNumerical()).EDecimal; + Assert.True(left.CompareTo(EDecimal.FromString("1E-30")) < 0, $"{root} leaves {left}"); + } + Assert.DoesNotContain(roots, root => root.EvalNumerical() is Real real + && (real.EDecimal - EDecimal.FromString("1.3233")).Abs().CompareTo(EDecimal.FromString("0.001")) < 0); + } + } +} From c88f9fe5dfea4fec6f0d2e9ac4b3cfcece11052b Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Mon, 2 Nov 2026 04:05:59 +0000 Subject: [PATCH 2/2] Settle a residual within precision of zero before the terms are evaluated The exact branch of IsEvidenceAgainst evaluated every term of the equation again for the largest, which the decimal branch skips for a residual under 10^-6. A true root in exact numbers leaves 0 at the working precision, and no more than share^2 is under the bound whatever the terms are. The exactness check walks the tree by hand: Nodes builds lazy enumerators over a fresh substitution for every root checked. SolveEasy allocated 491,797 B/op against master's 452,228, +8.7%, and the gate refused it at 9.8%; now 452,580, +0.08%. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../Solvers/EquationSolver/SolveStatement.cs | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs index ca2dbaa51..b867e52be 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs @@ -205,6 +205,11 @@ internal static bool IsEvidenceAgainst(Entity substituted) if (IsExactAndAlgebraic(substituted)) { var share = EDecimal.Create(EInteger.One, MathS.Settings.DecimalPrecisionContext.Value.Precision.Divide(2).Negate()); + // No more than share^2, which a true root usually leaves, is under the bound whatever + // the terms are, so they are not evaluated again for their size: that cost nearly a + // tenth more allocation on solving x^2 + x + 1. + if (size.CompareTo(share.Multiply(share)) <= 0) + return false; return size.GreaterThan(EDecimal.Max(LargestTerm(substituted), share).Multiply(share)); } if (size.LessThan(MathS.Settings.PrecisionErrorCommon)) @@ -218,14 +223,22 @@ internal static bool IsEvidenceAgainst(Entity substituted) /// such as pi or a decimal is not: a special function may be evaluated in doubles, and /// a decimal is only as accurate as what found it. /// + /// + /// Walked by hand rather than through : the tree is a fresh + /// substitution, so the lazy enumerators behind that are built anew for every root checked, + /// and they were a percent more allocation on solving x^2 + x + 1. + /// private static bool IsExactAndAlgebraic(Entity expression) - => expression.Nodes.All(node => node switch + => expression switch { Complex number => number.RealPart is Rational && number.ImaginaryPart is Rational, - Sumf or Minusf or Mulf or Divf => true, - Powf(_, Rational) => true, + Sumf(var augend, var addend) => IsExactAndAlgebraic(augend) && IsExactAndAlgebraic(addend), + Minusf(var minuend, var subtrahend) => IsExactAndAlgebraic(minuend) && IsExactAndAlgebraic(subtrahend), + Mulf(var multiplier, var multiplicand) => IsExactAndAlgebraic(multiplier) && IsExactAndAlgebraic(multiplicand), + Divf(var dividend, var divisor) => IsExactAndAlgebraic(dividend) && IsExactAndAlgebraic(divisor), + Powf(var @base, Rational) => IsExactAndAlgebraic(@base), _ => false, - }); + }; /// /// The largest of the terms the equation adds up to at the root, which is the scale