@@ -3350,6 +3350,209 @@ private static (Entity Elementary, Entity EiCoefficient) APowerTimesAnExponentia
33503350 return (exponential * sum, Number.Integer.Zero);
33513351 }
33523352
3353+ /// <summary>
3354+ /// A sum of exponentials of the variable, times a polynomial, over a whole power of a
3355+ /// linear: <c>P(x) sum_j c_j e^(r_j x)/(e + f x)^n</c>, which is how a polynomial in
3356+ /// <c>sinh</c> and <c>cosh</c> of a linear arrives, onto the hyperbolic sine and cosine
3357+ /// integrals. Under <c>u = e + f x</c> each term is <c>u^m e^(k u)</c>, the exponential
3358+ /// integral's (<see cref="APowerTimesAnExponential"/>), and two of opposite rates pair:
3359+ /// <c>A Ei(k u) + B Ei(-k u) = (A + B) Chi(k u) + (A - B) Shi(k u)</c>, since
3360+ /// <c>Ei(y) + Ei(-y) = 2 Chi(y)</c> and <c>Ei(y) - Ei(-y) = 2 Shi(y)</c> for <c>y > 0</c>, and
3361+ /// for <c>y < 0</c> up to a constant. A rate of 0, which an even power leaves, is a power
3362+ /// of <c>u</c> alone. Rubi's 6.1.1, <c>(c + d x)^m (a + b sinh(e + f x))^n</c>, with
3363+ /// <c>m</c> negative.
3364+ /// https://github.com/asc-community/AngouriMath/issues/1501
3365+ /// </summary>
3366+ /// <remarks>
3367+ /// After <see cref="SolveAnExponentialOfALinearOverAPowerOfALinear"/>, which answers one
3368+ /// exponential alone; and only where an integral is left.
3369+ /// </remarks>
3370+ internal static Entity? SolveAHyperbolicOfALinearOverAPowerOfALinear(Entity expr, Entity.Variable x)
3371+ {
3372+ if (expr is not (Divf or Mulf) || !HasASumOfExponentials(expr, x))
3373+ return null;
3374+ Entity constant = Number.Integer.One;
3375+ Entity? linear = null;
3376+ var power = 0;
3377+ Entity? polynomial = null;
3378+ Entity? exponentials = null;
3379+ foreach (var (factor, underneath) in FactorsOfTheIntegrand(expr))
3380+ {
3381+ if (!factor.ContainsNode(x))
3382+ {
3383+ constant = underneath ? constant / factor : constant * factor;
3384+ continue;
3385+ }
3386+ var (@base, exponent) = factor is Powf(var raised, Number.Integer whole) && whole.EInteger.CanFitInInt32() && !whole.EInteger.IsZero
3387+ ? (raised, whole.EInteger.ToInt32Checked()) : (factor, 1);
3388+ if (underneath)
3389+ exponent = -exponent;
3390+ if (exponent < 0 && TreeAnalyzer.TryGetPolyLinear(@base, x, out var slopeOfIt, out _) && !TreeAnalyzer.IsZero(slopeOfIt))
3391+ {
3392+ if (linear is not null && linear != @base)
3393+ return null;
3394+ linear = @base;
3395+ power -= exponent;
3396+ continue;
3397+ }
3398+ if (underneath)
3399+ return null;
3400+ if (factor.Nodes.Any(node => node is Powf(var b, var e) && !b.ContainsNode(x) && e.ContainsNode(x)))
3401+ {
3402+ exponentials = exponentials is null ? factor : exponentials * factor;
3403+ continue;
3404+ }
3405+ if (TreeAnalyzer.TryGetPolynomial(factor, x, out var monomials) && monomials.Keys.All(degree => degree.Sign >= 0 && degree.CompareTo(EInteger.FromInt32(12)) <= 0))
3406+ {
3407+ polynomial = polynomial is null ? factor : polynomial * factor;
3408+ continue;
3409+ }
3410+ return null;
3411+ }
3412+ if (exponentials is null || linear is null || power > 12 || !TreeAnalyzer.TryGetPolyLinear(linear, x, out var f, out var e0))
3413+ return null;
3414+ // The exponentials as a sum of c e^(r x), gathered by the rate r.
3415+ var rates = new List<(Entity Rate, Entity Coefficient)>();
3416+ foreach (var term in Sumf.LinearChildren(exponentials.Expand()))
3417+ {
3418+ if (ReadAnExponentialTerm(term, x) is not { } read)
3419+ return null;
3420+ // The same rate may be written two ways, `2 (-b)` and `-2 b`.
3421+ var at = rates.FindIndex(pair => pair.Rate == read.Rate
3422+ || pair.Rate.Vars.Any() && TreeAnalyzer.IsZero(Functions.PartialFractions.Bare((pair.Rate - read.Rate).Simplify())));
3423+ if (at < 0)
3424+ rates.Add(read);
3425+ else
3426+ rates[at] = (read.Rate, rates[at].Coefficient + read.Coefficient);
3427+ }
3428+ var u = Variable.CreateUnique(expr, "u");
3429+ var inU = polynomial is null ? Number.Integer.One : polynomial.Substitute(x, (u - e0) / f);
3430+ if (!TreeAnalyzer.TryGetPolynomial(inU, u, out var powers) || powers.Keys.All(degree => degree.CompareTo(EInteger.FromInt32(power)) >= 0)
3431+ || powers.Keys.Any(degree => !degree.CanFitInInt32()))
3432+ return null;
3433+ Entity sum = Number.Integer.Zero;
3434+ // The constant in front goes into each coefficient, so that the numbers fold.
3435+ var overall = (constant / f).InnerSimplified;
3436+ // The coefficient of Ei(k u) for each rate k in u, to be paired.
3437+ var integrals = new List<(Entity Rate, Entity Coefficient)>();
3438+ foreach (var (rate, coefficientOfTheRate) in rates)
3439+ {
3440+ var coefficient = (overall * coefficientOfTheRate).InnerSimplified;
3441+ if (TreeAnalyzer.IsZero(rate))
3442+ {
3443+ // The terms of rate 0 come from several products and may cancel, as in
3444+ // `cosh(y) sinh(y)^3`, where `e^(2 a) e^(-2 a)` has to be seen to be 1.
3445+ var constantPart = Functions.PartialFractions.Bare(coefficient.Simplify());
3446+ if (TreeAnalyzer.IsZero(constantPart))
3447+ continue;
3448+ foreach (var monomial in powers)
3449+ {
3450+ var m = monomial.Key.ToInt32Checked() - power;
3451+ sum += constantPart * monomial.Value * (m == -1 ? MathS.Ln(u) : MathS.Pow(u, m + 1) / (m + 1));
3452+ }
3453+ continue;
3454+ }
3455+ // e^(r x) = e^(-r e/f) e^(k u) with k = r/f.
3456+ var k = (rate / f).InnerSimplified;
3457+ var shifted = (coefficient * MathS.Pow(MathS.e, (-rate * e0 / f).InnerSimplified)).InnerSimplified;
3458+ var exponentialOfU = MathS.Pow(MathS.e, k * u);
3459+ Entity elementary = Number.Integer.Zero, ei = Number.Integer.Zero;
3460+ foreach (var monomial in powers)
3461+ {
3462+ var (below, eiBelow) = APowerTimesAnExponential(monomial.Key.ToInt32Checked() - power, k, exponentialOfU, u);
3463+ elementary += monomial.Value * below;
3464+ ei += monomial.Value * eiBelow;
3465+ }
3466+ sum += shifted * elementary;
3467+ ei = (shifted * ei).InnerSimplified;
3468+ if (!TreeAnalyzer.IsZero(ei))
3469+ integrals.Add((k, ei));
3470+ }
3471+ if (integrals.Count == 0)
3472+ return null;
3473+ // Pairs of opposite rates as Chi and Shi, the positive rate written.
3474+ while (integrals.Count > 0)
3475+ {
3476+ var (k, a) = integrals[0];
3477+ integrals.RemoveAt(0);
3478+ // Simplified, since `d/f` and `-d/f` do not cancel inner-simplified.
3479+ var opposite = integrals.FindIndex(pair => TreeAnalyzer.IsZero(Functions.PartialFractions.Bare((pair.Rate + k).Simplify())));
3480+ if (opposite < 0)
3481+ {
3482+ sum += Functions.PartialFractions.Bare(a.Simplify()) * MathS.Ei(k * u);
3483+ continue;
3484+ }
3485+ var b = integrals[opposite].Coefficient;
3486+ integrals.RemoveAt(opposite);
3487+ if (k.Evaled is Number.Real { IsNegative: true })
3488+ (k, a, b) = ((-k).InnerSimplified, b, a);
3489+ // The two coefficients simplified, which is where `e e^(-4/3)` becomes `e^(-1/3)`.
3490+ sum += Functions.PartialFractions.Bare((a + b).Simplify()) * MathS.Chi(k * u)
3491+ + Functions.PartialFractions.Bare((a - b).Simplify()) * MathS.Shi(k * u);
3492+ }
3493+ return Functions.PartialFractions.Bare(sum.Substitute(u, linear).InnerSimplified);
3494+ }
3495+
3496+ /// <summary>
3497+ /// A term <c>c e^(r x)</c> of an expanded sum of exponentials, as its rate and coefficient:
3498+ /// a product of constants and of constants to linears in <paramref name="x"/>, or
3499+ /// <see langword="null"/>.
3500+ /// </summary>
3501+ private static (Entity Rate, Entity Coefficient)? ReadAnExponentialTerm(Entity term, Entity.Variable x)
3502+ {
3503+ Entity rate = Number.Integer.Zero;
3504+ Entity coefficient = Number.Integer.One;
3505+ foreach (var (written, underneath) in FactorsOfTheIntegrand(term))
3506+ if (!Read(written, underneath ? -1 : 1))
3507+ return null;
3508+ // The rate is a sum of the slopes, `b + b - b - b` for four exponentials of `a + b x`,
3509+ // and inner simplification leaves that as `2 b + 2 (-b)`, a zero nothing reads: a
3510+ // rate with a symbol in it is simplified.
3511+ var folded = rate.InnerSimplified;
3512+ if (folded.Vars.Any())
3513+ folded = Functions.PartialFractions.Bare(folded.Simplify());
3514+ return (folded, coefficient.InnerSimplified);
3515+
3516+ // A factor to a whole power: a constant, an exponential of a linear, or a product or
3517+ // a quotient of those -- `(e^(2 x)/4)^2`, as the expansion of `sinh(x)^2 cosh(x)^2`
3518+ // leaves one, is `e^(4 x)/16`.
3519+ bool Read(Entity factor, int times)
3520+ {
3521+ while (factor is Powf(var inner, Number.Integer whole) && whole.EInteger.CanFitInInt32())
3522+ (factor, times) = (inner, times * whole.EInteger.ToInt32Checked());
3523+ if (!factor.ContainsNode(x))
3524+ {
3525+ coefficient *= times == 1 ? factor : MathS.Pow(factor, times);
3526+ return true;
3527+ }
3528+ switch (factor)
3529+ {
3530+ case Mulf(var left, var right):
3531+ return Read(left, times) && Read(right, times);
3532+ case Divf(var numerator, var denominator):
3533+ return Read(numerator, times) && Read(denominator, -times);
3534+ case Powf(var @base, var exponent) when !@base.ContainsNode(x)
3535+ && TreeAnalyzer.TryGetPolyLinear(exponent, x, out var slope, out var intercept):
3536+ var logarithm = @base == MathS.e ? (Entity)Number.Integer.One : MathS.Ln(@base);
3537+ rate += times * slope * logarithm;
3538+ coefficient *= MathS.Pow(@base, times * intercept);
3539+ return true;
3540+ default:
3541+ return false;
3542+ }
3543+ }
3544+ }
3545+
3546+ /// <summary>Whether a factor of <paramref name="expr"/> is a sum with an exponential of the variable in it.</summary>
3547+ private static bool HasASumOfExponentials(Entity expr, Entity.Variable x) => expr switch
3548+ {
3549+ Sumf or Minusf => expr.Nodes.Any(node => node is Powf(var b, var e) && !b.ContainsNode(x) && e.ContainsNode(x)),
3550+ Powf(var @base, Number.Integer) => HasASumOfExponentials(@base, x),
3551+ Mulf(var left, var right) => HasASumOfExponentials(left, x) || HasASumOfExponentials(right, x),
3552+ Divf(var numerator, _) => HasASumOfExponentials(numerator, x),
3553+ _ => false,
3554+ };
3555+
33533556 /// <summary>
33543557 /// A factor of <paramref name="expr"/>, read through products and quotients, that is a
33553558 /// constant to a linear in <paramref name="x"/>, above the bar, or <see langword="null"/>.
0 commit comments