Skip to content

Commit 0a56a2a

Browse files
Make Spouge's constants from exact ratios and powers of e (#2090)
The factorial of a non-integer is Spouge's approximation, with a = 1.3 P constants at P digits, cached per precision. Each constant c_k = (-1)^(k - 1) (a - k)^(k - 1/2) e^(a - k)/(k - 1)! was made with a power to a non-integer exponent and an exponential, a logarithm and two exponentials at 1.95 P digits for each of the 1.3 P terms. The first factorial at a precision took 0.9 s at 100 digits, 20.5 s at 300 and 218 s at 600. (a - k)^(k - 1)/(k - 1)! is a ratio of exact integers, sqrt(a - k) one square root, and e^(a - k) the running product of e, carried with ten more digits for the a multiplications and rounded back to the constants' precision. The first factorial now takes 0.12 s at 100 digits, 0.57 s at 300, 1.5 s at 600, 5.5 s at 1000 and 30 s at 2000. The values are unchanged. Against mpmath, Gamma(1/3), Gamma(1/3 + i), Gamma(2/7), Gamma(-7/3), Gamma(101/2) and Gamma(-5/2 + 3i) have the same error on both builds at 100 and 300 digits, within 1.3 units of the last digit, and stay within that at 600 and 1000. Once compiled, an evaluation at 100 digits costs the same, 7 to 10 ms, on both. A test of (1/3)! and (1/3 + i)! at 300 digits is added. Part of #2075. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 2be40ba commit 0a56a2a

2 files changed

Lines changed: 35 additions & 10 deletions

File tree

‎Sources/AngouriMath/Functions/InternalAMExtensions.cs‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,17 +1245,24 @@ internal static EDecimal[] GetSpougeFactorialConstants(int a)
12451245

12461246
constants[0] = EDecimal.PI(mc).Multiply(2, mc).Sqrt(mc);
12471247

1248-
var negative = false;
1248+
// c_k = (-1)^(k - 1) (a - k)^(k - 1)/(k - 1)! sqrt(a - k) e^(a - k), the first factor a ratio of exact
1249+
// integers and e^(a - k) a product of e's, with digits to spare for the a multiplications that make it.
1250+
// With a power of a - k to k - 1/2 and an exponential for each k, the constants for 300 digits took 20
1251+
// seconds, and for 600 digits 218.
1252+
var work = EContext.ForPrecision(a * 15 / 10 + 10);
1253+
var e = EDecimal.One.Exp(work);
1254+
var powersOfE = new EDecimal[a];
1255+
powersOfE[0] = EDecimal.One;
1256+
for (int m = 1; m < a; m++)
1257+
powersOfE[m] = powersOfE[m - 1].Multiply(e, work);
12491258
for (int k = 1; k < a; k++)
12501259
{
1251-
var deltaAK = EDecimal.FromInt32(a - k);
1252-
var ck = deltaAK.Pow(EDecimal.FromInt32(k).Subtract(0.5m), mc);
1253-
ck = deltaAK.Exp(mc).Multiply(ck, mc);
1254-
ck = ck.Divide(Factorial(k - 1), mc);
1255-
if (negative)
1256-
ck = ck.Negate();
1257-
constants[k] = ck;
1258-
negative = !negative;
1260+
var m = a - k;
1261+
var ck = EDecimal.FromEInteger(EInteger.FromInt32(m).Pow(k - 1)).Divide(EDecimal.FromEInteger(Factorial(k - 1)), work)
1262+
.Multiply(EDecimal.FromInt32(m).Sqrt(work), work)
1263+
.Multiply(powersOfE[m], work)
1264+
.RoundToPrecision(mc);
1265+
constants[k] = k % 2 == 0 ? ck.Negate() : ck;
12591266
}
12601267

12611268
return constants;

‎Sources/Tests/UnitTests/Core/FactorialFarFromZeroTest.cs‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Copyright (c) 2019-2026 Angouri.
33
// AngouriMath is licensed under MIT.
44
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
@@ -78,6 +78,24 @@ public void ADivisorsModulusNearAWholeNumberIsNotRoundedToIt()
7878
Close("-0.002993872340987814525483818825494753213055396187438519990678101458851071", value.ImaginaryPart.EDecimal, 60);
7979
}
8080

81+
/// <summary>
82+
/// At 300 digits, on the real line and off it. Spouge's 390 constants were made with a power and an exponential
83+
/// each, and the first factorial at that precision took 20 seconds; they are a ratio of integers, a square root and
84+
/// a product of <c>e</c>'s now.
85+
/// </summary>
86+
[Fact]
87+
public void AtThreeHundredDigits()
88+
{
89+
var context = EContext.ForPrecision(300);
90+
Close("0.89297951156924921121856431365822588137622979265243370031680944253013920338924793984699429634706292980638634973335742111190636152316815741999257112256948992272242994101666773944751677408238564727251848331757626146492648043525527613387077304719882217534457586134019354517131441367733861313846090028508017368",
91+
Evaluate("(1/3)!", context).RealPart.EDecimal, 295);
92+
var value = Evaluate("(1/3 + i)!", context);
93+
Close("0.55310533708451368643420143855148996023577256508846542223376506819123717723035945992089147907370521801219397887942272059738687781730094054546525636372408182990085240891013953705988378166744479672870436455961985418938874599630727253813312731648659256268261654562175265755195218626689594794087234395264053064",
94+
value.RealPart.EDecimal, 295);
95+
Close("0.0097530148049768242524751667053160295250646412881078165005696874705860541880090280173509525760804027158706765197226375317213454843960898145149813764056673011748957471092198259369271410261828558753507851947578513569017247888018679994921424076865568363297838883827120115940210102202849352569014259005743340527",
96+
value.ImaginaryPart.EDecimal, 293);
97+
}
98+
8199
/// <summary>The reflection formula off the real line, which the complex factorial now takes left of zero.</summary>
82100
[Fact]
83101
public void LeftOfZeroOffTheRealLine()

0 commit comments

Comments
 (0)