Skip to content

Commit fac6ce1

Browse files
The special functions integrate by parts (#1631)
None of the nine special functions was a factor integration by parts would differentiate, so a power of x beside one had no route: x erf(b x), x^2 Ei(b x) and x Si(b x) were all left unevaluated, and so were Ei, li, Si, Ci, Shi and Chi of a linear argument on their own. Each has an elementary derivative, which is what puts the logarithm and the inverse trigonometric functions first in LIATE, and they stand beside them now: against a power of x the function is differentiated, and what is left is the power times e^(-u^2), e^u/u, sin(u)/u and the like, which the rules answer. Alone, each is integrated against 1, as erf already was: int Ei(u) = u Ei(u) - e^u, int li(u) = u li(u) - Ei(2 ln u), int Si(u) = u Si(u) + cos(u), int Ci(u) = u Ci(u) - sin(u), int Shi(u) = u Shi(u) - cosh(u) and int Chi(u) = u Chi(u) - sinh(u), each over the rate. After Si or Ci the remainder x^m sin(b x) needs parts again, and with more nodes than x^m Si(b x) and no factor left to differentiate, the measure that bounds the descent declined it. Against a polynomial the next steps descend on its degree, so parts are allowed on that remainder there. Allowed against anything, they took erf(b x)^2 from a decline in a third of a second to a timeout. Rubi's family 8, whose statable files are #1501's tranche: 8.1 from 9 to 105 of 177, 8.3 from 2 to 34 of 47, 8.4 from 0 to 44 of 98 and 8.5 from 18 to 75 of 98, 29 to 258 of 420 in all, with 0 wrong and 0 timeouts on master (371b38e) and here. Families 0 to 7, side by side with master: family 0 is 1753 of 1814 on both, row for row, and the families sample goes from 810 to 811 of 912, 0 wrong on both. The move is 3.1.5:213, (a + b ln(c x^n))/(x^2 (d + e ln(f x^m))), which master declines in 5.0 s alone and this answers in 0.9 s. SpecialFunctionsByPartsTest has 24 rows from Rubi's 8.1, 8.3, 8.4 and 8.5, each differentiated back with its parameters pinned. Part of #1501. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3cfecdd commit fac6ce1

4 files changed

Lines changed: 195 additions & 1 deletion

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,6 +2199,20 @@ multiple of it, `(d - c^2 d x^2)^(3/2)`, is written over it the same way, with
21992199
Rubi's 5.1.4, 5.1.5, 5.2.4 and 5.2.5, all 504 problems that count: 405 to 463, no row lost, 77
22002200
timeouts to 19.
22012201

2202+
### The special functions are integrated by parts, and so is what leads to one
2203+
2204+
Integration by parts differentiates each of the nine special functions now, where it meets one
2205+
beside a power of `x`, as it does a logarithm or an inverse trigonometric function: each has an
2206+
elementary derivative. `Ei`, `li`, `Si`, `Ci`, `Shi` and `Chi` of a linear argument are integrated
2207+
alone, against 1, as `erf` already was
2208+
([#1501](https://github.com/asc-community/AngouriMath/issues/1501)). An integrand holding one of
2209+
these functions had no reading in 2.5.0, which the entries for the functions themselves record. An
2210+
elementary integrand whose antiderivative is reached through one is answered where it was not:
2211+
2212+
| Input | Was (2.5.0) | Now |
2213+
|---|---|---|
2214+
| `"(a+b*ln(c*x^n))/(x^2*(d+e*ln(f*x^m)))".Integrate("x")`, Rubi's 3.1.5 row 213 | `integral(...)` | an antiderivative in `Ei`, provided `f > 0` and `e^d f^e > 0` |
2215+
22022216
### An inverse trigonometric function below the bar is integrated to the sine and cosine integrals
22032217

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

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,13 @@ private static Entity IntegrateAPowerOfTheVariable(Entity @base, Entity power, E
15951595
/// gives an antiderivative holding <c>x*atan(x)</c> again, which is where the search went
15961596
/// instead and why it was left unevaluated.
15971597
/// </para>
1598+
/// <para>
1599+
/// The special functions belong here for the same reason, since each has an elementary
1600+
/// derivative: <c>x Ei(b x)</c> leaves <c>x e^(b x)/2</c> after one step, and
1601+
/// <c>x^2 Si(b x)</c> leaves <c>x^2 sin(b x)/3</c>, where integrating the special function
1602+
/// first gives an antiderivative that holds it again.
1603+
/// https://github.com/asc-community/AngouriMath/issues/1501
1604+
/// </para>
15981605
/// https://github.com/asc-community/AngouriMath/issues/718
15991606
/// </remarks>
16001607
private static bool IsDifferentiatedBeforeAPolynomial(Entity factor)
@@ -1603,6 +1610,8 @@ private static bool IsDifferentiatedBeforeAPolynomial(Entity factor)
16031610
Logf or Entity.Arcsinf or Entity.Arccosf
16041611
or Entity.Arctanf or Entity.Arccotanf
16051612
or Entity.Arcsecantf or Entity.Arccosecantf => true,
1613+
Entity.Erff or Entity.Erfcf or Entity.Erfif or Entity.Eif or Entity.Lif
1614+
or Entity.Sif or Entity.Cif or Entity.Shif or Entity.Chif => true,
16061615
// And a whole power of one, which is the same function for this purpose:
16071616
// differentiating ln(x)^2 gives 2ln(x)/x, whose x cancels against the integrated
16081617
// polynomial exactly as ln(x)'s does, leaving x*ln(x) -- one step simpler, and
@@ -1616,6 +1625,31 @@ or Entity.Arctanf or Entity.Arccotanf
16161625
_ => false
16171626
};
16181627

1628+
/// <summary>
1629+
/// Whether <paramref name="factor"/> is a special function of an argument linear in
1630+
/// <paramref name="x"/>, other than the logarithmic integral: differentiated beside a
1631+
/// power of <c>x</c>, it leaves that power times <c>sin(u)</c>, <c>e^u</c> or the like,
1632+
/// which parts against the power answer in as many steps as its degree.
1633+
/// </summary>
1634+
/// <remarks>
1635+
/// Not <c>li</c>: its derivative <c>1/ln(u)</c> leaves <c>x^m/ln(a + b x)</c>, which the
1636+
/// exponential integral's rule answers, and parts there would integrate the reciprocal of
1637+
/// the logarithm back into <c>li</c>.
1638+
/// </remarks>
1639+
private static bool IsASpecialFunctionOfALinear(Entity factor, Variable x)
1640+
=> (factor switch
1641+
{
1642+
Entity.Erff(var u) => u,
1643+
Entity.Erfcf(var u) => u,
1644+
Entity.Erfif(var u) => u,
1645+
Entity.Eif(var u) => u,
1646+
Entity.Sif(var u) => u,
1647+
Entity.Cif(var u) => u,
1648+
Entity.Shif(var u) => u,
1649+
Entity.Chif(var u) => u,
1650+
_ => null
1651+
}) is { } argument && TreeAnalyzer.TryGetPolyLinear(argument, x, out _, out _);
1652+
16191653
internal static Entity? SolveIntegratingByParts(Entity expr, Entity.Variable x)
16201654
{
16211655
// The measure the nested call below decreases on. Read once, since every step
@@ -1735,8 +1769,19 @@ static Entity WithTheConstantMatchedTo(Entity antiderivative, Entity derivativeO
17351769
// is the runaway the node bound exists for: measured at thirty seconds for a
17361770
// decline, where the node bound alone declined it in one.
17371771
var remainingPower = HighestDifferentiatedPower(remaining);
1772+
// And after a special function of a linear argument, whose derivative leaves the
1773+
// power of x beside sin(u), e^u and the like: `x Si(b x)` leaves `x sin(b x)/2`,
1774+
// which parts against the power answer in as many steps as its degree. It has
1775+
// more nodes than `x Si(b x)` and no factor left to differentiate, so the measure
1776+
// alone declined it, and with it every power of x beside Si or Ci.
1777+
//
1778+
// Only against a polynomial, which is what makes the next steps a descent on its
1779+
// degree. Allowed against anything, it took erf(b x)^2, whose second step is
1780+
// against x e^(-b^2 x^2), from a decline in a third of a second to a timeout.
1781+
// https://github.com/asc-community/AngouriMath/issues/1501
17381782
var partsOnTheRemainder = remaining.Nodes.Count() < wholeSize
1739-
|| (remainingPower >= 1 && remainingPower < wholePower);
1783+
|| (remainingPower >= 1 && remainingPower < wholePower)
1784+
|| IsASpecialFunctionOfALinear(v, x) && MathS.TryPolynomial(u, x, out _);
17401785
// Spelled as the product its own next step reads, where there is one. `Simplify`
17411786
// writes `2 arctan(x)/(1 + x^2) * x^2/2` as `arctan(x) x^2/(x^2 + 1)`, a
17421787
// quotient, and this rule runs on a product. `x*arctan(x)^2` is answered by

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ internal static Entity AntiderivativeLog(Entity arg)
6161
Entity.Erff(var arg) => arg,
6262
Entity.Erfcf(var arg) => arg,
6363
Entity.Erfif(var arg) => arg,
64+
Entity.Eif(var arg) => arg,
65+
Entity.Lif(var arg) => arg,
66+
Entity.Sif(var arg) => arg,
67+
Entity.Cif(var arg) => arg,
68+
Entity.Shif(var arg) => arg,
69+
Entity.Chif(var arg) => arg,
6470
Entity.Arcsinf(var arg) => arg,
6571
Entity.Arccosf(var arg) => arg,
6672
Entity.Arctanf(var arg) => arg,
@@ -344,6 +350,36 @@ _ when GaussianMoment(expr, x) is { } moment => moment,
344350
TreeAnalyzer.TryGetPolyLinear(arg, x, out var a, out _) =>
345351
(arg * MathS.Erfi(arg) - MathS.Pow(MathS.e, MathS.Sqr(arg)) / MathS.Sqrt(MathS.pi)) / a,
346352

353+
// And the exponential, logarithmic, trigonometric and hyperbolic integrals, by parts
354+
// against 1 the same way: each derivative is elementary and cancels the u the
355+
// integrated 1 leaves beside it. int Ei(u) = u Ei(u) - e^u, int li(u) = u li(u) -
356+
// Ei(2 ln u), int Si(u) = u Si(u) + cos(u), int Ci(u) = u Ci(u) - sin(u),
357+
// int Shi(u) = u Shi(u) - cosh(u) and int Chi(u) = u Chi(u) - sinh(u).
358+
// https://github.com/asc-community/AngouriMath/issues/1501
359+
Entity.Eif(var arg) when
360+
TreeAnalyzer.TryGetPolyLinear(arg, x, out var a, out _) =>
361+
(arg * MathS.Ei(arg) - MathS.Pow(MathS.e, arg)) / a,
362+
363+
Entity.Lif(var arg) when
364+
TreeAnalyzer.TryGetPolyLinear(arg, x, out var a, out _) =>
365+
(arg * MathS.Li(arg) - MathS.Ei(2 * MathS.Ln(arg))) / a,
366+
367+
Entity.Sif(var arg) when
368+
TreeAnalyzer.TryGetPolyLinear(arg, x, out var a, out _) =>
369+
(arg * MathS.Si(arg) + MathS.Cos(arg)) / a,
370+
371+
Entity.Cif(var arg) when
372+
TreeAnalyzer.TryGetPolyLinear(arg, x, out var a, out _) =>
373+
(arg * MathS.Ci(arg) - MathS.Sin(arg)) / a,
374+
375+
Entity.Shif(var arg) when
376+
TreeAnalyzer.TryGetPolyLinear(arg, x, out var a, out _) =>
377+
(arg * MathS.Shi(arg) - MathS.Hyperbolic.Cosh(arg)) / a,
378+
379+
Entity.Chif(var arg) when
380+
TreeAnalyzer.TryGetPolyLinear(arg, x, out var a, out _) =>
381+
(arg * MathS.Chi(arg) - MathS.Hyperbolic.Sinh(arg)) / a,
382+
347383
Entity.Absf(var arg) when
348384
TreeAnalyzer.TryGetPolyLinear(arg, x, out var a, out _) => // ∫ |ax + b| dx = sgn(ax + b) * (ax + b)^2 / (2a)
349385
MathS.Signum(arg) * MathS.Pow(arg, 2) / (2 * a),
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 System.Linq;
10+
using AngouriMath.Extensions;
11+
using Xunit;
12+
13+
namespace AngouriMath.Tests.Calculus
14+
{
15+
/// <summary>
16+
/// The special functions integrated by parts: each has an elementary derivative, so against a
17+
/// power of <c>x</c> it is the factor differentiated, and alone it is integrated against 1.
18+
/// The rows are Rubi's, from its files 8.1, 8.3, 8.4 and 8.5.
19+
/// https://github.com/asc-community/AngouriMath/issues/1501
20+
/// </summary>
21+
[Trait("Area", "Calculus")]
22+
public sealed class SpecialFunctionsByPartsTest
23+
{
24+
/// <summary>Off 0, where the negative powers are undefined.</summary>
25+
private static readonly double[] Points = { -1.7, -0.6, 0.35, 0.9, 1.45 };
26+
27+
/// <summary>
28+
/// Integrates, pins the parameters, and compares the derivative of the answer with the
29+
/// integrand at <see cref="Points"/>. The parameters are pinned after integrating, so the
30+
/// rule is asked the symbolic question.
31+
/// </summary>
32+
private static void DifferentiatesBack(string integrand, params (string Name, string Value)[] pins)
33+
{
34+
var integral = integrand.ToEntity().Integrate("x");
35+
Assert.DoesNotContain("integral(", integral.Stringize());
36+
Entity Pinned(Entity e) => pins.Aggregate(e, (current, pin) => current.Substitute(pin.Name, pin.Value.ToEntity()));
37+
var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x");
38+
var original = Pinned(integrand.ToEntity());
39+
foreach (var at in Points)
40+
{
41+
var got = derivative.Substitute("x", at).EvalNumerical();
42+
var want = original.Substitute("x", at).EvalNumerical();
43+
var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart);
44+
var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart));
45+
Assert.True(difference / scale < 1e-9,
46+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
47+
}
48+
}
49+
50+
private static readonly (string, string)[] Parameters = { ("a", "2/5"), ("b", "13/10"), ("c", "7/10"), ("d", "19/10") };
51+
52+
/// <summary>
53+
/// Alone, against 1: <c>int Ei(u) = u Ei(u) - e^u</c> and its kin, each over the rate.
54+
/// </summary>
55+
[Theory]
56+
[InlineData("Ei(b*x)")]
57+
[InlineData("Ei(a + b*x)")]
58+
[InlineData("Si(b*x)")]
59+
[InlineData("Si(a + b*x)")]
60+
[InlineData("Ci(b*x)")]
61+
[InlineData("Shi(a + b*x)")]
62+
[InlineData("Chi(b*x)")]
63+
public void ASpecialFunctionAloneIsByPartsAgainstOne(string integrand)
64+
=> DifferentiatesBack(integrand, Parameters);
65+
66+
/// <summary>
67+
/// The logarithmic integral, where its argument stays above 0 at every point: <c>a = 3</c>
68+
/// keeps <c>a + b x</c> between 0.79 and 4.9.
69+
/// </summary>
70+
[Theory]
71+
[InlineData("li(a + b*x)")]
72+
[InlineData("x*li(a + b*x)")]
73+
public void TheLogarithmicIntegralIsByParts(string integrand)
74+
=> DifferentiatesBack(integrand, ("a", "3"), ("b", "13/10"));
75+
76+
/// <summary>
77+
/// Against a power of <c>x</c>, the special function is the factor differentiated: what is
78+
/// left is the power times <c>e^(-u^2)</c>, <c>e^u/u</c>, <c>sin(u)/u</c> and the like.
79+
/// </summary>
80+
[Theory]
81+
[InlineData("x^3*erf(b*x)")]
82+
[InlineData("x^2*erf(b*x)")]
83+
[InlineData("x*erf(b*x)")]
84+
[InlineData("erf(b*x)/x^2")]
85+
[InlineData("erf(b*x)/x^3")]
86+
[InlineData("(c + d*x)^2*erfc(a + b*x)")]
87+
[InlineData("x*erfi(b*x)")]
88+
[InlineData("x^2*Ei(b*x)")]
89+
[InlineData("x*Ei(a + b*x)")]
90+
[InlineData("Ei(b*x)/x^2")]
91+
[InlineData("x*Si(b*x)")]
92+
[InlineData("x^2*Ci(b*x)")]
93+
[InlineData("Si(b*x)/x^2")]
94+
[InlineData("x*Shi(b*x)")]
95+
[InlineData("x^3*Chi(b*x)")]
96+
public void APowerTimesASpecialFunctionIsByParts(string integrand)
97+
=> DifferentiatesBack(integrand, Parameters);
98+
}
99+
}

0 commit comments

Comments
 (0)