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
21 changes: 21 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,27 @@ converges, what is left tends to the logarithms of the leading coefficients
| `integral(1/(x^7+1), x, 0, +oo)` | unevaluated | `pi / (7 sin(pi/7))`, written through `cotan(pi/7)` |
| `integral(1/(x^5+1), x, 1, +oo)` | unevaluated | `0.18064…`, in closed form |

### An integral through a sum over roots is not NaN at infinity, nor taken across a real root

**Not silent: wrong answers.** The antiderivative of `1/(x^3 + x + 1)` is a sum over the roots of `r^3 + r + 1`.
Taken at `+oo` it was that sum with `+oo` inside, which evaluates to `NaN`. So the integral over `[0, +oo)` simplified
to `NaN`, a claim that it does not exist, where it is `0.92176…`. Across a real root of the polynomial, the sum was
treated as continuous. The integral over `[-1, 0]`, which diverges at the root `-0.6823…`, came out as a complex
number.

At `+oo` the root sum is now 0 where its multiples are `N(r) / p'(r)` with the degree of `N` at most `deg p - 2`. That
is the Euler–Jacobi identity, and it is what the integral's convergence asks of the integrand. A real root strictly
inside the range is now a break, and the integral is left unevaluated there unless both sides settle. At `-oo` a root
sum leaves the integral unevaluated ([#2624](https://github.com/asc-community/AngouriMath/issues/2624)).

| simplified | Was (2.10.0) | Now |
|---|---|---|
| `integral(1/(x^3+x+1), x, 0, +oo)` | `NaN` | `-sum(ln(-r) / (1 + 3 * r ^ 2), r in { r : 1 + r ^ 3 + r = 0 })` = `0.92176…` |
| `integral(1/(x^3-x^2+1), x, 0, +oo)` | `NaN` | the root sum `1.79044…` |
| `integral(1/(x^5+x+1), x, 0, +oo)` | `NaN` | the root sum `0.79590…` |
| `integral(1/(x^3+x+1), x, -1, 0)` | a complex number | unevaluated (it diverges) |
| `integral(1/(x^3+x+1), x, -oo, 0)` | `NaN` | unevaluated |

### An inequality with abs of x is solved

**Not silent: an inequality that threw has an answer.** `Solve` threw `NotSufficientlySupportedException`, *"Only
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// Copyright (c) 2019-2026 Angouri.
// AngouriMath is licensed under MIT.
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
Expand Down Expand Up @@ -59,6 +59,19 @@ internal static class AntiderivativeBreaks
internal static List<(Entity At, Real Value)>? Inside(Entity antiderivative, Variable x, Real lower, Real upper)
{
var breaks = new List<(Entity At, Real Value)>();
// A sum over the roots of a polynomial, sum(c(r) ln(x - r)), breaks where x is one of its real roots,
// which are the zeros of the polynomial in x. Its terms are read no further: ln(x - r) has the bound r in
// it, whose zeros the solver cannot list, and so the root sum's breaks were never listed, and the integral
// of 1/(x^3 + x + 1) over [-1, 0] was taken across the pole at its real root as a complex number.
// https://github.com/asc-community/AngouriMath/issues/2624
if (antiderivative.Nodes.Any(node => node is SumOverSetf sum && sum.ContainsNode(x)))
{
foreach (var sum in antiderivative.Nodes.OfType<SumOverSetf>().Where(sum => sum.ContainsNode(x)).Distinct())
if (RootPolynomial(sum, x) is not { } polynomial || !ZerosOf(polynomial, x, lower, upper, breaks))
return null;
var standIn = Variable.CreateUnique(antiderivative, "s");
antiderivative = antiderivative.Replace(node => node is SumOverSetf sum && sum.ContainsNode(x) ? standIn : node);
}
// A subexpression repeated in the antiderivative is read once.
var read = new HashSet<Entity>();
foreach (var node in antiderivative.Nodes)
Expand Down Expand Up @@ -93,6 +106,16 @@ internal static class AntiderivativeBreaks
return distinct;
}

/// <summary>
/// The polynomial in <paramref name="x"/> whose roots <paramref name="sum"/> runs over, read off its set
/// <c>{ r : p(r) = q(r) }</c> as <c>p(x) - q(x)</c>, or <see langword="null"/> where the set is not that shape.
/// </summary>
internal static Entity? RootPolynomial(SumOverSetf sum, Variable x)
=> sum.Over is ConditionalSet { Var: Variable setVariable, Predicate: Equalsf(var left, var right) }
&& !left.ContainsNode(x) && !right.ContainsNode(x)
? (left - right).Substitute(setVariable, x)
: null;

/// <summary>
/// Adds the real zeros of <paramref name="expression"/> strictly inside the range, and
/// says whether they could all be listed. A cosine or a sine is zero on a periodic set,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr)
/// </remarks>
private static Entity? AtTheBound(Entity antiderivative, Variable x, Entity bound, Entity otherBound, bool continuousInside)
{
if (bound.Evaled is Number.Real { EDecimal: var infinite } && infinite.IsInfinity()
&& antiderivative.Nodes.Any(node => node is SumOverSetf sum && sum.ContainsNode(x)))
return AtAnInfiniteBoundThroughSumsOverRoots(antiderivative, x, bound, otherBound, continuousInside, infinite.IsPositiveInfinity());
var value = antiderivative.Substitute(x, bound);
if (!value.InnerSimplified.IsNaN)
return continuousInside && AtAnInfiniteBound(antiderivative, x, bound, value) is { } atInfinity ? atInfinity : value;
Expand Down Expand Up @@ -352,6 +355,90 @@ private static bool HoldsAnIncompleteGammaFunction(Entity expr)
return limit.IsNaN ? value : limit;
}

/// <summary>
/// The antiderivative's value at an infinite bound where it holds a sum over the roots of a polynomial, each such
/// sum taken at <c>+oo</c> to its limit 0 where that is shown; or <see langword="null"/>.
/// </summary>
/// <remarks>
/// <para>
/// The antiderivative of <c>1/(x^3 + x + 1)</c> is <c>sum(ln(x - r) / (3 r^2 + 1), r)</c> over the roots of
/// <c>r^3 + r + 1</c>. At <c>+oo</c> it was <c>sum(ln(+oo - r) / (3 r^2 + 1), r)</c>, which nothing settles and
/// which evaluates to <c>NaN</c>, and the integral over <c>[0, +oo)</c> simplified to <c>NaN</c>, where it is
/// <c>0.92176...</c>.
/// https://github.com/asc-community/AngouriMath/issues/2624
/// </para>
/// <para>
/// <c>sum(c(r) ln(x - r))</c> is <c>(sum c(r)) ln(x) + sum(c(r) ln(1 - r/x))</c> for <c>x</c> past every root's
/// modulus, the principal logarithm being continuous near 1. The second part tends to 0 at <c>+oo</c>. The first
/// is 0 where <c>c(r)</c> is <c>N(r) / p'(r)</c> with <c>p</c> square-free and <c>N</c> of degree at most
/// <c>deg p - 2</c>, by the Euler–Jacobi identity <c>sum r^k / p'(r) = 0</c> for <c>k &lt;= n - 2</c>. That is
/// what the integral's convergence at <c>+oo</c> asks of a rational integrand, and the sum is then 0 at the
/// bound exactly. A sum not shown so, and any at <c>-oo</c>, where <c>ln(x - r)</c> takes a branch of each root's
/// own, leave the bound with no value.
/// </para>
/// </remarks>
private static Entity? AtAnInfiniteBoundThroughSumsOverRoots(Entity antiderivative, Variable x, Entity bound,
Entity otherBound, bool continuousInside, bool positive)
{
if (!positive)
return null;
var settled = antiderivative.Replace(node =>
node is SumOverSetf sum && sum.ContainsNode(x) && VanishesAtPlusInfinity(sum, x) ? Integer.Zero : node);
if (settled.Nodes.Any(node => node is SumOverSetf sum && sum.ContainsNode(x)))
return null;
return AtTheBound(settled.InnerSimplified, x, bound, otherBound, continuousInside);
}

/// <summary>
/// Whether <paramref name="sum"/> is <c>sum(c(r) ln(x - r))</c> over the roots of a square-free polynomial
/// <c>p</c>, with <c>c(r) p'(r)</c> a polynomial in <c>r</c> of degree at most <c>deg p - 2</c>: then it tends
/// to 0 at <c>+oo</c> (see <see cref="AtAnInfiniteBoundThroughSumsOverRoots"/>).
/// </summary>
private static bool VanishesAtPlusInfinity(SumOverSetf sum, Variable x)
{
if (sum.Var is not Variable root || AntiderivativeBreaks.RootPolynomial(sum, x) is not { } polynomial)
return false;
var logarithm = sum.Expression.Nodes.FirstOrDefault(node =>
node is Logf(var logBase, Minusf(var variable, var subtracted)) && logBase == MathS.e && variable == x && subtracted == root);
if (logarithm is null)
return false;
var standIn = Variable.CreateUnique(sum, "u");
var linear = sum.Expression.Substitute(logarithm, standIn);
var coefficient = linear.Differentiate(standIn).InnerSimplified;
if (coefficient.ContainsNode(standIn) || coefficient.ContainsNode(x)
|| Bare((linear - coefficient * standIn).Simplify()) != Integer.Zero)
return false;
if (MathS.Polynomials.SquareFreePart(polynomial, x) is not { } squareFree
|| DegreeIn(squareFree, x) is not { } squareFreeDegree
|| DegreeIn(polynomial, x) is not { } degree || squareFreeDegree != degree || degree < 2)
return false;
// A condition the simplification writes on, that 3 r^2 + 1 is not 0, holds at every root of a
// square-free p, where p'(r) is not 0.
var numerator = Bare((coefficient.Substitute(root, x) * polynomial.Differentiate(x)).Simplify());
if (numerator == Integer.Zero)
return true;
return DegreeIn(numerator, x) is { } numeratorDegree && numeratorDegree <= degree - 2;

static Entity Bare(Entity expr)
{
while (expr is Providedf(var inner, _))
expr = inner;
return expr;
}

static int? DegreeIn(Entity polynomial, Variable x)
{
if (Functions.Algebra.LimitSolvers.ParseAsPolynomial(polynomial, x) is not { } monomials
|| monomials.Values.Any(coefficient => coefficient.ContainsNode(x)))
return null;
if (monomials.Count == 0)
return 0;
var highest = monomials.Keys.Max()!;
return highest.IsInteger() && highest.Sign >= 0 && highest.CompareTo(PeterO.Numbers.EDecimal.FromInt32(1000)) < 0
? highest.ToInt32Checked() : null;
}
}

/// <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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// 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
{
/// <summary>
/// The antiderivative of <c>1/(x^3 + x + 1)</c> is a sum over the roots of <c>r^3 + r + 1</c>,
/// <c>sum(ln(x - r) / (3 r^2 + 1), r)</c>. At <c>+oo</c> it was the same sum with <c>+oo</c> in it, which
/// evaluates to <c>NaN</c>, so the integral over <c>[0, +oo)</c> simplified to <c>NaN</c>, a claim that the
/// integral does not exist, where it is <c>0.92176...</c>. Across a real root the root sum was taken as if it were
/// continuous, and the integral of the same function over <c>[-1, 0]</c>, which diverges at the root
/// <c>-0.6823...</c>, was a complex number.
/// https://github.com/asc-community/AngouriMath/issues/2624
/// </summary>
[Trait("Area", "Calculus")]
public sealed class ASumOverRootsAtInfinityIsZeroTest
{
/// <summary>Values from mpmath's quadrature.</summary>
[Theory]
[InlineData("1/(x^3 + x + 1)", "0", "0.921763372185058")]
[InlineData("1/(x^3 - x^2 + 1)", "0", "1.79044536812823")]
[InlineData("1/(x^5 + x + 1)", "0", "0.795904895024779")]
[InlineData("1/(x^5 + x + 1)", "1", "0.142012887209207")]
[InlineData("x/(x^3 + x + 1)", "0", "0.96934396330681")]
[InlineData("(x + 2)/(x^3 + x + 1)", "0", "2.81287070767692")]
[InlineData("1/(x^4 + x + 1)", "0", "0.835877591843161")]
[InlineData("1/(x^5 + 3x + 5)", "0", "0.229150744145838")]
public void TheIntegralUpToInfinityIsItsValue(string integrand, string from, string expected)
{
var integral = integrand.ToEntity().Integrate("x", from.ToEntity(), "+oo".ToEntity()).Simplify();
Assert.DoesNotContain(integral.Nodes, node => node is Integralf);
var value = integral.EvalNumerical();
Assert.True((value - expected.ToEntity().EvalNumerical()).Abs() < 1e-12,
$"{integrand} over [{from}, +oo) was {integral}, {value}, not {expected}");
}

/// <summary>Never NaN, where the integral diverges or is not settled.</summary>
[Theory]
[InlineData("x^2/(x^3 + x + 1)", "0", "+oo")]
[InlineData("1/(x^3 + x + 1)", "-1", "0")]
[InlineData("1/(x^3 + x + 1)", "-oo", "-1")]
[InlineData("1/(x^3 + x + 1)", "-oo", "+oo")]
public void NoValueIsNotNaN(string integrand, string from, string to)
{
var integral = integrand.ToEntity().Integrate("x", from.ToEntity(), to.ToEntity()).Simplify();
Assert.False(integral.Evaled.IsNaN, $"{integrand} over [{from}, {to}] was NaN");
// Across the real root -0.6823... the integral diverges, so it has no finite value.
if (from == "-1")
Assert.Contains(integral.Nodes, node => node is Integralf);
}
}
}
Loading