Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -125,12 +133,19 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr)
/// undefined, the value stays undefined, as it is for <c>sin(x)</c> from 0 to <c>+oo</c>,
/// whose antiderivative has no limit there. Where the limit is not decided there is no value,
/// <see langword="null"/>, and the integral is left as written.
/// <para>
/// 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: <c>erf(+oo - y)</c>, the antiderivative
/// of <c>e^(-(t - y)^2)</c> at <c>+oo</c>. That is not a value either, and the limit is taken there too
/// (<paramref name="continuousInside"/> permitting; see <see cref="AtAnInfiniteBound"/>).
/// https://github.com/asc-community/AngouriMath/issues/2451
/// </para>
/// </remarks>
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
{
Expand All @@ -157,6 +172,56 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr)
return limit.IsNaN ? value : limit;
}

/// <summary>
/// The antiderivative's limit at an infinite bound, where substituting the bound left an infinity inside
/// the value, or <see langword="null"/> to keep the substituted value as it was.
/// </summary>
/// <remarks>
/// <para>
/// <c>erf(+oo - y)</c> is left as it is by the arithmetic, since <c>y</c> 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 <c>erf(t - y)</c> as <c>t</c> grows reads <c>y</c> as exactly that, a finite constant, and is 1. So
/// <c>e^(-(t - y)^2)</c> over the line is <c>sqrt(pi)</c> rather than a difference of two unsettled error
/// functions. https://github.com/asc-community/AngouriMath/issues/2451
/// </para>
/// <para>
/// 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: <c>1/(t^2 + y^2)</c>'s antiderivative
/// 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
/// symbolic <c>y</c>, and taking its limits would answer 0, the principal value, where a concrete <c>y = i</c>
/// answers <c>NaN</c>. What the breaks read is the real line with the parameters real: <c>arctan(t - y)</c>
/// 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,
/// 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
/// answer at a concrete such <c>y</c> 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.
/// </para>
/// <para>
/// A limit that is undecided, undefined, or still holds an infinity inside it -- <c>e^(-y t)</c> at
/// <c>+oo</c> depends on the sign of <c>y</c>, and the limit says so by staying <c>e^(+oo * -y)</c> -- leaves
/// the substituted value as it was.
/// </para>
/// </remarks>
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;
}

/// <summary>
/// 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
Expand Down
73 changes: 73 additions & 0 deletions Sources/Tests/UnitTests/Calculus/ImproperIntegralTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Website: https://am.angouri.org.
//

using System.Linq;
using AngouriMath.Extensions;
using Xunit;

Expand Down Expand Up @@ -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}");
}
}

/// <summary>
/// An infinite bound substituted into an antiderivative with a parameter left the infinity inside it,
/// <c>erf(+oo - y)</c>, which the arithmetic does not settle; the limit there does, reading <c>y</c> 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
/// </summary>
[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}");
}

/// <summary>
/// And differentiated in the parameter, under the integral sign: the integral of <c>f(t - y)</c> from 0 to
/// <c>+oo</c> has derivative <c>f(-y)</c> in <c>y</c>, since <c>f</c> vanishes at the infinite end.
/// https://github.com/asc-community/AngouriMath/issues/2451
/// </summary>
[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");

/// <summary>
/// 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: <c>e^(-y t)</c> vanishes at <c>+oo</c> only for a positive <c>y</c>. Nor is it
/// taken where a break of the antiderivative moves with the parameter and cannot be listed: <c>1/(t - y)^2</c>
/// over <c>[0, +oo)</c> diverges for a positive <c>y</c>, and <c>1/(t^2 + y^2)</c>'s logarithm for
/// <c>y^2 &lt; 0</c> would answer the principal value, 0, where the integral does not exist.
/// https://github.com/asc-community/AngouriMath/issues/2451
/// </summary>
[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)));
}
}
Loading