Skip to content

Commit 879fb2d

Browse files
A polynomial over a power of a trinomial past the square is taken down a power at a time (#1703)
x^4/(a + b x^2 + c x^4)^3 was declined: the Hermite reduction answers the square of a trinomial with symbols in it in one linear solve, and its system grows past what it takes at the cube. With w = x^n, T = a + b w + c w^2 and D = 4 a c - b^2, the derivative of x^(m+1) (beta0 + beta1 w) T^(p+1) is x^m T^p plus multiples of x^m and x^(m+n) over T^-(p+1), with beta0 = (b^2 - 2 a c)/(a n (p + 1) D), beta1 = b c/(a n (p + 1) D), delta0 = -((2 a c - b^2)(m + 1) + n (p + 1) D)/(a n (p + 1) D) and delta1 = b c (m + 2 n p + 3 n + 1)/(a n (p + 1) D). So each monomial goes down a power at a time as two monomials a power up, and what reaches the first power is gathered into one polynomial over T and integrated once. After the binomial's recurrence (#1699), for n >= 2. Part of #718. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 148f300 commit 879fb2d

3 files changed

Lines changed: 200 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,21 @@ square and the cube keep the Hermite reduction's answers. Rubi's 1.1.3.8 `P(x) (
276276
| `"(c + d*x)/(a + b*x^3)^4".ToEntity().Integrate("x")` | `integral(...)` | the same at the roots of `a + b x^3` |
277277
| `"x^2/(a + b*x^5)^5".ToEntity().Integrate("x")` | `integral(...)` | four algebraic terms, and the integral of `x^2/(a + b x^5)` |
278278

279+
### A polynomial over a power of a trinomial past the square is integrated
280+
281+
`P(x)/(a + b x^n + c x^(2n))^k`, a polynomial over a power of a trinomial in `x^n` with symbols in
282+
it, was left unevaluated past the square, which the Hermite reduction answers in one linear solve.
283+
Now each monomial is taken down a power at a time by the trinomial's recurrence, two monomials a
284+
power up each step, and what reaches the first power is integrated once over the trinomial. Rubi's
285+
1.2.2.2 and 1.2.2.4 have these by the dozen
286+
([#718](https://github.com/asc-community/AngouriMath/issues/718)).
287+
288+
| Input | Was (2.5.0) | Now |
289+
|---|---|---|
290+
| `"x^4/(a + b*x^2 + c*x^4)^3".ToEntity().Integrate("x")` | `integral(...)` | odd polynomials over `(a + b x^2 + c x^4)^2` and over it, and arctangents at its roots |
291+
| `"1/(a + b*x^2 + c*x^4)^3".ToEntity().Integrate("x")` | `integral(...)` | the same |
292+
| `"x/(a + b*x^3 + c*x^6)^2".ToEntity().Integrate("x")` | `integral(...)` | a polynomial over `a + b x^3 + c x^6`, and the integral of a polynomial over it |
293+
279294
### A symbolic parameter no longer stops a rational integrand being integrated
280295

281296
`1/(8 + x^3)` and `1/(16 - x^4)` are answered at once. `1/(a^3 + x^3)` and `1/(a^4 - x^4)` were not,

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

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,11 @@ is var (multiple, leftover)
760760
if (IntegrateAPolynomialOverAPowerOfABinomial(numerator, denominator, x, integrateByParts) is { } downThePowers)
761761
return downThePowers;
762762

763+
// And of a trinomial in x^n, `x^4/(a + b x^2 + c x^4)^3`: its recurrence, a power at a
764+
// time, down to the trinomial itself.
765+
if (IntegrateAPolynomialOverAPowerOfATrinomial(numerator, denominator, x, integrateByParts) is { } downTheTrinomialsPowers)
766+
return downTheTrinomialsPowers;
767+
763768
// Splitting into coprime blocks comes before peeling one root off, and the order is
764769
// load-bearing rather than a preference.
765770
//
@@ -1219,6 +1224,119 @@ Entity InU(Dictionary<EInteger, Entity> polynomial)
12191224
/// </summary>
12201225
private const int MaximumPowerOfABinomial = 12;
12211226

1227+
/// <summary>
1228+
/// A polynomial over a power of a trinomial in <c>x^n</c>,
1229+
/// <c>P(x)/(a + b x^n + c x^(2n))^k</c> with <c>n &gt;= 2</c> and <c>k &gt;= 2</c>, a power at
1230+
/// a time down to a polynomial over the trinomial itself.
1231+
/// </summary>
1232+
/// <remarks>
1233+
/// <para>
1234+
/// For a monomial, with <c>w = x^n</c>, <c>T = a + b w + c w^2</c> and <c>D = 4 a c - b^2</c>,
1235+
/// the derivative of <c>x^(m+1) (beta0 + beta1 w) T^(p+1)</c> is
1236+
/// <c>x^m T^p + delta0 x^m T^(p+1) + delta1 x^(m+n) T^(p+1)</c>, where
1237+
/// <code>
1238+
/// beta0 = (b^2 - 2 a c)/(a n (p + 1) D), beta1 = b c/(a n (p + 1) D),
1239+
/// delta0 = -((2 a c - b^2)(m + 1) + n (p + 1) D)/(a n (p + 1) D),
1240+
/// delta1 = b c (m + 2 n p + 3 n + 1)/(a n (p + 1) D),
1241+
/// </code>
1242+
/// which is what matching the powers of <c>w</c> in
1243+
/// <c>(m + 1)(beta0 + beta1 w) T + n beta1 w T + (p + 1) n w (b + 2 c w)(beta0 + beta1 w)</c>
1244+
/// against <c>1 + (delta0 + delta1 w) T</c> gives. So <c>int x^m T^p</c> is that algebraic
1245+
/// term, less <c>delta0 int x^m T^(p+1)</c> and <c>delta1 int x^(m+n) T^(p+1)</c>: two
1246+
/// monomials a power up. What reaches the first power is gathered into one polynomial
1247+
/// over <c>T</c> and integrated once.
1248+
/// </para>
1249+
/// <para>
1250+
/// After the Hermite reduction, which answers the square with symbols in it in one linear
1251+
/// solve and keeps those answers; its system grows with the degree, and
1252+
/// <c>x^4/(a + b x^2 + c x^4)^3</c> was declined. Rubi's 1.2.2.2 and 1.2.2.4 have these by the
1253+
/// dozen. <c>a</c> and <c>D</c> are divided by, which is the generic case, as everywhere in
1254+
/// the integrator: <c>D = 0</c> is a square, which the rules for one take.
1255+
/// https://github.com/asc-community/AngouriMath/issues/718
1256+
/// </para>
1257+
/// </remarks>
1258+
private static Entity? IntegrateAPolynomialOverAPowerOfATrinomial(Entity numerator, Entity denominator, Entity.Variable x, bool integrateByParts)
1259+
{
1260+
Entity constant = Number.Integer.One;
1261+
Entity? trinomial = null;
1262+
var power = 0;
1263+
foreach (var factor in Mulf.LinearChildren(denominator))
1264+
{
1265+
if (!factor.ContainsNode(x))
1266+
constant = constant == Number.Integer.One ? factor : constant * factor;
1267+
else if (trinomial is null && factor is Powf(var @base, Number.Integer { EInteger: var e }) && e.CompareTo(EInteger.FromInt32(2)) >= 0 && e.CanFitInInt32())
1268+
{
1269+
trinomial = @base;
1270+
power = e.ToInt32Unchecked();
1271+
}
1272+
else
1273+
return null;
1274+
}
1275+
if (trinomial is null || power > MaximumPowerOfABinomial
1276+
|| !TreeAnalyzer.TryGetPolynomial(trinomial, x, out var below) || below.Count != 3
1277+
|| below.Keys.Any(degree => !degree.CanFitInInt32()) || below.Values.Any(coefficient => coefficient.ContainsNode(x))
1278+
|| !TreeAnalyzer.TryGetPolynomial(numerator, x, out var above)
1279+
|| above.Keys.Any(degree => degree.Sign < 0 || !degree.CanFitInInt32())
1280+
|| above.Values.Any(coefficient => coefficient.ContainsNode(x)))
1281+
return null;
1282+
var degrees = below.Keys.Select(degree => degree.ToInt32Unchecked()).OrderBy(degree => degree).ToArray();
1283+
var n = degrees[1];
1284+
if (degrees[0] != 0 || n < 2 || degrees[2] != 2 * n)
1285+
return null;
1286+
var (a, b, c) = (below[EInteger.Zero], below[EInteger.FromInt32(n)], below[EInteger.FromInt32(2 * n)]);
1287+
var discriminant = 4 * a * c - b * b;
1288+
if (VanishesIdentically(a) || VanishesIdentically(c) || VanishesIdentically(b) || VanishesIdentically(discriminant))
1289+
return null;
1290+
1291+
// Each monomial down the powers: the algebraic part over T^(j - 1) gathered by j, and
1292+
// what is left at the first power gathered by the monomial's degree.
1293+
var overPowers = new Dictionary<int, Entity>();
1294+
var atThisPower = new Dictionary<int, Entity>();
1295+
foreach (var pair in above)
1296+
atThisPower[pair.Key.ToInt32Unchecked()] = pair.Value;
1297+
for (var j = power; j >= 2; j--)
1298+
{
1299+
var p = -j;
1300+
var over = a * n * (p + 1) * discriminant;
1301+
var beta0 = Functions.PartialFractions.InLowestTermsOverTheSymbols((b * b - 2 * a * c) / over);
1302+
var beta1 = Functions.PartialFractions.InLowestTermsOverTheSymbols(b * c / over);
1303+
var atTheNext = new Dictionary<int, Entity>();
1304+
foreach (var pair in atThisPower)
1305+
{
1306+
var (m, coefficient) = (pair.Key, pair.Value);
1307+
if (VanishesIdentically(coefficient))
1308+
continue;
1309+
var term = Functions.PartialFractions.InLowestTermsOverTheSymbols(coefficient * beta0) * MathS.Pow(x, m + 1)
1310+
+ Functions.PartialFractions.InLowestTermsOverTheSymbols(coefficient * beta1) * MathS.Pow(x, m + 1 + n);
1311+
overPowers[j - 1] = overPowers.TryGetValue(j - 1, out var so) ? so + term : term;
1312+
var delta0 = -((2 * a * c - b * b) * (m + 1) + n * (p + 1) * discriminant) / over;
1313+
var delta1 = b * c * (m + 2 * n * p + 3 * n + 1) / over;
1314+
foreach (var (degree, times) in new[] { (m, delta0), (m + n, delta1) })
1315+
{
1316+
var next = Functions.PartialFractions.InLowestTermsOverTheSymbols(-coefficient * times);
1317+
atTheNext[degree] = atTheNext.TryGetValue(degree, out var already)
1318+
? Functions.PartialFractions.InLowestTermsOverTheSymbols(already + next) : next;
1319+
}
1320+
}
1321+
atThisPower = atTheNext;
1322+
}
1323+
1324+
Entity answer = Number.Integer.Zero;
1325+
foreach (var pair in overPowers.OrderByDescending(pair => pair.Key))
1326+
answer = answer + pair.Value / (pair.Key == 1 ? trinomial : MathS.Pow(trinomial, pair.Key));
1327+
Entity rest = Number.Integer.Zero;
1328+
foreach (var pair in atThisPower.OrderBy(pair => pair.Key))
1329+
if (!VanishesIdentically(pair.Value))
1330+
rest = rest + pair.Value * (pair.Key == 0 ? Number.Integer.One : pair.Key == 1 ? x : MathS.Pow(x, pair.Key));
1331+
if (rest != Number.Integer.Zero)
1332+
{
1333+
if (Integration.ComputeIndefiniteIntegral(rest / trinomial, x, integrateByParts) is not { } overTheTrinomial)
1334+
return null;
1335+
answer = answer + overTheTrinomial;
1336+
}
1337+
return (constant == Number.Integer.One ? answer : answer / constant).InnerSimplified;
1338+
}
1339+
12221340
/// <summary>
12231341
/// A polynomial over a binomial <c>a x^n + b</c>, <c>n >= 3</c>, decomposed at the
12241342
/// <c>n</c>-th roots of <c>-b/a</c> and integrated term by term in closed form.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using System;
9+
using AngouriMath.Extensions;
10+
using Xunit;
11+
12+
namespace AngouriMath.Tests.Calculus
13+
{
14+
/// <summary>
15+
/// A polynomial over a power of a trinomial <c>a + b x^n + c x^(2n)</c> with symbols in it,
16+
/// taken down a power at a time by its recurrence to the trinomial itself.
17+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
18+
/// </summary>
19+
/// <remarks>
20+
/// Rubi's 1.2.2.2 <c>x^4/(a + b x^2 + c x^4)^3</c> and the like: the Hermite reduction answers
21+
/// the square, and its system grew past what it takes at the cube. Differentiated back on
22+
/// both sides of <c>4 a c - b^2</c>, since the roots the last step writes are complex for one.
23+
/// </remarks>
24+
[Trait("Area", "Calculus")]
25+
public sealed class PowerOfATrinomialIntegralTest
26+
{
27+
private static readonly double[] Points = { 0.3, 0.9, 1.7, 2.6 };
28+
29+
private static void DifferentiatesBack(string integrand, params (string, double)[] pins)
30+
{
31+
var integral = integrand.ToEntity().Integrate("x");
32+
Assert.DoesNotContain("integral(", integral.Stringize());
33+
var derivative = integral.Substitute("C", 0).Differentiate("x");
34+
Entity original = integrand.ToEntity();
35+
foreach (var (name, value) in pins)
36+
{
37+
derivative = derivative.Substitute(name, value);
38+
original = original.Substitute(name, value);
39+
}
40+
var compared = 0;
41+
foreach (var at in Points)
42+
{
43+
var got = derivative.Substitute("x", at).EvalNumerical();
44+
var want = original.Substitute("x", at).EvalNumerical();
45+
if (got.IsNaN || want.IsNaN)
46+
continue;
47+
compared++;
48+
var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart);
49+
var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart));
50+
Assert.True(difference / scale < 1e-9,
51+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
52+
}
53+
Assert.True(compared >= 3, $"only {compared} points could be compared for {integrand}");
54+
}
55+
56+
/// <summary>The cube of a trinomial in x^2, and the square of one in x^3.</summary>
57+
[Theory]
58+
[InlineData("x^4/(a + b*x^2 + c*x^4)^3", 0.7)]
59+
[InlineData("1/(a + b*x^2 + c*x^4)^3", 0.7)]
60+
[InlineData("(d + k*x^2)/(a + b*x^2 + c*x^4)^3", 0.7)]
61+
[InlineData("x/(a + b*x^3 + c*x^6)^2", 0.7)]
62+
[InlineData("x^4/(a + b*x^2 + c*x^4)^3", 3.1)]
63+
[InlineData("(d + k*x^2)/(a + b*x^2 + c*x^4)^3", 3.1)]
64+
public void PastTheSquare(string integrand, double b)
65+
=> DifferentiatesBack(integrand, ("a", 1.3), ("b", b), ("c", 0.9), ("d", 0.4), ("k", 1.1));
66+
}
67+
}

0 commit comments

Comments
 (0)