Skip to content

Commit a17c310

Browse files
A compiled gamma ratio past the doubles is taken in logarithms (#2251)
A compiled binomial coefficient, falling or rising factorial or Catalan number at an argument that is not whole is a ratio of gamma functions. Past 170! a factorial is past the doubles where the ratio need not be, so the ratio was oo/oo, NaN: binomial(216, pi) is about 2954596.68, fallingfactorial(300, 1/2) about 17.33, catalannumber(2017/5) about 5.16e238, and each was NaN. A ratio whose terms are under the doubles was 0/0, as binomial(-601/2, 7/3) was, which is about 109419.24. Where the ratio does not come out as a number, it is now the exponential of the difference of the factorials' logarithms. SpecialFunctions.LogFactorial is the logarithm of Gamma(z + 1), by the Lanczos approximation the compiled gamma function already uses, and by reflection below 1/2. At a real argument its imaginary part is a multiple of pi, odd where the factorial is negative, which gives the ratio's sign. None of the ratios is 0 away from a pole, which the callers answer first, so a ratio that came out as a number is kept as it was, to the last place. GammaRatioCompilationTest compares ten values with mpmath 1.3.0's at 50 digits, under both compilers and over double, and all ten fail on master. Two of them are past the doubles, binomial(2000, 1001/2) and catalannumber(1001), and are +oo. Evaluation is NaN for three of the ten, binomial(10, 601/2), binomial(2000, 1001/2) and catalannumber(-2017/5), and EvalNumerical throws CannotEvalException for catalannumber(1001). The last two of the three are past 1e200 or under 1e-200, the decimal range #1498 is about. Those are evaluation's, and not changed here. Found by work/compilesweep. Fixes #2248. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 3d21a61 commit a17c310

5 files changed

Lines changed: 112 additions & 5 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,13 +373,16 @@ start. Each compiles in double precision and on the interpreter's terms: a whole
373373
through the gamma function, with `NaN` at a pole of the numerator and `0` at one of the denominator. Where every argument
374374
is whole, the product is computed exactly and rounded once, so it is evaluation's exact value, as a double. The double
375375
factorial has no value off the whole numbers or at a negative even one
376-
([#2195](https://github.com/asc-community/AngouriMath/issues/2195)).
376+
([#2195](https://github.com/asc-community/AngouriMath/issues/2195)). A gamma ratio whose factorials are past the doubles,
377+
as `216!` is, is taken in logarithms, the Catalan number's too, so `binomial(216, pi)` is about `2954596.68` where a
378+
ratio of infinities had no value ([#2248](https://github.com/asc-community/AngouriMath/issues/2248)).
377379

378380
| Input | Was (2.6.0) | Now |
379381
|---|---|---|
380382
| `"binomial(x, 2)".ToEntity().Compile("x").Call(5)` | `UncompilableNodeException` | `(10, 0)` |
381383
| `"binomial(x, 45)".ToEntity().Compile<double, double>("x")(54)` | `UncompilableNodeException` | `5317936260` |
382384
| `"binomial(x, 1/2)".ToEntity().Compile<double, double>("x")(3)` | `UncompilableNodeException` | `2.037183271576257` |
385+
| `"binomial(x, pi)".ToEntity().Compile<double, double>("x")(216)` | `UncompilableNodeException` | `2954596.6790617765` |
383386

384387
### The number-theoretic functions compile
385388

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
360360
[Tests/UnitTests/Common/CombinatorialCompilationTest.cs]
361361
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
362362

363+
[Tests/UnitTests/Common/GammaRatioCompilationTest.cs]
364+
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
365+
363366
[AngouriMath/Numerics/CombinatorialFunctions.cs]
364367
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
365368

‎Sources/AngouriMath/Numerics/CombinatorialFunctions.cs‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ public static NumericsComplex Binomial(NumericsComplex n, NumericsComplex k)
6363
return NaN;
6464
if (IsWhole(n - k, out var difference) && difference < 0)
6565
return 0;
66-
return SpecialFunctions.Factorial(n) / (SpecialFunctions.Factorial(k) * SpecialFunctions.Factorial(n - k));
66+
var ratio = SpecialFunctions.Factorial(n) / (SpecialFunctions.Factorial(k) * SpecialFunctions.Factorial(n - k));
67+
return InTheDoubles(ratio) ? ratio : FromLogarithm(
68+
SpecialFunctions.LogFactorial(n) - SpecialFunctions.LogFactorial(k) - SpecialFunctions.LogFactorial(n - k),
69+
n.Imaginary == 0 && k.Imaginary == 0);
6770
}
6871

6972
public static double Binomial(double n, double k) => Real(Binomial(new NumericsComplex(n, 0), new NumericsComplex(k, 0)));
@@ -82,7 +85,9 @@ public static NumericsComplex FallingFactorial(NumericsComplex x, NumericsComple
8285
return NaN;
8386
if (IsWhole(x - n, out var difference) && difference < 0)
8487
return 0;
85-
return SpecialFunctions.Factorial(x) / SpecialFunctions.Factorial(x - n);
88+
var ratio = SpecialFunctions.Factorial(x) / SpecialFunctions.Factorial(x - n);
89+
return InTheDoubles(ratio) ? ratio : FromLogarithm(
90+
SpecialFunctions.LogFactorial(x) - SpecialFunctions.LogFactorial(x - n), x.Imaginary == 0 && n.Imaginary == 0);
8691
}
8792

8893
public static double FallingFactorial(double x, double n) => Real(FallingFactorial(new NumericsComplex(x, 0), new NumericsComplex(n, 0)));
@@ -101,7 +106,9 @@ public static NumericsComplex RisingFactorial(NumericsComplex x, NumericsComplex
101106
return NaN;
102107
if (IsWhole(x, out var bottom) && bottom <= 0)
103108
return 0;
104-
return SpecialFunctions.Factorial(x + n - 1) / SpecialFunctions.Factorial(x - 1);
109+
var ratio = SpecialFunctions.Factorial(x + n - 1) / SpecialFunctions.Factorial(x - 1);
110+
return InTheDoubles(ratio) ? ratio : FromLogarithm(
111+
SpecialFunctions.LogFactorial(x + n - 1) - SpecialFunctions.LogFactorial(x - 1), x.Imaginary == 0 && n.Imaginary == 0);
105112
}
106113

107114
public static double RisingFactorial(double x, double n) => Real(RisingFactorial(new NumericsComplex(x, 0), new NumericsComplex(n, 0)));
@@ -157,7 +164,10 @@ public static NumericsComplex CatalanNumber(NumericsComplex n)
157164
return NaN;
158165
var top = SpecialFunctions.Factorial(n - 0.5);
159166
var bottom = SpecialFunctions.Factorial(n + 1);
160-
return NumericsComplex.Pow(4, n) * top / (Math.Sqrt(Math.PI) * bottom);
167+
var ratio = NumericsComplex.Pow(4, n) * top / (Math.Sqrt(Math.PI) * bottom);
168+
return InTheDoubles(ratio) ? ratio : FromLogarithm(
169+
n * Math.Log(4) + SpecialFunctions.LogFactorial(n - 0.5) - SpecialFunctions.LogFactorial(n + 1) - Math.Log(Math.PI) / 2,
170+
n.Imaginary == 0);
161171
}
162172

163173
public static double CatalanNumber(double n) => Real(CatalanNumber(new NumericsComplex(n, 0)));
@@ -254,6 +264,23 @@ private static double ProductOfWholeFactors(long first, long step, long count, b
254264

255265
private static bool IsInfinite(NumericsComplex z) => double.IsInfinity(z.Real) || double.IsInfinity(z.Imaginary);
256266

267+
/// <summary>
268+
/// Whether a ratio of factorials, at arguments that are none of its poles, came out as a number. A factorial past
269+
/// 170 is past the doubles where a ratio of them need not be: <c>binomial(216, pi)</c> is about <c>2954596.68</c>
270+
/// and <c>216!</c> about <c>1e408</c>, so the ratio was <c>∞/∞</c>, <c>NaN</c>. None of the ratios is 0 away from a
271+
/// pole, so a 0 is one whose terms were under the doubles. https://github.com/asc-community/AngouriMath/issues/2248
272+
/// </summary>
273+
private static bool InTheDoubles(NumericsComplex ratio)
274+
=> ratio != 0 && !IsInfinite(ratio) && !double.IsNaN(ratio.Real) && !double.IsNaN(ratio.Imaginary);
275+
276+
/// <summary>
277+
/// The ratio whose logarithm is <paramref name="logarithm"/>, where its terms are past the doubles. A real one's
278+
/// logarithm has a multiple of <c>pi</c> for an imaginary part, odd where the ratio is negative, which is its sign.
279+
/// </summary>
280+
private static NumericsComplex FromLogarithm(NumericsComplex logarithm, bool real)
281+
=> !real ? NumericsComplex.Exp(logarithm)
282+
: new NumericsComplex(Math.Cos(logarithm.Imaginary) < 0 ? -Math.Exp(logarithm.Real) : Math.Exp(logarithm.Real), 0);
283+
257284
// The real value of a real argument's result, and NaN where it is not real.
258285
private static double Real(NumericsComplex z) => z.Imaginary == 0 ? z.Real : double.NaN;
259286
}

‎Sources/AngouriMath/Numerics/SpecialFunctions.cs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,17 @@ private static NumericsComplex Gamma(NumericsComplex z)
456456
? ExpTimes(Math.Log(Math.PI) - LogSine(Math.PI * z) - LogGamma(1 - z), 1)
457457
: ExpTimes(LogGamma(z), 1);
458458

459+
/// <summary>
460+
/// A logarithm of <c>z!</c>, <c>Γ(z + 1)</c>, at a number that is not one of its poles, for a ratio of factorials
461+
/// that are past the doubles where the ratio is not. At a real number its imaginary part is a multiple of
462+
/// <c>pi</c>, odd where the factorial is negative. https://github.com/asc-community/AngouriMath/issues/2248
463+
/// </summary>
464+
internal static NumericsComplex LogFactorial(NumericsComplex z)
465+
{
466+
var w = z + 1;
467+
return w.Real < 0.5 ? Math.Log(Math.PI) - LogSine(Math.PI * w) - LogGamma(1 - w) : LogGamma(w);
468+
}
469+
459470
/// <summary>A logarithm of the gamma function for <c>Re z &gt;= 1/2</c>, by Lanczos's approximation.</summary>
460471
private static NumericsComplex LogGamma(NumericsComplex z)
461472
{
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.Numerics;
9+
using AngouriMath;
10+
using AngouriMath.Extensions;
11+
using Xunit;
12+
13+
namespace AngouriMath.Tests.Common
14+
{
15+
/// <summary>
16+
/// A compiled binomial coefficient, factorial power or Catalan number whose factorials are past the doubles is its
17+
/// value, where it was <c>NaN</c>: <c>binomial(216, pi)</c> is about <c>2954596.68</c>, and <c>216!</c>, about
18+
/// <c>1e408</c>, made the gamma ratio <c>∞/∞</c>. The values are mpmath 1.3.0's at 50 digits.
19+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2248">#2248</a>
20+
/// </summary>
21+
[Trait("Area", "Common")]
22+
public sealed class GammaRatioCompilationTest
23+
{
24+
private static bool Agree(Complex expected, Complex actual)
25+
=> Complex.Abs(expected - actual) <= 1e-10 * Complex.Abs(expected);
26+
27+
[Theory]
28+
[InlineData("binomial(216, pi)", 2954596.6790612643608, 0)]
29+
[InlineData("binomial(10, 601/2)", 7.7046763087758266086e-22, 0)]
30+
[InlineData("binomial(-601/2, 7/3)", 109419.23916039716406, 0)]
31+
[InlineData("fallingfactorial(300, 1/2)", 17.327726454436284054, 0)]
32+
[InlineData("risingfactorial(200, 1/2)", 14.133299559727925473, 0)]
33+
[InlineData("catalannumber(2017/5)", 5.1595508663052781318e+238, 0)]
34+
[InlineData("catalannumber(-2017/5)", -2.8923921724035808851e-247, 0)]
35+
[InlineData("binomial(200 + i, 1/2)", 15.967717653807835931, 0.039869146616281137615)]
36+
public void ItIsTheValue(string expression, double real, double imaginary)
37+
{
38+
var expected = new Complex(real, imaginary);
39+
var compiled = $"x + {expression}".ToEntity();
40+
var fast = compiled.Compile("x").Call(0);
41+
var linq = compiled.Compile<Complex, Complex>("x")(0);
42+
Assert.True(Agree(expected, fast), $"{expression}: {fast} by FastExpression, where {expected}");
43+
Assert.True(Agree(expected, linq), $"{expression}: {linq} by LINQ, where {expected}");
44+
if (imaginary == 0)
45+
{
46+
var overDoubles = compiled.Compile<double, double>("x")(0);
47+
Assert.True(Agree(expected, overDoubles), $"{expression}: {overDoubles} over double, where {expected}");
48+
}
49+
}
50+
51+
/// <summary>Past the doubles, at about <c>9.8e486</c> and <c>8.2e597</c>, the value is <c>+oo</c>.</summary>
52+
[Theory]
53+
[InlineData("binomial(2000, 1001/2)")]
54+
[InlineData("catalannumber(1001)")]
55+
public void PastTheDoublesItIsInfinite(string expression)
56+
{
57+
var compiled = $"x + {expression}".ToEntity();
58+
Assert.Equal(new Complex(double.PositiveInfinity, 0), compiled.Compile("x").Call(0));
59+
Assert.Equal(new Complex(double.PositiveInfinity, 0), compiled.Compile<Complex, Complex>("x")(0));
60+
Assert.Equal(double.PositiveInfinity, compiled.Compile<double, double>("x")(0));
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)