1 /*
2     Copyright (C) 2015 Fredrik Johansson
3 
4     This file is part of Arb.
5 
6     Arb is free software: you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 2.1 of the License, or
9     (at your option) any later version.  See <http://www.gnu.org/licenses/>.
10 */
11 
12 #include "acb_hypgeom.h"
13 
14 /*
15 
16 which == 0 -- z
17 which == 1 -- z/(z-1)
18 which == 2 -- 1/z
19 which == 3 -- 1/(1-z)
20 which == 4 -- 1-z
21 which == 5 -- 1-1/z
22 
23 */
24 
25 void
_acb_hypgeom_2f1_transform_limit(acb_t res,const acb_poly_t a,const acb_poly_t b,const acb_poly_t c,const acb_poly_t z,int which,slong prec)26 _acb_hypgeom_2f1_transform_limit(acb_t res, const acb_poly_t a, const acb_poly_t b,
27     const acb_poly_t c, const acb_poly_t z, int which, slong prec)
28 {
29     acb_poly_t ba, ca, cb, cab, ac1, bc1, ab1, ba1, w, t, u, v, s;
30     acb_t tt;
31 
32     acb_poly_init(ba);
33     acb_poly_init(ca); acb_poly_init(cb); acb_poly_init(cab);
34     acb_poly_init(ac1); acb_poly_init(bc1);
35     acb_poly_init(ab1); acb_poly_init(ba1);
36     acb_poly_init(w); acb_poly_init(t);
37     acb_poly_init(u); acb_poly_init(v);
38     acb_poly_init(s);
39     acb_init(tt);
40 
41     acb_poly_add_si(s, z, -1, prec);   /* s = 1 - z */
42     acb_poly_neg(s, s);
43     acb_poly_sub(ba, b, a, prec);      /* ba = b - a */
44     acb_poly_sub(ca, c, a, prec);      /* ca = c - a */
45     acb_poly_sub(cb, c, b, prec);      /* cb = c - b */
46     acb_poly_sub(cab, ca, b, prec);    /* cab = c - a - b */
47     acb_poly_add_si(ac1, ca, -1, prec); acb_poly_neg(ac1, ac1); /* ac1 = a - c + 1 */
48     acb_poly_add_si(bc1, cb, -1, prec); acb_poly_neg(bc1, bc1); /* bc1 = b - c + 1 */
49     acb_poly_add_si(ab1, ba, -1, prec); acb_poly_neg(ab1, ab1); /* ab1 = a - b + 1 */
50     acb_poly_add_si(ba1, ba, 1, prec);                          /* ba1 = b - a + 1 */
51 
52     /* t = left term, u = right term (DLMF 15.8.1 - 15.8.5) */
53     if (which == 2)
54     {
55         acb_poly_inv_series(w, z, 2, prec);  /* w = 1/z */
56         acb_hypgeom_2f1_series_direct(t, a, ac1, ab1, w, 1, 2, prec);
57         acb_hypgeom_2f1_series_direct(u, b, bc1, ba1, w, 1, 2, prec);
58     }
59     else if (which == 3)
60     {
61         acb_poly_inv_series(w, s, 2, prec);  /* w = 1/(1-z) */
62         acb_hypgeom_2f1_series_direct(t, a, cb, ab1, w, 1, 2, prec);
63         acb_hypgeom_2f1_series_direct(u, b, ca, ba1, w, 1, 2, prec);
64     }
65     else if (which == 4)
66     {
67         acb_poly_set(w, s);                  /* w = 1-z */
68         acb_poly_add(v, ac1, b, prec);       /* v = a+b-c+1 */
69         acb_hypgeom_2f1_series_direct(t, a, b, v, w, 1, 2, prec);
70         acb_poly_add_si(v, cab, 1, prec);    /* v = c-a-b+1 */
71         acb_hypgeom_2f1_series_direct(u, ca, cb, v, w, 1, 2, prec);
72     }
73     else if (which == 5)
74     {
75         acb_poly_inv_series(w, z, 2, prec);  /* w = 1-1/z */
76         acb_poly_neg(w, w);
77         acb_poly_add_si(w, w, 1, prec);
78         acb_poly_add(v, ac1, b, prec);       /* v = a+b-c+1 */
79         acb_hypgeom_2f1_series_direct(t, a, ac1, v, w, 1, 2, prec);
80         acb_poly_add_si(v, cab, 1, prec);    /* v = c-a-b+1 */
81         acb_poly_add_si(u, a, -1, prec);     /* u = 1-a */
82         acb_poly_neg(u, u);
83         acb_hypgeom_2f1_series_direct(u, ca, u, v, w, 1, 2, prec);
84     }
85     else
86     {
87         flint_printf("invalid transformation!\n");
88         flint_abort();
89     }
90 
91     /* gamma factors */
92     acb_poly_rgamma_series(v, a, 2, prec);
93     acb_poly_mullow(u, u, v, 2, prec);
94     acb_poly_rgamma_series(v, ca, 2, prec);
95     acb_poly_mullow(t, t, v, 2, prec);
96 
97     acb_poly_rgamma_series(v, b, 2, prec);
98     if (which == 2 || which == 3)
99         acb_poly_mullow(t, t, v, 2, prec);
100     else
101         acb_poly_mullow(u, u, v, 2, prec);
102 
103     acb_poly_rgamma_series(v, cb, 2, prec);
104     if (which == 2 || which == 3)
105         acb_poly_mullow(u, u, v, 2, prec);
106     else
107         acb_poly_mullow(t, t, v, 2, prec);
108 
109     if (which == 2 || which == 3)
110     {
111         if (which == 2)
112             acb_poly_neg(s, z);  /* -z, otherwise 1-z since before */
113 
114         acb_poly_neg(v, a);
115         acb_poly_pow_series(v, s, v, 2, prec);
116         acb_poly_mullow(t, t, v, 2, prec);
117 
118         acb_poly_neg(v, b);
119         acb_poly_pow_series(v, s, v, 2, prec);
120         acb_poly_mullow(u, u, v, 2, prec);
121     }
122     else
123     {
124         acb_poly_pow_series(v, s, cab, 2, prec);
125         acb_poly_mullow(u, u, v, 2, prec);
126 
127         if (which == 5)
128         {
129             acb_poly_neg(v, a);
130             acb_poly_pow_series(v, z, v, 2, prec);
131             acb_poly_mullow(t, t, v, 2, prec);
132 
133             acb_poly_neg(v, ca);
134             acb_poly_pow_series(v, z, v, 2, prec);
135             acb_poly_mullow(u, u, v, 2, prec);
136         }
137     }
138 
139     acb_poly_sub(t, t, u, prec);
140 
141     if (which == 2 || which == 3)
142         acb_poly_sin_pi_series(v, ba, 2, prec);
143     else
144         acb_poly_sin_pi_series(v, cab, 2, prec);
145 
146     acb_poly_get_coeff_acb(tt, t, 1);
147     acb_poly_get_coeff_acb(res, v, 1);
148     acb_div(res, tt, res, prec);
149     acb_const_pi(tt, prec);
150     acb_mul(res, res, tt, prec);
151 
152     acb_poly_clear(ba);
153     acb_poly_clear(ca); acb_poly_clear(cb); acb_poly_clear(cab);
154     acb_poly_clear(ac1); acb_poly_clear(bc1);
155     acb_poly_clear(ab1); acb_poly_clear(ba1);
156     acb_poly_clear(w); acb_poly_clear(t);
157     acb_poly_clear(u); acb_poly_clear(v);
158     acb_poly_clear(s);
159     acb_clear(tt);
160 }
161 
162 void
acb_hypgeom_2f1_transform_limit(acb_t res,const acb_t a,const acb_t b,const acb_t c,const acb_t z,int regularized,int which,slong prec)163 acb_hypgeom_2f1_transform_limit(acb_t res, const acb_t a, const acb_t b,
164     const acb_t c, const acb_t z, int regularized, int which, slong prec)
165 {
166     acb_poly_t aa, bb, cc, zz;
167     acb_t t;
168 
169     if (acb_contains_zero(z) || !acb_is_finite(z))
170     {
171         acb_indeterminate(res);
172         return;
173     }
174 
175     if (arb_contains_si(acb_realref(z), 1) && arb_contains_zero(acb_imagref(z)))
176     {
177         acb_indeterminate(res);
178         return;
179     }
180 
181     if (!regularized)
182     {
183         acb_init(t);
184         acb_gamma(t, c, prec);
185         acb_hypgeom_2f1_transform_limit(res, a, b, c, z, 1, which, prec);
186         acb_mul(res, res, t, prec);
187         acb_clear(t);
188         return;
189     }
190 
191     acb_poly_init(aa);
192     acb_poly_init(bb);
193     acb_poly_init(cc);
194     acb_poly_init(zz);
195     acb_init(t);
196 
197     acb_poly_set_acb(aa, a);
198     acb_poly_set_acb(bb, b);
199     acb_poly_set_acb(cc, c);
200     acb_poly_set_acb(zz, z);
201 
202     if (which == 2 || which == 3)
203     {
204         acb_sub(t, b, a, prec);
205         acb_poly_set_coeff_si(aa, 1, 1);
206 
207         /* prefer b-a nonnegative (either is correct) to avoid
208            expensive operations in the hypergeometric series */
209         if (arb_is_nonnegative(acb_realref(t)))
210             _acb_hypgeom_2f1_transform_limit(res, aa, bb, cc, zz, which, prec);
211         else
212             _acb_hypgeom_2f1_transform_limit(res, bb, aa, cc, zz, which, prec);
213     }
214     else
215     {
216         acb_poly_set_coeff_si(aa, 1, 1);
217         _acb_hypgeom_2f1_transform_limit(res, aa, bb, cc, zz, which, prec);
218     }
219 
220     acb_poly_clear(aa);
221     acb_poly_clear(bb);
222     acb_poly_clear(cc);
223     acb_poly_clear(zz);
224     acb_clear(t);
225 }
226 
227 void
acb_hypgeom_2f1_transform_nolimit(acb_t res,const acb_t a,const acb_t b,const acb_t c,const acb_t z,int regularized,int which,slong prec)228 acb_hypgeom_2f1_transform_nolimit(acb_t res, const acb_t a, const acb_t b,
229     const acb_t c, const acb_t z, int regularized, int which, slong prec)
230 {
231     acb_t ba, ca, cb, cab, ac1, bc1, ab1, ba1, w, t, u, v, s;
232 
233     if (acb_contains_zero(z) || !acb_is_finite(z))
234     {
235         acb_indeterminate(res);
236         return;
237     }
238 
239     if (arb_contains_si(acb_realref(z), 1) && arb_contains_zero(acb_imagref(z)))
240     {
241         acb_indeterminate(res);
242         return;
243     }
244 
245     if (!regularized)
246     {
247         acb_init(t);
248         acb_gamma(t, c, prec);
249         acb_hypgeom_2f1_transform_nolimit(res, a, b, c, z, 1, which, prec);
250         acb_mul(res, res, t, prec);
251         acb_clear(t);
252         return;
253     }
254 
255     acb_init(ba);
256     acb_init(ca); acb_init(cb); acb_init(cab);
257     acb_init(ac1); acb_init(bc1);
258     acb_init(ab1); acb_init(ba1);
259     acb_init(w); acb_init(t);
260     acb_init(u); acb_init(v);
261     acb_init(s);
262 
263     acb_add_si(s, z, -1, prec);   /* s = 1 - z */
264     acb_neg(s, s);
265 
266     acb_sub(ba, b, a, prec);      /* ba = b - a */
267     acb_sub(ca, c, a, prec);      /* ca = c - a */
268     acb_sub(cb, c, b, prec);      /* cb = c - b */
269     acb_sub(cab, ca, b, prec);    /* cab = c - a - b */
270 
271     acb_add_si(ac1, ca, -1, prec); acb_neg(ac1, ac1); /* ac1 = a - c + 1 */
272     acb_add_si(bc1, cb, -1, prec); acb_neg(bc1, bc1); /* bc1 = b - c + 1 */
273     acb_add_si(ab1, ba, -1, prec); acb_neg(ab1, ab1); /* ab1 = a - b + 1 */
274     acb_add_si(ba1, ba, 1, prec);                     /* ba1 = b - a + 1 */
275 
276     /* t = left term, u = right term (DLMF 15.8.1 - 15.8.5) */
277     if (which == 2)
278     {
279         acb_inv(w, z, prec);  /* w = 1/z */
280         acb_hypgeom_2f1_direct(t, a, ac1, ab1, w, 1, prec);
281         acb_hypgeom_2f1_direct(u, b, bc1, ba1, w, 1, prec);
282     }
283     else if (which == 3)
284     {
285         acb_inv(w, s, prec);  /* w = 1/(1-z) */
286         acb_hypgeom_2f1_direct(t, a, cb, ab1, w, 1, prec);
287         acb_hypgeom_2f1_direct(u, b, ca, ba1, w, 1, prec);
288     }
289     else if (which == 4)
290     {
291         acb_set(w, s);                  /* w = 1-z */
292         acb_add(v, ac1, b, prec);       /* v = a+b-c+1 */
293         acb_hypgeom_2f1_direct(t, a, b, v, w, 1, prec);
294         acb_add_si(v, cab, 1, prec);    /* v = c-a-b+1 */
295         acb_hypgeom_2f1_direct(u, ca, cb, v, w, 1, prec);
296     }
297     else if (which == 5)
298     {
299         acb_inv(w, z, prec);  /* w = 1-1/z */
300         acb_neg(w, w);
301         acb_add_si(w, w, 1, prec);
302         acb_add(v, ac1, b, prec);       /* v = a+b-c+1 */
303         acb_hypgeom_2f1_direct(t, a, ac1, v, w, 1, prec);
304         acb_add_si(v, cab, 1, prec);    /* v = c-a-b+1 */
305         acb_add_si(u, a, -1, prec);     /* u = 1-a */
306         acb_neg(u, u);
307         acb_hypgeom_2f1_direct(u, ca, u, v, w, 1, prec);
308     }
309     else
310     {
311         flint_printf("invalid transformation!\n");
312         flint_abort();
313     }
314 
315     /* gamma factors */
316     acb_rgamma(v, a, prec);
317     acb_mul(u, u, v, prec);
318     acb_rgamma(v, ca, prec);
319     acb_mul(t, t, v, prec);
320 
321     acb_rgamma(v, b, prec);
322     if (which == 2 || which == 3)
323         acb_mul(t, t, v, prec);
324     else
325         acb_mul(u, u, v, prec);
326 
327     acb_rgamma(v, cb, prec);
328     if (which == 2 || which == 3)
329         acb_mul(u, u, v, prec);
330     else
331         acb_mul(t, t, v, prec);
332 
333     if (which == 2 || which == 3)
334     {
335         if (which == 2)
336             acb_neg(s, z);  /* -z, otherwise 1-z since before */
337 
338         acb_neg(v, a);
339         acb_pow(v, s, v, prec);
340         acb_mul(t, t, v, prec);
341 
342         acb_neg(v, b);
343         acb_pow(v, s, v, prec);
344         acb_mul(u, u, v, prec);
345     }
346     else
347     {
348         acb_pow(v, s, cab, prec);
349         acb_mul(u, u, v, prec);
350 
351         if (which == 5)
352         {
353             acb_neg(v, a);
354             acb_pow(v, z, v, prec);
355             acb_mul(t, t, v, prec);
356 
357             acb_neg(v, ca);
358             acb_pow(v, z, v, prec);
359             acb_mul(u, u, v, prec);
360         }
361     }
362 
363     acb_sub(t, t, u, prec);
364 
365     if (which == 2 || which == 3)
366         acb_sin_pi(v, ba, prec);
367     else
368         acb_sin_pi(v, cab, prec);
369 
370     acb_div(t, t, v, prec);
371     acb_const_pi(v, prec);
372     acb_mul(t, t, v, prec);
373     acb_set(res, t);
374 
375     acb_clear(ba);
376     acb_clear(ca); acb_clear(cb); acb_clear(cab);
377     acb_clear(ac1); acb_clear(bc1);
378     acb_clear(ab1); acb_clear(ba1);
379     acb_clear(w); acb_clear(t);
380     acb_clear(u); acb_clear(v);
381     acb_clear(s);
382 }
383 
384 void
acb_hypgeom_2f1_transform(acb_t res,const acb_t a,const acb_t b,const acb_t c,const acb_t z,int flags,int which,slong prec)385 acb_hypgeom_2f1_transform(acb_t res, const acb_t a, const acb_t b,
386     const acb_t c, const acb_t z, int flags, int which, slong prec)
387 {
388     int regularized;
389 
390     regularized = flags & ACB_HYPGEOM_2F1_REGULARIZED;
391 
392     if (which == 1)
393     {
394         acb_t t, u, v;
395 
396         acb_init(t);
397         acb_init(u);
398         acb_init(v);
399 
400         acb_sub_ui(t, z, 1, prec); /* t = z-1 */
401         acb_div(u, z, t, prec); /* u = z/(z-1) */
402         acb_neg(t, t);
403         acb_neg(v, a);
404         acb_pow(t, t, v, prec); /* t = (1-z)^-a */
405         acb_sub(v, c, b, prec); /* v = c-b */
406 
407         /* We cannot use regularized=1 directly, since if c is a nonnegative
408            integer, the transformation formula reads (lhs) * 0 = (rhs) * 0. */
409         acb_hypgeom_2f1_direct(u, a, v, c, u, 1, prec);
410 
411         if (!regularized)
412         {
413             acb_gamma(v, c, prec);
414             acb_mul(u, u, v, prec);
415         }
416 
417         acb_mul(res, u, t, prec);
418 
419 
420         acb_clear(t);
421         acb_clear(u);
422         acb_clear(v);
423     }
424     else
425     {
426         acb_t d;
427         int limit;
428 
429         acb_init(d);
430 
431         if (which == 2 || which == 3)
432         {
433             if (flags & ACB_HYPGEOM_2F1_AB)
434             {
435                 limit = 1;
436             }
437             else
438             {
439                 acb_sub(d, b, a, prec);
440                 limit = acb_is_int(d);
441             }
442         }
443         else
444         {
445             if (flags & ACB_HYPGEOM_2F1_ABC)
446             {
447                 limit = 1;
448             }
449             else
450             {
451                 acb_sub(d, c, a, prec);
452                 acb_sub(d, d, b, prec);
453                 limit = acb_is_int(d);
454             }
455         }
456 
457         if (limit)
458             acb_hypgeom_2f1_transform_limit(res, a, b, c, z, regularized, which, prec);
459         else
460             acb_hypgeom_2f1_transform_nolimit(res, a, b, c, z, regularized, which, prec);
461 
462         acb_clear(d);
463     }
464 
465     if (!acb_is_finite(res))
466         acb_indeterminate(res);
467 }
468 
469