+
    0iD2                         R t ^ RIHtHt ^ RIt^ RIHt ^ RIH	t	H
t
 ^ RIHt  ^ RIt^ RIHtHtHtHtHtHtHtHtHtHtHt ^ RIHt R tR	 tR
 tR tR tR t R t!]"R8X  d
   ]!! 4        R# R#   ] d     L0i ; i)zPrecompute coefficients of several series expansions
of Wright's generalized Bessel function Phi(a, b, x).

See https://dlmf.nist.gov/10.46.E1 with rho=a, beta=b, z=x.
)ArgumentParserRawTextHelpFormatterN)quad)minimize_scalar	curve_fit)time)
EulerGammaRationalSSum	factorialgamma	gammasimppi	polygammasymbolszeta)hornerc                    ^p \        R4      w  rr4. p. p. p\        W4,          \        V4      ,          \        W,          V,           4      ,          V^ \        P
                  34      p\        V4      \        P                  ! V4      ,          V,          p\        ^ V ^,           4       F  p	VP                  W4      P                  V^ 4      P                  4       P                  4       p
V
P                  \        ^ V4      ^4      P                  \        R 4      pVRV	,          ,          pVP                  W,          \        V	4      ,          4       VP                  \!        V4      4       VP                  \!        W,          P                  4       4      4       K  	  RpVR,          p\#        . R	OWVV.4       FF  w  r\        \%        V4      4       F)  pVRV RV R2\'        W,          4      ,           ,          pK+  	  KH  	  V# )
zATylor series expansion of Phi(a, b, x) in a=0 up to order 5.
    a b x kc                      ^ #      argss   *e/var/www/html/photoedit/myenv/lib/python3.14/site-packages/scipy/special/_precompute/wright_bessel.py<lambda> series_small_a.<locals>.<lambda>&       A    z=Tylor series expansion of Phi(a, b, x) in a=0 up to order 5.
zAPhi(a, b, x) = exp(x)/gamma(b) * sum(A[i] * X[i] * B[i], i=0..5)

[] = )AXB)r   r   r   r   r
   Infinitysympyexprangediffsubssimplifydoitr   replaceappendr   ziplenstr)orderabxkr%   r&   r'   
expressionntermx_partsnamecis                   r   series_small_arB      s    E#JA!
A
A
AQT)A,&uQSU|3aAJJ5GHJq%))A,&3J 1eAgq$))!Q/88:??A))IaOQ/79o6 	 	2'	il"#	 	..012  	IA	MMAq	2s1vA2dV1QCt$s14y00A  3 Hr    c                    \        R4      pRV ,          \        ,
          \        P                  ! RV,          \	        V4      ,          W^,
          ,          ,          V^V^,           34      ,           # )z|Symbolic expansion of digamma(z) in z=0 to order n.

See https://dlmf.nist.gov/5.7.E4 and with https://dlmf.nist.gov/5.5.E2
r9   r$   )r   r   r)   	summationr   )zr;   r9   s   && r   	dg_seriesrF   7   sU    
 	Aa4*a$q')A!H4q!QqSkBC Cr    c                P    \         P                  ! \        WV ,           4      W4      # )z8Symbolic expansion of polygamma(k, z) in z=0 to order n.)r)   r,   rF   )r9   rE   r;   s   &&&r   	pg_seriesrH   A   s    ::iQ3'..r    c                 
  aa ^o\        R4      w  rr#\        R4      w  rEp\        V\        V\        ^4      V/p. p. p	. p
. p\	        V4      \
        P                  ! V4      ,          \        W#,          \        V4      ,          \	        W,          V,           4      ,          V^ \        P                  34      ,          p\        ^ S^,           4       EFx  oVP                  V S4      P                  V ^ 4      P                  4       P                  4       pVP                  \!        ^ V4      ^4      P#                  \         R 4      pVRS,          ,          pW,          \	        V4      ,          pS^8  d   VP#                  \         VV3R l4      pVP%                  V^ S^,           S,
          R7      P'                  4       P                  \!        ^^4      R\        ^4      ,          4      P                  4       pVP)                  V S,          \        S4      ,          4       V	P)                  \+        V4      4       V
P)                  V4       EK{  	  \
        P,                  ! V
^,          P                  V4      V4      P/                  4       pVP1                  4        \        \3        V4      4       F-  pVV,          \        V4      ,          P                  4       VV&   K/  	  RpVR,          pVR,          pVR	,          pVR
,          pVR,          pVR,          p\5        RR.W.4       FJ  w  pp\        \3        V4      4       F,  pVRV RV R2,          pV\7        VV,          4      ,          pK.  	  KL  	  \        \3        V4      4       F  pVRV R2,          pV\7        VV,          4      ,          pVRV R2,          pV\7        VV,          P                  V\        V\        V\        ^4      /4      P9                  ^4      4      ,          pK  	  VR,          pVR,          pVR,          p\;        \        S^,
          4       Uu. uF/  q1V,          \        V4      ,          W^,           ,          ,          NK1  	  up4      pVV
^,          P                  V4      ,
          P                  4       pVRV\        ^ 4      8H   2,          pVR,          p\;        \        S^,
          4       Uu. uF/  q1V,          \        V4      ,          W^,           ,          ,          NK1  	  up4      pVV
^,          P                  V4      ,
          P                  4       pVRV\        ^ 4      8H   2,          pV# u upi u upi )an  Tylor series expansion of Phi(a, b, x) in a=0 and b=0 up to order 5.

Be aware of cancellation of poles in b=0 of digamma(b)/Gamma(b) and
polygamma functions.

digamma(b)/Gamma(b) = -1 - 2*M_EG*b + O(b^2)
digamma(b)^2/Gamma(b) = 1/b + 3*M_EG + b*(-5/12*PI^2+7/2*M_EG^2) + O(b^2)
polygamma(1, b)/Gamma(b) = 1/b + M_EG + b*(1/12*PI^2 + 1/2*M_EG^2) + O(b^2)
and so on.
r   zM_PI M_EG M_Z3c                      ^ # r   r   r   s   *r   r   (series_small_a_small_b.<locals>.<lambda>e   r   r    c                 8   < \        WS^,           S,           4      #    )rH   )r9   r8   r;   r5   s   &&r   r   rK   m   s    9Q57193Mr    )r;   zDTylor series expansion of Phi(a, b, x) in a=0 and b=0 up to order 5.z9
Phi(a, b, x) = exp(x) * sum(A[i] * X[i] * B[i], i=0..5)
z	B[0] = 1
z&B[i] = sum(C[k+i-1] * b**k/k!, k=0..)
z

M_PI = piz
M_EG = EulerGammaz
M_Z3 = zeta(3)r%   r&   r!   r"   r#   z
# C[z
C[z/

Test if B[i] does have the assumed structure.z"
C[i] are derived from B[1] alone.z:
Test B[2] == C[1] + b*C[2] + b^2/2*C[3] + b^3/6*C[4] + ..z
test successful = z-
Test B[3] == C[2] + b*C[3] + b^2/2*C[4] + ..r$   )r   r   r   r   r   r)   r*   r   r   r
   r(   r+   r,   r-   r.   r/   r   r0   seriesremoveOr1   r   Polycoeffsreverser3   r2   r4   evalfsum)r6   r7   r8   r9   M_PIM_EGM_Z3c_subsr%   r&   r'   Cr:   r<   r=   pg_partrA   r>   r?   r@   testr;   r5   s                        @@r   series_small_a_small_br^   F   so    E#JA!/0D$
D$q'48F
A
A
A
A
 q%))A,&AD1eACEl*Q1::,>?@J 1eAgq!$))!Q/88:??A))IaOQ/79o6 	 	2'+eAh&6ooi&MOG~~aeAgai~8	Yq!_baj9 
  	
Ail"#	 	+ 0 	

1Q499V$a(//1AIIK3q6]!y|#--/!  	OA	FFAA	22AA	A	ASzA6*as1vA2dV1QCt$$AQqTNA  +
 3q6]	vaS	S1Y	tA3d^	S1D*dBd1gFG%) 		  	<<A	..A	FFAE%'NCNq1Yq\!AcF**NCDD1Q499V$$..0Dad
|	,,A	99AE%'NCNq1Yq\!AcF**NCDD1Q499V$$..0Dad
|	,,AH D Ds   5U(%5U-c            	       a ^p  ! V3R lR\         P                  4      o ! V3R lR\         P                  4      p\        R4      w  r#pV! ^ W#4      pRpVR,          pVR,          pVR	,          pVR
,          pVR,          pVR,          pVR,          p\        ^V ^,           4       EF  pV! WrV4      V^V,           V,          ,          ,          P	                  4       p\         P
                  ! V4      P                  4        U	u. uF  qP                  4       NK  	  p
p	\         P                  ! V
4      p
W,          P	                  4       P                  V\         P                  4      pVP                  V^,           V/4      pVRV RV
 RV R2,          pVRV R\        V4       R2,          pEK
  	  ^ RIpVP                  R4      pVP                  RV4      pVP                  R4      pVP                  RV4      pVP!                  RR4      pVP!                  RR4      pVP                  R4      pVP                  RV4      pV# u up	i )a}  Asymptotic expansion for large x.

Phi(a, b, x) ~ Z^(1/2-b) * exp((1+a)/a * Z) * sum_k (-1)^k * C_k / Z^k
Z = (a*x)^(1/(1+a))

Wright (1935) lists the coefficients C_0 and C_1 (he calls them a_0 and
a_1). With slightly different notation, Paris (2017) lists coefficients
c_k up to order k=3.
Paris (2017) uses ZP = (1+a)/a * Z  (ZP = Z of Paris) and
C_k = C_0 * (-a/(1+a))^k * c_k
c                   @   <a  ] tR t^t o Rt^t]V3R l4       tRtV t	R# )asymptotic_series.<locals>.gzHelper function g according to Wright (1935)

g(n, rho, v) = (1 + (rho+2)/3 * v + (rho+2)*(rho+3)/(2*3) * v^2 + ...)

Note: Wright (1935) uses square root of above definition.
c                P  < V^ 8  g   \        R4      hV^ 8X  d   ^# S! V^,
          W#4      \        \        V^,           V,           4      \        V^,           4      ,          4      \        \        ^V,           4      \        ^4      ,          4      ,          W1,          ,          ,           # )r   zmust have n >= 0)
ValueErrorr   r   )clsr;   rhovgs   &&&&r   eval!asymptotic_series.<locals>.g.eval   s    6 !344a1c~c!eAguSU| ;<ac
58 34556T:: :r    r   N
__name__
__module____qualname____firstlineno____doc__nargsclassmethodrh   __static_attributes____classdictcell____classdict__rg   s   @r   rg   ra      s#     	 		: 
	:r    rg   c                   @   <a  ] tR t^t o Rt^t]V3R l4       tRtV t	R# )!asymptotic_series.<locals>.coef_CzCalculate coefficients C_m for integer m.

C_m is the coefficient of v^(2*m) in the Taylor expansion in v=0 of
Gamma(m+1/2)/(2*pi) * (2/(rho+1))^(m+1/2) * (1-v)^(-b)
    * g(rho, v)^(-m-1/2)
c                  < V^ 8  g   \        R4      h\        R4      p^V,
          V) ,          S! ^V,          W$4      V) \        ^^4      ,
          ,          ,          pVP                  V^V,          4      P	                  V^ 4      \        ^V,          4      ,          pV\        V\        ^^4      ,           4      ^\        ,          ,          ^V^,           ,          V\        ^^4      ,           ,          ,          ,          pV# )r   zmust have m >= 0rf   )rc   r   r	   r,   r-   r   r   r   )rd   mre   betarf   r:   resrg   s   &&&&   r   rh   &asymptotic_series.<locals>.coef_C.eval   s    6 !344AA#$!AaC.A2hq!n;L*MMJ//!QqS)..q!4y1~ECq8Aq>12ad;s1uIXa^);<= >CJr    r   Nrj   rt   s   @r   coef_Crw      s#     	 			 
		r    r}   z	xa b xap1z!Asymptotic expansion for large x
z.Phi(a, b, x) = Z**(1/2-b) * exp((1+a)/a * Z) 
z3               * sum((-1)**k * C[k]/Z**k, k=0..6)

zZ      = pow(a * x, 1/(1+a))
zA[k]   = pow(a, k)
zB[k]   = pow(b, k)
zAp1[k] = pow(1+a, k)

z#C[0] = 1./sqrt(2. * M_PI * Ap1[1])
zC[z] = C[0] / (z * Ap1[z])
z] *= z

Nzxa\*\*(\d+)zA[\1]z
b\*\*(\d+)zB[\1]xap1zAp1[1]xar6   z	(\d{10,})z\1.)r)   Functionr   r+   r.   rR   rS   denominatorlcmcollectfactorxreplacer4   recompilesubr0   )r5   r}   r   r7   r~   C0r>   rA   exprr8   r   r   re_are_b	re_digitsrg   s                  @r   asymptotic_seriesr      s    E:ENN :( , +&KB4	2	B,A	::A	@@A	))A	A	A	##A	//A1eAgqa B"qyL1;;=+0::d+;+B+B+DE+Da--/+DE6"'')11!U\\B}}bdD\*	r!Ls$77	r!E#d)D))  ::n%D1A::m$D1A			&(#A			$A 

<(Ifa AH# Fs   Ic            
       aaaa	a
a R o
RV
3R llo. ROo. ROo. ROo	\         P                  ! SSS	4      w  ooo	SP                  4       SP                  4       S	P                  4       uooo	. p \        S	P                  4       F5  oV P                  \        VVVV	V3R lRRRR/R7      P                  4       K7  	  \         P                  ! V 4      p RSR	SR
S	RV /pR p\        \        W!VR,          RR7      ^ ,          4      pRpVR,          pVR,          pVR,          pVR,          pTRP                  V Uu. uF  qUR NK  	  up4      ,          pV# u upi )a  Fit optimal choice of epsilon for integral representation.

The integrand of
    int_0^pi P(eps, a, b, x, phi) * dphi
can exhibit oscillatory behaviour. It stems from the cosine of P and can be
minimized by minimizing the arc length of the argument
    f(phi) = eps * sin(phi) - x * eps^(-a) * sin(a * phi) + (1 - b) * phi
of cos(f(phi)).
We minimize the arc length in eps for a grid of values (a, b, x) and fit a
parametric function to it.
c                    \         P                  ! RV ,          V) 4      pV \         P                  ! V4      ,          W,          V,          \         P                  ! W,          4      ,          ,
          ^,           V,
          # )zDerivative of f w.r.t. phi.g      ?)nppowercos)epsr6   r7   r8   phieps_as   &&&&& r   fp$optimal_epsilon_integral.<locals>.fp  sO    cA2&RVVC[ 155=266!'?#BBQFJJr    c                f   <a aaa \        VVV VV3R l^ \        P                  V^dR7      ^ ,          # )zCompute Arc length of f.

Note that the arc length of a function f from t0 to t1 is given by
    int_t0^t1 sqrt(1 + f'(t)^2) dt
c           
      `   < \         P                  ! ^S! SSSSV 4      ^,          ,           4      # rM   )r   sqrt)r   r6   r7   r   r   r8   s   &r   r   =optimal_epsilon_integral.<locals>.arclength.<locals>.<lambda>  s%    BsAq!S,A1,D(D Er    )epsrellimit)r   r   r   )r   r6   r7   r8   r   r   r   s   ffff&&r   	arclength+optimal_epsilon_integral.<locals>.arclength	  s.     EEruu!../1 	1r    MbP?c                 D   < S! V SS,          SS,          SS,          4      # Nr   )r   r   data_adata_bdata_xrA   s   &r   r   *optimal_epsilon_integral.<locals>.<lambda>  s    	#vay&)28))=r    Boundedxatol)boundsmethodoptionsr6   r7   r8   r   c           
        V R,          pV R,          pV R,          p	W,          \         P                  ! RV,          4      ,          \         P                  ! V^^V,           ,          \         P                  ! V	4      ,          ,           V\         P                  ! V) V,          4      ,          ,
          V^\         P                  ! Wg,          4      ,           ,          ,           4      ,           # )z#Compute parametric function to fit.r6   r7   r8   g      )r   r*   log)
dataA0A1A2A3A4A5r6   r7   r8   s
   &&&&&&&   r   func&optimal_epsilon_integral.<locals>.func*  s    IIIq))&&a1q5kBFF1I55RVVRC!G_8LLRVVBF^!345 66 	7r    trf)r   z7Fit optimal eps for integrand P via minimal arc length
zwith parametric function:
zBoptimal_eps = (A0 * b * exp(-a/2) + exp(A1 + 1 / (1 + a) * log(x)
z=              - A2 * exp(-A3 * a) + A4 / (1 + exp(A5 * a)))

z Fitted parameters A0 to A5 are:
z, z.5g)g{Gz?d   )
r   g?g      ?g?rN                  )r   rN   r      
   )rN   g      ?r   r   r      2   r      i  g     @@g     @g     @)r   i  )r   meshgridflattenr+   sizer1   r   r8   arraylistr   join)best_epsdfr   func_paramsr>   r8   r   r   r   r   r   rA   s         @@@@@@r   optimal_epsilon_integralr      s[   K
1 5FFEF[[@FFF$nn.0@$nn. FFFH6;; = =#/#,woG HIq		
   xx!H
v
v
v

B7 y2e9UCAFGKBA	&&A	NNA	JJA	,,A41gJ4	55AH 5s   9E
c                  F   \        4       p \        \        \        R 7      pVP	                  R\
        . RORR7       VP                  4       p^R ^R ^R ^R /pVP                  VP                  R 4      ! 4        \        R	\        4       V ,
          ^<,          R
 R24       R# ))descriptionformatter_classactionzchose what expansion to precompute
1 : Series for small a
2 : Series for small a and small b
3 : Asymptotic series for large x
    This may take some time (>4h).
4 : Fit optimal eps for integral representation.)typechoiceshelpc                  (    \        \        4       4      # r   )printrB   r   r    r   r   main.<locals>.<lambda>L  s    ~/0r    c                  (    \        \        4       4      # r   )r   r^   r   r    r   r   r   M  s    578r    c                  (    \        \        4       4      # r   )r   r   r   r    r   r   r   N  s    023r    c                  (    \        \        4       4      # r   )r   r   r   r    r   r   r   O  s    79:r    c                      \        R 4      # )zInvalid input.)r   r   r    r   r   r   Q  s
    E*:$;r    r!   z.1fz minutes elapsed.
N)rN   r      r   )
r   r   ro   r   add_argumentint
parse_argsgetr   r   )t0parserr   switchs       r   mainr   >  s    	B,@BF
sLP   D083:F
 JJt{{;<>	BR$$7
89r    __main__)#ro   argparser   r   numpyr   scipy.integrater   scipy.optimizer   r   r   r)   r   r	   r
   r   r   r   r   r   r   r   r   sympy.polys.polyfuncsr   ImportErrorrB   rF   rH   r^   r   r   r   rk   r   r    r   <module>r      s   
 :    5 	B B B B,
DC/
VrVrCL:. zF I
  		s   $A/ /A98A9