Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - basemath - ellanal.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 21059-cbe0d6a) Lines: 666 726 91.7 %
Date: 2017-09-22 06:24:58 Functions: 55 58 94.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2010  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /********************************************************************/
      15             : /**                                                                **/
      16             : /**                  L functions of elliptic curves                **/
      17             : /**                                                                **/
      18             : /********************************************************************/
      19             : #include "pari.h"
      20             : #include "paripriv.h"
      21             : 
      22             : struct baby_giant
      23             : {
      24             :   GEN baby, giant, sum;
      25             :   GEN bnd, rbnd;
      26             : };
      27             : 
      28             : /* Generic Buhler-Gross algorithm */
      29             : 
      30             : struct bg_data
      31             : {
      32             :   GEN E, N; /* ell, conductor */
      33             :   GEN bnd; /* t_INT; will need all an for n <= bnd */
      34             :   ulong rootbnd; /* sqrt(bnd) */
      35             :   GEN an; /* t_VECSMALL: cache of an, n <= rootbnd */
      36             :   GEN p;  /* t_VECSMALL: primes <= rootbnd */
      37             : };
      38             : 
      39             : typedef void bg_fun(void*el, GEN n, GEN a);
      40             : 
      41             : /* a = a_n, where p = bg->pp[i] divides n, and lasta = a_{n/p}.
      42             :  * Call fun(E, N, a_N), for all N, n | N, P^+(N) <= p, a_N != 0,
      43             :  * i.e. assumes that fun accumulates a_N * w(N) */
      44             : 
      45             : static void
      46     1949409 : gen_BG_add(void *E, bg_fun *fun, struct bg_data *bg, GEN n, long i, GEN a, GEN lasta)
      47             : {
      48     1949409 :   pari_sp av = avma;
      49             :   long j;
      50     1949409 :   ulong nn = itou_or_0(n);
      51     1949409 :   if (nn && nn <= bg->rootbnd) bg->an[nn] = itos(a);
      52             : 
      53     1949409 :   if (signe(a))
      54             :   {
      55      481180 :     fun(E, n, a);
      56      481180 :     j = 1;
      57             :   }
      58             :   else
      59     1468229 :     j = i;
      60     3893211 :   for(; j <= i; j++)
      61             :   {
      62     3091599 :     ulong p = bg->p[j];
      63     3091599 :     GEN nexta, pn = mului(p, n);
      64     5041008 :     if (cmpii(pn, bg->bnd) > 0) return;
      65     1943802 :     nexta = mulis(a, bg->an[p]);
      66     1943802 :     if (i == j && umodiu(bg->N, p)) nexta = subii(nexta, mului(p, lasta));
      67     1943802 :     gen_BG_add(E, fun, bg, pn, j, nexta, a);
      68     1943802 :     avma = av;
      69             :   }
      70             : }
      71             : 
      72             : static void
      73          70 : gen_BG_init(struct bg_data *bg, GEN E, GEN N, GEN bnd)
      74             : {
      75          70 :   bg->E = E;
      76          70 :   bg->N = N;
      77          70 :   bg->bnd = bnd;
      78          70 :   bg->rootbnd = itou(sqrtint(bnd));
      79          70 :   bg->p = primes_upto_zv(bg->rootbnd);
      80          70 :   bg->an = ellanQ_zv(E, bg->rootbnd);
      81          70 : }
      82             : 
      83             : static void
      84          70 : gen_BG_rec(void *E, bg_fun *fun, struct bg_data *bg)
      85             : {
      86          70 :   long i, j, lp = lg(bg->p)-1;
      87          70 :   GEN bndov2 = shifti(bg->bnd, -1);
      88          70 :   pari_sp av = avma, av2;
      89             :   GEN p;
      90             :   forprime_t S;
      91          70 :   (void)forprime_init(&S, utoipos(bg->p[lp]+1), bg->bnd);
      92          70 :   av2 = avma;
      93          70 :   if (DEBUGLEVEL)
      94           0 :     err_printf("1st stage, using recursion for p <= %ld\n", bg->p[lp]);
      95        5677 :   for (i = 1; i <= lp; i++)
      96             :   {
      97        5607 :     ulong pp = bg->p[i];
      98        5607 :     long ap = bg->an[pp];
      99        5607 :     gen_BG_add(E, fun, bg, utoipos(pp), i, stoi(ap), gen_1);
     100        5607 :     avma = av2;
     101             :   }
     102          70 :   if (DEBUGLEVEL) err_printf("2nd stage, looping for p <= %Ps\n", bndov2);
     103     1402758 :   while ( (p = forprime_next(&S)) )
     104             :   {
     105             :     long jmax;
     106     1402688 :     GEN ap = ellap(bg->E, p);
     107     1402688 :     pari_sp av3 = avma;
     108     1402688 :     if (!signe(ap)) continue;
     109             : 
     110      700714 :     jmax = itou( divii(bg->bnd, p) ); /* 2 <= jmax <= el->rootbound */
     111      700714 :     fun(E, p, ap);
     112    10957478 :     for (j = 2;  j <= jmax; j++)
     113             :     {
     114    10256764 :       long aj = bg->an[j];
     115             :       GEN a, n;
     116    10256764 :       if (!aj) continue;
     117     1545635 :       a = mulis(ap, aj);
     118     1545635 :       n = muliu(p, j);
     119     1545635 :       fun(E, n, a);
     120     1545635 :       avma = av3;
     121             :     }
     122      700714 :     avma = av2;
     123      700714 :     if (abscmpii(p, bndov2) >= 0) break;
     124             :   }
     125          70 :   if (DEBUGLEVEL) err_printf("3nd stage, looping for p <= %Ps\n", bg->bnd);
     126     1260021 :   while ( (p = forprime_next(&S)) )
     127             :   {
     128     1259881 :     GEN ap = ellap(bg->E, p);
     129     1259881 :     if (!signe(ap)) continue;
     130      629356 :     fun(E, p, ap);
     131      629356 :     avma = av2;
     132             :   }
     133          70 :   avma = av;
     134          70 : }
     135             : 
     136             : /******************************************************************
     137             :  *
     138             :  * L functions of elliptic curves
     139             :  * Pascal Molin (molin.maths@gmail.com) 2014
     140             :  *
     141             :  ******************************************************************/
     142             : 
     143             : struct lcritical
     144             : {
     145             :   GEN h;    /* real */
     146             :   long cprec; /* computation prec */
     147             :   long L; /* number of points */
     148             :   GEN  K; /* length of series */
     149             :   long real;
     150             : };
     151             : 
     152             : static double
     153         609 : logboundG0(long e, double aY)
     154             : {
     155             :   double cla, loggam;
     156         609 :   cla = 1 + 1/sqrt(aY);
     157         609 :   if (e) cla = ( cla + 1/(2*aY) ) / (2*sqrt(aY));
     158         609 :   loggam = (e) ? LOG2-aY : -aY + log( log( 1+1/aY) );
     159         609 :   return log(cla) + loggam;
     160             : }
     161             : 
     162             : static void
     163         609 : param_points(GEN N, double Y, double tmax, long bprec, long *cprec, long *L,
     164             :              GEN *K, double *h)
     165             : {
     166             :   double D, a, aY, X, logM;
     167         609 :   long d = 2, w = 1;
     168         609 :   tmax *= d;
     169         609 :   D = bprec * LOG2 + M_PI/4*tmax + 2;
     170         609 :   *cprec = nbits2prec(ceil(D / LOG2) + 5);
     171         609 :   a = 2 * M_PI / sqrt(gtodouble(N));
     172         609 :   aY = a * cos(M_PI/2*Y);
     173         609 :   logM = 2*LOG2 + logboundG0(w+1, aY) + tmax * Y * M_PI/2;
     174         609 :   *h = ( 2 * M_PI * M_PI / 2 * Y ) / ( D + logM );
     175         609 :   X = log( D / a);
     176         609 :   *L = ceil( X / *h);
     177         609 :   *K = ceil_safe(dbltor( D / a ));
     178         609 : }
     179             : 
     180             : static GEN
     181         574 : vecF2_lk(GEN E, GEN K, GEN rbnd, GEN Q, GEN sleh, long prec)
     182             : {
     183         574 :   pari_sp av = avma, av2;
     184         574 :   long l, L  = lg(K)-1;
     185         574 :   GEN a = ellanQ_zv(E, itos(gel(K,1)));
     186         574 :   GEN S = cgetg(L+1, t_VEC);
     187       21575 :   for (l = 1; l <= L; l++)
     188       21001 :     gel(S,l) = cgetr(prec);
     189         574 :   av2 = avma;
     190       21575 :   for (l = 1; l <= L; ++l)
     191             :   {
     192             :     GEN e1, Sl;
     193             :     long aB, b, A, B;
     194             :     GEN z, zB;
     195             :     pari_sp av3;
     196       21001 :     long Kl = itou(gel(K,l));
     197             :     /* FIXME: could reduce prec here (useful for large prec) */
     198       21001 :     e1 = gel(Q, l);
     199       21001 :     Sl = real_0(prec);;
     200             :     /* baby-step giant step */
     201       21001 :     A = rbnd[l]; B = A;
     202       21001 :     z = powersr(e1, B); zB = gel(z, B+1);
     203       21001 :     av3 = avma;
     204      291773 :     for (aB = A*B; aB >= 0; aB -= B)
     205             :     {
     206      270772 :       GEN s = real_0(prec); /* could change also prec here */
     207    15721932 :       for (b = B; b > 0; --b)
     208             :       {
     209    15451160 :         long k = aB+b;
     210    15451160 :         if (k <= Kl && a[k]) s = addrr(s, mulsr(a[k], gel(z, b+1)));
     211    15451160 :         if (gc_needed(av3, 1))
     212          76 :           gerepileall(av3, 2, &s, &Sl);
     213             :       }
     214      270772 :       Sl = addrr(mulrr(Sl, zB), s);
     215             :     }
     216       21001 :     affrr(mulrr(Sl, gel(sleh,l)), gel(S, l)); /* to avoid copying all S */
     217       21001 :     avma = av2;
     218             :   }
     219         574 :   return gerepilecopy(av, S);
     220             : }
     221             : 
     222             : /* Return C, C[i][j] = Q[j]^i, i = 1..nb */
     223             : static void
     224          35 : baby_init(struct baby_giant *bb, GEN Q, GEN bnd, GEN rbnd, long prec)
     225             : {
     226          35 :   long i, j, l = lg(Q);
     227             :   GEN R, C, r0;
     228          35 :   C = cgetg(l,t_VEC);
     229        1218 :   for (i = 1; i < l; ++i)
     230        1183 :     gel(C, i) = powersr(gel(Q, i), rbnd[i]);
     231          35 :   R = cgetg(l,t_VEC);
     232          35 :   r0 = real_0(prec);
     233        1218 :   for (i = 1; i < l; ++i)
     234             :   {
     235        1183 :     gel(R, i) = cgetg(rbnd[i]+1, t_VEC);
     236        1183 :     gmael(R, i, 1) = cgetr(prec);
     237        1183 :     affrr(gmael(C, i, 2),gmael(R, i, 1));
     238       80234 :     for (j = 2; j <= rbnd[i]; j++)
     239             :     {
     240       79051 :       gmael(R, i, j) = cgetr(prec);
     241       79051 :       affrr(r0, gmael(R, i, j));
     242             :     }
     243             :   }
     244          35 :   bb->baby = C; bb->giant = R;
     245          35 :   bb->bnd = bnd; bb->rbnd = rbnd;
     246          35 : }
     247             : 
     248             : static long
     249         609 : baby_size(GEN rbnd, long Ks, long prec)
     250             : {
     251         609 :   long i, s, m, l = lg(rbnd);
     252       22793 :   for (s = 0, i = 1; i < l; ++i)
     253       22184 :     s += rbnd[i];
     254         609 :   m = 2*s*prec + 3*l + s;
     255         609 :   if (DEBUGLEVEL)
     256           0 :     err_printf("ellL1: BG_add: %ld words, ellan: %ld words\n", m, Ks);
     257         609 :   return m;
     258             : }
     259             : 
     260             : static void
     261      454972 : ellL1_add(void *E, GEN n, GEN a)
     262             : {
     263      454972 :   pari_sp av = avma;
     264      454972 :   struct baby_giant *bb = (struct baby_giant*) E;
     265      454972 :   long j, l = lg(bb->giant);
     266     2292864 :   for (j = 1; j < l; j++)
     267     2292864 :     if (cmpii(n, gel(bb->bnd,j)) <= 0)
     268             :     {
     269     1837892 :       ulong r, q = udiviu_rem(n, bb->rbnd[j], &r);
     270     1837892 :       GEN giant = gel(bb->giant, j), baby = gel(bb->baby, j);
     271     1837892 :       affrr(addrr(gel(giant, q+1), mulri(gel(baby, r+1), a)), gel(giant, q+1));
     272     1837892 :       avma = av;
     273      454972 :     } else break;
     274      454972 : }
     275             : 
     276             : static GEN
     277          35 : vecF2_lk_bsgs(GEN E, GEN bnd, GEN rbnd, GEN Q, GEN sleh, GEN N, long prec)
     278             : {
     279          35 :   pari_sp av = avma;
     280             :   struct bg_data bg;
     281             :   struct baby_giant bb;
     282          35 :   long k, L = lg(bnd)-1;
     283             :   GEN S;
     284          35 :   baby_init(&bb, Q, bnd, rbnd, prec);
     285          35 :   gen_BG_init(&bg, E, N, gel(bnd,1));
     286          35 :   gen_BG_rec((void*) &bb, ellL1_add, &bg);
     287          35 :   S = cgetg(L+1, t_VEC);
     288        1218 :   for (k = 1; k <= L; ++k)
     289             :   {
     290        1183 :     pari_sp av2 = avma;
     291        1183 :     long j, g = rbnd[k];
     292        1183 :     GEN giant = gmael(bb.baby, k, g+1);
     293        1183 :     GEN Sl = real_0(prec);
     294       81417 :     for (j = g; j >=1; j--)
     295       80234 :       Sl = addrr(mulrr(Sl, giant), gmael(bb.giant,k,j));
     296        1183 :     gel(S, k) = gerepileupto(av2, mulrr(gel(sleh,k), Sl));
     297             :   }
     298          35 :   return gerepileupto(av, S);
     299             : }
     300             : 
     301             : static GEN
     302         609 : vecF(struct lcritical *C, GEN E)
     303             : {
     304         609 :   pari_sp av = avma, av2;
     305         609 :   long prec = C->cprec, Ks = itos_or_0(C->K), l, L = C->L;
     306         609 :   GEN N = ellQ_get_N(E);
     307         609 :   GEN PiN = shiftr(divrr(mppi(prec), gsqrt(N, prec)), 1);
     308         609 :   GEN eh = mpexp(C->h), elh = powersr(eh, L-1), sleh = elh;
     309             :   GEN Q, bnd, rbnd, vec;
     310         609 :   rbnd = cgetg(L+1, t_VECSMALL);
     311         609 :   av2 = avma;
     312         609 :   bnd = cgetg(L+1, t_VEC);
     313         609 :   Q  = cgetg(L+1, t_VEC);
     314       22793 :   for (l = 1; l <= L; ++l)
     315             :   {
     316       22184 :     gel(bnd,l) = l==1 ? C->K: ceil_safe(divir(C->K, gel(elh, l)));
     317       22184 :     rbnd[l] = itou(sqrtint(gel(bnd,l)))+1;
     318       22184 :     gel(Q, l) = mpexp(mulrr(negr(PiN), gel(elh, l)));
     319             :   }
     320         609 :   gerepileall(av2, 2, &bnd, &Q);
     321         609 :   if (Ks && baby_size(rbnd, Ks, prec) > (Ks>>1))
     322         574 :     vec = vecF2_lk(E, bnd, rbnd, Q, sleh, prec);
     323             :   else
     324          35 :     vec = vecF2_lk_bsgs(E, bnd, rbnd, Q, sleh, N, prec);
     325         609 :   return gerepileupto(av, vec);
     326             : }
     327             : 
     328             : /* ************************************************************************
     329             :  *
     330             :  * Compute Lambda function by Fourier inversion
     331             :  *
     332             :  */
     333             : 
     334             : static GEN
     335         637 : glambda(GEN t, GEN vec, GEN h, long real, long prec)
     336             : {
     337             :   GEN ehs, elhs;
     338             :   GEN r;
     339         637 :   long L = lg(vec)-1, l;
     340             :   /* assume vec is a grid */
     341         637 :   ehs = gexp(gmul(gen_I(),gmul(h, t)), prec);
     342         637 :   elhs = (real == 1) ? gen_1 : mkcomplex(gen_0, gen_m1);
     343         637 :   r = gmul2n(greal(gmul(greal(gel(vec, 1)), elhs)), -1);
     344             :   /* FIXME: summing backward may be more stable */
     345       24921 :   for (l = 2; l <= L; ++l)
     346             :   {
     347       24284 :     elhs = gmul(elhs, ehs);
     348       24284 :     r = gadd(r, greal(gmul(gel(vec, l), elhs)));
     349             :   }
     350         637 :   return gmul(mulsr(4, h), r);
     351             : }
     352             : 
     353             : static GEN
     354         609 : Lpoints(struct lcritical *C, GEN e, GEN tmax, long bprec)
     355             : {
     356         609 :   double h = 0, Y = .97;
     357         609 :   GEN N = ellQ_get_N(e);
     358         609 :   param_points(N, Y, gtodouble(tmax), bprec, &C->cprec, &C->L, &C->K, &h);
     359         609 :   C->real = ellrootno_global(e);
     360         609 :   C->h = rtor(dbltor(h), C->cprec);
     361         609 :   return vecF(C, e);
     362             : }
     363             : 
     364             : static GEN
     365         637 : Llambda(GEN vec, struct lcritical *C, GEN t, long prec)
     366             : {
     367         637 :   GEN lambda = glambda(gprec_w(t, C->cprec), vec, C->h, C->real, C->cprec);
     368         637 :   return gprec_w(lambda, prec);
     369             : }
     370             : 
     371             : /* 2*(2*Pi)^(-s)*gamma(s)*N^(s/2); */
     372             : static GEN
     373         637 : ellgammafactor(GEN N, GEN s, long prec)
     374             : {
     375         637 :   GEN c = gpow(divrr(gsqrt(N,prec), Pi2n(1,prec)), s, prec);
     376         637 :   return gmul(gmul2n(c,1), ggamma(s, prec));
     377             : }
     378             : 
     379             : static GEN
     380         637 : ellL1_eval(GEN e, GEN vec, struct lcritical *C, GEN t, long prec)
     381             : {
     382         637 :   GEN g = ellgammafactor(ellQ_get_N(e), gaddgs(gmul(gen_I(),t), 1), prec);
     383         637 :   return gdiv(Llambda(vec, C, t, prec), g);
     384             : }
     385             : 
     386             : static GEN
     387         637 : ellL1_der(GEN e, GEN vec, struct lcritical *C, GEN t, long der, long prec)
     388             : {
     389         637 :   GEN r = polcoeff0(ellL1_eval(e, vec, C, t, prec), der, 0);
     390         637 :   r = gmul(r,powIs(C->real == 1 ? -der: 1-der));
     391         637 :   return gmul(real_i(r), mpfact(der));
     392             : }
     393             : 
     394             : GEN
     395         595 : ellL1_bitprec(GEN E, long r, long bitprec)
     396             : {
     397         595 :   pari_sp av = avma;
     398             :   struct lcritical C;
     399         595 :   long prec = nbits2prec(bitprec);
     400             :   GEN e, vec, t;
     401         595 :   if (r < 0)
     402           7 :     pari_err_DOMAIN("ellL1", "derivative order", "<", gen_0, stoi(r));
     403         588 :   e = ellanal_globalred(E, NULL);
     404         588 :   if (r == 0 && ellrootno_global(e) < 0) { avma = av; return gen_0; }
     405         574 :   vec = Lpoints(&C, e, gen_0, bitprec);
     406         574 :   t = r ? scalarser(gen_1, 0, r):  zeroser(0, 0);
     407         574 :   setvalp(t, 1);
     408         574 :   return gerepileupto(av, ellL1_der(e, vec, &C, t, r, prec));
     409             : }
     410             : 
     411             : GEN
     412         385 : ellL1(GEN E, long r, long prec) { return ellL1_bitprec(E, r, prec2nbits(prec)); }
     413             : 
     414             : GEN
     415          35 : ellanalyticrank_bitprec(GEN E, GEN eps, long bitprec)
     416             : {
     417          35 :   pari_sp av = avma, av2;
     418          35 :   long prec = nbits2prec(bitprec);
     419             :   struct lcritical C;
     420             :   pari_timer ti;
     421             :   GEN e, vec;
     422             :   long rk;
     423          35 :   if (DEBUGLEVEL) timer_start(&ti);
     424          35 :   if (!eps)
     425          35 :     eps = real2n(-bitprec/2+1, DEFAULTPREC);
     426             :   else
     427           0 :     if (typ(eps) != t_REAL) {
     428           0 :       eps = gtofp(eps, DEFAULTPREC);
     429           0 :       if (typ(eps) != t_REAL) pari_err_TYPE("ellanalyticrank", eps);
     430             :     }
     431          35 :   e = ellanal_globalred(E, NULL);
     432          35 :   vec = Lpoints(&C, e, gen_0, bitprec);
     433          35 :   if (DEBUGLEVEL) timer_printf(&ti, "init L");
     434          35 :   av2 = avma;
     435          63 :   for (rk = C.real>0 ? 0: 1;  ; rk += 2)
     436             :   {
     437             :     GEN Lrk;
     438          63 :     GEN t = rk ? scalarser(gen_1, 0, rk):  zeroser(0, 0);
     439          63 :     setvalp(t, 1);
     440          63 :     Lrk = ellL1_der(e, vec, &C, t, rk, prec);
     441          63 :     if (DEBUGLEVEL) timer_printf(&ti, "L^(%ld)=%Ps", rk, Lrk);
     442          63 :     if (abscmprr(Lrk, eps) > 0)
     443          70 :       return gerepilecopy(av, mkvec2(stoi(rk), Lrk));
     444          28 :     avma = av2;
     445          28 :   }
     446             : }
     447             : 
     448             : GEN
     449           0 : ellanalyticrank(GEN E, GEN eps, long prec)
     450             : {
     451           0 :   return ellanalyticrank_bitprec(E, eps, prec2nbits(prec));
     452             : }
     453             : 
     454             : /*        Heegner point computation
     455             : 
     456             :    This section is a C version by Bill Allombert of a GP script by
     457             :    Christophe Delaunay which was based on a GP script by John Cremona.
     458             :    Reference: Henri Cohen's book GTM 239.
     459             : */
     460             : 
     461             : static void
     462           0 : heegner_L1_bg(void*E, GEN n, GEN a)
     463             : {
     464           0 :   struct baby_giant *bb = (struct baby_giant*) E;
     465           0 :   long j, l = lg(bb->giant);
     466           0 :   for (j = 1; j < l; j++)
     467           0 :     if (cmpii(n, gel(bb->bnd,j)) <= 0)
     468             :     {
     469           0 :       ulong r, q = udiviu_rem(n, bb->rbnd[j], &r);
     470           0 :       GEN giant = gel(bb->giant, j), baby = gel(bb->baby, j);
     471           0 :       gaffect(gadd(gel(giant, q+1), gdiv(gmul(gel(baby, r+1), a), n)), gel(giant, q+1));
     472             :     }
     473           0 : }
     474             : 
     475             : static void
     476     2901913 : heegner_L1(void*E, GEN n, GEN a)
     477             : {
     478     2901913 :   struct baby_giant *bb = (struct baby_giant*) E;
     479     2901913 :   long j, l = lg(bb->giant);
     480    15942829 :   for (j = 1; j < l; j++)
     481    13040916 :     if (cmpii(n, gel(bb->bnd,j)) <= 0)
     482             :     {
     483    10642583 :       ulong r, q = udiviu_rem(n, bb->rbnd[j], &r);
     484    10642583 :       GEN giant = gel(bb->giant, j), baby = gel(bb->baby, j);
     485    10642583 :       GEN ex = mulreal(gel(baby, r+1), gel(giant, q+1));
     486    10642583 :       affrr(addrr(gel(bb->sum, j), divri(mulri(ex, a), n)), gel(bb->sum, j));
     487             :     }
     488     2901913 : }
     489             : /* Return C, C[i][j] = Q[j]^i, i = 1..nb */
     490             : static void
     491           0 : baby_init2(struct baby_giant *bb, GEN Q, GEN bnd, GEN rbnd, long prec)
     492             : {
     493           0 :   long i, j, l = lg(Q);
     494             :   GEN R, C, r0;
     495           0 :   C = cgetg(l,t_VEC);
     496           0 :   for (i = 1; i < l; ++i)
     497           0 :     gel(C, i) = gpowers(gel(Q, i), rbnd[i]);
     498           0 :   R = cgetg(l,t_VEC);
     499           0 :   r0 = mkcomplex(real_0(prec),real_0(prec));
     500           0 :   for (i = 1; i < l; ++i)
     501             :   {
     502           0 :     gel(R, i) = cgetg(rbnd[i]+1, t_VEC);
     503           0 :     gmael(R, i, 1) = cgetc(prec);
     504           0 :     gaffect(gmael(C, i, 2),gmael(R, i, 1));
     505           0 :     for (j = 2; j <= rbnd[i]; j++)
     506             :     {
     507           0 :       gmael(R, i, j) = cgetc(prec);
     508           0 :       gaffect(r0, gmael(R, i, j));
     509             :     }
     510             :   }
     511           0 :   bb->baby = C; bb->giant = R;
     512           0 :   bb->bnd = bnd; bb->rbnd = rbnd;
     513           0 : }
     514             : 
     515             : /* Return C, C[i][j] = Q[j]^i, i = 1..nb */
     516             : static void
     517          35 : baby_init3(struct baby_giant *bb, GEN Q, GEN bnd, GEN rbnd, long prec)
     518             : {
     519          35 :   long i, l = lg(Q);
     520             :   GEN R, C, S;
     521          35 :   C = cgetg(l,t_VEC);
     522         168 :   for (i = 1; i < l; ++i)
     523         133 :     gel(C, i) = gpowers(gel(Q, i), rbnd[i]);
     524          35 :   R = cgetg(l,t_VEC);
     525         168 :   for (i = 1; i < l; ++i)
     526         133 :     gel(R, i) = gpowers(gmael(C, i, 1+rbnd[i]), rbnd[i]);
     527          35 :   S = cgetg(l,t_VEC);
     528         168 :   for (i = 1; i < l; ++i)
     529             :   {
     530         133 :     gel(S, i) = cgetr(prec);
     531         133 :     affrr(real_i(gmael(C, i, 2)), gel(S, i));
     532             :   }
     533          35 :   bb->baby = C; bb->giant = R; bb->sum = S;
     534          35 :   bb->bnd = bnd; bb->rbnd = rbnd;
     535          35 : }
     536             : 
     537             : /* ymin a t_REAL */
     538             : static GEN
     539          35 : heegner_psi(GEN E, GEN N, GEN points, long bitprec)
     540             : {
     541          35 :   pari_sp av = avma, av2;
     542             :   struct baby_giant bb;
     543             :   struct bg_data bg;
     544          35 :   long k, L = lg(points)-1, prec = nbits2prec(bitprec)+EXTRAPRECWORD;
     545          35 :   GEN  Q, pi2 = Pi2n(1, prec), bnd, rbnd;
     546             :   long l;
     547          35 :   GEN B = divrr(mulur(bitprec,mplog2(DEFAULTPREC)), pi2);
     548             :   GEN bndmax;
     549          35 :   rbnd = cgetg(L+1, t_VECSMALL);
     550          35 :   av2 = avma;
     551          35 :   bnd = cgetg(L+1, t_VEC);
     552          35 :   Q  = cgetg(L+1, t_VEC);
     553         168 :   for (l = 1; l <= L; ++l)
     554             :   {
     555         133 :     gel(bnd,l) = ceil_safe(divrr(B,gimag(gel(points, l))));
     556         133 :     rbnd[l] = itou(sqrtint(gel(bnd,l)))+1;
     557         133 :     gel(Q, l) = expIxy(pi2, gel(points, l), prec);
     558             :   }
     559          35 :   gerepileall(av2, 2, &bnd, &Q);
     560          35 :   bndmax = gel(bnd,vecindexmax(bnd));
     561          35 :   gen_BG_init(&bg, E, N, bndmax);
     562          35 :   if (bitprec >= 1900)
     563             :   {
     564             :     GEN S;
     565           0 :     baby_init2(&bb, Q, bnd, rbnd, prec);
     566           0 :     gen_BG_rec((void*)&bb, heegner_L1_bg, &bg);
     567           0 :     S = cgetg(L+1, t_VEC);
     568           0 :     for (k = 1; k <= L; ++k)
     569             :     {
     570           0 :       pari_sp av2 = avma;
     571           0 :       long j, g = rbnd[k];
     572           0 :       GEN giant = gmael(bb.baby, k, g+1);
     573           0 :       GEN Sl = real_0(prec);
     574           0 :       for (j = g; j >=1; j--)
     575           0 :         Sl = gadd(gmul(Sl, giant), gmael(bb.giant,k,j));
     576           0 :       gel(S, k) = gerepileupto(av2, real_i(Sl));
     577             :     }
     578           0 :     return gerepileupto(av, S);
     579             :   }
     580             :   else
     581             :   {
     582          35 :     baby_init3(&bb, Q, bnd, rbnd, prec);
     583          35 :     gen_BG_rec((void*)&bb, heegner_L1, &bg);
     584          35 :     return gerepilecopy(av, bb.sum);
     585             :   }
     586             : }
     587             : 
     588             : /*Returns lambda_bad list for one prime p, nv = localred(E, p) */
     589             : static GEN
     590          84 : lambda1(GEN E, GEN nv, GEN p, long prec)
     591             : {
     592             :   pari_sp av;
     593             :   GEN res, lp;
     594          84 :   long kod = itos(gel(nv, 2));
     595          84 :   if (kod==2 || kod ==-2) return cgetg(1,t_VEC);
     596          84 :   av = avma; lp = glog(p, prec);
     597          84 :   if (kod > 4)
     598             :   {
     599          14 :     long n = Z_pval(ell_get_disc(E), p);
     600          14 :     long j, m = kod - 4, nl = 1 + (m >> 1L);
     601          14 :     res = cgetg(nl, t_VEC);
     602          35 :     for (j = 1; j < nl; j++)
     603          21 :       gel(res, j) = gmul(lp, gsubgs(gdivgs(sqru(j), n), j)); /* j^2/n - j */
     604             :   }
     605          70 :   else if (kod < -4)
     606           7 :     res = mkvec2(negr(lp), shiftr(mulrs(lp, kod), -2));
     607             :   else
     608             :   {
     609          63 :     const long lam[] = {8,9,0,6,0,0,0,3,4};
     610          63 :     long m = -lam[kod+4];
     611          63 :     res = mkvec(divru(mulrs(lp, m), 6));
     612             :   }
     613          84 :   return gerepilecopy(av, res);
     614             : }
     615             : 
     616             : static GEN
     617          35 : lambdalist(GEN E, long prec)
     618             : {
     619          35 :   pari_sp ltop = avma;
     620          35 :   GEN glob = ellglobalred(E), plist = gmael(glob,4,1), L = gel(glob,5);
     621          35 :   GEN res, v, D = ell_get_disc(E);
     622          35 :   long i, j, k, l, m, n, np = lg(plist), lr = 1;
     623          35 :   v = cgetg(np, t_VEC);
     624         126 :   for (j = 1, i = 1 ; j < np; ++j)
     625             :   {
     626          91 :     GEN p = gel(plist, j);
     627          91 :     if (dvdii(D, sqri(p)))
     628             :     {
     629          84 :       GEN la = lambda1(E, gel(L,j), p, prec);
     630          84 :       gel(v, i++) = la;
     631          84 :       lr *= lg(la);
     632             :     }
     633             :   }
     634          35 :   np = i;
     635          35 :   res = cgetg(lr+1, t_VEC);
     636          35 :   gel(res, 1) = gen_0; n = 1; m = 1;
     637         119 :   for (j = 1; j < np; ++j)
     638             :   {
     639          84 :     GEN w = gel(v, j);
     640          84 :     long lw = lg(w);
     641         294 :     for (k = 1; k <= n; k++)
     642             :     {
     643         210 :       GEN t = gel(res, k);
     644         434 :       for (l = 1, m = n; l < lw; l++, m+=n)
     645         224 :         gel(res, k + m) = mpadd(t, gel(w, l));
     646             :     }
     647          84 :     n = m;
     648             :   }
     649          35 :   return gerepilecopy(ltop, res);
     650             : }
     651             : 
     652             : /* P a t_INT or t_FRAC, return its logarithmic height */
     653             : static GEN
     654          70 : heightQ(GEN P, long prec)
     655             : {
     656             :   long s;
     657          70 :   if (typ(P) == t_FRAC)
     658             :   {
     659          28 :     GEN a = gel(P,1), b = gel(P,2);
     660          28 :     P = abscmpii(a,b) > 0 ? a: b;
     661             :   }
     662          70 :   s = signe(P);
     663          70 :   if (!s) return real_0(prec);
     664          56 :   if (s < 0) P = absi(P);
     665          56 :   return glog(P, prec);
     666             : }
     667             : 
     668             : /* t a t_INT or t_FRAC, returns max(1, log |t|), returns a t_REAL */
     669             : static GEN
     670          98 : logplusQ(GEN t, long prec)
     671             : {
     672          98 :   if (typ(t) == t_INT)
     673             :   {
     674          42 :     if (!signe(t)) return real_1(prec);
     675          28 :     if (signe(t) < 0) t = absi(t);
     676             :   }
     677             :   else
     678             :   {
     679          56 :     GEN a = gel(t,1), b = gel(t,2);
     680          56 :     if (abscmpii(a, b) < 0) return real_1(prec);
     681          28 :     if (signe(a) < 0) t = gneg(t);
     682             :   }
     683          56 :   return glog(t, prec);
     684             : }
     685             : 
     686             : /* See GTM239, p532, Th 8.1.18
     687             :  * Return M such that h_naive <= M */
     688             : static GEN
     689          70 : hnaive_max(GEN ell, GEN ht)
     690             : {
     691          70 :   const long prec = LOWDEFAULTPREC; /* minimal accuracy */
     692          70 :   GEN b2     = ell_get_b2(ell), j = ell_get_j(ell);
     693          70 :   GEN logd   = glog(absi(ell_get_disc(ell)), prec);
     694          70 :   GEN logj   = logplusQ(j, prec);
     695          70 :   GEN hj     = heightQ(j, prec);
     696         168 :   GEN logb2p = signe(b2)? addrr(logplusQ(gdivgs(b2, 12),prec), mplog2(prec))
     697          98 :                         : real_1(prec);
     698          70 :   GEN mu     = addrr(divru(addrr(logd, logj),6), logb2p);
     699          70 :   return addrs(addrr(addrr(ht, divru(hj,12)), mu), 2);
     700             : }
     701             : 
     702             : static GEN
     703         133 : qfb_root(GEN Q, GEN vDi)
     704             : {
     705         133 :   GEN a2 = shifti(gel(Q, 1),1), b = gel(Q, 2);
     706         133 :   return mkcomplex(gdiv(negi(b),a2),divri(vDi,a2));
     707             : }
     708             : 
     709             : static GEN
     710       23940 : qimag2(GEN Q)
     711             : {
     712       23940 :   pari_sp av = avma;
     713       23940 :   GEN z = gdiv(negi(qfb_disc(Q)), shifti(sqri(gel(Q, 1)),2));
     714       23940 :   return gerepileupto(av, z);
     715             : }
     716             : 
     717             : /***************************************************/
     718             : /*Routines for increasing the imaginary parts using*/
     719             : /*Atkin-Lehner operators                           */
     720             : /***************************************************/
     721             : 
     722             : static GEN
     723       23940 : qfb_mult(GEN Q, GEN M)
     724             : {
     725       23940 :   GEN A = gel(Q, 1) , B = gel(Q, 2) , C = gel(Q, 3);
     726       23940 :   GEN a = gcoeff(M, 1, 1), b = gcoeff(M, 1, 2);
     727       23940 :   GEN c = gcoeff(M, 2, 1), d = gcoeff(M, 2, 2);
     728       23940 :   GEN W1 = addii(addii(mulii(sqri(a), A), mulii(mulii(c, a), B)), mulii(sqri(c), C));
     729       23940 :   GEN W2 = addii(addii(mulii(mulii(shifti(b,1), a), A),
     730             :                        mulii(addii(mulii(d, a), mulii(c, b)), B)),
     731             :                  mulii(mulii(shifti(d,1), c), C));
     732       23940 :   GEN W3 = addii(addii(mulii(sqri(b), A), mulii(mulii(d, b), B)), mulii(sqri(d), C));
     733       23940 :   GEN D = gcdii(W1, gcdii(W2, W3));
     734       23940 :   if (!equali1(D)) {
     735       21644 :     W1 = diviiexact(W1,D);
     736       21644 :     W2 = diviiexact(W2,D);
     737       21644 :     W3 = diviiexact(W3,D);
     738             :   }
     739       23940 :   return qfi(W1, W2, W3);
     740             : }
     741             : 
     742             : #ifdef DEBUG
     743             : static void
     744             : best_point_old(GEN Q, GEN NQ, GEN f, GEN *u, GEN *v)
     745             : {
     746             :   long n, k;
     747             :   GEN U, c, d;
     748             :   GEN A = gel(f, 1);
     749             :   GEN B = gel(f, 2);
     750             :   GEN C = gel(f, 3);
     751             :   GEN q = qfi(mulii(NQ, C), negi(B), diviiexact(A, NQ));
     752             :   redimagsl2(q, &U);
     753             :   *u = c = gcoeff(U, 1, 1);
     754             :   *v = d = gcoeff(U, 2, 1);
     755             :   if (equali1(gcdii(mulii(*u, NQ), mulii(*v, Q))))
     756             :     return;
     757             :   for (n = 1; ; n++)
     758             :   {
     759             :     for (k = -n; k<=n; k++)
     760             :     {
     761             :       *u = addis(c, k); *v = addiu(d, n);
     762             :       if (equali1(ggcd(mulii(*u, NQ), mulii(*v, Q))))
     763             :         return;
     764             :       *v= subiu(d, n);
     765             :       if (equali1(ggcd(mulii(*u, NQ), mulii(*v, Q))))
     766             :         return;
     767             :       *u = addiu(c, n); *v = addis(d, k);
     768             :       if (equali1(ggcd(mulii(*u, NQ), mulii(*v, Q))))
     769             :         return;
     770             :       *u = subiu(c, n);
     771             :       if (equali1(ggcd(mulii(*u, NQ), mulii(*v, Q))))
     772             :         return;
     773             :     }
     774             :   }
     775             : }
     776             : /* q(x,y) = ax^2 + bxy + cy^2 */
     777             : static GEN
     778             : qfb_eval(GEN q, GEN x, GEN y)
     779             : {
     780             :   GEN a = gel(q,1), b = gel(q,2), c = gel(q,3);
     781             :   GEN x2 = sqri(x), y2 = sqri(y), xy = mulii(x,y);
     782             :   return addii(addii(mulii(a, x2), mulii(b,xy)), mulii(c, y2));
     783             : }
     784             : #endif
     785             : 
     786             : static long
     787        6573 : nexti(long i) { return i>0 ? -i : 1-i; }
     788             : 
     789             : /* q0 + i q1 + i^2 q2 */
     790             : static GEN
     791       12299 : qfmin_eval(GEN q0, GEN q1, GEN q2, long i)
     792       12299 : { return addii(mulis(addii(mulis(q2, i), q1), i), q0); }
     793             : 
     794             : /* assume a > 0, return gcd(a,b,c) */
     795             : static ulong
     796       16422 : gcduii(ulong a, GEN b, GEN c)
     797             : {
     798       16422 :   ulong d = a;
     799       16422 :   d = ugcd(umodiu(b, d), d );
     800       16422 :   if (d == 1) return 1;
     801        5747 :   d = ugcd(umodiu(c, d), d );
     802        5747 :   return d;
     803             : }
     804             : 
     805             : static void
     806       23940 : best_point(GEN Q, GEN NQ, GEN f, GEN *pu, GEN *pv)
     807             : {
     808       23940 :   GEN a = mulii(NQ, gel(f,3)), b = negi(gel(f,2)), c = diviiexact(gel(f,1), NQ);
     809       23940 :   GEN D = absi( qfb_disc(f) );
     810       23940 :   GEN U, qr = redimagsl2(qfi(a,b,c), &U);
     811       23940 :   GEN A = gel(qr,1), B = gel(qr,2), A2 = shifti(A,1), AA4 = sqri(A2);
     812             :   GEN V, best;
     813             :   long y;
     814             : 
     815             :   /* 4A qr(x,y) = (2A x + By)^2 + D y^2
     816             :    * Write x = x0(y) + i, where x0 is an integer minimum
     817             :    * (the smallest in case of tie) of x-> qr(x,y), for given y.
     818             :    * 4A qr(x,y) = ((2A x0 + By)^2 + Dy^2) + 4A i (2A x0 + By) + 4A^2 i^2
     819             :    *            = q0(y) + q1(y) i + q2 i^2
     820             :    * Loop through (x,y), y>0 by (roughly) increasing values of qr(x,y) */
     821             : 
     822             :   /* We must test whether [X,Y]~ := U * [x,y]~ satisfy (X NQ, Y Q) = 1
     823             :    * This is equivalent to (X,Y) = 1 (note that (X,Y) = (x,y)), and
     824             :    * (X, Q) = (Y, NQ) = 1.
     825             :    * We have U * [x0+i, y]~ = U * [x0,y]~ + i U[,1] =: V0 + i U[,1] */
     826             : 
     827             :   /* try [1,0]~ = first minimum */
     828       23940 :   V = gel(U,1); /* U *[1,0]~ */
     829       23940 :   *pu = gel(V,1);
     830       23940 :   *pv = gel(V,2);
     831       42238 :   if (is_pm1(gcdii(*pu, Q)) && is_pm1(gcdii(*pv, NQ))) return;
     832             : 
     833             :   /* try [0,1]~ = second minimum */
     834       11802 :   V = gel(U,2); /* U *[0,1]~ */
     835       11802 :   *pu = gel(V,1);
     836       11802 :   *pv = gel(V,2);
     837       11802 :   if (is_pm1(gcdii(*pu, Q)) && is_pm1(gcdii(*pv, NQ))) return;
     838             : 
     839             :   /* (X,Y) = (1, \pm1) always works. Try to do better now */
     840        5642 :   best = subii(addii(a, c), absi(b));
     841        5642 :   *pu = gen_1;
     842        5642 :   *pv = signe(b) < 0? gen_1: gen_m1;
     843             : 
     844       14763 :   for (y = 1;; y++)
     845             :   {
     846             :     GEN Dy2, r, By, x0, q0, q1, V0;
     847             :     long i;
     848       14763 :     if (y > 1)
     849             :     {
     850       12516 :       if (gcduii(y, gcoeff(U,1,1),  Q) != 1) continue;
     851        7301 :       if (gcduii(y, gcoeff(U,2,1), NQ) != 1) continue;
     852             :     }
     853       11375 :     Dy2 = mulii(D, sqru(y));
     854       11375 :     if (cmpii(Dy2, best) >= 0) break; /* we won't improve. STOP */
     855        5733 :     By = muliu(B,y), x0 = truedvmdii(negi(By), A2, &r);
     856        5733 :     if (cmpii(r, A) >= 0) { x0 = subiu(x0,1); r = subii(r, A2); }
     857             :     /* (2A x + By)^2 + Dy^2, minimal at x = x0. Assume A2 > 0 */
     858             :     /* r = 2A x0 + By */
     859        5733 :     q0 = addii(sqri(r), Dy2); /* minimal value for this y, at x0 */
     860        5733 :     if (cmpii(q0, best) >= 0) continue; /* we won't improve for this y */
     861        5726 :     q1 = shifti(mulii(A2, r), 1);
     862             : 
     863        5726 :     V0 = ZM_ZC_mul(U, mkcol2(x0, utoipos(y)));
     864       12299 :     for (i = 0;; i = nexti(i))
     865             :     {
     866       12299 :       pari_sp av2 = avma;
     867       12299 :       GEN x, N = qfmin_eval(q0, q1, AA4, i);
     868       12299 :       if (cmpii(N , best) >= 0) break;
     869       12257 :       x = addis(x0, i);
     870       12257 :       if (ugcd(umodiu(x, y), y) == 1)
     871             :       {
     872             :         GEN u, v;
     873       12215 :         V = ZC_add(V0, ZC_z_mul(gel(U,1), i)); /* [X, Y] */
     874       12215 :         u = gel(V,1);
     875       12215 :         v = gel(V,2);
     876       12215 :         if (is_pm1(gcdii(u, Q)) && is_pm1(gcdii(v, NQ)))
     877             :         {
     878        5684 :           *pu = u;
     879        5684 :           *pv = v;
     880        5684 :           best = N; break;
     881             :         }
     882             :       }
     883        6573 :       avma = av2;
     884        6573 :     }
     885        9121 :   }
     886             : #ifdef DEBUG
     887             :   {
     888             :     GEN oldu, oldv, F = qfi(a,b,c);
     889             :     best_point_old(Q, NQ, f, &oldu, &oldv);
     890             :     if (!equalii(oldu, *pu) || !equalii(oldv, *pv))
     891             :     {
     892             :       if (!equali1(gcdii(mulii(*pu, NQ), mulii(*pv, Q))))
     893             :         pari_err_BUG("best_point (gcd)");
     894             :       if (cmpii(qfb_eval(F, *pu,*pv), qfb_eval(F, oldu, oldv)) > 0)
     895             :       {
     896             :         pari_warn(warner, "%Ps,%Ps,%Ps, %Ps > %Ps",
     897             :                           Q,NQ,f, mkvec2(*pu,*pv), mkvec2(oldu,oldv));
     898             :         pari_err_BUG("best_point (too large)");
     899             :       }
     900             :     }
     901             :   }
     902             : #endif
     903             : }
     904             : 
     905             : static GEN
     906       23940 : best_lift(GEN N, GEN Q, GEN NQ, GEN f)
     907             : {
     908             :   GEN a,b,c,d,M;
     909       23940 :   best_point(Q, NQ, f, &c, &d);
     910       23940 :   (void)bezout(mulii(d, Q), mulii(NQ, c), &a, &b);
     911       23940 :   M = mkmat2( mkcol2(mulii(d, Q), mulii(negi(N), c)),
     912             :               mkcol2(b, mulii(a, Q)));
     913       23940 :   return qfb_mult(f, M);
     914             : }
     915             : 
     916             : static GEN
     917        2296 : lift_points(GEN N, GEN listQ, GEN f, GEN *pt, GEN *pQ)
     918             : {
     919        2296 :   pari_sp av = avma;
     920        2296 :   GEN yf = gen_0, tf = NULL, Qf = NULL;
     921        2296 :   long k, l = lg(listQ);
     922       26236 :   for (k = 1; k < l; ++k)
     923             :   {
     924       23940 :     GEN c = gel(listQ, k), Q = gel(c,1), NQ = gel(c,2);
     925       23940 :     GEN t = best_lift(N, Q, NQ, f), y = qimag2(t);
     926       23940 :     if (gcmp(y, yf) > 0) { yf = y; Qf = Q; tf = t; }
     927             :   }
     928        2296 :   gerepileall(av, 3, &tf, &Qf, &yf);
     929        2296 :   *pt = tf; *pQ = Qf; return yf;
     930             : }
     931             : 
     932             : /***************************/
     933             : /*         Twists          */
     934             : /***************************/
     935             : 
     936             : static GEN
     937          49 : ltwist1(GEN E, GEN d, long bitprec)
     938             : {
     939          49 :   pari_sp av = avma;
     940          49 :   GEN Ed = ellinit(elltwist(E, d), NULL, DEFAULTPREC);
     941          49 :   GEN z = ellL1_bitprec(Ed, 0, bitprec);
     942          49 :   obj_free(Ed); return gerepileuptoleaf(av, z);
     943             : }
     944             : 
     945             : /* Return O_re*c(E)/(4*O_vol*|E_t|^2) */
     946             : 
     947             : static GEN
     948          35 : heegner_indexmult(GEN om, long t, GEN tam, long prec)
     949             : {
     950          35 :   pari_sp av = avma;
     951          35 :   GEN Ovr = gabs(gimag(gel(om, 2)), prec); /* O_vol/O_re, t_REAL */
     952          35 :   return gerepileupto(av, divru(divir(tam, Ovr), 4*t*t));
     953             : }
     954             : 
     955             : 
     956             : /* omega(gcd(D, N)), given faN = factor(N) */
     957             : static long
     958          49 : omega_N_D(GEN faN, ulong D)
     959             : {
     960          49 :   GEN P = gel(faN, 1);
     961          49 :   long i, l = lg(P), w = 0;
     962         175 :   for (i = 1; i < l; i++)
     963         126 :     if (dvdui(D, gel(P,i))) w++;
     964          49 :   return w;
     965             : }
     966             : 
     967             : static GEN
     968          49 : heegner_indexmultD(GEN faN, GEN a, long D, GEN sqrtD)
     969             : {
     970          49 :   pari_sp av = avma;
     971             :   GEN c;
     972             :   long w;
     973          49 :   switch(D)
     974             :   {
     975           0 :     case -3: w = 9; break;
     976           0 :     case -4: w = 4; break;
     977          49 :     default: w = 1;
     978             :   }
     979          49 :   c = shifti(stoi(w), omega_N_D(faN,-D)); /* (w(D)/2)^2 * 2^omega(gcd(D,N)) */
     980          49 :   return gerepileupto(av, mulri(mulrr(a, sqrtD), c));
     981             : }
     982             : 
     983             : static GEN
     984         868 : heegner_try_point(GEN E, GEN lambdas, GEN ht, GEN z, long prec)
     985             : {
     986         868 :   long l = lg(lambdas);
     987             :   long i, eps;
     988         868 :   GEN P = greal(pointell(E, z, prec)), x = gel(P,1);
     989         868 :   GEN rh = subrr(ht, shiftr(ellheightoo(E, P, prec),1));
     990       11242 :   for (i = 1; i < l; ++i)
     991             :   {
     992       10409 :     GEN logd = shiftr(gsub(rh, gel(lambdas, i)), -1);
     993       10409 :     GEN d, approxd = gexp(logd, prec);
     994       10409 :     if (DEBUGLEVEL > 2)
     995           0 :       err_printf("Trying lambda number %ld, logd=%Ps, approxd=%Ps\n", i, logd, approxd);
     996       10409 :     d = grndtoi(approxd, &eps);
     997       10409 :     if (signe(d) > 0 && eps<-10)
     998             :     {
     999          56 :       GEN X, ylist, d2 = sqri(d), approxn = mulir(d2, x);
    1000          56 :       if (DEBUGLEVEL > 2) err_printf("approxn=%Ps  eps=%ld\n", approxn,eps);
    1001          56 :       X = gdiv(ground(approxn), d2);
    1002          56 :       ylist = ellordinate(E, X, prec);
    1003          56 :       if (lg(ylist) > 1)
    1004             :       {
    1005          49 :         GEN P = mkvec2(X, gel(ylist, 1));
    1006          49 :         GEN hp = ghell(E,P,prec);
    1007          49 :         if (cmprr(hp, shiftr(ht,1)) < 0 && cmprr(hp, shiftr(ht,-1)) > 0)
    1008          35 :           return P;
    1009          14 :         if (DEBUGLEVEL)
    1010           0 :           err_printf("found non-Heegner point %Ps\n", P);
    1011             :       }
    1012             :     }
    1013             :   }
    1014         833 :   return NULL;
    1015             : }
    1016             : 
    1017             : static GEN
    1018          35 : heegner_find_point(GEN e, GEN om, GEN ht, GEN z1, long k, long prec)
    1019             : {
    1020          35 :   GEN lambdas = lambdalist(e, prec);
    1021          35 :   pari_sp av = avma;
    1022             :   long m;
    1023          35 :   GEN Ore = gel(om, 1), Oim = gel(om, 2);
    1024          35 :   if (DEBUGLEVEL)
    1025           0 :     err_printf("%ld*%ld multipliers to test\n",k,lg(lambdas)-1);
    1026         504 :   for (m = 0; m < k; m++)
    1027             :   {
    1028         504 :     GEN P, z2 = divru(addrr(z1, mulsr(m, Ore)), k);
    1029         504 :     if (DEBUGLEVEL > 2)
    1030           0 :       err_printf("Trying multiplier %ld\n",m);
    1031         504 :     P = heegner_try_point(e, lambdas, ht, z2, prec);
    1032         504 :     if (P) return P;
    1033         476 :     if (signe(ell_get_disc(e)) > 0)
    1034             :     {
    1035         364 :       z2 = gadd(z2, gmul2n(Oim, -1));
    1036         364 :       P = heegner_try_point(e, lambdas, ht, z2, prec);
    1037         364 :       if (P) return P;
    1038             :     }
    1039         469 :     avma = av;
    1040             :   }
    1041           0 :   pari_err_BUG("ellheegner, point not found");
    1042             :   return NULL; /* LCOV_EXCL_LINE */
    1043             : }
    1044             : 
    1045             : /* N > 1, fa = factor(N), return factor(4*N) */
    1046             : static GEN
    1047          35 : fa_shift2(GEN fa)
    1048             : {
    1049          35 :   GEN P = gel(fa,1), E = gel(fa,2);
    1050          35 :   if (absequaliu(gcoeff(fa,1,1), 2))
    1051             :   {
    1052          21 :     E = shallowcopy(E);
    1053          21 :     gel(E,1) = addiu(gel(E,1), 2);
    1054             :   }
    1055             :   else
    1056             :   {
    1057          14 :     P = shallowconcat(gen_2, P);
    1058          14 :     E = shallowconcat(gen_2, E);
    1059             :   }
    1060          35 :   return mkmat2(P, E);
    1061             : }
    1062             : 
    1063             : /* P = prime divisors of N(E). Return the product of primes p in P, a_p != -1
    1064             :  * HACK: restrict to small primes since large ones won't divide our C-long
    1065             :  * discriminants */
    1066             : static GEN
    1067          35 : get_bad(GEN E, GEN P)
    1068             : {
    1069          35 :   long k, l = lg(P), ibad = 1;
    1070          35 :   GEN B = cgetg(l, t_VECSMALL);
    1071         126 :   for (k = 1; k < l; k++)
    1072             :   {
    1073          91 :     GEN p = gel(P,k);
    1074          91 :     long pp = itos_or_0(p);
    1075          91 :     if (!pp) break;
    1076          91 :     if (! equalim1(ellap(E,p))) B[ibad++] = pp;
    1077             :   }
    1078          35 :   setlg(B, ibad); return ibad == 1? NULL: zv_prod_Z(B);
    1079             : }
    1080             : 
    1081             : /* list of pairs [Q,N/Q], where Q | N and gcd(Q,N/Q) = 1 */
    1082             : static GEN
    1083          35 : find_div(GEN N, GEN faN)
    1084             : {
    1085          35 :   GEN listQ = divisors(faN);
    1086          35 :   long j, k, l = lg(listQ);
    1087             : 
    1088          35 :   gel(listQ, 1) = mkvec2(gen_1, N);
    1089        1582 :   for (j = k = 2; k < l; ++k)
    1090             :   {
    1091        1547 :     GEN Q = gel(listQ, k), NQ = diviiexact(N, Q);
    1092        1547 :     if (is_pm1(gcdii(Q,NQ))) gel(listQ, j++) = mkvec2(Q,NQ);
    1093             :   }
    1094          35 :   setlg(listQ, j); return listQ;
    1095             : }
    1096             : 
    1097             : static long
    1098        8113 : testDisc(GEN bad, long d)
    1099        8113 : { return !bad || ugcd(umodiu(bad, -d), -d) == 1; }
    1100             : /* bad = product of bad primes. Return the NDISC largest fundamental
    1101             :  * discriminants D < d such that (D,bad) = 1 and d is a square mod 4N */
    1102             : static GEN
    1103          35 : listDisc(GEN fa4N, GEN bad, long d)
    1104             : {
    1105          35 :   const long NDISC = 10;
    1106          35 :   GEN v = cgetg(NDISC+1, t_VECSMALL);
    1107          35 :   pari_sp av = avma;
    1108          35 :   long j = 1;
    1109             :   for(;;)
    1110             :   {
    1111        8113 :     d -= odd(d)? 1: 3;
    1112        8113 :     if (testDisc(bad,d) && unegisfundamental(-d) && Zn_issquare(stoi(d), fa4N))
    1113             :     {
    1114         350 :       v[j++] = d;
    1115         350 :       if (j > NDISC) break;
    1116             :     }
    1117        8078 :     avma = av;
    1118        8078 :   }
    1119          35 :   avma = av; return v;
    1120             : }
    1121             : /* L = vector of [q1,q2] or [q1,q2,q2']
    1122             :  * cd = (b^2 - D)/(4N) */
    1123             : static void
    1124      148932 : listfill(GEN N, GEN b, GEN c, GEN d, GEN L, long *s)
    1125             : {
    1126      148932 :   long k, l = lg(L);
    1127      148932 :   GEN add, frm2, a = mulii(d, N), V = mkqfi(a,b,c), frm = redimag(V);
    1128      568253 :   for (k = 1; k < l; ++k)
    1129             :   { /* Lk = [v,frm] or [v,frm,frm2] */
    1130      565957 :     GEN Lk = gel(L,k);
    1131             :     long i;
    1132     1447236 :     for (i = 2; i < lg(Lk); i++) /* 1 or 2 elements */
    1133     1027915 :       if (gequal(frm, gel(Lk,i)))
    1134             :       {
    1135      146636 :         GEN v = gel(Lk, 1);
    1136      146636 :         if (cmpii(a, gel(v,1)) < 0) gel(Lk,1) = V;
    1137      295568 :         return;
    1138             :       }
    1139             :   }
    1140        2296 :   frm2 = redimag( mkqfi(d, negi(b), mulii(c,N)) );
    1141        2296 :   add = gequal(frm, frm2)? mkvec2(V,frm): mkvec3(V,frm,frm2);
    1142        2296 :   vectrunc_append(L, add);
    1143        2296 :   *s += lg(add) - 2;
    1144             : }
    1145             : /* faN4 = factor(4*N) */
    1146             : static GEN
    1147         350 : listheegner(GEN N, GEN faN4, GEN listQ, GEN D)
    1148             : {
    1149         350 :   pari_sp av = avma;
    1150         350 :   const long kmin = 30;
    1151         350 :   long h = itos(gel(quadclassunit0(D, 0, NULL, DEFAULTPREC), 1));
    1152         350 :   GEN ymin, b = Zn_sqrt(D, faN4), L = vectrunc_init(h+1);
    1153         350 :   long l, k, s = 0;
    1154       10850 :   for (k = 0; k < kmin || s < h; k++)
    1155             :   {
    1156       10500 :     GEN bk = addii(b, mulsi(2*k, N));
    1157       10500 :     GEN C = diviiexact(shifti(subii(sqri(bk), D), -2), N);
    1158       10500 :     GEN div = divisors(C);
    1159       10500 :     long i, l = lg(div);
    1160      159432 :     for (i = 1; i < l; i++)
    1161             :     {
    1162      148932 :       GEN d = gel(div, i), c = gel(div, l-i); /* cd = C */
    1163      148932 :       listfill(N, bk, c, d, L, &s);
    1164             :     }
    1165             :   }
    1166         350 :   l = lg(L); ymin = NULL;
    1167        2646 :   for (k = 1; k < l; k++)
    1168             :   {
    1169        2296 :     GEN t, Q, Lk = gel(L,k), f = gel(Lk,1);
    1170        2296 :     GEN y = lift_points(N, listQ, f, &t, &Q);
    1171        2296 :     gel(L, k) = mkvec3(t, stoi(lg(Lk) - 2), Q);
    1172        2296 :     if (!ymin || gcmp(y, ymin) < 0) ymin = y;
    1173             :   }
    1174         350 :   if (DEBUGLEVEL > 1)
    1175           0 :     err_printf("Disc %Ps : N*ymin = %Pg\n", D,
    1176             :                            gmul(gsqrt(ymin, DEFAULTPREC),N));
    1177         350 :   return gerepilecopy(av, mkvec3(ymin, L, D));
    1178             : }
    1179             : 
    1180             : /* Q | N, P = prime divisors of N, R[i] = local epsilon-factor at P[i].
    1181             :  * Return \prod_{p | Q} R[i] */
    1182             : static long
    1183         133 : rootno(GEN Q, GEN P, GEN R)
    1184             : {
    1185         133 :   long s = 1, i, l = lg(P);
    1186         539 :   for (i = 1; i < l; i++)
    1187         406 :     if (dvdii(Q, gel(P,i))) s *= R[i];
    1188         133 :   return s;
    1189             : }
    1190             : 
    1191             : static void
    1192          35 : heegner_find_disc(GEN *points, GEN *coefs, long *pind, GEN E,
    1193             :                   GEN indmult, long prec)
    1194             : {
    1195          35 :   long d = 0;
    1196             :   GEN faN4, bad, N, faN, listQ, listR;
    1197             : 
    1198          35 :   ellQ_get_Nfa(E, &N, &faN);
    1199          35 :   faN4 = fa_shift2(faN);
    1200          35 :   listQ = find_div(N, faN);
    1201          35 :   bad = get_bad(E, gel(faN, 1));
    1202          35 :   listR = gel(obj_check(E, Q_ROOTNO), 2);
    1203             :   for(;;)
    1204             :   {
    1205          35 :     pari_sp av = avma;
    1206          35 :     GEN list, listD = listDisc(faN4, bad, d);
    1207          35 :     long k, l = lg(listD);
    1208          35 :     list = cgetg(l, t_VEC);
    1209         385 :     for (k = 1; k < l; ++k)
    1210         350 :       gel(list, k) = listheegner(N, faN4, listQ, stoi(listD[k]));
    1211          35 :     list = vecsort0(list, gen_1, 0);
    1212          49 :     for (k = l-1; k > 0; --k)
    1213             :     {
    1214          49 :       long bprec = 8;
    1215          49 :       GEN Lk = gel(list,k), D = gel(Lk,3);
    1216          49 :       GEN sqrtD = sqrtr_abs(itor(D, prec)); /* sqrt(|D|) */
    1217          49 :       GEN indmultD = heegner_indexmultD(faN, indmult, itos(D), sqrtD);
    1218             :       do
    1219             :       {
    1220             :         GEN mulf, indr;
    1221             :         pari_timer ti;
    1222          49 :         if (DEBUGLEVEL) timer_start(&ti);
    1223          49 :         mulf = ltwist1(E, D, bprec+expo(indmultD));
    1224          49 :         if (DEBUGLEVEL) timer_printf(&ti,"ellL1twist");
    1225          49 :         indr = mulrr(indmultD, mulf);
    1226          49 :         if (DEBUGLEVEL) err_printf("Disc = %Ps, Index^2 = %Ps\n", D, indr);
    1227          49 :         if (signe(indr)>0 && expo(indr) >= -1) /* indr >=.5 */
    1228             :         {
    1229             :           long e, i, l;
    1230          35 :           GEN pts, cfs, L, indi = grndtoi(sqrtr_abs(indr), &e);
    1231          35 :           if (e > expi(indi)-7)
    1232             :           {
    1233           0 :             bprec++;
    1234           0 :             pari_warn(warnprec, "ellL1",bprec);
    1235           0 :             continue;
    1236             :           }
    1237          35 :           *pind = itos(indi);
    1238          35 :           L = gel(Lk, 2); l = lg(L);
    1239          35 :           pts = cgetg(l, t_VEC);
    1240          35 :           cfs = cgetg(l, t_VECSMALL);
    1241         168 :           for (i = 1; i < l; ++i)
    1242             :           {
    1243         133 :             GEN P = gel(L,i), z = gel(P,2), Q = gel(P,3); /* [1 or 2, Q] */
    1244             :             long c;
    1245         133 :             gel(pts, i) = qfb_root(gel(P,1), sqrtD);
    1246         133 :             c = rootno(Q, gel(faN,1), listR);
    1247         133 :             if (!equali1(z)) c *= 2;
    1248         133 :             cfs[i] = c;
    1249             :           }
    1250          35 :           if (DEBUGLEVEL)
    1251           0 :             err_printf("N = %Ps, ymin*N = %Ps\n",N,
    1252           0 :                        gmul(gsqrt(gel(Lk, 1), prec),N));
    1253          70 :           *coefs = cfs; *points = pts; return;
    1254             :         }
    1255             :       } while(0);
    1256             :     }
    1257           0 :     d = listD[l-1]; avma = av;
    1258           0 :   }
    1259             : }
    1260             : 
    1261             : GEN
    1262         434 : ellanal_globalred_all(GEN e, GEN *cb, GEN *N, GEN *tam)
    1263             : {
    1264         434 :   GEN E = ellanal_globalred(e, cb), red = obj_check(E, Q_GLOBALRED);
    1265         434 :   *N = gel(red, 1);
    1266         434 :   *tam = gel(red,2);
    1267         434 :   if (signe(ell_get_disc(E))>0) *tam = shifti(*tam,1);
    1268         434 :   return E;
    1269             : }
    1270             : 
    1271             : GEN
    1272          49 : ellheegner(GEN E)
    1273             : {
    1274          49 :   pari_sp av = avma;
    1275             :   GEN z, P, ht, points, coefs, s, om, indmult;
    1276             :   long ind, lint, k, l, wtor, etor;
    1277          49 :   long bitprec = 16, prec = nbits2prec(bitprec)+1;
    1278             :   pari_timer ti;
    1279             :   GEN N, cb, tam, torsion;
    1280             : 
    1281          49 :   E = ellanal_globalred_all(E, &cb, &N, &tam);
    1282          49 :   if (ellrootno_global(E) == 1)
    1283           7 :     pari_err_DOMAIN("ellheegner", "(analytic rank)%2","=",gen_0,E);
    1284          42 :   torsion = elltors(E);
    1285          42 :   wtor = itos( gel(torsion,1) ); /* #E(Q)_tor */
    1286          42 :   etor = wtor > 1? itos(gmael(torsion, 2, 1)): 1; /* exponent of E(Q)_tor */
    1287             :   while (1)
    1288             :   {
    1289             :     GEN hnaive, l1;
    1290             :     long bitneeded;
    1291          77 :     if (DEBUGLEVEL) timer_start(&ti);
    1292          77 :     l1 = ellL1_bitprec(E, 1, bitprec);
    1293          77 :     if (DEBUGLEVEL) timer_printf(&ti,"ellL1");
    1294          77 :     if (expo(l1) < 1 - bitprec/2)
    1295           7 :       pari_err_DOMAIN("ellheegner", "analytic rank",">",gen_1,E);
    1296          70 :     om = ellR_omega(E,prec);
    1297          70 :     ht = divrr(mulru(l1, wtor * wtor), mulri(gel(om,1), tam));
    1298          70 :     if (DEBUGLEVEL) err_printf("Expected height=%Ps\n", ht);
    1299          70 :     hnaive = hnaive_max(E, ht);
    1300          70 :     if (DEBUGLEVEL) err_printf("Naive height <= %Ps\n", hnaive);
    1301          70 :     bitneeded = itos(gceil(divrr(hnaive, mplog2(prec)))) + 10;
    1302          70 :     if (DEBUGLEVEL) err_printf("precision = %ld\n", bitneeded);
    1303          70 :     if (bitprec>=bitneeded) break;
    1304          35 :     bitprec = bitneeded;
    1305          35 :     prec = nbits2prec(bitprec)+1;
    1306          35 :   }
    1307          35 :   indmult = heegner_indexmult(om, wtor, tam, prec);
    1308          35 :   heegner_find_disc(&points, &coefs, &ind, E, indmult, prec);
    1309          35 :   if (DEBUGLEVEL) timer_start(&ti);
    1310          35 :   s = heegner_psi(E, N, points, bitprec);
    1311          35 :   if (DEBUGLEVEL) timer_printf(&ti,"heegner_psi");
    1312          35 :   l = lg(points);
    1313          35 :   z = mulsr(coefs[1], gel(s, 1));
    1314          35 :   for (k = 2; k < l; ++k) z = addrr(z, mulsr(coefs[k], gel(s, k)));
    1315          35 :   if (DEBUGLEVEL) err_printf("z=%Ps\n", z);
    1316          35 :   z = gsub(z, gmul(gel(om,1), ground(gdiv(z, gel(om,1)))));
    1317          35 :   lint = wtor > 1 ? cgcd(ind, etor): 1;
    1318          35 :   P = heegner_find_point(E, om, ht, gmulsg(2*lint, z), lint*2*ind, prec);
    1319          35 :   if (DEBUGLEVEL) timer_printf(&ti,"heegner_find_point");
    1320          35 :   if (cb) P = ellchangepointinv(P, cb);
    1321          35 :   return gerepilecopy(av, P);
    1322             : }
    1323             : 
    1324             : /* Modular degree */
    1325             : 
    1326             : /* Modular degree of elliptic curve e over Q, assuming Manin constant = 1
    1327             :    (otherwise multiply by square of Manin constant). */
    1328             : GEN
    1329          56 : ellmoddegree(GEN e, long bitprec)
    1330             : {
    1331          56 :   pari_sp ltop = avma;
    1332          56 :   long prec = nbits2prec(bitprec);
    1333          56 :   GEN E = ellminimalmodel(e, NULL);
    1334          56 :   GEN nor = lfunellmfpeters(E, bitprec);
    1335          56 :   GEN deg = gdiv(gmul(nor, sqrr(Pi2n(1, prec))), member_area(E));
    1336          56 :   GEN degr = bestappr(deg, int2n(bitprec>>1));
    1337          56 :   long err = gexpo(gsub(gen_1, gdiv(deg,degr)));
    1338          56 :   obj_free(E);
    1339          56 :   return gerepilecopy(ltop, mkvec2(degr, stoi(err)));
    1340             : }

Generated by: LCOV version 1.11