Skip to content

Commit 2d3dc6a

Browse files
A quotient in x^2 over a power of x and a biquadratic is split in x^2 (#1806)
Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 0630504 commit 2d3dc6a

3 files changed

Lines changed: 146 additions & 23 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,24 @@ quotient of two such linears the sum is `(b - d t^2)^2 + (c t^2 - a)^2`. Rubi's
336336
| `"(c + d*tan(x))^(3/2)/(a + b*tan(x))^3".ToEntity().Integrate("x")` | `integral(...)` | the same |
337337
| `"1/((a + b*tan(x))^(3/2)*(c + d*tan(x))^(3/2))".ToEntity().Integrate("x")` | `integral(...)`; past a minute on the unreleased master | in the root of the quotient of the two, in a second |
338338

339+
### A quotient in `x^2` over a power of `x` and a biquadratic is split in `x^2`
340+
341+
**Shorter answers, sooner.** `cot(x)^(13/2) (a + b tan(x))^(5/2) (A + B tan(x))` was declined in 2.5.0
342+
and answered in a million characters after eighteen seconds since; it is answered in two thousand,
343+
in under a second. Under `u = tan(x)` and the root of the quotient `t = sqrt(u/(a + b u))`, Rubi's
344+
half-odd powers of the cotangent beside one of `a + b tan(x)` leave a polynomial in `t^2` over
345+
`t^(2k) Q(t^2)`, `Q` a biquadratic, which was split in `t`: the repeated `t^(2k)` beside the quartic
346+
went to the conjugates of a sum of two squares, at length, or, with the power of a product
347+
distributed, to the Hermite reduction, which declined it after seconds. With a power of `x` on both
348+
sides cancelled, such a quotient is split in `w = x^2` now: the terms in `w^(-j)` are the expansion
349+
of the remainder over `Q` at `w = 0`, and what is left is `(d + e w)/Q`, which the biquadratic's rule
350+
answers by arctangents ([#718](https://github.com/asc-community/AngouriMath/issues/718)).
351+
352+
| Input | Was (2.5.0) | Now |
353+
|---|---|---|
354+
| `"cot(x)^(13/2)*(a + b*tan(x))^(5/2)*(A + B*tan(x))".ToEntity().Integrate("x")` | `integral(...)`; 1,018,377 characters after 18 seconds on the unreleased master | 2,305 characters, in under a second |
355+
| `"cot(x)^(5/2)/(a + b*tan(x))^(3/2)".ToEntity().Integrate("x")` | `integral(...)`; 4,779 characters after 6 seconds on the unreleased master | 955 characters |
356+
339357
### A function of `x^n` for a symbolic `n` beside a power of `x` is integrated in a power of `x`
340358

341359
**Answers where there were none.** `x^(-1 + 4n)/(a + b x^n + c x^(2n))` and the rest of Rubi's

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

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,7 +2422,15 @@ over is Entity.Powf(var @base, var power) ?
24222422
/// <c>(d + e r_1)/(q (x^2 - r_1)) - (d + e r_2)/(q (x^2 - r_2))</c>, and
24232423
/// <c>1/(x^2 - r)</c> is <c>atan(x/sqrt(-r))/sqrt(-r)</c> for any complex <c>r</c>. A
24242424
/// higher even degree is divided down first, and an odd numerator is left to the
2425-
/// substitution <c>u = x^2</c>.
2425+
/// substitution <c>u = x^2</c>. And over <c>x^(2k)</c> times the biquadratic, a power of
2426+
/// <c>x</c> on both sides cancelled first: in <c>w = x^2</c> the remainder <c>R</c> over
2427+
/// <c>w^k Q</c> is the expansion of <c>R/Q</c> at <c>w = 0</c> up to <c>w^(k-1)</c>, over
2428+
/// <c>w^k</c>, plus <c>(d + e w)/Q</c>, so the terms in <c>x^(-2j)</c> go by the power
2429+
/// rule and the rest as above. Split in <c>x</c>, the repeated factor <c>x^(2k)</c> beside
2430+
/// the quartic went to the Hermite reduction or to the conjugates of a sum of two squares,
2431+
/// and <c>2 a (1 - b t^2)^4/(t^4 (1 - 2 b t^2 + (a^2 + b^2) t^4))</c>, which the root of the
2432+
/// quotient makes of Rubi's <c>cot(c + d x)^(5/2)/(a + b tan(c + d x))^(3/2)</c>, was
2433+
/// declined after seconds in one spelling and answered at length in the other.
24262434
/// </summary>
24272435
/// <remarks>
24282436
/// <para>
@@ -2441,46 +2449,76 @@ over is Entity.Powf(var @base, var power) ?
24412449
{
24422450
if (!TryReadAsQuotient(expr, out var numerator, out var denominator))
24432451
return null;
2444-
if (!TreeAnalyzer.TryGetPolynomial(denominator, x, out var below) || below.Count == 0
2445-
|| !below.ContainsKey(EInteger.FromInt32(4)) || below.Keys.Any(power => !(power.IsZero || power.Equals(EInteger.FromInt32(2)) || power.Equals(EInteger.FromInt32(4))))
2446-
|| below.Values.Any(coefficient => coefficient.ContainsNode(x))
2452+
if (!TreeAnalyzer.TryGetPolynomial(denominator, x, out var belowAsWritten) || belowAsWritten.Count == 0
2453+
|| belowAsWritten.Keys.Any(power => power.Sign < 0 || !power.CanFitInInt32())
2454+
|| belowAsWritten.Values.Any(coefficient => coefficient.ContainsNode(x))
24472455
|| !denominator.Vars.Any(v => v != x))
24482456
return null;
2449-
var a = below.TryGetValue(EInteger.Zero, out var a0) ? a0 : Number.Integer.Zero;
2450-
var b = below.TryGetValue(EInteger.FromInt32(2), out var b0) ? b0 : Number.Integer.Zero;
2451-
var c = below[EInteger.FromInt32(4)];
2457+
if (!TreeAnalyzer.TryGetPolynomial(numerator, x, out var aboveAsWritten) || aboveAsWritten.Count == 0
2458+
|| aboveAsWritten.Keys.Any(power => power.Sign < 0 || !power.CanFitInInt32())
2459+
|| aboveAsWritten.Values.Any(coefficient => coefficient.ContainsNode(x)))
2460+
return null;
2461+
// A power of x on both sides is cancelled first, and what is left below is x^(2k) times the
2462+
// biquadratic: `2 a (1 - b t^2)^4 t/(t^5 ((1 - b t^2)^2 + a^2 t^4))` is how the root of
2463+
// a quotient writes Rubi's `cot(c + d x)^(5/2)/(a + b tan(c + d x))^(3/2)`.
2464+
var common = EInteger.Min(aboveAsWritten.Keys.Min()!, belowAsWritten.Keys.Min()!).ToInt32Unchecked();
2465+
var below = belowAsWritten.ToDictionary(pair => pair.Key.ToInt32Unchecked() - common, pair => pair.Value);
2466+
var above = aboveAsWritten.ToDictionary(pair => pair.Key.ToInt32Unchecked() - common, pair => pair.Value);
2467+
var lowest = below.Keys.Min();
2468+
if (lowest % 2 != 0 || !below.ContainsKey(lowest + 4)
2469+
|| below.Keys.Any(power => power != lowest && power != lowest + 2 && power != lowest + 4)
2470+
|| above.Keys.Any(power => power % 2 != 0))
2471+
return null;
2472+
var k = lowest / 2;
2473+
var a = below[lowest];
2474+
var b = below.TryGetValue(lowest + 2, out var b0) ? b0 : Number.Integer.Zero;
2475+
var c = below[lowest + 4];
24522476
// Zero as a value, and not only as written: the coefficients are read off the
24532477
// denominator as it is written, and `((c - u^2)/c - 1)(u^2 - c)` is `-u^2 (u^2 - c)/c`,
24542478
// with `-(c/c - 1) c` for its constant term. Read as a biquadratic, one of its roots
24552479
// in `u^2` was that zero, and `atan(u/sqrt(0))/sqrt(0)` made the answer NaN.
24562480
// https://github.com/asc-community/AngouriMath/issues/1665
24572481
if (Functions.PartialFractions.IsZeroAsAValue(a) || Functions.PartialFractions.IsZeroAsAValue(c))
24582482
return null;
2459-
if (!TreeAnalyzer.TryGetPolynomial(numerator, x, out var above) || above.Count == 0
2460-
|| above.Keys.Any(power => power.Sign < 0 || !power.IsEven) || above.Values.Any(coefficient => coefficient.ContainsNode(x)))
2461-
return null;
24622483

24632484
var discriminant = Functions.PartialFractions.Bare((b * b - 4 * a * c).Simplify());
24642485
if (Functions.PartialFractions.IsZeroAsAValue(discriminant))
24652486
return null;
24662487

2467-
// The numerator in w = x^2, divided down by c w^2 + b w + a to a remainder d + e w.
2468-
var degree = above.Keys.Max()!.ToInt32Checked() / 2;
2469-
var inW = new Entity[degree + 1];
2470-
for (var k = 0; k <= degree; k++)
2471-
inW[k] = above.TryGetValue(EInteger.FromInt32(2 * k), out var at) ? at : Number.Integer.Zero;
2488+
// The numerator in w = x^2, divided down by w^k (c w^2 + b w + a) to a remainder of a
2489+
// degree below k + 2.
2490+
var degree = above.Keys.Max() / 2;
2491+
var inW = new Entity[System.Math.Max(degree, k + 1) + 1];
2492+
for (var j = 0; j < inW.Length; j++)
2493+
inW[j] = above.TryGetValue(2 * j, out var at) ? at : Number.Integer.Zero;
24722494
Entity polynomialPart = Number.Integer.Zero;
2473-
for (var k = degree; k >= 2; k--)
2495+
for (var j = degree; j >= k + 2; j--)
24742496
{
2475-
var lead = Functions.PartialFractions.InLowestTermsOverTheSymbols(inW[k] / c);
2497+
var lead = Functions.PartialFractions.InLowestTermsOverTheSymbols(inW[j] / c);
24762498
if (lead == Number.Integer.Zero)
24772499
continue;
2478-
polynomialPart += lead * MathS.Pow(x, 2 * (k - 2));
2479-
inW[k - 1] = Functions.PartialFractions.InLowestTermsOverTheSymbols(inW[k - 1] - lead * b);
2480-
inW[k - 2] = Functions.PartialFractions.InLowestTermsOverTheSymbols(inW[k - 2] - lead * a);
2481-
}
2482-
var d = inW[0];
2483-
var e = degree >= 1 ? inW[1] : Number.Integer.Zero;
2500+
polynomialPart += lead * MathS.Pow(x, 2 * (j - k - 2));
2501+
inW[j - 1] = Functions.PartialFractions.InLowestTermsOverTheSymbols(inW[j - 1] - lead * b);
2502+
inW[j - 2] = Functions.PartialFractions.InLowestTermsOverTheSymbols(inW[j - 2] - lead * a);
2503+
}
2504+
// The remainder R over w^k Q is sum r_m w^(m - k) over m < k, the expansion of R/Q at
2505+
// w = 0, plus (d + e w)/Q: r_m = (R_m - b r_(m-1) - c r_(m-2))/a, and d + e w is what
2506+
// (R - Q sum r_m w^m)/w^k leaves, every lower coefficient cancelling by the recurrence.
2507+
var pole = new Entity[k];
2508+
for (var m = 0; m < k; m++)
2509+
{
2510+
var term = inW[m];
2511+
if (m >= 1) term -= b * pole[m - 1];
2512+
if (m >= 2) term -= c * pole[m - 2];
2513+
pole[m] = Functions.PartialFractions.InLowestTermsOverTheSymbols(term / a);
2514+
}
2515+
var dTerm = inW[k];
2516+
if (k >= 1) dTerm -= b * pole[k - 1];
2517+
if (k >= 2) dTerm -= c * pole[k - 2];
2518+
var eTerm = inW[k + 1];
2519+
if (k >= 1) eTerm -= c * pole[k - 1];
2520+
var d = k == 0 ? inW[0] : Functions.PartialFractions.InLowestTermsOverTheSymbols(dTerm);
2521+
var e = k == 0 ? (degree >= 1 ? inW[1] : Number.Integer.Zero) : Functions.PartialFractions.InLowestTermsOverTheSymbols(eTerm);
24842522

24852523
var q = MathS.Sqrt(discriminant);
24862524
var firstRoot = (-b + q) / (2 * c);
@@ -2492,6 +2530,14 @@ over is Entity.Powf(var @base, var power) ?
24922530
return null;
24932531
total += whole;
24942532
}
2533+
// r_m x^(2m - 2k), whose exponent is odd and so never minus one.
2534+
for (var m = 0; m < k; m++)
2535+
{
2536+
if (pole[m] == Number.Integer.Zero)
2537+
continue;
2538+
var exponent = 2 * (m - k) + 1;
2539+
total += pole[m] * MathS.Pow(x, exponent) / exponent;
2540+
}
24952541
// `1/(x^2 - r)` is `atan(x/s)/s` with `s = sqrt(-r)` for every complex `r` but zero:
24962542
// `d/dx atan(x/s)/s` is `1/(s^2 + x^2)` whatever `s` is, and for a positive `r` the
24972543
// arctangent of an imaginary argument is the hyperbolic one, `-atanh(x/sqrt(r))/sqrt(r)`,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 in <c>x^2</c> over <c>x^(2k)</c> times a biquadratic with a symbol in it, a power
16+
/// of <c>x</c> on both sides cancelled first, split in <c>w = x^2</c>: the terms in
17+
/// <c>w^(-j)</c> are the expansion of the remainder over the biquadratic at <c>w = 0</c>, and
18+
/// what is left is the biquadratic's own. A half-odd power of the cotangent beside one of
19+
/// <c>a + b tan(x)</c> comes to one under <c>u = tan(x)</c> and the root of the quotient
20+
/// <c>t = sqrt(u/(a + b u))</c>; split in <c>t</c>, the answer to the last row ran to a million
21+
/// characters. Rubi's 4.3.2.1 and 4.3.3.1.
22+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
23+
/// </summary>
24+
/// <remarks>
25+
/// The cotangent's rows are real where <c>tan(x)</c> and <c>a + b tan(x)</c> are positive,
26+
/// which the points are. The pinned answer is differentiated as it is, since simplifying
27+
/// answers of this shape costs minutes.
28+
/// </remarks>
29+
[Trait("Area", "Calculus")]
30+
public sealed class EvenQuotientOverAPowerOfXAndABiquadraticIntegralTest
31+
{
32+
[Theory]
33+
[InlineData("1/(x^2*(a + b*x^2 + c*x^4))")]
34+
[InlineData("(1 + x^10)/(x^2*(a + b*x^2 + c*x^4))")]
35+
[InlineData("2*a*(1 - b*x^2)^4*x/(x^5*(1 - 2*b*x^2 + (a^2 + b^2)*x^4))")]
36+
[InlineData("cot(x)^(5/2)/(a + b*tan(x))^(3/2)")]
37+
[InlineData("cot(x)^(9/2)*sqrt(a + b*tan(x))*(A + B*tan(x))")]
38+
[InlineData("cot(x)^(13/2)*(a + b*tan(x))^(5/2)*(A + B*tan(x))")]
39+
public void SplitInTheSquare(string integrand)
40+
{
41+
var integral = integrand.ToEntity().Integrate("x");
42+
var text = integral.Stringize();
43+
Assert.DoesNotContain("integral(", text);
44+
Assert.True(text.Length < 5000, $"{text.Length} characters of answer for {integrand}");
45+
Entity Pinned(Entity e) => e.Substitute("a", 1.3).Substitute("b", 0.7).Substitute("c", 2.9)
46+
.Substitute("A", 0.4).Substitute("B", 1.1);
47+
var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x");
48+
var original = Pinned(integrand.ToEntity());
49+
foreach (var at in new[] { 0.2, 0.5, 0.9, 1.2 })
50+
{
51+
var want = original.Substitute("x", at).EvalNumerical();
52+
var got = derivative.Substitute("x", at).EvalNumerical();
53+
Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart)
54+
< 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart)),
55+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
56+
}
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)