diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index d5b2b35ed..f0fa88b8d 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -119,6 +119,21 @@ one point, or else `Simplify` proving it zero. A singular matrix answers as `[[1 | `"[[sin(x)^2, 1], [1 - cos(x)^2, 1]]^(-1)".Simplify()` | `NaN` | `1 / [[sin(x) ^ 2, 1], [sin(x) ^ 2, 1]]` | | `"[[e^x, e^(2x)], [2, e^x]]^(-1)".Simplify()` | its inverse | unchanged | +### A root near 0 of an equation in tanh, sinh or cosh is not a near copy of 0 + +**Not silent: a false root is gone.** tanh, sinh and cosh are written with exponentials, and near 0 the parts of +`e^u - 1` or `e^u - e^(-u)` cancel. A root the numeric search found near 0 was judged against those parts, of size +`10^3` and more, rather than against the terms of the equation, and kept beside 0. A root found in decimals is now +also judged against the terms of the sum the equation is, each read whole, and dropped where it leaves a residual +large beside them ([#2645](https://github.com/asc-community/AngouriMath/issues/2645)). + +| `.Solve("x")` | Was (2.10.0) | Now | +|---|---|---| +| `10^3 tanh(x / 10^3) = x / 2` | `{ 0, 1.1013…E-13 }` | `{ 0 }` | +| `10^20 tanh(x / 10^20) = x / 3` | `{ -3.1958…E-13, 0 }` | `{ 0 }` | +| `10^6 sinh(x / 10^6) = 2 x` | `{ -4.9027…E-13, 0, 4.9027…E-13 }` | `{ 0 }` | +| `tanh(x) = x / 2` | its roots | unchanged | + ### The whole part of a large rational power is taken out **Silent, in the printed form only.** A rational raised to a rational power was written as a whole part times a diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs index 56345910d..39609de86 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs @@ -233,6 +233,8 @@ private static RootVerdict Verdict(Entity equation, Variable x, Entity root) case ResidualWeight.CannotTell: return RootVerdict.ShownNotARoot; } + if (!IsExact(root) && IsLargeBesideTheTermsAsWritten(equation, x, root, substituted)) + return RootVerdict.Spurious; return !substituted.Vars.Any() && IsAboveTheIntervalFloor(equation, x, root) && IsShownNotZero(substituted) ? RootVerdict.ShownNotARoot : RootVerdict.Kept; @@ -626,6 +628,66 @@ private static bool IsExactAndAlgebraic(Entity expression) _ => false, }; + /// + /// Whether a root found in decimals leaves a residual large beside the terms of + /// as it is written in , each term's size its value at the root. + /// + /// + /// reads the equation after the root is put in, where every part is a number, so it + /// multiplies out a sum that is a factor of a term, or the numerator of one, though that sum depends on the + /// root and cancels there. tanh is written with exponentials, and 10^3 tanh(x / 10^3) = x / 2 is + /// 10^3 (e^(2x / 10^3) - 1) / (e^(2x / 10^3) + 1) - x / 2: near 0, e^(2x / 10^3) - 1 cancels, and was + /// read as terms of 500, beside which the residual 5.5e-14 at 1.1e-13 was nothing, so 1.1e-13 was answered + /// beside 0. The terms are 1.1e-13 and 5.5e-14. Here the terms are those of the sum the equation is, each read + /// whole, and that sum is reached through a factor or a divisor that does not depend on , + /// as the terms of (sin(x) - x) 10^-12 are sin(x) 10^-12 and x 10^-12. sinh and cosh are + /// written with exponentials too, and 10^6 sinh(x / 10^6) = 2x had ±4.9e-13 beside 0 the same way. + /// https://github.com/asc-community/AngouriMath/issues/2645 + /// + private static bool IsLargeBesideTheTermsAsWritten(Entity equation, Variable x, Entity root, Entity substituted) + { + if (substituted.Evaled is not Number.Complex residual || !residual.IsFinite) + return false; + var cancels = false; + if (LargestTermAsWritten(equation, x, root, ref cancels) is not { } largest || !cancels) + return false; + return residual.Abs().EDecimal.GreaterThan(largest.Multiply(RelativeResidualTolerance)); + } + + /// + /// The largest term of at : the terms are those of the sum the + /// equation is, reached through factors and divisors that do not depend on , each its value + /// at the root and not split further; where a part has no finite value there. + /// + private static EDecimal? LargestTermAsWritten(Entity expr, Variable x, Entity root, ref bool cancels) + { + switch (expr) + { + case Sumf or Minusf: + cancels = true; + var largest = EDecimal.Zero; + foreach (var term in Sumf.LinearChildren(expr)) + { + if (SizeAt(term, x, root) is not { } size) + return null; + largest = EDecimal.Max(largest, size); + } + return largest; + case Mulf(var multiplier, var multiplicand) when !multiplier.ContainsNode(x): + return SizeAt(multiplier, x, root) is { } factor && LargestTermAsWritten(multiplicand, x, root, ref cancels) is { } rest ? factor.Multiply(rest) : null; + case Mulf(var multiplier, var multiplicand) when !multiplicand.ContainsNode(x): + return SizeAt(multiplicand, x, root) is { } constant && LargestTermAsWritten(multiplier, x, root, ref cancels) is { } other ? constant.Multiply(other) : null; + case Divf(var dividend, var divisor) when !divisor.ContainsNode(x): + return SizeAt(divisor, x, root) is { } below && !below.IsZero && LargestTermAsWritten(dividend, x, root, ref cancels) is { } above + ? above.Divide(below, MathS.Settings.DecimalPrecisionContext) : null; + default: + return SizeAt(expr, x, root); + } + + static EDecimal? SizeAt(Entity part, Variable x, Entity root) + => part.Substitute(x, root).Evaled is Number.Complex { IsFinite: true } value ? value.Abs().EDecimal : null; + } + /// /// The largest of the terms the equation adds up to at the root, which is the scale /// the residual has to be read against, and in whether there diff --git a/Sources/Tests/UnitTests/Algebra/SolveTest/ARootNearZeroIsJudgedByTheTermsAsWrittenTest.cs b/Sources/Tests/UnitTests/Algebra/SolveTest/ARootNearZeroIsJudgedByTheTermsAsWrittenTest.cs new file mode 100644 index 000000000..8e8571317 --- /dev/null +++ b/Sources/Tests/UnitTests/Algebra/SolveTest/ARootNearZeroIsJudgedByTheTermsAsWrittenTest.cs @@ -0,0 +1,57 @@ +// +// 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 +{ + /// + /// tanh, sinh and cosh are written with exponentials, and near 0 the parts of e^u - 1 or e^u - e^(-u) cancel. A + /// candidate the numeric search found near 0 was judged against those parts, of size 10^3 and more, and kept: + /// 10^3 tanh(x / 10^3) = x / 2 was { 0, 1.1e-13 }, where 1.1e-13 leaves half the size of its terms. + /// https://github.com/asc-community/AngouriMath/issues/2645 + /// + [Trait("Area", "Algebra")] + public sealed class ARootNearZeroIsJudgedByTheTermsAsWrittenTest + { + // Every real root is a true one: each is 0 or at least the size the equation's terms put it at. + [Theory] + [InlineData("10^3 tanh(x / 10^3) = x / 2", 1e-3)] + [InlineData("10^6 tanh(x / 10^6) = x / 2", 1e-3)] + [InlineData("10^6 tanh(x / 10^6) = x / 3", 1e-3)] + [InlineData("10^20 tanh(x / 10^20) = x / 3", 1e-3)] + [InlineData("10^6 sinh(x / 10^6) = 2 x", 1e-3)] + public void NoRootIsANearCopyOfZero(string equation, double smallestTrueRoot) + { + var roots = Assert.IsType(equation.ToEntity().Solve("x")) + .Select(root => (Number.Complex)root.Evaled).ToList(); + Assert.Contains(roots, root => root.IsZero); + foreach (var root in roots.Where(root => !root.IsZero)) + Assert.True(root.Abs().EDecimal.ToDouble() >= smallestTrueRoot, $"{equation}: {root} is a near copy of 0"); + } + + // The roots found at unit size are kept, those near 0 among them. + [Theory] + [InlineData("tanh(x) = x / 2", 1.915008048154537)] + [InlineData("sinh(x) = 2 x", 2.177318984965310)] + [InlineData("sin(x) = x / 2", 1.895494267033981)] + [InlineData("(sin(x) - x / 2) * 10^(-12) = 0", 1.895494267033981)] + public void AUnitSizeRootIsKept(string equation, double positive) + { + var reals = Assert.IsType(equation.ToEntity().Solve("x")) + .Select(root => (Number.Complex)root.Evaled).Where(root => root.ImaginaryPart.IsZero) + .Select(root => root.RealPart.EDecimal.ToDouble()).ToList(); + foreach (var expected in new[] { -positive, 0, positive }) + Assert.Contains(reals, root => System.Math.Abs(root - expected) < 1e-9); + } + } +}