diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index c9d1249ff..1c5ea6720 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -390,6 +390,22 @@ 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` | +### An infinity beside a symbol, and a quotient of roots of negative radicands, have limits + +**Not silent: limits that were left unevaluated have values.** A term tending to an infinity beside a constant with a +symbol in it, such as `tan(x) - a` at `pi/2` from the left, was left unevaluated where `tan(x) - 3` was `+oo`: the +symbol is a finite number, as `+oo * a` already reads it. And `sqrt(u) / sqrt(v)` with both radicands negative on the +way in is `sqrt(u / v)`, since each root is `i` times a real one; it was left unevaluated on that side only +([#2656](https://github.com/asc-community/AngouriMath/issues/2656)). + +| `.Limit(x, ..., side)` | Was (2.10.0) | Now | +|---|---|---| +| `tan(x) - a` at `pi/2`, left | `limitleft(tan(x) - a, x, pi / 2)` | `+oo` | +| `tan(x) - a` at `pi/2`, right | unevaluated | `-oo` | +| `cotan(x) - a` at 0, right | unevaluated | `+oo` | +| `sqrt(cos(x)) / sqrt(pi/2 - x)` at `pi/2`, right | unevaluated | `1` | +| `sqrt(sin(x)) / sqrt(x)` at 0, left | unevaluated | `1` | + ### 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 diff --git a/Sources/.editorconfig b/Sources/.editorconfig index cf33fc601..4f4319576 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -75,6 +75,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [AngouriMath/Functions/PredicateEntailment.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/OneSidedLimitsAtAPoleAndAtNegativeRadicandsTest.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/Common/AConstantFactorIsPairedAcrossAQuotientTest.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 f6bc2cd0a..3ecfc803c 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs @@ -37,8 +37,11 @@ partial record Sumf { if (ComputeLimitImpl(this, x, dist, side) is { } lim) return CancelledDifference(lim, x); + var (augendLimit, addendLimit) = (Augend.ComputeLimitDivideEtImpera(x, dist, side), Addend.ComputeLimitDivideEtImpera(x, dist, side)); + if (AnInfinityBesideAConstant(augendLimit, addendLimit, x) is { } infinite) + return infinite; var (augend, addend) = - (Augend.ComputeLimitDivideEtImpera(x, dist, side), Addend.ComputeLimitDivideEtImpera(x, dist, side)) switch + (augendLimit, addendLimit) switch { ({ } lim1, { } lim2) when IsDeterminate(New(lim1, lim2), x) => (lim1, lim2), var (lim1, lim2) => (lim1 is { IsFinite: true } ? lim1 : Augend, @@ -56,8 +59,11 @@ partial record Minusf { if (ComputeLimitImpl(this, x, dist, side) is { } lim) return CancelledDifference(lim, x); + var (minuendLimit, subtrahendLimit) = (Minuend.ComputeLimitDivideEtImpera(x, dist, side), Subtrahend.ComputeLimitDivideEtImpera(x, dist, side)); + if (AnInfinityBesideAConstant(minuendLimit, subtrahendLimit is Real { IsFinite: false, IsNaN: false } infinity ? (-infinity).Evaled : subtrahendLimit, x) is { } infinite) + return infinite; var (minuend, subtrahend) = - (Minuend.ComputeLimitDivideEtImpera(x, dist, side), Subtrahend.ComputeLimitDivideEtImpera(x, dist, side)) switch + (minuendLimit, subtrahendLimit) switch { ({ } lim1, { } lim2) when IsDeterminate(New(lim1, lim2), x) => (lim1, lim2), var (lim1, lim2) => (lim1 is { IsFinite: true } ? lim1 : Minuend, @@ -69,6 +75,28 @@ partial record Minusf } } + /// + /// The real infinity one term tends to, where the other tends to a constant that is not an infinity, or + /// . A symbol is a finite number, as it is everywhere else here (+oo * a), so + /// +oo - a is +oo; evaluated, it is not a number, and the descent read it as undecided. tan(x) - a at + /// pi/2 from the left was left unevaluated where tan(x) - 3 was +oo. + /// https://github.com/asc-community/AngouriMath/issues/2656 + /// + private static Entity? AnInfinityBesideAConstant(Entity? first, Entity? second, Variable x) + { + static bool IsARealInfinity(Entity? e) => e is Real { IsFinite: false, IsNaN: false }; + // A constant as written and as evaluated: the descent can hand back 1 / 0, which holds no + // infinity as written and is no number at all, and read as a constant beside -oo it + // answered (cot(x) - 1/x) / (2x) at 0 from the right with -oo where it is -1/6. + static bool IsAConstant(Entity? e, Variable x) + => e is not null && !e.ContainsNode(x) + && !e.Nodes.Any(static node => node is Number { IsFinite: false } or Number { IsNaN: true } or Limitf) + && !e.Evaled.Nodes.Any(static node => node is Number { IsFinite: false } or Number { IsNaN: true }); + if (IsARealInfinity(first) && IsAConstant(second, x)) return first; + if (IsARealInfinity(second) && IsAConstant(first, x)) return second; + return null; + } + /// /// The limit of a sum or a difference with each term whose limit is finite replaced by that limit, the /// terms that are not left together as one sum, or where no term has a finite @@ -224,6 +252,18 @@ partial record Divf // taken from it. x ln(x) at 0 comes back as 0 * (-oo), which is no answer, and from both sides that was read // as NaN, where x / log(x, 2) read as written is 0. And only over a constant: x^x / (x / x) as a product is // x^x x / x, which took twenty times as long and answered from both sides of 0 where x^x itself is NaN. + // A quotient of two square roots whose radicands are both negative on the way in is the + // square root of the quotient: each root is i times a real one, and the two i cancel. + // sqrt(cos(x)) / sqrt(pi/2 - x) at pi/2 from the right was left unevaluated where it is 1, + // as it is from the left, where both radicands are positive and the descent reads it. + // https://github.com/asc-community/AngouriMath/issues/2656 + if (Dividend is Powf(var above, Rational { ERational: var p }) && Divisor is Powf(var below, Rational { ERational: var q }) + && p.Equals(PeterO.Numbers.ERational.Create(1, 2)) && q.Equals(p) + && above.ContainsNode(x) && below.ContainsNode(x) + && HoldsNear(above < 0 & below < 0, x, dist, side) is true + && ComputeLimit(MathS.Sqrt(above / below), x, dist, side) is { } asOneRoot + && asOneRoot.Evaled != MathS.NaN) + return asOneRoot; if (Divisor is Divf(var over, var under) && under.ContainsNode(x) && !over.ContainsNode(x) && New(Dividend * under, over).ComputeLimitDivideEtImpera(x, dist, side) is { } asProducts && !asProducts.ContainsNode(x) && asProducts.Evaled != MathS.NaN) diff --git a/Sources/Tests/UnitTests/Calculus/OneSidedLimitsAtAPoleAndAtNegativeRadicandsTest.cs b/Sources/Tests/UnitTests/Calculus/OneSidedLimitsAtAPoleAndAtNegativeRadicandsTest.cs new file mode 100644 index 000000000..a6636af80 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/OneSidedLimitsAtAPoleAndAtNegativeRadicandsTest.cs @@ -0,0 +1,60 @@ +// +// 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 +{ + /// + /// An infinity beside a symbolic constant is that infinity, and a quotient of square roots + /// whose radicands are both negative on the way in is the root of the quotient. Both were left + /// unevaluated. https://github.com/asc-community/AngouriMath/issues/2656 + /// + [Trait("Area", "Calculus")] + public sealed class OneSidedLimitsAtAPoleAndAtNegativeRadicandsTest + { + [Theory] + [InlineData("tan(x) - a", "pi/2", ApproachFrom.Left, "+oo")] + [InlineData("tan(x) - a", "pi/2", ApproachFrom.Right, "-oo")] + [InlineData("a - tan(x)", "pi/2", ApproachFrom.Left, "-oo")] + [InlineData("tan(x) + ln(a)", "pi/2", ApproachFrom.Left, "+oo")] + [InlineData("cotan(x) - a", "0", ApproachFrom.Right, "+oo")] + [InlineData("sec(x) - a", "pi/2", ApproachFrom.Left, "+oo")] + [InlineData("sqrt(cos(x)) / sqrt(pi/2 - x)", "pi/2", ApproachFrom.Right, "1")] + [InlineData("sqrt(cos(x)) / sqrt(pi/2 - x)", "pi/2", ApproachFrom.Left, "1")] + [InlineData("sqrt(sin(x)) / sqrt(x)", "0", ApproachFrom.Left, "1")] + [InlineData("sqrt(x) / sqrt(x^3)", "0", ApproachFrom.Left, "+oo")] + // A part the descent hands back as 1 / 0 is no constant: read as one beside -oo, this was -oo. + [InlineData("(cotan(x) - 1/x) / (2 * x)", "0", ApproachFrom.Right, "-1/6")] + [InlineData("(ln(sin(x)) - ln(x)) / x^2", "0", ApproachFrom.Right, "-1/6")] + public void IsAnswered(string expression, string destination, ApproachFrom side, string expected) + { + var limit = expression.ToEntity().Limit("x", destination.ToEntity(), side); + Assert.DoesNotContain(limit.Nodes, node => node is Entity.Limitf); + Assert.Equal(expected.ToEntity().Evaled, limit.Evaled); + } + + /// + /// The values agree: tan(x) - a just left of pi/2 is large and positive for each a tried, and + /// sqrt(cos(x)) / sqrt(pi/2 - x) just right of it is near 1. + /// + [Fact] + public void TheValuesAgree() + { + foreach (var a in new[] { "-5/2", "3/5", "19/10" }) + { + var near = "tan(pi/2 - 1/1000000) - a".ToEntity().Substitute("a", a.ToEntity()).Evaled; + Assert.True(((Entity.Number.Real)near).EDecimal.CompareTo(PeterO.Numbers.EDecimal.FromInt32(100000)) > 0); + } + var root = (Entity.Number.Complex)"sqrt(cos(pi/2 + 1/1000000)) / sqrt(-1/1000000)".ToEntity().Evaled; + Assert.True(((Entity.Number.Complex)(root - 1).Evaled).Abs() < 1e-6); + } + } +}