Skip to content

Commit d5f7a40

Browse files
A perfect square in a power of the variable is read as one (#1491)
sqrt(a^2 + 2 a b x^2 + b^2 x^4) is |a + b x^2|, but the rule that reads a root of a perfect square read only a quadratic in x. It now reads the same quadratic in a power of x, a x^(2k) + b x^k + c, with sgn(x^k + h) in front of the integral as for k = 1. There is no sign where k is even and h is a positive number. The shift h is simplified where it holds a symbol. Rubi's 1.2.2.2, 1.2.2.4, 1.2.2.7 and 1.2.3.2, the 574 problems that count with a perfect square written with symbols: 182 to 388, no row lost, 19 timeouts to 9. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f25f632 commit d5f7a40

3 files changed

Lines changed: 81 additions & 9 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,25 @@ multiple of it, `(d - c^2 d x^2)^(3/2)`, is written over it the same way, with
14441444
Rubi's 5.1.4, 5.1.5, 5.2.4 and 5.2.5, all 504 problems that count: 405 to 463, no row lost, 77
14451445
timeouts to 19.
14461446

1447+
### A perfect square in a power of the variable is read as one
1448+
1449+
`sqrt(a^2 + 2 a b x^2 + b^2 x^4) sqrt(c + e x + d x^2)` was left unevaluated. The radicand is
1450+
`(a + b x^2)^2`, so its root is the modulus `|a + b x^2|`, but the rule that reads a root of a
1451+
perfect square read only a quadratic in `x`. It now reads the same quadratic in a power of `x`,
1452+
`a x^(2k) + b x^k + c`, and puts `sgn(x^k + h)` in front of the integral, as it does for `k = 1`.
1453+
There is no sign where `k` is even and `h` is a positive number, since `x^k + h` is then
1454+
positive. The shift `h` is simplified where it holds a symbol: `2 a b/(2 b^2)` is `a/b`, for
1455+
`k = 1` as well.
1456+
1457+
| Input | Was (2.5.0) | Now |
1458+
|---|---|---|
1459+
| `"sqrt(c+pe*x+d*x^2)*sqrt(a^2+2*a*b*x^2+b^2*x^4)".Integrate("x")` | left unevaluated | the antiderivative, with `sgn(x^2 + a/b) sqrt(b^2)` in front |
1460+
| `"x/sqrt(a^2+2*a*b*x^3+b^2*x^6)".Integrate("x")` | left unevaluated | logarithms and an arctangent in `(a/b)^(1/3)`, with `sgn(x^3 + a/b)` in front |
1461+
| `"(1+2*x^2+x^4)^(3/2)".Integrate("x")` | left unevaluated | `x + 3 x^3/3 + 3 x^5/5 + x^7/7`, with no sign |
1462+
1463+
Rubi's 1.2.2.2, 1.2.2.4, 1.2.2.7 and 1.2.3.2, the 574 problems that count with a perfect square
1464+
written with symbols: 182 to 388, no row lost, 19 timeouts to 9.
1465+
14471466
### `binomial(n, k)` is a function
14481467

14491468
**Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled

‎Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
});

‎Sources/Tests/UnitTests/Calculus/RootOfAPerfectSquareIntegralTest.cs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,42 @@ Entity Pin(Entity e) => e.Substitute("a", 0.9).Substitute("b", 1.7).Substitute("
9292
Assert.True(compared >= 5, $"only {compared} points were comparable for {integrand}");
9393
}
9494

95+
/// <summary>
96+
/// The same square in a power of the variable: <c>a^2 + 2 a b x^2 + b^2 x^4</c> is
97+
/// <c>(a + b x^2)^2</c>, and its root is <c>sqrt(b^2) sgn(x^2 + a/b) (x^2 + a/b)</c>. Pinned
98+
/// with <c>a</c> and <c>b</c> of opposite signs, so that the sign changes among the points.
99+
/// Rubi's 1.2.2.7 and 1.2.3.2.
100+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
101+
/// </summary>
102+
[Theory]
103+
[InlineData("sqrt(c + pe*x + d*x^2)*sqrt(a^2 + 2*a*b*x^2 + b^2*x^4)")]
104+
[InlineData("x*sqrt(c + pe*x + d*x^2)*sqrt(a^2 + 2*a*b*x^2 + b^2*x^4)")]
105+
[InlineData("x/sqrt(a^2 + 2*a*b*x^3 + b^2*x^6)")]
106+
public void ASquareInAPowerOfTheVariable(string integrand)
107+
{
108+
var integral = integrand.ToEntity().Integrate("x").Substitute("C", 0);
109+
Assert.DoesNotContain("integral(", integral.Stringize());
110+
Assert.DoesNotContain("NaN", integral.Stringize());
111+
Entity Pin(Entity e) => e.Substitute("a", -0.9).Substitute("b", 1.7).Substitute("c", 2.1)
112+
.Substitute("pe", 0.3).Substitute("d", 1.3);
113+
var derivative = Pin(integral).Differentiate("x");
114+
var original = Pin(integrand.ToEntity());
115+
var compared = 0;
116+
foreach (var at in Points)
117+
{
118+
var got = derivative.Substitute("x", at).EvalNumerical();
119+
var want = original.Substitute("x", at).EvalNumerical();
120+
if (got.IsNaN || want.IsNaN)
121+
continue;
122+
compared++;
123+
var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart);
124+
var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart));
125+
Assert.True(difference / scale < 1e-9,
126+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
127+
}
128+
Assert.True(compared >= 5, $"only {compared} points were comparable for {integrand}");
129+
}
130+
95131
[Fact]
96132
public void TheSignIsTheSignOfTheLinearFactor()
97133
{

0 commit comments

Comments
 (0)