Skip to content

Commit 47ea3b3

Browse files
The factorial and the gamma function are defined off the real line (#2260)
The factorial's condition was x in RR and (x >= 0 or not x in ZZ), which asked for a real argument as well, though i! is 0.498 - 0.155i and the only poles are the negative integers. x!/x! simplified to 1 provided x in RR and (...), which has no value at x = i, where it is 1. The condition is now x in ZZ implies x >= 0: off the integers x in ZZ is false and the implication is true. It is written as Simplify writes it, so that a quotient's not x! = 0, which Simplify reads as True under this condition, folds into it. As not (x in ZZ and x < 0) the answer carried both. ExponentialSeries.HoldsOverTheRange read the old shape to drop a factorial's condition over a summation range, and integral(floor(x) / (floor(x) + 1)!, x, 0, +oo) stopped being 1. An implication now holds over the range wherever its conclusion does. FactorialOffTheRealLineTest takes the condition at eleven points, off the real line, at poles and between them, and two simplified quotients at complex points; six fail on master. SimplifyTest's three x!/x!-shaped tests pinned the old condition, and now have the new one. Found by work/domaincheck. Part of #2256. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent aab5e7f commit 47ea3b3

7 files changed

Lines changed: 84 additions & 7 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,19 @@ Both are real, `im(3 + 4i)` being `4`, real-linear, and turned by the conjugate
672672
| `"im(3 + 4i)".ToEntity().Simplify()` | `(3 + 4i) * im` | `4` |
673673
| `"re * x".ToEntity()` | `re * x` | `re * x` |
674674

675+
### The factorial and the gamma function are defined off the real line
676+
677+
**Silent: the condition a simplified factorial carries is `x in ZZ implies x >= 0`, where it was `x in RR and (x >= 0
678+
or not x in ZZ)`.** A factorial, and the gamma function with it, has poles at the negative integers and nowhere else.
679+
Its condition also asked for a real argument, though `i!` is `0.498 - 0.155i`. So `x!/x!` simplified to an answer with
680+
no value at `i`, where it is 1 ([#2256](https://github.com/asc-community/AngouriMath/issues/2256)).
681+
682+
| Input | Was (2.6.0) | Now |
683+
|---|---|---|
684+
| `"x!/x!".ToEntity().Simplify()` | `1 provided x in RR and (x >= 0 or not x in ZZ)` | `1 provided x in ZZ implies x >= 0` |
685+
| `"x! = 0".ToEntity().Simplify()` | `False provided x in RR and (x >= 0 or not x in ZZ)` | `False provided x in ZZ implies x >= 0` |
686+
| `"x!".ToEntity().DomainCondition` | `x in RR and (x >= 0 or not x in ZZ)` | `x in ZZ implies x >= 0` |
687+
675688
### A number's conjugate is made under the setting in force
676689

677690
**Silent: `Complex.Conjugate` is made under the downcasting setting of the caller, where the first caller's setting was

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
240240
[Tests/UnitTests/Convenience/ConjugateAndArgumentTest.cs]
241241
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
242242

243+
[Tests/UnitTests/Convenience/FactorialOffTheRealLineTest.cs]
244+
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
245+
243246
[Tests/UnitTests/Algebra/SolveTest/FractionalPowerRangeTest.cs]
244247
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
245248

‎Sources/AngouriMath/Functions/Algebra/Polynomials/ExponentialSeries.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ private static bool HoldsOverTheRange(Entity condition, Variable index, int star
202202
=> HoldsOverTheRange(left, index, start) && HoldsOverTheRange(right, index, start),
203203
Orf(var left, var right)
204204
=> HoldsOverTheRange(left, index, start) || HoldsOverTheRange(right, index, start),
205+
// A factorial's own condition, index + a in ZZ implies index + a >= 0: true wherever
206+
// its conclusion is. https://github.com/asc-community/AngouriMath/issues/2256
207+
Impliesf(_, var conclusion)
208+
=> HoldsOverTheRange(conclusion, index, start),
205209
_ => false,
206210
};
207211

‎Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,15 @@ public partial record Factorialf
526526
{
527527
// Factorial is defined for non-negative integers in the traditional sense,
528528
// but extends to complex numbers via the gamma function: n! = Γ(n+1)
529-
// The gamma function has poles at negative integers, so factorial is undefined there
530-
private protected override Entity IntrinsicCondition =>
531-
Argument.In(MathS.Sets.R) & (Argument >= 0 | !Argument.In(MathS.Sets.Z));
529+
// The gamma function has poles at negative integers, so factorial is undefined there,
530+
// and only there. The condition asked for a real argument as well, though i! is
531+
// 0.498 - 0.155i, so x!/x! simplified to 1 provided x in RR, which has no value at i.
532+
// Off the integers x >= 0 may have no truth value and x in ZZ is false, so the implication
533+
// is true. It is written as Simplify writes it, so that a quotient's x! = 0, which Simplify
534+
// reads as False under this same condition, folds into it.
535+
// https://github.com/asc-community/AngouriMath/issues/2256
536+
private protected override Entity IntrinsicCondition =>
537+
new Impliesf(Argument.In(MathS.Sets.Z), Argument >= 0);
532538

533539
/// <inheritdoc/>
534540
protected override Entity InnerSimplify(bool isExact) =>

‎Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.EqualityInequality.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ internal static Entity PushNotInside(IEnumerable<Entity> operands, bool disjoin)
280280
// a! = 0
281281
// A factorial is never zero *where it is defined*, and at a negative integer it is
282282
// not: `(-1)!` is a pole, so `(-1)! = 0` is NaN rather than False. The condition owed
283-
// is therefore the factorial's own -- `x in RR and (x >= 0 or not x in ZZ)`.
283+
// is therefore the factorial's own -- `x in ZZ implies x >= 0`.
284284
//
285285
// What was written was `Factorialf({ DomainCondition: var condition })`, a property
286286
// pattern on the factorial's *argument*. For a bare variable that condition is True,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 AngouriMath;
9+
using AngouriMath.Extensions;
10+
using Xunit;
11+
12+
namespace AngouriMath.Tests.Convenience
13+
{
14+
/// <summary>
15+
/// The factorial, and the gamma function with it, is defined everywhere but at the negative integers, its poles, where
16+
/// its condition asked for a real argument as well: <c>i!</c> is <c>0.498 - 0.155i</c>, and <c>x!/x!</c> simplified to
17+
/// <c>1 provided x in RR</c>, which has no value at <c>i</c>.
18+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2256">#2256</a>
19+
/// </summary>
20+
[Trait("Area", "Convenience")]
21+
public sealed class FactorialOffTheRealLineTest
22+
{
23+
[Theory]
24+
[InlineData("x!", "i", true)]
25+
[InlineData("x!", "1/2 + 6/5 * i", true)]
26+
[InlineData("x!", "-2 * i", true)]
27+
[InlineData("x!", "-1/2", true)]
28+
[InlineData("x!", "0", true)]
29+
[InlineData("x!", "3", true)]
30+
[InlineData("x!", "-1", false)]
31+
[InlineData("x!", "-2", false)]
32+
[InlineData("gamma(x)", "i", true)]
33+
[InlineData("gamma(x)", "0", false)]
34+
[InlineData("gamma(x)", "-3", false)]
35+
public void ItIsDefinedOffThePoles(string expression, string at, bool defined)
36+
{
37+
var condition = expression.ToEntity().DomainCondition.Substitute("x", at.ToEntity()).Evaled;
38+
Assert.Equal(defined ? Entity.Boolean.True : Entity.Boolean.False, condition);
39+
Assert.Equal(!defined, expression.ToEntity().Substitute("x", at.ToEntity()).Evaled.IsNaN);
40+
}
41+
42+
[Theory]
43+
[InlineData("x! / x!", "i")]
44+
[InlineData("gamma(x) / gamma(x)", "1/2 + 6/5 * i")]
45+
public void ASimplificationKeepsItsValueThere(string expression, string at)
46+
=> Assert.Equal(Entity.Number.Integer.One, expression.ToEntity().Simplify().Substitute("x", at.ToEntity()).Evaled);
47+
}
48+
}

‎Sources/Tests/UnitTests/PatternsTest/SimplifyTest.cs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,16 @@ [Fact] public void Patt8Negative() =>
8989
// with the unsimplified quotient at every one -- NaN at the three poles and 1 elsewhere.
9090
// The old form was adequate here by accident, since `(1 + x)!` is NaN at a pole and a
9191
// comparison against NaN is not True; the new one says so rather than relying on it.
92-
[Fact] public void FactorialXP1OverFactorialXP1() => AssertSimplify(MathS.Factorial(x + 1) / MathS.Factorial(x + 1), "1 provided 1 + x in RR and (1 + x >= 0 or not 1 + x in ZZ)");
92+
// A factorial is defined off its poles, the negative integers, and off the real line too: the
93+
// condition asked for 1 + x in RR, so this had no value at x = i, where it is 1.
94+
// https://github.com/asc-community/AngouriMath/issues/2256
95+
[Fact] public void FactorialXP1OverFactorialXP1() => AssertSimplify(MathS.Factorial(x + 1) / MathS.Factorial(x + 1), "1 provided 1 + x in ZZ implies 1 + x >= 0");
9396
[Fact] public void FactorialXP1OverFactorialX() => AssertSimplify(MathS.Factorial(1 + x) / MathS.Factorial(x), 1 + x);
9497
[Fact] public void FactorialXP1OverFactorialXM2() => AssertSimplify(MathS.Factorial(1 + x) / MathS.Factorial(-2 + x), "x3 - x");
9598
[Fact] public void FactorialXP1OverFactorialXM1() => AssertSimplify(MathS.Factorial(1 + x) / MathS.Factorial(-1 + x), x * (1 + x));
9699
[Fact] public void FactorialXP1OverFactorialXM3() => AssertSimplifyIdentical(MathS.Factorial(x + 1) / MathS.Factorial(x - 3));
97100
[Fact] public void FactorialXOverFactorialXP1() => AssertSimplify(MathS.Factorial(x) / MathS.Factorial(x + 1), 1 / (1 + x));
98-
[Fact] public void FactorialXOverFactorialX() => AssertSimplify(MathS.Factorial(x) / MathS.Factorial(x), "1 provided x in RR and (x >= 0 or not x in ZZ)");
101+
[Fact] public void FactorialXOverFactorialX() => AssertSimplify(MathS.Factorial(x) / MathS.Factorial(x), "1 provided x in ZZ implies x >= 0");
99102
[Fact] public void FactorialXOverFactorialXM1() => AssertSimplify(MathS.Factorial(0 + x) / MathS.Factorial(x - 1), x);
100103
[Fact] public void FactorialXOverFactorialXM2() => AssertSimplify(MathS.Factorial(x + 0) / MathS.Factorial(x - 2), x.Pow(2) - x);
101104
[Fact] public void FactorialXOverFactorialXM3() => AssertSimplifyIdentical(MathS.Factorial(x) / MathS.Factorial(x - 3));
@@ -105,7 +108,7 @@ [Fact] public void Patt8Negative() =>
105108
// commit, because a bare `x!` reached this through the candidate polynomial long division
106109
// produces rather than through a cancellation rule. That division carries its divisor's
107110
// domain now as well, so all three of these agree again.
108-
[Fact] public void FactorialXM1OverFactorialXM1() => AssertSimplify(MathS.Factorial(x - 1) / MathS.Factorial(x - 1), "1 provided x - 1 in RR and (x - 1 >= 0 or not x - 1 in ZZ)");
111+
[Fact] public void FactorialXM1OverFactorialXM1() => AssertSimplify(MathS.Factorial(x - 1) / MathS.Factorial(x - 1), "1 provided x - 1 in ZZ implies x - 1 >= 0");
109112
[Fact] public void FactorialXM1OverFactorialXM2() => AssertSimplify(MathS.Factorial(-1 + x) / MathS.Factorial(-2 + x), x - 1);
110113
// Same two factors, now in ascending order: the polynomial factorer offers
111114
// (x - 1) * (x - 2) and the complexity metric cannot tell the two orders apart.

0 commit comments

Comments
 (0)