From abc0540c69e3ee6647dc354b6e2275c859892324 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 29 Nov 2026 01:43:11 +0000 Subject: [PATCH 1/2] Search for numeric roots at the sizes of the equation's numbers (#2643) The numeric search starts from a grid from -10 to 10 (NewtonSetting.From and To). Its tolerances are made for roots and values of about unit size: the step it settles to is absolute from 1 up, and a root is kept by an absolute residual. 10^20 arcsin(x / 10^20) = x pi / 3 has the roots of arcsin(t) = t pi / 3 times 10^20, and it was answered with 0 alone, as was every k other than 0 in 10^k arcsin(x / 10^k) = x pi / 3, for k from -30 to 30 in steps of 3. Where the scaling is a substitution t = x / K, as in arcsin(x / K) = x pi / (3 K), the solver solves for t first and the grid is never at fault. Written K F(x / K) = 0, it is not. The equation is now searched once more for each decimal size of its whole and rational numbers, at least two orders from 1 and at most six of them. Each search is in x = 10^e t, with the values divided by the power of ten nearest their size at a few unit-size points, so the existing grid and tolerances meet roots and values of unit size. The roots are put back, exactly, by a power of ten. A root found at more than one size is kept once, compared relatively. A search at another size that cannot be run, or whose points are not isolated, is skipped, so it can add roots but never decline an equation the unit-size search answered. Over 84 equations in four families of K F(x / K), complete real root sets go from 9 to 84. Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 17 ++++ .../Solvers/NumericalSolving/NewtonSolver.cs | 79 +++++++++++++++++++ ...ricSearchFollowsTheSizeOfTheNumbersTest.cs | 46 +++++++++++ 3 files changed, 142 insertions(+) create mode 100644 Sources/Tests/UnitTests/Algebra/SolveTest/TheNumericSearchFollowsTheSizeOfTheNumbersTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index fdf4f6d11..dd83d80fa 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -337,6 +337,23 @@ sum, `arcsin(a)` or `arccos(1/3)`, is written as before | `cos(x) = -1` | `union({ pi / 2 - -pi / 2 + 2 * pi * n, -(pi / 2 - -pi / 2) + 2 * pi * n }, n in ZZ)` | `union({ pi + 2 * pi * n }, n in ZZ)` | | `2cos(x)^2 + cos(x) - 1 = 0` | four members, among them `pi / 2 - -pi / 2` and `-(pi / 2 - -pi / 2)` | `union({ pi + 2 * pi * n, pi / 3 + 2 * pi * n, -pi / 3 + 2 * pi * n }, n in ZZ)` | +### A root of a numeric search is found at the size of the equation's numbers + +**Not silent: an incomplete answer has more roots.** The numeric search starts from a grid from `-10` to `10`, and +its tolerances are made for roots of about unit size. `10^20 arcsin(x / 10^20) = x pi / 3` has the roots of +`arcsin(t) = t pi / 3` times `10^20`, and it was answered with `0` alone. An equation is now searched once more for each +decimal size of its whole and rational numbers, at least two orders from 1 and at most six sizes: in `x = 10^e t`, with +its values divided by their size there. The roots found are put back. An equation whose numbers are all near 1 is +searched as before, and where a search at another size cannot be run, the answer at unit size stands +([#2643](https://github.com/asc-community/AngouriMath/issues/2643)). + +| `.Solve("x")` | Was (2.10.0) | Now | +|---|---|---| +| `10^20 arcsin(x / 10^20) = x pi / 3` | `{ 0 }` | `{ 0, -50000000000000000000, 50000000000000000000 }` | +| `10^20 arctan(x / 10^20) = x / 2` | `{ 0 }` | `{ 0, -2.3311…E+20, 2.3311…E+20 }` | +| `10^(-12) arcsin(x / 10^(-12)) = x pi / 3` | `{ 0 }` | `{ 0, -5E-13, 5E-13 }` | +| `arcsin(x) = x pi / 3` | `{ -0.4999…, 0, 0.4999… }` | unchanged | + ### A definite integral of a function of abs is split where its argument changes sign **An answer where there was none.** A definite integral whose integrand holds `abs(u)` was left unevaluated unless the diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/NumericalSolving/NewtonSolver.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/NumericalSolving/NewtonSolver.cs index ef7b4c605..25f543391 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/NumericalSolving/NewtonSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/NumericalSolving/NewtonSolver.cs @@ -41,6 +41,85 @@ internal static HashSet SolveNt(Entity expr, Entity.Variable v, MathS.S => Search(expr, v, settings, forTheSolver: true); private static HashSet? Search(Entity expr, Entity.Variable v, MathS.Settings.NewtonSetting settings, bool forTheSolver) + { + if (SearchAtOneScale(expr, v, settings, forTheSolver) is not { } found) + return null; + // The grid, the steps the iteration settles to and the residual a root is kept by are all in + // absolute terms, made for roots and values of about unit size. An equation whose numbers are + // powers of ten far from 1 can have its roots, and its values, at that size instead: + // 10^20 arcsin(x / 10^20) = x pi / 3 has the roots of arcsin(t) = t pi / 3 times 10^20, which the + // grid from -10 to 10 does not reach, and was answered with 0 alone. So it is searched once more + // for each such size of its numbers, in x = 10^e t and divided by the size its values have there, + // and the roots found are put back. A root found at more than one size is kept once. + // https://github.com/asc-community/AngouriMath/issues/2643 + foreach (var exponent in ExponentsOfItsNumbers(expr)) + { + var size = Rational.Create(EInteger.FromInt32(10).Pow(Math.Abs(exponent))); + Entity scaleOfX = exponent > 0 ? size : 1 / size; + var inT = expr.Substitute(v, scaleOfX * v); + if (SizeOfItsValues(inT, v) is { } valueSize) + inT = (inT / valueSize).InnerSimplified; + // A search at another size that cannot be run, or whose points are not isolated, adds nothing; the + // answer at unit size stands as it was. + if (SearchAtOneScale(inT, v, settings, forTheSolver) is not { } atThisScale) + continue; + foreach (var t in atThisScale) + { + var root = Complex.Create(t.RealPart.EDecimal.ScaleByPowerOfTen(exponent), t.ImaginaryPart.EDecimal.ScaleByPowerOfTen(exponent)); + if (!found.Any(known => AreOneRootAtAnySize(known, root))) + found.Add(root); + } + } + return found; + } + + /// + /// The decimal exponents, at least two in size, of the whole and rational numbers in + /// , the largest first and at most of them. + /// + private static List ExponentsOfItsNumbers(Entity expr) + => expr.InnerSimplified.Nodes.OfType() + .Where(number => !number.ERational.IsZero) + .Select(number => (int)Math.Round(Math.Log10(Math.Abs(number.ERational.ToDouble())))) + .Where(exponent => Math.Abs(exponent) >= 2 && Math.Abs(exponent) < 300) + .Distinct() + .OrderByDescending(exponent => Math.Abs(exponent)).ThenByDescending(exponent => exponent) + .Take(MostOtherScales) + .ToList(); + + /// How many sizes other than 1 an equation is searched at. + private const int MostOtherScales = 6; + + /// + /// The power of ten nearest the largest value takes at a few points of + /// about unit size, or where it takes none there. + /// + private static Entity? SizeOfItsValues(Entity expr, Entity.Variable v) + { + var largest = 0.0; + foreach (var point in new[] { "0.37", "-0.83", "1.29", "-2.11", "0.61i" }) + if (expr.Substitute(v, point.ToEntity()).Evaled is Complex { IsFinite: true, IsNaN: false } value) + largest = Math.Max(largest, value.Abs().EDecimal.ToDouble()); + if (largest == 0 || double.IsInfinity(largest) || double.IsNaN(largest)) + return null; + var exponent = (int)Math.Round(Math.Log10(largest)); + var power = Rational.Create(EInteger.FromInt32(10).Pow(Math.Abs(exponent))); + return exponent >= 0 ? power : 1 / power; + } + + /// + /// Whether two roots found at different sizes are one, to a part in 10^9 of the larger, or both 0: relative + /// at every size, where is for roots of one search and absolute below 1. + /// + private static bool AreOneRootAtAnySize(Complex a, Complex b) + { + var difference = ((Complex)(a - b)).Abs().EDecimal; + var size = EDecimal.Max(a.Abs().EDecimal, b.Abs().EDecimal); + return difference.IsZero || difference.CompareTo(size.Multiply(EDecimal.Create(1, -9))) <= 0; + } + + /// The search at the size the settings give, which is unit size by default. + private static HashSet? SearchAtOneScale(Entity expr, Entity.Variable v, MathS.Settings.NewtonSetting settings, bool forTheSolver) { // Perform one iteration of searching for a root with Newton-Raphson method static Complex NewtonIter(FastExpression f, FastExpression df, NumericsComplex value, int precision) diff --git a/Sources/Tests/UnitTests/Algebra/SolveTest/TheNumericSearchFollowsTheSizeOfTheNumbersTest.cs b/Sources/Tests/UnitTests/Algebra/SolveTest/TheNumericSearchFollowsTheSizeOfTheNumbersTest.cs new file mode 100644 index 000000000..4826c6390 --- /dev/null +++ b/Sources/Tests/UnitTests/Algebra/SolveTest/TheNumericSearchFollowsTheSizeOfTheNumbersTest.cs @@ -0,0 +1,46 @@ +// +// 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; +using static AngouriMath.Entity.Set; + +namespace AngouriMath.Tests.Algebra.SolveTest +{ + /// + /// The numeric search starts from a grid from -10 to 10, and its tolerances are made for roots of about unit size. + /// 10^k arcsin(x / 10^k) = x pi / 3 has the roots of arcsin(t) = t pi / 3 times 10^k, and was + /// answered with 0 alone at k = 20. It is searched at the sizes of its numbers as well now. + /// https://github.com/asc-community/AngouriMath/issues/2643 + /// + [Trait("Area", "Algebra")] + public sealed class TheNumericSearchFollowsTheSizeOfTheNumbersTest + { + [Theory] + [InlineData("arcsin(x / 10^({0})) * 10^({0}) = x pi / 3", 0.5)] + [InlineData("arctan(x / 10^({0})) * 10^({0}) = x / 2", 2.331122370414422)] + [InlineData("tanh(x / 10^({0})) * 10^({0}) = x / 2", 1.915008048154537)] + public void TheRealRootsScaleWithTheEquation(string written, double positiveRoot) + { + foreach (var k in new[] { -30, -20, -12, -6, -3, 3, 6, 12, 20, 30 }) + { + var equation = string.Format(written, k); + var roots = Assert.IsType(equation.ToEntity().Solve("x")) + .Select(root => (Number.Complex)root.Evaled) + .Where(root => root.ImaginaryPart.IsZero) + .Select(root => root.RealPart.EDecimal.ScaleByPowerOfTen(-k).ToDouble()) + .ToList(); + foreach (var expected in new[] { -positiveRoot, 0, positiveRoot }) + Assert.True(roots.Any(root => System.Math.Abs(root - expected) <= 1e-9 * System.Math.Max(1, System.Math.Abs(expected))), + $"{equation}: {expected} * 10^{k} is not among [{string.Join(", ", roots)}] (each over 10^{k})"); + } + } + } +} From 7fc31030b3cf45ed2cabb362ee97d3a1fd2bdd9a Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 29 Nov 2026 03:21:27 +0000 Subject: [PATCH 2/2] Add a root from another size only where it holds as written (#2643) A search at another size runs on the equation divided by the size of its values at a few unit-size points. A point where the equation is small beside that size passes the search's absolute residual floor without being a root. (1 + 4 10^30 x)^(1/3) + sqrt(-1 - 10^30 x) = -4 has no roots. It is about 10^30 in size away from 0 and 5 + i at 0, and the search at 10^30 returned 0. AnalyticalEquationSolver took that non-empty result as the search's answer. WithoutSpuriousRoots then dropped 0 as no root, and the equation was answered { }, where a search that finds nothing leaves it unsolved. Two more x-scaled radcheck equations went the same way. A root from another size is now added only where it holds in the equation as written: defined there, and with a residual that StatementSolver.IsEvidenceAgainst does not count against it beside the equation's terms. The other sizes can then only add verified roots, and cannot turn an empty unit-size search into a non-empty one that verification then empties. The three radcheck equations are unsolved again, as on master. radcheck at x:10^(-30), x:10^(-20), x:10^(-12) and x:10^30 agrees with master: 0 of 200 differ at each. The K F(x/K) family is still 84 of 84 complete. Co-Authored-By: Claude Opus 5.5 --- .../Solvers/NumericalSolving/NewtonSolver.cs | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/NumericalSolving/NewtonSolver.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/NumericalSolving/NewtonSolver.cs index 25f543391..0a414fb06 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/NumericalSolving/NewtonSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/NumericalSolving/NewtonSolver.cs @@ -66,13 +66,35 @@ internal static HashSet SolveNt(Entity expr, Entity.Variable v, MathS.S foreach (var t in atThisScale) { var root = Complex.Create(t.RealPart.EDecimal.ScaleByPowerOfTen(exponent), t.ImaginaryPart.EDecimal.ScaleByPowerOfTen(exponent)); - if (!found.Any(known => AreOneRootAtAnySize(known, root))) + if (!found.Any(known => AreOneRootAtAnySize(known, root)) && HoldsAsWritten(expr, v, root)) found.Add(root); } } return found; } + /// + /// Whether a root found at another size holds in itself: defined there, and leaving a + /// residual that is no evidence against it beside the terms the equation adds up to. + /// + /// + /// A search at another size is of the equation divided by the size of its values at a few points, and a point + /// where the equation is small beside that size passes the search's absolute floor without being a root. + /// (1 + 4 10^30 x)^(1/3) + sqrt(-1 - 10^30 x) = -4, which has no roots, is about 10^30 in size away + /// from 0 and 5 + i at 0, and 0 came back from the search at 10^30. It was then dropped as no root + /// of the equation, and the search, which had found nothing at unit size, answered { }, where an empty search + /// leaves an equation unsolved. So a search at another size adds only roots that hold as the equation is + /// written, and never makes the answer of the search at unit size empty where it was not. + /// https://github.com/asc-community/AngouriMath/issues/2643 + /// + private static bool HoldsAsWritten(Entity expr, Entity.Variable v, Complex root) + { + var substituted = expr.Substitute(v, root); + return !AnalyticalSolving.StatementSolver.IsUndefinedExactly(substituted) + && substituted.Evaled is Complex { IsFinite: true } + && !AnalyticalSolving.StatementSolver.IsEvidenceAgainst(substituted); + } + /// /// The decimal exponents, at least two in size, of the whole and rational numbers in /// , the largest first and at most of them.