Skip to content

Commit 6c94652

Browse files
An exponential or a hyperbolic function over several linears is split into partial fractions (#1640)
e^x/(x (x + 1)) was left unevaluated, where e^x/x and e^x/(x + 1) were each answered with the exponential integral. The rule for an exponential of a linear over a power of a linear read one linear below the bar, and the rule for sinh and cosh, which arrive as sums of exponentials, did the same. The rule for a sine or a cosine already split a quotient over several linears into partial fractions, each term the one-linear question; the exponential and hyperbolic ones do too now, through PartialFractions.TrySplitOverWrittenFactors, which reads symbolic coefficients and checks the split at pinned points before it is used. e^x/(x (x + 1)) is Ei(x) - Ei(x + 1)/e. It is also what by parts leaves of Ei(a + b x)/x^2: e^(a + b x)/((a + b x) x). The polynomial part comes off by long division only where the fraction is not proper already. The rule for a sine or a cosine divided every time, and x over (c + d x)(x - 2) came back as a quotient and a remainder 2 provided not c + d x = 0, which nothing splits: x sin(x)/((c + d x)(x - 2)) was declined where x sin(x)/((x + 3)(x - 2)) was answered. Both rules divide only an improper fraction now. Measured on the Rubi corpus, master at 9193d97 and this change on it, run side by side: family 8 296 -> 302 of 420 (Ei, Shi and Chi of a + b x over x^2 and over x^3), family 6 at twenty a file 412 -> 414 of 472 (cosh(c + d x)/(x (a + b x)) and over x^3 (a + b x)^3), family 2 543 of 650 on both, the independent suites 1756 of 1814 and families 1, 3, 4, 5 and 7 at five a file 682 of 768 on both. 0 wrong everywhere. Run alone, the eight are 0 of 8 on master and 8 of 8 here. The unit tests pass, 14,240, also with the GC heap held to 4 GB, and the performance gate passes on 3b94625d, which is this change on 9193d97: allocation is what the baseline says on all 19 gated benchmarks. ExponentialIntegralIntegrationTest and HyperbolicIntegralIntegrationTest have a row set each over several linears, and TrigonometricIntegralIntegrationTest two rows over symbolic ones, each differentiated back with its parameters pinned. Part of #1501. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2db4278 commit 6c94652

6 files changed

Lines changed: 152 additions & 2 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2315,6 +2315,26 @@ no integrand without one changes: Rubi's independent suites and a sample of its
23152315
| `"Ei(b*x)^2".Integrate("x")` | `Ei * (b * x) ^ 3 / 3 / b + C`, with `Ei` a variable | an antiderivative in `Ei(b x)` and `Ei(2 b x)` |
23162316
| `"x*erf(b*x)^2".Integrate("x")` | `UnrecognizedFunctionParseException`: there is no function `erf` | an antiderivative in `erf(b x)` |
23172317

2318+
### An exponential or a hyperbolic function over several linears is split into partial fractions over them
2319+
2320+
`e^x/(x (x + 1))` was left unevaluated, where `e^x/x` and `e^x/(x + 1)` were each answered with
2321+
the exponential integral: the rule for an exponential of a linear over a power of a linear read
2322+
one linear below the bar, and the rule for `sinh` and `cosh` did the same. The quotient is split
2323+
into partial fractions over its linears now, as the rule for a sine or a cosine already split it,
2324+
and each term is the one-linear question: `e^x/(x (x + 1))` is `Ei(x) - Ei(x + 1)/e`. It is also
2325+
what by parts leaves of `Ei(a + b x)/x^2`, `e^(a + b x)/((a + b x) x)`
2326+
([#1501](https://github.com/asc-community/AngouriMath/issues/1501)). The rule for a sine or a cosine
2327+
declined a polynomial over linears with a symbol among their coefficients, `x sin(x)/((c + d x)(x - 2))`,
2328+
for dividing a fraction that was proper already; neither rule divides one now. Both columns
2329+
measured on a build, `v2.5.0` against this change.
2330+
2331+
| Input | Was (2.5.0) | Now |
2332+
|---|---|---|
2333+
| `"e^x/(x*(x+1))".Integrate("x")` | `integral(e ^ x / (x * (x + 1)), x)` | `Ei(x) + -1 / e * Ei(x + 1) + C` |
2334+
| `"sinh(x)/(x*(x+1))".Integrate("x")` | `integral((e ^ x - e ^ (-x)) / 2 / (x * (x + 1)), x)` | `1/2 * (2 * Shi(x) + ((-1) / e + e) * Chi(x + 1) + ((-1) / e - e) * Shi(x + 1)) + C` |
2335+
| `"x^3*e^(2*x)/((x+1)*(x-2))".Integrate("x")` | `integral(x ^ 3 * e ^ (2 * x) / ((x + 1) * (x - 2)), x)` | `e ^ (2 * x) * (-1/4 + 1/2 * x) + e ^ (2 * x) / 2 + 1/3 * e ^ (-2) * Ei(2 * (x + 1)) + 8/3 * e ^ 4 * Ei(2 * (x - 2)) + C` |
2336+
| `"x*sin(x)/((c+d*x)*(x-2))".Integrate("x")` | `integral(x * sin(x) / ((c + d * x) * (x - 2)), x)` | in `Si` and `Ci` of `(c + d x)/d` and `x - 2` |
2337+
23182338
### An inverse trigonometric function below the bar is integrated to the sine and cosine integrals
23192339

23202340
`1/arcsin(x)` was left unintegrated. Under the substitution that undoes the inverse function,

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

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3859,6 +3859,102 @@ bool Read(Entity factor, int times)
38593859
}
38603860
}
38613861

3862+
/// <summary>
3863+
/// An exponential of a linear, or a sum of them -- which is how <c>sinh</c> and <c>cosh</c>
3864+
/// arrive -- times a polynomial, over two or more linears each to a whole power: split into
3865+
/// partial fractions over the linears, and each term the one-linear question of
3866+
/// <see cref="SolveAnExponentialOfALinearOverAPowerOfALinear"/> and
3867+
/// <see cref="SolveAHyperbolicOfALinearOverAPowerOfALinear"/>. <c>e^x/(x (x + 1))</c> is
3868+
/// <c>e^x/x - e^x/(x + 1)</c>, which is <c>Ei(x) - Ei(x + 1)/e</c>; the trigonometric rule
3869+
/// splits the same way (<see cref="OverAPowerOfALinear"/>). It is also what by parts leaves
3870+
/// of <c>Ei(a + b x)/x^2</c>, <c>e^(a + b x)/((a + b x) x)</c>.
3871+
/// https://github.com/asc-community/AngouriMath/issues/1501
3872+
/// </summary>
3873+
internal static Entity? SolveAnExponentialOverSeveralLinears(Entity expr, Entity.Variable x)
3874+
{
3875+
if (expr is not (Divf or Mulf) || AnExponentialOfALinear(expr, x) is null && !HasASumOfExponentials(expr, x))
3876+
return null;
3877+
Entity constant = Number.Integer.One;
3878+
var linears = new List<(Entity Linear, int Power)>();
3879+
Entity? polynomial = null;
3880+
Entity? exponentials = null;
3881+
foreach (var (factor, underneath) in FactorsOfTheIntegrand(expr))
3882+
{
3883+
if (!factor.ContainsNode(x))
3884+
{
3885+
constant = underneath ? constant / factor : constant * factor;
3886+
continue;
3887+
}
3888+
var (@base, exponent) = factor is Powf(var raised, Number.Integer whole) && whole.EInteger.CanFitInInt32() && !whole.EInteger.IsZero
3889+
? (raised, whole.EInteger.ToInt32Checked()) : (factor, 1);
3890+
if (underneath)
3891+
exponent = -exponent;
3892+
if (exponent < 0 && TreeAnalyzer.TryGetPolyLinear(@base, x, out var slopeOfIt, out _) && !TreeAnalyzer.IsZero(slopeOfIt))
3893+
{
3894+
var at = linears.FindIndex(pair => pair.Linear == @base);
3895+
if (at < 0)
3896+
linears.Add((@base, -exponent));
3897+
else
3898+
linears[at] = (@base, linears[at].Power - exponent);
3899+
continue;
3900+
}
3901+
if (underneath)
3902+
return null;
3903+
if (factor.Nodes.Any(node => node is Powf(var b, var e) && !b.ContainsNode(x) && e.ContainsNode(x)))
3904+
{
3905+
exponentials = exponentials is null ? factor : exponentials * factor;
3906+
continue;
3907+
}
3908+
if (TreeAnalyzer.TryGetPolynomial(factor, x, out var monomials) && monomials.Keys.All(degree => degree.Sign >= 0 && degree.CompareTo(EInteger.FromInt32(12)) <= 0))
3909+
{
3910+
polynomial = polynomial is null ? factor : polynomial * factor;
3911+
continue;
3912+
}
3913+
return null;
3914+
}
3915+
if (exponentials is null || linears.Count < 2 || linears.Count > 4 || linears.Sum(pair => pair.Power) > 12)
3916+
return null;
3917+
// Over each linear by partial fractions, the polynomial part first, since the split is
3918+
// of a proper fraction -- as the trigonometric rule does it. Only where the fraction is
3919+
// not proper already: divided, `x` over `(c + d x)(x - 2)` came back as a quotient and a
3920+
// remainder `2 provided not c + d x = 0`, which nothing splits.
3921+
var denominator = linears.Aggregate((Entity)Number.Integer.One, (product, pair) => product * MathS.Pow(pair.Linear, pair.Power));
3922+
Entity numerator = polynomial ?? Number.Integer.One;
3923+
var terms = new List<Entity>();
3924+
if (numerator.ContainsNode(x)
3925+
&& TreeAnalyzer.TryGetPolynomial(numerator, x, out var monomialsAbove)
3926+
&& monomialsAbove.Keys.Any(degree => degree.CompareTo(EInteger.FromInt32(linears.Sum(pair => pair.Power))) >= 0)
3927+
&& TreeAnalyzer.PolynomialLongDivision(numerator, denominator, genericCase: true, inTermsOf: x) is var (quotient, remainder))
3928+
{
3929+
if (!TreeAnalyzer.IsZero(quotient))
3930+
terms.Add(quotient);
3931+
numerator = remainder is Divf(var left, _) ? left : (numerator - quotient * denominator).Expand().InnerSimplified;
3932+
}
3933+
if (!TreeAnalyzer.IsZero(numerator))
3934+
{
3935+
if (!Functions.PartialFractions.TrySplitOverWrittenFactors(numerator, denominator, x, out var decomposition))
3936+
return null;
3937+
var fractions = decomposition.InnerSimplified;
3938+
if (fractions is Divf(var over, var by) && !by.ContainsNode(x))
3939+
fractions = Sumf.LinearChildren(over).Aggregate((Entity)Number.Integer.Zero, (all, one) => all + one / by);
3940+
terms.AddRange(Sumf.LinearChildren(fractions));
3941+
}
3942+
Entity sum = Number.Integer.Zero;
3943+
foreach (var term in terms)
3944+
{
3945+
// Each term as the quotient the one-linear rules read, or, with no linear left, the
3946+
// elementary product of a polynomial and the exponentials.
3947+
var (above, linear, power) = OverOneLinear(term, x);
3948+
var question = linear is null ? above * exponentials : above * exponentials / MathS.Pow(linear, power);
3949+
if ((SolveAnExponentialOfALinearOverAPowerOfALinear(question, x)
3950+
?? SolveAHyperbolicOfALinearOverAPowerOfALinear(question, x)
3951+
?? Integration.ComputeIndefiniteIntegral(question, x, false)) is not { } integral)
3952+
return null;
3953+
sum += integral;
3954+
}
3955+
return Functions.PartialFractions.Bare((constant * sum).InnerSimplified);
3956+
}
3957+
38623958
/// <summary>Whether a factor of <paramref name="expr"/> is a sum with an exponential of the variable in it.</summary>
38633959
private static bool HasASumOfExponentials(Entity expr, Entity.Variable x) => expr switch
38643960
{
@@ -4542,11 +4638,15 @@ Entity LessTheRoot(Entity root) =>
45424638
? Functions.PartialFractions.Bare((constant * one).InnerSimplified)
45434639
: null;
45444640
// Over each linear by partial fractions, and each term the one-linear question; the
4545-
// polynomial part first, since the split is of a proper fraction.
4641+
// polynomial part first, since the split is of a proper fraction -- and only where it is
4642+
// not proper already, as in SolveAnExponentialOverSeveralLinears.
45464643
var denominator = linears.Aggregate((Entity)Number.Integer.One, (product, pair) => product * MathS.Pow(pair.Linear, pair.Power));
45474644
Entity numerator = polynomial ?? Number.Integer.One;
45484645
var terms = new List<Entity>();
4549-
if (numerator.ContainsNode(x) && TreeAnalyzer.PolynomialLongDivision(numerator, denominator, genericCase: true, inTermsOf: x) is var (quotient, remainder))
4646+
if (numerator.ContainsNode(x)
4647+
&& TreeAnalyzer.TryGetPolynomial(numerator, x, out var monomialsAbove)
4648+
&& monomialsAbove.Keys.Any(degree => degree.CompareTo(EInteger.FromInt32(linears.Sum(pair => pair.Power))) >= 0)
4649+
&& TreeAnalyzer.PolynomialLongDivision(numerator, denominator, genericCase: true, inTermsOf: x) is var (quotient, remainder))
45504650
{
45514651
if (!TreeAnalyzer.IsZero(quotient))
45524652
terms.Add(quotient);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
643643
if ((answer = IndefiniteIntegralSolver.SolveAnExponentialOfALinearOverAPowerOfALinear(expr, x)) is { }) return answer;
644644
// And a sum of exponentials, which is how sinh and cosh arrive, onto Shi and Chi.
645645
if ((answer = IndefiniteIntegralSolver.SolveAHyperbolicOfALinearOverAPowerOfALinear(expr, x)) is { }) return answer;
646+
// And either over several linears, split into partial fractions over them first, as the
647+
// trigonometric rule below splits.
648+
if ((answer = IndefiniteIntegralSolver.SolveAnExponentialOverSeveralLinears(expr, x)) is { }) return answer;
646649
// Sines and cosines of a linear over a power of a linear, onto Si and Ci under u = the
647650
// linear, where the search would take the quotient by parts without end.
648651
if ((answer = IndefiniteIntegralSolver.SolveATrigonometricOfALinearOverAPowerOfALinear(expr, x)) is { }) return answer;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ private static readonly (string, string)[] Pins =
6262
public void AnExponentialOfALinearOverAPowerOfALinear(string integrand)
6363
=> DifferentiatesBack(integrand, AroundZero, Pins);
6464

65+
/// <summary>
66+
/// Over several linears, the quotient is split into partial fractions over them first, and
67+
/// each term is the question above: <c>e^x/(x (x + 1))</c> is <c>Ei(x) - Ei(x + 1)/e</c>. The
68+
/// last row is what by parts leaves of <c>Ei(a + b x)/x^2</c>.
69+
/// </summary>
70+
[Theory]
71+
[InlineData("e^x/(x*(x + 1))")]
72+
[InlineData("e^x/(x*(x - a))")]
73+
[InlineData("e^x/(x^2*(x + 1))")]
74+
[InlineData("x^3*e^(2*x)/((x + 1)*(x - 2))")]
75+
[InlineData("e^(a + b*x)/((a + b*x)*x)")]
76+
public void AnExponentialOfALinearOverSeveralLinears(string integrand)
77+
=> DifferentiatesBack(integrand, AroundZero, Pins);
78+
6579
/// <summary>
6680
/// The Gaussian's odd negative moments end at <c>int e^(A x^2)/x = Ei(A x^2)/2</c>, and
6781
/// <c>x e^(-1/x^2)</c> is one of them under <c>u = 1/x</c>.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ private static readonly (string, string)[] Pins =
6767
public void ASumOfExponentialsOverAPowerOfALinear(string integrand)
6868
=> DifferentiatesBack(integrand, AroundZero, Pins);
6969

70+
/// <summary>
71+
/// And over several linears, split into partial fractions over them first, each term the
72+
/// question above.
73+
/// </summary>
74+
[Theory]
75+
[InlineData("sinh(x)/(x*(x + 1))")]
76+
[InlineData("cosh(2*x)/(x*(x + 3))")]
77+
[InlineData("x*sinh(a + b*x)/((c + d*x)*(x - 2))")]
78+
public void ASumOfExponentialsOverSeveralLinears(string integrand)
79+
=> DifferentiatesBack(integrand, AroundZero, Pins);
80+
7081
/// <summary>
7182
/// An inverse hyperbolic tangent below the bar, beside a whole power of <c>1 - a^2 x^2</c>:
7283
/// under <c>x = tanh(u)/a</c> it is a polynomial in <c>sinh(u)</c> and <c>cosh(u)</c> over

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public void APowerOfASineOrACosineOverAPowerOfALinear(string integrand)
8888
[InlineData("cos(2*x + 1)/((x - 1)*(x + 2)^2)")]
8989
[InlineData("sin(c + d*x)/(x^2*(a + b*x))")]
9090
[InlineData("x^3*sin(x)^2/((x + 1)*(x + 3))")]
91+
[InlineData("x*sin(x)/((c + d*x)*(x - 2))")]
92+
[InlineData("x*cos(a + b*x)/((c + d*x)*(x + 1))")]
9193
public void OverSeveralLinears(string integrand)
9294
=> DifferentiatesBack(integrand, AroundZero, Pins);
9395

0 commit comments

Comments
 (0)