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
16 changes: 16 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,27 @@ internal static T WithinABudget<T>(int limits, System.Func<T> compute)
/// 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
{
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)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// A limit that comes back as 0 written another way -- <c>sin(a) / cos(a) - tan(a)</c>,
/// <c>1/2 a + -1/2 a</c> -- is read as 0. Left as written it made the difference quotient of
/// <c>tan</c> at a symbolic point a constant over a vanishing divisor, which answered NaN.
/// https://github.com/asc-community/AngouriMath/issues/2639
/// </summary>
[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}");
}
}
}
}
}
Loading