Skip to content

Commit d92add5

Browse files
A function of x^n for a symbolic n beside a power of x is integrated in a power of x (#1784)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent 2b86349 commit d92add5

4 files changed

Lines changed: 195 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,20 @@ rules' already
287287
| `"asin(sqrt(1 + x) - sqrt(x))".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in `arcsin(sqrt(1 + x) - sqrt(x))` |
288288
| `"x^3*atan(-sqrt(x)+sqrt(1+x))".ToEntity().Integrate("x")` | `integral(...)` | an antiderivative in the arctangent |
289289

290+
### A function of `x^n` for a symbolic `n` beside a power of `x` is integrated in a power of `x`
291+
292+
**Answers where there were none.** `x^(-1 + 4n)/(a + b x^n + c x^(2n))` and the rest of Rubi's
293+
1.2.3.2 functions of `x^n` beside a power `x^m` with `(m + 1)/n` rational were declined: the
294+
substitution search reads `x^(2n)` as a power of `x^n` only for a number `n`, and offered nothing
295+
for `x^(n/2)`, which is not written. Under `u = x^(n/d)` every such power is a whole power of `u`,
296+
and `dx/x` is `(d/n) du/u` ([#718](https://github.com/asc-community/AngouriMath/issues/718)).
297+
298+
| Input | Was (2.5.0) | Now |
299+
|---|---|---|
300+
| `"x^(-1+4*n)/(a+b*x^n+c*x^(2*n))".ToEntity().Integrate("x")` | `integral(...)` | powers of `x^n`, a logarithm and an arctangent of it, over `n` |
301+
| `"1/(x*(a+b*x^n+c*x^(2*n)))".ToEntity().Integrate("x")` | `integral(...)` | `ln(x^n)/(a n)`, a logarithm and an arctangent of `x^n` |
302+
| `"x^(-1+n/2)/(a+b*x^n+c*x^(2*n))".ToEntity().Integrate("x")` | `integral(...)` | arctangents of `x^(n/2)` over the roots of `a + b u + c u^2`, over `n` |
303+
290304
### Rational functions of `x` and `x^n` with symbols in them are taken apart
291305

292306
**Answers where there were none.** The partial fractions took apart a denominator with symbols in it

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

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26663,6 +26663,117 @@ private static bool IsARationalFunctionOfExponentials(Entity expr, Entity.Variab
2666326663
return !stripped.ContainsNode(x);
2666426664
}
2666526665

26666+
/// <summary>
26667+
/// A function of <c>x^n</c> for a symbolic <c>n</c> beside a power of <c>x</c>,
26668+
/// <c>x^m R(x^n)</c> with <c>(m + 1)/n</c> rational, under <c>u = x^(n/d)</c>: Rubi's
26669+
/// <c>x^(-1 + 4n)/(a + b x^n + c x^(2n))</c> and <c>x^(-1 + n/4)/(b x^n + c x^(2n))</c>.
26670+
/// </summary>
26671+
/// <remarks>
26672+
/// <para>
26673+
/// With <c>g = x f(x)</c>, <c>int f dx = (d/n) int g/u du</c>, since <c>dx/x = (d/n) du/u</c>;
26674+
/// and every power <c>x^t</c> in <c>g</c> is <c>u^(t d/n)</c>, a whole power of <c>u</c>
26675+
/// where <c>t d/n</c> is a whole number, which makes it the same value for every
26676+
/// <c>x</c>. The search reads <c>x^(2n)</c> as a power of <c>x^n</c> only for a number
26677+
/// <c>n</c>, and found no candidate for <c>x^(n/4)</c>, which is not written; so these
26678+
/// were declined. The powers of <c>x</c> are gathered first, <c>x x^(-1 + 4n)</c> into
26679+
/// <c>x^(4n)</c>, and the unit is each exponent left, divided by up to six.
26680+
/// https://github.com/asc-community/AngouriMath/issues/718
26681+
/// </para>
26682+
/// </remarks>
26683+
internal static Entity? SolveByAPowerOfXWithASymbolicExponent(Entity expr, Entity.Variable x, bool integrateByParts)
26684+
{
26685+
if (!expr.Nodes.Any(node => node is Powf(var @base, var exponent) && @base == x && exponent is not Number && !exponent.ContainsNode(x)))
26686+
return null;
26687+
// An algebraic function of the powers only. Under a function the power is the
26688+
// function's, and the search finds the substitution for it: `coth(a + b ln(c x^n))^3/x`
26689+
// is `t = ln(c x^n)`, and in `u = x^n` it was `coth(a + b ln(c u))^3/u`, the same
26690+
// question in another letter at the cost of a second search.
26691+
if (expr.Nodes.Any(node => node.ContainsNode(x)
26692+
&& node is not (Sumf or Minusf or Mulf or Divf or Variable) && !(node is Powf(_, var exponent) && !exponent.ContainsNode(x))))
26693+
return null;
26694+
// Over one bar first, so that an x inside `(x (a + b x^n))^(-1)` is a factor to gather.
26695+
var g = Patterns.GatherPowersOfOneBase(Functions.SingleQuotient.Combine(x * expr));
26696+
var exponents = g.Nodes.OfType<Powf>().Where(power => power.Base == x && power.Exponent is not Number && !power.Exponent.ContainsNode(x))
26697+
.Select(power => power.Exponent).Distinct().OrderBy(exponent => exponent.Complexity).ToList();
26698+
if (exponents.Count == 0)
26699+
return null;
26700+
var u = Variable.CreateUnique(expr, "u_power");
26701+
foreach (var unit in exponents)
26702+
foreach (var d in new[] { 1, 2, 3, 4, 6 })
26703+
{
26704+
var step = (unit / d).InnerSimplified;
26705+
// Every power of x a whole power of u, and no x left bare or elsewhere.
26706+
var whole = true;
26707+
var inU = g.Replace(node =>
26708+
{
26709+
if (!whole || node is not Powf(var @base, var exponent) || @base != x)
26710+
return node;
26711+
if (Functions.PartialFractions.Bare((exponent / step).Simplify()) is Number.Integer power)
26712+
return power == Number.Integer.One ? u : MathS.Pow(u, power);
26713+
whole = false;
26714+
return node;
26715+
});
26716+
if (!whole || inU.ContainsNode(x))
26717+
continue;
26718+
// Divided by u through the gathering, where `u/u` left in is a quotient the
26719+
// chain declines; the constant `d/n` outside it.
26720+
var overU = Functions.PartialFractions.Bare(Patterns.GatherPowersOfOneBase(Functions.SingleQuotient.Combine(inU) * MathS.Pow(u, -1)).InnerSimplified);
26721+
// Each constant with a symbol in it that is not built from symbols by arithmetic
26722+
// and whole powers -- `e^a`, `c^b`, a function of the symbols -- named while the
26723+
// integral in u is asked, and written back into what it gives:
26724+
// `coth(a + b ln(c x^n))^3/x` arrives here as
26725+
// `((e^a)^2 (c^b)^2 x^(2 n b) + 1)^3/(...)^3/x`, whose integral in u was past half a
26726+
// minute with `(e^a)^2 (c^b)^2` carried through the partial fractions, and is under
26727+
// one in `k^2 m^2`. Those only: a coefficient named whole hides what relates it to
26728+
// the others, and `a^2 + 2 a b u + b^2 u^2` named so is a quadratic with no double
26729+
// root, answered wrongly.
26730+
var named = new List<(Variable Name, Entity Value)>();
26731+
var namedOverU = NameTheAtoms(overU);
26732+
if (Integration.ComputeIndefiniteIntegral(namedOverU, u, integrateByParts) is not { } inTermsOfU)
26733+
return null;
26734+
foreach (var (name, value) in named)
26735+
inTermsOfU = inTermsOfU.Substitute(name, value);
26736+
var answer = (d / unit * inTermsOfU).Substitute(u, MathS.Pow(x, step)).InnerSimplified;
26737+
return answer.Nodes.Any(node => node is Number.Complex { IsNaN: true }) ? null : answer;
26738+
26739+
// Arithmetic and whole powers taken apart; a root of something with u in it keeps
26740+
// its exponent; any other constant with a symbol in it is an atom, and named.
26741+
Entity NameTheAtoms(Entity e)
26742+
{
26743+
switch (e)
26744+
{
26745+
case Number or Variable:
26746+
return e;
26747+
case Sumf(var left, var right):
26748+
return NameTheAtoms(left) + NameTheAtoms(right);
26749+
case Minusf(var left, var right):
26750+
return NameTheAtoms(left) - NameTheAtoms(right);
26751+
case Mulf(var left, var right):
26752+
return NameTheAtoms(left) * NameTheAtoms(right);
26753+
case Divf(var above, var below):
26754+
return NameTheAtoms(above) / NameTheAtoms(below);
26755+
case Powf(var @base, Number.Integer whole):
26756+
return MathS.Pow(NameTheAtoms(@base), whole);
26757+
case Powf(var @base, var exponent) when @base.ContainsNode(u) && !exponent.ContainsNode(u):
26758+
return MathS.Pow(NameTheAtoms(@base), exponent);
26759+
default:
26760+
return !e.ContainsNode(u) && e.Vars.Any() ? Named(e) : e;
26761+
}
26762+
}
26763+
26764+
Entity Named(Entity value)
26765+
{
26766+
foreach (var (name, earlier) in named)
26767+
if (earlier == value)
26768+
return name;
26769+
var fresh = Variable.CreateUnique(overU + named.Aggregate((Entity)Number.Integer.Zero, (sum, pair) => sum + pair.Name), "k_named");
26770+
named.Add((fresh, value));
26771+
return fresh;
26772+
}
26773+
}
26774+
return null;
26775+
}
26776+
2666626777
/// <summary>
2666726778
/// A quotient whose numerator is a constant multiple of its denominator's derivative,
2666826779
/// with a symbolic exponent about: <c>c ln(D)</c>, the constant read off the terms.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,10 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
908908
// Two roots of linears with one slope, rationalised together by their sum: before the
909909
// substitution search, which reads each root on its own.
910910
if ((answer = IndefiniteIntegralSolver.SolveBySubstitutingTheSumOfTwoRootsOfLinears(expr, x, integrateByParts)) is { }) return answer;
911+
// A function of `x^n` for a symbolic `n` beside a power of `x`, under `u = x^(n/d)`:
912+
// before the substitution search, which reads `x^(2n)` as a power of `x^n` only for a
913+
// number `n`.
914+
if ((answer = IndefiniteIntegralSolver.SolveByAPowerOfXWithASymbolicExponent(expr, x, integrateByParts)) is { }) return answer;
911915
if ((answer = IndefiniteIntegralSolver.SolveBySubstitution(expr, x, integrateByParts)) is { }) return answer;
912916
// A logarithmic derivative the substitution above could not read for a symbol in
913917
// an exponent: `(x^(n-1) - 1)/(x^n - n x)`.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 function of <c>x^n</c> for a symbolic <c>n</c> beside a power of <c>x</c>,
16+
/// <c>x^m R(x^n)</c> with <c>(m + 1)/n</c> rational, under <c>u = x^(n/d)</c>: Rubi's 1.2.3.2.
17+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
18+
/// </summary>
19+
/// <remarks>
20+
/// Checked by differentiating back with <c>a = 1.3</c>, <c>b = 0.7</c>, <c>c = 0.4</c> and
21+
/// <c>n = 1.7</c>, at positive <c>x</c>, where a symbolic power of it is real.
22+
/// </remarks>
23+
[Trait("Area", "Calculus")]
24+
public sealed class PowerOfXWithASymbolicExponentIntegralTest
25+
{
26+
[Theory]
27+
[InlineData("x^(-1 + 4*n)/(a + b*x^n + c*x^(2*n))")]
28+
[InlineData("x^(-1 + n)/(b*x^n + c*x^(2*n))")]
29+
[InlineData("1/(x*(a + b*x^n + c*x^(2*n)))")]
30+
// Under u = x^(n/2).
31+
[InlineData("x^(-1 + n/2)/(a + b*x^n + c*x^(2*n))")]
32+
[InlineData("sqrt(a + b*x^n + c*x^(2*n))/x")]
33+
// A perfect square, which stays one in u: its coefficients are not named apart.
34+
[InlineData("1/(x*(a^2 + 2*a*b*x^n + b^2*x^(2*n))^(3/2))")]
35+
public void IsIntegratedInAPowerOfX(string integrand) => DifferentiatesBack(integrand);
36+
37+
/// <summary>
38+
/// <c>coth(a + b ln(c x^n))^3/x</c> is written through the exponential form as a rational
39+
/// function of <c>x^(2 n b)</c> with <c>(e^a)^2 (c^b)^2</c> in it, and its integral in
40+
/// <c>u</c> is asked with that constant named: carried through the partial fractions as
41+
/// written it was past half a minute, where named it is under one. A regression makes
42+
/// this crawl rather than fail.
43+
/// </summary>
44+
[Theory]
45+
[InlineData("coth(a + b*ln(c*x^n))^3/x")]
46+
[InlineData("tanh(a + b*ln(c*x^n))^3/x")]
47+
public void ACompoundConstantIsNamedInThePower(string integrand) => DifferentiatesBack(integrand);
48+
49+
private static void DifferentiatesBack(string integrand)
50+
{
51+
var integral = integrand.ToEntity().Integrate("x");
52+
Assert.DoesNotContain("integral(", integral.Stringize());
53+
Entity Pinned(Entity e) => e.Substitute("a", 1.3).Substitute("b", 0.7).Substitute("c", 0.4).Substitute("n", 1.7);
54+
var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x");
55+
var original = Pinned(integrand.ToEntity());
56+
foreach (var at in new[] { 0.3, 0.8, 1.4, 2.1 })
57+
{
58+
var want = original.Substitute("x", at).EvalNumerical();
59+
var got = derivative.Substitute("x", at).EvalNumerical();
60+
Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart)
61+
< 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)),
62+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
63+
}
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)