Skip to content

Commit 28682b1

Browse files
Rafael-SOWNetclaude
andcommitted
Count the jump where a definite integral's antiderivative crosses a branch cut off the real line
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: arctan(t - 3i) meets arctan's cut at -3i and jumps by pi, so the integral of 1/(1 + (t - 3i)^2) over [-2, 2] was 2.8198 where it is -0.3218, and over the line pi where it is 0. 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 argument with numeric coefficients, which tan and cot integrate to: the cosine of p + i q is real where sin p = 0 or q = 0, a periodic set and a single point of t, and so is the sine where cos p = 0 or q = 0. Each jump is a whole multiple of pi or 2 pi i / ln(base), read from the values either side, and is counted with the node's coefficient; 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 -- a power whose base crosses the negative real axis, a bound that is not a number, a symbol beside the complex constant, any other argument off the line -- the integral is left as written. A symbol is still read as real, as everywhere the breaks are read, so integral(1/(1+(t-y)^2), t, -oo, +oo) stays pi. Part of #2460 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5ba95bc commit 28682b1

4 files changed

Lines changed: 418 additions & 11 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,40 @@ before. With `Simplify`:
106106

107107
The continuity read is on the real line with real parameters, as it is for finite bounds: for a non-real `y` with
108108
`|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.
109+
At a concrete such `y` it is now 0: see the next entry.
110+
111+
### A definite integral whose antiderivative crosses a branch cut off the real line counts the jump
112+
113+
**Silent: a wrong value is corrected, and some are no longer answered.** The breaks of an antiderivative were looked for
114+
on the real line, where `arctan` and the logarithm of a real argument have no cut to cross. A complex constant takes the
115+
argument off the line, and there it can cross one where the integrand is continuous: `arctan(t - 3i)` meets the
116+
imaginary axis at `-3i`, on arctan's cut, and jumps by `pi`, so the answer was off by `pi`
117+
([#2460](https://github.com/asc-community/AngouriMath/issues/2460)). The crossings of arctan's cut and the logarithm's
118+
are now placed for a polynomial argument with numeric coefficients, and for the logarithm of the cosine or the sine of a
119+
linear one with numeric coefficients, which `tan` and `cot` integrate to, and each jump is counted; a bound on the cut
120+
takes the value from inside the range. Where a crossing cannot be placed or its jump is not a constant, the integral is
121+
left as written: a power whose base crosses the negative real axis, a bound that is not a number, a symbol beside the
122+
complex constant, an argument of any other shape. With `Simplify`, against mpmath's quadrature:
123+
124+
| Input | Was (2.7.0) | Now | Value |
125+
|---|---|---|---|
126+
| `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 |
127+
| `integral(1/(1+(t-3i)^2), t, -oo, +oo)` | `pi` | `0` | 0 |
128+
| `integral(1/(1+(t-2i)^2), t, -oo, +oo)` | `pi` | `0` | 0 |
129+
| `integral(1/(1+(t-3i)^2), t, 0, +oo)` | `pi / 2 - arctan(-3i)` = 3.1416 + 0.3466i | `-1/2 * pi - arctan(-3i)` | 0.3466i |
130+
| `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 |
131+
| `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 |
132+
| `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 |
133+
| `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 |
134+
| `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 |
135+
| `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 |
136+
| `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 |
137+
| `integral(1/(1+(t-3i)^2), t, 0, s)` | `arctan((-6i + 2 * s) / 2) - arctan(-3i)` | left as written | |
138+
| `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 | |
139+
| `integral(1/(1+(t-i/2)^2), t, -oo, +oo)` | `pi` | unchanged: the path stays inside the strip | `pi` |
140+
141+
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
142+
`pi`, which is the value for `|Im y| < 1`.
110143

111144
### `dot` and `cross` of a name for a vector are nodes
112145

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

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,239 @@ private static bool PeriodicZeros(Entity argument, Variable x, Real lower, Real
172172
return true;
173173
}
174174

175+
/// <summary>
176+
/// The points of <c>[<paramref name="lower"/>, <paramref name="upper"/>]</c> where a node of
177+
/// <paramref name="antiderivative"/> whose argument leaves the real line crosses its branch cut, each with the
178+
/// node, the point, its decimal value and the unit its jump there is a whole multiple of; an empty list where no
179+
/// argument leaves the real line, or none crosses a cut; and <see langword="null"/> where a crossing cannot be
180+
/// ruled out or its jump cannot be settled. Bounds that are not numbers are passed as <see langword="null"/>, and
181+
/// any crossing at all is then one that cannot be placed.
182+
/// </summary>
183+
/// <remarks>
184+
/// <para>
185+
/// <see cref="Inside"/> reads the real line with the parameters real, where <c>arctan</c> and the logarithm of a
186+
/// real argument have no cut to cross. A complex constant takes the argument off the line, and the path then
187+
/// can cross a cut where the integrand is continuous: <c>arctan(t - 3i)</c>, the antiderivative of
188+
/// <c>1/(1 + (t - 3i)^2)</c>, meets the imaginary axis at <c>-3i</c>, on <c>arctan</c>'s cut, and jumps by
189+
/// <c>pi</c> there, so its values at -2 and 2 differ by <c>pi</c> more than the integral between them does.
190+
/// https://github.com/asc-community/AngouriMath/issues/2460
191+
/// </para>
192+
/// <para>
193+
/// The cuts placed are <c>arctan</c>'s, the imaginary axis beyond <c>±i</c>, and the logarithm's, the negative
194+
/// real axis: for an argument that is a polynomial with numeric coefficients, where its real or its imaginary
195+
/// part, a real polynomial, is zero; and for the logarithm of the cosine or the sine of <c>a t + b</c>, which
196+
/// <c>tan</c> and <c>cot</c> integrate to, where its imaginary part is zero (see
197+
/// <see cref="TrigonometricCrossings"/>). Their jumps are whole multiples of <c>pi</c> and of
198+
/// <c>2 pi i / ln(base)</c>, and which multiple is read from the values a little either side. A power with an
199+
/// argument off the line is taken where its base does not cross the negative real axis, since its jump there
200+
/// is not a constant; a tangent, secant, cotangent or cosecant where its argument does not meet the real line,
201+
/// where its poles are. Anything else with an argument off the line is not read at all.
202+
/// </para>
203+
/// </remarks>
204+
internal static List<(Entity Node, Entity At, Real Value, Entity Unit)>? CutCrossings(Entity antiderivative, Variable x, Real? lower, Real? upper)
205+
{
206+
var crossings = new List<(Entity Node, Entity At, Real Value, Entity Unit)>();
207+
var read = new HashSet<Entity>();
208+
foreach (var node in antiderivative.Nodes)
209+
{
210+
if (!read.Add(node) || !node.DirectChildren.Any(child => child.ContainsNode(x) && OffTheRealLine(child)))
211+
continue;
212+
var settled = node switch
213+
{
214+
Sumf or Minusf or Mulf or Divf or Absf or Signumf or Sinf or Cosf
215+
or Erff or Erfcf or Erfif or Sif or Shif or Conjugatef or Ref or Imf => true,
216+
// b^g = e^(g ln b) for a constant b, and an integer power of anything, are continuous wherever their
217+
// argument is.
218+
Powf(var @base, _) when !@base.ContainsNode(x) || !OffTheRealLine(@base) => true,
219+
Powf(_, Integer) => true,
220+
Powf(var @base, _) => PolynomialCrossings(@base, x, imaginaryAxis: false) is { } points
221+
&& points.All(point => lower is not null && upper is not null && (point.Value < lower || point.Value > upper)),
222+
Tanf(var argument) => MeetsTheRealLine(argument, x) is false,
223+
Secantf(var argument) => MeetsTheRealLine(argument, x) is false,
224+
Cotanf(var argument) => MeetsTheRealLine(argument, x) is false,
225+
Cosecantf(var argument) => MeetsTheRealLine(argument, x) is false,
226+
Arctanf(var argument) => Add(node, PolynomialCrossings(argument, x, imaginaryAxis: true), MathS.pi),
227+
Logf(var @base, Cosf(var argument)) when !@base.ContainsNode(x) =>
228+
Add(node, TrigonometricCrossings(argument, x, lower, upper, cosine: true), 2 * MathS.pi * MathS.i / MathS.Ln(@base)),
229+
Logf(var @base, Sinf(var argument)) when !@base.ContainsNode(x) =>
230+
Add(node, TrigonometricCrossings(argument, x, lower, upper, cosine: false), 2 * MathS.pi * MathS.i / MathS.Ln(@base)),
231+
Logf(var @base, var argument) when !@base.ContainsNode(x) =>
232+
Add(node, PolynomialCrossings(argument, x, imaginaryAxis: false), 2 * MathS.pi * MathS.i / MathS.Ln(@base)),
233+
_ => false
234+
};
235+
if (!settled)
236+
return null;
237+
}
238+
return crossings;
239+
240+
bool Add(Entity node, List<(Entity At, Real Value)>? points, Entity unit)
241+
{
242+
if (points is null)
243+
return false;
244+
foreach (var (at, value) in points)
245+
{
246+
if (lower is null || upper is null)
247+
return false;
248+
if (value >= lower && value <= upper)
249+
crossings.Add((node, at, value, unit));
250+
}
251+
return true;
252+
}
253+
}
254+
255+
/// <summary>Whether <paramref name="expression"/> holds a constant that is not real.</summary>
256+
private static bool OffTheRealLine(Entity expression)
257+
{
258+
foreach (var node in expression.Nodes)
259+
if (node is Complex { ImaginaryPart.IsZero: false })
260+
return true;
261+
return false;
262+
}
263+
264+
/// <summary>
265+
/// Whether the linear <paramref name="argument"/> takes a real value at a real <paramref name="x"/>, or
266+
/// <see langword="null"/> where it is not linear with numeric coefficients.
267+
/// </summary>
268+
private static bool? MeetsTheRealLine(Entity argument, Variable x)
269+
=> TreeAnalyzer.TryGetPolyLinear(argument, x, out var a, out var b)
270+
&& a.Evaled is Complex { RealPart.EDecimal.IsFinite: true, ImaginaryPart.EDecimal.IsFinite: true } slope
271+
&& b.Evaled is Complex { RealPart.EDecimal.IsFinite: true, ImaginaryPart.EDecimal.IsFinite: true } intercept
272+
? !slope.ImaginaryPart.IsZero || intercept.ImaginaryPart.IsZero
273+
: null;
274+
275+
/// <summary>
276+
/// The real <paramref name="x"/> where the polynomial <paramref name="argument"/> crosses the imaginary axis
277+
/// beyond <c>±i</c> (<paramref name="imaginaryAxis"/>) or the negative real axis, or <see langword="null"/> where
278+
/// it is not a polynomial with numeric coefficients, its crossings cannot be listed, it runs along the axis, or it
279+
/// meets the end of the cut, which is a singularity of the integrand.
280+
/// </summary>
281+
private static List<(Entity At, Real Value)>? PolynomialCrossings(Entity argument, Variable x, bool imaginaryAxis)
282+
{
283+
if (!TreeAnalyzer.TryGetPolynomial(argument, x, out var coefficients))
284+
return null;
285+
// The part that is zero on the axis, and the one that decides whether a point of it is on the cut.
286+
Entity across = Integer.Zero, along = Integer.Zero;
287+
var acrossIsZero = true;
288+
foreach (var pair in coefficients)
289+
{
290+
var (power, coefficient) = (pair.Key, pair.Value);
291+
if (power.Sign < 0 || coefficient.Evaled is not Complex { RealPart.EDecimal.IsFinite: true, ImaginaryPart.EDecimal.IsFinite: true } value)
292+
return null;
293+
var (acrossPart, alongPart) = imaginaryAxis ? (value.RealPart, value.ImaginaryPart) : (value.ImaginaryPart, value.RealPart);
294+
if (!acrossPart.IsZero && !power.IsZero)
295+
acrossIsZero = false;
296+
// x^0 is written as 1, since 0^0 is undefined here and would take the zero at 0 out of the solution.
297+
Entity monomial = power.IsZero ? Integer.One : MathS.Pow(x, Integer.Create(power));
298+
if (!acrossPart.IsZero)
299+
across += acrossPart * monomial;
300+
if (!alongPart.IsZero)
301+
along += alongPart * monomial;
302+
}
303+
// Constant across the axis: off it everywhere, or along it everywhere.
304+
if (acrossIsZero)
305+
return across.Evaled is Real { IsZero: true } ? null : new();
306+
if (across.InnerSimplified.SolveEquation(x) is not FiniteSet roots)
307+
return null;
308+
var points = new List<(Entity At, Real Value)>();
309+
foreach (var root in roots)
310+
{
311+
if (root.ContainsNode(x) || root.Vars.Any())
312+
return null;
313+
switch (root.Evaled)
314+
{
315+
case Real { EDecimal.IsFinite: true } at:
316+
if (along.Substitute(x, root).Evaled is not Real { EDecimal.IsFinite: true } there)
317+
return null;
318+
var onTheCut = imaginaryAxis ? there > 1 || there < -1 : there < 0;
319+
var atItsEnd = imaginaryAxis ? there >= 1 && there <= 1 || there <= -1 && there >= -1 : there.IsZero;
320+
if (atItsEnd)
321+
return null;
322+
if (onTheCut)
323+
points.Add((root, at));
324+
break;
325+
case Complex { ImaginaryPart.IsZero: false }:
326+
break;
327+
default:
328+
return null;
329+
}
330+
}
331+
return points;
332+
}
333+
334+
/// <summary>
335+
/// The real <paramref name="x"/> in the range where the cosine (<paramref name="cosine"/>) or the sine of
336+
/// <c>a x + b</c>, with <c>a</c> and <c>b</c> complex numbers, crosses the negative real axis, or
337+
/// <see langword="null"/> where the argument is not of that shape, the range is not finite where it would have
338+
/// to be, or the path meets a zero.
339+
/// </summary>
340+
/// <remarks>
341+
/// With <c>p = Re(a x + b)</c> and <c>q = Im(a x + b)</c>, both linear in <c>x</c>,
342+
/// <c>cos(p + i q) = cos p cosh q - i sin p sinh q</c> is real where <c>sin p = 0</c> or <c>q = 0</c>: at the
343+
/// multiples of <c>pi</c>, negative at the odd ones, and at the one point where <c>q = 0</c>, negative where
344+
/// <c>cos p</c> is there. <c>sin(p + i q) = sin p cosh q + i cos p sinh q</c> is real where <c>cos p = 0</c> or
345+
/// <c>q = 0</c>: negative at <c>-pi/2 + 2 k pi</c>, and where <c>sin p</c> is at the point <c>q = 0</c>. A point
346+
/// where the value touches the axis without crossing it jumps by no multiple, which the caller reads.
347+
/// </remarks>
348+
private static List<(Entity At, Real Value)>? TrigonometricCrossings(Entity argument, Variable x, Real? lower, Real? upper, bool cosine)
349+
{
350+
if (lower is null || upper is null
351+
|| !TreeAnalyzer.TryGetPolyLinear(argument, x, out var a, out var b)
352+
|| a.Evaled is not Complex { RealPart.EDecimal.IsFinite: true, ImaginaryPart.EDecimal.IsFinite: true } slope
353+
|| b.Evaled is not Complex { RealPart.EDecimal.IsFinite: true, ImaginaryPart.EDecimal.IsFinite: true } intercept
354+
|| slope.RealPart.IsZero && slope.ImaginaryPart.IsZero
355+
|| MathS.pi.Evaled is not Real pi)
356+
return null;
357+
var points = new List<(Entity At, Real Value)>();
358+
// Where q = 0, if q moves at all: the value there is the real cos p or sin p.
359+
if (!slope.ImaginaryPart.IsZero)
360+
{
361+
var at = (-(Entity)intercept.ImaginaryPart / slope.ImaginaryPart).InnerSimplified;
362+
if (at.Evaled is not Real { EDecimal.IsFinite: true } value)
363+
return null;
364+
if (value >= lower && value <= upper)
365+
{
366+
var p = (slope.RealPart * (Entity)at + intercept.RealPart).InnerSimplified;
367+
if ((cosine ? MathS.Cos(p) : MathS.Sin(p)).EvalNumerical() is not { ImaginaryPart.IsZero: true, RealPart: var there })
368+
return null;
369+
// A zero on the path is a pole of the integrand's tan or cot.
370+
if (there.EDecimal.Abs().CompareTo(EDecimal.Create(1, -30)) < 0)
371+
return null;
372+
if (there < 0)
373+
points.Add((at, value));
374+
}
375+
}
376+
// 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.
377+
if (!slope.RealPart.IsZero)
378+
{
379+
if (!lower.EDecimal.IsFinite || !upper.EDecimal.IsFinite)
380+
return null;
381+
// p = c + 2 k pi, so k runs over the whole numbers between the ends mapped back.
382+
Entity first = cosine ? MathS.pi : -MathS.pi / 2;
383+
var shift = cosine ? pi : -pi / 2;
384+
var atLower = (slope.RealPart * lower + intercept.RealPart - shift) / (2 * pi);
385+
var atUpper = (slope.RealPart * upper + intercept.RealPart - shift) / (2 * pi);
386+
var (from, to) = atLower < atUpper ? (atLower, atUpper) : (atUpper, atLower);
387+
var k0 = from.EDecimal.RoundToIntegerNoRoundedFlag(EContext.CliDecimal.WithRounding(ERounding.Floor)).ToEInteger();
388+
var k1 = to.EDecimal.RoundToIntegerNoRoundedFlag(EContext.CliDecimal.WithRounding(ERounding.Ceiling)).ToEInteger();
389+
if ((k1 - k0).CompareTo(EInteger.FromInt32(MostBreaks)) > 0)
390+
return null;
391+
for (var k = k0; k.CompareTo(k1) <= 0; k += EInteger.One)
392+
{
393+
var at = ((first + 2 * Integer.Create(k) * MathS.pi - intercept.RealPart) / slope.RealPart).InnerSimplified;
394+
if (at.Evaled is not Real { EDecimal.IsFinite: true } value)
395+
return null;
396+
// The same point as the one where q = 0 is one crossing.
397+
if (value >= lower && value <= upper && !points.Any(point => Coincide(point.Value, value)))
398+
points.Add((at, value));
399+
}
400+
}
401+
else if ((cosine ? MathS.Sin(intercept.RealPart) : MathS.Cos(intercept.RealPart)).EvalNumerical() is not { RealPart: var constant }
402+
|| constant.EDecimal.Abs().CompareTo(EDecimal.Create(1, -30)) < 0)
403+
// p is constant at a point where the value is real all along the path.
404+
return null;
405+
return points;
406+
}
407+
175408
/// <summary>
176409
/// Two decimal values of the same point, reached by different exact spellings.
177410
/// </summary>

0 commit comments

Comments
 (0)