From c41150013d698b67d34d294a5ee6d83761421558 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sat, 5 Dec 2026 21:14:43 +0000 Subject: [PATCH] Take a sign at a definite integral's bound from inside the range (#2714) An antiderivative holding sgn(u), which the integrator writes for sgn and abs of u, was taken at a bound by substitution. Where u is zero at the bound, sgn(u) there is 0, while the integral takes the antiderivative's limit from inside the range, where sgn(u) is the sign of u next to the bound. So exp(x) sgn(x) over [0, 1/2] was sqrt(e) where it is sqrt(e) - 1, abs(ln(x)) over [1, 2] was negative, and exp(-x) abs(sin(x)) over [0, pi] was 0. Interior breaks were already taken by one-sided limits. Where the antiderivative's breaks in the range are listed, the sign of u is constant between a bound and the nearest break, since the zeros of u are among the breaks. Each sgn(u) whose value at the bound differs from the sign of u at the midpoint of that stretch is replaced by that sign before the bound is taken. This is exact, and it changes nothing where u is not zero at the bound. Where the breaks could not be listed and a sgn(u) has zeros of a kind that is listed, there were too many in the range or infinitely many, and the antiderivative jumps at them uncounted: exp(-x) abs(sin(x)) over [0, +oo) was 0. Such an integral is now left unevaluated. A sign whose zeros are of no listed kind keeps the old path, because the integrator writes it beside a factor that makes the antiderivative continuous, as in arctan(tan(x^2/2)) sgn(sin(x^2)) for x sgn(sin(x^2)). Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 22 +++++++ .../Integration/Integration.Definition.cs | 59 +++++++++++++++--- .../ASignWhoseZeroIsAtABoundIntegralTest.cs | 62 +++++++++++++++++++ 3 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 Sources/Tests/UnitTests/Calculus/ASignWhoseZeroIsAtABoundIntegralTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index fad9d71b9..253ffc490 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -42,6 +42,28 @@ A not-equal written `<>` is read as `not (a = b)`, and printed that way. ## Unreleased — since 2.10.0 +### A definite integral takes a sign at its bound from inside the range + +**Silent: wrong answers become right ones.** An antiderivative holding `sgn(u)`, which the integrator writes for +`sgn` and `abs` of `u`, was taken at a bound where `u` is zero, so `sgn(u)` there was 0. The integral takes the +antiderivative's limit from inside the range, where `sgn(u)` is the sign of `u` next to the bound. A sign whose zeros +are too many to list in the range, or infinitely many, is no longer taken across them uncounted, and such an integral is +left unevaluated. Measured on 2.10.0 and on this branch, and checked against mpmath +([#2714](https://github.com/asc-community/AngouriMath/issues/2714)). + +| simplified | Was (2.10.0) | Now | +|---|---|---| +| `integral(exp(x) * sgn(x), x, 0, 1/2)` | `sqrt(e)` | `sqrt(e) - 1` | +| `integral(exp(x) * abs(x), x, 0, 1/2)` | `-1/2 * sqrt(e)` | `1 - 1/2 * sqrt(e)` | +| `integral(abs(ln(x)), x, 1, 2)` | `2 * ln(2) - 2` | `2 * ln(2) - 1` | +| `integral(sgn(1 - x^2) * x^2, x, 0, 1)` | `0` | `1/3` | +| `integral(exp(-x) * abs(sin(x)), x, 0, pi)` | `0` | `(1 + e^(-pi))/2` | +| `integral(sgn(sin(x)) * cosh(x), x, 0, 2 pi)` | `e^pi - e^(-pi)` | `2 sinh(pi) - sinh(2 pi)` | +| `integral(Si(x) * sgn(1 - x), x, -1, 1)` | `Si(-1) - cos(-1)` | `0` | +| `integral(exp(-x) * sgn(x), x, 0, +oo)` | `0` | `1` | +| `integral(exp(-x) * abs(sin(x)), x, 0, +oo)` | `0` | unevaluated | +| `integral(sgn(sin(x)) * exp(-x), x, 0, 300)` | `e^(-300)` | unevaluated | + ### Evaluation keeps its digits from 1e-100000 to 1e100000 **Silent: wrong numbers become right ones.** The decimals evaluation works in had a hundred digits, but exponents only diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index c17fa3a90..e89a97ce2 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -164,17 +164,31 @@ public Entity Integrate(Variable x, Entity from, Entity to) => 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 + // A sign whose argument's zeros are of a kind that is listed, but were not listed in this range -- too many, + // or infinitely many -- jumps at them uncounted: e^(-x) abs(sin(x)) over [0, +oo), whose antiderivative holds + // sgn(sin(x)), was 0. One whose zeros are of no kind that is listed is left to the antiderivative as before: + // sgn(sin(x^2)) x integrates to arctan(tan(x^2/2)) sgn(sin(x^2)), which is continuous. + // https://github.com/asc-community/AngouriMath/issues/2714 + if (breaks is null && antiderivative.Nodes.Any(node => node is Signumf(var argument) && argument.ContainsNode(x) + && AntiderivativeBreaks.ZerosOf(argument, x, Integer.Zero, Integer.One, new List<(Entity At, Number.Real Value)>()))) + return null; + if (breaks is null) + // A break could not be listed, and no point is known to have none between it and a bound. + return AtTheBound(antiderivative, x, to, from, continuousInside: false) is { } atTheTop + && AtTheBound(antiderivative, x, from, to, continuousInside: false) is { } atTheBottom + ? WithTheCuts(atTheTop - atTheBottom) + : null; + if (breaks.Count == 0) + // A range the antiderivative was read to be continuous across. + return AtTheBound(FromInside(antiderivative, x, upper, lower), x, to, from, continuousInside: true) is { } atTheTop + && AtTheBound(FromInside(antiderivative, x, lower, upper), x, from, to, continuousInside: true) is { } 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) + if (HoldsAnIncompleteGammaFunction(antiderivative) + || AtTheBound(FromInside(antiderivative, x, lower, breaks[0].Value), x, from, to, continuousInside: true) is not { } atTheStart) return null; var total = WithTheCuts(-atTheStart); foreach (var (at, _) in breaks) @@ -193,7 +207,38 @@ public Entity Integrate(Variable x, Entity from, Entity to) => return null; total += before - after; } - return AtTheBound(antiderivative, x, to, from, continuousInside: true) is { } atTheEnd ? total + atTheEnd : null; + return AtTheBound(FromInside(antiderivative, x, upper, breaks[breaks.Count - 1].Value), x, to, from, continuousInside: true) is { } atTheEnd + ? total + atTheEnd + : null; + } + + /// + /// as it nears the finite from inside the range, toward + /// , with no break between the two: each sgn(u) whose value at the bound is not + /// the sign of u between them is replaced by that sign. + /// + /// + /// sgn(u) is 0 where u is, and the integral takes the antiderivative's limit from inside the range, + /// where it is the sign of u next to the bound. That sign is constant up to the nearest break, which the + /// zeros of u are among, and is read at the midpoint. Taken at the bound, e^x sgn(x) over + /// [0, 1/2] was sqrt(e) where it is sqrt(e) - 1, and abs(ln(x)) over [1, 2], + /// whose antiderivative is x (abs(ln(x)) - sgn(ln(x))), was negative. + /// https://github.com/asc-community/AngouriMath/issues/2714 + /// + private static Entity FromInside(Entity antiderivative, Variable x, Number.Real bound, Number.Real inside) + { + if (!bound.IsFinite || bound == inside) + return antiderivative; + var between = inside.IsFinite + ? ((bound + inside) / 2).InnerSimplified + : (bound + (inside > bound ? Integer.One : Integer.MinusOne)).InnerSimplified; + return antiderivative.Replace(node => + node is Signumf(var argument) && argument.ContainsNode(x) + && argument.Substitute(x, between).Evaled is Number.Real { IsFinite: true, IsZero: false } there + && (there.IsPositive ? Integer.One : Integer.MinusOne) is var sign + && MathS.Signum(argument.Substitute(x, bound)).Evaled != sign + ? sign + : node); } /// diff --git a/Sources/Tests/UnitTests/Calculus/ASignWhoseZeroIsAtABoundIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/ASignWhoseZeroIsAtABoundIntegralTest.cs new file mode 100644 index 000000000..63a07a6f5 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/ASignWhoseZeroIsAtABoundIntegralTest.cs @@ -0,0 +1,62 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System.Linq; +using AngouriMath.Extensions; +using Xunit; +using static AngouriMath.Entity; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// An antiderivative holding sgn(u) was taken at a bound where u is zero, where sgn(u) is 0, + /// rather than at its limit from inside the range: e^x sgn(x) over [0, 1/2] was sqrt(e). + /// https://github.com/asc-community/AngouriMath/issues/2714 + /// + [Trait("Area", "Calculus")] + public sealed class ASignWhoseZeroIsAtABoundIntegralTest + { + /// The values, checked against mpmath's quad at 50 digits. + [Theory] + [InlineData("exp(x) * sgn(x)", "0", "1/2", "sqrt(e) - 1")] + [InlineData("exp(x) * sgn(x)", "-1/2", "0", "e^(-1/2) - 1")] + [InlineData("exp(x) * sgn(x)", "1/2", "0", "1 - sqrt(e)")] + [InlineData("exp(x) * abs(x)", "0", "1/2", "1 - sqrt(e)/2")] + [InlineData("abs(ln(x))", "1", "2", "2 ln(2) - 1")] + [InlineData("abs(ln(x))", "1/2", "1", "1/2 - ln(2)/2")] + [InlineData("sgn(1 - x^2) * x^2", "0", "1", "1/3")] + [InlineData("sgn(1 - x^2) * x^2", "-1", "1", "2/3")] + [InlineData("sgn(x^2 - 1) * x^2", "-2", "1", "5/3")] + [InlineData("exp(-x) * abs(sin(x))", "0", "pi", "(1 + e^(-pi))/2")] + [InlineData("exp(-x) * abs(sin(x))", "0", "2 pi", "(1 + e^(-pi))^2/2")] + [InlineData("sgn(sin(x)) * cosh(x)", "0", "2 pi", "2 sinh(pi) - sinh(2 pi)")] + [InlineData("Si(x) * sgn(1 - x)", "-1", "1", "0")] + [InlineData("sqrt(1 - x) * sgn(x + 1)", "-1", "0", "2/3 (2 sqrt(2) - 1)")] + [InlineData("x * sgn(x - 1)", "0", "1", "-1/2")] + public void TheSignIsTakenFromInsideTheRange(string integrand, string from, string to, string expected) + { + var integral = integrand.ToEntity().Integrate("x", from.ToEntity(), to.ToEntity()).Simplify(); + Assert.DoesNotContain(integral.Nodes, node => node is Integralf); + var difference = (integral - expected.ToEntity()).EvalNumerical(); + Assert.True(difference.Abs() < 1e-12, $"{integrand} from {from} to {to} was {integral}, not {expected}"); + } + + /// + /// Over a half-line the zeros of sin(x) cannot all be listed, and the jumps of the antiderivative at them + /// cannot be counted, so the integral is left as written rather than answered as 0. + /// + [Theory] + [InlineData("exp(-x) * abs(sin(x))", "coth(pi/2)/2")] + [InlineData("exp(-x) * abs(cos(x))", "0.71726860404734789718844565912759918578575380285527")] + public void UncountedJumpsAreNotAnswered(string integrand, string value) + { + var integral = integrand.ToEntity().Integrate("x", 0, "+oo".ToEntity()).Simplify(); + Assert.True(integral.Nodes.Any(node => node is Integralf) + || (integral - value.ToEntity()).EvalNumerical().Abs() < 1e-12, $"{integrand} over [0, +oo) was {integral}"); + } + } +}