From 25a47f7886af5e5a577f9c7b7a51c7718e86c52b Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sat, 5 Dec 2026 22:44:41 +0000 Subject: [PATCH] Take the side approached at an inverse trigonometric cut in limits A limit whose argument reached the cut of arcsin, arccos, arcsec, arccsc or arccot off the real line was answered with the value on the cut, which is the limit from one side only: arcsin(2 + i/x) at +oo gave arcsin(2) = pi/2 - 1.317i where it tends to pi/2 + 1.317i. The descent now reads the side from the sign of the argument's imaginary part (the real part for arccot), split structurally into parts real on the approach, and answers the value across the jump from the other side, or declines where the side cannot be read; substitution and Gruntz decline the same arguments unless they come from the value's side. Along the negative half of the cut the library's value is the limit from below, but 1 / sqrt(1 - t^2) and the logarithmic form take the other side there, so every reading through derivatives or series disagreed with eval: arccos(-x) / ln(x) at +oo was -i where it is i. Arguments real on the approach and below -1 (between -1 and 0 for arcsec and arccsc) are rewritten through the negated argument, arcsin(t) = arcsin(-t) - pi and arccos(t) = pi + arccos(-t), which are the library's own values and on whose half every reading agrees. Substitution now writes arcsin and arccos of an infinity as the angle they evaluate to. Checked against mpmath on 286 generated variants (0 wrong, was 58) and on the 1041 sweep rows holding these functions. Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 27 +++ .../Limits/Solvers/Limit.Classes.cs | 44 ++-- .../Limits/Solvers/Limit.Solvers.cs | 8 +- .../Limits/Solvers/Solvers.Definition.cs | 8 + .../Continuous/Limits/Transformations.cs | 200 ++++++++++++++++++ .../Calculus/AnInverseTrigonometricCutTest.cs | 120 +++++++++++ 6 files changed, 394 insertions(+), 13 deletions(-) create mode 100644 Sources/Tests/UnitTests/Calculus/AnInverseTrigonometricCutTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index fad9d71b9..53dffcf61 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -42,6 +42,33 @@ A not-equal written `<>` is read as `not (a = b)`, and printed that way. ## Unreleased — since 2.10.0 +### A limit at an inverse trigonometric cut takes the side it arrives from + +**Silent: wrong limits become right ones, and one whose side keeps changing becomes unevaluated.** `arcsin` and `arccos` take their value past +1 and -1 from below (`arcsin(2) = pi/2 - 1.317i`, `arccos(-2) = pi + 1.317i`), `arcsec` and `arccsc` past 0 and 1 from +above, and `arccot` on the imaginary axis between `-i` and `i` from the left above 0 and from the right below it. A +limit whose argument arrived there from the other side was answered with that value. It is now answered from the side +the argument comes from, or left unevaluated where that side cannot be read, as `ln(-1 + i/x)` already is. Along the +negative half of the cut, limits read through derivatives took the other side from the function's own value: `arccos(-x)` +is `pi + i arcosh(x)`, and `arccos(-x) / ln(x)` at `+oo` was `-i`. Values checked against mpmath. Measured on master +`409d68805` and on this branch ([#2709](https://github.com/asc-community/AngouriMath/issues/2709)). + +| Limit | Was | Now | +|---|---|---| +| `arcsin(2 + i/x)` at `+oo` | `arcsin(2)`, `pi/2 - 1.317i` | `pi - arcsin(2)`, `pi/2 + 1.317i` | +| `arccos(2 + i/x)` at `+oo` | `arccos(2)`, `1.317i` | `-arccos(2)` | +| `arccos(-2 + i x)` at `0` from the right | `arccos(-2)`, `pi + 1.317i` | `2 pi - arccos(-2)`, `pi - 1.317i` | +| `arcsin(x + i)` at `+oo` | `pi/2 - ooi` | `pi/2 + ooi` | +| `arcsec(1/2 - i/x)` at `+oo` | `arcsec(1/2)`, `1.317i` | `-arcsec(1/2)` | +| `arccotan(i/2 + 1/x)` at `+oo` | `arccotan(i/2)`, `-pi/2 - 0.549i` | `arccotan(i/2) + pi`, `pi/2 - 0.549i` | +| `arccos(-x) / ln(x)` at `+oo` | `-i` | `i` | +| `arcsin(-x) / ln(x)` at `+oo` | `i` | `-i` | +| `(1/x) / arccos(1/x)` at `0` from the left | `-ooi` | `+ooi` | +| `arcsec(-x) / ln(x)` at `0` from the right | `i` | `-i` | +| `(arcsin(-1 - x) + pi/2) / sqrt(x)` at `0` from the right | `i sqrt(2)` | `-i sqrt(2)` | +| `(arcsin(-2 + x) - arcsin(-2)) / x` at `0` | `1 / sqrt(-3)`, `-i/sqrt(3)` | `-1 / sqrt(-3)`, `i/sqrt(3)` | +| `arcsin(2 + i sin(x)/x)` at `+oo` | `arcsin(2)` | unevaluated: the side keeps changing | + ### Evaluation keeps its digits from 1e-100000 to 1e100000 **Silent: wrong numbers become right ones.** The decimals evaluation works in had a hundred digits, but exponents only diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs index ed08bf760..c70f6d9bd 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Classes.cs @@ -517,11 +517,15 @@ partial record Arcsecantf // Asked in front of the substitution, which for a diverging argument does answer // -- with arcsec(+oo), a right angle written as a function of an infinity rather // than as the right angle it is. - if (InverseTrigonometryAtInfinity(this, Argument.ComputeLimitDivideEtImpera(x, dist, side)) is { } atInfinity) + var argumentLimit = Argument.ComputeLimitDivideEtImpera(x, dist, side); + if (InverseTrigonometryAtInfinity(this, argumentLimit) is { } atInfinity) return atInfinity; + // Before the argument's limit is put in, which is the limit only from the side the function is + // continuous on its cut from. https://github.com/asc-community/AngouriMath/issues/2709 + if (InverseTrigonometryOnTheCut(this, Argument, argumentLimit, x, dist, side) is (true, var onTheCut)) + return onTheCut; return ComputeLimitImpl(this, x, dist, side) is { } lim ? lim - : ComputeLimitImpl(New( - Argument.ComputeLimitDivideEtImpera(x, dist, side) is { IsFinite: true } lim1 ? lim1 : Argument), + : ComputeLimitImpl(New(argumentLimit is { IsFinite: true } lim1 ? lim1 : Argument), x, dist, side); } } @@ -532,11 +536,15 @@ partial record Arccosecantf { // A diverging argument first, as for the arcsecant: substituting it leaves the // function of an infinity rather than the angle it tends to. - if (InverseTrigonometryAtInfinity(this, Argument.ComputeLimitDivideEtImpera(x, dist, side)) is { } atInfinity) + var argumentLimit = Argument.ComputeLimitDivideEtImpera(x, dist, side); + if (InverseTrigonometryAtInfinity(this, argumentLimit) is { } atInfinity) return atInfinity; + // Before the argument's limit is put in, which is the limit only from the side the function is + // continuous on its cut from. https://github.com/asc-community/AngouriMath/issues/2709 + if (InverseTrigonometryOnTheCut(this, Argument, argumentLimit, x, dist, side) is (true, var onTheCut)) + return onTheCut; return ComputeLimitImpl(this, x, dist, side) is { } lim ? lim - : ComputeLimitImpl(New( - Argument.ComputeLimitDivideEtImpera(x, dist, side) is { IsFinite: true } lim1 ? lim1 : Argument), + : ComputeLimitImpl(New(argumentLimit is { IsFinite: true } lim1 ? lim1 : Argument), x, dist, side); } } @@ -725,11 +733,15 @@ partial record Arcsinf { internal override Entity? ComputeLimitDivideEtImpera(Variable x, Entity dist, ApproachFrom side) { - if (InverseTrigonometryAtInfinity(this, Argument.ComputeLimitDivideEtImpera(x, dist, side)) is { } atInfinity) + var argumentLimit = Argument.ComputeLimitDivideEtImpera(x, dist, side); + // Before the argument's limit is put in, which is the limit only from the side the function is + // continuous on its cut from. https://github.com/asc-community/AngouriMath/issues/2709 + if (InverseTrigonometryOnTheCut(this, Argument, argumentLimit, x, dist, side) is (true, var onTheCut)) + return onTheCut; + if (InverseTrigonometryAtInfinity(this, argumentLimit) is { } atInfinity) return atInfinity; return ComputeLimitImpl(this, x, dist, side) is { } lim ? lim - : ComputeLimitImpl(New( - Argument.ComputeLimitDivideEtImpera(x, dist, side) is { IsFinite: true } lim1 ? lim1 : Argument), + : ComputeLimitImpl(New(argumentLimit is { IsFinite: true } lim1 ? lim1 : Argument), x, dist, side); } } @@ -738,11 +750,15 @@ partial record Arccosf { internal override Entity? ComputeLimitDivideEtImpera(Variable x, Entity dist, ApproachFrom side) { - if (InverseTrigonometryAtInfinity(this, Argument.ComputeLimitDivideEtImpera(x, dist, side)) is { } atInfinity) + var argumentLimit = Argument.ComputeLimitDivideEtImpera(x, dist, side); + // Before the argument's limit is put in, which is the limit only from the side the function is + // continuous on its cut from. https://github.com/asc-community/AngouriMath/issues/2709 + if (InverseTrigonometryOnTheCut(this, Argument, argumentLimit, x, dist, side) is (true, var onTheCut)) + return onTheCut; + if (InverseTrigonometryAtInfinity(this, argumentLimit) is { } atInfinity) return atInfinity; return ComputeLimitImpl(this, x, dist, side) is { } lim ? lim - : ComputeLimitImpl(New( - Argument.ComputeLimitDivideEtImpera(x, dist, side) is { IsFinite: true } lim1 ? lim1 : Argument), + : ComputeLimitImpl(New(argumentLimit is { IsFinite: true } lim1 ? lim1 : Argument), x, dist, side); } } @@ -914,6 +930,10 @@ partial record Arccotanf var argumentLimit = Argument.ComputeLimitDivideEtImpera(x, dist, side); if (InverseTrigonometryAtInfinity(this, argumentLimit) is { } atInfinity) return atInfinity; + // Before the argument's limit is put in, which is the limit only from the side the function is + // continuous on its cut from. https://github.com/asc-community/AngouriMath/issues/2709 + if (InverseTrigonometryOnTheCut(this, Argument, argumentLimit, x, dist, side) is (true, var onTheCut)) + return onTheCut; // At 0 the arccotangent jumps: it is arctan(1/t), with range (-pi/2, pi/2], so // arccot(0.001) = 1.5698, arccot(-0.001) = -1.5698 and arccot(0) = pi/2. A // one-sided limit there is taken from the side the argument arrives from, as for diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Solvers.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Solvers.cs index 771579792..708e101e4 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Solvers.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Limit.Solvers.cs @@ -89,7 +89,13 @@ internal static class LimitSolvers return limit.RealPart; // TODO: sometimes we get { oo + value * i } so we assume it is just infinity if (limit == Integer.Zero) return limit; - return res; + // An arcsine or arccosine of an infinity is the angle it tends to, which is what it evaluates to, written + // as that angle: arcsin(+oo) - pi is -pi/2 - ooi. + return res.Nodes.Any(static node => node is Arcsinf or Arccosf) + ? res.Replace(static node => node is Arcsinf or Arccosf + && LimitFunctional.InverseTrigonometryAtInfinity(node, node.DirectChildren[0]) is { } angle + ? angle : node) + : res; } return null; } diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs index adebd6b1c..fa2aaccee 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Solvers/Solvers.Definition.cs @@ -805,6 +805,14 @@ node is Derivativef(var expression, Variable variable, var iterations) && iterat : VanishesNear(divisor, x, dest, side))) return MathS.NaN; + // Every reading below of an arcsine on the negative half of its cut takes the other side of it from its value + // there. Not the function alone, which its own reading answers from the value, and which rewritten would be + // read at an infinity as arcsin(+oo) - pi rather than as the angle. + // https://github.com/asc-community/AngouriMath/issues/2709 + if (expr is not (Arcsinf or Arccosf or Arcsecantf or Arccosecantf) + && expr.Nodes.Any(static node => node is Arcsinf or Arccosf or Arcsecantf or Arccosecantf)) + expr = expr.Replace(node => OnThePositiveHalfOfTheCut(node, x, dest, side)); + expr = expr.Replace(ExpandLogarithm); expr = expr.Replace(node => LogarithmOfAPower(node, x, dest, side)); expr = expr.Replace(node => PowerOfAPower(node, x, dest, side)); diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs index 1b10ceae0..5e86843b8 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Transformations.cs @@ -964,6 +964,14 @@ internal static bool TakesItsValueOnACut(Entity node, Variable x, Entity dest) // the approach reaches, and its value there is the limit from one side only: // arctan(log(2, x)) at -1+ has its argument arriving at i pi / ln(2) from the left // half-plane, and tends to -pi/2 + 0.224i, where the value there is pi/2 + 0.224i. + // The other inverse trigonometric functions' cuts, which an argument real on the approach arrives along + // and takes the value on. https://github.com/asc-community/AngouriMath/issues/2709 + Arcsinf or Arccosf or Arcsecantf or Arccosecantf or Arccotanf + => node.DirectChildren[0] is var argument && argument.ContainsNode(x) + && argument.Substitute(x, dest).Evaled is Complex { IsNaN: false } at + && OnTheInverseTrigonometricCut(node, at) && !IsRealOnTheApproach(argument, x, dest) + && !IsRealWithoutComplexConstants(argument, x, dest, ApproachFrom.BothSides) + && TakesTheValuesSide(node, argument, at, x, dest, ApproachFrom.BothSides) is not true, Arctanf(var argument) => argument.ContainsNode(x) && argument.Substitute(x, dest).Evaled is Complex { RealPart.IsZero: true, ImaginaryPart: var imaginary } && imaginary.EDecimal.Abs().CompareTo(PeterO.Numbers.EDecimal.One) > 0, @@ -1073,6 +1081,198 @@ private static bool IsFiniteNode(Entity expr) }; } + /// + /// Whether , an argument's limit, lies on the cut of the inverse trigonometric + /// : past 1 or -1 on the real line, or at its end, an infinity, for the arcsine and the + /// arccosine; between -1 and 1, but for 0, for the arcsecant and the arccosecant, which are those two of the + /// reciprocal; and on the imaginary axis between -i and i, but for 0, for the arccotangent, the arctangent of the + /// reciprocal. + /// + private static bool OnTheInverseTrigonometricCut(Entity function, Complex at) + => function switch + { + Arcsinf or Arccosf => at is Real real && (real > 1 || real < -1) + || !at.RealPart.IsFinite && !at.RealPart.IsNaN && at.ImaginaryPart.IsFinite, + Arcsecantf or Arccosecantf => at is Real { IsFinite: true, IsZero: false } real && real < 1 && real > -1, + Arccotanf => at is not Real && at.IsFinite && at.RealPart.IsZero + && at.ImaginaryPart < 1 && at.ImaginaryPart > -1, + _ => false + }; + + /// + /// The limit of an inverse trigonometric function whose argument arrives on its cut off the real line, read from + /// the side the argument arrives from. OnTheCut is false where the argument does not do that, and the limit + /// is where the side cannot be read. + /// + /// + /// The function is continuous on its cut from one side only, and its value there is the limit from that side. + /// This library takes the arcsine and the arccosine past 1 and -1 from below -- arcsin(2) = pi/2 - 1.317i and + /// arcsin(-2) = -pi/2 - 1.317i, arccos(2) = 1.317i and arccos(-2) = pi + 1.317i -- and so the arcsecant and the + /// arccosecant, whose reciprocal arguments cross the real line the other way, from above: arcsec(1/2) = 1.317i, + /// arcsec(-1/2) = pi + 1.317i, arccsc(1/2) = pi/2 - 1.317i, arccsc(-1/2) = -pi/2 - 1.317i. The arccotangent of + /// i/2 is -pi/2 - 0.549i, the limit from the left, and of -i/2 is pi/2 + 0.549i, the limit from the right. From + /// the other side the value is the one across the jump, which is the complex conjugate for the first four and the + /// value less or plus pi for the arccotangent: arcsin(2 + i/x) at +oo tends to pi/2 + 1.317i, where reading off the + /// value at 2 answered pi/2 - 1.317i. Each value was checked against mpmath at 10^-30 off the cut. + /// + /// The side is the sign of the argument's imaginary part, or for the arccotangent of its real part, near the + /// destination, read where the argument is a sum, product or quotient by a real part of parts real on the approach + /// and constants; anything else is declined, as the logarithm declines an argument arriving on its cut off the real + /// line. An argument real on the approach arrives along the cut and takes the value there, and is not read here. + /// https://github.com/asc-community/AngouriMath/issues/2709 + /// + internal static (bool OnTheCut, Entity? Limit) InverseTrigonometryOnTheCut(Entity function, Entity argument, Entity? argumentLimit, + Variable x, Entity dest, ApproachFrom side) + { + if (function is not (Arcsinf or Arccosf or Arcsecantf or Arccosecantf or Arccotanf) + || !argument.ContainsNode(x) || argumentLimit is null || argumentLimit.ContainsNode(x) + || argumentLimit.Evaled is not Complex { IsNaN: false } at + || !OnTheInverseTrigonometricCut(function, at) + || IsRealOnTheApproach(argument, x, dest) || IsRealWithoutComplexConstants(argument, x, dest, side)) + return (false, null); + if (TakesTheValuesSide(function, argument, at, x, dest, side) is not { } fromItsSide) + return (true, null); + // At the cut's end, an infinity, the imaginary part grows without bound with the sign of the side. + if (!at.IsFinite) + { + var imaginaryInfinity = MathS.i * Real.PositiveInfinity; + return (true, function switch + { + Arcsinf when fromItsSide => (at.RealPart.IsNegative ? -MathS.pi / 2 : MathS.pi / 2) - imaginaryInfinity, + Arcsinf => (at.RealPart.IsNegative ? -MathS.pi / 2 : MathS.pi / 2) + imaginaryInfinity, + Arccosf when fromItsSide => (at.RealPart.IsNegative ? MathS.pi : 0) + imaginaryInfinity, + Arccosf => (at.RealPart.IsNegative ? MathS.pi : 0) - imaginaryInfinity, + _ => null + }); + } + Entity value = function switch + { + Arcsinf => MathS.Arcsin(argumentLimit), + Arccosf => MathS.Arccos(argumentLimit), + Arcsecantf => MathS.Arcsec(argumentLimit), + Arccosecantf => MathS.Arccosec(argumentLimit), + _ => MathS.Arccotan(argumentLimit) + }; + if (fromItsSide) + return (true, value); + var positive = at is Real real ? real.IsPositive : !at.ImaginaryPart.IsNegative; + return (true, function switch + { + Arcsinf or Arccosecantf => (positive ? MathS.pi : -MathS.pi) - value, + Arccosf or Arcsecantf => positive ? -value : 2 * MathS.pi - value, + _ => positive ? value + MathS.pi : value - MathS.pi + }); + } + + // Real near the destination by the readings of IsRealOnEachSide, which count a complex constant as real since it is + // the same on each side; an argument holding one is not real for that. + private static bool IsRealWithoutComplexConstants(Entity argument, Variable x, Entity dest, ApproachFrom side) + => !argument.Nodes.Any(static node => node is Complex and not Real) && IsRealOnEachSide(argument, x, dest, side); + + /// + /// Whether an argument arriving at on the cut of off the real line + /// comes from the side the function takes its value there from, by the sign of its imaginary part, or for the + /// arccotangent of its real part, near the destination; where that sign cannot be read. + /// + private static bool? TakesTheValuesSide(Entity function, Entity argument, Complex at, Variable x, Entity dest, ApproachFrom side) + { + if (RealAndImaginaryParts(argument, x, dest) is not var (realPart, imaginaryPart)) + return null; + var reading = function is Arccotanf ? realPart : imaginaryPart; + // A reading that is 0 all the way in is an argument along the cut, which takes the value there. + var arrivesFrom = reading.ContainsNode(x) + ? SignOnEachSide(reading, x, dest, side) + : reading.Evaled is Real { IsNaN: false } constant ? (constant.IsZero ? 0 : constant.IsNegative ? -1 : 1) : null; + if (arrivesFrom is not { } sign) + return null; + return sign == 0 || function switch + { + Arcsinf or Arccosf => sign < 0, + Arcsecantf or Arccosecantf => sign > 0, + _ => sign == (at.ImaginaryPart.IsNegative ? 1 : -1) + }; + } + + /// + /// as a real part and an imaginary part, each real as runs along the + /// real line to , read through sums, differences, products, and quotients by a real + /// divisor, of parts real on the approach and of numbers; for anything else. + /// + private static (Entity Real, Entity Imaginary)? RealAndImaginaryParts(Entity expr, Variable x, Entity dest) + { + if (!expr.ContainsNode(x)) + return expr.Evaled is Complex { IsFinite: true, IsNaN: false } number ? (number.RealPart, number.ImaginaryPart) : null; + if (IsRealOnTheApproach(expr, x, dest)) + return (expr, Integer.Zero); + switch (expr) + { + case Sumf(var augend, var addend) + when RealAndImaginaryParts(augend, x, dest) is var (ar, ai) && RealAndImaginaryParts(addend, x, dest) is var (br, bi): + return ((ar + br).InnerSimplified, (ai + bi).InnerSimplified); + case Minusf(var minuend, var subtrahend) + when RealAndImaginaryParts(minuend, x, dest) is var (ar, ai) && RealAndImaginaryParts(subtrahend, x, dest) is var (br, bi): + return ((ar - br).InnerSimplified, (ai - bi).InnerSimplified); + case Mulf(var multiplier, var multiplicand) + when RealAndImaginaryParts(multiplier, x, dest) is var (ar, ai) && RealAndImaginaryParts(multiplicand, x, dest) is var (br, bi): + return ((Times(ar, br) - Times(ai, bi)).InnerSimplified, (Times(ar, bi) + Times(ai, br)).InnerSimplified); + case Divf(var dividend, var divisor) + when IsRealOnTheApproach(divisor, x, dest) && RealAndImaginaryParts(dividend, x, dest) is var (ar, ai): + return (Over(ar, divisor).InnerSimplified, Over(ai, divisor).InnerSimplified); + default: + return null; + } + // A part that is 0 stays 0, where 0 / x is not simplified to it, x possibly being 0. + static Entity Times(Entity a, Entity b) => a is Integer { IsZero: true } || b is Integer { IsZero: true } ? Integer.Zero : a * b; + static Entity Over(Entity a, Entity b) => a is Integer { IsZero: true } ? Integer.Zero : a / b; + } + + /// + /// An arcsine or arccosine of an argument real on the approach and below -1 near the destination, or an + /// arcsecant or arccosecant of one between -1 and 0, written through the function of the negated argument, which + /// lies on the positive half of the cut; anything else as it is. + /// + /// + /// This library reads the arcsine on both halves of its cut from below (see + /// ), and its derivative, 1 / sqrt(1 - t^2), and its logarithmic form, + /// -i ln(i t + sqrt(1 - t^2)), take the principal square root of the negative 1 - t^2, which is the reading from + /// below past 1 and from above past -1. So every reading of a limit through derivatives or series took the other + /// side on the negative half: arcsin(-x) / ln(x) at +oo was answered i, where arcsin(-x) is + /// -pi/2 - i arcosh(x) and the limit is -i, and (1/x) / arccos(1/x) at 0 from the left -ooi, where it is +ooi. + /// For real t below -1 the value is arcsin(-t) - pi, and arccos(t) = pi + arccos(-t), and on the positive half + /// every one of those readings agrees with the value; arcsec(t) = pi + arcsec(-t) and arccsc(t) = arccsc(-t) - pi + /// for t between -1 and 0 likewise. + /// https://github.com/asc-community/AngouriMath/issues/2709 + /// + internal static Entity OnThePositiveHalfOfTheCut(Entity node, Variable x, Entity dest, ApproachFrom side) + { + var (argument, belowMinusOne) = node switch + { + Arcsinf(var a) => (a, true), + Arccosf(var a) => (a, true), + Arcsecantf(var a) => (a, false), + Arccosecantf(var a) => (a, false), + _ => (null, false) + }; + if (argument is null || !argument.ContainsNode(x) || !IsRealOnTheApproach(argument, x, dest) + || EvalAssumingContinuous(ComputeLimit(argument, x, dest, side) ?? MathS.NaN) is not Real { IsNaN: false } limit) + return node; + var onTheNegativeHalf = belowMinusOne + ? limit < -1 || limit == -1 && SignOnEachSide(argument + 1, x, dest, side) is -1 + : limit > -1 && limit < 0 + || limit == -1 && SignOnEachSide(argument + 1, x, dest, side) is 1 + || limit.IsZero && SignOnEachSide(argument, x, dest, side) is -1; + if (!onTheNegativeHalf) + return node; + var negated = (-argument).InnerSimplified; + return node switch + { + Arcsinf => MathS.Arcsin(negated) - MathS.pi, + Arccosf => MathS.pi + MathS.Arccos(negated), + Arcsecantf => MathS.pi + MathS.Arcsec(negated), + _ => MathS.Arccosec(negated) - MathS.pi + }; + } + /// /// Whether putting the parts' own limits in place of the parts settles the whole -- that /// is, whether the combination of the two is a number rather than an indeterminate form. diff --git a/Sources/Tests/UnitTests/Calculus/AnInverseTrigonometricCutTest.cs b/Sources/Tests/UnitTests/Calculus/AnInverseTrigonometricCutTest.cs new file mode 100644 index 000000000..08c01aa92 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/AnInverseTrigonometricCutTest.cs @@ -0,0 +1,120 @@ +// +// 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 AngouriMath.Core; +using Xunit; +using static AngouriMath.Entity.Number; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// A limit reaching the cut of an inverse trigonometric function takes the side the argument arrives from, and + /// along the real line the side the function's own value takes there. This library reads arcsin and arccos past 1 + /// and -1 from below: arcsin(2) = pi/2 - 1.317i, arcsin(-2) = -pi/2 - 1.317i, arccos(-2) = pi + 1.317i. Values by + /// mpmath, 10^-30 off the cut. + /// https://github.com/asc-community/AngouriMath/issues/2709 + /// + [Trait("Area", "Calculus")] + public sealed class AnInverseTrigonometricCutTest + { + private const double ArcoshOfTwo = 1.3169578969248167; + private const double ArtanhOfOneHalf = 0.5493061443340548; + private const double HalfPi = 1.5707963267948966; + + private static void AssertNear(double re, double im, Entity limit) + { + Assert.IsNotType(limit); + var value = Assert.IsAssignableFrom(limit.Evaled); + Assert.Equal(re, value.RealPart.AsDouble(), 10); + Assert.Equal(im, value.ImaginaryPart.AsDouble(), 10); + } + + // Approached from the side the value is not taken from: arcsin(2 + i/x) at x = 1e30 is pi/2 + 1.317i. + [Theory] + [InlineData("arcsin(2 + i/x)", HalfPi, ArcoshOfTwo)] + [InlineData("arccos(2 + i/x)", 0, -ArcoshOfTwo)] + [InlineData("arcsin(-2 + i/x)", -HalfPi, ArcoshOfTwo)] + [InlineData("arccos(-2 + i/x)", 2 * HalfPi, -ArcoshOfTwo)] + [InlineData("arcsec(1/2 - i/x)", 0, -ArcoshOfTwo)] + [InlineData("arcsec(-1/2 - i/x)", 2 * HalfPi, -ArcoshOfTwo)] + [InlineData("arccosec(1/2 - i/x)", HalfPi, ArcoshOfTwo)] + [InlineData("arccosec(-1/2 - i/x)", -HalfPi, ArcoshOfTwo)] + [InlineData("arccotan(i/2 + 1/x)", HalfPi, -ArtanhOfOneHalf)] + [InlineData("arccotan(-i/2 - 1/x)", -HalfPi, ArtanhOfOneHalf)] + public void Issue2709_TakesTheSideApproachedFrom(string input, double re, double im) + => AssertNear(re, im, MathS.FromString(input).Limit("x", Real.PositiveInfinity)); + + // From the side the value is taken from, and along the cut itself, the value there is the limit. + [Theory] + [InlineData("arcsin(2 - i/x)", HalfPi, -ArcoshOfTwo)] + [InlineData("arccos(-2 - i/x)", 2 * HalfPi, ArcoshOfTwo)] + [InlineData("arcsec(1/2 + i/x)", 0, ArcoshOfTwo)] + [InlineData("arccotan(i/2 - 1/x)", -HalfPi, -ArtanhOfOneHalf)] + [InlineData("arccotan(i/2 + i/x)", -HalfPi, -ArtanhOfOneHalf)] + public void Issue2709_TakesTheValueFromItsSide(string input, double re, double im) + => AssertNear(re, im, MathS.FromString(input).Limit("x", Real.PositiveInfinity)); + + // arccos(-2 + i x) at 0 from the right comes from above, to pi - 1.317i, where arccos(-2) = pi + 1.317i. + [Fact] + public void Issue2709_TakesTheSideApproachedFromAtAFinitePoint() + { + AssertNear(2 * HalfPi, -ArcoshOfTwo, MathS.FromString("arccos(-2 + i x)").Limit("x", 0, ApproachFrom.Right)); + AssertNear(HalfPi, ArcoshOfTwo, MathS.FromString("arcsin(2 + i x)").Limit("x", 0, ApproachFrom.Right)); + AssertNear(HalfPi, -ArcoshOfTwo, MathS.FromString("arcsin(2 + i x)").Limit("x", 0, ApproachFrom.Left)); + } + + // Growing without bound above the cut, the imaginary part grows with the other sign: arcsin(1e30 + i) is + // pi/2 + 69.77i, where arcsin(1e30) = pi/2 - 69.77i. + [Theory] + [InlineData("arcsin(x + i)", HalfPi, double.PositiveInfinity)] + [InlineData("arcsin(-x + i/x)", -HalfPi, double.PositiveInfinity)] + [InlineData("arccos(x + i/x)", 0, double.NegativeInfinity)] + [InlineData("arccos(-x + i)", 2 * HalfPi, double.NegativeInfinity)] + [InlineData("arcsin(x - i)", HalfPi, double.NegativeInfinity)] + public void Issue2709_GrowsOnTheSideApproachedFrom(string input, double re, double im) + { + var value = Assert.IsAssignableFrom(MathS.FromString(input).Limit("x", Real.PositiveInfinity).Evaled); + Assert.Equal(re, value.RealPart.AsDouble(), 10); + Assert.Equal(im, value.ImaginaryPart.AsDouble()); + } + + // A side that keeps changing is not read. + [Fact] + public void Issue2709_IsLeftUnevaluatedWithoutASide() + => Assert.IsType(MathS.FromString("arcsin(2 + i sin(x) / x)").Limit("x", Real.PositiveInfinity)); + + // Two sides arriving from either side of the cut tend to different values, so there is no two-sided limit. + [Fact] + public void Issue2709_HasNoTwoSidedLimitAcrossTheCut() + => Assert.Equal(MathS.NaN, MathS.FromString("arccos(2 + i x)").Limit("x", 0, ApproachFrom.BothSides)); + + // Along the negative half of the cut every reading through derivatives took the other side: arccos(-x) is + // pi + i arcosh(x), and arccos(-x) / ln(x) at +oo was -i where eval(arccos(-1e30) / ln(1e30)) = 0.045 + 1.010i. + [Theory] + [InlineData("arccos(-x) / ln(x)", "+oo", ApproachFrom.BothSides, 0, 1)] + [InlineData("arcsin(-x) / ln(x)", "+oo", ApproachFrom.BothSides, 0, -1)] + [InlineData("arcsec(-x) / ln(x)", "0", ApproachFrom.Right, 0, -1)] + [InlineData("arccosec(-x) / ln(x)", "0", ApproachFrom.Right, 0, 1)] + [InlineData("(arcsin(-1 - x) + pi / 2) / sqrt(x)", "0", ApproachFrom.Right, 0, -1.4142135623730950)] + [InlineData("(arcsin(-2 + x) - arcsin(-2)) / x", "0", ApproachFrom.Left, 0, 0.5773502691896258)] + [InlineData("(arccos(-2 + x) - arccos(-2)) / x", "0", ApproachFrom.Right, 0, -0.5773502691896258)] + [InlineData("arcsin(x) / ln(x)", "+oo", ApproachFrom.BothSides, 0, -1)] + [InlineData("arccos(x) / ln(x)", "+oo", ApproachFrom.BothSides, 0, 1)] + public void Issue2709_TakesTheValuesSideAlongTheRealLine(string input, string destination, ApproachFrom side, double re, double im) + => AssertNear(re, im, MathS.FromString(input).Limit("x", destination, side)); + + // (1/x) / arccos(1/x) at 0 from the left was -ooi: arccos(1/x) is pi + i arcosh(-1/x) there. + [Fact] + public void Issue2709_GrowsOnTheValuesSideAlongTheRealLine() + { + Assert.Equal(Complex.Create(Integer.Zero, Real.PositiveInfinity), + MathS.FromString("(1/x) / arccos(1/x)").Limit("x", 0, ApproachFrom.Left)); + Assert.Equal(Complex.Create(Integer.Zero, Real.NegativeInfinity), + MathS.FromString("(1/x) / arcsin(1/x)").Limit("x", 0, ApproachFrom.Left)); + } + } +}