Skip to content

Commit 9588be9

Browse files
A whole power of a quotient with a symbol in it is integrated as the quotient of the powers (#1736)
(c/(a + c x^2))^2 was declined after twenty-six seconds while c^2/(a + c x^2)^2 was answered at once: the table reads the quotient of the powers. It is written so wherever the quotient is of two polynomials with a symbol in them and leaves a monomial above the bar, the table's x^m/(a + b x^n)^p, which is exact for a whole power. The substitution u = x^2 writes x/(a + c x^4)^2 that way, and it took twenty seconds where it takes a twentieth of one now. 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 ea200c5 commit 9588be9

4 files changed

Lines changed: 113 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,25 @@ Rubi's `x^m (a + b x^n)^p` and `(a + b x^n)^p (c + d x^n)^q`
232232
| `"1/(2+3/x^2)^3".Integrate("x")`, `1/(a + b/x^3)` | left unevaluated | an antiderivative |
233233
| `"(a+b*x^n)*(c+d*x^n)^3".Integrate("x")` | left unevaluated | written out, eight powers of `x` integrated: `a c^3 x + ... + b d^3 x^(4 n + 1)/(4 n + 1)` |
234234

235+
### A whole power of a quotient with a symbol in it is integrated as the quotient of the powers
236+
237+
**Answers where there were none, and a slowdown since 2.5.0 undone.** `(c/(a + c x^2))^2` was
238+
declined, after twenty-six seconds, while `c^2/(a + c x^2)^2`, the same function, was answered at
239+
once: the table reads the quotient of the powers and nothing read the power of the quotient. It is
240+
written so now wherever the quotient is of two polynomials in `x` with a symbol in them and what
241+
it leaves above the bar is a monomial, the table's `x^m/(a + b x^n)^p`; that is exact for a whole
242+
power. That is
243+
also how the substitution `u = x^2` writes `x/(a + c x^4)^2` since 2.5.0, having simplified
244+
`1/(a/c + u^2)^2`, and that integrand took twenty seconds where 2.5.0 took a tenth of one
245+
([#718](https://github.com/asc-community/AngouriMath/issues/718)).
246+
247+
| Input | Was (2.5.0) | Now |
248+
|---|---|---|
249+
| `"(c/(a + c*x^2))^2".ToEntity().Integrate("x")` | `integral(...)`, after a minute | a quotient and an arctangent or a logarithm, by the sign of `a c` |
250+
| `"(1/(a + c*x^2))^2".ToEntity().Integrate("x")` | `integral(...)` | the same |
251+
| `"(x/(a + c*x^2))^2".ToEntity().Integrate("x")` | `integral(...)` | the same |
252+
| `"x/(a + c*x^4)^2".ToEntity().Integrate("x")` | the answer, in 0.1 s; 22 s on the unreleased master | the answer, in 0.06 s |
253+
235254
### A polynomial with symbols in it that is a binomial or an even quartic in a shifted variable is integrated in it
236255

237256
**Answers where there were none.** `1/(c^2 x^3 + 3 b c x^2 + 3 b^2 x + 3 a b)` was left unevaluated,

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17126,6 +17126,44 @@ node is Powf(Mulf(var l, var r) product, Number.Integer power) && product.Contai
1712617126
return distributed == expr ? null : Integration.ComputeAsTheSameQuestion(distributed, x, integrateByParts);
1712717127
}
1712817128

17129+
/// <summary>
17130+
/// A whole power of a quotient with a symbol in it, written as the quotient of the powers:
17131+
/// <c>(c/(a + c u^2))^2</c> is <c>c^2/(a + c u^2)^2</c>, exact for a whole power. That is how
17132+
/// the substitution <c>u = x^2</c> writes <c>x/(a + c x^4)^2</c>, having simplified
17133+
/// <c>1/(a/c + u^2)^2</c>, and the table reads the quotient of the powers and not the power
17134+
/// of the quotient: <c>c^2/(a + c x^2)^2</c> was answered in a few milliseconds where
17135+
/// <c>(c/(a + c x^2))^2</c> went to parts and was declined after twenty-six seconds, and
17136+
/// <c>x/(a + c x^4)^2</c> was answered after forty, by another route. With numbers only it
17137+
/// is answered as it is already. Only a quotient of two polynomials in <paramref name="x"/>:
17138+
/// the hyperbolic functions arrive as quotients of exponentials, and the imaginary tangent's
17139+
/// sum as <c>A e^(i z)/cos(z)</c>, and written apart those went to slower routes --
17140+
/// <c>cosh(c + d x)/(a + b tanh(c + d x)^2)</c> from 17 ms to past five seconds. And only with
17141+
/// a monomial left above the bar, the table's <c>x^m/(a + b x^n)^p</c>: a sum there is
17142+
/// multiplied out by the rational rules, and the folded <c>tanh(a + 2 ln(x))^2</c>,
17143+
/// <c>((e^(2a) x^4 - 1)/(e^(2a) x^4 + 1))^2</c>, went from five seconds to forty-five that way.
17144+
/// Asked as the same question.
17145+
/// https://github.com/asc-community/AngouriMath/issues/718
17146+
/// </summary>
17147+
internal static Entity? SolveByDistributingWholePowersOfQuotients(Entity expr, Entity.Variable x, bool integrateByParts)
17148+
{
17149+
var distributed = expr.Replace(node =>
17150+
node is Powf(Divf(var above, var below) quotient, Number.Integer power) && quotient.ContainsNode(x)
17151+
&& quotient.Vars.Any(symbol => symbol != x)
17152+
&& IsAPolynomialIn(above, x) && IsAPolynomialIn(below, x)
17153+
&& IsAMonomialIn(power.EInteger.Sign > 0 ? above : below, x)
17154+
? power.EInteger.Sign > 0
17155+
? MathS.Pow(above, power) / MathS.Pow(below, power)
17156+
: MathS.Pow(below, Number.Integer.Create(power.EInteger.Negate())) / MathS.Pow(above, Number.Integer.Create(power.EInteger.Negate()))
17157+
: node);
17158+
return distributed == expr ? null : Integration.ComputeAsTheSameQuestion(distributed, x, integrateByParts);
17159+
17160+
static bool IsAPolynomialIn(Entity expr, Entity.Variable x)
17161+
=> TreeAnalyzer.TryGetPolynomial(expr, x, out var terms)
17162+
&& terms.Keys.All(power => power.Sign >= 0) && terms.Values.All(coefficient => !coefficient.ContainsNode(x));
17163+
static bool IsAMonomialIn(Entity expr, Entity.Variable x)
17164+
=> TreeAnalyzer.TryGetPolynomial(expr, x, out var terms) && terms.Count == 1;
17165+
}
17166+
1712917167
/// <summary>
1713017168
/// A polynomial in <c>x</c> times a rational function of exponentials of <c>x</c>,
1713117169
/// by parts against the whole rational function: <c>x tanh(x)^2</c> is

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
687687
// A whole power of a product of a constant and the variable, as the product of
688688
// the powers, which is how the inverse hyperbolic secant and cosecant arrive.
689689
if ((answer = IndefiniteIntegralSolver.SolveByDistributingWholePowersOfProducts(expr, x, integrateByParts)) is { }) return answer;
690+
// And of quotients, which a substitution leaves where it has simplified a sum over
691+
// the bar: `(c/(a + c u^2))^2` is read by nothing that reads `c^2/(a + c u^2)^2`.
692+
if ((answer = IndefiniteIntegralSolver.SolveByDistributingWholePowersOfQuotients(expr, x, integrateByParts)) is { }) return answer;
690693
// And a fractional or symbolic power of a monomial: `(c x^n)^b` is `c^b x^(n b)`
691694
// for a positive `c`, on the `x > 0` where a symbolic `n` leaves the integrand real.
692695
if ((answer = IndefiniteIntegralSolver.SolveByDistributingAPowerOfAMonomial(expr, x, integrateByParts)) is { }) return answer;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 whole power of a quotient with a symbol in it is the quotient of the powers:
16+
/// <c>(c/(a + c x^2))^2</c> is <c>c^2/(a + c x^2)^2</c>, which the table answers, and was
17+
/// declined. It is how the substitution <c>u = x^2</c> writes <c>x/(a + c x^4)^2</c>.
18+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
19+
/// </summary>
20+
/// <remarks>
21+
/// Checked by differentiating back with <c>a = 2.3</c>, <c>b = 0.7</c>, <c>c = 1.3</c>, on
22+
/// both sides of zero.
23+
/// </remarks>
24+
[Trait("Area", "Calculus")]
25+
public sealed class WholePowerOfAQuotientIntegralTest
26+
{
27+
[Theory]
28+
[InlineData("(c/(a + c*x^2))^2")]
29+
[InlineData("(1/(a + c*x^2))^2")]
30+
[InlineData("(x/(a + c*x^2))^2")]
31+
[InlineData("(c/(a + c*x^2))^3")]
32+
[InlineData("((a + c*x^2)/x)^(-2)")]
33+
[InlineData("x/(a + c*x^4)^2")]
34+
[InlineData("x^2/(a + b*x^6)^2")]
35+
public void IsTheQuotientOfThePowers(string integrand)
36+
{
37+
var integral = integrand.ToEntity().Integrate("x");
38+
Assert.DoesNotContain("integral(", integral.Stringize());
39+
Assert.DoesNotContain("NaN", integral.Stringize());
40+
Entity Pinned(Entity e) => e.Substitute("a", 2.3).Substitute("b", 0.7).Substitute("c", 1.3);
41+
var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x");
42+
var original = Pinned(integrand.ToEntity());
43+
foreach (var at in new[] { -1.4, -0.6, 0.5, 1.2 })
44+
{
45+
var got = derivative.Substitute("x", at).EvalNumerical();
46+
var want = original.Substitute("x", at).EvalNumerical();
47+
Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart)
48+
< 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart)),
49+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)