@@ -16524,9 +16524,17 @@ private static bool VanishesIdentically(Entity expr)
1652416524 /// splits cannot factor is declined by them at a cost that grows with it. Asked, not
1652516525 /// volunteered, like every rule that lands on a search of any size.
1652616526 /// </para>
16527+ /// <para>
16528+ /// <b>The sum the integrand writes.</b> Where the integrand holds <c>d + e x + f sqrt(Q)</c>
16529+ /// with <c>e^2</c> the leading coefficient of <c>Q</c> times <c>f^2</c>, that sum is the
16530+ /// variable, and its powers are powers of it: Rubi's
16531+ /// <c>(d + e x + f sqrt(a + b x + e^2 x^2/f^2))^n</c>. Asked with only that, the rule
16532+ /// answers through such a sum or not at all, which is how it is asked before the split
16533+ /// that would expand the powers of <c>Q</c> beside one.
16534+ /// </para>
1652716535 /// https://github.com/asc-community/AngouriMath/issues/718
1652816536 /// </remarks>
16529- internal static Entity? SolveByEulerSubstitution(Entity expr, Entity.Variable x)
16537+ internal static Entity? SolveByEulerSubstitution(Entity expr, Entity.Variable x, bool onlyBySumItWrites = false )
1653016538 {
1653116539
1653216540 // One square root of a quadratic in x, and otherwise a rational function of x --
@@ -16659,15 +16667,15 @@ private static bool VanishesIdentically(Entity expr)
1665916667 // monomials in `t` to the power `a`, the second a rational function of `sqrt(t)`,
1666016668 // and each is handed to the chain in `t` rather than to the rational integrator,
1666116669 // since neither is a quotient of polynomials.
16662- var powered = false;
16663- if (powersOfTheSubstitution.Count > 0)
16670+ var root = MathS.Pow(radicand, Number.Rational.Create(1, 2));
16671+ // `x - sqrt(Q)` is the same substitution with the root's sign flipped:
16672+ // `t = x - sqrt(Q)` gives the same `x(t)`, `sqrt(Q) = x - t` in place of `t - x`,
16673+ // and `t = x - sqrt(Q)` on the way back. Both signs in one integrand is neither.
16674+ // Whether the powers are flipped so, or null where they are not powers of either.
16675+ bool? FlippedForThePowers()
1666416676 {
1666516677 if (which != 1 || !(a.Evaled is Number.Integer { IsZero: false } aOne && aOne.EInteger.Equals(EInteger.One)))
1666616678 return null;
16667- // `x - sqrt(Q)` is the same substitution with the root's sign flipped:
16668- // `t = x - sqrt(Q)` gives the same `x(t)`, `sqrt(Q) = x - t` in place of `t - x`,
16669- // and `t = x - sqrt(Q)` on the way back. Both signs in one integrand is neither.
16670- var root = MathS.Pow(radicand, Number.Rational.Create(1, 2));
1667116679 var flipped = (bool?)null;
1667216680 foreach (var power in powersOfTheSubstitution)
1667316681 {
@@ -16679,17 +16687,86 @@ private static bool VanishesIdentically(Entity expr)
1667916687 flipped = minus;
1668016688 else if (flipped != minus)
1668116689 return null;
16682- expr = expr.Substitute(power, MathS.Pow(t, power.Exponent));
1668316690 }
16684- if (flipped == true)
16691+ return flipped;
16692+ }
16693+ var flippedForThePowers = powersOfTheSubstitution.Count > 0 ? FlippedForThePowers() : null;
16694+
16695+ // **The sum the integrand writes, as the variable.** `(d + e x + f sqrt(Q))^n` with
16696+ // `e^2 = a f^2` -- Rubi's, with `Q = c + b x + (e^2/f^2) x^2` -- is the first
16697+ // substitution shifted and scaled: `t = d + e x + f sqrt(Q)` squares, as
16698+ // `(t - d - e x)^2 = f^2 Q`, to an equation linear in x, the `e^2 x^2` on the left
16699+ // being the `a f^2 x^2` on the right. So `x = ((t - d)^2 - c f^2)/(2 e (t - d) + b f^2)`
16700+ // and `sqrt(Q) = (t - d - e x)/f`, exactly, with no root of `a` taken; and a power of
16701+ // the sum is a power of t, whatever its exponent. Taken where the substitution as
16702+ // written does not reach it: a symbol for `a`, whose root the generic case writes as
16703+ // `sqrt(e^2/f^2)`, which cancels against nothing, and a power of the sum besides
16704+ // `x ± sqrt(Q)`.
16705+ Entity? writtenSum = null;
16706+ Entity? writtenDerivative = null;
16707+ var named = new List<(Variable Name, Entity Value)>();
16708+ if ((aValue is null || powersOfTheSubstitution.Count > 0 && flippedForThePowers is null)
16709+ && TheSumOfALinearAndTheRoot(expr, root, a, x) is var (sum, d, e, f))
16710+ {
16711+ if (powersOfTheSubstitution.Any(power => power.Base != sum))
16712+ return null;
16713+ writtenSum = sum;
16714+ // With `rho = d - b f^2/(2e)` and `kappa = c f^2 - (b f^2/(2e))^2`,
16715+ // `2 e (t - d) + b f^2` is `2 e (t - rho)`, `sqrt(Q)` is
16716+ // `((t - rho)^2 + kappa)/(2 f (t - rho))` and `dx/dt` is
16717+ // `((t - rho)^2 + kappa)/(2 e (t - rho)^2)`. Each is named while the question is
16718+ // asked, since the rules read a symbol where they would meet the compound:
16719+ // `sqrt(d + e x + f sqrt(a + b x + e^2 x^2/f^2))` took most of a minute unnamed.
16720+ // Simplified once, while they are small: `b = 2 d e/f^2` makes `kappa` `c f^2 - d^2`.
16721+ var shiftValue = Functions.PartialFractions.Bare((d - b * MathS.Sqr(f) / (2 * e)).Simplify());
16722+ var kappaValue = Functions.PartialFractions.Bare((c * MathS.Sqr(f) - MathS.Sqr(b * MathS.Sqr(f) / (2 * e))).Simplify());
16723+ Entity shift = Number.Integer.Zero;
16724+ if (!VanishesIdentically(shiftValue))
16725+ {
16726+ var shiftName = Variable.CreateUnique(expr, "k_euler");
16727+ named.Add((shiftName, shiftValue));
16728+ shift = shiftName;
16729+ }
16730+ var kappa = Variable.CreateUnique(expr * shift, "k_euler");
16731+ named.Add((kappa, kappaValue));
16732+ xInT = (MathS.Sqr(t - d) - c * MathS.Sqr(f)) / (2 * e * (t - shift));
16733+ rootInT = (MathS.Sqr(t - shift) + kappa) / (2 * f * (t - shift));
16734+ writtenDerivative = (MathS.Sqr(t - shift) + kappa) / (2 * e * MathS.Sqr(t - shift));
16735+ backSubstitution = sum;
16736+ expr = expr.Replace(node => node == sum ? t : node);
16737+ }
16738+ if (onlyBySumItWrites && writtenSum is null)
16739+ return null;
16740+
16741+ var powered = false;
16742+ if (powersOfTheSubstitution.Count > 0)
16743+ {
16744+ if (writtenSum is null)
1668516745 {
16686- rootInT = xInT - t;
16687- backSubstitution = x - root;
16746+ if (flippedForThePowers is not { } flipped)
16747+ return null;
16748+ foreach (var power in powersOfTheSubstitution)
16749+ expr = expr.Substitute(power, MathS.Pow(t, power.Exponent));
16750+ if (flipped)
16751+ {
16752+ rootInT = xInT - t;
16753+ backSubstitution = x - root;
16754+ }
1668816755 }
1668916756 powered = true;
1669016757 }
1669116758
16692- var dxdt = xInT.Differentiate(t);
16759+ var dxdt = writtenDerivative ?? xInT.Differentiate(t);
16760+ // Back in x: the variable, then the names the written sum's constants were given.
16761+ Entity InX(Entity inT)
16762+ {
16763+ // Inner-simplified while the names stand for the constants, which is half the
16764+ // length of the answer for a half-odd power: written out, they repeat in every term.
16765+ var back = (named.Count > 0 ? inT.InnerSimplified : inT).Substitute(t, backSubstitution);
16766+ foreach (var (name, value) in named)
16767+ back = back.Substitute(name, value);
16768+ return back.InnerSimplified;
16769+ }
1669316770 // `Q^(k/2)` is the root to the `k`th power, and a whole power `Q^n` the root to the
1669416771 // `2n`th: read as the `n`th, it was `Q^(n/2)`, and `Q^2/(x + sqrt(Q))` was answered as
1669516772 // `Q/(x + sqrt(Q))`. https://github.com/asc-community/AngouriMath/issues/1770
@@ -16720,13 +16797,22 @@ node is Powf(var @base, Number.Rational power) && @base == radicand
1672016797 Entity cancelledPowered = polynomialAbove / poweredBelow;
1672116798 if (Functions.PolynomialGcd.TryCancel(polynomialAbove.InnerSimplified, poweredBelow.InnerSimplified, out var poweredByGcd) && poweredByGcd is not null)
1672216799 cancelledPowered = Functions.PartialFractions.Bare(poweredByGcd);
16800+ // A polynomial in t over a monomial in it, beside the powers, is integrated term
16801+ // by term by the power rule. That is what the written sum makes of
16802+ // `Q^k (d + e x + f sqrt(Q))^n` where `b f^2 = 2 d e`, `2 e (t - d) + b f^2` being
16803+ // `2 e t` there; the chain took seconds a term over it.
16804+ if (ByThePowerRuleTermByTerm(setAside * cancelledPowered, t) is { } termByTerm)
16805+ {
16806+ var byTerms = InX(termByTerm);
16807+ return byTerms.Nodes.Any(n => n is Number.Complex { IsNaN: true }) ? null : byTerms;
16808+ }
1672316809 var poweredInT = Functions.SingleQuotient.Combine(setAside * cancelledPowered).Simplify();
1672416810 if (poweredInT is Providedf(var bareInT, _))
1672516811 poweredInT = bareInT;
1672616812 var inTByTheChain = Integration.ComputeIndefiniteIntegral(poweredInT, t, integrateByParts: true);
1672716813 if (inTByTheChain is null)
1672816814 return null;
16729- var poweredAnswer = inTByTheChain.Substitute(t, backSubstitution).InnerSimplified ;
16815+ var poweredAnswer = InX(inTByTheChain) ;
1673016816 return poweredAnswer.Nodes.Any(n => n is Number.Complex { IsNaN: true }) ? null : poweredAnswer;
1673116817 }
1673216818 var (numerator, denominator) = Functions.SingleQuotient.Of(rewritten.InnerSimplified);
@@ -16802,11 +16888,115 @@ node is Powf(var @base, Number.Rational power) && @base == radicand
1680216888 ?? (cleanDenominator.Evaled is Number ? Integration.ComputeIndefiniteIntegral(rational, t, integrateByParts: false) : null);
1680316889 if (inT is null)
1680416890 return null;
16805- var answer = inT.Substitute(t, backSubstitution).InnerSimplified ;
16891+ var answer = InX(inT) ;
1680616892 // Not answering is legitimate; answering NaN is not.
1680716893 return answer.Nodes.Any(n => n is Number.Complex { IsNaN: true }) ? null : answer;
1680816894 }
1680916895
16896+ /// <summary>
16897+ /// The antiderivative of <paramref name="integrand"/> where it is a product of powers of
16898+ /// <paramref name="t"/> times a polynomial in it over a constant multiple of one whole
16899+ /// power of it: the sum of the power rule's antiderivatives of its terms, the generic
16900+ /// case where an exponent is a symbol. <see langword="null"/> where it is not of that shape.
16901+ /// </summary>
16902+ private static Entity? ByThePowerRuleTermByTerm(Entity integrand, Entity.Variable t)
16903+ {
16904+ var (above, below) = Functions.SingleQuotient.Of(Functions.SingleQuotient.Combine(Functions.PartialFractions.Bare(integrand)));
16905+ Entity exponent = Number.Integer.Zero;
16906+ Entity polynomialAbove = Number.Integer.One;
16907+ Entity polynomialBelow = Number.Integer.One;
16908+ foreach (var (side, sign) in new[] { (above, 1), (below, -1) })
16909+ foreach (var factor in Mulf.LinearChildren(side))
16910+ if (factor is Powf(var @base, var power) && @base == t && power is not Number.Integer && !power.ContainsNode(t))
16911+ exponent = sign > 0 ? exponent + power : exponent - power;
16912+ else if (sign > 0)
16913+ polynomialAbove *= factor;
16914+ else
16915+ polynomialBelow *= factor;
16916+ if (exponent == Number.Integer.Zero)
16917+ return null;
16918+ // Read as polynomials in every symbol, which collects like terms as it multiplies:
16919+ // the expansion of the substitution's cubes and squares, term by term, passes the
16920+ // expansion's bound before anything cancels.
16921+ var variables = polynomialAbove.Vars.Concat(polynomialBelow.Vars).Append(t).Distinct()
16922+ .OrderBy(variable => variable.Name, System.StringComparer.Ordinal).ToArray();
16923+ if (variables.Length > Functions.MultivariatePolynomial.MaxVariables)
16924+ return null;
16925+ var indices = new Dictionary<Variable, int>();
16926+ for (var i = 0; i < variables.Length; i++)
16927+ indices[variables[i]] = i;
16928+ if (Functions.MultivariatePolynomial.TryParse(polynomialAbove, indices) is not { } top
16929+ || Functions.MultivariatePolynomial.TryParse(polynomialBelow, indices) is not { } bottom
16930+ || bottom.CoefficientsIn(indices[t]) is not { Count: 1 } belowRead || top.IsZero)
16931+ return null;
16932+ var lowest = belowRead.Single();
16933+ if (lowest.Value.IsZero)
16934+ return null;
16935+ // Each coefficient over the one below the bar in lowest terms: the substitution's
16936+ // powers of f and e cancel, and left in they were most of a two-page answer.
16937+ var order = Enumerable.Range(0, variables.Length).ToArray();
16938+ var belowPrimitive = lowest.Value.Normalized(out var belowScale);
16939+ Entity Coefficient(Functions.MultivariatePolynomial coefficient)
16940+ {
16941+ var abovePrimitive = coefficient.Normalized(out var aboveScale);
16942+ var belowPart = belowPrimitive;
16943+ if (Functions.PolynomialGcd.Gcd(abovePrimitive, belowPart, order, 0) is { IsConstant: false } common
16944+ && abovePrimitive.DivideExact(common) is { } reducedAbove && belowPart.DivideExact(common) is { } reducedBelow)
16945+ (abovePrimitive, belowPart) = (reducedAbove, reducedBelow);
16946+ // Each polynomial is its primitive part over its scale.
16947+ Entity number = Number.Rational.Create(belowScale.Divide(aboveScale));
16948+ return (number * abovePrimitive.ToEntity(variables) / belowPart.ToEntity(variables)).InnerSimplified;
16949+ }
16950+ Entity? answer = null;
16951+ foreach (var term in top.CoefficientsIn(indices[t]))
16952+ {
16953+ var raised = (exponent + Number.Integer.Create(term.Key - lowest.Key + 1)).InnerSimplified;
16954+ var antiderivative = raised.Evaled is Number.Complex { IsZero: true }
16955+ ? MathS.Ln(t)
16956+ : MathS.Pow(t, raised) / raised;
16957+ var summand = Coefficient(term.Value) * antiderivative;
16958+ answer = answer is null ? summand : answer + summand;
16959+ }
16960+ return answer;
16961+ }
16962+
16963+ /// <summary>
16964+ /// Whether <paramref name="expr"/> holds a power, not a whole one, of a sum with a square
16965+ /// root in it: the shape <see cref="SolveByEulerSubstitution"/> takes as its variable
16966+ /// where the sum is <c>d + e x + f sqrt(Q)</c> with <c>e^2 = a f^2</c>.
16967+ /// </summary>
16968+ internal static bool HoldsAPowerOfASumWithARoot(Entity expr, Entity.Variable x)
16969+ => expr.Nodes.Any(node => node is Powf(Sumf or Minusf, var exponent) power
16970+ && exponent is not Number.Integer && !exponent.ContainsNode(x)
16971+ && power.Base.Nodes.Any(inner => inner is Powf(var radicand, Number.Rational half)
16972+ && half.ERational.Denominator.Equals(EInteger.FromInt32(2)) && radicand.ContainsNode(x)));
16973+
16974+ /// <summary>
16975+ /// The one sum <c>d + e x + f sqrt(Q)</c> in <paramref name="expr"/> with <c>e^2 = a f^2</c>,
16976+ /// <c>a</c> the <paramref name="leading"/> coefficient of <c>Q</c>, and its <c>d</c>,
16977+ /// <c>e</c> and <c>f</c>; <see langword="null"/> where there is none, or more than one.
16978+ /// </summary>
16979+ private static (Entity Sum, Entity Constant, Entity Slope, Entity OfTheRoot)? TheSumOfALinearAndTheRoot(
16980+ Entity expr, Entity root, Entity leading, Entity.Variable x)
16981+ {
16982+ var r = Entity.Variable.CreateUnique(expr, "r_euler");
16983+ (Entity Sum, Entity Constant, Entity Slope, Entity OfTheRoot)? found = null;
16984+ foreach (var node in expr.Nodes)
16985+ {
16986+ if (node is not (Sumf or Minusf) || !node.ContainsNode(root))
16987+ continue;
16988+ if (!TreeAnalyzer.TryGetPolyLinear(node.Substitute(root, r), r, out var ofTheRoot, out var rest)
16989+ || ofTheRoot.ContainsNode(x) || !TreeAnalyzer.TryGetPolyLinear(rest, x, out var slope, out var constant)
16990+ || VanishesIdentically(ofTheRoot) || VanishesIdentically(slope)
16991+ || !VanishesIdentically(slope * slope - leading * ofTheRoot * ofTheRoot))
16992+ continue;
16993+ if (found is { } other && other.Sum != node)
16994+ return null;
16995+ found = (node, constant, slope, ofTheRoot);
16996+ }
16997+ return found;
16998+ }
16999+
1681017000 /// <summary>
1681117001 /// A rational function of <c>x</c> and one square root of a <b>palindromic quartic</b>
1681217002 /// <c>a x^4 + d x^3 + b x^2 + s d x + a</c>, integrated by <c>u = x - 1/x</c> or
0 commit comments