@@ -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 ) ) ;
0 commit comments