Skip to content

Commit 2db4278

Browse files
A special function beside an elementary factor is integrated by parts, and so is its square (#1639)
x Si(b x) sin(b x) was left unevaluated where Si(b x) sin(b x) was not. By parts it is Si(b x) against x sin(b x), and the integral of that factor was taken with parts off, which x sin(b x) needs. Beside a special function of a linear argument, a polynomial times something else is integrated by the polynomial's parts now, which end in as many steps as its degree, and what is left, the special function's derivative times polynomials and elementary functions, is given parts in turn. A square of a special function of b x is two rounds of parts, and the remainder of the first comes back as one product over a sum, (b x Ei(b x) - e^(b x)) e^(b x)/(b x) for Ei(b x)^2, that no rule reads whole, where its terms e^(b x) Ei(b x) and e^(2 b x)/(b x) are each answered when asked. So where the remainder after a special function is not answered, its terms are asked one at a time: the ordinary way first, and then as questions of their own. That is only at the top, where such asking reaches. It is done once, since a term asked so is at the top itself, and the same asking inside it lifted the search again with the depth reset each time: together with a rule that wrote a + b x as t, x Ei(a + b x)^2 grew a test host to twelve gigabytes. A decline that the once-only flag caused is not cached (Integration.DeclinedForItsScope), as a decline that ran out of depth is not. It covers a multiple of x only: of a + b x the remainder divides by the linear, its terms are the exponentials a hyperbolic function is written as, and the decline of x Shi(a + b x)^2 went from a second to past twenty asking them. Rubi's family 8, #1501's tranche of 420: 296 -> 367 solved, 0 wrong, 0 timeout, against master with #1638. The 71 gains are 0 of 71 on master and 71 of 71 here run alone, and the 53 still declined take 103 s against 117 s together, median ratio 1.00, the slowest change being x^2 Ci(a + b x) cos(a + b x), 0.25 s to 2 s. Nothing else moves: the independent suites are 1756 of 1814 and families 1 to 7 at five a file 813 of 912 on both, 0 wrong everywhere, and what is declined in both costs what it did (11.9 s against 11.5 s, 98.1 s against 98.9 s). The unit tests pass, 14,247, and the performance gate passes on ee5cb79e, whose tree is this commit's but for this entry in BREAKING-CHANGES.md: allocation is what the baseline says on all 19 gated benchmarks. SpecialFunctionsByPartsTest: six rows beside a power and the elementary factor of the derivative, eleven squares, each differentiated back with its parameters pinned. Part of #1501. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9193d97 commit 2db4278

4 files changed

Lines changed: 163 additions & 1 deletion

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,6 +2295,26 @@ no integrand without one changes: Rubi's independent suites and a sample of its
22952295
| `"e^(b*x)*Ei(b*x)/x".Integrate("x")` | `Ei * e ^ (b * x) + C`, with `Ei` a variable | `Ei(b * x) ^ 2 / 2 + C` |
22962296
| `"Si(b*x)*sin(b*x)/x".Integrate("x")` | `Si * -cos(b * x) + C`, with `Si` a variable | `Si(b * x) ^ 2 / 2 + C` |
22972297

2298+
### A square of a special function, and one beside a power of `x` and its elementary derivative, are integrated by parts
2299+
2300+
`x Si(b x) sin(b x)` was left unevaluated where `Si(b x) sin(b x)` was not: by parts it is
2301+
`Si(b x)` against `x sin(b x)`, whose integral needs parts of its own, and the integral of that
2302+
factor was taken with parts off. It is integrated by the polynomial's parts now, beside a special
2303+
function of a linear argument, and what is left, `sin(b x)/x` times sines and cosines, by parts in
2304+
turn. A square of one of `b x` is two rounds of parts, and what the first round leaves comes back
2305+
as one product over a sum -- `(b x Ei(b x) - e^(b x)) e^(b x)/(b x)` for `Ei(b x)^2` -- that no rule
2306+
reads whole: its terms are asked one at a time now
2307+
([#1501](https://github.com/asc-community/AngouriMath/issues/1501)). An integrand holding one of
2308+
these functions had no reading in 2.5.0, which the entries for the functions themselves record, and
2309+
no integrand without one changes: Rubi's independent suites and a sample of its families 1 to 7,
2310+
2726 problems, are answered alone as they were.
2311+
2312+
| Input | Was (2.5.0) | Now |
2313+
|---|---|---|
2314+
| `"x*Si(b*x)*sin(b*x)".Integrate("x")` | a polynomial in a variable `Si` | an antiderivative in `Si(b x)`, `Ci(2 b x)` and `ln(x)` |
2315+
| `"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)` |
2316+
| `"x*erf(b*x)^2".Integrate("x")` | `UnrecognizedFunctionParseException`: there is no function `erf` | an antiderivative in `erf(b x)` |
2317+
22982318
### An inverse trigonometric function below the bar is integrated to the sine and cosine integrals
22992319

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

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

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,19 @@ static Entity WithTheConstantMatchedTo(Entity antiderivative, Entity derivativeO
17221722
// a coefficient's sign whose conditions are decided, `0 = 0`, and a remainder
17231723
// holding that piecewise is read by nothing.
17241724
var integralOfU = Integration.ComputeIndefiniteIntegral(u, x, false)?.InnerSimplified;
1725+
// Beside a special function of a linear argument, a polynomial times something
1726+
// else is integrated by the polynomial's parts, which end in as many steps as its
1727+
// degree: `x sin(b x)` beside `Si(b x)`, which nothing answers with parts off.
1728+
// https://github.com/asc-community/AngouriMath/issues/1501
1729+
var besideASpecialFunction = IsASpecialFunctionOfALinearOrAPowerOfOne(v, x);
1730+
var byThePolynomialsParts = false;
1731+
if (integralOfU is null && besideASpecialFunction
1732+
&& ThePolynomialFactor(u, x) is var (polynomialBeside, restBeside)
1733+
&& polynomialBeside is not null && restBeside is not null)
1734+
{
1735+
integralOfU = IntegrateByPartsPolynomial(polynomialBeside, restBeside, x)?.InnerSimplified;
1736+
byThePolynomialsParts = integralOfU is not null;
1737+
}
17251738
if (integralOfU is null) return null;
17261739

17271740
// Differentiate v
@@ -1780,7 +1793,12 @@ static Entity WithTheConstantMatchedTo(Entity antiderivative, Entity derivativeO
17801793
// https://github.com/asc-community/AngouriMath/issues/1501
17811794
var partsOnTheRemainder = remaining.Nodes.Count() < wholeSize
17821795
|| (remainingPower >= 1 && remainingPower < wholePower)
1783-
|| IsASpecialFunctionOfALinear(v, x) && MathS.TryPolynomial(u, x, out _);
1796+
|| IsASpecialFunctionOfALinear(v, x) && MathS.TryPolynomial(u, x, out _)
1797+
// And where what was integrated beside it was a polynomial times an
1798+
// elementary function, whose integral the polynomial's parts wrote: the
1799+
// remainder is then the special function's derivative times polynomials and
1800+
// elementary functions, and parts on it descend on their degrees.
1801+
|| byThePolynomialsParts;
17841802
// Spelled as the product its own next step reads, where there is one. `Simplify`
17851803
// writes `2 arctan(x)/(1 + x^2) * x^2/2` as `arctan(x) x^2/(x^2 + 1)`, a
17861804
// quotient, and this rule runs on a product. `x*arctan(x)^2` is answered by
@@ -1804,6 +1822,46 @@ static Entity WithTheConstantMatchedTo(Entity antiderivative, Entity derivativeO
18041822
// search above it carry on, thirty seconds where the gate leaves it at one.
18051823
var remainingIntegral = (Integration.AnsweringTheQuestionAsked ? SolveByEulerSubstitution(remaining, x) : null)
18061824
?? Integration.ComputeIndefiniteIntegral(remaining, x, partsOnTheRemainder);
1825+
// After a special function, or a power of one, the remainder is its derivative
1826+
// times what was integrated, written as one product over a sum: for `Ei(b x)^2`,
1827+
// `(b x Ei(b x) - e^(b x)) e^(b x)/(b x)`, which no rule reads, where its two
1828+
// terms are `e^(b x) Ei(b x)` and `e^(2 b x)/(b x)`, each answered when asked.
1829+
// So its terms are asked, each as a question of its own. From the top only,
1830+
// which is where that asking reaches, and once: a term asked so is at the top
1831+
// itself, and its own remainder asked the same way would lift the search again,
1832+
// each level with the depth reset. And of a multiple of x only: of `a + b x`, the
1833+
// remainder is over the linear, and its terms are the exponentials a hyperbolic
1834+
// function is written as, each a harder question than the whole -- the decline of
1835+
// `x Shi(a + b x)^2` went from a second to past twenty asking them.
1836+
// https://github.com/asc-community/AngouriMath/issues/1501
1837+
if (remainingIntegral is null && besideASpecialFunction && OfAMultipleOfTheVariable(v, x)
1838+
&& Integration.AnsweringTheQuestionAsked && !askingTheTermsOfARemainder)
1839+
{
1840+
var terms = remaining is Divf(var remainingAbove, var remainingBelow)
1841+
? DistributedOverTheSum(remainingAbove).Select(term => term / remainingBelow).ToList()
1842+
: DistributedOverTheSum(remaining);
1843+
askingTheTermsOfARemainder = true;
1844+
try
1845+
{
1846+
foreach (var term in terms)
1847+
{
1848+
if ((Integration.ComputeIndefiniteIntegral(term, x, integrateByParts: true)
1849+
?? Integration.ComputeAsAQuestionOfItsOwn(term, x, integrateByParts: true)) is not { } termIntegral)
1850+
{
1851+
remainingIntegral = null;
1852+
break;
1853+
}
1854+
remainingIntegral = remainingIntegral is null ? termIntegral : remainingIntegral + termIntegral;
1855+
}
1856+
}
1857+
finally
1858+
{
1859+
askingTheTermsOfARemainder = false;
1860+
}
1861+
}
1862+
else if (remainingIntegral is null && besideASpecialFunction && OfAMultipleOfTheVariable(v, x)
1863+
&& Integration.AnsweringTheQuestionAsked)
1864+
Integration.DeclinedForItsScope();
18071865
if (remainingIntegral is null) return null;
18081866

18091867
return v * integralOfU - remainingIntegral;
@@ -20155,10 +20213,49 @@ Entity Term(Entity? sum, Entity coefficient, int power)
2015520213
/// <a href="https://github.com/asc-community/AngouriMath/issues/1501">#1501</a>: the error
2015620214
/// functions and the exponential, logarithmic, sine, cosine and hyperbolic integrals.
2015720215
/// </summary>
20216+
/// <summary>
20217+
/// Set while the terms of a remainder after a special function are asked as questions of
20218+
/// their own, so that the asking is not nested; see <c>TryIntegrateByPartsOnce</c>.
20219+
/// </summary>
20220+
[System.ThreadStatic] private static bool askingTheTermsOfARemainder;
20221+
2015820222
private static bool IsASpecialFunction(Entity node)
2015920223
=> node is Entity.Erff or Entity.Erfcf or Entity.Erfif or Entity.Eif or Entity.Lif
2016020224
or Entity.Sif or Entity.Cif or Entity.Shif or Entity.Chif;
2016120225

20226+
/// <summary>
20227+
/// A special function of a linear argument, as <see cref="IsASpecialFunctionOfALinear"/>
20228+
/// reads one, or a positive whole power of one: <c>Si(b x)^2</c>.
20229+
/// </summary>
20230+
private static bool IsASpecialFunctionOfALinearOrAPowerOfOne(Entity factor, Variable x)
20231+
=> IsASpecialFunctionOfALinear(factor, x)
20232+
|| factor is Powf(var @base, Number.Integer power) && power.EInteger.Sign > 0 && IsASpecialFunctionOfALinear(@base, x);
20233+
20234+
/// <summary>
20235+
/// Whether the special function in <paramref name="factor"/>, or in the base of its power,
20236+
/// is of a multiple of <paramref name="x"/>, <c>b x</c>, with no offset.
20237+
/// </summary>
20238+
private static bool OfAMultipleOfTheVariable(Entity factor, Variable x)
20239+
=> (factor is Powf(var @base, _) ? @base : factor).DirectChildren.FirstOrDefault() is { } argument
20240+
&& TreeAnalyzer.TryGetPolyLinear(argument, x, out _, out var offset) && TreeAnalyzer.IsZero(offset);
20241+
20242+
/// <summary>
20243+
/// The factors of <paramref name="expr"/> that are polynomials in <paramref name="x"/> of
20244+
/// positive degree, against the rest: <c>x sin(b x)</c> is <c>x</c> and <c>sin(b x)</c>.
20245+
/// Both halves <see langword="null"/> where either would be empty.
20246+
/// </summary>
20247+
private static (Entity? Polynomial, Entity? Others) ThePolynomialFactor(Entity expr, Variable x)
20248+
{
20249+
Entity? polynomial = null;
20250+
Entity? rest = null;
20251+
foreach (var factor in Mulf.LinearChildren(expr))
20252+
if (factor.ContainsNode(x) && MathS.TryPolynomial(factor, x, out _))
20253+
polynomial = polynomial is null ? factor : polynomial * factor;
20254+
else
20255+
rest = rest is null ? factor : rest * factor;
20256+
return polynomial is null || rest is null || !rest.ContainsNode(x) ? (null, null) : (polynomial, rest);
20257+
}
20258+
2016220259
/// <summary>
2016320260
/// Whether <paramref name="expr"/> has what the derivative of the special function
2016420261
/// <paramref name="special"/> is made of: an exponential of a quadratic in

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,13 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
412412
/// </summary>
413413
[System.ThreadStatic] private static bool descentTruncated;
414414

415+
/// <summary>
416+
/// Marks what is being worked out as declined for the scope it was asked in rather than
417+
/// for its mathematics, as running out of descent is, so that the decline is not
418+
/// remembered: the same question asked where that scope does not bind may be answered.
419+
/// </summary>
420+
internal static void DeclinedForItsScope() => descentTruncated = true;
421+
415422
/// <summary>
416423
/// The integrals this thread is part-way through, so that asking for one again while it is
417424
/// still being worked out is recognised as a cycle rather than followed round again.

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,43 @@ public void TheLogarithmicIntegralIsByParts(string integrand)
9595
[InlineData("x^3*Chi(b*x)")]
9696
public void APowerTimesASpecialFunctionIsByParts(string integrand)
9797
=> DifferentiatesBack(integrand, Parameters);
98+
99+
/// <summary>
100+
/// Beside a power of <c>x</c> times the elementary function its derivative is made of, the
101+
/// special function is still the factor differentiated: <c>x sin(b x)</c> is integrated by
102+
/// its polynomial's parts, and what is left, <c>sin(b x)/x</c> times that, is products of
103+
/// sines and cosines over powers of <c>x</c>.
104+
/// </summary>
105+
[Theory]
106+
[InlineData("x*Si(b*x)*sin(b*x)")]
107+
[InlineData("x^3*Si(b*x)*sin(b*x)")]
108+
[InlineData("x^2*Ci(b*x)*cos(b*x)")]
109+
[InlineData("x*Ci(b*x)*sin(b*x)")]
110+
[InlineData("x*Si(a + b*x)*sin(a + b*x)")]
111+
[InlineData("x*Si(c + d*x)*sin(a + b*x)")]
112+
public void BesideAPowerAndTheElementaryFactorOfItsDerivative(string integrand)
113+
=> DifferentiatesBack(integrand, Parameters);
114+
115+
/// <summary>
116+
/// A square of a special function of <c>b x</c> is two rounds of parts: the first against
117+
/// the power of <c>x</c> leaves the special function once, beside its derivative, and that
118+
/// is the case above or a substitution. The remainder of a round is asked term by term,
119+
/// since it comes back as one product over a sum -- <c>(b x Ei(b x) - e^(b x)) e^(b x)/(b x)</c>
120+
/// for <c>Ei(b x)^2</c> -- that no rule reads whole.
121+
/// </summary>
122+
[Theory]
123+
[InlineData("Ei(b*x)^2")]
124+
[InlineData("x*Ei(b*x)^2")]
125+
[InlineData("x^2*Ei(b*x)^2")]
126+
[InlineData("x*Si(b*x)^2")]
127+
[InlineData("Ci(b*x)^2")]
128+
[InlineData("x*Ci(b*x)^2")]
129+
[InlineData("x^2*erf(b*x)^2")]
130+
[InlineData("x*erfc(b*x)^2")]
131+
[InlineData("erfi(b*x)^2/x^3")]
132+
[InlineData("x*Shi(b*x)^2")]
133+
[InlineData("Chi(b*x)^2")]
134+
public void ASquareIsTwoRoundsOfParts(string integrand)
135+
=> DifferentiatesBack(integrand, Parameters);
98136
}
99137
}

0 commit comments

Comments
 (0)