From 515d3038d5910dae0ff07ecd13c6b9189d1dbb26 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 29 Nov 2026 07:55:31 +0000 Subject: [PATCH] Read a limit that is 0 written another way as 0 (#2639) #2632 reads a limit that comes back as a constant minus itself as 0. A zero written any other way was still kept as a nonzero constant: the limit of tan(a + x) - tan(a) at 0 comes back sin(a) / cos(a) - tan(a), one step of l'Hopital's rule on (cosh(a x) - 1) / x^2 leaves 1/2 a + -1/2 a, and the difference quotients of tan, cot, sec, csc, artanh and arcosh at a symbolic point were NaN for it. A polynomial's constant coefficient written -a^4 + a^4 made (((a + x)^2)^2 - a^4) / x answer +oo times it. CancelledDifference now also asks a constant sum, free of x and of infinities, whether Simplify reduces it to 0, keeping a condition Simplify attaches. It is applied to Sumf's descent results as Minusf's already were, and ParseAsPolynomial drops a coefficient it reduces to 0. Found by a sweep of limits at symbolic points and with symbolic parameters compared by value at five values of the parameter: on master it reports 82 rows of NaN where the values settle, on this branch 12. Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 16 ++++++ Sources/.editorconfig | 3 ++ .../Limits/Solvers/Limit.Classes.cs | 6 ++- .../Limits/Solvers/Limit.Solvers.cs | 6 ++- .../Limits/Solvers/Solvers.Definition.cs | 25 ++++++++-- .../AZeroLimitWrittenAnotherWayTest.cs | 50 +++++++++++++++++++ 6 files changed, 98 insertions(+), 8 deletions(-) create mode 100644 Sources/Tests/UnitTests/Calculus/AZeroLimitWrittenAnotherWayTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index d50fcf3b7..ba4dc5bb6 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -259,6 +259,22 @@ now 0, with the constant's domain condition where it has one | `e^(a + x) - e^a` at 0 | `e ^ a - e ^ a` | `0` | | `ln(a + x) - ln(a)` at 0 | `ln(a) - ln(a)` | `0 provided not a = 0` | +### A limit that is 0 written another way is read as 0 + +**Not silent: a wrong answer is corrected.** Since #2628, a limit that comes back as a constant minus itself is 0. +A zero written any other way, such as `sin(a) / cos(a) - tan(a)` or `1/2 a + -1/2 a`, was still kept as a nonzero +constant, and a quotient over a vanishing divisor then answered `NaN` or an infinity times it. A constant sum that +`Simplify` reduces to 0 is now 0, and so is such a coefficient where a limit reads a polynomial +([#2639](https://github.com/asc-community/AngouriMath/issues/2639)). + +| `.Limit(x, 0)` | Was (2.10.0) | Now | +|---|---|---| +| `(tan(a + x) - tan(a)) / x` | `NaN` | `1 / cos(a) ^ 2` | +| `(cosh(a x) - 1) / x^2` | `NaN` | `a ^ 2 * 2 / 4` | +| `(cosh(a x) - 1) / x` | `1/2 * a + -1/2 * a` | `0` | +| `(artanh(a + x) - artanh(a)) / x` | `NaN` | `(-1) / (a ^ 2 + -1)` | +| `(((a + x)^2)^2 - a^4) / x` | `+oo * (-a ^ 4 + a ^ 4)` | `4 * a ^ 3` | + ### A root found by Newton's method prints only digits that are right **Silent, in the digits.** A root that the solver could only find numerically was computed in `double` and widened to diff --git a/Sources/.editorconfig b/Sources/.editorconfig index 3b57a7f5b..6833dca16 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -48,6 +48,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [AngouriMath/Functions/Continuous/StepPieces.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/Calculus/AZeroLimitWrittenAnotherWayTest.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 + [AngouriMath/Functions/Continuous/Limits/Gruntz/SeriesExponent.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/Limits/Solvers/Limit.Classes.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs index 135097319..df4e593fe 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs @@ -36,7 +36,7 @@ partial record Sumf internal override Entity? ComputeLimitDivideEtImpera(Variable x, Entity dist, ApproachFrom side) { if (ComputeLimitImpl(this, x, dist, side) is { } lim) - return lim; + return CancelledDifference(lim, x); var (augend, addend) = (Augend.ComputeLimitDivideEtImpera(x, dist, side), Addend.ComputeLimitDivideEtImpera(x, dist, side)) switch { @@ -44,7 +44,9 @@ partial record Sumf var (lim1, lim2) => (lim1 is { IsFinite: true } ? lim1 : Augend, lim2 is { IsFinite: true } ? lim2 : Addend) }; - return ComputeLimitImpl(New(augend, addend), x, dist, side) ?? ByTheLimitsOfItsTerms(this, x, dist, side); + return ComputeLimitImpl(New(augend, addend), x, dist, side) is { } byParts + ? CancelledDifference(byParts, x) + : ByTheLimitsOfItsTerms(this, x, dist, side); } } diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Solvers.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Solvers.cs index f2baac681..771579792 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Solvers.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Solvers.cs @@ -37,7 +37,11 @@ internal static class LimitSolvers // at every point near 1/2, and a coefficient evaluated to NaN answered NaN. if (simplified.Evaled is Number { IsFinite: false }) return null; - if (simplified != Integer.Zero) + // A coefficient that is 0 written another way is 0 too: (a + x)^4 - a^4 over x has the + // constant term -a^4 + a^4, and kept, it read as a constant over a vanishing divisor. + // https://github.com/asc-community/AngouriMath/issues/2639 + if (simplified != Integer.Zero + && LimitFunctional.CancelledDifference(simplified, x) is not (Integer { IsZero: true } or Providedf(Integer { IsZero: true }, _))) { filteredDictionary.Add(monomial.Key, simplified); } diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs index 49d953778..84dc7bc4b 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs @@ -495,12 +495,27 @@ internal static T WithinABudget(int limits, System.Func compute) /// https://github.com/asc-community/AngouriMath/issues/2628 /// internal static Entity CancelledDifference(Entity limit, Variable x) - => limit is Minusf(var left, var right) && (left == right || left.InnerSimplified == right.InnerSimplified) && !left.ContainsNode(x) - && !left.Nodes.Any(static node => node is Number { IsFinite: false } or Limitf) - ? left.DomainCondition == Boolean.True + { + if (limit is not (Minusf or Sumf) || limit.ContainsNode(x) + || limit.Nodes.Any(static node => node is Number { IsFinite: false } or Number { IsNaN: true } or Limitf)) + return limit; + if (limit is Minusf(var left, var right) && (left == right || left.InnerSimplified == right.InnerSimplified)) + return left.DomainCondition == Boolean.True ? Integer.Zero - : new Providedf(Integer.Zero, left.DomainCondition) - : limit; + : new Providedf(Integer.Zero, left.DomainCondition); + // And 0 written another way: sin(a) / cos(a) - tan(a), which the limit of tan(a + x) - tan(a) + // comes back as, or 1/2 a + -1/2 a, from one step of l'Hopital's rule on (cosh(a x) - 1) / x^2. + // Neither is u - u, and left as written each made a difference quotient at a symbolic point a + // constant over a vanishing divisor, NaN from both sides. Only a constant sum is asked, and only + // whether it simplifies to 0; anything else is kept as it came. + // https://github.com/asc-community/AngouriMath/issues/2639 + return limit.Simplify() switch + { + Integer { IsZero: true } zero => zero, + Providedf(Integer { IsZero: true }, _) conditional => conditional, + _ => limit, + }; + } public static Entity? ComputeLimit(Entity expr, Variable x, Entity dest, ApproachFrom side = ApproachFrom.BothSides, bool acceptNaN = false) { diff --git a/Sources/Tests/UnitTests/Calculus/AZeroLimitWrittenAnotherWayTest.cs b/Sources/Tests/UnitTests/Calculus/AZeroLimitWrittenAnotherWayTest.cs new file mode 100644 index 000000000..86d7903b4 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/AZeroLimitWrittenAnotherWayTest.cs @@ -0,0 +1,50 @@ +// +// 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.Core; +using AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// A limit that comes back as 0 written another way -- sin(a) / cos(a) - tan(a), + /// 1/2 a + -1/2 a -- is read as 0. Left as written it made the difference quotient of + /// tan at a symbolic point a constant over a vanishing divisor, which answered NaN. + /// https://github.com/asc-community/AngouriMath/issues/2639 + /// + [Trait("Area", "Calculus")] + public sealed class AZeroLimitWrittenAnotherWayTest + { + [Theory] + [InlineData("(tan(a + x) - tan(a)) / x", "0", "1 / cos(a)^2")] + [InlineData("(tan(x) - tan(a)) / (x - a)", "a", "1 / cos(a)^2")] + [InlineData("(cotan(a + x) - cotan(a)) / x", "0", "-1 / sin(a)^2")] + [InlineData("(sec(a + x) - sec(a)) / x", "0", "sin(a) / cos(a)^2")] + [InlineData("(cosh(a * x) - 1) / x^2", "0", "a^2 / 2")] + [InlineData("(cosh(a * x) - 1) / x", "0", "0")] + [InlineData("(artanh(a + x) - artanh(a)) / x", "0", "1 / (1 - a^2)")] + [InlineData("(((a + x)^2)^2 - a^4) / x", "0", "4 * a^3")] + public void IsTheValue(string expression, string point, string expected) + { + foreach (var side in new[] { ApproachFrom.BothSides, ApproachFrom.Left, ApproachFrom.Right }) + { + var limit = expression.ToEntity().Limit("x", point.ToEntity(), side); + Assert.DoesNotContain(limit.Nodes, node => node is Entity.Limitf); + foreach (var value in new[] { "-7/10", "3/5", "19/10" }) + { + var got = limit.Substitute("a", value.ToEntity()).Evaled; + var want = expected.ToEntity().Substitute("a", value.ToEntity()).Evaled; + Assert.True(got is Entity.Number.Complex { IsNaN: false } g && want is Entity.Number.Complex w + && ((Entity.Number.Complex)(g - w).Evaled).Abs() < 1e-20, + $"{expression} at {point} from {side}: {limit} = {got} at a = {value}, against {want}"); + } + } + } + } +}