Skip to content

Commit e4f84aa

Browse files
Take the limit at an infinite bound where substitution leaves an infinity inside the value (#2462)
A definite integral to an infinite bound substituted the bound into the antiderivative, and with a parameter beside it the infinity stayed inside a function: erf(+oo - y), since y could itself be infinite. The limit at the bound is now taken there, reading the parameter as the finite value an integral with a parameter is a function of: e^(-(t - y)^2) over the line is sqrt(pi), and 1/(1 + (t - y)^2) is pi. It is taken only where the antiderivative was read to be continuous across the range, numeric bounds with no break listed or all breaks listed, so that 1/(t^2 + y^2)'s logarithm for y^2 < 0, whose zeros move with y, is not answered with its principal value. A limit that is undecided, undefined, or still holds an infinity (e^(-y t), which depends on the sign of y) keeps the substituted value as before. Part of #2451 Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1b9d66f commit e4f84aa

3 files changed

Lines changed: 172 additions & 9 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,31 @@ Euler's totient is `phi(n)`, and `φ(n)` and `ϕ(n)` are refused. Each name with
8383
| `"divergence(x)".ToEntity()` | `divergence * x` | throws `UnrecognizedFunctionParseException` |
8484
| `"gradient(x^2*y, [x, y])".ToEntity()` | throws `UnhandledParseException` | throws `UnrecognizedFunctionParseException` |
8585

86+
### A definite integral with a parameter takes the limit at an infinite bound
87+
88+
**Silent: an unsettled value is now settled.** An infinite bound was substituted into the antiderivative, and where a
89+
parameter stood beside it the infinity stayed inside a function: `erf(+oo - y)`, since `y` could itself be infinite. The
90+
limit is now taken there, reading the parameter as the finite value the integral is a function of
91+
([#2451](https://github.com/asc-community/AngouriMath/issues/2451)). It is taken only where the antiderivative was read to
92+
be continuous across the range, and only where the limit settles without an infinity left in it; otherwise the value is as
93+
before. With `Simplify`:
94+
95+
| Input | Was (2.7.0) | Now |
96+
|---|---|---|
97+
| `integral(e^(-(t-y)^2), t, -oo, +oo)` | `sqrt(pi) * (erf(+oo - y) - erf(-oo - y)) / 2` | `sqrt(pi)` |
98+
| `integral(e^(-(t-y)^2), t, 0, +oo)` | `sqrt(pi) * (erf(+oo - y) - erf(-y)) / 2` | `sqrt(pi) * (1/2 + -1/2 * erf(-y))` |
99+
| `integral(1/(1+(t-y)^2), t, -oo, +oo)` | `arctan((+oo + (-2) * y) / 2) - arctan(((-2) * y - +oo) / 2)` | `pi` |
100+
| `integral(1/(1+(t-y)^2), t, 0, +oo)` | `arctan((+oo + (-2) * y) / 2) - arctan(-y)` | `pi / 2 - arctan(-y)` |
101+
| `integral(t*e^(-(t-y)^2), t, -oo, +oo)` | `(y * sqrt(pi) * (erf(+oo - y) - erf(-oo - y)) + e ^ (-(-oo - y) ^ 2) - e ^ (-(+oo - y) ^ 2)) / 2` | `sqrt(pi) * y` |
102+
| `integral(e^(-t^2-2*y*t), t, -oo, +oo)` | `sqrt(pi) * (-erf(y - +oo) * e ^ y ^ 2 + erf(y + +oo) * e ^ y ^ 2) / 2` | `sqrt(pi) * e ^ y ^ 2` |
103+
| `integral(e^(-y*t), t, 0, +oo)` | `(1 - e ^ ((-oo) * y)) / y` | unchanged: the limit depends on the sign of `y` |
104+
| `integral(1/(t-y)^2, t, 0, +oo)` | `(-oo) / (y * (+oo - y))` | unchanged: the pole at `y` cannot be placed |
105+
| `integral(1/(1+(t-y)^2), t, -oo, y)` | `-arctan(((-2) * y - +oo) / 2)` | unchanged: a symbolic bound |
106+
107+
The continuity read is on the real line with real parameters, as it is for finite bounds: for a non-real `y` with
108+
`|Im y| > 1`, `arctan(t - y)` crosses arctan's cut and the integral of `1/(1 + (t - y)^2)` over the line is 0, not `pi`.
109+
That was already the answer at a concrete such `y` -- `integral(1/(1+(t-3i)^2), t, -oo, +oo)` is `pi` in 2.7.0 and now.
110+
86111
### `dot` and `cross` of a name for a vector are nodes
87112

88113
**Not silent: what was refused now parses.** `dot(a, b)` and `cross(a, b)` threw `InvalidArgumentParseException`

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

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,25 @@ public Entity Integrate(Variable x, Entity from, Entity to) =>
7676
private static Entity? AcrossTheRange(Entity antiderivative, Variable x, Entity from, Entity to)
7777
{
7878
if (from.Evaled is not Number.Real { IsNaN: false } lower || to.Evaled is not Number.Real { IsNaN: false } upper
79-
|| lower == upper
80-
|| (lower < upper
81-
? AntiderivativeBreaks.Inside(antiderivative, x, lower, upper)
82-
: AntiderivativeBreaks.Inside(antiderivative, x, upper, lower)) is not { Count: > 0 } breaks)
83-
return AtTheBound(antiderivative, x, to, from) is { } atTheTop && AtTheBound(antiderivative, x, from, to) is { } atTheBottom
79+
|| lower == upper)
80+
return AtTheBound(antiderivative, x, to, from, continuousInside: false) is { } atTheTop
81+
&& AtTheBound(antiderivative, x, from, to, continuousInside: false) is { } atTheBottom
82+
? atTheTop - atTheBottom
83+
: null;
84+
var breaks = lower < upper
85+
? AntiderivativeBreaks.Inside(antiderivative, x, lower, upper)
86+
: AntiderivativeBreaks.Inside(antiderivative, x, upper, lower);
87+
if (breaks is not { Count: > 0 })
88+
// An empty list is a range the antiderivative was read to be continuous across; no list, one where a
89+
// break could not be listed.
90+
return AtTheBound(antiderivative, x, to, from, continuousInside: breaks is not null) is { } atTheTop
91+
&& AtTheBound(antiderivative, x, from, to, continuousInside: breaks is not null) is { } atTheBottom
8492
? atTheTop - atTheBottom
8593
: null;
8694
if (lower > upper)
8795
breaks.Reverse();
8896
// Not across a break of an antiderivative in the incomplete gamma function: see AtTheBound.
89-
if (HoldsAnIncompleteGammaFunction(antiderivative) || AtTheBound(antiderivative, x, from, to) is not { } atTheStart)
97+
if (HoldsAnIncompleteGammaFunction(antiderivative) || AtTheBound(antiderivative, x, from, to, continuousInside: true) is not { } atTheStart)
9098
return null;
9199
var total = -atTheStart;
92100
foreach (var (at, _) in breaks)
@@ -99,7 +107,7 @@ public Entity Integrate(Variable x, Entity from, Entity to) =>
99107
return null;
100108
total += before - after;
101109
}
102-
return AtTheBound(antiderivative, x, to, from) is { } atTheEnd ? total + atTheEnd : null;
110+
return AtTheBound(antiderivative, x, to, from, continuousInside: true) is { } atTheEnd ? total + atTheEnd : null;
103111
}
104112

105113
private static bool HoldsAnIncompleteGammaFunction(Entity expr)
@@ -125,12 +133,19 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr)
125133
/// undefined, the value stays undefined, as it is for <c>sin(x)</c> from 0 to <c>+oo</c>,
126134
/// whose antiderivative has no limit there. Where the limit is not decided there is no value,
127135
/// <see langword="null"/>, and the integral is left as written.
136+
/// <para>
137+
/// An infinite bound substituted can also leave an infinity standing inside the value, where the
138+
/// arithmetic cannot settle it because a parameter is in the way: <c>erf(+oo - y)</c>, the antiderivative
139+
/// of <c>e^(-(t - y)^2)</c> at <c>+oo</c>. That is not a value either, and the limit is taken there too
140+
/// (<paramref name="continuousInside"/> permitting; see <see cref="AtAnInfiniteBound"/>).
141+
/// https://github.com/asc-community/AngouriMath/issues/2451
142+
/// </para>
128143
/// </remarks>
129-
private static Entity? AtTheBound(Entity antiderivative, Variable x, Entity bound, Entity otherBound)
144+
private static Entity? AtTheBound(Entity antiderivative, Variable x, Entity bound, Entity otherBound, bool continuousInside)
130145
{
131146
var value = antiderivative.Substitute(x, bound);
132147
if (!value.InnerSimplified.IsNaN)
133-
return value;
148+
return continuousInside && AtAnInfiniteBound(antiderivative, x, bound, value) is { } atInfinity ? atInfinity : value;
134149
// The bounds' values, so that a bound written as pi or 2 pi is placed as well as 3.
135150
ApproachFrom? side = (bound.Evaled, otherBound.Evaled) switch
136151
{
@@ -157,6 +172,56 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr)
157172
return limit.IsNaN ? value : limit;
158173
}
159174

175+
/// <summary>
176+
/// The antiderivative's limit at an infinite bound, where substituting the bound left an infinity inside
177+
/// the value, or <see langword="null"/> to keep the substituted value as it was.
178+
/// </summary>
179+
/// <remarks>
180+
/// <para>
181+
/// <c>erf(+oo - y)</c> is left as it is by the arithmetic, since <c>y</c> could itself be infinite; but an
182+
/// integral with a parameter is a function of that parameter at each of its finite values, and the limit
183+
/// of <c>erf(t - y)</c> as <c>t</c> grows reads <c>y</c> as exactly that, a finite constant, and is 1. So
184+
/// <c>e^(-(t - y)^2)</c> over the line is <c>sqrt(pi)</c> rather than a difference of two unsettled error
185+
/// functions. https://github.com/asc-community/AngouriMath/issues/2451
186+
/// </para>
187+
/// <para>
188+
/// It is taken only where the antiderivative was read to be continuous across the range, so that the limit
189+
/// at the end is the whole integral and not the last piece of it: <c>1/(t^2 + y^2)</c>'s antiderivative
190+
/// for <c>y^2 &lt; 0</c> is a logarithm with zeros at <c>t = ±sqrt(-y^2)</c>, which cannot be listed for a
191+
/// symbolic <c>y</c>, and taking its limits would answer 0, the principal value, where a concrete <c>y = i</c>
192+
/// answers <c>NaN</c>. What the breaks read is the real line with the parameters real: <c>arctan(t - y)</c>
193+
/// is continuous in <c>t</c> for every real <c>y</c>, but for <c>|Im y| &gt; 1</c> it crosses arctan's cut,
194+
/// and the integral of <c>1/(1 + (t - y)^2)</c> over the line is 0 there and not <c>pi</c>. That is the
195+
/// answer at a concrete such <c>y</c> as well, and before this limit was taken; it is the antiderivative's
196+
/// continuity that is not read off the real line, not this limit.
197+
/// </para>
198+
/// <para>
199+
/// A limit that is undecided, undefined, or still holds an infinity inside it -- <c>e^(-y t)</c> at
200+
/// <c>+oo</c> depends on the sign of <c>y</c>, and the limit says so by staying <c>e^(+oo * -y)</c> -- leaves
201+
/// the substituted value as it was.
202+
/// </para>
203+
/// </remarks>
204+
private static Entity? AtAnInfiniteBound(Entity antiderivative, Variable x, Entity bound, Entity value)
205+
{
206+
if (bound.Evaled is not Number.Real { EDecimal: var at } || !at.IsInfinity())
207+
return null;
208+
var settled = value.InnerSimplified;
209+
if (settled is Number || !HoldsAnInfinity(settled) || HoldsAnIncompleteGammaFunction(antiderivative))
210+
return null;
211+
var limit = LimitAtTheBound(antiderivative, x, bound, at.IsPositiveInfinity() ? ApproachFrom.Left : ApproachFrom.Right);
212+
if (limit.IsNaN || limit.Nodes.Any(node => node is Limitf) || (limit is not Number && HoldsAnInfinity(limit)))
213+
return null;
214+
return limit;
215+
}
216+
217+
private static bool HoldsAnInfinity(Entity expr)
218+
{
219+
foreach (var node in expr.Nodes)
220+
if (node is Number.Complex number && (!number.RealPart.EDecimal.IsFinite || !number.ImaginaryPart.EDecimal.IsFinite))
221+
return true;
222+
return false;
223+
}
224+
160225
/// <summary>
161226
/// The antiderivative's limit at a bound, remembered under the settings it was taken in, as
162227
/// the cache of answers remembers an antiderivative. A definite integral left as written is evaluated

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Website: https://am.angouri.org.
66
//
77

8+
using System.Linq;
89
using AngouriMath.Extensions;
910
using Xunit;
1011

@@ -90,5 +91,77 @@ public void ABoundWhoseLimitIsNotDecidedLeavesTheIntegralAsWritten(string integr
9091
[InlineData("1/x", "1", "+oo", "+oo")]
9192
public void ABoundWithAValueKeepsIt(string integrand, string from, string to, string value)
9293
=> Assert.Equal(value.ToEntity(), Definite(integrand, from, to));
94+
95+
private static Entity DefiniteOverT(string integrand, string from, string to)
96+
=> MathS.Integral(integrand.ToEntity(), "t", from.ToEntity(), to.ToEntity()).Simplify();
97+
98+
private static bool HoldsAnInfinity(Entity expr)
99+
=> expr.Nodes.Any(node => node is Entity.Number.Complex number
100+
&& (!number.RealPart.EDecimal.IsFinite || !number.ImaginaryPart.EDecimal.IsFinite));
101+
102+
private static void AgreeAtRealY(Entity got, Entity want, string what)
103+
{
104+
foreach (var at in new[] { -2.3, -0.7, 0, 0.4, 1.9 })
105+
{
106+
var g = got.Substitute("y", at).EvalNumerical();
107+
var w = want.Substitute("y", at).EvalNumerical();
108+
Assert.True(System.Math.Abs((double)(g - w).RealPart) + System.Math.Abs((double)(g - w).ImaginaryPart)
109+
< 1e-12 * System.Math.Max(1, System.Math.Abs((double)w.RealPart)),
110+
$"{what} is {g} at y = {at}, where it should be {w}");
111+
}
112+
}
113+
114+
/// <summary>
115+
/// An infinite bound substituted into an antiderivative with a parameter left the infinity inside it,
116+
/// <c>erf(+oo - y)</c>, which the arithmetic does not settle; the limit there does, reading <c>y</c> as the
117+
/// finite value an integral with a parameter is a function of. Checked against the closed forms, which were
118+
/// checked against quadrature to forty digits at the same five points.
119+
/// https://github.com/asc-community/AngouriMath/issues/2451
120+
/// </summary>
121+
[Theory]
122+
[InlineData("e^(-(t-y)^2)", "-oo", "+oo", "sqrt(pi)")]
123+
[InlineData("e^(-(t-y)^2)", "0", "+oo", "sqrt(pi) * (1 + erf(y)) / 2")]
124+
[InlineData("e^(-(t-y)^2)", "-oo", "0", "sqrt(pi) * (1 - erf(y)) / 2")]
125+
[InlineData("e^(-(t-y)^2)", "1", "+oo", "sqrt(pi) * (1 - erf(1 - y)) / 2")]
126+
[InlineData("1/(1+(t-y)^2)", "-oo", "+oo", "pi")]
127+
[InlineData("1/(1+(t-y)^2)", "0", "+oo", "pi / 2 + arctan(y)")]
128+
[InlineData("t * e^(-(t-y)^2)", "-oo", "+oo", "sqrt(pi) * y")]
129+
[InlineData("t^2 * e^(-(t-y)^2)", "-oo", "+oo", "sqrt(pi) * (1/2 + y^2)")]
130+
[InlineData("e^(-(t-y)^2/2)", "-oo", "+oo", "sqrt(2 pi)")]
131+
[InlineData("e^(-t^2 - 2 y t)", "-oo", "+oo", "sqrt(pi) * e^(y^2)")]
132+
public void Issue2451_AnInfiniteBoundWithAParameterIsALimit(string integrand, string from, string to, string value)
133+
{
134+
var result = DefiniteOverT(integrand, from, to);
135+
Assert.DoesNotContain(result.Nodes, node => node is Entity.Integralf);
136+
Assert.False(HoldsAnInfinity(result), $"{result} still holds an infinity");
137+
AgreeAtRealY(result, value.ToEntity(), $"the integral of {integrand} from {from} to {to}");
138+
}
139+
140+
/// <summary>
141+
/// And differentiated in the parameter, under the integral sign: the integral of <c>f(t - y)</c> from 0 to
142+
/// <c>+oo</c> has derivative <c>f(-y)</c> in <c>y</c>, since <c>f</c> vanishes at the infinite end.
143+
/// https://github.com/asc-community/AngouriMath/issues/2451
144+
/// </summary>
145+
[Theory]
146+
[InlineData("e^(-(t-y)^2)", "e^(-y^2)")]
147+
[InlineData("1/(1+(t-y)^2)", "1/(1+y^2)")]
148+
public void Issue2451_TheIntegralDifferentiatesBackInItsParameter(string integrand, string derivative)
149+
=> AgreeAtRealY(DefiniteOverT(integrand, "0", "+oo").Differentiate("y"), derivative.ToEntity(),
150+
$"d/dy of the integral of {integrand} from 0 to +oo");
151+
152+
/// <summary>
153+
/// Where the limit depends on more than the parameter being finite it is not decided, and the value is left as
154+
/// the substitution gave it: <c>e^(-y t)</c> vanishes at <c>+oo</c> only for a positive <c>y</c>. Nor is it
155+
/// taken where a break of the antiderivative moves with the parameter and cannot be listed: <c>1/(t - y)^2</c>
156+
/// over <c>[0, +oo)</c> diverges for a positive <c>y</c>, and <c>1/(t^2 + y^2)</c>'s logarithm for
157+
/// <c>y^2 &lt; 0</c> would answer the principal value, 0, where the integral does not exist.
158+
/// https://github.com/asc-community/AngouriMath/issues/2451
159+
/// </summary>
160+
[Theory]
161+
[InlineData("e^(-y*t)", "0", "+oo")]
162+
[InlineData("1/(t-y)^2", "0", "+oo")]
163+
[InlineData("1/(t^2+y^2)", "-oo", "+oo")]
164+
public void Issue2451_ALimitThatDependsOnTheParameterIsNotTaken(string integrand, string from, string to)
165+
=> Assert.True(HoldsAnInfinity(DefiniteOverT(integrand, from, to)));
93166
}
94167
}

0 commit comments

Comments
 (0)