From 1924669ca5995334ce078b3da4718566e48a093e Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 4 Oct 2026 08:11:20 +0000 Subject: [PATCH 1/3] A trigonometric function of an imaginary multiple of a logarithm is integrated in exponentials tan(a + i ln(x)) and sin(a + ln(c x^2) sqrt(-1/4)) were declined: the closed form for a power of the variable times a sine or cosine of a logarithm divides by (m + 1)^2 + B^2, which an imaginary B can make zero, and Rubi's 4.7.5 is built on exactly that. e^(i (a + b ln(u))) is e^(i a) times a real power of u when b is imaginary, so each function is written as a sum or quotient of such powers when b is an imaginary number; a symbolic b is left alone, since whether the zero is there turns on a sign. Part of #718. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 17 ++++ .../Integration/IndefiniteIntegralSolver.cs | 77 +++++++++++++++++++ .../Integration/Integration.Definition.cs | 3 + ...etricOfAnImaginaryLogarithmIntegralTest.cs | 48 ++++++++++++ 4 files changed, 145 insertions(+) create mode 100644 Sources/Tests/UnitTests/Calculus/TrigonometricOfAnImaginaryLogarithmIntegralTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index e9ae2c6a1..c22134770 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -503,6 +503,23 @@ read them as nonzero. They are now decided over one bar, expanded, and at pinned | `"e^(3*acoth(a*x))/(c-a*c*x)^3".Integrate("x")`, and over `(c - a c x)^4` | left unevaluated | an antiderivative in `sqrt((a x + 1)/(a x - 1))` | | `"e^(2*acoth(a*x))*sqrt(c-a*c*x)/x".Integrate("x")`, and over `x^2` | left unevaluated | an antiderivative in `sqrt(c - a c x)`, by cases on the sign of `c` | +### A trigonometric function of an imaginary multiple of a logarithm is integrated in exponentials + +**Answers where there were none.** `tan(a + i ln(x))` and `sin(a + ln(c x^2) sqrt(-1/4))` were +declined: the closed form for a power of the variable times a sine or cosine of a logarithm divides +by `(m + 1)^2 + B^2`, which an imaginary `B` can make zero, and Rubi's 4.7.5 is built on exactly +that. `e^(i (a + b ln(u)))` is `e^(i a)` times a real power of `u` when `b` is imaginary, so each +such function is a sum or quotient of powers of `u`, and is written so now when `b` is an imaginary +number; the zero is a power `1/x`, whose integral is a logarithm. A symbolic `b`, +`sqrt(-1/n^2)`, is left alone, since whether the zero is there turns on the sign of `n` +([#718](https://github.com/asc-community/AngouriMath/issues/718)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"tan(a + i*ln(x))".ToEntity().Integrate("x")` | `integral(...)` | a rational function of `x` and `e^(i a)`, with a logarithm | +| `"sin(a + ln(c*x^2)*sqrt(-1/4))".ToEntity().Integrate("x")` | `integral(...)` | powers of `x` and `c x^2` and a logarithm, through `e^(i a)` | +| `"1/cos(a - 2*i*ln(c*x))^(3/2)".ToEntity().Integrate("x")` | `integral(...)` | an arctangent and a logarithm of a root, through `e^(i a)` | + ### `NaN` again, from an exponent that was read as written rather than as a number **A wrong answer, and a second one of the same kind.** `(a^2 + 2abx^2 + b^2x^4)^3/x^7` came back as diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index 9965d1608..28e368685 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -21134,6 +21134,83 @@ private static (Entity Factor, Entity Inside)? TryTakeACommonLinearFactorOfASum( return back.Nodes.Any(node => node == MathS.NaN) ? null : back; } + /// + /// A trigonometric function of a + b ln(u) with an imaginary b, written in + /// exponentials: e^(i (a + b ln(u))) is e^(i a) times the real power + /// u^(i b), so the sine, cosine and the rest are sums and quotients of powers of + /// u, which the rules for those answer. + /// + /// + /// sin(a + ln(c x^2) sqrt(-1/4)) and tan(a + i ln(x)) were declined: the closed + /// form for a power of the variable times a sine or cosine of a logarithm divides by + /// (m + 1)^2 + B^2, which an imaginary B makes zero, and Rubi's 4.7.5 has 62 + /// problems built on exactly that, with b = sqrt(-(m + 1)^2/n^2). In exponentials the + /// zero is a power of x that is 1/x, and its integral a logarithm. Only for a + /// number: a real b is the closed form's, after which this is asked, and a symbolic + /// one, sqrt(-1/n^2), makes the zero one that only a sign of n decides. + /// https://github.com/asc-community/AngouriMath/issues/718 + /// + internal static Entity? SolveByWritingATrigonometricFunctionOfAnImaginaryLogarithmInExponentials(Entity expr, Entity.Variable x, bool integrateByParts) + { + if (!expr.Nodes.Any(node => node is Logf && node.ContainsNode(x))) + return null; + var any = false; + var rewritten = expr.Replace(node => + { + var (argument, kind) = node switch + { + Sinf(var of) => (of, 0), + Cosf(var of) => (of, 1), + Tanf(var of) => (of, 2), + Cotanf(var of) => (of, 3), + Secantf(var of) => (of, 4), + Cosecantf(var of) => (of, 5), + _ => ((Entity?)null, -1) + }; + if (argument is null || !IsLinearInALogarithmWithAnImaginaryCoefficient(argument, x)) + return node; + any = true; + var plus = MathS.Pow(MathS.e, MathS.i * argument); + var minus = MathS.Pow(MathS.e, -MathS.i * argument); + return kind switch + { + 0 => (plus - minus) / (2 * MathS.i), + 1 => (plus + minus) / 2, + 2 => (plus - minus) / (MathS.i * (plus + minus)), + 3 => MathS.i * (plus + minus) / (plus - minus), + 4 => 2 / (plus + minus), + _ => 2 * MathS.i / (plus - minus), + }; + }); + if (!any) + return null; + return Integration.ComputeAsTheSameQuestion(rewritten, x, integrateByParts); + } + + /// + /// Whether is a + b ln(u), with u the only place the + /// variable is and b an imaginary number. + /// + private static bool IsLinearInALogarithmWithAnImaginaryCoefficient(Entity argument, Entity.Variable x) + { + var logarithms = argument.Nodes + .Where(node => node is Logf(var @base, var of) && @base == MathS.e && of.ContainsNode(x)) + .Distinct().ToList(); + if (logarithms.Count != 1) + return false; + var placeholder = Variable.CreateUnique(argument, "l_log"); + var inPlaceholder = argument.Replace(node => node == logarithms[0] ? placeholder : node); + if (inPlaceholder.ContainsNode(x) + || !TreeAnalyzer.TryGetPolyLinear(inPlaceholder, placeholder, out var coefficient, out _) + || TreeAnalyzer.IsZero(coefficient)) + return false; + // A number, and not a symbol's: with `b = sqrt(-1/n^2)` the power of the variable this + // leaves is `1/x` for every positive n, which nothing reading `n` symbolically can know, + // and the general power's formula comes back with a zero below its bar. + return coefficient.Vars.Count() == 0 && coefficient.Evaled is Number.Complex number && number is not Number.Real + && System.Math.Abs((double)number.RealPart) < 1e-12 * System.Math.Abs((double)number.ImaginaryPart); + } + /// /// A power of the variable times a sine or cosine of a logarithm, in closed form: /// int x^m sin(L) dx is x^(m + 1)((m + 1) sin(L) - B cos(L))/((m + 1)^2 + B^2) diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index 8cae2ee5f..5335699b7 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -690,6 +690,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) => // A power of the variable times a sine or cosine of a logarithm, where two rounds of // parts close on the integrand: `int x^m sin(a + b ln(c x^n))` in closed form. if ((answer = IndefiniteIntegralSolver.SolveAPowerTimesATrigonometricOfALogarithm(expr, x, integrateByParts)) is { }) return answer; + // The same with an imaginary coefficient on the logarithm, where the closed form divides + // by zero: in exponentials, powers of the variable. + if ((answer = IndefiniteIntegralSolver.SolveByWritingATrigonometricFunctionOfAnImaginaryLogarithmInExponentials(expr, x, integrateByParts)) is { }) return answer; // A logarithm of a quotient that cancels with the functions in it as // indeterminates, which is how an inverse hyperbolic function of a hyperbolic // one arrives: atanh(tanh(u)) is 1/2 ln(e^(2u)) once its quotient is cancelled. diff --git a/Sources/Tests/UnitTests/Calculus/TrigonometricOfAnImaginaryLogarithmIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/TrigonometricOfAnImaginaryLogarithmIntegralTest.cs new file mode 100644 index 000000000..087c2252f --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/TrigonometricOfAnImaginaryLogarithmIntegralTest.cs @@ -0,0 +1,48 @@ +// +// 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 AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// A trigonometric function of a + b ln(u) with an imaginary number for b, in + /// exponentials: e^(i (a + b ln(u))) is e^(i a) times a real power of + /// u. The closed form for a power of the variable times a sine or cosine of a logarithm + /// divides by zero on exactly these, Rubi's 4.7.5. The integrands are complex, and compared as + /// complex numbers. + /// #718 + /// + [Trait("Area", "Calculus")] + public sealed class TrigonometricOfAnImaginaryLogarithmIntegralTest + { + [Theory] + [InlineData("sin(a + ln(c*x^2)*sqrt(-1/4))")] + [InlineData("tan(a + i*ln(x))")] + [InlineData("cot(a + i*ln(x))/x^2")] + [InlineData("1/cos(a - 2*i*ln(c*x))^(3/2)")] + [InlineData("x^2*cos(a + i*ln(x))^2")] + public void InExponentials(string integrand) + { + var integral = integrand.ToEntity().Integrate("x"); + Assert.DoesNotContain("integral(", integral.Stringize()); + Entity Pinned(Entity e) => e.Substitute("a", 0.4).Substitute("c", 1.3); + var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x"); + var original = Pinned(integrand.ToEntity()); + foreach (var at in new[] { 0.3, 0.8, 1.5, 2.4 }) + { + var want = original.Substitute("x", at).EvalNumerical(); + var got = derivative.Substitute("x", at).EvalNumerical(); + Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart) + < 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)), + $"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}"); + } + } + } +} From 38f870014d2fc0e92edcc7c7121f88cc56b23923 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 4 Oct 2026 12:30:56 +0000 Subject: [PATCH 2/3] A logarithm beside its own derivative is left to the substitution Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- .../Integration/IndefiniteIntegralSolver.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index 28e368685..7649462ba 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -21184,6 +21184,18 @@ private static (Entity Factor, Entity Inside)? TryTakeACommonLinearFactorOfASum( }); if (!any) return null; + // Beside its own derivative the logarithm is a substitution's: `tan(a + i ln(x))/x` is + // `tan(a + i u)` in `u = ln(x)`, which the integrator answers in a closed form, where the + // exponentials would answer it as a piecewise in `e^(i a)`. + if (expr.Nodes.FirstOrDefault(node => node is Logf(var @base, var of) && @base == MathS.e && of.ContainsNode(x)) is { } logarithm) + { + Entity beside = Number.Integer.One; + foreach (var (factor, underneath) in FactorsOfTheIntegrand(expr)) + if (!factor.ContainsNode(logarithm)) + beside = underneath ? beside / factor : beside * factor; + if (beside.ContainsNode(x) && AreProportionalAtSampledPoints(beside, logarithm.Differentiate(x), x)) + return null; + } return Integration.ComputeAsTheSameQuestion(rewritten, x, integrateByParts); } From 7d23e9363e10b260f04d3972b13a23a9914a0834 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 4 Oct 2026 12:55:51 +0000 Subject: [PATCH 3/3] A tangent of an imaginary logarithm beside its derivative keeps the substitution's answer Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- ...metricOfAnImaginaryLogarithmIntegralTest.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Sources/Tests/UnitTests/Calculus/TrigonometricOfAnImaginaryLogarithmIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/TrigonometricOfAnImaginaryLogarithmIntegralTest.cs index 087c2252f..1e33f74c0 100644 --- a/Sources/Tests/UnitTests/Calculus/TrigonometricOfAnImaginaryLogarithmIntegralTest.cs +++ b/Sources/Tests/UnitTests/Calculus/TrigonometricOfAnImaginaryLogarithmIntegralTest.cs @@ -28,9 +28,25 @@ public sealed class TrigonometricOfAnImaginaryLogarithmIntegralTest [InlineData("cot(a + i*ln(x))/x^2")] [InlineData("1/cos(a - 2*i*ln(c*x))^(3/2)")] [InlineData("x^2*cos(a + i*ln(x))^2")] - public void InExponentials(string integrand) + public void InExponentials(string integrand) => DifferentiatesBack(integrand, integrand.ToEntity().Integrate("x")); + + /// + /// Beside its own derivative the logarithm is left to the substitution: + /// tan(a + i ln(x))/x is tan(a + i u) in u = ln(x), answered in a + /// closed form, where the exponentials would answer it as a piecewise in e^(i a). + /// + [Theory] + [InlineData("tan(a + i*ln(x))/x")] + [InlineData("cot(a + i*ln(x))/x")] + public void BesideItsOwnDerivativeByTheSubstitution(string integrand) { var integral = integrand.ToEntity().Integrate("x"); + Assert.DoesNotContain("piecewise", integral.Stringize()); + DifferentiatesBack(integrand, integral); + } + + private static void DifferentiatesBack(string integrand, Entity integral) + { Assert.DoesNotContain("integral(", integral.Stringize()); Entity Pinned(Entity e) => e.Substitute("a", 0.4).Substitute("c", 1.3); var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x");