Skip to content

Commit e33fbab

Browse files
Symbolic factors of a denominator that share a factor are written over it (#1687)
* Symbolic factors of a denominator that share a factor are written over it The partial fractions over written symbolic factors read them as coprime and squarefree, and the refactoring after them reads rational coefficients only. A quadratic that shares a root with a linear beside it, (d + e x)(a d e + (c d^2 + a e^2) x + c d e x^2)^2, or is a square, c d^2 + 2 c d e x + c e^2 x^2, was read as an irreducible quadratic: the first was declined after four seconds, (d + e x)^6/(a d e + ...)^4 ran past thirty, and x^2/((a + b x)(a^2 + 2 a b x + b^2 x^2)) was declined. The written factors are now taken apart over their greatest common divisors, with one another and each with its derivative in x, by the multivariate gcd the library has, until they are coprime and squarefree; a factor shared with the numerator is taken apart the same way and cancelled. Each factor is written monic in x, as the integrator writes one, with what is free of x in front. Each of the four above is answered, under three seconds. Part of #718. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura * A breaking-changes entry for symbolic factors written over what they share Measured on the 2.5.0 tag and on this branch. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura * A factor taken apart keeps its sign A divisor is found up to a unit, so taking -(b u^2 - 1)^2 apart over its derivative leaves a part -1, whose content is 1, and the part was dropped with its sign: the denominator came out as the square. A part free of the variable now goes in front whole. Rubi's 1.1.2.2 x^4/(a + b x^2)^(3/2) is this under t = x/sqrt(a + b x^2). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura * A power of x that factors share is left to the content The split over common factors took b x^2 + c x^4 apart over its derivative as x^2 (b/c + x^2) c. Over that monic quadratic, with a symbol below its bar, Rubi's 1.1.4.3 x^12 (A + B x^2)/(b x^2 + c x^4)^3 had an answer of four million characters, and checking it ran past the budget. Taking a power of x out of a factor is what the content does, before this split, and with the content taken out the answer has seven thousand characters. A common factor that is a power of x alone is now not split over. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 70b4be6 commit e33fbab

3 files changed

Lines changed: 274 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,23 @@ power of the variable every term holds comes out the same way.
564564
| `"x^2/((a*x + a)*(1 - x^2))".Integrate("x")` | unevaluated | the partial fractions over `(x + 1)^2 (1 - x)`, over `a` |
565565
| `"cot(x)^3/(a + b*csc(x))".Integrate("x")` | unevaluated | `-1/(b sin(x)) - a ln(sin(x))/b^2 + (b^2 - a^2) ln(a sin(x) + b)/(a b^2)`, once `a u^4 + b u^3` is read as `u^3 (a u + b)` |
566566

567+
### Symbolic factors of a denominator that share a factor are written over it
568+
569+
**Improvement, not silent.** Partial fractions over factors with symbols in them read the written
570+
factors as coprime and squarefree, and the refactoring after them reads rational coefficients only.
571+
A quadratic that shares a root with a linear beside it, or that is a square, was read as an
572+
irreducible quadratic and declined. The written factors are now taken apart over their greatest
573+
common divisors, in every variable, with one another and each with its derivative, and a factor
574+
shared with the numerator is cancelled. Rubi's 1.2.1.2
575+
([#718](https://github.com/asc-community/AngouriMath/issues/718)).
576+
577+
| Input | Was (2.5.0) | Now |
578+
|---|---|---|
579+
| `"1/((d+e*x)*(a*d*e+(c*d^2+a*e^2)*x+c*d*e*x^2)^2)".Integrate("x")` | unevaluated | the partial fractions over `(d + e x)^3 (a e + c d x)^2` |
580+
| `"(d+e*x)^6/(a*d*e+(c*d^2+a*e^2)*x+c*d*e*x^2)^4".Integrate("x")` | no answer in 90 s | the antiderivative of `(d + e x)^2/(a e + c d x)^4` |
581+
| `"1/((d+e*x)^2*(c*d^2+2*c*d*e*x+c*e^2*x^2)^3)".Integrate("x")` | unevaluated | `-1/(7 c^3 e^8 (d/e + x)^7)` |
582+
| `"x^2/((a+b*x)*(a^2+2*a*b*x+b^2*x^2))".Integrate("x")` | unevaluated | the partial fractions over `(a + b x)^3` |
583+
567584
### Fractional powers of `a ± a sin` are integrated by the half angle at which they are squares
568585

569586
**Improvement, not silent.** `(A + C sin(y)^2)/((c - c sin(y))^(3/2) sqrt(a + a sin(y)))` and

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

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,19 @@ is var (multiple, leftover)
665665
?? Integration.ComputeIndefiniteIntegral(numerator / primitive, x, integrateByParts)) is { } overPrimitives)
666666
return overPrimitives;
667667

668+
// Written factors with a symbol in them that share a factor, or repeat one inside,
669+
// written over what they share: `(d + e x)(a d e + (c d^2 + a e^2) x + c d e x^2)^2`
670+
// is `(d + e x)^3 (a e + c d x)^2`, and `a^2 + 2 a b x + b^2 x^2` beside `a + b x`
671+
// is a third power of it. The splits below read the written factors as coprime and
672+
// squarefree, and the refactoring after them reads rational coefficients only:
673+
// Rubi's 1.2.1.2 `1/((d + e x)(a d e + (c d^2 + a e^2) x + c d e x^2)^2)` was declined
674+
// after four seconds, where written so it is answered in a tenth of one. Once: what
675+
// this writes is coprime and squarefree already.
676+
if (OverSymbolicFactorsWrittenApart(numerator, denominator, x) is var (above, below)
677+
&& (SolveByPartialFractions(above / below, x, integrateByParts)
678+
?? Integration.ComputeIndefiniteIntegral(above / below, x, integrateByParts)) is { } overWhatTheyShare)
679+
return overWhatTheyShare;
680+
668681
// Written linear factors with symbols in their coefficients, two or more, one of
669682
// them to a power: decomposed over the written factors, the coefficients read
670683
// off derivatives at the roots and each a line. First, before the respellings
@@ -10280,6 +10293,221 @@ private static bool IsASumOfMonomials(Entity expr, Entity.Variable x)
1028010293
return changed ? product : null;
1028110294
}
1028210295

10296+
/// <summary>
10297+
/// <paramref name="numerator"/> over <paramref name="denominator"/>, the denominator's
10298+
/// written factors taken apart over their greatest common divisors -- with one another,
10299+
/// and each with its own derivative in <paramref name="x"/> -- until they are coprime and
10300+
/// squarefree as written, and what the numerator shares with them cancelled;
10301+
/// <see langword="null"/> where no factor holds a symbol, they are coprime and squarefree
10302+
/// already, or one is not a polynomial.
10303+
/// </summary>
10304+
/// <remarks>
10305+
/// The divisors are taken over the rationals in every variable, so a factor of the symbols
10306+
/// alone can come with one: that goes in front, as a constant, and each factor kept is
10307+
/// primitive in <paramref name="x"/>. `c d^2 + 2 c d e x + c e^2 x^2` shares `c (d + e x)`
10308+
/// with its derivative, and is `c (d + e x)^2`.
10309+
/// </remarks>
10310+
private static (Entity Numerator, Entity Denominator)? OverSymbolicFactorsWrittenApart(Entity numerator, Entity denominator, Entity.Variable x)
10311+
{
10312+
var written = new List<(Entity Base, int Power)>();
10313+
Entity constant = Number.Integer.One;
10314+
foreach (var factor in Mulf.LinearChildren(denominator))
10315+
{
10316+
if (!factor.ContainsNode(x))
10317+
{
10318+
constant = constant == Number.Integer.One ? factor : constant * factor;
10319+
continue;
10320+
}
10321+
var (@base, power) = factor is Powf(var b, Number.Integer e) && e.EInteger.Sign > 0 && e.EInteger.CanFitInInt32()
10322+
? (b, e.EInteger.ToInt32Unchecked())
10323+
: (factor, 1);
10324+
// Over one bar, the constant below it in front: the integrator writes a quadratic
10325+
// monic, `a/c + (c d^2 + a e^2)/(c d e) x + x^2`, and it is a polynomial in the
10326+
// symbols over `c d e`.
10327+
var (overOneBar, belowTheBar) = Functions.SingleQuotient.Of(Functions.SingleQuotient.Combine(@base));
10328+
if (belowTheBar.ContainsNode(x))
10329+
return null;
10330+
if (belowTheBar != Number.Integer.One)
10331+
{
10332+
var front = power == 1 ? belowTheBar : MathS.Pow(belowTheBar, power);
10333+
constant = constant == Number.Integer.One ? 1 / front : constant / front;
10334+
}
10335+
written.Add((overOneBar, power));
10336+
}
10337+
if (!written.Any(pair => pair.Base.Vars.Any(v => v != x)))
10338+
return null;
10339+
var (numeratorOverOneBar, belowTheNumerator) = Functions.SingleQuotient.Of(Functions.SingleQuotient.Combine(numerator));
10340+
if (!belowTheNumerator.ContainsNode(x))
10341+
{
10342+
numerator = numeratorOverOneBar;
10343+
if (belowTheNumerator != Number.Integer.One)
10344+
constant = constant == Number.Integer.One ? belowTheNumerator : constant * belowTheNumerator;
10345+
}
10346+
var variables = numerator.Vars.Concat(denominator.Vars).Distinct().OrderBy(v => v.Name, System.StringComparer.Ordinal).ToList();
10347+
if (variables.Count > MultivariatePolynomial.MaxVariables)
10348+
return null;
10349+
var indices = new Dictionary<Variable, int>();
10350+
for (var i = 0; i < variables.Count; i++)
10351+
indices[variables[i]] = i;
10352+
var all = Enumerable.Range(0, variables.Count).ToList();
10353+
var at = indices[x];
10354+
var rest = all.Where(i => i != at).ToList();
10355+
var parts = new List<(MultivariatePolynomial Poly, int Power)>();
10356+
var degree = 0;
10357+
foreach (var (@base, power) in written)
10358+
{
10359+
if (MultivariatePolynomial.TryParse(@base, indices) is not { } poly || poly.DegreeIn(at) == 0)
10360+
return null;
10361+
degree += poly.DegreeIn(at);
10362+
parts.Add((poly, power));
10363+
}
10364+
if (degree > 12 || parts.Count < 2 && parts[0].Poly.DegreeIn(at) < 2)
10365+
return null;
10366+
10367+
// A power of x is what a factor shares with x^k beside it, or with its own derivative
10368+
// where its lowest term is x^k, and taking it out is the content's, before this. Split
10369+
// here, `b x^2 + c x^4` came apart as `x^2 (b/c + x^2) c`, and over that monic quadratic
10370+
// with a symbol below its bar Rubi's `x^12 (A + B x^2)/(b x^2 + c x^4)^3` had an answer
10371+
// of four million characters, where with the content taken out it has seven thousand.
10372+
bool IsAPowerOfX(MultivariatePolynomial poly)
10373+
=> poly.Monomials.Take(2).Count() == 1
10374+
&& rest.All(variable => MultivariatePolynomial.PowerOfMonomial(poly.Monomials.First(), variable) == 0);
10375+
10376+
// What is free of x in a factor goes in front, to the factor's power.
10377+
void Front(MultivariatePolynomial free, int power)
10378+
{
10379+
if (free.IsConstant && free.SameAs(MultivariatePolynomial.One(free.VariableCount)))
10380+
return;
10381+
var front = free.ToEntity(variables);
10382+
var raised = power == 1 ? front : MathS.Pow(front, power);
10383+
constant = constant == Number.Integer.One ? raised : constant * raised;
10384+
}
10385+
bool Keep(MultivariatePolynomial poly, int power)
10386+
{
10387+
// A part free of x goes in front whole, its sign with it: a divisor is found up to
10388+
// a unit, so taking -(b u^2 - 1)^2 apart leaves a part -1, whose content is 1.
10389+
if (poly.DegreeIn(at) == 0)
10390+
{
10391+
Front(poly, power);
10392+
return true;
10393+
}
10394+
if (Functions.PolynomialGcd.ContentIn(poly, at, rest, 0) is not { } content || poly.DivideExact(content) is not { } primitive)
10395+
return false;
10396+
Front(content, power);
10397+
parts.Add((primitive, power));
10398+
return true;
10399+
}
10400+
10401+
var changed = false;
10402+
for (var round = 0; round < 64; round++)
10403+
{
10404+
var split = false;
10405+
for (var i = 0; i < parts.Count && !split; i++)
10406+
{
10407+
var (poly, power) = parts[i];
10408+
// A factor repeated inside: what it shares with its derivative.
10409+
if (poly.DegreeIn(at) >= 2 && Functions.PolynomialGcd.Gcd(poly, poly.DerivativeIn(at), all, 0) is { } repeated
10410+
&& repeated.DegreeIn(at) >= 1 && !IsAPowerOfX(repeated) && poly.DivideExact(repeated) is { } remaining)
10411+
{
10412+
parts.RemoveAt(i);
10413+
if (!Keep(repeated, power) || !Keep(remaining, power))
10414+
return null;
10415+
split = true;
10416+
break;
10417+
}
10418+
// Two factors that share one.
10419+
for (var j = i + 1; j < parts.Count; j++)
10420+
{
10421+
if (Functions.PolynomialGcd.Gcd(poly, parts[j].Poly, all, 0) is not { } common || common.DegreeIn(at) == 0 || IsAPowerOfX(common))
10422+
continue;
10423+
if (poly.DivideExact(common) is not { } left || parts[j].Poly.DivideExact(common) is not { } right)
10424+
return null;
10425+
var other = parts[j].Power;
10426+
parts.RemoveAt(j);
10427+
parts.RemoveAt(i);
10428+
if (!Keep(common, power + other) || !Keep(left, power) || !Keep(right, other))
10429+
return null;
10430+
split = true;
10431+
break;
10432+
}
10433+
}
10434+
if (!split)
10435+
break;
10436+
changed = true;
10437+
}
10438+
10439+
// A factor that shares one with the numerator, taken apart the same way:
10440+
// `(d + e x)^6/(a d e + (c d^2 + a e^2) x + c d e x^2)^4` is `(d + e x)^2/(a e + c d x)^4`.
10441+
var top = MultivariatePolynomial.TryParse(numerator, indices);
10442+
for (var round = 0; top is not null && round < 16; round++)
10443+
{
10444+
var split = false;
10445+
for (var i = 0; i < parts.Count; i++)
10446+
{
10447+
var (poly, power) = parts[i];
10448+
if (poly.DegreeIn(at) < 2 || Functions.PolynomialGcd.Gcd(top, poly, all, 0) is not { } common
10449+
|| common.DegreeIn(at) == 0 || IsAPowerOfX(common) || common.DegreeIn(at) == poly.DegreeIn(at)
10450+
|| poly.DivideExact(common) is not { } remaining)
10451+
continue;
10452+
parts.RemoveAt(i);
10453+
if (!Keep(common, power) || !Keep(remaining, power))
10454+
return null;
10455+
split = true;
10456+
break;
10457+
}
10458+
if (!split)
10459+
break;
10460+
changed = true;
10461+
}
10462+
if (!changed)
10463+
return null;
10464+
10465+
// What the numerator shares with the factors, cancelled.
10466+
Entity above = numerator;
10467+
if (top is not null)
10468+
{
10469+
var cancelled = false;
10470+
for (var i = 0; i < parts.Count; i++)
10471+
while (parts[i].Power > 0 && top.DivideExact(parts[i].Poly) is { } fewer)
10472+
{
10473+
top = fewer;
10474+
parts[i] = (parts[i].Poly, parts[i].Power - 1);
10475+
cancelled = true;
10476+
}
10477+
if (cancelled)
10478+
above = top.ToEntity(variables);
10479+
}
10480+
// Each factor monic in x, the leading coefficient to its power in front, which is how
10481+
// the integrator writes a factor: `d + e x` is `e (d/e + x)`, and written the other
10482+
// way the next normalisation took `e` out again and the answer carried `e^(-8) e^8`.
10483+
var monicFactors = new List<Entity>();
10484+
foreach (var (poly, power) in parts)
10485+
{
10486+
if (power == 0)
10487+
continue;
10488+
var lead = poly.LeadingCoefficientIn(at).ToEntity(variables);
10489+
Entity monic = Number.Integer.Zero;
10490+
foreach (var pair in poly.CoefficientsIn(at).OrderBy(pair => pair.Key))
10491+
{
10492+
var coefficient = pair.Key == poly.DegreeIn(at) ? Number.Integer.One : Reduced(pair.Value.ToEntity(variables) / lead);
10493+
var term = pair.Key == 0 ? coefficient : coefficient * (pair.Key == 1 ? x : MathS.Pow(x, pair.Key));
10494+
monic = monic == Number.Integer.Zero ? term : monic + term;
10495+
}
10496+
monicFactors.Add(power == 1 ? monic : MathS.Pow(monic, power));
10497+
var raised = power == 1 ? lead : MathS.Pow(lead, power);
10498+
constant = constant == Number.Integer.One ? raised : constant * raised;
10499+
}
10500+
// The constant in lowest terms: the bars and the contents taken out of the factors
10501+
// and the leading coefficients put back leave `e^(-8) e^8` in it as written.
10502+
Entity below = Reduced(constant);
10503+
foreach (var factor in monicFactors)
10504+
below = below == Number.Integer.One ? factor : below * factor;
10505+
return (above, below);
10506+
10507+
static Entity Reduced(Entity constant)
10508+
=> constant.Vars.Any() ? Functions.PartialFractions.InLowestTermsOverTheSymbols(constant) : constant.InnerSimplified;
10509+
}
10510+
1028310511
/// <summary>
1028410512
/// <paramref name="denominator"/> with every written factor that has rational coefficients
1028510513
/// and splits over the rationals written in its factors, where another written factor

‎Sources/Tests/UnitTests/Calculus/SymbolicFactorsPartialFractionsTest.cs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,35 @@ public void TheCoefficientsAreInLowestTerms(string integrand, int atMost)
278278
public void ProportionalLinearFactorsAreOneFactor(string integrand)
279279
=> DifferentiatesBack(integrand, "x", ("a", 1.7), ("b", 2.3), ("c", 0.4), ("d", 1.1), ("g", 0.7));
280280

281+
/// <summary>
282+
/// Written factors that share a factor, or repeat one inside, are written over what they
283+
/// share, the greatest common divisors taken in every variable: Rubi's 1.2.1.2,
284+
/// <c>(d + e x)^m (a d e + (c d^2 + a e^2) x + c d e x^2)^p</c>, whose quadratic is
285+
/// <c>(d + e x)(a e + c d x)</c>, and the square <c>c d^2 + 2 c d e x + c e^2 x^2</c>
286+
/// beside a power of its linear, or with the numerator sharing it. Each was declined or
287+
/// ran past the corpus's budget.
288+
/// </summary>
289+
[Theory]
290+
[InlineData("1/((d + h*x)*(a*d*h + (c*d^2 + a*h^2)*x + c*d*h*x^2)^2)")]
291+
[InlineData("(d + h*x)^6/(a*d*h + (c*d^2 + a*h^2)*x + c*d*h*x^2)^4")]
292+
[InlineData("1/((d + h*x)^2*(c*d^2 + 2*c*d*h*x + c*h^2*x^2)^3)")]
293+
[InlineData("x^2/((a + b*x)*(a^2 + 2*a*b*x + b^2*x^2))")]
294+
public void FactorsThatShareOneAreWrittenOverIt(string integrand)
295+
=> DifferentiatesBack(integrand, "x", ("a", 1.7), ("b", 2.3), ("c", 0.4), ("d", 1.1), ("h", 0.7));
296+
297+
/// <summary>
298+
/// The negative of a square, <c>-(b x^2 - 1)^2</c>, is the square with its sign in front:
299+
/// a divisor is found up to a unit, and the part the split left was <c>-1</c>. Rubi's
300+
/// 1.1.2.2 <c>x^4/(a + b x^2)^(3/2)</c> is this under <c>t = x/sqrt(a + b x^2)</c>, and was
301+
/// answered with the wrong sign.
302+
/// </summary>
303+
[Theory]
304+
[InlineData("(2*x^2/b - 1/b^2)/(-b^2*x^4 + 2*b*x^2 - 1)")]
305+
[InlineData("x^4/(a + b*x^2)^(3/2)")]
306+
[InlineData("(c + d*x^3)^2/(a + b*x^3)^(4/3)")]
307+
public void TheNegativeOfASquareKeepsItsSign(string integrand)
308+
=> DifferentiatesBack(integrand, "x", ("a", 1.7), ("b", 2.3), ("c", 0.4), ("d", 1.1));
309+
281310
/// <summary>
282311
/// A written power of a linear is integrated as the power it is: expanded,
283312
/// <c>(f/g + x)^2</c> is a quadratic whose discriminant is zero in a spelling the

0 commit comments

Comments
 (0)