Skip to content

Commit ab9145f

Browse files
The hyperbolic sine and cosine integrals are integrated to (#1535)
`sinh(x)/x` was left unintegrated. `sinh` and `cosh` arrive as exponentials, and the exponential integral's rule reads one exponential over a power of a linear, not a sum of them. A polynomial in exponentials of a linear, beside a polynomial, over a whole power of a linear `e + f x` is now read as a sum of `c e^(r x)`. Under `u = e + f x` each term is `u^m e^(k u)`, taken to `Ei(k u)` and elementary terms, and two of opposite rates pair: `A Ei(k u) + B Ei(-k u) = (A + B) Chi(k u) + (A - B) Shi(k u)`, up to a constant. A rate of 0 is a power of `u` alone. A whole power of a product or quotient of exponentials is read factor by factor, and a rate with a symbol in it is simplified before rates are compared, since one expansion writes the same rate as `2 (-b)` and `-2 b`, and zero as `2 b + 2 (-b)`. Rubi's problems whose answer uses Si, Ci, Shi or Chi (1186, 3 s): 19 more solved -- 6.1.1, 6.7.1 and 7.1.4 -- none lost, 0 wrong. Part of #1501. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e23fb48 commit ab9145f

5 files changed

Lines changed: 300 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,21 @@ argument `a + b x^r` beside a power of `x` is the same question under `u = x^r`
17031703
| `"sin(x)/(x*(1+x))".Integrate("x")` | `integral(sin(x) / (x * (1 + x)), x)` | `Si(x) + -cos(1) * Si(1 + x) + sin(1) * Ci(1 + x) + C` |
17041704
| `"sin(a+b/x)/x".Integrate("x")` | `integral(sin(a + b / x) / x, x)` | `-(cos(a) * Si(b * 1 / x) + sin(a) * Ci(b * 1 / x)) + C` |
17051705

1706+
### The hyperbolic sine and cosine integrals are integrated to
1707+
1708+
`sinh(x)/x` was left unintegrated. `sinh` and `cosh` arrive as exponentials, and a polynomial in
1709+
exponentials of a linear, beside a polynomial, over a whole power of a linear is now integrated to
1710+
`Shi` and `Chi`. Under `u` = the linear each term is `u^m e^(k u)`, the exponential integral's, and
1711+
two terms of opposite rates pair: `A Ei(k u) + B Ei(-k u) = (A + B) Chi(k u) + (A - B) Shi(k u)`, up
1712+
to a constant ([#1501](https://github.com/asc-community/AngouriMath/issues/1501)).
1713+
1714+
| Input | Was (2.5.0) | Now |
1715+
|---|---|---|
1716+
| `"sinh(x)/x".Integrate("x")` | `integral((e ^ x - e ^ (-x)) / 2 / x, x)` | `Shi(x) + C` |
1717+
| `"cosh(x)/x".Integrate("x")` | `integral((e ^ x + e ^ (-x)) / 2 / x, x)` | `Chi(x) + C` |
1718+
| `"sinh(x)^2/x".Integrate("x")` | `integral(((e ^ x - e ^ (-x)) / 2) ^ 2 / x, x)` | `-1/2 * ln(x) + 1/2 * Chi(2 * x) + C` |
1719+
| `"cosh(a+b*x)/(c+d*x)".Integrate("x")` | `integral((e ^ (a + b * x) + e ^ (-(a + b * x))) / 2 / (c + d * x), x)` | `Chi` and `Shi` of `b (c + d x)/d`, with `cosh(a - b c/d)/d` and `sinh(a - b c/d)/d` in front, written in exponentials |
1720+
17061721
### A definite integral is the limit at a bound where its antiderivative is undefined
17071722

17081723
`integral(x * e^(-x), x, 0, +oo)` was `NaN`, this library's way of saying a value does not

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,3 +672,6 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
672672

673673
[Tests/UnitTests/Core/DowncastingOffComparisonTest.cs]
674674
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
675+
676+
[Tests/UnitTests/Calculus/HyperbolicIntegralIntegrationTest.cs]
677+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

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

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3350,6 +3350,209 @@ private static (Entity Elementary, Entity EiCoefficient) APowerTimesAnExponentia
33503350
return (exponential * sum, Number.Integer.Zero);
33513351
}
33523352

3353+
/// <summary>
3354+
/// A sum of exponentials of the variable, times a polynomial, over a whole power of a
3355+
/// linear: <c>P(x) sum_j c_j e^(r_j x)/(e + f x)^n</c>, which is how a polynomial in
3356+
/// <c>sinh</c> and <c>cosh</c> of a linear arrives, onto the hyperbolic sine and cosine
3357+
/// integrals. Under <c>u = e + f x</c> each term is <c>u^m e^(k u)</c>, the exponential
3358+
/// integral's (<see cref="APowerTimesAnExponential"/>), and two of opposite rates pair:
3359+
/// <c>A Ei(k u) + B Ei(-k u) = (A + B) Chi(k u) + (A - B) Shi(k u)</c>, since
3360+
/// <c>Ei(y) + Ei(-y) = 2 Chi(y)</c> and <c>Ei(y) - Ei(-y) = 2 Shi(y)</c> for <c>y > 0</c>, and
3361+
/// for <c>y &lt; 0</c> up to a constant. A rate of 0, which an even power leaves, is a power
3362+
/// of <c>u</c> alone. Rubi's 6.1.1, <c>(c + d x)^m (a + b sinh(e + f x))^n</c>, with
3363+
/// <c>m</c> negative.
3364+
/// https://github.com/asc-community/AngouriMath/issues/1501
3365+
/// </summary>
3366+
/// <remarks>
3367+
/// After <see cref="SolveAnExponentialOfALinearOverAPowerOfALinear"/>, which answers one
3368+
/// exponential alone; and only where an integral is left.
3369+
/// </remarks>
3370+
internal static Entity? SolveAHyperbolicOfALinearOverAPowerOfALinear(Entity expr, Entity.Variable x)
3371+
{
3372+
if (expr is not (Divf or Mulf) || !HasASumOfExponentials(expr, x))
3373+
return null;
3374+
Entity constant = Number.Integer.One;
3375+
Entity? linear = null;
3376+
var power = 0;
3377+
Entity? polynomial = null;
3378+
Entity? exponentials = null;
3379+
foreach (var (factor, underneath) in FactorsOfTheIntegrand(expr))
3380+
{
3381+
if (!factor.ContainsNode(x))
3382+
{
3383+
constant = underneath ? constant / factor : constant * factor;
3384+
continue;
3385+
}
3386+
var (@base, exponent) = factor is Powf(var raised, Number.Integer whole) && whole.EInteger.CanFitInInt32() && !whole.EInteger.IsZero
3387+
? (raised, whole.EInteger.ToInt32Checked()) : (factor, 1);
3388+
if (underneath)
3389+
exponent = -exponent;
3390+
if (exponent < 0 && TreeAnalyzer.TryGetPolyLinear(@base, x, out var slopeOfIt, out _) && !TreeAnalyzer.IsZero(slopeOfIt))
3391+
{
3392+
if (linear is not null && linear != @base)
3393+
return null;
3394+
linear = @base;
3395+
power -= exponent;
3396+
continue;
3397+
}
3398+
if (underneath)
3399+
return null;
3400+
if (factor.Nodes.Any(node => node is Powf(var b, var e) && !b.ContainsNode(x) && e.ContainsNode(x)))
3401+
{
3402+
exponentials = exponentials is null ? factor : exponentials * factor;
3403+
continue;
3404+
}
3405+
if (TreeAnalyzer.TryGetPolynomial(factor, x, out var monomials) && monomials.Keys.All(degree => degree.Sign >= 0 && degree.CompareTo(EInteger.FromInt32(12)) <= 0))
3406+
{
3407+
polynomial = polynomial is null ? factor : polynomial * factor;
3408+
continue;
3409+
}
3410+
return null;
3411+
}
3412+
if (exponentials is null || linear is null || power > 12 || !TreeAnalyzer.TryGetPolyLinear(linear, x, out var f, out var e0))
3413+
return null;
3414+
// The exponentials as a sum of c e^(r x), gathered by the rate r.
3415+
var rates = new List<(Entity Rate, Entity Coefficient)>();
3416+
foreach (var term in Sumf.LinearChildren(exponentials.Expand()))
3417+
{
3418+
if (ReadAnExponentialTerm(term, x) is not { } read)
3419+
return null;
3420+
// The same rate may be written two ways, `2 (-b)` and `-2 b`.
3421+
var at = rates.FindIndex(pair => pair.Rate == read.Rate
3422+
|| pair.Rate.Vars.Any() && TreeAnalyzer.IsZero(Functions.PartialFractions.Bare((pair.Rate - read.Rate).Simplify())));
3423+
if (at < 0)
3424+
rates.Add(read);
3425+
else
3426+
rates[at] = (read.Rate, rates[at].Coefficient + read.Coefficient);
3427+
}
3428+
var u = Variable.CreateUnique(expr, "u");
3429+
var inU = polynomial is null ? Number.Integer.One : polynomial.Substitute(x, (u - e0) / f);
3430+
if (!TreeAnalyzer.TryGetPolynomial(inU, u, out var powers) || powers.Keys.All(degree => degree.CompareTo(EInteger.FromInt32(power)) >= 0)
3431+
|| powers.Keys.Any(degree => !degree.CanFitInInt32()))
3432+
return null;
3433+
Entity sum = Number.Integer.Zero;
3434+
// The constant in front goes into each coefficient, so that the numbers fold.
3435+
var overall = (constant / f).InnerSimplified;
3436+
// The coefficient of Ei(k u) for each rate k in u, to be paired.
3437+
var integrals = new List<(Entity Rate, Entity Coefficient)>();
3438+
foreach (var (rate, coefficientOfTheRate) in rates)
3439+
{
3440+
var coefficient = (overall * coefficientOfTheRate).InnerSimplified;
3441+
if (TreeAnalyzer.IsZero(rate))
3442+
{
3443+
// The terms of rate 0 come from several products and may cancel, as in
3444+
// `cosh(y) sinh(y)^3`, where `e^(2 a) e^(-2 a)` has to be seen to be 1.
3445+
var constantPart = Functions.PartialFractions.Bare(coefficient.Simplify());
3446+
if (TreeAnalyzer.IsZero(constantPart))
3447+
continue;
3448+
foreach (var monomial in powers)
3449+
{
3450+
var m = monomial.Key.ToInt32Checked() - power;
3451+
sum += constantPart * monomial.Value * (m == -1 ? MathS.Ln(u) : MathS.Pow(u, m + 1) / (m + 1));
3452+
}
3453+
continue;
3454+
}
3455+
// e^(r x) = e^(-r e/f) e^(k u) with k = r/f.
3456+
var k = (rate / f).InnerSimplified;
3457+
var shifted = (coefficient * MathS.Pow(MathS.e, (-rate * e0 / f).InnerSimplified)).InnerSimplified;
3458+
var exponentialOfU = MathS.Pow(MathS.e, k * u);
3459+
Entity elementary = Number.Integer.Zero, ei = Number.Integer.Zero;
3460+
foreach (var monomial in powers)
3461+
{
3462+
var (below, eiBelow) = APowerTimesAnExponential(monomial.Key.ToInt32Checked() - power, k, exponentialOfU, u);
3463+
elementary += monomial.Value * below;
3464+
ei += monomial.Value * eiBelow;
3465+
}
3466+
sum += shifted * elementary;
3467+
ei = (shifted * ei).InnerSimplified;
3468+
if (!TreeAnalyzer.IsZero(ei))
3469+
integrals.Add((k, ei));
3470+
}
3471+
if (integrals.Count == 0)
3472+
return null;
3473+
// Pairs of opposite rates as Chi and Shi, the positive rate written.
3474+
while (integrals.Count > 0)
3475+
{
3476+
var (k, a) = integrals[0];
3477+
integrals.RemoveAt(0);
3478+
// Simplified, since `d/f` and `-d/f` do not cancel inner-simplified.
3479+
var opposite = integrals.FindIndex(pair => TreeAnalyzer.IsZero(Functions.PartialFractions.Bare((pair.Rate + k).Simplify())));
3480+
if (opposite < 0)
3481+
{
3482+
sum += Functions.PartialFractions.Bare(a.Simplify()) * MathS.Ei(k * u);
3483+
continue;
3484+
}
3485+
var b = integrals[opposite].Coefficient;
3486+
integrals.RemoveAt(opposite);
3487+
if (k.Evaled is Number.Real { IsNegative: true })
3488+
(k, a, b) = ((-k).InnerSimplified, b, a);
3489+
// The two coefficients simplified, which is where `e e^(-4/3)` becomes `e^(-1/3)`.
3490+
sum += Functions.PartialFractions.Bare((a + b).Simplify()) * MathS.Chi(k * u)
3491+
+ Functions.PartialFractions.Bare((a - b).Simplify()) * MathS.Shi(k * u);
3492+
}
3493+
return Functions.PartialFractions.Bare(sum.Substitute(u, linear).InnerSimplified);
3494+
}
3495+
3496+
/// <summary>
3497+
/// A term <c>c e^(r x)</c> of an expanded sum of exponentials, as its rate and coefficient:
3498+
/// a product of constants and of constants to linears in <paramref name="x"/>, or
3499+
/// <see langword="null"/>.
3500+
/// </summary>
3501+
private static (Entity Rate, Entity Coefficient)? ReadAnExponentialTerm(Entity term, Entity.Variable x)
3502+
{
3503+
Entity rate = Number.Integer.Zero;
3504+
Entity coefficient = Number.Integer.One;
3505+
foreach (var (written, underneath) in FactorsOfTheIntegrand(term))
3506+
if (!Read(written, underneath ? -1 : 1))
3507+
return null;
3508+
// The rate is a sum of the slopes, `b + b - b - b` for four exponentials of `a + b x`,
3509+
// and inner simplification leaves that as `2 b + 2 (-b)`, a zero nothing reads: a
3510+
// rate with a symbol in it is simplified.
3511+
var folded = rate.InnerSimplified;
3512+
if (folded.Vars.Any())
3513+
folded = Functions.PartialFractions.Bare(folded.Simplify());
3514+
return (folded, coefficient.InnerSimplified);
3515+
3516+
// A factor to a whole power: a constant, an exponential of a linear, or a product or
3517+
// a quotient of those -- `(e^(2 x)/4)^2`, as the expansion of `sinh(x)^2 cosh(x)^2`
3518+
// leaves one, is `e^(4 x)/16`.
3519+
bool Read(Entity factor, int times)
3520+
{
3521+
while (factor is Powf(var inner, Number.Integer whole) && whole.EInteger.CanFitInInt32())
3522+
(factor, times) = (inner, times * whole.EInteger.ToInt32Checked());
3523+
if (!factor.ContainsNode(x))
3524+
{
3525+
coefficient *= times == 1 ? factor : MathS.Pow(factor, times);
3526+
return true;
3527+
}
3528+
switch (factor)
3529+
{
3530+
case Mulf(var left, var right):
3531+
return Read(left, times) && Read(right, times);
3532+
case Divf(var numerator, var denominator):
3533+
return Read(numerator, times) && Read(denominator, -times);
3534+
case Powf(var @base, var exponent) when !@base.ContainsNode(x)
3535+
&& TreeAnalyzer.TryGetPolyLinear(exponent, x, out var slope, out var intercept):
3536+
var logarithm = @base == MathS.e ? (Entity)Number.Integer.One : MathS.Ln(@base);
3537+
rate += times * slope * logarithm;
3538+
coefficient *= MathS.Pow(@base, times * intercept);
3539+
return true;
3540+
default:
3541+
return false;
3542+
}
3543+
}
3544+
}
3545+
3546+
/// <summary>Whether a factor of <paramref name="expr"/> is a sum with an exponential of the variable in it.</summary>
3547+
private static bool HasASumOfExponentials(Entity expr, Entity.Variable x) => expr switch
3548+
{
3549+
Sumf or Minusf => expr.Nodes.Any(node => node is Powf(var b, var e) && !b.ContainsNode(x) && e.ContainsNode(x)),
3550+
Powf(var @base, Number.Integer) => HasASumOfExponentials(@base, x),
3551+
Mulf(var left, var right) => HasASumOfExponentials(left, x) || HasASumOfExponentials(right, x),
3552+
Divf(var numerator, _) => HasASumOfExponentials(numerator, x),
3553+
_ => false,
3554+
};
3555+
33533556
/// <summary>
33543557
/// A factor of <paramref name="expr"/>, read through products and quotients, that is a
33553558
/// constant to a linear in <paramref name="x"/>, above the bar, or <see langword="null"/>.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,8 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
594594
if ((answer = IndefiniteIntegralSolver.SolveAProductOfExponentialsOfQuadratics(expr, x)) is { }) return answer;
595595
// An exponential of a linear over a power of a linear, onto the exponential integral.
596596
if ((answer = IndefiniteIntegralSolver.SolveAnExponentialOfALinearOverAPowerOfALinear(expr, x)) is { }) return answer;
597+
// And a sum of exponentials, which is how sinh and cosh arrive, onto Shi and Chi.
598+
if ((answer = IndefiniteIntegralSolver.SolveAHyperbolicOfALinearOverAPowerOfALinear(expr, x)) is { }) return answer;
597599
// Sines and cosines of a linear over a power of a linear, onto Si and Ci under u = the
598600
// linear, where the search would take the quotient by parts without end.
599601
if ((answer = IndefiniteIntegralSolver.SolveATrigonometricOfALinearOverAPowerOfALinear(expr, x)) is { }) return answer;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 integrals that answer in the hyperbolic sine and cosine integrals: sums of
17+
/// exponentials of a linear, which is how <c>sinh</c> and <c>cosh</c> arrive, over a power of
18+
/// a linear.
19+
/// https://github.com/asc-community/AngouriMath/issues/1501
20+
/// </summary>
21+
[Trait("Area", "Calculus")]
22+
public sealed class HyperbolicIntegralIntegrationTest
23+
{
24+
private static Entity DifferentiatesBack(string integrand, double[] points, params (string Name, string Value)[] pins)
25+
{
26+
var integral = integrand.ToEntity().Integrate("x");
27+
Assert.DoesNotContain("integral(", integral.Stringize());
28+
Entity Pinned(Entity e) => pins.Aggregate(e, (current, pin) => current.Substitute(pin.Name, pin.Value.ToEntity()));
29+
var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x");
30+
var original = Pinned(integrand.ToEntity());
31+
foreach (var at in points)
32+
{
33+
var got = derivative.Substitute("x", at).EvalNumerical();
34+
var want = original.Substitute("x", at).EvalNumerical();
35+
var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart);
36+
var scale = Math.Max(1.0, Math.Abs((double)want.RealPart));
37+
Assert.True(difference / scale < 1e-9, $"d/dx of {integral} is {got} at x = {at}, where {integrand} is {want}");
38+
}
39+
return integral;
40+
}
41+
42+
/// <summary>Either side of 0 and of every pole below.</summary>
43+
private static readonly double[] AroundZero = { -1.7, 0.35, 0.9, 1.45 };
44+
45+
private static readonly (string, string)[] Pins =
46+
{ ("a", "1/3"), ("b", "2/3"), ("c", "5/4"), ("d", "1/2") };
47+
48+
/// <summary>
49+
/// Under <c>u = c + d x</c>, each exponential is <c>u^m e^(k u)</c>, and the two of
50+
/// opposite rates pair: <c>A Ei(k u) + B Ei(-k u) = (A + B) Chi(k u) + (A - B) Shi(k u)</c>.
51+
/// Rubi's 6.1.1 and 6.2.1, <c>(c + d x)^m sinh(a + b x)^n</c> with <c>m</c> negative, and
52+
/// 6.7.1's products of powers, whose expansion writes one rate several ways -- and a zero
53+
/// rate as <c>2 b + 2 (-b)</c>.
54+
/// </summary>
55+
[Theory]
56+
[InlineData("sinh(x)/x")]
57+
[InlineData("cosh(x)/x")]
58+
[InlineData("sinh(2*x + 1)/(3*x + 4)")]
59+
[InlineData("cosh(x)/x^2")]
60+
[InlineData("x*sinh(x)/(x + 1)^2")]
61+
[InlineData("sinh(x)^2/x")]
62+
[InlineData("sinh(x)*cosh(x)/x")]
63+
[InlineData("cosh(a + b*x)/(c + d*x)")]
64+
[InlineData("cosh(a + b*x)*sinh(a + b*x)^3/x")]
65+
[InlineData("sinh(a + b*x)^2*cosh(a + b*x)^2/(c + d*x)")]
66+
[InlineData("(e^x + 2*e^(3*x))/x")]
67+
public void ASumOfExponentialsOverAPowerOfALinear(string integrand)
68+
=> DifferentiatesBack(integrand, AroundZero, Pins);
69+
70+
[Theory]
71+
[InlineData("sinh(x)/x", "Shi(x)")]
72+
[InlineData("cosh(x)/x", "Chi(x)")]
73+
[InlineData("sinh(2*x)/x", "Shi(2 * x)")]
74+
public void TheDefinitionsAreTheAnswers(string integrand, string expected)
75+
=> Assert.Equal(expected.ToEntity() + "C".ToEntity(), integrand.ToEntity().Integrate("x"));
76+
}
77+
}

0 commit comments

Comments
 (0)