Skip to content

Commit 6a7b656

Browse files
Integrate a power beside a sine or a cosine as incomplete gamma functions (#2085)
A power of x or of a linear that is not a polynomial, beside sin(u)^k cos(u)^j, is integrated through Euler's formula: sin(u)^k cos(u)^j is (2 i)^(-k) 2^(-j) sum C(k, r) (-1)^(k - r) C(j, t) e^(i (2 r - k + 2 t - j) u), each e^(i q u) beside the power is a gamma function of the gamma rule for a power beside an exponential, and the term with q = 0 is the power's own integral. Where any term is declined -- a whole power, whose integral is elementary and answered by parts, or a half-whole order, which is Fresnel's -- so is the integrand. The gamma functions' arguments are imaginary on the real line, off the cut. Rubi's suite on the harness that reads GAMMA, against master 23c8eca: 48,700 -> 48,850 of 49,052, 0 wrong, no solved row lost, no row to a timeout, no answer's allocation grown. The gains are 57 in 4.1.12, 26 in 4.1.10, 25 in 4.2.10, 18 in 4.2.12, 12 in 5.1.4, 9 in 4.7.3, 2 in 8.4 and one Moses problem. Four definite integrals agree with quadrature to 25 digits, real. Part of #2075. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 23c8eca commit 6a7b656

3 files changed

Lines changed: 137 additions & 1 deletion

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ as `a - b = 0`, as a statement's own solve reads it: `"x + 1 = 3".Solve("x")` wa
148148
- **Integrals in it.** `x^m F^(a + b x^n)` and `(c + d x)^m F^(a + b x)` are `-F^a x^(m + 1) z^(-s) gamma(s, z)/n`, with
149149
`z = -b ln(F) x^n` and `s = (m + 1)/n`, where `s` is neither whole nor half a whole number, or is past 30, as
150150
`x^31 e^(-x)` is, whose sum of 32 terms is `-gamma(32, x)` now. A definite integral of such an answer that is
151-
undefined at a bound is left as written.
151+
undefined at a bound is left as written. And beside a power of a sine and a cosine, which Euler's formula writes as
152+
those exponentials, the gamma functions of an imaginary argument.
152153
([#2075](https://github.com/asc-community/AngouriMath/issues/2075))
153154

154155
| Input | Was (2.6.0) | Now |
@@ -164,6 +165,8 @@ as `a - b = 0`, as a statement's own solve reads it: `"x + 1 = 3".Solve("x")` wa
164165
| `"x^(1/3)*gamma(2, a + b*x)".ToEntity().Integrate("x")` | a `FunctionArgumentCountException` | `-b * e ^ (-a) * x ^ (7/3) * (b * x) ^ (-7/3) * gamma(7/3, b * x) + -(1 + a) * e ^ (-a) * x ^ (4/3) * (b * x) ^ (-4/3) * gamma(4/3, b * x) + C` |
165166
| `"(c + d*x)^m*e^(a + b*x)".ToEntity().Integrate("x")` | `integral((c + d * x) ^ m * e ^ (a + b * x), x)` | `e ^ (a - b * c / d) * (c + d * x) ^ m * (-b * (c + d * x) / d) ^ (-m) * gamma(m + 1, -b * (c + d * x) / d) / b + C` |
166167
| `"x^31*e^(-x)".ToEntity().Integrate("x")` | a sum of 32 terms, 6,172 characters | `-gamma(32, x) + C` |
168+
| `"x^m*sin(b*x)".ToEntity().Integrate("x")` | `integral(x ^ m * sin(b * x), x)` | `gamma(m + 1, -i b x)` and `gamma(m + 1, i b x)`, each beside `x ^ (m + 1) (∓i b x) ^ (-(m + 1))` |
169+
| `"sin(c + d*x^3)".ToEntity().Integrate("x")` | `integral(sin(c + d * x ^ 3), x)` | `gamma(1/3, -i d x ^ 3)` and `gamma(1/3, i d x ^ 3)`, beside `e ^ (±i c)` |
167170
| `"integral(e^(x^3), x, 0, 1)".ToEntity().Simplify()` | `integral(e ^ x ^ 3, x, 0, 1)` | `(gamma(1/3, -1) - (-2/3)!) / 3`, which is `1.3419044179774…` |
168171

169172
### Numerical equality is to half the evaluation's digits

‎Sources/AngouriMath/Functions/Continuous/Integration/IntegralPatterns.cs‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,120 @@ private static bool TakeMomentFactor(Entity factor, bool inverted, Entity.Variab
497497
* MathS.Pow(under, -exponentOfPower) * MathS.IncompleteGamma((exponentOfPower + 1).InnerSimplified, under) / (logarithm * rate);
498498
}
499499

500+
/// <summary>
501+
/// <c>int P sin(u)^k cos(u)^j dx</c> for a power <c>P</c> of <c>x</c>, or of a linear, which is not a polynomial -- or
502+
/// none, beside a function of a power of <c>x</c>, <c>sin(a + b x^3)</c>: by
503+
/// Euler's formula <c>sin(u)^k cos(u)^j</c> is a sum of <c>e^(i q u)</c>, each beside <c>P</c> a gamma function of
504+
/// <see cref="APowerBesideAnExponentialOfAPower"/>, and the term with <c>q = 0</c> is <c>P</c>'s own integral. Where any
505+
/// term is declined -- a whole power of <c>x</c>, whose integral is elementary and answered by parts -- so is the
506+
/// integrand. The gamma functions' arguments are imaginary on the real line, off the cut. Rubi's 4.1.10 and 4.1.12.
507+
/// https://github.com/asc-community/AngouriMath/issues/2075
508+
/// </summary>
509+
private static Entity? APowerBesideATrigonometricFunction(Entity expr, Entity.Variable x)
510+
{
511+
// Asked of every integrand that reaches the table, so a type test comes first, and nothing here captures a
512+
// local before it has found a sine or a cosine.
513+
if (expr is not (Entity.Mulf or Entity.Divf or Entity.Sinf or Entity.Cosf or Entity.Powf(Entity.Sinf or Entity.Cosf, _)))
514+
return null;
515+
var (numerator, denominator) = expr is Entity.Divf(var n, var d) ? (n, d) : (expr, (Entity)Entity.Number.Integer.One);
516+
Entity coefficient = Entity.Number.Integer.One;
517+
Entity? argument = null;
518+
var sines = 0;
519+
var cosines = 0;
520+
Entity? above = null;
521+
Entity? below = null;
522+
foreach (var factor in Entity.Mulf.LinearChildren(numerator))
523+
{
524+
if (!factor.ContainsNode(x))
525+
coefficient *= factor;
526+
else if (TheTrigonometricPower(factor) is var (isSine, trigonometricArgument, times)
527+
&& (argument is null || argument == trigonometricArgument))
528+
{
529+
argument = trigonometricArgument;
530+
if (isSine) sines += times; else cosines += times;
531+
}
532+
else
533+
above = above is null ? factor : above * factor;
534+
}
535+
if (argument is null || sines + cosines > 8)
536+
return null;
537+
foreach (var factor in Entity.Mulf.LinearChildren(denominator))
538+
if (!factor.ContainsNode(x))
539+
coefficient /= factor;
540+
else
541+
below = below is null ? factor : below * factor;
542+
543+
// sin(u)^k cos(u)^j = (2 i)^(-k) 2^(-j) sum C(k, r) (-1)^(k - r) C(j, t) e^(i (2 r - k + 2 t - j) u).
544+
var weights = new SortedDictionary<int, EInteger>();
545+
for (var r = 0; r <= sines; r++)
546+
for (var t = 0; t <= cosines; t++)
547+
{
548+
var q = 2 * r - sines + 2 * t - cosines;
549+
var weight = Binomial(sines, r).EInteger.Multiply(Binomial(cosines, t).EInteger);
550+
if ((sines - r) % 2 != 0)
551+
weight = weight.Negate();
552+
weights[q] = weights.TryGetValue(q, out var already) ? already.Add(weight) : weight;
553+
}
554+
Entity sum = Entity.Number.Integer.Zero;
555+
// The highest frequency first: where the power is whole, the gamma rule declines it at once.
556+
foreach (var weight in weights.Reverse())
557+
{
558+
if (weight.Value.IsZero)
559+
continue;
560+
Entity? term;
561+
if (weight.Key == 0)
562+
term = TheIntegralOfAPower(above, below, x);
563+
else
564+
{
565+
var exponential = MathS.Pow(MathS.e, (MathS.i * weight.Key * argument).Expand());
566+
var integrand = above is null ? (below is null ? exponential : exponential / below) : below is null ? above * exponential : above * exponential / below;
567+
term = APowerBesideAnExponentialOfAPower(integrand, x);
568+
}
569+
if (term is null)
570+
return null;
571+
sum += Entity.Number.Integer.Create(weight.Value) * term;
572+
}
573+
return coefficient / (MathS.Pow(2 * MathS.i, sines) * MathS.Pow(2, cosines)) * sum;
574+
}
575+
576+
/// <summary><paramref name="factor"/> as <c>sin(u)^k</c> or <c>cos(u)^k</c> for a whole <c>k >= 1</c>.</summary>
577+
private static (bool IsSine, Entity Argument, int Times)? TheTrigonometricPower(Entity factor) => factor switch
578+
{
579+
Entity.Sinf(var u) => (true, u, 1),
580+
Entity.Cosf(var u) => (false, u, 1),
581+
Entity.Powf(Entity.Sinf(var u), Entity.Number.Integer { EInteger: var k }) when k.Sign > 0 && k.CompareTo(EInteger.FromInt32(8)) <= 0
582+
=> (true, u, k.ToInt32Checked()),
583+
Entity.Powf(Entity.Cosf(var u), Entity.Number.Integer { EInteger: var k }) when k.Sign > 0 && k.CompareTo(EInteger.FromInt32(8)) <= 0
584+
=> (false, u, k.ToInt32Checked()),
585+
_ => null
586+
};
587+
588+
/// <summary>
589+
/// The integral of a power <c>(d x)^m</c> above or below the bar, or of a power <c>(c + d x)^m</c>, for any <c>m</c> but -1,
590+
/// or <see langword="null"/>.
591+
/// </summary>
592+
private static Entity? TheIntegralOfAPower(Entity? above, Entity? below, Entity.Variable x)
593+
{
594+
if (above is null && below is null)
595+
return x;
596+
if (above is not null && below is not null)
597+
return null;
598+
var power = above ?? below!;
599+
Entity @base, m;
600+
if (power == x)
601+
(@base, m) = (x, Entity.Number.Integer.One);
602+
else if (power is Entity.Powf(var written, var exponent) && !exponent.ContainsNode(x))
603+
(@base, m) = (written, exponent);
604+
else
605+
return null;
606+
if (!TreeAnalyzer.TryGetPolyLinear(@base, x, out var slope, out _))
607+
return null;
608+
var mPlusOne = (above is null ? -m + 1 : m + 1).InnerSimplified;
609+
if (TreeAnalyzer.IsZero(mPlusOne))
610+
return null;
611+
return MathS.Pow(@base, mPlusOne) / (slope * mPlusOne);
612+
}
613+
500614
/// <summary>
501615
/// The gamma function's order <paramref name="s"/>, as the number it is where its symbols cancel: <c>(-1 + n + 1)/n</c>
502616
/// is 1, which inner simplification leaves as written, and <c>(p x)^(n - 1) e^(x^n)</c>, whose integral is
@@ -802,6 +916,10 @@ _ when IncompleteGammaBesideAPower(expr, x) is { } gamma => gamma,
802916
// neither elementary nor an error function. https://github.com/asc-community/AngouriMath/issues/2075
803917
_ when APowerBesideAnExponentialOfAPower(expr, x) is { } gammaForm => gammaForm,
804918

919+
// And beside a power of a sine and a cosine, as the exponentials Euler's formula writes them.
920+
// https://github.com/asc-community/AngouriMath/issues/2075
921+
_ when APowerBesideATrigonometricFunction(expr, x) is { } trigonometricGammaForm => trigonometricGammaForm,
922+
805923
Entity.Absf(var arg) when
806924
TreeAnalyzer.TryGetPolyLinear(arg, x, out var a, out _) => // ∫ |ax + b| dx = sgn(ax + b) * (ax + b)^2 / (2a)
807925
MathS.Signum(arg) * MathS.Pow(arg, 2) / (2 * a),

‎Sources/Tests/UnitTests/Calculus/APowerBesideAnExponentialOfAPowerIntegralTest.cs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ public void ADefiniteIntegralFromZero(string integral, double value)
9797
public void APositiveRateIsTakenOutOfThePower()
9898
=> Assert.Equal("-gamma(4/3, x)", DifferentiatesBack("x^(1/3)*e^(-x)").Substitute("C", 0).InnerSimplified.Stringize());
9999

100+
/// <summary>
101+
/// Beside a power of a sine and a cosine, by Euler's formula: each <c>e^(i q u)</c> beside the power is a gamma function
102+
/// of an imaginary argument. Rubi's 4.1.10 and 4.1.12.
103+
/// </summary>
104+
[Theory]
105+
[InlineData("(c + d*x)^m*sin(a + b*x)")]
106+
[InlineData("(c + d*x)^m*cos(a + b*x)^2")]
107+
[InlineData("x^m*sin(b*x)")]
108+
[InlineData("x^(1/3)*sin(x)*cos(x)")]
109+
[InlineData("x*(a + b*sin(c + d*x^3))")]
110+
[InlineData("sin(c + d*x^3)^2")]
111+
public void APowerBesideASineOrACosine(string integrand) => DifferentiatesBack(integrand);
112+
100113
/// <summary>
101114
/// Not where the integral is elementary, an exponential integral or an error function: those keep the form
102115
/// they had.
@@ -106,6 +119,8 @@ public void APositiveRateIsTakenOutOfThePower()
106119
[InlineData("sqrt(x)*e^(-x)")]
107120
[InlineData("e^(b*x^2)")]
108121
[InlineData("e^(b*x)/x")]
122+
[InlineData("x^2*sin(x)")]
123+
[InlineData("sin(x)/x")]
109124
public void NotWhereTheIntegralIsElementaryOrAnErrorFunction(string integrand)
110125
=> Assert.DoesNotContain("gamma(", DifferentiatesBack(integrand).Stringize());
111126
}

0 commit comments

Comments
 (0)