Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,17 @@ 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
{
({ } lim1, { } lim2) when IsDeterminate(New(lim1, lim2), x) => (lim1, lim2),
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,12 +484,32 @@ internal static T WithinABudget<T>(int limits, System.Func<T> compute)
}
}

/// <summary>
/// 0 for a limit that came back as one finite constant minus itself, and the limit as it
/// is otherwise. The inner simplification leaves <c>u - u</c> as it is, so the limit of
/// <c>e^(a + x) - e^a</c> at 0 came back <c>e^a - e^a</c>. Nothing read that as 0, so the
/// difference quotient <c>(e^(a + x) - e^a) / x</c> was taken for a constant over a
/// vanishing divisor, which has no limit from both sides, and it answered NaN where the
/// limit is <c>e^a</c>. The constant's own domain condition is carried, as
/// <c>Simplify</c> carries it for <c>ln(a) - ln(a)</c>.
/// https://github.com/asc-community/AngouriMath/issues/2628
/// </summary>
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// The limit of a difference quotient at a symbolic point is the derivative there. The
/// numerator's limit came back as <c>u - u</c>, 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
/// </summary>
[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}");
}
}
}

/// <summary>A difference whose terms tend to one constant tends to 0.</summary>
[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,
});
}
}
Loading