diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 3b341c47f..0f882f264 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -83,6 +83,31 @@ Euler's totient is `phi(n)`, and `φ(n)` and `ϕ(n)` are refused. Each name with | `"divergence(x)".ToEntity()` | `divergence * x` | throws `UnrecognizedFunctionParseException` | | `"gradient(x^2*y, [x, y])".ToEntity()` | throws `UnhandledParseException` | throws `UnrecognizedFunctionParseException` | +### A definite integral with a parameter takes the limit at an infinite bound + +**Silent: an unsettled value is now settled.** An infinite bound was substituted into the antiderivative, and where a +parameter stood beside it the infinity stayed inside a function: `erf(+oo - y)`, since `y` could itself be infinite. The +limit is now taken there, reading the parameter as the finite value the integral is a function of +([#2451](https://github.com/asc-community/AngouriMath/issues/2451)). It is taken only where the antiderivative was read to +be continuous across the range, and only where the limit settles without an infinity left in it; otherwise the value is as +before. With `Simplify`: + +| Input | Was (2.7.0) | Now | +|---|---|---| +| `integral(e^(-(t-y)^2), t, -oo, +oo)` | `sqrt(pi) * (erf(+oo - y) - erf(-oo - y)) / 2` | `sqrt(pi)` | +| `integral(e^(-(t-y)^2), t, 0, +oo)` | `sqrt(pi) * (erf(+oo - y) - erf(-y)) / 2` | `sqrt(pi) * (1/2 + -1/2 * erf(-y))` | +| `integral(1/(1+(t-y)^2), t, -oo, +oo)` | `arctan((+oo + (-2) * y) / 2) - arctan(((-2) * y - +oo) / 2)` | `pi` | +| `integral(1/(1+(t-y)^2), t, 0, +oo)` | `arctan((+oo + (-2) * y) / 2) - arctan(-y)` | `pi / 2 - arctan(-y)` | +| `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` | +| `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` | +| `integral(e^(-y*t), t, 0, +oo)` | `(1 - e ^ ((-oo) * y)) / y` | unchanged: the limit depends on the sign of `y` | +| `integral(1/(t-y)^2, t, 0, +oo)` | `(-oo) / (y * (+oo - y))` | unchanged: the pole at `y` cannot be placed | +| `integral(1/(1+(t-y)^2), t, -oo, y)` | `-arctan(((-2) * y - +oo) / 2)` | unchanged: a symbolic bound | + +The continuity read is on the real line with real parameters, as it is for finite bounds: for a non-real `y` with +`|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`. +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. + ### `dot` and `cross` of a name for a vector are nodes **Not silent: what was refused now parses.** `dot(a, b)` and `cross(a, b)` threw `InvalidArgumentParseException` diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index 88fd5b114..afdde98f5 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -76,17 +76,25 @@ public Entity Integrate(Variable x, Entity from, Entity to) => private static Entity? AcrossTheRange(Entity antiderivative, Variable x, Entity from, Entity to) { if (from.Evaled is not Number.Real { IsNaN: false } lower || to.Evaled is not Number.Real { IsNaN: false } upper - || lower == upper - || (lower < upper - ? AntiderivativeBreaks.Inside(antiderivative, x, lower, upper) - : AntiderivativeBreaks.Inside(antiderivative, x, upper, lower)) is not { Count: > 0 } breaks) - return AtTheBound(antiderivative, x, to, from) is { } atTheTop && AtTheBound(antiderivative, x, from, to) is { } atTheBottom + || lower == upper) + return AtTheBound(antiderivative, x, to, from, continuousInside: false) is { } atTheTop + && AtTheBound(antiderivative, x, from, to, continuousInside: false) is { } atTheBottom + ? atTheTop - atTheBottom + : null; + var breaks = lower < upper + ? AntiderivativeBreaks.Inside(antiderivative, x, lower, upper) + : AntiderivativeBreaks.Inside(antiderivative, x, upper, lower); + if (breaks is not { Count: > 0 }) + // An empty list is a range the antiderivative was read to be continuous across; no list, one where a + // break could not be listed. + return AtTheBound(antiderivative, x, to, from, continuousInside: breaks is not null) is { } atTheTop + && AtTheBound(antiderivative, x, from, to, continuousInside: breaks is not null) is { } atTheBottom ? atTheTop - atTheBottom : null; if (lower > upper) breaks.Reverse(); // Not across a break of an antiderivative in the incomplete gamma function: see AtTheBound. - if (HoldsAnIncompleteGammaFunction(antiderivative) || AtTheBound(antiderivative, x, from, to) is not { } atTheStart) + if (HoldsAnIncompleteGammaFunction(antiderivative) || AtTheBound(antiderivative, x, from, to, continuousInside: true) is not { } atTheStart) return null; var total = -atTheStart; foreach (var (at, _) in breaks) @@ -99,7 +107,7 @@ public Entity Integrate(Variable x, Entity from, Entity to) => return null; total += before - after; } - return AtTheBound(antiderivative, x, to, from) is { } atTheEnd ? total + atTheEnd : null; + return AtTheBound(antiderivative, x, to, from, continuousInside: true) is { } atTheEnd ? total + atTheEnd : null; } private static bool HoldsAnIncompleteGammaFunction(Entity expr) @@ -125,12 +133,19 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr) /// undefined, the value stays undefined, as it is for sin(x) from 0 to +oo, /// whose antiderivative has no limit there. Where the limit is not decided there is no value, /// , and the integral is left as written. + /// + /// An infinite bound substituted can also leave an infinity standing inside the value, where the + /// arithmetic cannot settle it because a parameter is in the way: erf(+oo - y), the antiderivative + /// of e^(-(t - y)^2) at +oo. That is not a value either, and the limit is taken there too + /// ( permitting; see ). + /// https://github.com/asc-community/AngouriMath/issues/2451 + /// /// - private static Entity? AtTheBound(Entity antiderivative, Variable x, Entity bound, Entity otherBound) + private static Entity? AtTheBound(Entity antiderivative, Variable x, Entity bound, Entity otherBound, bool continuousInside) { var value = antiderivative.Substitute(x, bound); if (!value.InnerSimplified.IsNaN) - return value; + return continuousInside && AtAnInfiniteBound(antiderivative, x, bound, value) is { } atInfinity ? atInfinity : value; // The bounds' values, so that a bound written as pi or 2 pi is placed as well as 3. ApproachFrom? side = (bound.Evaled, otherBound.Evaled) switch { @@ -157,6 +172,56 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr) return limit.IsNaN ? value : limit; } + /// + /// The antiderivative's limit at an infinite bound, where substituting the bound left an infinity inside + /// the value, or to keep the substituted value as it was. + /// + /// + /// + /// erf(+oo - y) is left as it is by the arithmetic, since y could itself be infinite; but an + /// integral with a parameter is a function of that parameter at each of its finite values, and the limit + /// of erf(t - y) as t grows reads y as exactly that, a finite constant, and is 1. So + /// e^(-(t - y)^2) over the line is sqrt(pi) rather than a difference of two unsettled error + /// functions. https://github.com/asc-community/AngouriMath/issues/2451 + /// + /// + /// It is taken only where the antiderivative was read to be continuous across the range, so that the limit + /// at the end is the whole integral and not the last piece of it: 1/(t^2 + y^2)'s antiderivative + /// for y^2 < 0 is a logarithm with zeros at t = ±sqrt(-y^2), which cannot be listed for a + /// symbolic y, and taking its limits would answer 0, the principal value, where a concrete y = i + /// answers NaN. What the breaks read is the real line with the parameters real: arctan(t - y) + /// is continuous in t for every real y, but for |Im y| > 1 it crosses arctan's cut, + /// and the integral of 1/(1 + (t - y)^2) over the line is 0 there and not pi. That is the + /// answer at a concrete such y as well, and before this limit was taken; it is the antiderivative's + /// continuity that is not read off the real line, not this limit. + /// + /// + /// A limit that is undecided, undefined, or still holds an infinity inside it -- e^(-y t) at + /// +oo depends on the sign of y, and the limit says so by staying e^(+oo * -y) -- leaves + /// the substituted value as it was. + /// + /// + private static Entity? AtAnInfiniteBound(Entity antiderivative, Variable x, Entity bound, Entity value) + { + if (bound.Evaled is not Number.Real { EDecimal: var at } || !at.IsInfinity()) + return null; + var settled = value.InnerSimplified; + if (settled is Number || !HoldsAnInfinity(settled) || HoldsAnIncompleteGammaFunction(antiderivative)) + return null; + var limit = LimitAtTheBound(antiderivative, x, bound, at.IsPositiveInfinity() ? ApproachFrom.Left : ApproachFrom.Right); + if (limit.IsNaN || limit.Nodes.Any(node => node is Limitf) || (limit is not Number && HoldsAnInfinity(limit))) + return null; + return limit; + } + + private static bool HoldsAnInfinity(Entity expr) + { + foreach (var node in expr.Nodes) + if (node is Number.Complex number && (!number.RealPart.EDecimal.IsFinite || !number.ImaginaryPart.EDecimal.IsFinite)) + return true; + return false; + } + /// /// The antiderivative's limit at a bound, remembered under the settings it was taken in, as /// the cache of answers remembers an antiderivative. A definite integral left as written is evaluated diff --git a/Sources/Tests/UnitTests/Calculus/ImproperIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/ImproperIntegralTest.cs index 77b7c4f39..ac7723b1c 100644 --- a/Sources/Tests/UnitTests/Calculus/ImproperIntegralTest.cs +++ b/Sources/Tests/UnitTests/Calculus/ImproperIntegralTest.cs @@ -5,6 +5,7 @@ // Website: https://am.angouri.org. // +using System.Linq; using AngouriMath.Extensions; using Xunit; @@ -90,5 +91,77 @@ public void ABoundWhoseLimitIsNotDecidedLeavesTheIntegralAsWritten(string integr [InlineData("1/x", "1", "+oo", "+oo")] public void ABoundWithAValueKeepsIt(string integrand, string from, string to, string value) => Assert.Equal(value.ToEntity(), Definite(integrand, from, to)); + + private static Entity DefiniteOverT(string integrand, string from, string to) + => MathS.Integral(integrand.ToEntity(), "t", from.ToEntity(), to.ToEntity()).Simplify(); + + private static bool HoldsAnInfinity(Entity expr) + => expr.Nodes.Any(node => node is Entity.Number.Complex number + && (!number.RealPart.EDecimal.IsFinite || !number.ImaginaryPart.EDecimal.IsFinite)); + + private static void AgreeAtRealY(Entity got, Entity want, string what) + { + foreach (var at in new[] { -2.3, -0.7, 0, 0.4, 1.9 }) + { + var g = got.Substitute("y", at).EvalNumerical(); + var w = want.Substitute("y", at).EvalNumerical(); + Assert.True(System.Math.Abs((double)(g - w).RealPart) + System.Math.Abs((double)(g - w).ImaginaryPart) + < 1e-12 * System.Math.Max(1, System.Math.Abs((double)w.RealPart)), + $"{what} is {g} at y = {at}, where it should be {w}"); + } + } + + /// + /// An infinite bound substituted into an antiderivative with a parameter left the infinity inside it, + /// erf(+oo - y), which the arithmetic does not settle; the limit there does, reading y as the + /// finite value an integral with a parameter is a function of. Checked against the closed forms, which were + /// checked against quadrature to forty digits at the same five points. + /// https://github.com/asc-community/AngouriMath/issues/2451 + /// + [Theory] + [InlineData("e^(-(t-y)^2)", "-oo", "+oo", "sqrt(pi)")] + [InlineData("e^(-(t-y)^2)", "0", "+oo", "sqrt(pi) * (1 + erf(y)) / 2")] + [InlineData("e^(-(t-y)^2)", "-oo", "0", "sqrt(pi) * (1 - erf(y)) / 2")] + [InlineData("e^(-(t-y)^2)", "1", "+oo", "sqrt(pi) * (1 - erf(1 - y)) / 2")] + [InlineData("1/(1+(t-y)^2)", "-oo", "+oo", "pi")] + [InlineData("1/(1+(t-y)^2)", "0", "+oo", "pi / 2 + arctan(y)")] + [InlineData("t * e^(-(t-y)^2)", "-oo", "+oo", "sqrt(pi) * y")] + [InlineData("t^2 * e^(-(t-y)^2)", "-oo", "+oo", "sqrt(pi) * (1/2 + y^2)")] + [InlineData("e^(-(t-y)^2/2)", "-oo", "+oo", "sqrt(2 pi)")] + [InlineData("e^(-t^2 - 2 y t)", "-oo", "+oo", "sqrt(pi) * e^(y^2)")] + public void Issue2451_AnInfiniteBoundWithAParameterIsALimit(string integrand, string from, string to, string value) + { + var result = DefiniteOverT(integrand, from, to); + Assert.DoesNotContain(result.Nodes, node => node is Entity.Integralf); + Assert.False(HoldsAnInfinity(result), $"{result} still holds an infinity"); + AgreeAtRealY(result, value.ToEntity(), $"the integral of {integrand} from {from} to {to}"); + } + + /// + /// And differentiated in the parameter, under the integral sign: the integral of f(t - y) from 0 to + /// +oo has derivative f(-y) in y, since f vanishes at the infinite end. + /// https://github.com/asc-community/AngouriMath/issues/2451 + /// + [Theory] + [InlineData("e^(-(t-y)^2)", "e^(-y^2)")] + [InlineData("1/(1+(t-y)^2)", "1/(1+y^2)")] + public void Issue2451_TheIntegralDifferentiatesBackInItsParameter(string integrand, string derivative) + => AgreeAtRealY(DefiniteOverT(integrand, "0", "+oo").Differentiate("y"), derivative.ToEntity(), + $"d/dy of the integral of {integrand} from 0 to +oo"); + + /// + /// Where the limit depends on more than the parameter being finite it is not decided, and the value is left as + /// the substitution gave it: e^(-y t) vanishes at +oo only for a positive y. Nor is it + /// taken where a break of the antiderivative moves with the parameter and cannot be listed: 1/(t - y)^2 + /// over [0, +oo) diverges for a positive y, and 1/(t^2 + y^2)'s logarithm for + /// y^2 < 0 would answer the principal value, 0, where the integral does not exist. + /// https://github.com/asc-community/AngouriMath/issues/2451 + /// + [Theory] + [InlineData("e^(-y*t)", "0", "+oo")] + [InlineData("1/(t-y)^2", "0", "+oo")] + [InlineData("1/(t^2+y^2)", "-oo", "+oo")] + public void Issue2451_ALimitThatDependsOnTheParameterIsNotTaken(string integrand, string from, string to) + => Assert.True(HoldsAnInfinity(DefiniteOverT(integrand, from, to))); } }