Skip to content

Commit dd7f10d

Browse files
A definite integral is taken piece by piece across its antiderivative's breaks (#1515)
F(b) - F(a) is the integral only where F is continuous on [a, b], and the integral of 1/x^2 over [-1, 1] was -1/x at the bounds, -2. Where both bounds have numeric values, the points between them where the antiderivative can break are listed -- the zeros of a denominator, of a base under a negative power, of a logarithm's argument, of the arguments of sgn, arccot, arcsec and arccsc, and of the cosine or sine under a tangent, secant, cotangent or cosecant of a linear argument -- and each piece takes its one-sided limits there: a pole is +oo or -oo, opposite divergences are NaN, and a jump where the integrand is continuous is left out. Where a break cannot be listed the integral is taken at the bounds as before. The limit of an arctangent, arccotangent or arccosecant of a diverging argument is answered, which the half-angle antiderivative's jump at pi needs, and a bound written as pi is placed by its value. Closes #1508. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ca85d0c commit dd7f10d

9 files changed

Lines changed: 388 additions & 19 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,30 @@ themselves by parts against 1. A symbolic exponent is answered for the generic c
16361636
| `"erf(x)".Integrate("x")` | `UnrecognizedFunctionParseException` | `x * erf(x) + e ^ (-x ^ 2) / sqrt(pi) + C` |
16371637
| `integral(e^(-x^2), x, -oo, +oo)` | `integral(e ^ (-x ^ 2), x, -oo, +oo)` | `sqrt(pi)` |
16381638

1639+
### A definite integral across a pole diverges, and a jump in its antiderivative is left out
1640+
1641+
`integral(1/x^2, x, -1, 1)` was `-2`, a negative number for a positive integrand: the
1642+
antiderivative `-1/x` was taken at the two bounds, and the pole between them was never seen.
1643+
Where the bounds have numeric values, the range is now split where the antiderivative can break,
1644+
and each piece takes its limit at a break from its own side. The breaks are the zeros of a
1645+
denominator, of a logarithm's argument, and of the cosine or sine under a tangent, secant,
1646+
cotangent or cosecant, among others. A pole the integral diverges across gives `+oo` or `-oo`,
1647+
and pieces diverging in opposite directions have no value. A jump where the integrand is
1648+
continuous, like the half-angle antiderivative's at `pi`, is left out. That needs the limit of an
1649+
arctangent, arccotangent or arccosecant whose argument diverges, which is now answered too
1650+
([#1508](https://github.com/asc-community/AngouriMath/issues/1508)).
1651+
1652+
| Input | Was (2.5.0) | Now |
1653+
|---|---|---|
1654+
| `integral(1/x^2, x, -1, 1)` | `-2` | `+oo` |
1655+
| `integral(1/(x - 2)^2, x, 0, 3)` | `-3/2` | `+oo` |
1656+
| `integral(tan(x)^2, x, 0, 3)` | `tan(3) - arctan(tan(3))` | `+oo` |
1657+
| `integral(1/x, x, -1, 1)` | `-ln(-1)` | `NaN` |
1658+
| `integral(1/(x^2 - 4), x, -3, 3)` | `ln(1/25) / 4` | `NaN` |
1659+
| `integral(1/(2 + cos(x)), x, 0, 2 * pi)` | `integral(1 / (2 + cos(x)), x, 0, 2 * pi)` | `2/3 * sqrt(3) * pi` |
1660+
| `integral(1/(2 + cos(x)), x, 0, pi)` | `integral(1 / (2 + cos(x)), x, 0, pi)` | `sqrt(3) * pi / 3` |
1661+
| `limitleft(arctan(tan(x / 2)), x, pi)` | `limitleft(arctan(tan(x / 2)), x, pi)` | `pi / 2` |
1662+
16391663
### `binomial(n, k)` is a function
16401664

16411665
**Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled

‎Sources/.editorconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,3 +627,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
627627

628628
[Tests/UnitTests/Calculus/GaussianIntegralTest.cs]
629629
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
630+
631+
[AngouriMath/Functions/Continuous/Integration/AntiderivativeBreaks.cs]
632+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
633+
634+
[Tests/UnitTests/Calculus/IntegralAcrossABreakTest.cs]
635+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using PeterO.Numbers;
11+
using static AngouriMath.Entity;
12+
using static AngouriMath.Entity.Number;
13+
using static AngouriMath.Entity.Set;
14+
15+
namespace AngouriMath.Functions.Algebra
16+
{
17+
/// <summary>
18+
/// The points strictly inside a numeric range where an antiderivative can stop being
19+
/// continuous, so that a definite integral is taken through it piece by piece.
20+
/// </summary>
21+
/// <remarks>
22+
/// <para>
23+
/// <c>F(b) - F(a)</c> is the integral only where <c>F</c> is continuous on <c>[a, b]</c>. A pole
24+
/// of the integrand between the bounds is a point where <c>F</c> is not even defined: the
25+
/// integral of <c>1/x^2</c> over <c>[-1, 1]</c> came back as <c>-1/x</c> at 1 less <c>-1/x</c>
26+
/// at -1, which is <c>-2</c>, a negative answer for a positive integrand whose integral
27+
/// diverges. An antiderivative can also jump where the integrand is continuous, and the
28+
/// answer through it is then wrong by the jump: <c>arccot(x)</c> jumps at 0 in this library's
29+
/// convention, and a half-angle substitution writes <c>tan(x/2)</c>.
30+
/// </para>
31+
/// <para>
32+
/// The places read are where a node of the antiderivative stops being continuous: the zeros
33+
/// of a denominator, of a base raised to a negative power, of a logarithm's argument, of the
34+
/// argument of <c>sgn</c>, <c>arccot</c>, <c>arcsec</c> and <c>arccsc</c>, and the zeros of the
35+
/// cosine or the sine under a tangent, secant, cotangent or cosecant of a linear argument.
36+
/// Where one of them cannot be listed -- a root with a symbol in it, a periodic argument
37+
/// that is not linear, more breaks than <see cref="MostBreaks"/> -- the caller is told so,
38+
/// and integrates as it did before.
39+
/// </para>
40+
/// <para>
41+
/// A break is carried as its exact point, where the one-sided limits are taken, and its
42+
/// decimal value, which only orders the breaks and places them in the range.
43+
/// https://github.com/asc-community/AngouriMath/issues/1508
44+
/// </para>
45+
/// </remarks>
46+
internal static class AntiderivativeBreaks
47+
{
48+
/// <summary>
49+
/// More breaks than this in one range are not listed: a tangent over a thousand
50+
/// periods has no integral to find, and listing its poles would be all the work.
51+
/// </summary>
52+
private const int MostBreaks = 64;
53+
54+
/// <summary>
55+
/// The points strictly between <paramref name="lower"/> and <paramref name="upper"/>,
56+
/// ascending, where <paramref name="antiderivative"/> may break, or
57+
/// <see langword="null"/> where one of them cannot be listed.
58+
/// </summary>
59+
internal static List<(Entity At, Real Value)>? Inside(Entity antiderivative, Variable x, Real lower, Real upper)
60+
{
61+
var breaks = new List<(Entity At, Real Value)>();
62+
// A subexpression repeated in the antiderivative is read once.
63+
var read = new HashSet<Entity>();
64+
foreach (var node in antiderivative.Nodes)
65+
{
66+
if (!read.Add(node))
67+
continue;
68+
var listed = node switch
69+
{
70+
Divf(_, var denominator) when denominator.ContainsNode(x) => ZerosOf(denominator, x, lower, upper, breaks),
71+
Powf(var @base, Real { IsNegative: true }) when @base.ContainsNode(x) => ZerosOf(@base, x, lower, upper, breaks),
72+
Logf(_, var antilogarithm) when antilogarithm.ContainsNode(x) => ZerosOf(antilogarithm, x, lower, upper, breaks),
73+
Signumf(var argument) when argument.ContainsNode(x) => ZerosOf(argument, x, lower, upper, breaks),
74+
Arccotanf(var argument) when argument.ContainsNode(x) => ZerosOf(argument, x, lower, upper, breaks),
75+
Arcsecantf(var argument) when argument.ContainsNode(x) => ZerosOf(argument, x, lower, upper, breaks),
76+
Arccosecantf(var argument) when argument.ContainsNode(x) => ZerosOf(argument, x, lower, upper, breaks),
77+
Tanf(var argument) when argument.ContainsNode(x) => PeriodicZeros(argument, x, lower, upper, halfPeriodShift: true, breaks),
78+
Secantf(var argument) when argument.ContainsNode(x) => PeriodicZeros(argument, x, lower, upper, halfPeriodShift: true, breaks),
79+
Cotanf(var argument) when argument.ContainsNode(x) => PeriodicZeros(argument, x, lower, upper, halfPeriodShift: false, breaks),
80+
Cosecantf(var argument) when argument.ContainsNode(x) => PeriodicZeros(argument, x, lower, upper, halfPeriodShift: false, breaks),
81+
_ => true
82+
};
83+
if (!listed || breaks.Count > MostBreaks)
84+
return null;
85+
}
86+
breaks.Sort((a, b) => a.Value.CompareTo(b.Value));
87+
// The same point from two nodes -- a pole of -ln(cos(x)) is also a zero of the
88+
// cosine's -- is one break.
89+
var distinct = new List<(Entity At, Real Value)>();
90+
foreach (var point in breaks)
91+
if (distinct.Count == 0 || !Coincide(distinct[distinct.Count - 1].Value, point.Value))
92+
distinct.Add(point);
93+
return distinct;
94+
}
95+
96+
/// <summary>
97+
/// Adds the real zeros of <paramref name="expression"/> strictly inside the range, and
98+
/// says whether they could all be listed. A cosine or a sine is zero on a periodic set,
99+
/// which the solver writes with an integer parameter, so it is listed across the range
100+
/// instead: <c>-ln(cos(x))</c>, the antiderivative of <c>tan(x)</c>, has no tangent in it
101+
/// to read. The modulus and a positive power are zero where what they hold is, a product
102+
/// where one of its factors is, and a quotient where its numerator is.
103+
/// </summary>
104+
private static bool ZerosOf(Entity expression, Variable x, Real lower, Real upper, List<(Entity At, Real Value)> breaks)
105+
=> expression switch
106+
{
107+
Cosf(var argument) => PeriodicZeros(argument, x, lower, upper, halfPeriodShift: true, breaks),
108+
Sinf(var argument) => PeriodicZeros(argument, x, lower, upper, halfPeriodShift: false, breaks),
109+
Absf(var inner) => ZerosOf(inner, x, lower, upper, breaks),
110+
Powf(var inner, Real { IsPositive: true }) => ZerosOf(inner, x, lower, upper, breaks),
111+
Mulf(var left, var right) =>
112+
(!left.ContainsNode(x) || ZerosOf(left, x, lower, upper, breaks))
113+
&& (!right.ContainsNode(x) || ZerosOf(right, x, lower, upper, breaks)),
114+
// A quotient is zero where its numerator is; its denominator is read as one.
115+
Divf(var numerator, _) => !numerator.ContainsNode(x) || ZerosOf(numerator, x, lower, upper, breaks),
116+
_ => SolvedZeros(expression, x, lower, upper, breaks)
117+
};
118+
119+
/// <summary>The real zeros the solver lists for <paramref name="expression"/>, strictly inside the range.</summary>
120+
private static bool SolvedZeros(Entity expression, Variable x, Real lower, Real upper, List<(Entity At, Real Value)> breaks)
121+
{
122+
if (expression.SolveEquation(x) is not FiniteSet roots)
123+
return false;
124+
foreach (var root in roots)
125+
{
126+
if (root.ContainsNode(x) || root.Vars.Any())
127+
return false;
128+
switch (root.Evaled)
129+
{
130+
case Real { EDecimal.IsFinite: true } value:
131+
if (value > lower && value < upper)
132+
breaks.Add((root, value));
133+
break;
134+
case Complex { ImaginaryPart.IsZero: false }:
135+
break;
136+
default:
137+
return false;
138+
}
139+
}
140+
return true;
141+
}
142+
143+
/// <summary>
144+
/// Adds the zeros of the cosine (<paramref name="halfPeriodShift"/>) or the sine of a
145+
/// linear argument strictly inside the range: where <c>a x + b</c> is <c>pi/2 + k pi</c>,
146+
/// or <c>k pi</c>, for a whole <c>k</c>.
147+
/// </summary>
148+
private static bool PeriodicZeros(Entity argument, Variable x, Real lower, Real upper, bool halfPeriodShift, List<(Entity At, Real Value)> breaks)
149+
{
150+
if (!TreeAnalyzer.TryGetPolyLinear(argument, x, out var a, out var b)
151+
|| a.Evaled is not Real { EDecimal.IsFinite: true, IsZero: false } slope
152+
|| b.Evaled is not Real { EDecimal.IsFinite: true } intercept
153+
|| MathS.pi.Evaled is not Real pi)
154+
return false;
155+
if (!lower.EDecimal.IsFinite || !upper.EDecimal.IsFinite)
156+
return false;
157+
// x = (c + k pi - b)/a, so k runs over the whole numbers between the ends mapped back.
158+
var shift = halfPeriodShift ? pi / 2 : Integer.Zero;
159+
var atLower = (slope * lower + intercept - shift) / pi;
160+
var atUpper = (slope * upper + intercept - shift) / pi;
161+
var (from, to) = atLower < atUpper ? (atLower, atUpper) : (atUpper, atLower);
162+
var first = from.EDecimal.RoundToIntegerNoRoundedFlag(EContext.CliDecimal.WithRounding(ERounding.Floor)).ToEInteger();
163+
var last = to.EDecimal.RoundToIntegerNoRoundedFlag(EContext.CliDecimal.WithRounding(ERounding.Ceiling)).ToEInteger();
164+
if ((last - first).CompareTo(EInteger.FromInt32(MostBreaks)) > 0)
165+
return false;
166+
for (var k = first; k.CompareTo(last) <= 0; k += EInteger.One)
167+
{
168+
var at = ((halfPeriodShift ? MathS.pi / 2 : Integer.Zero) + Integer.Create(k) * MathS.pi - b) / a;
169+
if (at.InnerSimplified.Evaled is Real { EDecimal.IsFinite: true } value && value > lower && value < upper)
170+
breaks.Add((at.InnerSimplified, value));
171+
}
172+
return true;
173+
}
174+
175+
/// <summary>
176+
/// Two decimal values of the same point, reached by different exact spellings.
177+
/// </summary>
178+
private static bool Coincide(Real a, Real b)
179+
=> a.EDecimal.Subtract(b.EDecimal).Abs().CompareTo(EDecimal.Create(1, -30)) < 0;
180+
}
181+
}

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

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,46 @@ public Entity Integrate(Variable x, Entity from, Entity to) =>
5757
Functions.Algebra.BreakpointIntegration.HasABreak(this, x)
5858
? Functions.Algebra.BreakpointIntegration.Split(this, x, from, to) ?? new Integralf(this, x, (from, to))
5959
: Transformation.Integration(x).Apply(this).Output is { } antiderivative
60-
? AtTheBound(antiderivative, x, to, from) - AtTheBound(antiderivative, x, from, to)
60+
? AcrossTheRange(antiderivative, x, from, to) ?? new Integralf(this, x, (from, to))
6161
: new Integralf(this, x, (from, to));
6262

63+
/// <summary>
64+
/// <c>F(b) - F(a)</c> through <paramref name="antiderivative"/>, taken piece by piece
65+
/// where it breaks between the bounds, or <see langword="null"/> where a piece's limit at
66+
/// a break is not decided.
67+
/// </summary>
68+
/// <remarks>
69+
/// At each break the piece below takes its limit from the left and the piece above from
70+
/// the right, so a jump in the antiderivative is not counted and a pole is: <c>1/x^2</c>
71+
/// over <c>[-1, 1]</c> is <c>+oo</c>, and <c>1/x</c> over the same range, whose pieces are
72+
/// <c>-oo</c> and <c>+oo</c>, has no value. Where the bounds are not numbers, or the breaks
73+
/// cannot all be listed, the antiderivative is taken at the bounds alone, as it was.
74+
/// https://github.com/asc-community/AngouriMath/issues/1508
75+
/// </remarks>
76+
private static Entity? AcrossTheRange(Entity antiderivative, Variable x, Entity from, Entity to)
77+
{
78+
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) - AtTheBound(antiderivative, x, from, to);
84+
if (lower > upper)
85+
breaks.Reverse();
86+
var total = -AtTheBound(antiderivative, x, from, to);
87+
foreach (var (at, _) in breaks)
88+
{
89+
// Approached from the side the range comes from, then left on the other.
90+
var (arriving, leaving) = lower < upper ? (ApproachFrom.Left, ApproachFrom.Right) : (ApproachFrom.Right, ApproachFrom.Left);
91+
var before = antiderivative.Limit(x, at, arriving);
92+
var after = antiderivative.Limit(x, at, leaving);
93+
if (before.Nodes.Any(node => node is Limitf) || after.Nodes.Any(node => node is Limitf))
94+
return null;
95+
total += before - after;
96+
}
97+
return total + AtTheBound(antiderivative, x, to, from);
98+
}
99+
63100
/// <summary>
64101
/// The antiderivative's value at one bound of a definite integral: its value there, and
65102
/// where that is undefined, its limit as the bound is approached from inside the range,
@@ -71,7 +108,7 @@ public Entity Integrate(Variable x, Entity from, Entity to) =>
71108
/// at 0 is <c>0 * -oo</c>, so the integral of <c>ln(x)</c> from 0 to 1 was <c>NaN</c> where it
72109
/// is -1. The limit is taken only where the value is undefined, so a bound the substitution
73110
/// answered is answered as before. Its side is known at an infinity, and at a finite bound
74-
/// from the other bound where both are numbers. Elsewhere, and where the limit is not
111+
/// from the other bound where both have numeric values. Elsewhere, and where the limit is not
75112
/// decided, the value stays undefined, as it is for <c>sin(x)</c> from 0 to <c>+oo</c>,
76113
/// whose antiderivative has no limit there.
77114
/// </remarks>
@@ -80,7 +117,8 @@ private static Entity AtTheBound(Entity antiderivative, Variable x, Entity bound
80117
var value = antiderivative.Substitute(x, bound);
81118
if (!value.InnerSimplified.IsNaN)
82119
return value;
83-
ApproachFrom? side = (bound.InnerSimplified, otherBound.InnerSimplified) switch
120+
// The bounds' values, so that a bound written as pi or 2 pi is placed as well as 3.
121+
ApproachFrom? side = (bound.Evaled, otherBound.Evaled) switch
84122
{
85123
(Number.Real { EDecimal: var at }, _) when at.IsPositiveInfinity() => ApproachFrom.Left,
86124
(Number.Real { EDecimal: var at }, _) when at.IsNegativeInfinity() => ApproachFrom.Right,

0 commit comments

Comments
 (0)