Skip to content

Commit c73cc68

Browse files
Evaluate the incomplete gamma function's series faster (#2079)
Gamma(a, z) by its series is Gamma(a) - gamma(a, z), and Gamma(a) was computed by Spouge's approximation on every evaluation: six of the nine milliseconds at a hundred digits. It is remembered now, per order and precision, since a function the integrator samples at points is asked it at one a for every z. A real a and z sum the series in real arithmetic, one product and one quotient a term where the complex arithmetic took nineteen operations, and the logarithm on the cut takes pi from the cache rather than computing it afresh. gamma(2.91, -3.3) 9.52 -> 0.92 ms, gamma(1/3, 2) 23.0 -> 2.2 ms, gamma(1/2 + i, 2 + 3i) 11.8 -> 2.7 ms; the continued fraction and the exponential integral's series are unchanged. Found by #2075's next integration rule, whose answers one declined integral sampled three thousand times. 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 b21f013 commit c73cc68

1 file changed

Lines changed: 68 additions & 20 deletions

File tree

‎Sources/AngouriMath/Core/Entity/Continuous/Number/IncompleteGamma.cs‎

Lines changed: 68 additions & 20 deletions
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.
@@ -129,28 +129,50 @@ private static ((EDecimal Re, EDecimal Im) Value, EDecimal Scale) AsGammaLessThe
129129
{
130130
var epsilon = EDecimal.Create(EInteger.One, EInteger.FromInt32(-work.Precision.ToInt32Checked()));
131131
// term_k = z^k / (a (a + 1) ... (a + k)), starting at 1/a
132-
var term = Reciprocal(a, work);
133-
var sum = term;
134-
var peak = Magnitude(term);
135-
var aPlusK = a;
136-
for (var k = 1; ; k++)
132+
(EDecimal Re, EDecimal Im) sum;
133+
EDecimal peak;
134+
if (a.Im.IsZero && z.Im.IsZero)
137135
{
138-
aPlusK = (aPlusK.Re.Add(EDecimal.One, work), aPlusK.Im);
139-
term = Multiply(Multiply(term, z, work), Reciprocal(aPlusK, work), work);
140-
sum = (sum.Re.Add(term.Re, work), sum.Im.Add(term.Im, work));
141-
var size = Magnitude(term);
142-
if (size.CompareTo(peak) > 0)
143-
peak = size;
144-
if (k > 4 && size.CompareTo(Magnitude(sum).Multiply(epsilon, work)) < 0 && size.CompareTo(peak) < 0)
145-
break;
136+
// Real, as a sampled function's arguments are: one product and one quotient a term, where the complex
137+
// arithmetic took nineteen operations, and an evaluation four milliseconds.
138+
var term = EDecimal.One.Divide(a.Re, work);
139+
var real = term;
140+
peak = term.Abs();
141+
var aPlusK = a.Re;
142+
for (var k = 1; ; k++)
143+
{
144+
aPlusK = aPlusK.Add(EDecimal.One, work);
145+
term = term.Multiply(z.Re, work).Divide(aPlusK, work);
146+
real = real.Add(term, work);
147+
var size = term.Abs();
148+
if (size.CompareTo(peak) > 0)
149+
peak = size;
150+
if (k > 4 && size.CompareTo(real.Abs().Multiply(epsilon, work)) < 0 && size.CompareTo(peak) < 0)
151+
break;
152+
}
153+
sum = (real, EDecimal.Zero);
154+
}
155+
else
156+
{
157+
var term = Reciprocal(a, work);
158+
sum = term;
159+
peak = Magnitude(term);
160+
var aPlusK = a;
161+
for (var k = 1; ; k++)
162+
{
163+
aPlusK = (aPlusK.Re.Add(EDecimal.One, work), aPlusK.Im);
164+
term = Multiply(Multiply(term, z, work), Reciprocal(aPlusK, work), work);
165+
sum = (sum.Re.Add(term.Re, work), sum.Im.Add(term.Im, work));
166+
var size = Magnitude(term);
167+
if (size.CompareTo(peak) > 0)
168+
peak = size;
169+
if (k > 4 && size.CompareTo(Magnitude(sum).Multiply(epsilon, work)) < 0 && size.CompareTo(peak) < 0)
170+
break;
171+
}
146172
}
147173
var prefactor = Multiply(ComplexPower(z, a, work), ComplexExp((z.Re.Negate(), z.Im.Negate()), work), work);
148174
var lower = Multiply(prefactor, sum, work);
149-
Complex gammaOfA;
150-
using (MathS.Settings.DecimalPrecisionContext.Set(work))
151-
using (MathS.Settings.DowncastingEnabled.Set(false))
152-
gammaOfA = Factorial(Complex.Create(a.Re.Subtract(EDecimal.One, work), a.Im) as Complex);
153-
var gamma = (Re: gammaOfA.RealPart.EDecimal, Im: gammaOfA.ImaginaryPart.EDecimal);
175+
var gamma = GammaOf(a, work);
154176
var scale = EDecimal.Max(Magnitude(gamma), Magnitude(prefactor).Multiply(peak, work));
155177
return ((gamma.Re.Subtract(lower.Re, work), gamma.Im.Subtract(lower.Im, work)), scale);
156178
}
@@ -210,6 +232,29 @@ private static ((EDecimal Re, EDecimal Im) Value, EDecimal Scale) AtANonPositive
210232
return ((difference.Re.Multiply(coefficient, work), difference.Im.Multiply(coefficient, work)), scale.Multiply(coefficient.Abs(), work));
211233
}
212234

235+
/// <summary>
236+
/// <c>Gamma(a)</c> at <paramref name="work"/>'s digits, remembered: it is most of the cost of an evaluation, six
237+
/// milliseconds at a hundred digits, and a function sampled at points is asked it at one <c>a</c> for every
238+
/// <c>z</c>.
239+
/// </summary>
240+
private static (EDecimal Re, EDecimal Im) GammaOf((EDecimal Re, EDecimal Im) a, EContext work)
241+
{
242+
var key = (a.Re, a.Im, work.Precision.ToInt32Checked());
243+
if (gammaOfTheOrder.TryGetValue(key, out var known))
244+
return known;
245+
Complex value;
246+
using (MathS.Settings.DecimalPrecisionContext.Set(work))
247+
using (MathS.Settings.DowncastingEnabled.Set(false))
248+
value = Factorial(Complex.Create(a.Re.Subtract(EDecimal.One, work), a.Im) as Complex);
249+
var gamma = (value.RealPart.EDecimal, value.ImaginaryPart.EDecimal);
250+
// Emptied rather than grown without end, as the integrator's answers are.
251+
if (gammaOfTheOrder.Count >= 256)
252+
gammaOfTheOrder.Clear();
253+
gammaOfTheOrder[key] = gamma;
254+
return gamma;
255+
}
256+
[ConcurrentField] private static readonly System.Collections.Concurrent.ConcurrentDictionary<(EDecimal, EDecimal, int), (EDecimal, EDecimal)> gammaOfTheOrder = new();
257+
213258
/// <summary>The power of ten of a nonzero finite decimal's leading digit: 2 for 345.6, -3 for 0.00123.</summary>
214259
private static int DecimalOrder(EDecimal value)
215260
=> value.Exponent.Add(value.Precision()).Subtract(EInteger.One).ToInt32Checked();
@@ -227,8 +272,11 @@ private static (EDecimal Re, EDecimal Im) ComplexLn((EDecimal Re, EDecimal Im) z
227272
{
228273
if (z.Im.IsZero && z.Re.Sign > 0)
229274
return (z.Re.Log(work), EDecimal.Zero);
275+
// On the cut, ln |z| + i pi, with pi from the cache: computed afresh it was a third of an evaluation.
276+
if (z.Im.IsZero)
277+
return (z.Re.Negate().Log(work), InternalAMExtensions.ConstantCache.Lookup(work).Pi);
230278
var modulus = z.Re.Multiply(z.Re, work).Add(z.Im.Multiply(z.Im, work), work).Sqrt(work);
231-
return (modulus.Log(work), z.Im.IsZero ? EDecimal.PI(work) : z.Im.Arctan2(z.Re, work));
279+
return (modulus.Log(work), z.Im.Arctan2(z.Re, work));
232280
}
233281

234282
/// <summary><c>z^a = e^(a ln z)</c>, principal.</summary>

0 commit comments

Comments
 (0)