diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 4c1062ec1..d0dc7bf5a 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -171,6 +171,25 @@ folded. The logarithm of any other point on the unit circle, such as `-1/2 + i s | `"sin(x) + cos(x) = 1".Solve("x")` | `union({ 2 * pi * n, ln(i) / i + 2 * pi * n }, n in ZZ)` | `union({ 2 * pi * n, pi / 2 + 2 * pi * n }, n in ZZ)` | | `"sin(2x) = sin(x)".Solve("x")` | the root `ln(-1) / i + 2 * pi * n` | `pi + 2 * pi * n` | +### The limit of a difference quotient at a symbolic point is the derivative + +**Not silent: a wrong answer is corrected.** The limit of `(e^(a + x) - e^a) / x` at 0 was `NaN`, which claims there +is no limit, and the same held for `sqrt`, `sin`, `ln` and cube roots. The limit of the numerator came back as +`e^a - e^a`, which the inner simplification leaves written. Nothing read that as 0, so the quotient was taken for a +constant over a vanishing divisor, whose two sides disagree. A limit that comes back as one constant minus itself is +now 0, with the constant's domain condition where it has one +([#2628](https://github.com/asc-community/AngouriMath/issues/2628)). + +| `.Limit(x, ...)` | Was (2.10.0) | Now | +|---|---|---| +| `(e^(a + x) - e^a) / x` at 0 | `NaN` | `e ^ a` | +| `(sin(a + x) - sin(a)) / x` at 0 | `NaN` | `cos(a)` | +| `(ln(a + x) - ln(a)) / x` at 0 | `NaN` | `1 / a` | +| `(sqrt(a + x) - sqrt(a)) / x` at 0 | `NaN` | `a ^ (-1/2) / 2` | +| `(sqrt(x) - sqrt(b)) / (x - b)` at `b` | `NaN` | `b ^ (-1/2) / 2` | +| `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 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 7e73dcafb..0f845a56f 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -72,6 +72,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [AngouriMath/Functions/TreeAnalyzer/RealValued.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/ADifferenceQuotientAtASymbolicPointTest.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/Harnesses/ModenovCorpus/*.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 0f5eaaa1d..135097319 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs @@ -53,7 +53,7 @@ partial record Minusf 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 (minuend, subtrahend) = (Minuend.ComputeLimitDivideEtImpera(x, dist, side), Subtrahend.ComputeLimitDivideEtImpera(x, dist, side)) switch { @@ -61,7 +61,9 @@ partial record Minusf var (lim1, lim2) => (lim1 is { IsFinite: true } ? lim1 : Minuend, lim2 is { IsFinite: true } ? lim2 : Subtrahend) }; - return ComputeLimitImpl(New(minuend, subtrahend), x, dist, side) ?? ByTheLimitsOfItsTerms(this, x, dist, side); + return ComputeLimitImpl(New(minuend, subtrahend), x, dist, side) is { } byParts + ? CancelledDifference(byParts, x) + : ByTheLimitsOfItsTerms(this, x, dist, side); } } diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs index 219ab66e1..49d953778 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs @@ -484,12 +484,32 @@ internal static T WithinABudget(int limits, System.Func compute) } } + /// + /// 0 for a limit that came back as one finite constant minus itself, and the limit as it + /// is otherwise. The inner simplification leaves u - u as it is, so the limit of + /// e^(a + x) - e^a at 0 came back e^a - e^a. Nothing read that as 0, so the + /// difference quotient (e^(a + x) - e^a) / x was taken for a constant over a + /// vanishing divisor, which has no limit from both sides, and it answered NaN where the + /// limit is e^a. The constant's own domain condition is carried, as + /// Simplify carries it for ln(a) - ln(a). + /// 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 + ? Integer.Zero + : new Providedf(Integer.Zero, left.DomainCondition) + : limit; + public static Entity? ComputeLimit(Entity expr, Variable x, Entity dest, ApproachFrom side = ApproachFrom.BothSides, bool acceptNaN = false) { var key = (expr, x, dest, side, acceptNaN); if (limitMemo is { } memo && memo.TryGetValue(key, out var known)) return known; - var found = ComputeLimitUnmemoised(expr, x, dest, side, acceptNaN); + var found = ComputeLimitUnmemoised(expr, x, dest, side, acceptNaN) is { } computed + ? CancelledDifference(computed, x) + : null; if (found is { } && limitMemo is { } store && found.Evaled != MathS.NaN) store[key] = found; return found; diff --git a/Sources/Tests/UnitTests/Calculus/ADifferenceQuotientAtASymbolicPointTest.cs b/Sources/Tests/UnitTests/Calculus/ADifferenceQuotientAtASymbolicPointTest.cs new file mode 100644 index 000000000..d5224409f --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/ADifferenceQuotientAtASymbolicPointTest.cs @@ -0,0 +1,66 @@ +// +// 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; +using System.Linq; +using AngouriMath.Core; +using AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// The limit of a difference quotient at a symbolic point is the derivative there. The + /// numerator's limit came back as u - u, which the inner simplification leaves + /// written, so the quotient was read as a constant over a vanishing divisor and answered + /// NaN, the claim that there is no limit. + /// https://github.com/asc-community/AngouriMath/issues/2628 + /// + [Trait("Area", "Calculus")] + public sealed class ADifferenceQuotientAtASymbolicPointTest + { + [Theory] + [InlineData("(sqrt(a + x) - sqrt(a)) / x", "0", "1 / (2 * sqrt(a))")] + [InlineData("(e^(a + x) - e^a) / x", "0", "e^a")] + [InlineData("(sin(a + x) - sin(a)) / x", "0", "cos(a)")] + [InlineData("(ln(a + x) - ln(a)) / x", "0", "1 / a")] + [InlineData("((a + x)^(1/3) - a^(1/3)) / x", "0", "1 / (3 * a^(2/3))")] + [InlineData("(sqrt(a + x) - sqrt(a - x)) / x", "0", "1 / sqrt(a)")] + [InlineData("(sqrt(x) - sqrt(b)) / (x - b)", "b", "1 / (2 * sqrt(b))")] + public void IsTheDerivative(string quotient, string point, string derivative) + { + foreach (var side in new[] { ApproachFrom.BothSides, ApproachFrom.Left, ApproachFrom.Right }) + { + var limit = quotient.ToEntity().Limit("x", point.ToEntity(), side); + Assert.DoesNotContain(limit.Nodes, node => node is Entity.Limitf); + Assert.NotEqual(MathS.NaN, limit.Evaled); + // Compared by value at points on either side of 0 for each symbol, since the + // forms the limit and the derivative are written in differ. + foreach (var value in new[] { 0.7, 2.5, 9 }) + { + var got = (Entity.Number.Complex)limit.Substitute("a", value).Substitute("b", value).Evaled; + var expected = (Entity.Number.Complex)derivative.ToEntity().Substitute("a", value).Substitute("b", value).Evaled; + Assert.True(((Entity.Number.Complex)(got - expected).Evaled).Abs() < 1e-20, + $"{quotient} at {point} from {side}: {limit} = {got} at {value}, against {expected}"); + } + } + } + + /// A difference whose terms tend to one constant tends to 0. + [Theory] + [InlineData("e^(a + x) - e^a")] + [InlineData("sqrt(a + x) - sqrt(a)")] + [InlineData("ln(a + x) - ln(a)")] + public void ADifferenceOfOneLimitIsZero(string difference) + => Assert.Equal(Entity.Number.Integer.Zero, + difference.ToEntity().Limit("x", 0).Evaled switch + { + Entity.Providedf(var inner, _) => inner, + var bare => bare, + }); + } +}