Skip to content

Commit aff8368

Browse files
Take the power's branch in the incomplete gamma function (#2080)
gamma(a, z) took the principal branch of z^a everywhere, and the power takes the real root of a negative base for an exact exponent of odd denominator below 10 (SimplificationContract section 6). So on the negative axis, for such an a, the function's derivative -z^(a - 1) e^(-z), whose power takes the real root, was not the derivative of its value: the difference quotient of gamma(1/3, z) at z = -2 was the derivative turned by e^(-2 i pi/3). An antiderivative written in the function had the values of one turned the same way; with the next integration rule, the integral of e^(x^3) from 1 to 2 came out -137.8 plus an imaginary part, where it is 275.5, and differentiating back did not show it. Gamma(a, z) is Gamma(a) - z^a g(a, z) with g entire, and its z^a is now the power's: rho = (-1)^p e^(-i pi p/q) times the principal one, which makes the value real. gamma(1/3, -2) is 10.2125..., DLMF 8.7.3's sum with the real root at 90 digits. The conventions table records it, with the bound below 10 that it did not: (-8)^(1/11) is principal. Part of #2075; see #204. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent c73cc68 commit aff8368

5 files changed

Lines changed: 104 additions & 20 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,12 @@ as `a - b = 0`, as a statement's own solve reads it: `"x + 1 = 3".Solve("x")` wa
134134
**Not silent: what was a parse error is a function.** `gamma` took one argument, and a second was a
135135
`FunctionArgumentCountException`. With two it is now the upper incomplete gamma function,
136136
`Gamma(a, z) = int_z^oo t^(a - 1) e^(-t) dt`, as Mathematica's `Gamma[a, z]` is.
137-
- **Branch.** It takes the principal branch of `z^a`, with the cut on the negative real axis.
137+
- **Branch.** It takes the branch of `z^a` the power takes: the principal one, with the cut on the negative real axis,
138+
and on that axis the real root for an exact `a` of odd denominator below 10, as `(-8)^(1/3)` is `-2`
139+
([#204](https://github.com/asc-community/AngouriMath/issues/204)). So `gamma(1/3, -2)` is real, where Mathematica's
140+
`Gamma[1/3, -2]` is not, and the derivative below is the derivative of the value there too.
138141
- **Evaluation.** It is evaluated to the working precision for any complex `a` and `z`.
139-
- **Derivative.** In `z` it is `-z^(a - 1) e^(-z)`. At a negative `z` that power takes the real root for an exact
140-
`a` of small odd denominator, so there it is not the derivative of the value
141-
([#204](https://github.com/asc-community/AngouriMath/issues/204)).
142+
- **Derivative.** In `z` it is `-z^(a - 1) e^(-z)`.
142143
- **Exact forms.** A whole `a >= 1` and `a = 1/2` are elementary.
143144
- **Integrals.** Of a linear `u`, beside a polynomial, or beside any power of `x` but `1/x` where `u` is a multiple
144145
of `x`, by parts, in gamma functions of `u`.
@@ -149,6 +150,7 @@ as `a - b = 0`, as a statement's own solve reads it: `"x + 1 = 3".Solve("x")` wa
149150
| `"gamma(1/3, x)".ToEntity()` | a `FunctionArgumentCountException` | `gamma(1/3, x)` |
150151
| `"gamma(1/3, 2)".EvalNumerical()` | a `FunctionArgumentCountException` | `0.068136414441459136…` |
151152
| `"gamma(3, x)".ToEntity().InnerSimplified` | a `FunctionArgumentCountException` | `2 * e ^ (-x) * (1 + x + x ^ 2 / 2)` |
153+
| `"gamma(1/3, -2)".EvalNumerical()` | a `FunctionArgumentCountException` | `10.212552064243320672…`, real |
152154
| `"x^2*gamma(0, a*x)".ToEntity().Integrate("x")` | a `FunctionArgumentCountException` | `x ^ 3 / 3 * gamma(0, a * x) - 1 / (3 * a ^ 3) * 2 * e ^ (-a * x) * (1 + a * x + (a * x) ^ 2 / 2) + C` |
153155
| `"x^m*gamma(n, b*x)".ToEntity().Integrate("x")` | a `FunctionArgumentCountException` | `(x ^ (m + 1) * gamma(n, b * x) - x ^ m * (b * x) ^ (-m) * gamma(n + m + 1, b * x) / b) / (m + 1) + C` |
154156

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,11 +1479,10 @@ public static Entity VectorLength(Matrix v)
14791479
/// <param name="lowerLimit">The lower limit <c>z</c>, any complex number; <c>0</c> only where <c>Re a > 0</c></param>
14801480
/// <returns>The <see cref="Entity.IncompleteGammaf"/> node</returns>
14811481
/// <remarks>
1482-
/// <c>z^a</c> takes its principal branch, so the cut is the negative real axis, which keeps the value from above.
1483-
/// <c>Gamma(a, 0)</c> is <c>Gamma(a)</c>, and the derivative in <c>z</c> is <c>-z^(a - 1) e^(-z)</c>. At a negative
1484-
/// <c>z</c> the power there takes the real root for an exact <c>a</c> of small odd denominator, not the principal
1485-
/// branch, so for such an <c>a</c> it is not the derivative of the value
1486-
/// (https://github.com/asc-community/AngouriMath/issues/204). It is written
1482+
/// <c>z^a</c> is the power's: its principal branch, so the cut is the negative real axis, which keeps the value from
1483+
/// above, and on that axis the real root where the power takes it, for an exact <c>a</c> of odd denominator below 10,
1484+
/// where the value is real (https://github.com/asc-community/AngouriMath/issues/204).
1485+
/// <c>Gamma(a, 0)</c> is <c>Gamma(a)</c>, and the derivative in <c>z</c> is <c>-z^(a - 1) e^(-z)</c>. It is written
14871486
/// <c>gamma(a, z)</c>, as Mathematica's <c>Gamma[a, z]</c> and mpmath's <c>gammainc(a, z)</c> are.
14881487
/// https://github.com/asc-community/AngouriMath/issues/2075
14891488
/// </remarks>

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

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ partial record Number
1616
{
1717
/// <summary>
1818
/// The upper incomplete gamma function, <c>Gamma(a, z) = int_z^oo t^(a - 1) e^(-t) dt</c>, at the working
19-
/// precision, for complex <c>a</c> and <c>z</c>, with the principal branch of <c>z^a</c>: the cut is the
20-
/// negative real axis, which keeps the value from above.
19+
/// precision, for complex <c>a</c> and <c>z</c>, with the branch of <c>z^a</c> the power takes: the principal
20+
/// one, whose cut is the negative real axis and which keeps the value from above, and on that axis the real root
21+
/// where the power takes it, for an exact <c>a</c> of odd denominator below 10.
2122
/// </summary>
2223
/// <remarks>
2324
/// <para>
@@ -65,12 +66,30 @@ public static Complex IncompleteGamma(Complex a, Complex z)
6566
// summed instead.
6667
var order = Math.Sqrt(Math.Pow(ar.ToDouble(), 2) + Math.Pow(ai.ToDouble(), 2));
6768
var useTheFraction = modulus > 2 * order + 10 && (zr.Sign >= 0 || modulus > (digits + 5) * Math.Log(10));
69+
// Gamma(a, z) is Gamma(a) - z^a g(a, z) with g entire, and its z^a is the power's: on the negative axis, for
70+
// an exact a of odd denominator q below 10, the power takes the real root, rho = (-1)^p e^(-i pi p/q) times
71+
// the principal one. With the principal branch there, -z^(a - 1) e^(-z), whose power takes the same root, was
72+
// not the derivative of the value, and an antiderivative written in the function had the values of one
73+
// turned by rho: the integral of e^(x^3) from 1 to 2 came out -137.8 + 238.6 i, where it is 275.5.
74+
// SimplificationContract.md section 6; https://github.com/asc-community/AngouriMath/issues/204
75+
var turn = TheRealRootsTurn(a, zr, zi);
6876
var (re, im) = useTheFraction
6977
? ByContinuedFraction((ar, ai), (zr, zi), Working(context, 10))
70-
: BySeries((ar, ai), (zr, zi), modulus, context);
71-
// Real where it is real, exactly: for a real a and a positive z, and for a positive whole a and any real
72-
// z, where Gamma(n, z) is (n - 1)! e^(-z) times a polynomial; z^a leaves a residue of e^(i pi n) there.
73-
if (ai.IsZero && zi.IsZero && (zr.Sign > 0 || ar.IsInteger() && ar.Sign > 0))
78+
: BySeries((ar, ai), (zr, zi), modulus, context, turn);
79+
// The continued fraction is used on the negative axis only past (P + 5) ln 10, where the value is e^|z| in
80+
// size and (1 - rho) Gamma(a) + rho times the principal value cancels nothing.
81+
if (turn is { } fractionsTurn && useTheFraction)
82+
{
83+
var work = Working(context, 10);
84+
var rho = Turned(fractionsTurn, work);
85+
var gamma = GammaOf((ar, ai), work);
86+
var turned = Multiply(rho, (re, im), work);
87+
(re, im) = (gamma.Re.Subtract(gamma.Re.Multiply(rho.Re, work), work).Add(turned.Re, work), turned.Im.Subtract(gamma.Re.Multiply(rho.Im, work), work));
88+
}
89+
// Real where it is real, exactly: for a real a and a positive z, for a positive whole a and any real z,
90+
// where Gamma(n, z) is (n - 1)! e^(-z) times a polynomial and z^a leaves a residue of e^(i pi n), and where
91+
// z^a is the real root, each factor of Gamma(a) - z^a g(a, z) being real.
92+
if (ai.IsZero && zi.IsZero && (zr.Sign > 0 || ar.IsInteger() && ar.Sign > 0 || turn is not null))
7493
im = EDecimal.Zero;
7594
// Otherwise each part is accurate to its own size, whatever that is, and is never rounded onto zero by
7695
// size: Gamma(1/2, 300) is 3e-132, and Gamma(100, -5 + i/10) has an imaginary part 1e-86 of its real one.
@@ -104,15 +123,39 @@ private static (EDecimal Re, EDecimal Im) ByContinuedFraction((EDecimal Re, EDec
104123
return Multiply(Multiply(ComplexExp((z.Re.Negate(), z.Im.Negate()), work), ComplexPower(z, a, work), work), h, work);
105124
}
106125

107-
private static (EDecimal Re, EDecimal Im) BySeries((EDecimal Re, EDecimal Im) a, (EDecimal Re, EDecimal Im) z, double modulus, EContext context)
126+
/// <summary>
127+
/// Where the power takes the real root of <c>z^a</c>, the turn from the principal value to it, <c>e^(i pi k/q)</c>
128+
/// as <c>(k, q)</c>: an exact <paramref name="a"/> of odd denominator <c>q</c> below 10 and a negative real
129+
/// <c>z</c>, the case <see cref="Pow"/> searches for a real root in. <see langword="null"/> elsewhere.
130+
/// </summary>
131+
private static (EInteger Turns, EInteger Q)? TheRealRootsTurn(Complex a, EDecimal zr, EDecimal zi)
132+
{
133+
if (a is not Rational { ERational: var exponent } || a is Integer || !zi.IsZero || zr.Sign >= 0)
134+
return null;
135+
var q = exponent.Denominator.Abs();
136+
if (q.IsEven || q.CompareTo(EInteger.FromInt32(10)) >= 0)
137+
return null;
138+
// (-1)^p e^(-i pi p/q) = e^(i pi p (q - 1)/q), the angle taken modulo 2 pi.
139+
var p = exponent.Denominator.Sign < 0 ? exponent.Numerator.Negate() : exponent.Numerator;
140+
return (p.Multiply(q.Subtract(EInteger.One)).Mod(q.Multiply(EInteger.FromInt32(2))), q);
141+
}
142+
143+
private static (EDecimal Re, EDecimal Im) Turned((EInteger Turns, EInteger Q) turn, EContext work)
144+
{
145+
var angle = InternalAMExtensions.ConstantCache.Lookup(work).Pi.Multiply(EDecimal.FromEInteger(turn.Turns), work).Divide(EDecimal.FromEInteger(turn.Q), work);
146+
return (angle.Cos(work), angle.Sin(work));
147+
}
148+
149+
private static (EDecimal Re, EDecimal Im) BySeries((EDecimal Re, EDecimal Im) a, (EDecimal Re, EDecimal Im) z, double modulus, EContext context,
150+
(EInteger Turns, EInteger Q)? turn)
108151
{
109152
var guard = (int)Math.Ceiling(2 * modulus / Math.Log(10)) + 10;
110153
for (var attempt = 0; ; attempt++)
111154
{
112155
var work = Working(context, guard);
113156
var (value, scale) = a.Im.IsZero && a.Re.IsInteger() && a.Re.Sign <= 0
114157
? AtANonPositiveWholeOrder(a.Re.ToEInteger().Negate().ToInt32Checked(), z, work)
115-
: AsGammaLessTheLower(a, z, work);
158+
: AsGammaLessTheLower(a, z, work, turn);
116159
// How many digits the value lost to the cancellation, against the largest thing that cancelled.
117160
var size = Magnitude(value);
118161
var lost = size.IsZero ? guard : DecimalOrder(scale) - DecimalOrder(size) + 1;
@@ -125,7 +168,8 @@ private static (EDecimal Re, EDecimal Im) BySeries((EDecimal Re, EDecimal Im) a,
125168
/// <summary>
126169
/// <c>Gamma(a) - z^a e^(-z) sum_k z^k / (a)_(k + 1)</c>, and the size of the larger of the two terms.
127170
/// </summary>
128-
private static ((EDecimal Re, EDecimal Im) Value, EDecimal Scale) AsGammaLessTheLower((EDecimal Re, EDecimal Im) a, (EDecimal Re, EDecimal Im) z, EContext work)
171+
private static ((EDecimal Re, EDecimal Im) Value, EDecimal Scale) AsGammaLessTheLower((EDecimal Re, EDecimal Im) a, (EDecimal Re, EDecimal Im) z, EContext work,
172+
(EInteger Turns, EInteger Q)? turn)
129173
{
130174
var epsilon = EDecimal.Create(EInteger.One, EInteger.FromInt32(-work.Precision.ToInt32Checked()));
131175
// term_k = z^k / (a (a + 1) ... (a + k)), starting at 1/a
@@ -170,7 +214,10 @@ private static ((EDecimal Re, EDecimal Im) Value, EDecimal Scale) AsGammaLessThe
170214
break;
171215
}
172216
}
173-
var prefactor = Multiply(ComplexPower(z, a, work), ComplexExp((z.Re.Negate(), z.Im.Negate()), work), work);
217+
var power = ComplexPower(z, a, work);
218+
if (turn is { } realRoot)
219+
power = Multiply(Turned(realRoot, work), power, work);
220+
var prefactor = Multiply(power, ComplexExp((z.Re.Negate(), z.Im.Negate()), work), work);
174221
var lower = Multiply(prefactor, sum, work);
175222
var gamma = GammaOf(a, work);
176223
var scale = EDecimal.Max(Magnitude(gamma), Magnitude(prefactor).Multiply(peak, work));

‎Sources/AngouriMath/Docs/Contributing/SimplificationContract.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# What a simplification rule may assume
1+
# What a simplification rule may assume
22

33
A rewrite rule in this library is a claim: *this expression and that one are two ways of writing the
44
same thing.* This file says what has to be true for that claim to hold, so that a new rule can be
@@ -256,6 +256,8 @@ release touches a branch. A rewrite that changes any of them changes answers.
256256
| `(-8)^(1/5)` | `-1.5157...` | the real root |
257257
| `(-8)^(1/4)` | `1.1892 + 1.1892i` | principal — there is no real fourth root |
258258
| `(-8)^0.3333333333` | `1.0 + 1.7320i` | **principal**, not the real root |
259+
| `(-8)^(1/11)` | `1.1592 + 0.3404i` | principal: the real root is searched for below a denominator of 10 |
260+
| `gamma(1/3, -2)` | `10.2125...` | the power's real root, which `z^a` in `Gamma(a) - z^a g(a, z)` is |
259261

260262
Read the last three rows together, because they are a trap. For a negative base and an exact
261263
`Rational` exponent with an **odd denominator**, this library takes the real root. Write the same

‎Sources/Tests/UnitTests/Convenience/IncompleteGammaTest.cs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,40 @@ public void IsItsValue(string expression, string real, string imaginary)
5454
AssertClose(imaginary, value.ImaginaryPart.EDecimal);
5555
}
5656

57+
/// <summary>
58+
/// On the cut, for an exact order of odd denominator below 10, with the real root the power takes there:
59+
/// <c>Gamma(a) - z^a g(a, z)</c> with <c>g(a, z) = sum_k (-z)^k/(k! (a + k))</c> (DLMF 8.7.3) and <c>z^a</c> real. The
60+
/// references are that sum at 90 digits; the last is past the continued fraction's threshold.
61+
/// </summary>
62+
[Theory]
63+
[InlineData("gamma(1/3, -2)", "10.212552064243320672871765046103178972863292927441879189946367")]
64+
[InlineData("gamma(2/3, -4)", "-36.897696051078160810409733567406007863692044903007209488356707")]
65+
[InlineData("gamma(2/5, -17/15)", "-1.6376459092577785167485577680520445343763746015778210809826328")]
66+
[InlineData("gamma(-1/3, -300)", "9.71532351293767903599760645811774844613945799010165863950178E+126")]
67+
public void OnTheCutItTakesThePowersRealRoot(string expression, string real)
68+
{
69+
var value = Assert.IsAssignableFrom<Complex>(expression.EvalNumerical());
70+
AssertClose(real, value.RealPart.EDecimal);
71+
Assert.True(value.ImaginaryPart.EDecimal.IsZero, $"{value}");
72+
}
73+
74+
/// <summary>
75+
/// And so its derivative, whose power takes the same root, is the derivative of its value there: with the principal
76+
/// branch it was the derivative turned by <c>e^(-2 i pi/3)</c>.
77+
/// </summary>
78+
[Fact]
79+
public void OnTheCutTheDerivativeIsTheDerivativeOfTheValue()
80+
{
81+
var at = Rational.Create(-2, 1);
82+
var h = EDecimal.FromString("1E-30");
83+
var above = "gamma(1/3, z)".ToEntity().Substitute("z", Real.Create(EDecimal.FromInt32(-2).Add(h))).EvalNumerical();
84+
var below = "gamma(1/3, z)".ToEntity().Substitute("z", Real.Create(EDecimal.FromInt32(-2).Subtract(h))).EvalNumerical();
85+
var difference = Assert.IsType<Real>(above - below).EDecimal.Divide(h.Multiply(2), EContext.ForPrecision(100));
86+
var derivative = Assert.IsType<Real>("gamma(1/3, z)".ToEntity().Differentiate("z").Substitute("z", at).EvalNumerical()).EDecimal;
87+
var error = difference.Subtract(derivative, EContext.ForPrecision(100)).Abs().Divide(derivative.Abs(), EContext.ForPrecision(10));
88+
Assert.True(error.LessThan(EDecimal.FromString("1E-25")), $"the value changes at {difference} where the derivative is {derivative}");
89+
}
90+
5791
[Theory]
5892
[InlineData("gamma(1, x)", "e ^ (-x)")]
5993
[InlineData("gamma(3, x)", "2 * e ^ (-x) * (1 + x + x ^ 2 / 2)")]

0 commit comments

Comments
 (0)