@@ -10444,7 +10444,10 @@ private static bool IsAPerfectSquareDiscriminant(Entity a, Entity b, Entity c)
1044410444 /// A square root of a perfect square in <paramref name="x"/> is the modulus:
1044510445 /// <c>sqrt(x^2)</c> is <c>|x|</c>, which for a real <c>x</c> is <c>sgn(x) x</c>, and
1044610446 /// <c>sqrt(a (x + h)^2)</c> is <c>sqrt(a) sgn(x + h) (x + h)</c> for a positive number
10447- /// <c>a</c>. Every rule that reads a root of a quadratic assumes it is not one:
10447+ /// <c>a</c>, and a square in a power of <c>x</c> the same way:
10448+ /// <c>sqrt(a^2 + 2 a b x^2 + b^2 x^4)</c> is <c>sqrt(b^2) sgn(x^2 + a/b) (x^2 + a/b)</c>,
10449+ /// the square Rubi's 1.2.2 and 1.2.3 write. Every rule that reads a root of a quadratic
10450+ /// assumes it is not one:
1044810451 /// <c>1/sqrt(1 + csch(x)^2)</c> under <c>u = tanh(x)</c> is <c>sqrt(u^2)/(u^2 - 1)</c>,
1044910452 /// and the table rule for <c>sqrt(a w^2 + b w + c)</c> beside a linear, reached after the
1045010453 /// partial fractions and a reciprocal, answered it with a logarithm of zero.
@@ -10467,7 +10470,8 @@ private static bool IsAPerfectSquareDiscriminant(Entity a, Entity b, Entity c)
1046710470 internal static Entity? SolveByTakingARootOfAPerfectSquare(Entity expr, Entity.Variable x, bool integrateByParts)
1046810471 {
1046910472 // Asked of every integrand at every depth, so the radicand is read as a polynomial
10470- // only where it is small enough to be a written square: x^2, or a x^2 + b x + c.
10473+ // only where it is small enough to be a written square: x^2, a x^2 + b x + c, or
10474+ // the same quadratic in a power of x, a x^(2k) + b x^k + c.
1047110475 // At the top, any half-odd power of a perfect square is the power of the modulus;
1047210476 // below it the square root only, since a sign written for a substitution's variable
1047310477 // is a factor the rest of the search carries -- `(1 + 2u + u^2)^(5/2)` under
@@ -10493,12 +10497,20 @@ bool MayBeARootOfASquare(Entity node, Entity.Variable x)
1049310497 var r = (Number.Rational)exponent;
1049410498 if (!TreeAnalyzer.TryGetPolynomial(radicand, x, out var monomials) || monomials.Count == 0)
1049510499 return node;
10500+ // A quadratic in w = x^k: a w^2 + b w + c, the square being of w + h. Beyond
10501+ // k = 1 only with the constant term written, since `a x^(2k)` alone is a power
10502+ // of a monomial, which another rule distributes.
1049610503 var degree = monomials.Keys.Max()!;
10497- if (!degree.Equals(EInteger.FromInt32(2)) || monomials.Keys.Any(k => k.Sign < 0))
10504+ if (degree.IsZero || !degree.IsEven || monomials.Keys.Any(k => k.Sign < 0))
1049810505 return node;
10499- var a = monomials.TryGetValue(EInteger.FromInt32(2), out var a2) ? a2 : Number.Integer.Zero;
10500- var b = monomials.TryGetValue(EInteger.One, out var b1) ? b1 : Number.Integer.Zero;
10506+ var half = degree / EInteger.FromInt32(2);
10507+ if (monomials.Keys.Any(k => !k.IsZero && !k.Equals(half) && !k.Equals(degree))
10508+ || !half.Equals(EInteger.One) && !monomials.ContainsKey(EInteger.Zero))
10509+ return node;
10510+ var a = monomials[degree];
10511+ var b = monomials.TryGetValue(half, out var b1) ? b1 : Number.Integer.Zero;
1050110512 var c = monomials.TryGetValue(EInteger.Zero, out var c0) ? c0 : Number.Integer.Zero;
10513+ Entity w = half.Equals(EInteger.One) ? x : MathS.Pow(x, Number.Integer.Create(half));
1050210514 // A positive leading coefficient, since `sqrt(a (x + h)^2)` is `sqrt(a) |x + h|`:
1050310515 // a positive number, or one positive for a real parameter -- `b^2` is, and
1050410516 // `a^2 + 2 a b x + b^2 x^2` is the square Rubi writes -- whose condition travels
@@ -10517,17 +10529,22 @@ bool MayBeARootOfASquare(Entity node, Entity.Variable x)
1051710529 if (b.Evaled is Number.Complex and not Number.Real || c.Evaled is Number.Complex and not Number.Real
1051810530 || !IsAPerfectSquareDiscriminant(a, b, c))
1051910531 return node;
10520- // a (x + h)^2 with h = b/(2a): (a (x + h)^2)^r is a^r |x + h|^(2r), and with 2r odd
10521- // (a rational is held in lowest terms) that is a^r sgn(x + h) (x + h)^(2r).
10532+ // a (w + h)^2 with h = b/(2a): (a (w + h)^2)^r is a^r |w + h|^(2r), and with 2r odd
10533+ // (a rational is held in lowest terms) that is a^r sgn(w + h) (w + h)^(2r).
1052210534 var h = (b / (Number.Integer.Create(2) * a)).InnerSimplified;
10523- var linear = h == Number.Integer.Zero ? x : (x + h).InnerSimplified;
10535+ // `2 a b/(2 b^2)` is `a/b`, which the normalisation does not cancel.
10536+ if (h.Vars.Any())
10537+ h = Functions.PartialFractions.Bare(h.Simplify());
10538+ var linear = h == Number.Integer.Zero ? w : (w + h).InnerSimplified;
1052410539 var twoR = Number.Integer.Create(r.ERational.Numerator);
1052510540 Entity power = twoR == Number.Integer.One ? linear : MathS.Pow(linear, twoR);
1052610541 // The sign in front of the integral rather than inside it: it is constant
1052710542 // between the linear factor's zeros, and a rule below that differentiates the
1052810543 // integrand cannot evaluate `derivative(sgn(...))` -- which is the exception
1052910544 // `sqrt(a^2 + 2abx + b^2x^2) sqrt(c + ex + dx^2)` threw with it left in place.
10530- signs = signs * MathS.Signum(linear);
10545+ // None where it is plainly one: an even power of x plus a positive number.
10546+ if (!(half.IsEven && h.Evaled is Number.Real { IsPositive: true }))
10547+ signs = signs * MathS.Signum(linear);
1053110548 changed = true;
1053210549 return leading == Number.Integer.One ? power : MathS.Pow(leading, r) * power;
1053310550 });
0 commit comments