From 57767ba7b1ab86d1d912505380669cc47917a4ec Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sat, 3 Oct 2026 07:38:08 +0000 Subject: [PATCH] A tangent above an imaginary tangent below is written in sines and cosines, and those as exponentials With A + i A tan(z) below the bar written as A e^(i z)/cos(z), a tangent of z above it stayed one, so tan(z) cos(z) was handed on as written, and with z = c + d x the phase was expanded into sin(c) and cos(c) beside d x: tan(c + d x)/(a + i a tan(c + d x)) ran past the budget. The tangent or cotangent above is written in sines and cosines, what the identity puts below cancels against it, and the sines and cosines of z left are written as exponentials, so the whole is a rational function of e^(i z). Only where nothing of x stands beside them but exponentials of linears and polynomials in x: sec(z)^8/(a + i a tan(z))^4 and sqrt(tan(z))/(a + i a tan(z))^(5/2) are read through the tangent as before. Part of https://github.com/asc-community/AngouriMath/issues/718 Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 21 +++++++++ .../Integration/IndefiniteIntegralSolver.cs | 44 ++++++++++++++++++- .../Calculus/PolynomialExponentialTrigTest.cs | 36 +++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 70dbb7b71..e6c432d2d 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -2325,6 +2325,27 @@ rule answers, where the imaginary unit in the coefficient is read by nothing els | `"x/(2+2*i*tan(x))".Integrate("x")` | left unevaluated | the antiderivative | | `"(c+d*x)/(a+i*a*tan(pe+f*x))".Integrate("x")` | left unevaluated | the antiderivative | +### A tangent above an imaginary tangent below is written in sines and cosines, and those as exponentials + +**Answers where there were none.** `tan(c + d x)/(a + i a tan(c + d x))` ran past the corpus's budget +and `1/(a + i a tan(c + d x))` was declined. With `A + i A tan(z)` below the bar written as +`A e^(i z)/cos(z)`, a tangent of `z` above it stayed one, so `tan(z) cos(z)` was handed on as +written, and with `z = c + d x` the phase was expanded into `sin(c)` and `cos(c)` beside `d x`. The +tangent or cotangent above is written in sines and cosines now, what the identity puts below +cancels against it, and the sines and cosines of `z` that are left are written as the exponentials +they are, so that the whole is a rational function of `e^(i z)`. Only where nothing of `x` stands +beside them but exponentials of linears and polynomials in `x`: `sec(z)^8/(a + i a tan(z))^4` and +`sqrt(tan(z))/(a + i a tan(z))^(5/2)` are read through the tangent as before. Rubi's 4.3.2.1 and +4.4.2.1 ([#718](https://github.com/asc-community/AngouriMath/issues/718)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"tan(c+d*x)/(a+i*a*tan(c+d*x))".Integrate("x")` | `integral(...)` | `x` and `e^(-2 i (c + d x))`, over `a` | +| `"1/(a+i*a*tan(c+d*x))".Integrate("x")` | `integral(...)` | the same | +| `"tan(c+d*x)^3/(a+i*a*tan(c+d*x))^4".Integrate("x")` | `integral(...)` | `x` and powers of `e^(-2 i (c + d x))` | +| `"cot(c+d*x)/(a+i*a*cot(c+d*x))".Integrate("x")` | `integral(...)` | `x` and `e^(2 i (c + d x))` | +| `"tan(c+d*x)^2/(a+i*a*tan(c+d*x))".Integrate("x")` | `integral(...)` | a power of `e^(-i (c + d x))` and logarithms of `e^(+/-2 i (c + d x)) + 1` | + ### A symbolic constant factor no longer stops the rounds of parts `F^(c (a + b x)) sin(d + pe x)^3` was left as written. The exponent's constant part becomes a diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index 63cf404d1..aa457efe8 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -21949,6 +21949,7 @@ static bool IsAWholeNumberOfSteps(ERational difference) var (above, below) = Functions.SingleQuotient.Of(Functions.SingleQuotient.Combine(expr)); if (!below.ContainsNode(x)) return null; + var arguments = new List(); var rewritten = below.Replace(node => { if (node is not Sumf and not Minusf || !node.ContainsNode(x)) @@ -21998,12 +21999,53 @@ static bool IsAWholeNumberOfSteps(ERational difference) if (!plus && ratio.Evaled != (-MathS.i).Evaled) return node; var exponential = MathS.Pow(MathS.e, ((plus == isTangent ? MathS.i : -MathS.i) * argument).InnerSimplified); + arguments.Add(argument); // A + i A tan(z) is A e^(iz)/cos(z); A + i A cot(z) is i A e^(-iz)/sin(z). return isTangent ? constant * exponential / MathS.Cos(argument) : (plus ? MathS.i : -MathS.i) * constant * exponential / MathS.Sin(argument); }); - return rewritten == below ? null : Integration.ComputeAsTheSameQuestion((above / rewritten).InnerSimplified, x, integrateByParts); + if (rewritten == below) + return null; + // A tangent or a cotangent of the same argument above the bar in sines and cosines, so that + // the cosine or sine the identity puts below cancels it: `tan(z)/(A + i A tan(z))` is + // `sin(z) e^(-i z)/A`, an exponential times a sine, where `tan(z) cos(z) e^(-i z)/A` was + // a search past the budget with `z = c + d x`. + var inSinesAndCosines = above.Replace(node => node switch + { + Tanf(var inner) when arguments.Contains(inner) => MathS.Sin(inner) / MathS.Cos(inner), + Cotanf(var inner) when arguments.Contains(inner) => MathS.Cos(inner) / MathS.Sin(inner), + _ => node, + }); + var (numerator, denominator) = Functions.SingleQuotient.Of(Functions.SingleQuotient.Combine(inSinesAndCosines / rewritten)); + var (top, bottom) = CancelledWithFunctionsAsIndeterminates(numerator, denominator, x, expr); + // Only where what is left is made of those sines and cosines, exponentials of linears and + // polynomials in x: a secant or a root beside them, `sec(z)^8/(A + i A tan(z))^4`, is read + // through the tangent as written, and rewritten it was a search of minutes. + bool Plain(Entity node) => node switch + { + _ when !node.ContainsNode(x) => true, + Entity.Variable => true, + Sinf(var sineOf) => arguments.Contains(sineOf), + Cosf(var cosineOf) => arguments.Contains(cosineOf), + Powf(var @base, Number.Integer) => Plain(@base), + Powf(var @base, var exponent) => @base == MathS.e && TreeAnalyzer.TryGetPolyLinear(exponent, x, out _, out _), + Mulf or Divf or Sumf or Minusf => node.DirectChildren.All(Plain), + _ => false, + }; + if (!Plain(top) || !Plain(bottom)) + return Integration.ComputeAsTheSameQuestion((above / rewritten).InnerSimplified, x, integrateByParts); + // And what sines and cosines of it are left, as the exponentials they are, so that the whole is + // a rational function of `e^(i z)`: `tan(z)^2/(A + i A tan(z))` leaves `sin(z)^2/(A cos(z) e^(i z))`. + var inExponentials = (top / bottom).Replace(node => node switch + { + Sinf(var inner) when arguments.Contains(inner) + => (MathS.Pow(MathS.e, MathS.i * inner) - MathS.Pow(MathS.e, -MathS.i * inner)) / (2 * MathS.i), + Cosf(var inner) when arguments.Contains(inner) + => (MathS.Pow(MathS.e, MathS.i * inner) + MathS.Pow(MathS.e, -MathS.i * inner)) / 2, + _ => node, + }); + return Integration.ComputeAsTheSameQuestion(inExponentials.InnerSimplified, x, integrateByParts); } /// diff --git a/Sources/Tests/UnitTests/Calculus/PolynomialExponentialTrigTest.cs b/Sources/Tests/UnitTests/Calculus/PolynomialExponentialTrigTest.cs index ecebb5c89..7c5627b9e 100644 --- a/Sources/Tests/UnitTests/Calculus/PolynomialExponentialTrigTest.cs +++ b/Sources/Tests/UnitTests/Calculus/PolynomialExponentialTrigTest.cs @@ -263,6 +263,42 @@ public void TheResonanceIsSeenSymbolically(string integrand, string? symbol) [InlineData("(1 + 2*x)/(3 + 3*i*tan(1/2 + x))^2")] public void AnImaginaryTangentBelowTheBarIsAnExponential(string integrand) => DifferentiatesBack(integrand); + /// + /// A tangent of the same argument above the bar is written in sines and cosines, so that the + /// cosine the identity puts below cancels it, and what sines and cosines of it are left as + /// exponentials: tan(z)/(A + i A tan(z)) is sin(z) e^(-i z)/A, and + /// tan(z) cos(z) e^(-i z)/A was a search past the budget with z = c + d x. + /// The symbols pinned only after integrating. + /// + [Theory] + [InlineData("tan(c + d*x)/(a + i*a*tan(c + d*x))")] + [InlineData("tan(c + d*x)^2/(a + i*a*tan(c + d*x))")] + [InlineData("tan(c + d*x)^3/(a + i*a*tan(c + d*x))^4")] + [InlineData("cot(c + d*x)/(a + i*a*cot(c + d*x))")] + [InlineData("1/(a + i*a*tan(c + d*x))")] + public void AnImaginaryTangentOfALinearWithATangentAboveTheBar(string integrand) + { + var integral = integrand.ToEntity().Integrate("x"); + Assert.DoesNotContain("integral(", integral.Stringize()); + Assert.DoesNotContain("NaN", integral.Stringize()); + Entity Pin(Entity e) => e.Substitute("a", 0.9).Substitute("c", 0.3).Substitute("d", 1.1); + var derivative = Pin(integral.Substitute("C", 0)).Differentiate("x"); + var original = Pin(integrand.ToEntity()); + var compared = 0; + foreach (var at in Points) + { + var got = derivative.Substitute("x", at).EvalNumerical(); + var want = original.Substitute("x", at).EvalNumerical(); + if (got.IsNaN || want.IsNaN) + continue; + compared++; + var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart); + var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)); + Assert.True(difference / scale < 1e-9, $"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}"); + } + Assert.True(compared >= 4, $"only {compared} points could be compared for {integrand}"); + } + /// /// And its sibling, A cos(y) + i A sin(y), which is A e^(i y): a sum of a /// cosine and a sine with no real rotation, since A^2 + (i A)^2 is zero, and beside