Skip to content

Commit ecefbcb

Browse files
Read the gamma rule's integrand when it is written otherwise (#2096)
The rule for x^m F^(a + b x^n) and (c + d x)^m F^(a + b x) read one exponential, one term in x in its exponent, and nothing else in x beside the power: - e^(a + c + b x^n + d x^n) had two terms of one degree, now read as (b + d) x^n; - f^(a + b x^n) g^(c + d x^n) had two exponentials, now read as one, e^(u ln f + v ln g), or F^(u + v) for one base; - (c + d x)^m (k + f x) F^(a + b x) has a polynomial beside the power of the linear, now written in powers of the linear, each term then the power alone. By parts leaves this of (c + d x)^m gamma(2, a + b x). Terms gathered so can cancel, and then it declines: e^x e^(-x)/x is 1/x, and read as a gamma function of order 0 at 0 it was NaN, where by parts leaves it beside Shi(a + b x)^2. And at a whole or half-whole order it declines, for the elementary, exponential-integral and error-function routes to answer. Where none of them does, it is now asked last at any order: (c + d x)^(-1 - 4 n) F^(a + b (c + d x)^n) is a gamma function of order -4, and by parts reaches -3. 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 1f22ce5 commit ecefbcb

3 files changed

Lines changed: 105 additions & 29 deletions

File tree

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

Lines changed: 81 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -347,41 +347,52 @@ private static bool TakeMomentFactor(Entity factor, bool inverted, Entity.Variab
347347
return coefficient * (integrated * gamma - quotient * shifted / slope) / mPlusOne;
348348
}
349349

350+
/// <summary>
351+
/// <see cref="APowerBesideAnExponentialOfAPower"/> at a whole or half-whole order too, for
352+
/// the integrator to ask once nothing else has answered: <c>(c + d x)^(-1 - 4 n) F^(a + b (c + d x)^n)</c> is a gamma
353+
/// function of order -4, where the other routes reach to -3.
354+
/// </summary>
355+
internal static Entity? APowerBesideAnExponentialOfAPowerAtAnyOrder(Entity expr, Entity.Variable x) => APowerBesideAnExponentialOfAPower(expr, x, atAnyOrder: true);
356+
350357
/// <summary>
351358
/// <c>int x^m F^(a + b x^n) dx</c>, and <c>int (c + d x)^m F^(a + b x) dx</c>, where the integral is a gamma
352359
/// function of <c>s = (m + 1)/n</c>, for an <c>s</c> neither whole nor half a whole number -- where it is
353360
/// elementary, an exponential integral or an error function, and those are answered as such -- or not a
354361
/// number at all. With <c>z = -b ln(F) x^n</c>, <c>x^(m + 1) z^(-s)</c> is constant between the cuts and
355362
/// <c>Gamma(s, z)</c> differentiates to <c>-z^(s - 1) e^(-z) z'</c>, so the first is
356363
/// <c>-F^a x^(m + 1) z^(-s) Gamma(s, z)/n</c>; the second is the first with <c>n = 1</c> under
357-
/// <c>t = c + d x</c>. Rubi's 2.3. https://github.com/asc-community/AngouriMath/issues/2075
364+
/// <c>t = c + d x</c>. Several exponentials are read as one, and terms of one degree in the exponent as one term:
365+
/// <c>f^(a + b x^n) g^(c + d x^n)</c> is <c>e^(a ln f + c ln g + (b ln f + d ln g) x^n)</c>. Rubi's 2.3.
366+
/// https://github.com/asc-community/AngouriMath/issues/2075
358367
/// </summary>
359-
private static Entity? APowerBesideAnExponentialOfAPower(Entity expr, Entity.Variable x)
368+
private static Entity? APowerBesideAnExponentialOfAPower(Entity expr, Entity.Variable x, bool atAnyOrder = false)
360369
{
361370
// Asked of every integrand that reaches the table, so a type test comes first, and
362371
// nothing here captures a local.
363372
if (expr is not (Entity.Mulf or Entity.Divf or Entity.Powf))
364373
return null;
365374
var (numerator, denominator) = expr is Entity.Divf(var n, var d) ? (n, d) : (expr, (Entity)Entity.Number.Integer.One);
366-
Entity.Powf? exponential = null;
367-
var exponentialBelow = false;
368-
foreach (var factor in Entity.Mulf.LinearChildren(numerator))
369-
if (factor is Entity.Powf(var exponentialBase, var power) candidate && !exponentialBase.ContainsNode(x) && power.ContainsNode(x))
370-
{
371-
if (exponential is not null)
372-
return null;
373-
exponential = candidate;
374-
}
375-
foreach (var factor in Entity.Mulf.LinearChildren(denominator))
376-
if (factor is Entity.Powf(var exponentialBase, var power) candidate && !exponentialBase.ContainsNode(x) && power.ContainsNode(x))
377-
{
378-
if (exponential is not null)
379-
return null;
380-
exponential = candidate;
381-
exponentialBelow = true;
382-
}
383-
if (exponential is null)
375+
// The exponentials, read as one: F^u F^v is F^(u + v), and F^u G^v is e^(u ln F + v ln G), as
376+
// f^(a + b x^n) g^(c + d x^n) is.
377+
Entity? exponentialBase = null;
378+
Entity? exponent = null;
379+
Entity? inE = null;
380+
var sameBase = true;
381+
for (var side = 0; side < 2; side++)
382+
foreach (var factor in Entity.Mulf.LinearChildren(side == 0 ? numerator : denominator))
383+
if (factor is Entity.Powf(var @base, var power) && !@base.ContainsNode(x) && power.ContainsNode(x))
384+
{
385+
var signed = side == 0 ? power : -power;
386+
var natural = @base == MathS.e ? signed : MathS.Ln(@base) * signed;
387+
sameBase &= exponentialBase is null || exponentialBase == @base;
388+
exponentialBase ??= @base;
389+
exponent = exponent is null ? signed : exponent + signed;
390+
inE = inE is null ? natural : inE + natural;
391+
}
392+
if (exponentialBase is null || exponent is null || inE is null)
384393
return null;
394+
if (!sameBase)
395+
(exponentialBase, exponent) = (MathS.e, inE);
385396

386397
// What is beside it: constants, and powers of multiples of x, (d x)^m, whose product P has
387398
// P'/P = M/x with M the sum of their exponents -- or one power of a linear with an offset.
@@ -392,22 +403,26 @@ private static bool TakeMomentFactor(Entity factor, bool inverted, Entity.Variab
392403
var allOfX = true;
393404
var allMultiples = true;
394405
Entity? single = null;
406+
Entity? before = null;
395407
var singleBelow = false;
408+
var anyBelow = false;
396409
Entity singleBase = x;
397410
Entity singleScale = Entity.Number.Integer.One;
398411
for (var side = 0; side < 2; side++)
399412
foreach (var factor in Entity.Mulf.LinearChildren(side == 0 ? numerator : denominator))
400413
{
401-
if (ReferenceEquals(factor, exponential))
414+
if (factor is Entity.Powf(var exponentialFactorsBase, var power) && !exponentialFactorsBase.ContainsNode(x) && power.ContainsNode(x))
402415
continue;
403416
if (!factor.ContainsNode(x))
404417
{
405418
coefficient = side == 0 ? coefficient * factor : coefficient / factor;
406419
continue;
407420
}
408421
factorsInX++;
422+
before = single;
409423
single = factor;
410424
singleBelow = side == 1;
425+
anyBelow |= side == 1;
411426
if (TryReadAPowerOfAMultiple(factor, x, out var @base, out var scale, out var m))
412427
{
413428
written = written is null ? (side == 0 ? factor : 1 / factor) : side == 0 ? written * factor : written / factor;
@@ -419,27 +434,29 @@ private static bool TakeMomentFactor(Entity factor, bool inverted, Entity.Variab
419434
else
420435
allMultiples = false;
421436
}
422-
var exponent = exponentialBelow ? -exponential.Exponent : exponential.Exponent;
423-
var logarithm = exponential.Base == MathS.e ? Entity.Number.Integer.One : MathS.Ln(exponential.Base);
437+
var logarithm = exponentialBase == MathS.e ? Entity.Number.Integer.One : MathS.Ln(exponentialBase);
424438

425439
// The exponent as a + b x^n, the term in x as written, or as expanded: -(c + d x^3) is one term as written, and
426440
// it is how sinh(c + d x^3) arrives, as (e^(c + d x^3) - e^(-(c + d x^3)))/2.
427441
Entity constant = Entity.Number.Integer.Zero;
428442
Entity? term = null;
429443
Entity? degree = null;
444+
var collected = 0;
430445
for (var reading = 0; reading < 2 && term is null; reading++)
431446
{
432447
var asRead = reading == 0 ? exponent : exponent.Expand();
433448
if (reading == 1 && asRead == exponent)
434449
break;
435450
constant = Entity.Number.Integer.Zero;
451+
// And terms of one degree as one: b x^n + d x^n is (b + d) x^n.
436452
foreach (var summand in Entity.Sumf.LinearChildren(asRead))
437453
if (!summand.ContainsNode(x))
438454
constant += summand;
439-
else if (term is null && ThePowerOfX(summand, x) is { } read)
455+
else if (ThePowerOfX(summand, x) is { } read && (term is null || read == degree))
440456
{
441-
term = summand;
457+
term = term is null ? summand : term + summand;
442458
degree = read;
459+
collected++;
443460
}
444461
else
445462
{
@@ -458,19 +475,24 @@ private static bool TakeMomentFactor(Entity factor, bool inverted, Entity.Variab
458475
}
459476
if (term is null || degree is null || TreeAnalyzer.IsZero(degree))
460477
return null;
478+
// Terms gathered from two summands or two exponentials can cancel: e^x e^(-x)/x is 1/x, and read as a gamma
479+
// function of order 0 at 0 it was NaN, by parts beside Shi(a + b x)^2. Decided by Simplify, which only the
480+
// gathered ones reach.
481+
if (collected > 1 && !term.Simplify().ContainsNode(x))
482+
return null;
461483

462484
if (allMultiples)
463485
{
464486
var mPlusOne = (exponents + 1).InnerSimplified;
465487
var s = TheOrder(mPlusOne / degree);
466-
if (WholeOrHalfWhole(s))
488+
if (!atAnyOrder && WholeOrHalfWhole(s))
467489
return null;
468490
// z = k x for a linear exponent, with the rate read as the number it may be.
469491
var linearRate = degree.Equals(Entity.Number.Integer.One) && TreeAnalyzer.TryGetPolyLinear(term, x, out var rateOfTerm, out _)
470492
? (-logarithm * rateOfTerm).InnerSimplified
471493
: null;
472494
var z = linearRate is null ? (-logarithm * term).InnerSimplified : (linearRate * x).InnerSimplified;
473-
var leading = (-coefficient / degree).InnerSimplified * Exponential(exponential.Base, constant);
495+
var leading = (-coefficient / degree).InnerSimplified * Exponential(exponentialBase, constant);
474496
// For a positive k, x^(m + 1) z^(-s) is k^(-s) on every branch, and for a whole s with any k.
475497
if (linearRate is not null && allOfX && (linearRate is Entity.Number.Real { IsPositive: true } || s is Entity.Number.Integer))
476498
return leading * MathS.Pow(linearRate, (-s).InnerSimplified).InnerSimplified * MathS.IncompleteGamma(s, z);
@@ -491,17 +513,47 @@ private static bool TakeMomentFactor(Entity factor, bool inverted, Entity.Variab
491513
return leading * constantFactor * MathS.IncompleteGamma(s, z);
492514
}
493515

516+
// And a polynomial beside a power of a linear with an offset, written in powers of the linear, each term then that
517+
// power alone: (c + d x)^m (k + f x) F^(a + b x) is (c + d x)^m ((k d - f c) + f (c + d x))/d F^(a + b x). By
518+
// parts leaves this of (c + d x)^m gamma(2, a + b x).
519+
if (factorsInX == 2 && !anyBelow && before is not null && single is not null && degree.Equals(Entity.Number.Integer.One))
520+
foreach (var (power, polynomial) in new[] { (before, single), (single, before) })
521+
{
522+
if (power is not Entity.Powf(var linearBase, var exponentOfTheLinear) || exponentOfTheLinear.ContainsNode(x)
523+
|| exponentOfTheLinear is Entity.Number.Integer
524+
|| !TreeAnalyzer.TryGetPolyLinear(linearBase, x, out var slopeOfTheLinear, out var offsetOfTheLinear)
525+
|| TreeAnalyzer.IsZero(offsetOfTheLinear)
526+
|| !TreeAnalyzer.TryGetPolynomial(polynomial, x, out var inXsPowers) || inXsPowers.Keys.Any(degreeInX => degreeInX.Sign < 0 || degreeInX.CompareTo(EInteger.FromInt32(8)) > 0))
527+
continue;
528+
var l = Entity.Variable.CreateUnique(expr, "l_linear");
529+
if (!TreeAnalyzer.TryGetPolynomial(polynomial.Substitute(x, (l - offsetOfTheLinear) / slopeOfTheLinear), l, out var inLsPowers))
530+
continue;
531+
Entity? sum = null;
532+
foreach (var termInL in inLsPowers)
533+
{
534+
var (j, q) = (termInL.Key, termInL.Value);
535+
if (TreeAnalyzer.IsZero(q))
536+
continue;
537+
var termOfIt = coefficient * q * MathS.Pow(linearBase, (exponentOfTheLinear + Entity.Number.Integer.Create(j)).InnerSimplified)
538+
* Exponential(exponentialBase, exponent);
539+
if (APowerBesideAnExponentialOfAPower(termOfIt, x, atAnyOrder) is not { } integrated)
540+
return null;
541+
sum = sum is null ? integrated : sum + integrated;
542+
}
543+
return sum;
544+
}
545+
494546
// A power of a linear with an offset, beside an exponential of a linear.
495547
if (factorsInX != 1 || !degree.Equals(Entity.Number.Integer.One)
496548
|| single is not Entity.Powf(var linear, var exponentOfLinear) || exponentOfLinear.ContainsNode(x)
497549
|| !TreeAnalyzer.TryGetPolyLinear(linear, x, out var slope, out var offset)
498550
|| !TreeAnalyzer.TryGetPolyLinear(term, x, out var rate, out _))
499551
return null;
500552
var exponentOfPower = singleBelow ? (-exponentOfLinear).InnerSimplified : exponentOfLinear;
501-
if (WholeOrHalfWhole(TheOrder(exponentOfPower + 1)))
553+
if (!atAnyOrder && WholeOrHalfWhole(TheOrder(exponentOfPower + 1)))
502554
return null;
503555
var under = (-logarithm * rate * linear / slope).InnerSimplified;
504-
return coefficient * Exponential(exponential.Base, (constant - rate * offset / slope).InnerSimplified) * (singleBelow ? 1 / single : single)
556+
return coefficient * Exponential(exponentialBase, (constant - rate * offset / slope).InnerSimplified) * (singleBelow ? 1 / single : single)
505557
* MathS.Pow(under, -exponentOfPower) * MathS.IncompleteGamma((exponentOfPower + 1).InnerSimplified, under) / (logarithm * rate);
506558
}
507559

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,6 +1858,10 @@ internal static bool ASumOverRootsWouldAnswer
18581858
// power times a rational function of x and the exponential, last: by parts and the split leave
18591859
// terms that are not elementary apart.
18601860
if ((answer = IndefiniteIntegralSolver.SolveAPowerOfAFunctionOfAnExponentialBesideXByAnAnsatz(expr, x)) is { }) return answer;
1861+
// Last, a gamma function at a whole or half-whole order, which the routes above answer as an elementary
1862+
// function, an exponential integral or an error function where they can: by parts reaches
1863+
// (c + d x)^(-1 - 3 n) F^(a + b (c + d x)^n) and not -1 - 4 n. https://github.com/asc-community/AngouriMath/issues/2075
1864+
if ((answer = IntegralPatterns.APowerBesideAnExponentialOfAPowerAtAnyOrder(expr, x)) is { }) return answer;
18611865
return null;
18621866
}
18631867
/// <summary>

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,26 @@ public void APositiveRateIsTakenOutOfThePower()
173173
[InlineData("(d*x)^m*(f - b*ln(c*x^2))^n*ln(a*x)")]
174174
public void APowerOfALogarithmBesideALinearInALogarithm(string integrand) => DifferentiatesBack(integrand);
175175

176+
/// <summary>
177+
/// An exponent written otherwise: two terms of one degree, two exponentials of other bases, and a whole order
178+
/// that by parts does not reach, answered last as the gamma function it is. And a polynomial beside a power of a
179+
/// linear, written in powers of the linear, as by parts leaves it of a gamma function of whole order. Rubi's 2.3
180+
/// and 8.6.
181+
/// </summary>
182+
[Theory]
183+
[InlineData("e^(a + c + b*x^n + d*x^n)")]
184+
[InlineData("f^(a + b*x^3)*c^(d + a*x^3)")]
185+
[InlineData("x^m*f^(a + b*x^n)/c^(d + a*x^n)")]
186+
[InlineData("(d + f*x)^(-1 - 4*n)*c^(a + b*(d + f*x)^n)")]
187+
[InlineData("(d + f*x)^m*(a + b*x)*c^(a + b*x)")]
188+
[InlineData("(d + f*x)^m*gamma(2, a + b*x)")]
189+
[InlineData("(d + f*x)^m*gamma(3, c + x)")]
190+
// By parts leaves e^u e^(-u)/u beside Shi(u)^2, whose exponentials cancel: not a gamma function of order 0 at 0,
191+
// which was NaN.
192+
[InlineData("Shi(a + b*x)^2")]
193+
[InlineData("Chi(a + b*x)^2")]
194+
public void AnExponentWrittenOtherwise(string integrand) => DifferentiatesBack(integrand);
195+
176196
/// <summary>
177197
/// Not where the integral is elementary, an exponential integral or an error function: those keep the form
178198
/// they had.

0 commit comments

Comments
 (0)