@@ -8536,6 +8536,161 @@ private static Entity SquarefreePartAsWritten(Entity denominator, Entity.Variabl
85368536 return sum.InnerSimplified;
85378537 }
85388538
8539+ /// <summary>
8540+ /// A power of x times a half-odd power of a logarithm, <c>x^p F^n</c> with <c>F</c> =
8541+ /// <c>ln x</c> or <c>A + B ln(c x^r)</c> and <c>2n</c> odd, onto the Gaussian's moments.
8542+ /// With <c>t = sqrt(F)</c> and <c>F' = s/x</c>, <c>dx = 2 x t dt/s</c>, and
8543+ /// <c>x^(p + 1) e^(-(p + 1) F/s)</c> is a constant, so the integral is that constant times
8544+ /// <c>2/s</c> times the integral of <c>t^(2n + 1) e^((p + 1) t^2/s)</c>: an even moment of
8545+ /// the Gaussian, which the table answers. At <c>p = -1</c> it is
8546+ /// <c>F^(n + 1)/(s (n + 1))</c>. Rubi's 3.1.2, <c>(d x)^m (a + b ln(c x^n))^p</c> for a
8547+ /// half-odd <c>p</c>.
8548+ /// https://github.com/asc-community/AngouriMath/issues/1501
8549+ /// </summary>
8550+ /// <remarks>
8551+ /// The constant is written <c>x^(p + 1) e^(-(p + 1) F/s)</c>, as Rubi writes it, rather
8552+ /// than with <c>ln(c x^r)</c> split into <c>ln c + r ln x</c>, which holds only where
8553+ /// <c>c</c> is positive. The logarithm to the right of a whole power of it is
8554+ /// <see cref="SolveAPowerTimesAPowerOfTheLogarithm"/>'s.
8555+ /// </remarks>
8556+ internal static Entity? SolveAPowerTimesAHalfOddPowerOfTheLogarithm(Entity expr, Entity.Variable x)
8557+ {
8558+ Entity? p = null;
8559+ Number.Rational? n = null;
8560+ Entity constant = Number.Integer.One;
8561+ Entity? logarithm = null;
8562+ Dictionary<EInteger, Entity>? polynomial = null;
8563+ foreach (var (factor, underneath) in FactorsOfTheIntegrand(expr))
8564+ {
8565+ if (!factor.ContainsNode(x))
8566+ {
8567+ constant = underneath ? constant / factor : constant * factor;
8568+ continue;
8569+ }
8570+ if (factor == x && p is null)
8571+ p = underneath ? Number.Integer.MinusOne : Number.Integer.One;
8572+ else if (factor is Powf(var @base, var exponent) && @base == x && !exponent.ContainsNode(x) && p is null)
8573+ p = underneath ? -exponent : exponent;
8574+ else if (factor is Powf(Mulf(var scale, var scaled), var scaledExponent) && scaled == x && !scale.ContainsNode(x) && !scaledExponent.ContainsNode(x) && p is null)
8575+ {
8576+ p = underneath ? -scaledExponent : scaledExponent;
8577+ var scalePower = MathS.Pow(scale, scaledExponent);
8578+ constant = underneath ? constant / scalePower : constant * scalePower;
8579+ }
8580+ else if (n is null && factor is Powf(var f, Number.Rational half) && half.ERational.Denominator.Equals(EInteger.FromInt32(2)))
8581+ {
8582+ if (f == MathS.Ln(x) || IsAffineInALogarithmOfAPowerOfX(f, x))
8583+ (logarithm, n) = (f, underneath ? Number.Rational.Create(-half.ERational) : half);
8584+ // The logarithm of a power of a linear, Rubi's 3.3: the same question under
8585+ // u = d + e x. Declined, it was integrated by parts against the half-odd
8586+ // power's own antiderivative, which took (f + g x) (a + b ln(c (d + e x)^n))^(3/2)
8587+ // 37 s to give up.
8588+ else if (LinearUnderALogarithm(f, x) is { } linear)
8589+ return ByTheLinearUnderTheLogarithm(expr, x, linear);
8590+ else
8591+ return null;
8592+ }
8593+ // A polynomial beside it, which is how a linear outside the logarithm arrives once
8594+ // the linear inside it has been substituted: summed a power at a time.
8595+ else if (polynomial is null && !underneath && TreeAnalyzer.TryGetPolynomial(factor, x, out var monomials)
8596+ && monomials.Count > 1 && monomials.Keys.All(power => power.Sign >= 0 && power.CompareTo(EInteger.FromInt32(12)) <= 0))
8597+ polynomial = monomials;
8598+ else
8599+ return null;
8600+ }
8601+ if (logarithm is null || n is null)
8602+ return null;
8603+ p = (p ?? Number.Integer.Zero).InnerSimplified;
8604+ if (p.Evaled is Number.Complex and not Number.Real)
8605+ return null;
8606+ Entity slopeOfF = Number.Integer.One;
8607+ if (logarithm != MathS.Ln(x))
8608+ {
8609+ slopeOfF = Functions.PartialFractions.Bare((logarithm.Differentiate(x) * x).Simplify());
8610+ if (slopeOfF.ContainsNode(x) || slopeOfF.Evaled is Number.Complex { IsZero: true })
8611+ return null;
8612+ }
8613+ if (polynomial is null)
8614+ return HalfOddLogarithmicTerm(p, logarithm, n, slopeOfF, x) is { } single ? (constant * single).InnerSimplified : null;
8615+ Entity sum = Number.Integer.Zero;
8616+ foreach (var monomial in polynomial)
8617+ {
8618+ if (HalfOddLogarithmicTerm((p + Number.Integer.Create(monomial.Key)).InnerSimplified, logarithm, n, slopeOfF, x) is not { } term)
8619+ return null;
8620+ sum += monomial.Value * term;
8621+ }
8622+ return (constant * sum).InnerSimplified;
8623+ }
8624+
8625+ /// <summary>
8626+ /// The linear <c>d + e x</c>, with <c>d</c> not zero, whose powers and constant multiples
8627+ /// make up the argument of the one logarithm in <paramref name="expr"/>, or
8628+ /// <see langword="null"/>.
8629+ /// </summary>
8630+ private static Entity? LinearUnderALogarithm(Entity expr, Entity.Variable x)
8631+ {
8632+ Entity? argument = null;
8633+ foreach (var node in expr.Nodes)
8634+ if (node is Logf(var @base, var antilogarithm) && @base == MathS.e && antilogarithm.ContainsNode(x))
8635+ {
8636+ if (argument is not null && argument != antilogarithm)
8637+ return null;
8638+ argument = antilogarithm;
8639+ }
8640+ if (argument is null)
8641+ return null;
8642+ foreach (var node in argument.Nodes)
8643+ if (node is Sumf or Minusf && TreeAnalyzer.TryGetPolyLinear(node, x, out var slope, out var intercept)
8644+ && !TreeAnalyzer.IsZero(slope) && !TreeAnalyzer.IsZero(intercept))
8645+ {
8646+ var placeholder = Variable.CreateUnique(argument, "u_lin");
8647+ var inThePlaceholder = argument.Replace(child => child == node ? placeholder : child);
8648+ return !inThePlaceholder.ContainsNode(x) && IsAProductOfPowersOfTheVariable(inThePlaceholder, placeholder) ? node : null;
8649+ }
8650+ return null;
8651+ }
8652+
8653+ /// <summary>
8654+ /// <paramref name="expr"/> under <c>u = d + e x</c>, with <paramref name="linear"/> the
8655+ /// <c>d + e x</c> under its logarithm, answered as a power or a polynomial of <c>u</c>
8656+ /// beside a half-odd power of <c>A + B ln(c u^r)</c> and written back in <c>x</c>.
8657+ /// </summary>
8658+ private static Entity? ByTheLinearUnderTheLogarithm(Entity expr, Entity.Variable x, Entity linear)
8659+ {
8660+ if (!TreeAnalyzer.TryGetPolyLinear(linear, x, out var slope, out var intercept) || slope is null || intercept is null)
8661+ return null;
8662+ var u = Variable.CreateUnique(expr, "u");
8663+ var inU = (expr.Replace(node => node == linear ? u : node).Substitute(x, (u - intercept) / slope) / slope).InnerSimplified;
8664+ if (inU.ContainsNode(x) || LinearUnderALogarithm(inU, u) is not null)
8665+ return null;
8666+ return SolveAPowerTimesAHalfOddPowerOfTheLogarithm(inU, u)?.Substitute(u, linear).InnerSimplified;
8667+ }
8668+
8669+ /// <summary>
8670+ /// The integral of <c>x^q F^n</c> for a half-odd <c>n</c> and <c>F' = s/x</c>: see
8671+ /// <see cref="SolveAPowerTimesAHalfOddPowerOfTheLogarithm"/>.
8672+ /// </summary>
8673+ private static Entity? HalfOddLogarithmicTerm(Entity q, Entity logarithm, Number.Rational n, Entity slopeOfF, Entity.Variable x)
8674+ {
8675+ var qPlusOne = (q + 1).InnerSimplified;
8676+ if (qPlusOne.Evaled is Number.Complex { IsZero: true })
8677+ return MathS.Pow(logarithm, n + 1) / ((n + 1) * slopeOfF);
8678+ // t^(2n + 1) e^((q + 1) t^2/s) in a variable of its own, for the table's moments.
8679+ var t = Variable.CreateUnique(logarithm, "t");
8680+ var power = (2 * n.ERational + ERational.One).ToEInteger();
8681+ if (!power.CanFitInInt32() || power.Abs().CompareTo(EInteger.FromInt32(32)) > 0)
8682+ return null;
8683+ var gaussian = MathS.Pow(MathS.e, (qPlusOne / slopeOfF).InnerSimplified * MathS.Sqr(t));
8684+ var inT = power.IsZero ? gaussian : MathS.Pow(t, Number.Integer.Create(power)) * gaussian;
8685+ if (IntegralPatterns.TryStandardIntegrals(inT, t) is not { } answerInT)
8686+ return null;
8687+ // x^(q + 1) e^(-(q + 1) ln(x)) is 1 wherever ln(x) is real, so for ln(x) itself it is left out.
8688+ Entity theConstant = logarithm == MathS.Ln(x)
8689+ ? Number.Integer.One
8690+ : MathS.Pow(x, qPlusOne) * MathS.Pow(MathS.e, -qPlusOne * logarithm / slopeOfF);
8691+ return 2 / slopeOfF * theConstant * answerInT.Substitute(t, MathS.Sqrt(logarithm));
8692+ }
8693+
85398694 /// <summary>
85408695 /// Whether <paramref name="expr"/> is <c>A + B ln(c x^r)</c> with <c>A</c>, <c>B</c>,
85418696 /// <c>c</c> and <c>r</c> free of <paramref name="x"/> and the logarithm present: one
0 commit comments