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

Generated by: LCOV version 1.16