Skip to content

Commit b2b6abf

Browse files
A root in exact numbers is put back to the working precision (#2325)
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. A residual of no more than the square of that share is under the bound whatever the terms are, and is settled before they are evaluated again for their size; SolveEasy allocates 0.1% over the baseline. Generated equations against master at a2604a4: the radical cross-check's powers-of-x mode, 1000 at seed 41, loses its one finding, the unsound 1.3233. At seed 31 (200), the default mode (400 at seed 21), random polynomials (200 at seed 5, and 200 at seed 7 with a radical coefficient) and generated systems (1190) are unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent a2604a4 commit b2b6abf

4 files changed

Lines changed: 102 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,21 @@ carried it once combined: a second derivative of `1/sqrt(x^2 + 13)` stated `not
254254
| `"(a provided b > 0) + (c provided b > 0)".ToEntity().InnerSimplified` | `a + c provided b > 0 and b > 0` | `a + c provided b > 0` |
255255
| `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` |
256256

257+
### A root in exact numbers is put back to the working precision
258+
259+
**Silent: a number that is not a root, in exact numbers, is no longer answered as one.** Every root is put back into the
260+
equation, and one was dropped only where it left more than a hundredth of the largest term. That bound is for roots
261+
found numerically, which are only as accurate as the search. An exact root in an equation of exact numbers is evaluated
262+
to `MathS.Settings.DecimalPrecisionContext`'s digits, so a true root leaves only rounding, and it is now held to half of
263+
those digits. `(3x - 4) sqrt(x) + 3 + 3x - 4x^2 = 0` is solved by squaring, and one of the roots of the quartic that
264+
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
265+
decimal in it is still held to a hundredth. No issue was filed; a check of generated equations in `sqrt(x)` beside
266+
powers of `x` found it.
267+
268+
| Input | Was (2.6.0) | Now |
269+
|---|---|---|
270+
| `"(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 |
271+
257272
### A limit of a logarithm that is not decided is left unevaluated
258273

259274
**Not silent: what overflowed the stack now returns.** A limit of a logarithm whose argument has no limit, as

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
7272
[Tests/UnitTests/Algebra/SolveTest/ARadicalTimesAPowerTest.cs]
7373
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
7474

75+
[Tests/UnitTests/Algebra/SolveTest/AnExactRootIsCheckedToTheWorkingPrecisionTest.cs]
76+
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
77+
7578
[Tests/UnitTests/Core/FactorialCacheIsThreadSafeTest.cs]
7679
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
7780

‎Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ internal static Set UnsolvedWhereIndependenceIsDenied(Set roots, Entity conditio
118118
/// 1e-15, while every spurious one sits between 1.5 and 2. This is a hundredth:
119119
/// twenty times above the worst genuine root and a hundred and fifty times below
120120
/// the mildest spurious one.
121+
///
122+
/// It is a bound for roots found numerically. An equation and a root that are exact numbers
123+
/// are held to the working precision instead, in <see cref="IsEvidenceAgainst"/>.
121124
/// </remarks>
122125
[ConstantField] private static readonly EDecimal RelativeResidualTolerance = EDecimal.FromString("0.01");
123126

@@ -194,11 +197,49 @@ internal static bool IsEvidenceAgainst(Entity substituted)
194197
if (substituted.Evaled is not Number.Complex residual || !residual.IsFinite)
195198
return false;
196199
var size = residual.Abs().EDecimal;
200+
// Exact numbers in sums, products, quotients and rational powers are evaluated to the
201+
// working precision, so what they leave is either what rounding leaves of zero or a whole
202+
// quantity, and a percent is no bound for it. (3x - 4) sqrt(x) + 3 + 3x - 4x^2 = 0, raised
203+
// to a quartic, has a root of the quartic in radicals at 1.3233 that leaves 0.069 against
204+
// terms of 7, which a percent of them kept.
205+
if (IsExactAndAlgebraic(substituted))
206+
{
207+
var share = EDecimal.Create(EInteger.One, MathS.Settings.DecimalPrecisionContext.Value.Precision.Divide(2).Negate());
208+
// No more than share^2, which a true root usually leaves, is under the bound whatever
209+
// the terms are, so they are not evaluated again for their size: that cost nearly a
210+
// tenth more allocation on solving x^2 + x + 1.
211+
if (size.CompareTo(share.Multiply(share)) <= 0)
212+
return false;
213+
return size.GreaterThan(EDecimal.Max(LargestTerm(substituted), share).Multiply(share));
214+
}
197215
if (size.LessThan(MathS.Settings.PrecisionErrorCommon))
198216
return false;
199217
return size.GreaterThan(LargestTerm(substituted).Multiply(RelativeResidualTolerance));
200218
}
201219

220+
/// <summary>
221+
/// Whether <paramref name="expression"/> is exact numbers in sums, products, quotients and
222+
/// rational powers, which evaluation carries to the working precision. A function, a constant
223+
/// such as <c>pi</c> or a decimal is not: a special function may be evaluated in doubles, and
224+
/// a decimal is only as accurate as what found it.
225+
/// </summary>
226+
/// <remarks>
227+
/// Walked by hand rather than through <see cref="Entity.Nodes"/>: the tree is a fresh
228+
/// substitution, so the lazy enumerators behind that are built anew for every root checked,
229+
/// and they were a percent more allocation on solving <c>x^2 + x + 1</c>.
230+
/// </remarks>
231+
private static bool IsExactAndAlgebraic(Entity expression)
232+
=> expression switch
233+
{
234+
Complex number => number.RealPart is Rational && number.ImaginaryPart is Rational,
235+
Sumf(var augend, var addend) => IsExactAndAlgebraic(augend) && IsExactAndAlgebraic(addend),
236+
Minusf(var minuend, var subtrahend) => IsExactAndAlgebraic(minuend) && IsExactAndAlgebraic(subtrahend),
237+
Mulf(var multiplier, var multiplicand) => IsExactAndAlgebraic(multiplier) && IsExactAndAlgebraic(multiplicand),
238+
Divf(var dividend, var divisor) => IsExactAndAlgebraic(dividend) && IsExactAndAlgebraic(divisor),
239+
Powf(var @base, Rational) => IsExactAndAlgebraic(@base),
240+
_ => false,
241+
};
242+
202243
/// <summary>
203244
/// The largest of the terms the equation adds up to at the root, which is the scale
204245
/// the residual has to be read against. A single term is its own largest, so an
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using System.Linq;
9+
using AngouriMath.Extensions;
10+
using PeterO.Numbers;
11+
using Xunit;
12+
using static AngouriMath.Entity;
13+
using static AngouriMath.Entity.Number;
14+
15+
namespace AngouriMath.Tests.Algebra
16+
{
17+
/// <summary>
18+
/// A root in exact numbers is put back into an equation in exact numbers to the working
19+
/// precision, not to a percent of its largest term. <c>(3x - 4) sqrt(x) + 3 + 3x - 4x^2 = 0</c>,
20+
/// squared, is a quartic whose roots come back in radicals, and one of them, 1.3233, is the
21+
/// quartic's and not the equation's: it leaves 0.069 against a largest term of 7, under the
22+
/// percent that a root found numerically is allowed. Found by a check of generated equations in
23+
/// <c>sqrt(x)</c> beside powers of <c>x</c>; no issue was filed.
24+
/// </summary>
25+
[Trait("Area", "Algebra")]
26+
public sealed class AnExactRootIsCheckedToTheWorkingPrecisionTest
27+
{
28+
[Fact]
29+
public void ARootOfTheSquaredEquationOnlyIsNotAnswered()
30+
{
31+
const string residual = "((-4) + 3x) * sqrt(x) + 3 + 3x - 4x^2";
32+
var roots = Assert.IsType<Set.FiniteSet>($"{residual} = 0".ToEntity().Solve("x"));
33+
Assert.Equal(3, roots.Count);
34+
foreach (var root in roots)
35+
{
36+
var left = Complex.Abs(residual.ToEntity().Substitute("x", root).EvalNumerical()).EDecimal;
37+
Assert.True(left.CompareTo(EDecimal.FromString("1E-30")) < 0, $"{root} leaves {left}");
38+
}
39+
Assert.DoesNotContain(roots, root => root.EvalNumerical() is Real real
40+
&& (real.EDecimal - EDecimal.FromString("1.3233")).Abs().CompareTo(EDecimal.FromString("0.001")) < 0);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)