@@ -9093,6 +9093,13 @@ Dictionary<EInteger, Entity> PowerOfQ(int j)
90939093 && TryReadAPowerTimesARadicalQuadratic(expr, x))
90949094 return null;
90959095
9096+ // A perfect square under the root is no radical: `sqrt(u^2)` is `|u|`, which
9097+ // `SolveByTakingARootOfAPerfectSquare` writes as `sgn(u) u`, and the substitution
9098+ // written for it -- `t = sqrt(u^2) - u`, which is zero on half the line -- is
9099+ // declined rather than made.
9100+ if ((b * b - Number.Integer.Create(4) * a * c).InnerSimplified.Evaled is Number.Complex { IsZero: true })
9101+ return null;
9102+
90969103 var t = Variable.CreateUnique(expr, "t_euler");
90979104 Entity xInT, rootInT;
90989105 Entity backSubstitution;
@@ -10041,6 +10048,146 @@ Entity Without(Entity side)
1004110048 return answer;
1004210049 }
1004310050
10051+ /// <summary>
10052+ /// <c>x^(n - 1) g(x^n)</c> with a symbolic <c>n</c>, which is <c>g(u)/n</c> under
10053+ /// <c>u = x^n</c>: the power in front is the derivative of the power inside up to the
10054+ /// constant <c>n</c>, and the rest mentions <c>x</c> only as <c>x^n</c>. Rubi's 6.5.2 and
10055+ /// 6.6.2 -- <c>(e x)^(n - 1) (a + b sech(c + d x^n))^p</c> -- write the power in front as
10056+ /// <c>(e x)^(n - 1)</c>, which is <c>e^(n - 1) x^(n - 1)</c> for a positive <c>e</c>, and
10057+ /// the answer says so. A concrete <c>n</c> is the general substitution's; a symbolic
10058+ /// one was nobody's, and <c>x^(n - 1) e^(x^n)</c> was declined.
10059+ /// https://github.com/asc-community/AngouriMath/issues/718
10060+ /// </summary>
10061+ internal static Entity? SolveByAPowerOfTheVariableTimesAFunctionOfItsPower(Entity expr, Entity.Variable x, bool integrateByParts)
10062+ {
10063+ Entity? inFront = null;
10064+ Entity? scale = null;
10065+ Entity? rest = null;
10066+ Entity constant = Number.Integer.One;
10067+ foreach (var (factor, underneath) in FactorsOfTheIntegrand(expr))
10068+ {
10069+ if (!factor.ContainsNode(x))
10070+ {
10071+ constant = underneath ? constant / factor : constant * factor;
10072+ continue;
10073+ }
10074+ // The power in front: x^m, or (e x)^m with e free of x, to a symbolic m.
10075+ if (inFront is null && !underneath && factor is Powf(var @base, var exponent) && !exponent.ContainsNode(x)
10076+ && exponent.Evaled is not Number && exponent.Vars.Any()
10077+ && (@base == x || @base is Mulf(var l, var r) && (l == x && !r.ContainsNode(x) || r == x && !l.ContainsNode(x))))
10078+ {
10079+ inFront = exponent;
10080+ scale = @base == x ? null : @base is Mulf(var l2, var r2) ? (l2 == x ? r2 : l2) : null;
10081+ continue;
10082+ }
10083+ var piece = underneath ? Number.Integer.One / factor : factor;
10084+ rest = rest is null ? piece : rest * piece;
10085+ }
10086+ if (inFront is null || rest is null)
10087+ return null;
10088+ // The rest mentions x only as x^n, one n throughout.
10089+ Entity? n = null;
10090+ foreach (var node in rest.Nodes)
10091+ if (node is Powf(var b, var e) && b == x && !e.ContainsNode(x))
10092+ {
10093+ if (n is null)
10094+ n = e;
10095+ else if (n != e)
10096+ return null;
10097+ }
10098+ if (n is null || n.Evaled is Number)
10099+ return null;
10100+ // m + 1 = k n for a whole k >= 1: x^(k n - 1) g(x^n) is u^(k - 1) g(u)/n.
10101+ // Bare: the simplification attaches `provided not n = 0` to the quotient it
10102+ // cancelled, and the generic case is what every rule answers in.
10103+ var timesN = Functions.PartialFractions.Bare(((inFront + Number.Integer.One) / n).InnerSimplified);
10104+ if (timesN is not Number.Integer k || k.EInteger.Sign <= 0)
10105+ {
10106+ timesN = Functions.PartialFractions.Bare(((inFront + Number.Integer.One) / n).Simplify());
10107+ if (timesN is not Number.Integer k2 || k2.EInteger.Sign <= 0)
10108+ return null;
10109+ k = k2;
10110+ }
10111+ var u = Variable.CreateUnique(expr, "u_pow");
10112+ var inU = rest.Replace(node => node is Powf(var b, var e) && b == x && e == n ? u : node);
10113+ if (inU.ContainsNode(x))
10114+ return null;
10115+ if (k != Number.Integer.One)
10116+ inU = MathS.Pow(u, (k - Number.Integer.One).InnerSimplified) * inU;
10117+ if (Integration.ComputeAsAQuestionOfItsOwn(inU, u, integrateByParts) is not { } inner)
10118+ return null;
10119+ Entity answer = constant * inner.Substitute(u, MathS.Pow(x, n)) / n;
10120+ // (e x)^(n - 1) is e^(n - 1) x^(n - 1) for a positive e, which the answer states.
10121+ if (scale is { })
10122+ {
10123+ answer = MathS.Pow(scale, inFront) * answer;
10124+ if (scale.Evaled is not Number.Real { IsPositive: true })
10125+ answer = answer.Provided(new Greaterf(scale, Number.Integer.Zero));
10126+ }
10127+ return answer;
10128+ }
10129+
10130+ /// <summary>
10131+ /// A square root of a perfect square in <paramref name="x"/> is the modulus:
10132+ /// <c>sqrt(x^2)</c> is <c>|x|</c>, which for a real <c>x</c> is <c>sgn(x) x</c>, and
10133+ /// <c>sqrt(a (x + h)^2)</c> is <c>sqrt(a) sgn(x + h) (x + h)</c> for a positive number
10134+ /// <c>a</c>. Every rule that reads a root of a quadratic assumes it is not one:
10135+ /// <c>1/sqrt(1 + csch(x)^2)</c> under <c>u = tanh(x)</c> is <c>sqrt(u^2)/(u^2 - 1)</c>,
10136+ /// and the table rule for <c>sqrt(a w^2 + b w + c)</c> beside a linear, reached after the
10137+ /// partial fractions and a reciprocal, answered it with a logarithm of zero.
10138+ /// https://github.com/asc-community/AngouriMath/issues/718
10139+ /// </summary>
10140+ /// <remarks>
10141+ /// At every depth, unlike <see cref="SolveByTakingASquareFactorOutOfARoot"/>, since the
10142+ /// misreading is at depth; and the square root only, not <c>(x^2 + 2x + 1)^(5/2)</c>,
10143+ /// which that rule writes as <c>sgn(x + 1) (x + 1)^5</c> at the top. Below a
10144+ /// substitution the variable is one whose sign the substitution knows, and a sign
10145+ /// written for it is a factor the rest of the search has to carry:
10146+ /// <c>tanh(x)/(a + b tanh(x)^2)^(5/2)</c> under <c>u = e^(2x)</c> holds
10147+ /// <c>(1 + 2u + u^2)^(5/2)</c>, and with that rewritten the search ran past its budget
10148+ /// where it had answered in twenty seconds.
10149+ /// </remarks>
10150+ internal static Entity? SolveByTakingARootOfAPerfectSquare(Entity expr, Entity.Variable x, bool integrateByParts)
10151+ {
10152+ // Asked of every integrand at every depth, so the radicand is read as a polynomial
10153+ // only where it is small enough to be a written square: x^2, or a x^2 + b x + c.
10154+ static bool MayBeARootOfASquare(Entity node, Entity.Variable x)
10155+ => node is Powf(var radicand, Number.Rational r) && r.ERational.Equals(ERational.Create(1, 2))
10156+ && radicand.ContainsNode(x) && radicand.Complexity <= 12
10157+ && radicand.Nodes.Count(inner => inner == x) <= 2;
10158+ if (!expr.Nodes.Any(node => MayBeARootOfASquare(node, x)))
10159+ return null;
10160+ var changed = false;
10161+ var written = expr.Replace(node =>
10162+ {
10163+ if (!MayBeARootOfASquare(node, x))
10164+ return node;
10165+ var (radicand, exponent) = (Powf)node;
10166+ var r = (Number.Rational)exponent;
10167+ if (!TreeAnalyzer.TryGetPolynomial(radicand, x, out var monomials) || monomials.Count == 0)
10168+ return node;
10169+ var degree = monomials.Keys.Max()!;
10170+ if (!degree.Equals(EInteger.FromInt32(2)) || monomials.Keys.Any(k => k.Sign < 0))
10171+ return node;
10172+ var a = monomials.TryGetValue(EInteger.FromInt32(2), out var a2) ? a2 : Number.Integer.Zero;
10173+ var b = monomials.TryGetValue(EInteger.One, out var b1) ? b1 : Number.Integer.Zero;
10174+ var c = monomials.TryGetValue(EInteger.Zero, out var c0) ? c0 : Number.Integer.Zero;
10175+ if (a.Evaled is not Number.Real { IsPositive: true } leading || b.Evaled is not Number.Real || c.Evaled is not Number.Real
10176+ || (b * b - Number.Integer.Create(4) * a * c).InnerSimplified.Evaled is not Number.Complex { IsZero: true })
10177+ return node;
10178+ // a (x + h)^2 with h = b/(2a): (a (x + h)^2)^r is a^r |x + h|^(2r), and with 2r odd
10179+ // (a rational is held in lowest terms) that is a^r sgn(x + h) (x + h)^(2r).
10180+ var h = (b / (Number.Integer.Create(2) * a)).InnerSimplified;
10181+ var linear = h == Number.Integer.Zero ? x : (x + h).InnerSimplified;
10182+ var twoR = Number.Integer.Create(r.ERational.Numerator);
10183+ Entity power = twoR == Number.Integer.One ? linear : MathS.Pow(linear, twoR);
10184+ Entity signed = MathS.Signum(linear) * power;
10185+ changed = true;
10186+ return leading == Number.Integer.One ? signed : MathS.Pow(leading, r) * signed;
10187+ });
10188+ return changed ? Integration.ComputeAsTheSameQuestion(written, x, integrateByParts) : null;
10189+ }
10190+
1004410191 /// <summary>
1004510192 /// A whole power of a product in which the variable stands beside a constant, written
1004610193 /// as the product of the powers: <c>(c x)^2</c> is <c>c^2 x^2</c>, exact for a whole
0 commit comments