Skip to content

Commit da07bd9

Browse files
Integrate a power of a sum beside its derivative with symbolic powers in it (#1954)
c g' g^k is c g^(k + 1)/(k + 1), and c ln(g) for k = -1, but the derivative of ln(c x^n)^q is written q ln(c x^n)^(q - 1) c n x^(n - 1)/(c x^n), which nothing brought down to the n q ln(c x^n)^(q - 1)/x the integrand writes. The terms are now read as a coefficient times powers of x and of the other functions of x in them, each base's powers merged, and a power of x common to the sum's terms is taken out of it: a x^m + b x ln(c x^n)^q is x (a x^(m - 1) + b ln(c x^n)^q). Rubi's 3.5: rows 29, 31, 38, 39 and 40 go from unevaluated or timed out to solved (family 3 1349 -> 1354 of 1382), 0 wrong. Families 0 and 1 and samples of 2-8 otherwise identical; gate and suite pass. Part of #233. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 7280a44 commit da07bd9

4 files changed

Lines changed: 201 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,20 @@ compared everywhere with its value at one positive point
203203
| `"asin(sqrt(1+b*x^2))^n/sqrt(1+b*x^2)".ToEntity().Integrate("x")` | `integral(...)` | 136 characters |
204204
| `"acos(sqrt(1+b*x^2))^n/sqrt(1+b*x^2)".ToEntity().Integrate("x")` | `integral(...)` | 137 characters |
205205

206+
### A power of a sum beside its derivative is read with the logarithms in it as factors
207+
208+
**Answers where there were none.** `(a m x^m + b n q ln(c x^n)^(q - 1))/(x (a x^m + b ln(c x^n)^q))` was declined, and so
209+
were five more of Rubi's 3.5, the sum's square and the powers of `a x^2 + b x ln(c x^n)^2` among them. Each is a
210+
constant times `g' g^k` for a sum `g`, but the derivative of `ln(c x^n)^q` is written `q ln(c x^n)^(q - 1) c n x^(n - 1)/(c x^n)`,
211+
which nothing brought down to the `n q ln(c x^n)^(q - 1)/x` the integrand writes. The terms are now read as a
212+
coefficient times powers of x and of the other functions of x in them, and a power of x common to the sum is taken
213+
out of it ([#233](https://github.com/asc-community/AngouriMath/issues/233)).
214+
215+
| Input | Was (2.5.0) | Now |
216+
|---|---|---|
217+
| `"(a*m*x^m+b*n*q*ln(c*x^n)^(q-1))/(x*(a*x^m+b*ln(c*x^n)^q))".ToEntity().Integrate("x")` | `integral(...)` | `ln(a x^m + b ln(c x^n)^q)` |
218+
| `"(a*(m-1)*x^(m-1)+b*n*q*ln(c*x^n)^(q-1))/(a*x^m+b*x*ln(c*x^n)^q)".ToEntity().Integrate("x")` | `integral(...)` | `ln(a x^(m - 1) + b ln(c x^n)^q)` |
219+
206220
### A rational evaluated at a higher precision than before has all of its digits
207221

208222
**More digits where there were a hundred.** A rational kept its decimal value in the precision current when it was

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

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34500,6 +34500,161 @@ Entity Named(Entity value)
3450034500
}
3450134501
}
3450234502

34503+
/// <summary>
34504+
/// A power of a sum beside a constant multiple of the sum's derivative, with symbolic powers
34505+
/// about: <c>c g' g^k</c> is <c>c g^(k + 1)/(k + 1)</c>, and <c>c ln(g)</c> for <c>k = -1</c>.
34506+
/// Each term is read as a coefficient times a power of <c>x</c> times powers of the other
34507+
/// functions of <c>x</c> in it, the powers of each merged, so that
34508+
/// <c>d/dx ln(c x^n)^q = q n ln(c x^n)^(q - 1)/x</c> is matched against
34509+
/// <c>b n q ln(c x^n)^(q - 1)/x</c> as written. Where the terms do not match, a power of
34510+
/// <c>x</c> common to every term of the sum is taken out of it into the rest:
34511+
/// <c>a x^m + b x ln(c x^n)^q</c> is <c>x (a x^(m - 1) + b ln(c x^n)^q)</c>. Rubi's 3.5.
34512+
/// https://github.com/asc-community/AngouriMath/issues/233
34513+
/// </summary>
34514+
/// <remarks>
34515+
/// <see cref="SolveALogarithmicDerivativeWithASymbolicExponent"/> reads only powers of
34516+
/// <c>x</c>, and the substitution's simplification of the quotient by <c>du/dx</c> never
34517+
/// brings <c>c n x^(n - 1)/(c x^n)</c> down to <c>n/x</c>. Only where a power is symbolic:
34518+
/// otherwise the substitution answers it.
34519+
/// </remarks>
34520+
internal static Entity? SolveAPowerOfASumBesideItsDerivativeWithSymbolicPowers(Entity expr, Entity.Variable x)
34521+
{
34522+
if (!expr.Nodes.Any(node => node is Powf(var @base, var exponent) && @base.ContainsNode(x) && !exponent.ContainsNode(x) && exponent.Vars.Any()))
34523+
return null;
34524+
var factors = FactorsOfTheIntegrand(expr);
34525+
for (var chosen = 0; chosen < factors.Count; chosen++)
34526+
{
34527+
var (factor, underneath) = factors[chosen];
34528+
var (sum, power) = factor is Powf(var b, var e) && !e.ContainsNode(x) ? (b, e) : (factor, (Entity)Number.Integer.One);
34529+
if (sum is not (Sumf or Minusf) || !sum.ContainsNode(x) || power.Evaled is not Number.Rational k)
34530+
continue;
34531+
if (underneath)
34532+
k = (Number.Rational)(-k);
34533+
// The rest: at most one sum above the bar, every other factor one term.
34534+
Entity? restSum = null;
34535+
Entity monomial = Number.Integer.One;
34536+
var readable = true;
34537+
for (var other = 0; other < factors.Count && readable; other++)
34538+
{
34539+
if (other == chosen)
34540+
continue;
34541+
var (f, below) = factors[other];
34542+
if (!below && restSum is null && f is Sumf or Minusf && f.ContainsNode(x))
34543+
restSum = f;
34544+
else if (f is Sumf or Minusf && f.ContainsNode(x))
34545+
readable = false;
34546+
else
34547+
monomial = below ? monomial / f : monomial * f;
34548+
}
34549+
if (!readable)
34550+
continue;
34551+
var rest = (restSum is null ? new[] { monomial } : Sumf.LinearChildren(restSum).Select(term => term * monomial)).ToList();
34552+
if (Matched(rest, sum, x) is { } constant)
34553+
return Answer(constant, sum, k);
34554+
// A power of x common to the sum's terms, taken out: x^j g, so that c x^(j k) g'... is read against g.
34555+
if (Terms(Sumf.LinearChildren(sum), x) is not { } terms)
34556+
continue;
34557+
foreach (var common in terms.Select(t => t.Exponent).Where(e => e.Evaled is Number.Integer { IsZero: false }).Distinct().ToList())
34558+
{
34559+
Entity reduced = Number.Integer.Zero;
34560+
foreach (var t in terms)
34561+
{
34562+
var exponent = (t.Exponent - common).InnerSimplified;
34563+
Entity written = t.Atoms.Aggregate(t.Coefficient, (product, atom) => product * MathS.Pow(atom.Base, atom.Power));
34564+
if (exponent.Evaled is not Number.Complex { IsZero: true })
34565+
written *= exponent == Number.Integer.One ? x : MathS.Pow(x, exponent);
34566+
reduced = reduced == Number.Integer.Zero ? written : reduced + written;
34567+
}
34568+
// x^(common k) moves into the rest.
34569+
var moved = MathS.Pow(x, (common * k).InnerSimplified);
34570+
if (Matched(rest.Select(term => term * moved).ToList(), reduced, x) is { } c)
34571+
return Answer(c, reduced, k);
34572+
}
34573+
}
34574+
return null;
34575+
34576+
static Entity Answer(Entity constant, Entity sum, Number.Rational k)
34577+
=> k == Number.Integer.MinusOne
34578+
? constant * MathS.Ln(sum)
34579+
: constant / (k + 1) * MathS.Pow(sum, (Entity)(k + 1));
34580+
34581+
// The constant c with the rest's terms c times the sum's derivative's, or null.
34582+
static Entity? Matched(List<Entity> rest, Entity sum, Entity.Variable x)
34583+
{
34584+
var derivative = sum.Differentiate(x);
34585+
if (derivative is Providedf(var bare, _))
34586+
derivative = bare;
34587+
if (Terms(rest, x) is not { } above || Terms(Sumf.LinearChildren(derivative), x) is not { } below || above.Count != below.Count)
34588+
return null;
34589+
Entity? constant = null;
34590+
foreach (var term in above)
34591+
{
34592+
var matching = below.Where(other => SameMonomial(term, other)).ToList();
34593+
if (matching.Count != 1)
34594+
return null;
34595+
var ratio = Bare((term.Coefficient / matching[0].Coefficient).Simplify());
34596+
if (ratio.ContainsNode(x) || ratio.Nodes.Any(node => node == MathS.NaN))
34597+
return null;
34598+
if (constant is null)
34599+
constant = ratio;
34600+
else if (Bare((constant - ratio).Simplify()) != Number.Integer.Zero)
34601+
return null;
34602+
}
34603+
return constant;
34604+
}
34605+
34606+
static bool SameMonomial((Entity Coefficient, Entity Exponent, List<(Entity Base, Entity Power)> Atoms) one,
34607+
(Entity Coefficient, Entity Exponent, List<(Entity Base, Entity Power)> Atoms) other)
34608+
=> Bare((one.Exponent - other.Exponent).Simplify()) == Number.Integer.Zero
34609+
&& one.Atoms.Count == other.Atoms.Count
34610+
&& one.Atoms.All(atom => other.Atoms.Any(theirs => theirs.Base == atom.Base && Bare((theirs.Power - atom.Power).Simplify()) == Number.Integer.Zero));
34611+
34612+
static Entity Bare(Entity simplified) => simplified is Providedf(var inner, _) ? inner : simplified;
34613+
34614+
// Each term as a coefficient free of x, a power of x, and powers of other functions of
34615+
// x, each base's powers merged; null where a factor is a sum holding x.
34616+
static List<(Entity Coefficient, Entity Exponent, List<(Entity Base, Entity Power)> Atoms)>? Terms(IEnumerable<Entity> terms, Entity.Variable x)
34617+
{
34618+
var read = new List<(Entity, Entity, List<(Entity, Entity)>)>();
34619+
foreach (var term in terms)
34620+
{
34621+
var (numerator, denominator) = Functions.SingleQuotient.Of(term is Providedf(var t, _) ? t : term);
34622+
Entity exponent = Number.Integer.Zero;
34623+
Entity coefficient = Number.Integer.One;
34624+
var atoms = new List<(Entity Base, Entity Power)>();
34625+
foreach (var (side, underneath) in new[] { (numerator, false), (denominator, true) })
34626+
foreach (var factor in Mulf.LinearChildren(side))
34627+
{
34628+
if (!factor.ContainsNode(x))
34629+
{
34630+
coefficient = underneath ? coefficient / factor : coefficient * factor;
34631+
continue;
34632+
}
34633+
var (@base, power) = factor is Powf(var b, var e) && !e.ContainsNode(x) ? (b, e) : (factor, (Entity)Number.Integer.One);
34634+
if (underneath)
34635+
power = -power;
34636+
if (@base == x)
34637+
exponent += power;
34638+
else if (@base is Sumf or Minusf)
34639+
return null;
34640+
else
34641+
{
34642+
var at = atoms.FindIndex(atom => atom.Base == @base);
34643+
if (at < 0)
34644+
atoms.Add((@base, power));
34645+
else
34646+
atoms[at] = (@base, atoms[at].Power + power);
34647+
}
34648+
}
34649+
// A term the derivative writes as 0 times something is no term.
34650+
if (coefficient.InnerSimplified.Evaled is Number.Complex { IsZero: true })
34651+
continue;
34652+
read.Add((coefficient, exponent.InnerSimplified, atoms.Select(atom => (atom.Base, atom.Power.InnerSimplified)).ToList()));
34653+
}
34654+
return read;
34655+
}
34656+
}
34657+
3450334658
/// <summary>
3450434659
/// An exponential of a polynomial beside the polynomial's derivative, with the rest a
3450534660
/// function of the polynomial: <c>G^P k P' f(P)</c> is <c>k G^u f(u)</c> under <c>u = P</c>.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,8 @@ private static Entity WithItsPiecewiseSumsCombined(Entity expr)
11001100
// A logarithmic derivative the substitution above could not read for a symbol in
11011101
// an exponent: `(x^(n-1) - 1)/(x^n - n x)`.
11021102
if ((answer = IndefiniteIntegralSolver.SolveALogarithmicDerivativeWithASymbolicExponent(expr, x)) is { }) return answer;
1103+
// And a power of a sum beside its derivative, with functions of x besides powers of it in the terms.
1104+
if ((answer = IndefiniteIntegralSolver.SolveAPowerOfASumBesideItsDerivativeWithSymbolicPowers(expr, x)) is { }) return answer;
11031105
// After the general substitution rather than inside it, because the general one
11041106
// divides by du/dx and asks what is left, and that question loses the shape here:
11051107
// sqrt(tan(x)) over the derivative of sqrt(tan(x)) simplifies to sin(2x), in which
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 AngouriMath.Extensions;
9+
using Xunit;
10+
11+
namespace AngouriMath.Tests.Calculus
12+
{
13+
/// <summary>
14+
/// A power of a sum beside its derivative, where the terms hold a symbolic power of a
15+
/// logarithm: the derivative of <c>ln(c x^n)^q</c> is written <c>q ln(c x^n)^(q - 1) c n x^(n - 1)/(c x^n)</c>,
16+
/// which nothing brought down to the <c>n q ln(c x^n)^(q - 1)/x</c> the integrand writes. They were
17+
/// declined. Rubi's 3.5.
18+
/// <a href="https://github.com/asc-community/AngouriMath/issues/233">#233</a>
19+
/// </summary>
20+
[Trait("Area", "Calculus")]
21+
public sealed class APowerOfASumBesideItsDerivativeWithSymbolicPowersIntegralTest
22+
{
23+
[Theory]
24+
[InlineData("(a*m*x^m + b*n*q*ln(c*x^n)^(q - 1))*(a*x^m + b*ln(c*x^n)^q)/x", "1/2*(a*x^m + b*ln(c*x^n)^q)^2")]
25+
[InlineData("(a*m*x^m + b*n*q*ln(c*x^n)^(q - 1))/(x*(a*x^m + b*ln(c*x^n)^q))", "ln(a*x^m + b*ln(c*x^n)^q)")]
26+
[InlineData("(a*(m - 1)*x^(m - 1) + b*n*q*ln(c*x^n)^(q - 1))/(a*x^m + b*x*ln(c*x^n)^q)", "ln(a*x^(m - 1) + b*ln(c*x^n)^q)")]
27+
public void TheSumToTheNextPower(string integrand, string expected)
28+
=> Assert.Equal(expected.ToEntity().InnerSimplified, integrand.ToEntity().Integrate("x").Substitute("C", 0).InnerSimplified);
29+
}
30+
}

0 commit comments

Comments
 (0)