diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md
index 9a6277c4a..caec10ebd 100644
--- a/BREAKING-CHANGES.md
+++ b/BREAKING-CHANGES.md
@@ -61,6 +61,22 @@ The calculus nodes and the binders — `derivative`, `integral`, `limit`, `sum`,
names the node and says to compile `.Evaled` or `Simplify()` instead. The LINQ compiler said the bound variable was not
among the arguments.
+### A definite integral whose antiderivative splits into cases finds the breaks of each case
+
+**Silent: a wrong value is corrected.** The antiderivative of `1/(t^2 + y^2)` is a case split on `y`, and its case
+`y = 0`, `-1/t`, has a pole at 0. The breaks were looked for in the whole split, where the logarithm of the case
+`y^2 < 0` cannot be listed, so none were looked for at all, and the case `y = 0` answered `-1/t` taken at the bounds:
+`-2` over `[-1, 1]`, a negative value for a positive integrand whose integral diverges
+([#2461](https://github.com/asc-community/AngouriMath/issues/2461)). Each case whose condition is on the parameters is now
+taken across the range by itself, and the case `y = 0` diverges, as `integral(1/t^2, t, -1, 1)` does. The other cases
+are as they were. With `Simplify`, the case `y = 0` of:
+
+| Input | Was (2.7.0) | Now |
+|---|---|---|
+| `integral(1/(t^2+y^2), t, -1, 1)` | `(-2) provided (y = 0)` | `+oo provided (y = 0)` |
+| `integral(1/(t^2+y^2), t, -oo, +oo)` | `0 provided (y = 0)` | `+oo provided (y = 0)` |
+| `integral(1/(t^2+y^2), t, 1, 2)` | `(1/2) provided (y = 0)` | unchanged |
+
### `factorize`, `nextprime`, `divisors`, `gradient`, `divergence` and `φ` are refused by name
**Not silent: these names followed by a bracket now throw `UnrecognizedFunctionParseException` where they were read as
@@ -106,7 +122,40 @@ before. With `Simplify`:
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.
+At a concrete such `y` it is now 0: see the next entry.
+
+### A definite integral whose antiderivative crosses a branch cut off the real line counts the jump
+
+**Silent: a wrong value is corrected, and some are no longer answered.** The breaks of an antiderivative were looked for
+on the real line, where `arctan` and the logarithm of a real argument have no cut to cross. A complex constant takes the
+argument off the line, and there it can cross one where the integrand is continuous: `arctan(t - 3i)` meets the
+imaginary axis at `-3i`, on arctan's cut, and jumps by `pi`, so the answer was off by `pi`
+([#2460](https://github.com/asc-community/AngouriMath/issues/2460)). The crossings of arctan's cut and the logarithm's
+are now placed for a polynomial argument with numeric coefficients, and for the logarithm of the cosine or the sine of a
+linear one with numeric coefficients, which `tan` and `cot` integrate to, and each jump is counted; a bound on the cut
+takes the value from inside the range. Where a crossing cannot be placed or its jump is not a constant, the integral is
+left as written: a power whose base crosses the negative real axis, a bound that is not a number, a symbol beside the
+complex constant, an argument of any other shape. With `Simplify`, against mpmath's quadrature:
+
+| Input | Was (2.7.0) | Now | Value |
+|---|---|---|---|
+| `integral(1/(1+(t-3i)^2), t, -2, 2)` | `arctan(2 - 3i) - arctan(-2 - 3i)` = 2.8198 | `arctan(2 - 3i) - arctan(-2 - 3i) - pi` | -0.3218 |
+| `integral(1/(1+(t-3i)^2), t, -oo, +oo)` | `pi` | `0` | 0 |
+| `integral(1/(1+(t-2i)^2), t, -oo, +oo)` | `pi` | `0` | 0 |
+| `integral(1/(1+(t-3i)^2), t, 0, +oo)` | `pi / 2 - arctan(-3i)` = 3.1416 + 0.3466i | `-1/2 * pi - arctan(-3i)` | 0.3466i |
+| `integral(1/((1+i)*t-5-3i), t, 0, 5)` | `(1/2 - 1/2 * i) * ln(2i) + (-1/2 + 1/2 * i) * ln(-5 - 3i)` = 1.5510 + 2.6210i | `(1/2 - 1/2 * i) * ln(2i) + (-1/2 + 1/2 * i) * ln(-5 - 3i) + (-1 - i) * pi` | -1.5906 - 0.5206i |
+| `integral(t/(1+(t-3i)^2), t, -2, 2)` | `3i * arctan(2 - 3i) + (-3i) * arctan(-2 - 3i) + (ln(-4 - 12i) - ln(-4 + 12i)) / 2` = 6.5670i | the same `+ (-2i) * pi` | 0.2838i |
+| `integral(tan(t+i), t, 0, 4)` | `ln(cos(i)) - ln(cos(4 + i))` = 0.1376 - 2.4189i | `ln(cos(i)) - ln(cos(4 + i)) + 2i * pi` | 0.1376 + 3.8643i |
+| `integral(tan((1+i)*t), t, 0, 4)` | `(-1/2 + 1/2 * i) * ln(cos(4 + 4i))` = -2.7951 + 0.5116i | `(-1/2 + 1/2 * i) * ln(cos(4 + 4i)) + (1 + i) * pi` | 0.3464 + 3.6532i |
+| `integral(tan((2-i)*t+1/2), t, 0, 2)` | `(-2/5 - 1/5 * i) * ln(cos(9/2 - 2i)) + (2/5 + 1/5 * i) * ln(cos(1/2))` = -0.9264 + 0.4322i | the same `+ (2/5 - 4/5 * i) * pi` | 0.3302 - 2.0811i |
+| `integral(sqrt((1+i)*t-5-3i), t, 0, 5)` | `4/3 * i + (-1/3 + 1/3 * i) * (-5 - 3i) ^ (3/2)` = 0.1675 - 5.3020i | left as written | 2.0531 - 3.4186i |
+| `integral(1/(1+(t-i)^2), t, -1, 1)` | `arctan(1 - i) - arctan(-1 - i)` = 2.0344 | left as written | diverges: a pole at 0 |
+| `integral(1/(1+(t-3i)^2), t, 0, s)` | `arctan((-6i + 2 * s) / 2) - arctan(-3i)` | left as written | |
+| `integral(1/(1+(t-y-3i)^2), t, -2, 2)` | `arctan((4 - 6i + (-2) * y) / 2) - arctan((-4 - 6i + (-2) * y) / 2)` | left as written | |
+| `integral(1/(1+(t-i/2)^2), t, -oo, +oo)` | `pi` | unchanged: the path stays inside the strip | `pi` |
+
+A symbol is read as real here, as everywhere the breaks are read, so `integral(1/(1+(t-y)^2), t, -oo, +oo)` is still
+`pi`, which is the value for `|Im y| < 1`.
### `dot` and `cross` of a name for a vector are nodes
diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/AntiderivativeBreaks.cs b/Sources/AngouriMath/Functions/Continuous/Integration/AntiderivativeBreaks.cs
index 77151682b..579ae0d1c 100644
--- a/Sources/AngouriMath/Functions/Continuous/Integration/AntiderivativeBreaks.cs
+++ b/Sources/AngouriMath/Functions/Continuous/Integration/AntiderivativeBreaks.cs
@@ -172,6 +172,239 @@ private static bool PeriodicZeros(Entity argument, Variable x, Real lower, Real
return true;
}
+ ///
+ /// The points of [, ] where a node of
+ /// whose argument leaves the real line crosses its branch cut, each with the
+ /// node, the point, its decimal value and the unit its jump there is a whole multiple of; an empty list where no
+ /// argument leaves the real line, or none crosses a cut; and where a crossing cannot be
+ /// ruled out or its jump cannot be settled. Bounds that are not numbers are passed as , and
+ /// any crossing at all is then one that cannot be placed.
+ ///
+ ///
+ ///
+ /// reads the real line with the parameters real, where arctan and the logarithm of a
+ /// real argument have no cut to cross. A complex constant takes the argument off the line, and the path then
+ /// can cross a cut where the integrand is continuous: arctan(t - 3i), the antiderivative of
+ /// 1/(1 + (t - 3i)^2), meets the imaginary axis at -3i, on arctan's cut, and jumps by
+ /// pi there, so its values at -2 and 2 differ by pi more than the integral between them does.
+ /// https://github.com/asc-community/AngouriMath/issues/2460
+ ///
+ ///
+ /// The cuts placed are arctan's, the imaginary axis beyond ±i, and the logarithm's, the negative
+ /// real axis: for an argument that is a polynomial with numeric coefficients, where its real or its imaginary
+ /// part, a real polynomial, is zero; and for the logarithm of the cosine or the sine of a t + b, which
+ /// tan and cot integrate to, where its imaginary part is zero (see
+ /// ). Their jumps are whole multiples of pi and of
+ /// 2 pi i / ln(base), and which multiple is read from the values a little either side. A power with an
+ /// argument off the line is taken where its base does not cross the negative real axis, since its jump there
+ /// is not a constant; a tangent, secant, cotangent or cosecant where its argument does not meet the real line,
+ /// where its poles are. Anything else with an argument off the line is not read at all.
+ ///
+ ///
+ internal static List<(Entity Node, Entity At, Real Value, Entity Unit)>? CutCrossings(Entity antiderivative, Variable x, Real? lower, Real? upper)
+ {
+ var crossings = new List<(Entity Node, Entity At, Real Value, Entity Unit)>();
+ var read = new HashSet();
+ foreach (var node in antiderivative.Nodes)
+ {
+ if (!read.Add(node) || !node.DirectChildren.Any(child => child.ContainsNode(x) && OffTheRealLine(child)))
+ continue;
+ var settled = node switch
+ {
+ Sumf or Minusf or Mulf or Divf or Absf or Signumf or Sinf or Cosf
+ or Erff or Erfcf or Erfif or Sif or Shif or Conjugatef or Ref or Imf => true,
+ // b^g = e^(g ln b) for a constant b, and an integer power of anything, are continuous wherever their
+ // argument is.
+ Powf(var @base, _) when !@base.ContainsNode(x) || !OffTheRealLine(@base) => true,
+ Powf(_, Integer) => true,
+ Powf(var @base, _) => PolynomialCrossings(@base, x, imaginaryAxis: false) is { } points
+ && points.All(point => lower is not null && upper is not null && (point.Value < lower || point.Value > upper)),
+ Tanf(var argument) => MeetsTheRealLine(argument, x) is false,
+ Secantf(var argument) => MeetsTheRealLine(argument, x) is false,
+ Cotanf(var argument) => MeetsTheRealLine(argument, x) is false,
+ Cosecantf(var argument) => MeetsTheRealLine(argument, x) is false,
+ Arctanf(var argument) => Add(node, PolynomialCrossings(argument, x, imaginaryAxis: true), MathS.pi),
+ Logf(var @base, Cosf(var argument)) when !@base.ContainsNode(x) =>
+ Add(node, TrigonometricCrossings(argument, x, lower, upper, cosine: true), 2 * MathS.pi * MathS.i / MathS.Ln(@base)),
+ Logf(var @base, Sinf(var argument)) when !@base.ContainsNode(x) =>
+ Add(node, TrigonometricCrossings(argument, x, lower, upper, cosine: false), 2 * MathS.pi * MathS.i / MathS.Ln(@base)),
+ Logf(var @base, var argument) when !@base.ContainsNode(x) =>
+ Add(node, PolynomialCrossings(argument, x, imaginaryAxis: false), 2 * MathS.pi * MathS.i / MathS.Ln(@base)),
+ _ => false
+ };
+ if (!settled)
+ return null;
+ }
+ return crossings;
+
+ bool Add(Entity node, List<(Entity At, Real Value)>? points, Entity unit)
+ {
+ if (points is null)
+ return false;
+ foreach (var (at, value) in points)
+ {
+ if (lower is null || upper is null)
+ return false;
+ if (value >= lower && value <= upper)
+ crossings.Add((node, at, value, unit));
+ }
+ return true;
+ }
+ }
+
+ /// Whether holds a constant that is not real.
+ private static bool OffTheRealLine(Entity expression)
+ {
+ foreach (var node in expression.Nodes)
+ if (node is Complex { ImaginaryPart.IsZero: false })
+ return true;
+ return false;
+ }
+
+ ///
+ /// Whether the linear takes a real value at a real , or
+ /// where it is not linear with numeric coefficients.
+ ///
+ private static bool? MeetsTheRealLine(Entity argument, Variable x)
+ => TreeAnalyzer.TryGetPolyLinear(argument, x, out var a, out var b)
+ && a.Evaled is Complex { RealPart.EDecimal.IsFinite: true, ImaginaryPart.EDecimal.IsFinite: true } slope
+ && b.Evaled is Complex { RealPart.EDecimal.IsFinite: true, ImaginaryPart.EDecimal.IsFinite: true } intercept
+ ? !slope.ImaginaryPart.IsZero || intercept.ImaginaryPart.IsZero
+ : null;
+
+ ///
+ /// The real where the polynomial crosses the imaginary axis
+ /// beyond ±i () or the negative real axis, or where
+ /// it is not a polynomial with numeric coefficients, its crossings cannot be listed, it runs along the axis, or it
+ /// meets the end of the cut, which is a singularity of the integrand.
+ ///
+ private static List<(Entity At, Real Value)>? PolynomialCrossings(Entity argument, Variable x, bool imaginaryAxis)
+ {
+ if (!TreeAnalyzer.TryGetPolynomial(argument, x, out var coefficients))
+ return null;
+ // The part that is zero on the axis, and the one that decides whether a point of it is on the cut.
+ Entity across = Integer.Zero, along = Integer.Zero;
+ var acrossIsZero = true;
+ foreach (var pair in coefficients)
+ {
+ var (power, coefficient) = (pair.Key, pair.Value);
+ if (power.Sign < 0 || coefficient.Evaled is not Complex { RealPart.EDecimal.IsFinite: true, ImaginaryPart.EDecimal.IsFinite: true } value)
+ return null;
+ var (acrossPart, alongPart) = imaginaryAxis ? (value.RealPart, value.ImaginaryPart) : (value.ImaginaryPart, value.RealPart);
+ if (!acrossPart.IsZero && !power.IsZero)
+ acrossIsZero = false;
+ // x^0 is written as 1, since 0^0 is undefined here and would take the zero at 0 out of the solution.
+ Entity monomial = power.IsZero ? Integer.One : MathS.Pow(x, Integer.Create(power));
+ if (!acrossPart.IsZero)
+ across += acrossPart * monomial;
+ if (!alongPart.IsZero)
+ along += alongPart * monomial;
+ }
+ // Constant across the axis: off it everywhere, or along it everywhere.
+ if (acrossIsZero)
+ return across.Evaled is Real { IsZero: true } ? null : new();
+ if (across.InnerSimplified.SolveEquation(x) is not FiniteSet roots)
+ return null;
+ var points = new List<(Entity At, Real Value)>();
+ foreach (var root in roots)
+ {
+ if (root.ContainsNode(x) || root.Vars.Any())
+ return null;
+ switch (root.Evaled)
+ {
+ case Real { EDecimal.IsFinite: true } at:
+ if (along.Substitute(x, root).Evaled is not Real { EDecimal.IsFinite: true } there)
+ return null;
+ var onTheCut = imaginaryAxis ? there > 1 || there < -1 : there < 0;
+ var atItsEnd = imaginaryAxis ? there >= 1 && there <= 1 || there <= -1 && there >= -1 : there.IsZero;
+ if (atItsEnd)
+ return null;
+ if (onTheCut)
+ points.Add((root, at));
+ break;
+ case Complex { ImaginaryPart.IsZero: false }:
+ break;
+ default:
+ return null;
+ }
+ }
+ return points;
+ }
+
+ ///
+ /// The real in the range where the cosine () or the sine of
+ /// a x + b, with a and b complex numbers, crosses the negative real axis, or
+ /// where the argument is not of that shape, the range is not finite where it would have
+ /// to be, or the path meets a zero.
+ ///
+ ///
+ /// With p = Re(a x + b) and q = Im(a x + b), both linear in x,
+ /// cos(p + i q) = cos p cosh q - i sin p sinh q is real where sin p = 0 or q = 0: at the
+ /// multiples of pi, negative at the odd ones, and at the one point where q = 0, negative where
+ /// cos p is there. sin(p + i q) = sin p cosh q + i cos p sinh q is real where cos p = 0 or
+ /// q = 0: negative at -pi/2 + 2 k pi, and where sin p is at the point q = 0. A point
+ /// where the value touches the axis without crossing it jumps by no multiple, which the caller reads.
+ ///
+ private static List<(Entity At, Real Value)>? TrigonometricCrossings(Entity argument, Variable x, Real? lower, Real? upper, bool cosine)
+ {
+ if (lower is null || upper is null
+ || !TreeAnalyzer.TryGetPolyLinear(argument, x, out var a, out var b)
+ || a.Evaled is not Complex { RealPart.EDecimal.IsFinite: true, ImaginaryPart.EDecimal.IsFinite: true } slope
+ || b.Evaled is not Complex { RealPart.EDecimal.IsFinite: true, ImaginaryPart.EDecimal.IsFinite: true } intercept
+ || slope.RealPart.IsZero && slope.ImaginaryPart.IsZero
+ || MathS.pi.Evaled is not Real pi)
+ return null;
+ var points = new List<(Entity At, Real Value)>();
+ // Where q = 0, if q moves at all: the value there is the real cos p or sin p.
+ if (!slope.ImaginaryPart.IsZero)
+ {
+ var at = (-(Entity)intercept.ImaginaryPart / slope.ImaginaryPart).InnerSimplified;
+ if (at.Evaled is not Real { EDecimal.IsFinite: true } value)
+ return null;
+ if (value >= lower && value <= upper)
+ {
+ var p = (slope.RealPart * (Entity)at + intercept.RealPart).InnerSimplified;
+ if ((cosine ? MathS.Cos(p) : MathS.Sin(p)).EvalNumerical() is not { ImaginaryPart.IsZero: true, RealPart: var there })
+ return null;
+ // A zero on the path is a pole of the integrand's tan or cot.
+ if (there.EDecimal.Abs().CompareTo(EDecimal.Create(1, -30)) < 0)
+ return null;
+ if (there < 0)
+ points.Add((at, value));
+ }
+ }
+ // Where sin p = 0, or cos p = 0, if p moves at all; if it does not, q does and the point above is all.
+ if (!slope.RealPart.IsZero)
+ {
+ if (!lower.EDecimal.IsFinite || !upper.EDecimal.IsFinite)
+ return null;
+ // p = c + 2 k pi, so k runs over the whole numbers between the ends mapped back.
+ Entity first = cosine ? MathS.pi : -MathS.pi / 2;
+ var shift = cosine ? pi : -pi / 2;
+ var atLower = (slope.RealPart * lower + intercept.RealPart - shift) / (2 * pi);
+ var atUpper = (slope.RealPart * upper + intercept.RealPart - shift) / (2 * pi);
+ var (from, to) = atLower < atUpper ? (atLower, atUpper) : (atUpper, atLower);
+ var k0 = from.EDecimal.RoundToIntegerNoRoundedFlag(EContext.CliDecimal.WithRounding(ERounding.Floor)).ToEInteger();
+ var k1 = to.EDecimal.RoundToIntegerNoRoundedFlag(EContext.CliDecimal.WithRounding(ERounding.Ceiling)).ToEInteger();
+ if ((k1 - k0).CompareTo(EInteger.FromInt32(MostBreaks)) > 0)
+ return null;
+ for (var k = k0; k.CompareTo(k1) <= 0; k += EInteger.One)
+ {
+ var at = ((first + 2 * Integer.Create(k) * MathS.pi - intercept.RealPart) / slope.RealPart).InnerSimplified;
+ if (at.Evaled is not Real { EDecimal.IsFinite: true } value)
+ return null;
+ // The same point as the one where q = 0 is one crossing.
+ if (value >= lower && value <= upper && !points.Any(point => Coincide(point.Value, value)))
+ points.Add((at, value));
+ }
+ }
+ else if ((cosine ? MathS.Sin(intercept.RealPart) : MathS.Cos(intercept.RealPart)).EvalNumerical() is not { RealPart: var constant }
+ || constant.EDecimal.Abs().CompareTo(EDecimal.Create(1, -30)) < 0)
+ // p is constant at a point where the value is real all along the path.
+ return null;
+ return points;
+ }
+
///
/// Two decimal values of the same point, reached by different exact spellings.
///
diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs
index afdde98f5..fe583e7ba 100644
--- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs
+++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs
@@ -75,28 +75,57 @@ 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)
+ // Case by case where the cases are on the parameters, so that the breaks of each are found in it: 1/(t^2 + y^2)
+ // is 1/t^2 at y = 0, whose antiderivative -1/t has a pole at 0, and the logarithm of the case y^2 < 0, whose
+ // zeros cannot be listed, hid it when the antiderivative was read whole -- the integral over [-1, 1] was -2
+ // there. https://github.com/asc-community/AngouriMath/issues/2461
+ if (antiderivative is Piecewise piecewise && piecewise.Cases.All(@case => !@case.Predicate.ContainsNode(x)))
+ {
+ var cases = new List();
+ foreach (var @case in piecewise.Cases)
+ {
+ if (AcrossTheRange(@case.Expression, x, from, to) is not { } value)
+ return null;
+ cases.Add(new Providedf(value, @case.Predicate));
+ }
+ return new Piecewise(cases);
+ }
+ var (lower, upper) = (from.Evaled as Number.Real, to.Evaled as Number.Real);
+ if (lower is { IsNaN: true } || upper is { IsNaN: true })
+ (lower, upper) = (null, null);
+ // Where an argument off the real line crosses a branch cut, or may, the antiderivative jumps there although the
+ // integrand does not. Between bounds that are not numbers any crossing is one that cannot be placed.
+ // https://github.com/asc-community/AngouriMath/issues/2460
+ if (AntiderivativeBreaks.CutCrossings(antiderivative, x,
+ lower is null || upper is null ? null : lower < upper ? lower : upper,
+ lower is null || upper is null ? null : lower < upper ? upper : lower) is not { } crossings)
+ return null;
+ if (lower is null || upper is null || 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);
+ var (lowest, highest) = lower < upper ? (lower, upper) : (upper, lower);
+ var breaks = AntiderivativeBreaks.Inside(antiderivative, x, lowest, highest);
+ if (AcrossTheCuts(antiderivative, x, crossings, lowest, highest, breaks) is not { } acrossTheCuts)
+ return null;
+ if (lower > upper)
+ acrossTheCuts = -acrossTheCuts;
+ // Added only where a cut was crossed, so that an answer with none is written as it was.
+ Entity WithTheCuts(Entity value) => crossings.Count > 0 ? value + acrossTheCuts : value;
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
+ ? WithTheCuts(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, continuousInside: true) is not { } atTheStart)
return null;
- var total = -atTheStart;
+ var total = WithTheCuts(-atTheStart);
foreach (var (at, _) in breaks)
{
// Approached from the side the range comes from, then left on the other.
@@ -110,6 +139,68 @@ public Entity Integrate(Variable x, Entity from, Entity to) =>
return AtTheBound(antiderivative, x, to, from, continuousInside: true) is { } atTheEnd ? total + atTheEnd : null;
}
+ ///
+ /// What the jumps of where it crosses a branch cut add to F(b) - F(a) over
+ /// [, ], or where one cannot be
+ /// settled.
+ ///
+ ///
+ /// A node that jumps by J at t0 and stands in the antiderivative with the coefficient c
+ /// makes it jump by c(t0) J, which F(b) - F(a) counts and the integral does not. At a bound on the
+ /// cut, the antiderivative's value there is the one on the cut and the integral's is the one from inside the
+ /// range, and they differ by the part of the jump on that side. So 1/(1 + (t - 3i)^2) over [-2, 2]
+ /// is arctan(2 - 3i) - arctan(-2 - 3i) - pi, and over the line 0.
+ /// https://github.com/asc-community/AngouriMath/issues/2460
+ ///
+ private static Entity? AcrossTheCuts(Entity antiderivative, Variable x,
+ List<(Entity Node, Entity At, Number.Real Value, Entity Unit)> crossings, Number.Real lower, Number.Real upper,
+ List<(Entity At, Number.Real Value)>? breaks)
+ {
+ Entity total = Number.Integer.Zero;
+ // Close enough to the cut that the value has not moved by more than a part in 10^19 of the jump.
+ Entity near = Number.Rational.Create(EInteger.One, EInteger.FromInt32(10).Pow(20));
+ foreach (var (node, at, value, unit) in crossings)
+ {
+ if (value < lower || value > upper)
+ continue;
+ // A pole or a jump of the real line at the same point would be read twice.
+ if (breaks is { } listed && listed.Any(@break => @break.Value.EDecimal.Subtract(value.EDecimal).Abs().CompareTo(EDecimal.Create(1, -30)) < 0))
+ return null;
+ var before = value > lower ? at - near : at;
+ var after = value < upper ? at + near : at;
+ if (StepAcross(node, x, before, after, unit) is not { } step)
+ return null;
+ if (step == 0)
+ continue;
+ var standIn = Variable.CreateUnique(antiderivative, "u");
+ var coefficient = antiderivative.Substitute(node, standIn).Differentiate(standIn);
+ if (coefficient.ContainsNode(standIn))
+ return null;
+ var there = coefficient.Substitute(x, at).InnerSimplified;
+ if (there.IsNaN || HoldsAnInfinity(there))
+ return null;
+ total -= there * step * unit;
+ }
+ return total.InnerSimplified;
+ }
+
+ ///
+ /// How many s moves by from to
+ /// , two points either side of a cut, or one on it and one beside it; or
+ /// where that is not a whole number.
+ ///
+ private static int? StepAcross(Entity node, Variable x, Entity before, Entity after, Entity unit)
+ {
+ var moved = ((node.Substitute(x, after) - node.Substitute(x, before)) / unit).EvalNumerical();
+ if (moved is not { RealPart.EDecimal.IsFinite: true, ImaginaryPart.EDecimal.IsFinite: true })
+ return null;
+ var real = (double)moved.RealPart;
+ var whole = System.Math.Round(real);
+ if (System.Math.Abs(real - whole) > 1e-6 || System.Math.Abs((double)moved.ImaginaryPart) > 1e-6 || System.Math.Abs(whole) > 2)
+ return null;
+ return (int)whole;
+ }
+
private static bool HoldsAnIncompleteGammaFunction(Entity expr)
{
foreach (var node in expr.Nodes)
@@ -191,9 +282,10 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr)
/// 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.
+ /// and the integral of 1/(1 + (t - y)^2) over the line is 0 there and not pi. A symbol is read as
+ /// real wherever the breaks are read, so that is the answer for a symbolic y; at a concrete y off
+ /// the line the crossing is placed, and the answer is 0 (see ).
+ /// https://github.com/asc-community/AngouriMath/issues/2460
///
///
/// A limit that is undecided, undefined, or still holds an infinity inside it -- e^(-y t) at
diff --git a/Sources/Tests/UnitTests/Calculus/IntegralAcrossABreakTest.cs b/Sources/Tests/UnitTests/Calculus/IntegralAcrossABreakTest.cs
index 9b03e4b23..8aba21fd8 100644
--- a/Sources/Tests/UnitTests/Calculus/IntegralAcrossABreakTest.cs
+++ b/Sources/Tests/UnitTests/Calculus/IntegralAcrossABreakTest.cs
@@ -69,5 +69,93 @@ public void AJumpInTheAntiderivativeIsNotCounted()
[InlineData("tan(x)", "0", "1", "-ln(cos(1))")]
public void ABreakOutsideTheRangeChangesNothing(string integrand, string from, string to, string value)
=> Assert.Equal(value.ToEntity().Simplify(), Definite(integrand, from, to));
+
+ private static void AgreesWith(Entity got, double real, double imaginary, string what)
+ {
+ Assert.DoesNotContain(got.Nodes, node => node is Entity.Integralf);
+ var value = got.EvalNumerical();
+ var error = System.Math.Abs((double)value.RealPart - real) + System.Math.Abs((double)value.ImaginaryPart - imaginary);
+ Assert.True(error < 1e-12 * System.Math.Max(1, System.Math.Abs(real) + System.Math.Abs(imaginary)),
+ $"{what} is {got} = {value}, where it is {real} + {imaginary}i");
+ }
+
+ ///
+ /// A complex constant takes the antiderivative's argument off the real line, and there it can cross a branch
+ /// cut where the integrand is continuous: arctan(x - 3i) meets arctan's cut at -3i and
+ /// jumps by pi, which F(b) - F(a) counted. The values are mpmath's quadrature to thirty digits.
+ /// https://github.com/asc-community/AngouriMath/issues/2460
+ ///
+ [Theory]
+ [InlineData("1/(1 + (x - 3i)^2)", "-2", "2", -0.321750554396642193, 0)]
+ [InlineData("1/(1 + (x + 3i)^2)", "-2", "2", -0.321750554396642193, 0)]
+ [InlineData("1/(1 + (x - 3i)^2)", "2", "-2", 0.321750554396642193, 0)]
+ [InlineData("1/(1 + (x - 3i)^2)", "-oo", "+oo", 0, 0)]
+ [InlineData("1/(1 + (x - 2i)^2)", "-oo", "+oo", 0, 0)]
+ // Within the strip |Im| < 1 the path does not meet the cut.
+ [InlineData("1/(1 + (x - i/2)^2)", "-oo", "+oo", System.Math.PI, 0)]
+ [InlineData("1/(1 + (x - i/2)^2)", "-2", "2", 2.25311288166964470, 0)]
+ // A bound on the cut takes the value from inside the range.
+ [InlineData("1/(1 + (x - 3i)^2)", "0", "2", -0.160875277198321097, 0.117500907311433888)]
+ [InlineData("1/(1 + (x - 3i)^2)", "-2", "0", -0.160875277198321097, -0.117500907311433888)]
+ [InlineData("1/(1 + (x - 3i)^2)", "0", "+oo", 0, 0.346573590279972655)]
+ [InlineData("1/(1 + ((2 - i) x - 3i)^2)", "-2", "2", -0.119090420546762822, -0.0923407433318177927)]
+ // The logarithm's cut, the negative real axis, by a linear argument, by a quadratic one, and under a cosine
+ // and a sine.
+ [InlineData("1/((1 + i) x - 5 - 3i)", "0", "5", -1.59062445440680808, -0.520591372658672695)]
+ [InlineData("x/(1 + (x - 3i)^2)", "-2", "2", 0, 0.283794109208327846)]
+ // A power whose base crosses the negative real axis only outside the range.
+ [InlineData("sqrt((1 + i) x - 5 - 3i)", "0", "2", 0.957866108597855882, -4.10504061682829902)]
+ [InlineData("tan(x + i)", "0", "1", 0.176464319095173717, 0.870327424991119277)]
+ [InlineData("tan(x + i)", "0", "4", 0.137574017201678, 3.86425535734661)]
+ [InlineData("tan(x - i/2)", "-4", "4", 0, -7.2657696334242)]
+ [InlineData("cot(x + i)", "-2", "1", -0.0276416386049222, -3.02215428925017)]
+ // A complex slope: the imaginary part of the cosine's argument is zero at one point, and its real part at
+ // multiples of pi.
+ [InlineData("tan((1 + i) x)", "0", "1", 0.306505456801632903, 0.563821968189486374)]
+ [InlineData("tan((1 + i) x)", "0", "4", 0.346432013793773035, 3.65323607730064999)]
+ [InlineData("tan((2 - i) x + 1/2)", "0", "2", 0.330209656421608542, -2.0810570895816348)]
+ [InlineData("cot((1 + i) x + 1)", "0", "3", 0.0240825030271072548, -2.45573766107787857)]
+ [InlineData("tan(i x + 1)", "-2", "2", 1.96643859505754655, 0)]
+ public void Issue2460_ACrossingOfABranchCutIsCounted(string integrand, string from, string to, double real, double imaginary)
+ => AgreesWith(Definite(integrand, from, to), real, imaginary, $"the integral of {integrand} from {from} to {to}");
+
+ ///
+ /// Where a crossing cannot be placed or its jump settled, the integral is left as written: a power's jump is
+ /// not a constant, a bound that is not a number cannot say whether the crossing is in the range, and a symbol
+ /// beside the complex constant cannot say where it is. Each of these answered F(b) - F(a), and
+ /// 1/(1 + (x - i)^2), which has a pole at 0, answered a finite value for the range across it.
+ /// https://github.com/asc-community/AngouriMath/issues/2460
+ ///
+ [Theory]
+ [InlineData("sqrt((1 + i) x - 5 - 3i)", "0", "5")]
+ [InlineData("1/(1 + (x - 3i)^2)", "0", "s")]
+ [InlineData("1/(1 + (x - y - 3i)^2)", "-2", "2")]
+ [InlineData("1/(1 + (x - i)^2)", "-1", "1")]
+ public void Issue2460_ACrossingThatCannotBeSettledIsNotAnswered(string integrand, string from, string to)
+ => Assert.Contains(Definite(integrand, from, to).Nodes, node => node is Entity.Integralf);
+
+ ///
+ /// The antiderivative of 1/(x^2 + y^2) is a case split on y, and its case y = 0, -1/x,
+ /// has a pole at 0 that was not looked for, since the breaks were read on the whole split and the logarithm of
+ /// the case y^2 < 0 could not be listed: the integral over [-1, 1] was -2 at
+ /// y = 0. Each case is now taken across the range by itself, and that one diverges, as 1/x^2 does.
+ /// https://github.com/asc-community/AngouriMath/issues/2461
+ ///
+ [Theory]
+ [InlineData("1/(x^2 + y^2)", "-1", "1", "+oo")]
+ [InlineData("1/(x^2 + y^2)", "-oo", "+oo", "+oo")]
+ [InlineData("1/(x^2 + y^2)", "1", "2", "1/2")]
+ [InlineData("1/(x^2 + y^2)", "1", "-1", "-oo")]
+ public void Issue2461_TheCaseOfAParameterIsTakenAcrossTheRangeByItself(string integrand, string from, string to, string atZero)
+ => Assert.Equal(atZero.ToEntity().Simplify(), Definite(integrand, from, to).Substitute("y", 0).Simplify());
+
+ /// And the other cases are as they were: 2 arctan(1/|y|)/|y| over [-1, 1] for a real y.
+ [Theory]
+ [InlineData(0.5)]
+ [InlineData(-0.7)]
+ [InlineData(2)]
+ public void Issue2461_ANonzeroParameterKeepsItsValue(double y)
+ => AgreesWith(Definite("1/(x^2 + y^2)", "-1", "1").Substitute("y", y).Simplify(),
+ 2 * System.Math.Atan(1 / System.Math.Abs(y)) / System.Math.Abs(y), 0, $"the integral of 1/(x^2 + y^2) over [-1, 1] at y = {y}");
}
}