Skip to content

Commit ac6e33f

Browse files
Exponentials below the bar beside a power of a linear are integrated to the exponential integral (#1732)
Written in the exponential every other one is a whole power of, exponentials whose denominator in lowest terms is a power of that one alone are a sum of its powers, and each term over the linear is the exponential integral's: 1/((c + d x)(a + a tanh(e + f x))) is (1 + e^(-2(e + f x)))/(2 a (c + d x)). Rubi's 6.3.1 and 6.4.1. Part of #718. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 06070d2 commit ac6e33f

4 files changed

Lines changed: 212 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3160,6 +3160,23 @@ Both columns measured on a build, `v2.5.0` against this change.
31603160
| `"x*sin(ln(x))/ln(x)".Integrate("x")` | `integral(x * sin(ln(x)) / ln(x), x)` | `(-1/2 * i) * Ei((2 + i) * ln(x)) + 1/2 * i * Ei((2 - i) * ln(x)) + C` |
31613161
| `"cos(a+b*ln(c*x^n))^2".Integrate("x")` | `integral(cos(a + b * ln(c * x ^ n)) ^ 2, x)` | an antiderivative in `sin` and `cos` of `2 (a + b ln(c x^n))`, beside `x (c x^n)^(-1/n)` |
31623162

3163+
### Exponentials below the bar beside a power of a linear are integrated to the exponential integral
3164+
3165+
**Answers where there were none.** `1/(x e^(2x))` was declined where `e^(-2x)/x` was answered, and so
3166+
were `1/((c + d x)(a + a tanh(e + f x)))` and its powers, Rubi's 6.3.1, and the same with `coth`, 6.4.1:
3167+
the rules for an exponential over a linear read it above the bar. Written in the exponential every
3168+
other one is a whole power of, exponentials whose denominator is then a power of that one alone are a
3169+
sum of its powers -- `1/(1 + tanh(z))` is `(1 + e^(-2z))/2` -- and each term over the linear is an
3170+
exponential integral. Where anything else stays below the bar, `1/(x (1 + e^x))`, the integral is not
3171+
of this kind and is declined as before ([#718](https://github.com/asc-community/AngouriMath/issues/718)).
3172+
3173+
| Input | Was (2.5.0) | Now |
3174+
|---|---|---|
3175+
| `"1/(x*exp(2*x))".ToEntity().Integrate("x")` | `integral(...)` | `Ei(-2 x)` |
3176+
| `"1/(x*(1 + tanh(x)))".ToEntity().Integrate("x")` | `integral(...)` | `ln(x)/2 + Ei(-2 x)/2` |
3177+
| `"1/((c + d*x)*(a + a*tanh(e + f*x)))".ToEntity().Integrate("x")` | `integral(...)` | a logarithm and an exponential integral |
3178+
| `"1/((c + d*x)^2*(a + a*coth(e + f*x))^3)".ToEntity().Integrate("x")` | `integral(...)` | powers of `c + d x` and exponential integrals |
3179+
31633180
### An exponential or a hyperbolic function over several linears is split into partial fractions over them
31643181

31653182
`e^x/(x (x + 1))` was left unevaluated, where `e^x/x` and `e^x/(x + 1)` were each answered with

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

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5309,6 +5309,141 @@ void AddALinear(Entity linear, int power)
53095309
return Functions.PartialFractions.Bare((constant * sum).InnerSimplified);
53105310
}
53115311

5312+
/// <summary>
5313+
/// Exponentials of one linear below the bar, beside polynomials of <c>x</c> with one below
5314+
/// the bar: in <c>w</c>, the exponential every other is a whole power of, the exponentials
5315+
/// are a rational function of <c>w</c>, and where that is a sum of whole powers of <c>w</c>
5316+
/// -- its denominator in lowest terms a power of <c>w</c> alone -- each term is an
5317+
/// exponential of a linear over the polynomials, which
5318+
/// <see cref="SolveAnExponentialOfALinearOverAPowerOfALinear"/> answers onto the exponential
5319+
/// integral. <c>1 + tanh(z)</c> is <c>2 e^(2z)/(e^(2z) + 1)</c>, so
5320+
/// <c>1/((c + d x)(a + a tanh(e + f x)))</c> is <c>(1 + e^(-2(e + f x)))/(2 a (c + d x))</c>.
5321+
/// Rubi's 6.3.1 and 6.4.1, <c>(c + d x)^m (a + a tanh(e + f x))^n</c> and the same with
5322+
/// <c>coth</c>, for a negative <c>m</c>.
5323+
/// https://github.com/asc-community/AngouriMath/issues/718
5324+
/// </summary>
5325+
/// <remarks>
5326+
/// The rules for an exponential over a linear read it above the bar: <c>e^(-2x)/x</c> was
5327+
/// answered and <c>1/(x e^(2x))</c> declined. Where the denominator keeps a factor other
5328+
/// than <c>w</c> after cancelling, <c>1/(x (1 + e^x))</c>, the integral is neither elementary
5329+
/// nor an exponential integral, and this declines.
5330+
/// </remarks>
5331+
internal static Entity? SolveAnExponentialBelowTheBarBesideAPowerOfALinear(Entity expr, Entity.Variable x)
5332+
{
5333+
bool IsAnExponential(Entity node) => node is Powf(var b, var p) && !b.ContainsNode(x) && p.ContainsNode(x);
5334+
if (!expr.Nodes.Any(node => node is Divf(_, var below) && below.Nodes.Any(IsAnExponential)
5335+
|| node is Powf(var raised, Number.Integer { IsNegative: true }) && raised.Nodes.Any(IsAnExponential)))
5336+
return null;
5337+
Entity constant = Number.Integer.One;
5338+
Entity aboveX = Number.Integer.One;
5339+
Entity belowX = Number.Integer.One;
5340+
Entity exponentials = Number.Integer.One;
5341+
foreach (var (factor, underneath) in FactorsOfTheIntegrand(expr))
5342+
{
5343+
if (!factor.ContainsNode(x))
5344+
{
5345+
constant = underneath ? constant / factor : constant * factor;
5346+
continue;
5347+
}
5348+
if (factor.Nodes.Any(IsAnExponential))
5349+
{
5350+
exponentials = underneath ? exponentials / factor : exponentials * factor;
5351+
continue;
5352+
}
5353+
// A polynomial, or a whole power of one, above or below the bar.
5354+
var (@base, power) = factor is Powf(var raised, Number.Integer whole) && whole.EInteger.CanFitInInt32() && !whole.EInteger.IsZero
5355+
? (raised, whole.EInteger.ToInt32Checked()) : (factor, 1);
5356+
if (underneath)
5357+
power = -power;
5358+
if (!TreeAnalyzer.TryGetPolynomial(@base, x, out var read) || read.Keys.Any(degree => degree.Sign < 0) || read.Values.Any(coefficient => coefficient.ContainsNode(x)))
5359+
return null;
5360+
if (power > 0)
5361+
aboveX = aboveX == Number.Integer.One ? MathS.Pow(@base, power) : aboveX * MathS.Pow(@base, power);
5362+
else
5363+
belowX = belowX == Number.Integer.One ? MathS.Pow(@base, -power) : belowX * MathS.Pow(@base, -power);
5364+
}
5365+
// Nothing of x below the bar: a polynomial beside the exponentials is the rules' by parts.
5366+
if (belowX == Number.Integer.One || exponentials.Complexity > 160)
5367+
return null;
5368+
5369+
// One base, and every exponent a rational multiple of the first, its offset included,
5370+
// so that w is a power of the base at that exponent and no constant is left beside it.
5371+
Entity? commonBase = null;
5372+
Entity? unit = null, unitSlope = null, unitOffset = null;
5373+
var multiples = new Dictionary<Entity, ERational>();
5374+
foreach (var node in exponentials.Nodes)
5375+
{
5376+
if (!IsAnExponential(node) || multiples.ContainsKey(node) || node is not Powf(var @base, var exponent))
5377+
continue;
5378+
if (!TreeAnalyzer.TryGetPolyLinear(exponent, x, out var slope, out var offset) || TreeAnalyzer.IsZero(slope))
5379+
return null;
5380+
if (commonBase is null)
5381+
{
5382+
(commonBase, unit, unitSlope, unitOffset) = (@base, exponent, slope, offset);
5383+
multiples[node] = ERational.One;
5384+
continue;
5385+
}
5386+
if (@base != commonBase
5387+
|| Functions.PartialFractions.Bare((slope / unitSlope!).Simplify()).Evaled is not Number.Rational ratio || ratio.ERational.IsZero
5388+
|| !IsTheZeroPolynomial((offset - ratio * unitOffset!).InnerSimplified))
5389+
return null;
5390+
multiples[node] = ratio.ERational;
5391+
}
5392+
if (commonBase is null || unit is null)
5393+
return null;
5394+
if (commonBase != MathS.e && (commonBase.Evaled is Number.Complex and not Number.Real || commonBase.Evaled is Number.Real { IsNegative: true } || TreeAnalyzer.IsZero(commonBase)))
5395+
return null;
5396+
var numerators = EInteger.Zero;
5397+
var denominators = EInteger.One;
5398+
foreach (var multiple in multiples.Values)
5399+
{
5400+
numerators = numerators.Gcd(multiple.Numerator.Abs());
5401+
denominators = denominators.Multiply(multiple.Denominator).Divide(denominators.Gcd(multiple.Denominator));
5402+
}
5403+
var step = ERational.Create(numerators, denominators);
5404+
if (multiples.Values.Any(multiple => multiple.Divide(step).ToLowestTerms().Numerator.Abs().CompareTo(EInteger.FromInt32(12)) > 0))
5405+
return null;
5406+
var w = Variable.CreateUnique(expr, "w_exp");
5407+
var inW = exponentials.Replace(node =>
5408+
multiples.TryGetValue(node, out var multiple) ? MathS.Pow(w, Number.Integer.Create(multiple.Divide(step).ToLowestTerms().Numerator)) : node);
5409+
if (inW.ContainsNode(x))
5410+
return null;
5411+
5412+
// A sum of whole powers of w: its denominator in lowest terms a power of w alone.
5413+
var (top, bottom) = Functions.SingleQuotient.Of(inW);
5414+
if (!TreeAnalyzer.TryGetPolynomial(bottom, w, out var below) || below.Count != 1)
5415+
{
5416+
if (!Functions.PolynomialGcd.TryCancel(top, bottom, out var cancelled, maxComplexity: 1024))
5417+
return null;
5418+
(top, bottom) = Functions.SingleQuotient.Of(cancelled is Providedf(var inner, _) ? inner : cancelled);
5419+
if (!TreeAnalyzer.TryGetPolynomial(bottom, w, out below) || below.Count != 1)
5420+
return null;
5421+
}
5422+
var lowest = below.Keys.Single();
5423+
var leading = below[lowest];
5424+
if (!TreeAnalyzer.TryGetPolynomial(top, w, out var above) || above.Count > 16 || TreeAnalyzer.IsZero(leading))
5425+
return null;
5426+
5427+
Entity sum = Number.Integer.Zero;
5428+
foreach (var term in above)
5429+
{
5430+
if (TreeAnalyzer.IsZero(term.Value))
5431+
continue;
5432+
var power = term.Key.Subtract(lowest);
5433+
var exponential = power.IsZero ? Number.Integer.One
5434+
: MathS.Pow(commonBase, (Number.Rational.Create(step.Multiply(ERational.FromEInteger(power))) * unit).InnerSimplified);
5435+
var question = constant * term.Value / leading * aboveX * exponential / belowX;
5436+
var integral = power.IsZero
5437+
? Integration.ComputeIndefiniteIntegral(question, x, false)
5438+
: SolveAnExponentialOfALinearOverAPowerOfALinear(question, x) ?? SolveAnExponentialOverSeveralLinears(question, x)
5439+
?? Integration.ComputeIndefiniteIntegral(question, x, false);
5440+
if (integral is null || integral.Nodes.Any(node => node is Integralf))
5441+
return null;
5442+
sum += integral;
5443+
}
5444+
return sum == Number.Integer.Zero ? null : Functions.PartialFractions.Bare(sum.InnerSimplified);
5445+
}
5446+
53125447
/// <summary>
53135448
/// An exponential of a linear times sines and cosines of linears, and a polynomial, over
53145449
/// linears: each sine and cosine is written as exponentials, <c>sin(c x) = (e^(i c x) - e^(-i c x))/(2i)</c>,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
656656
// And either over several linears, split into partial fractions over them first, as the
657657
// trigonometric rule below splits.
658658
if ((answer = IndefiniteIntegralSolver.SolveAnExponentialOverSeveralLinears(expr, x)) is { }) return answer;
659+
// And exponentials below the bar, where they are whole powers of one exponential and
660+
// its powers alone are left below: `1/((c + d x)(a + a tanh(e + f x)))`.
661+
if ((answer = IndefiniteIntegralSolver.SolveAnExponentialBelowTheBarBesideAPowerOfALinear(expr, x)) is { }) return answer;
659662
// And with sines and cosines beside the exponential, written as exponentials: each term
660663
// is then the exponential's, with a complex rate.
661664
if ((answer = IndefiniteIntegralSolver.SolveAnExponentialTimesATrigonometricOverLinears(expr, x)) is { }) return answer;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 AngouriMath.Extensions;
10+
using Xunit;
11+
12+
namespace AngouriMath.Tests.Calculus
13+
{
14+
/// <summary>
15+
/// Exponentials of one linear below the bar, beside a power of a linear: written in the
16+
/// exponential the others are whole powers of, they are a sum of whole powers of it, and
17+
/// each term over the linear is an exponential integral. <c>1/(x e^(2x))</c> was declined
18+
/// where <c>e^(-2x)/x</c> was answered, and so was <c>1/((c + d x)(a + a tanh(k + f x)))</c>,
19+
/// Rubi's 6.3.1, which is <c>(1 + e^(-2(k + f x)))/(2 a (c + d x))</c>.
20+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
21+
/// </summary>
22+
[Trait("Area", "Calculus")]
23+
public sealed class ExponentialBelowTheBarIntegralTest
24+
{
25+
[Theory]
26+
[InlineData("1/(x*exp(2*x))")]
27+
[InlineData("(exp(2*x) + 1)/(2*x*exp(2*x))")]
28+
[InlineData("1/(x*(1 + tanh(x)))")]
29+
[InlineData("1/((c + d*x)*(a + a*tanh(k + f*x)))")]
30+
[InlineData("1/((c + d*x)^2*(a + a*tanh(k + f*x))^3)")]
31+
[InlineData("1/((c + d*x)*(a + a*coth(k + f*x))^2)")]
32+
[InlineData("1/((c + d*x)*(a - a*tanh(e + f*x)))")]
33+
[InlineData("1/(x*(x + 1)*exp(x))")]
34+
public void ExpandedOverTheLinear(string integrand)
35+
{
36+
var integral = integrand.ToEntity().Integrate("x");
37+
Assert.DoesNotContain("integral(", integral.Stringize());
38+
Assert.DoesNotContain("NaN", integral.Stringize());
39+
Entity Pinned(Entity e) => e.Substitute("a", 2.3).Substitute("c", 1.3).Substitute("d", 1.7).Substitute("f", 0.9).Substitute("k", 0.4);
40+
var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x");
41+
var original = Pinned(integrand.ToEntity());
42+
var compared = 0;
43+
foreach (var at in new[] { -1.7, -0.9, 0.3, 0.8, 1.6, 2.9 })
44+
{
45+
var want = original.Substitute("x", at).EvalNumerical();
46+
if (want.IsNaN || Math.Abs((double)want.ImaginaryPart) > 1e-12)
47+
continue;
48+
compared++;
49+
var got = derivative.Substitute("x", at).EvalNumerical();
50+
Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart)
51+
< 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart)),
52+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
53+
}
54+
Assert.True(compared >= 5, $"only {compared} points could be compared for {integrand}");
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)