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 - base3.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.16.2 lcov report (development 29375-be3334fb81) Lines: 2048 2179 94.0 %
Date: 2024-05-26 08:08:42 Functions: 228 241 94.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  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             : /*                       BASIC NF OPERATIONS                       */
      18             : /*                                                                 */
      19             : /*******************************************************************/
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : 
      23             : #define DEBUGLEVEL DEBUGLEVEL_nf
      24             : 
      25             : /*******************************************************************/
      26             : /*                                                                 */
      27             : /*                OPERATIONS OVER NUMBER FIELD ELEMENTS.           */
      28             : /*     represented as column vectors over the integral basis       */
      29             : /*                                                                 */
      30             : /*******************************************************************/
      31             : static GEN
      32    39164927 : get_tab(GEN nf, long *N)
      33             : {
      34    39164927 :   GEN tab = (typ(nf) == t_MAT)? nf: gel(nf,9);
      35    39164927 :   *N = nbrows(tab); return tab;
      36             : }
      37             : 
      38             : /* x != 0, y t_INT. Return x * y (not memory clean if x = 1) */
      39             : static GEN
      40  1074320572 : _mulii(GEN x, GEN y) {
      41  1735210123 :   return is_pm1(x)? (signe(x) < 0)? negi(y): y
      42  1735045798 :                   : mulii(x, y);
      43             : }
      44             : 
      45             : GEN
      46       21902 : tablemul_ei_ej(GEN M, long i, long j)
      47             : {
      48             :   long N;
      49       21902 :   GEN tab = get_tab(M, &N);
      50       21902 :   tab += (i-1)*N; return gel(tab,j);
      51             : }
      52             : 
      53             : /* Outputs x.ei, where ei is the i-th elt of the algebra basis.
      54             :  * x an RgV of correct length and arbitrary content (polynomials, scalars...).
      55             :  * M is the multiplication table ei ej = sum_k M_k^(i,j) ek */
      56             : GEN
      57       11473 : tablemul_ei(GEN M, GEN x, long i)
      58             : {
      59             :   long j, k, N;
      60             :   GEN v, tab;
      61             : 
      62       11473 :   if (i==1) return gcopy(x);
      63       11473 :   tab = get_tab(M, &N);
      64       11473 :   if (typ(x) != t_COL) { v = zerocol(N); gel(v,i) = gcopy(x); return v; }
      65       11473 :   tab += (i-1)*N; v = cgetg(N+1,t_COL);
      66             :   /* wi . x = [ sum_j tab[k,j] x[j] ]_k */
      67       78155 :   for (k=1; k<=N; k++)
      68             :   {
      69       66682 :     pari_sp av = avma;
      70       66682 :     GEN s = gen_0;
      71      472206 :     for (j=1; j<=N; j++)
      72             :     {
      73      405524 :       GEN c = gcoeff(tab,k,j);
      74      405524 :       if (!gequal0(c)) s = gadd(s, gmul(c, gel(x,j)));
      75             :     }
      76       66682 :     gel(v,k) = gerepileupto(av,s);
      77             :   }
      78       11473 :   return v;
      79             : }
      80             : /* as tablemul_ei, assume x a ZV of correct length */
      81             : GEN
      82    23726126 : zk_ei_mul(GEN nf, GEN x, long i)
      83             : {
      84             :   long j, k, N;
      85             :   GEN v, tab;
      86             : 
      87    23726126 :   if (i==1) return ZC_copy(x);
      88    23726112 :   tab = get_tab(nf, &N); tab += (i-1)*N;
      89    23726075 :   v = cgetg(N+1,t_COL);
      90   168870082 :   for (k=1; k<=N; k++)
      91             :   {
      92   145147963 :     pari_sp av = avma;
      93   145147963 :     GEN s = gen_0;
      94  2138416978 :     for (j=1; j<=N; j++)
      95             :     {
      96  1993438251 :       GEN c = gcoeff(tab,k,j);
      97  1993438251 :       if (signe(c)) s = addii(s, _mulii(c, gel(x,j)));
      98             :     }
      99   144978727 :     gel(v,k) = gerepileuptoint(av, s);
     100             :   }
     101    23722119 :   return v;
     102             : }
     103             : 
     104             : /* table of multiplication by wi in R[w1,..., wN] */
     105             : GEN
     106       39081 : ei_multable(GEN TAB, long i)
     107             : {
     108             :   long k,N;
     109       39081 :   GEN m, tab = get_tab(TAB, &N);
     110       39081 :   tab += (i-1)*N;
     111       39081 :   m = cgetg(N+1,t_MAT);
     112      153293 :   for (k=1; k<=N; k++) gel(m,k) = gel(tab,k);
     113       39081 :   return m;
     114             : }
     115             : 
     116             : GEN
     117    10654523 : zk_multable(GEN nf, GEN x)
     118             : {
     119    10654523 :   long i, l = lg(x);
     120    10654523 :   GEN mul = cgetg(l,t_MAT);
     121    10654386 :   gel(mul,1) = x; /* assume w_1 = 1 */
     122    34018972 :   for (i=2; i<l; i++) gel(mul,i) = zk_ei_mul(nf,x,i);
     123    10650689 :   return mul;
     124             : }
     125             : GEN
     126        2604 : multable(GEN M, GEN x)
     127             : {
     128             :   long i, N;
     129             :   GEN mul;
     130        2604 :   if (typ(x) == t_MAT) return x;
     131           0 :   M = get_tab(M, &N);
     132           0 :   if (typ(x) != t_COL) return scalarmat(x, N);
     133           0 :   mul = cgetg(N+1,t_MAT);
     134           0 :   gel(mul,1) = x; /* assume w_1 = 1 */
     135           0 :   for (i=2; i<=N; i++) gel(mul,i) = tablemul_ei(M,x,i);
     136           0 :   return mul;
     137             : }
     138             : 
     139             : /* x integral in nf; table of multiplication by x in ZK = Z[w1,..., wN].
     140             :  * Return a t_INT if x is scalar, and a ZM otherwise */
     141             : GEN
     142     4841948 : zk_scalar_or_multable(GEN nf, GEN x)
     143             : {
     144     4841948 :   long tx = typ(x);
     145     4841948 :   if (tx == t_MAT || tx == t_INT) return x;
     146     4680741 :   x = nf_to_scalar_or_basis(nf, x);
     147     4680613 :   return (typ(x) == t_COL)? zk_multable(nf, x): x;
     148             : }
     149             : 
     150             : GEN
     151       21296 : nftrace(GEN nf, GEN x)
     152             : {
     153       21296 :   pari_sp av = avma;
     154       21296 :   nf = checknf(nf);
     155       21297 :   x = nf_to_scalar_or_basis(nf, x);
     156       21278 :   x = (typ(x) == t_COL)? RgV_dotproduct(x, gel(nf_get_Tr(nf),1))
     157       21299 :                        : gmulgu(x, nf_get_degree(nf));
     158       21300 :   return gerepileupto(av, x);
     159             : }
     160             : GEN
     161        1015 : rnfelttrace(GEN rnf, GEN x)
     162             : {
     163        1015 :   pari_sp av = avma;
     164        1015 :   checkrnf(rnf);
     165             :   /* avoid rnfabstorel special t_POL case misinterpretation */
     166        1008 :   if (typ(x) == t_POL && varn(x) == rnf_get_varn(rnf))
     167          63 :     x = gmodulo(x, rnf_get_pol(rnf));
     168        1008 :   x = rnfeltabstorel(rnf, x);
     169         693 :   x = (typ(x) == t_POLMOD)? rnfeltdown(rnf, gtrace(x))
     170         798 :                           : gmulgu(x, rnf_get_degree(rnf));
     171         798 :   return gerepileupto(av, x);
     172             : }
     173             : 
     174             : static GEN
     175          35 : famatQ_to_famatZ(GEN fa)
     176             : {
     177          35 :   GEN E, F, Q, P = gel(fa,1);
     178          35 :   long i, j, l = lg(P);
     179          35 :   if (l == 1 || RgV_is_ZV(P)) return fa;
     180           7 :   Q = cgetg(2*l, t_COL);
     181           7 :   F = cgetg(2*l, t_COL); E = gel(fa, 2);
     182          35 :   for (i = j = 1; i < l; i++)
     183             :   {
     184          28 :     GEN p = gel(P,i);
     185          28 :     if (typ(p) == t_INT)
     186          14 :     { gel(Q, j) = p; gel(F, j) = gel(E, i); j++; }
     187             :     else
     188             :     {
     189          14 :       gel(Q, j) = gel(p,1); gel(F, j) = gel(E, i); j++;
     190          14 :       gel(Q, j) = gel(p,2); gel(F, j) = negi(gel(E, i)); j++;
     191             :     }
     192             :   }
     193           7 :   setlg(Q, j); setlg(F, j); return mkmat2(Q, F);
     194             : }
     195             : static GEN
     196          35 : famat_cba(GEN fa)
     197             : {
     198          35 :   GEN Q, F, P = gel(fa, 1), E = gel(fa, 2);
     199          35 :   long i, j, lQ, l = lg(P);
     200          35 :   if (l == 1) return fa;
     201          28 :   Q = ZV_cba(P); lQ = lg(Q);
     202          28 :   F = cgetg(lQ, t_COL);
     203          77 :   for (j = 1; j < lQ; j++)
     204             :   {
     205          49 :     GEN v = gen_0, q = gel(Q,j);
     206          49 :     if (!equali1(q))
     207         203 :       for (i = 1; i < l; i++)
     208             :       {
     209         161 :         long e = Z_pval(gel(P,i), q);
     210         161 :         if (e) v = addii(v, muliu(gel(E,i), e));
     211             :       }
     212          49 :     gel(F, j) = v;
     213             :   }
     214          28 :   return mkmat2(Q, F);
     215             : }
     216             : static long
     217          35 : famat_sign(GEN fa)
     218             : {
     219          35 :   GEN P = gel(fa,1), E = gel(fa,2);
     220          35 :   long i, l = lg(P), s = 1;
     221         126 :   for (i = 1; i < l; i++)
     222          91 :     if (signe(gel(P,i)) < 0 && mpodd(gel(E,i))) s = -s;
     223          35 :   return s;
     224             : }
     225             : static GEN
     226          35 : famat_abs(GEN fa)
     227             : {
     228          35 :   GEN Q, P = gel(fa,1);
     229             :   long i, l;
     230          35 :   Q = cgetg_copy(P, &l);
     231         126 :   for (i = 1; i < l; i++) gel(Q,i) = absi_shallow(gel(P,i));
     232          35 :   return mkmat2(Q, gel(fa,2));
     233             : }
     234             : 
     235             : /* assume nf is a genuine nf, fa a famat */
     236             : static GEN
     237          35 : famat_norm(GEN nf, GEN fa)
     238             : {
     239          35 :   pari_sp av = avma;
     240          35 :   GEN G, g = gel(fa,1);
     241             :   long i, l, s;
     242             : 
     243          35 :   G = cgetg_copy(g, &l);
     244         112 :   for (i = 1; i < l; i++) gel(G,i) = nfnorm(nf, gel(g,i));
     245          35 :   fa = mkmat2(G, gel(fa,2));
     246          35 :   fa = famatQ_to_famatZ(fa);
     247          35 :   s = famat_sign(fa);
     248          35 :   fa = famat_reduce(famat_abs(fa));
     249          35 :   fa = famat_cba(fa);
     250          35 :   g = factorback(fa);
     251          35 :   return gerepileupto(av, s < 0? gneg(g): g);
     252             : }
     253             : GEN
     254      222756 : nfnorm(GEN nf, GEN x)
     255             : {
     256      222756 :   pari_sp av = avma;
     257             :   GEN c, den;
     258             :   long n;
     259      222756 :   nf = checknf(nf);
     260      222756 :   n = nf_get_degree(nf);
     261      222756 :   if (typ(x) == t_MAT) return famat_norm(nf, x);
     262      222721 :   x = nf_to_scalar_or_basis(nf, x);
     263      222720 :   if (typ(x)!=t_COL)
     264      126819 :     return gerepileupto(av, gpowgs(x, n));
     265       95901 :   x = nf_to_scalar_or_alg(nf, Q_primitive_part(x, &c));
     266       95901 :   x = Q_remove_denom(x, &den);
     267       95902 :   x = ZX_resultant_all(nf_get_pol(nf), x, den, 0);
     268       95902 :   return gerepileupto(av, c ? gmul(x, gpowgs(c, n)): x);
     269             : }
     270             : 
     271             : static GEN
     272         119 : to_RgX(GEN P, long vx)
     273             : {
     274         119 :   return varn(P) == vx ? P: scalarpol_shallow(P, vx);
     275             : }
     276             : 
     277             : GEN
     278         462 : rnfeltnorm(GEN rnf, GEN x)
     279             : {
     280         462 :   pari_sp av = avma;
     281             :   GEN nf, pol;
     282             :   long v;
     283         462 :   checkrnf(rnf);
     284         455 :   v = rnf_get_varn(rnf);
     285             :   /* avoid rnfabstorel special t_POL case misinterpretation */
     286         455 :   if (typ(x) == t_POL && varn(x) == v) x = gmodulo(x, rnf_get_pol(rnf));
     287         455 :   x = liftpol_shallow(rnfeltabstorel(rnf, x));
     288         245 :   nf = rnf_get_nf(rnf); pol = rnf_get_pol(rnf);
     289         490 :   x = (typ(x) == t_POL)
     290         119 :     ? rnfeltdown(rnf, nfX_resultant(nf,pol,to_RgX(x,v)))
     291         245 :     : gpowgs(x, rnf_get_degree(rnf));
     292         245 :   return gerepileupto(av, x);
     293             : }
     294             : 
     295             : /* x + y in nf */
     296             : GEN
     297    23432667 : nfadd(GEN nf, GEN x, GEN y)
     298             : {
     299    23432667 :   pari_sp av = avma;
     300             :   GEN z;
     301             : 
     302    23432667 :   nf = checknf(nf);
     303    23432667 :   x = nf_to_scalar_or_basis(nf, x);
     304    23432667 :   y = nf_to_scalar_or_basis(nf, y);
     305    23432667 :   if (typ(x) != t_COL)
     306    17669273 :   { z = (typ(y) == t_COL)? RgC_Rg_add(y, x): gadd(x,y); }
     307             :   else
     308     5763394 :   { z = (typ(y) == t_COL)? RgC_add(x, y): RgC_Rg_add(x, y); }
     309    23432667 :   return gerepileupto(av, z);
     310             : }
     311             : /* x - y in nf */
     312             : GEN
     313     1810800 : nfsub(GEN nf, GEN x, GEN y)
     314             : {
     315     1810800 :   pari_sp av = avma;
     316             :   GEN z;
     317             : 
     318     1810800 :   nf = checknf(nf);
     319     1810800 :   x = nf_to_scalar_or_basis(nf, x);
     320     1810800 :   y = nf_to_scalar_or_basis(nf, y);
     321     1810800 :   if (typ(x) != t_COL)
     322     1279089 :   { z = (typ(y) == t_COL)? Rg_RgC_sub(x,y): gsub(x,y); }
     323             :   else
     324      531711 :   { z = (typ(y) == t_COL)? RgC_sub(x,y): RgC_Rg_sub(x,y); }
     325     1810800 :   return gerepileupto(av, z);
     326             : }
     327             : 
     328             : /* product of ZC x,y in (true) nf; ( sum_i x_i sum_j y_j m^{i,j}_k )_k */
     329             : static GEN
     330     8483360 : nfmuli_ZC(GEN nf, GEN x, GEN y)
     331             : {
     332             :   long i, j, k, N;
     333     8483360 :   GEN TAB = get_tab(nf, &N), v = cgetg(N+1,t_COL);
     334             : 
     335    41618930 :   for (k = 1; k <= N; k++)
     336             :   {
     337    33135616 :     pari_sp av = avma;
     338    33135616 :     GEN s, TABi = TAB;
     339    33135616 :     if (k == 1)
     340     8483351 :       s = mulii(gel(x,1),gel(y,1));
     341             :     else
     342    24652055 :       s = addii(mulii(gel(x,1),gel(y,k)),
     343    24652265 :                 mulii(gel(x,k),gel(y,1)));
     344   217191635 :     for (i=2; i<=N; i++)
     345             :     {
     346   184060637 :       GEN t, xi = gel(x,i);
     347   184060637 :       TABi += N;
     348   184060637 :       if (!signe(xi)) continue;
     349             : 
     350    93095619 :       t = NULL;
     351  1061121446 :       for (j=2; j<=N; j++)
     352             :       {
     353   968027059 :         GEN p1, c = gcoeff(TABi, k, j); /* m^{i,j}_k */
     354   968027059 :         if (!signe(c)) continue;
     355   280661613 :         p1 = _mulii(c, gel(y,j));
     356   280668067 :         t = t? addii(t, p1): p1;
     357             :       }
     358    93094387 :       if (t) s = addii(s, mulii(xi, t));
     359             :     }
     360    33130998 :     gel(v,k) = gerepileuptoint(av,s);
     361             :   }
     362     8483314 :   return v;
     363             : }
     364             : static int
     365    74497200 : is_famat(GEN x) { return typ(x) == t_MAT && lg(x) == 3; }
     366             : /* product of x and y in nf */
     367             : GEN
     368    36295199 : nfmul(GEN nf, GEN x, GEN y)
     369             : {
     370             :   GEN z;
     371    36295199 :   pari_sp av = avma;
     372             : 
     373    36295199 :   if (x == y) return nfsqr(nf,x);
     374             : 
     375    32204207 :   nf = checknf(nf);
     376    32204209 :   if (is_famat(x) || is_famat(y)) return famat_mul(x, y);
     377    32203900 :   x = nf_to_scalar_or_basis(nf, x);
     378    32203899 :   y = nf_to_scalar_or_basis(nf, y);
     379    32203896 :   if (typ(x) != t_COL)
     380             :   {
     381    21793588 :     if (isintzero(x)) return gen_0;
     382    15735045 :     z = (typ(y) == t_COL)? RgC_Rg_mul(y, x): gmul(x,y); }
     383             :   else
     384             :   {
     385    10410308 :     if (typ(y) != t_COL)
     386             :     {
     387     4538549 :       if (isintzero(y)) return gen_0;
     388     1608981 :       z = RgC_Rg_mul(x, y);
     389             :     }
     390             :     else
     391             :     {
     392             :       GEN dx, dy;
     393     5871759 :       x = Q_remove_denom(x, &dx);
     394     5871759 :       y = Q_remove_denom(y, &dy);
     395     5871759 :       z = nfmuli_ZC(nf,x,y);
     396     5871755 :       dx = mul_denom(dx,dy);
     397     5871756 :       if (dx) z = ZC_Z_div(z, dx);
     398             :     }
     399             :   }
     400    23215778 :   return gerepileupto(av, z);
     401             : }
     402             : /* square of ZC x in nf */
     403             : static GEN
     404     6885651 : nfsqri_ZC(GEN nf, GEN x)
     405             : {
     406             :   long i, j, k, N;
     407     6885651 :   GEN TAB = get_tab(nf, &N), v = cgetg(N+1,t_COL);
     408             : 
     409    38243202 :   for (k = 1; k <= N; k++)
     410             :   {
     411    31357640 :     pari_sp av = avma;
     412    31357640 :     GEN s, TABi = TAB;
     413    31357640 :     if (k == 1)
     414     6885767 :       s = sqri(gel(x,1));
     415             :     else
     416    24471873 :       s = shifti(mulii(gel(x,1),gel(x,k)), 1);
     417   252471617 :     for (i=2; i<=N; i++)
     418             :     {
     419   221132225 :       GEN p1, c, t, xi = gel(x,i);
     420   221132225 :       TABi += N;
     421   221132225 :       if (!signe(xi)) continue;
     422             : 
     423    79311609 :       c = gcoeff(TABi, k, i);
     424    79311609 :       t = signe(c)? _mulii(c,xi): NULL;
     425   674586385 :       for (j=i+1; j<=N; j++)
     426             :       {
     427   595274066 :         c = gcoeff(TABi, k, j);
     428   595274066 :         if (!signe(c)) continue;
     429   231754620 :         p1 = _mulii(c, shifti(gel(x,j),1));
     430   231760873 :         t = t? addii(t, p1): p1;
     431             :       }
     432    79312319 :       if (t) s = addii(s, mulii(xi, t));
     433             :     }
     434    31339392 :     gel(v,k) = gerepileuptoint(av,s);
     435             :   }
     436     6885562 :   return v;
     437             : }
     438             : /* square of x in nf */
     439             : GEN
     440     8906059 : nfsqr(GEN nf, GEN x)
     441             : {
     442     8906059 :   pari_sp av = avma;
     443             :   GEN z;
     444             : 
     445     8906059 :   nf = checknf(nf);
     446     8906060 :   if (is_famat(x)) return famat_sqr(x);
     447     8906065 :   x = nf_to_scalar_or_basis(nf, x);
     448     8906065 :   if (typ(x) != t_COL) z = gsqr(x);
     449             :   else
     450             :   {
     451             :     GEN dx;
     452     2628528 :     x = Q_remove_denom(x, &dx);
     453     2628528 :     z = nfsqri_ZC(nf,x);
     454     2628527 :     if (dx) z = RgC_Rg_div(z, sqri(dx));
     455             :   }
     456     8906064 :   return gerepileupto(av, z);
     457             : }
     458             : 
     459             : /* x a ZC, v a t_COL of ZC/Z */
     460             : GEN
     461      205386 : zkC_multable_mul(GEN v, GEN x)
     462             : {
     463      205386 :   long i, l = lg(v);
     464      205386 :   GEN y = cgetg(l, t_COL);
     465      799264 :   for (i = 1; i < l; i++)
     466             :   {
     467      593878 :     GEN c = gel(v,i);
     468      593878 :     if (typ(c)!=t_COL) {
     469           0 :       if (!isintzero(c)) c = ZC_Z_mul(gel(x,1), c);
     470             :     } else {
     471      593878 :       c = ZM_ZC_mul(x,c);
     472      593878 :       if (ZV_isscalar(c)) c = gel(c,1);
     473             :     }
     474      593878 :     gel(y,i) = c;
     475             :   }
     476      205386 :   return y;
     477             : }
     478             : 
     479             : GEN
     480       56891 : nfC_multable_mul(GEN v, GEN x)
     481             : {
     482       56891 :   long i, l = lg(v);
     483       56891 :   GEN y = cgetg(l, t_COL);
     484      384047 :   for (i = 1; i < l; i++)
     485             :   {
     486      327156 :     GEN c = gel(v,i);
     487      327156 :     if (typ(c)!=t_COL) {
     488      272602 :       if (!isintzero(c)) c = RgC_Rg_mul(gel(x,1), c);
     489             :     } else {
     490       54554 :       c = RgM_RgC_mul(x,c);
     491       54554 :       if (QV_isscalar(c)) c = gel(c,1);
     492             :     }
     493      327156 :     gel(y,i) = c;
     494             :   }
     495       56891 :   return y;
     496             : }
     497             : 
     498             : GEN
     499      197964 : nfC_nf_mul(GEN nf, GEN v, GEN x)
     500             : {
     501             :   long tx;
     502             :   GEN y;
     503             : 
     504      197964 :   x = nf_to_scalar_or_basis(nf, x);
     505      197964 :   tx = typ(x);
     506      197964 :   if (tx != t_COL)
     507             :   {
     508             :     long l, i;
     509      149577 :     if (tx == t_INT)
     510             :     {
     511      140470 :       long s = signe(x);
     512      140470 :       if (!s) return zerocol(lg(v)-1);
     513      133108 :       if (is_pm1(x)) return s > 0? leafcopy(v): RgC_neg(v);
     514             :     }
     515       48503 :     l = lg(v); y = cgetg(l, t_COL);
     516      347452 :     for (i=1; i < l; i++)
     517             :     {
     518      298949 :       GEN c = gel(v,i);
     519      298949 :       if (typ(c) != t_COL) c = gmul(c, x); else c = RgC_Rg_mul(c, x);
     520      298949 :       gel(y,i) = c;
     521             :     }
     522       48503 :     return y;
     523             :   }
     524             :   else
     525             :   {
     526             :     GEN dx;
     527       48387 :     x = zk_multable(nf, Q_remove_denom(x,&dx));
     528       48387 :     y = nfC_multable_mul(v, x);
     529       48387 :     return dx? RgC_Rg_div(y, dx): y;
     530             :   }
     531             : }
     532             : static GEN
     533       10919 : mulbytab(GEN M, GEN c)
     534       10919 : { return typ(c) == t_COL? RgM_RgC_mul(M,c): RgC_Rg_mul(gel(M,1), c); }
     535             : GEN
     536        2604 : tablemulvec(GEN M, GEN x, GEN v)
     537             : {
     538             :   long l, i;
     539             :   GEN y;
     540             : 
     541        2604 :   if (typ(x) == t_COL && RgV_isscalar(x))
     542             :   {
     543           0 :     x = gel(x,1);
     544           0 :     return typ(v) == t_POL? RgX_Rg_mul(v,x): RgV_Rg_mul(v,x);
     545             :   }
     546        2604 :   x = multable(M, x); /* multiplication table by x */
     547        2604 :   y = cgetg_copy(v, &l);
     548        2604 :   if (typ(v) == t_POL)
     549             :   {
     550        2604 :     y[1] = v[1];
     551       13523 :     for (i=2; i < l; i++) gel(y,i) = mulbytab(x, gel(v,i));
     552        2604 :     y = normalizepol(y);
     553             :   }
     554             :   else
     555             :   {
     556           0 :     for (i=1; i < l; i++) gel(y,i) = mulbytab(x, gel(v,i));
     557             :   }
     558        2604 :   return y;
     559             : }
     560             : 
     561             : GEN
     562     1260249 : zkmultable_capZ(GEN mx) { return Q_denom(zkmultable_inv(mx)); }
     563             : GEN
     564     1575479 : zkmultable_inv(GEN mx) { return ZM_gauss(mx, col_ei(lg(mx)-1,1)); }
     565             : /* nf a true nf, x a ZC */
     566             : GEN
     567      315234 : zk_inv(GEN nf, GEN x) { return zkmultable_inv(zk_multable(nf,x)); }
     568             : 
     569             : /* inverse of x in nf */
     570             : GEN
     571      239169 : nfinv(GEN nf, GEN x)
     572             : {
     573      239169 :   pari_sp av = avma;
     574             :   GEN z;
     575             : 
     576      239169 :   nf = checknf(nf);
     577      239169 :   if (is_famat(x)) return famat_inv(x);
     578      239169 :   x = nf_to_scalar_or_basis(nf, x);
     579      239169 :   if (typ(x) == t_COL)
     580             :   {
     581             :     GEN d;
     582      190536 :     x = Q_remove_denom(x, &d);
     583      190536 :     z = zk_inv(nf, x);
     584      190536 :     if (d) z = RgC_Rg_mul(z, d);
     585             :   }
     586             :   else
     587       48633 :     z = ginv(x);
     588      239169 :   return gerepileupto(av, z);
     589             : }
     590             : 
     591             : /* quotient of x and y in nf */
     592             : GEN
     593       37019 : nfdiv(GEN nf, GEN x, GEN y)
     594             : {
     595       37019 :   pari_sp av = avma;
     596             :   GEN z;
     597             : 
     598       37019 :   nf = checknf(nf);
     599       37019 :   if (is_famat(x) || is_famat(y)) return famat_div(x,y);
     600       36928 :   y = nf_to_scalar_or_basis(nf, y);
     601       36928 :   if (typ(y) != t_COL)
     602             :   {
     603       22099 :     x = nf_to_scalar_or_basis(nf, x);
     604       22099 :     z = (typ(x) == t_COL)? RgC_Rg_div(x, y): gdiv(x,y);
     605             :   }
     606             :   else
     607             :   {
     608             :     GEN d;
     609       14829 :     y = Q_remove_denom(y, &d);
     610       14829 :     z = nfmul(nf, x, zk_inv(nf,y));
     611       14829 :     if (d) z = typ(z) == t_COL? RgC_Rg_mul(z, d): gmul(z, d);
     612             :   }
     613       36928 :   return gerepileupto(av, z);
     614             : }
     615             : 
     616             : /* product of INTEGERS (t_INT or ZC) x and y in (true) nf */
     617             : GEN
     618     3968632 : nfmuli(GEN nf, GEN x, GEN y)
     619             : {
     620     3968632 :   if (typ(x) == t_INT) return (typ(y) == t_COL)? ZC_Z_mul(y, x): mulii(x,y);
     621     2842295 :   if (typ(y) == t_INT) return ZC_Z_mul(x, y);
     622     2611578 :   return nfmuli_ZC(nf, x, y);
     623             : }
     624             : GEN
     625     4257160 : nfsqri(GEN nf, GEN x)
     626     4257160 : { return (typ(x) == t_INT)? sqri(x): nfsqri_ZC(nf, x); }
     627             : 
     628             : /* both x and y are RgV */
     629             : GEN
     630           0 : tablemul(GEN TAB, GEN x, GEN y)
     631             : {
     632             :   long i, j, k, N;
     633             :   GEN s, v;
     634           0 :   if (typ(x) != t_COL) return gmul(x, y);
     635           0 :   if (typ(y) != t_COL) return gmul(y, x);
     636           0 :   N = lg(x)-1;
     637           0 :   v = cgetg(N+1,t_COL);
     638           0 :   for (k=1; k<=N; k++)
     639             :   {
     640           0 :     pari_sp av = avma;
     641           0 :     GEN TABi = TAB;
     642           0 :     if (k == 1)
     643           0 :       s = gmul(gel(x,1),gel(y,1));
     644             :     else
     645           0 :       s = gadd(gmul(gel(x,1),gel(y,k)),
     646           0 :                gmul(gel(x,k),gel(y,1)));
     647           0 :     for (i=2; i<=N; i++)
     648             :     {
     649           0 :       GEN t, xi = gel(x,i);
     650           0 :       TABi += N;
     651           0 :       if (gequal0(xi)) continue;
     652             : 
     653           0 :       t = NULL;
     654           0 :       for (j=2; j<=N; j++)
     655             :       {
     656           0 :         GEN p1, c = gcoeff(TABi, k, j); /* m^{i,j}_k */
     657           0 :         if (gequal0(c)) continue;
     658           0 :         p1 = gmul(c, gel(y,j));
     659           0 :         t = t? gadd(t, p1): p1;
     660             :       }
     661           0 :       if (t) s = gadd(s, gmul(xi, t));
     662             :     }
     663           0 :     gel(v,k) = gerepileupto(av,s);
     664             :   }
     665           0 :   return v;
     666             : }
     667             : GEN
     668       49370 : tablesqr(GEN TAB, GEN x)
     669             : {
     670             :   long i, j, k, N;
     671             :   GEN s, v;
     672             : 
     673       49370 :   if (typ(x) != t_COL) return gsqr(x);
     674       49370 :   N = lg(x)-1;
     675       49370 :   v = cgetg(N+1,t_COL);
     676             : 
     677      351166 :   for (k=1; k<=N; k++)
     678             :   {
     679      301796 :     pari_sp av = avma;
     680      301796 :     GEN TABi = TAB;
     681      301796 :     if (k == 1)
     682       49370 :       s = gsqr(gel(x,1));
     683             :     else
     684      252426 :       s = gmul2n(gmul(gel(x,1),gel(x,k)), 1);
     685     1917310 :     for (i=2; i<=N; i++)
     686             :     {
     687     1615514 :       GEN p1, c, t, xi = gel(x,i);
     688     1615514 :       TABi += N;
     689     1615514 :       if (gequal0(xi)) continue;
     690             : 
     691      416836 :       c = gcoeff(TABi, k, i);
     692      416836 :       t = !gequal0(c)? gmul(c,xi): NULL;
     693     1668464 :       for (j=i+1; j<=N; j++)
     694             :       {
     695     1251628 :         c = gcoeff(TABi, k, j);
     696     1251628 :         if (gequal0(c)) continue;
     697      642908 :         p1 = gmul(gmul2n(c,1), gel(x,j));
     698      642908 :         t = t? gadd(t, p1): p1;
     699             :       }
     700      416836 :       if (t) s = gadd(s, gmul(xi, t));
     701             :     }
     702      301796 :     gel(v,k) = gerepileupto(av,s);
     703             :   }
     704       49370 :   return v;
     705             : }
     706             : 
     707             : static GEN
     708      245065 : _mul(void *data, GEN x, GEN y) { return nfmuli((GEN)data,x,y); }
     709             : static GEN
     710      757753 : _sqr(void *data, GEN x) { return nfsqri((GEN)data,x); }
     711             : 
     712             : /* Compute z^n in nf, left-shift binary powering */
     713             : GEN
     714      854836 : nfpow(GEN nf, GEN z, GEN n)
     715             : {
     716      854836 :   pari_sp av = avma;
     717             :   long s;
     718             :   GEN x, cx;
     719             : 
     720      854836 :   if (typ(n)!=t_INT) pari_err_TYPE("nfpow",n);
     721      854836 :   nf = checknf(nf);
     722      854834 :   s = signe(n); if (!s) return gen_1;
     723      854834 :   if (is_famat(z)) return famat_pow(z, n);
     724      794193 :   x = nf_to_scalar_or_basis(nf, z);
     725      794195 :   if (typ(x) != t_COL) return powgi(x,n);
     726      676452 :   if (s < 0)
     727             :   { /* simplified nfinv */
     728             :     GEN d;
     729       41527 :     x = Q_remove_denom(x, &d);
     730       41527 :     x = zk_inv(nf, x);
     731       41527 :     x = primitive_part(x, &cx);
     732       41527 :     cx = mul_content(cx, d);
     733       41527 :     n = negi(n);
     734             :   }
     735             :   else
     736      634925 :     x = primitive_part(x, &cx);
     737      676439 :   x = gen_pow_i(x, n, (void*)nf, _sqr, _mul);
     738      676447 :   if (cx)
     739       38075 :     x = gerepileupto(av, gmul(x, powgi(cx, n)));
     740             :   else
     741      638372 :     x = gerepilecopy(av, x);
     742      676454 :   return x;
     743             : }
     744             : /* Compute z^n in nf, left-shift binary powering */
     745             : GEN
     746      342210 : nfpow_u(GEN nf, GEN z, ulong n)
     747             : {
     748      342210 :   pari_sp av = avma;
     749             :   GEN x, cx;
     750             : 
     751      342210 :   if (!n) return gen_1;
     752      342210 :   x = nf_to_scalar_or_basis(nf, z);
     753      342210 :   if (typ(x) != t_COL) return gpowgs(x,n);
     754      306004 :   x = primitive_part(x, &cx);
     755      306004 :   x = gen_powu_i(x, n, (void*)nf, _sqr, _mul);
     756      306004 :   if (cx)
     757             :   {
     758      111311 :     x = gmul(x, powgi(cx, utoipos(n)));
     759      111311 :     return gerepileupto(av,x);
     760             :   }
     761      194693 :   return gerepilecopy(av, x);
     762             : }
     763             : 
     764             : long
     765         588 : nfissquare(GEN nf, GEN z, GEN *px)
     766             : {
     767         588 :   pari_sp av = avma;
     768         588 :   long v = fetch_var_higher();
     769             :   GEN R;
     770         588 :   nf = checknf(nf);
     771         588 :   if (nf_get_degree(nf) == 1)
     772             :   {
     773          21 :     z = algtobasis(nf, z);
     774          21 :     if (!issquareall(gel(z,1), px)) return gc_long(av, 0);
     775          14 :     if (px) *px = gerepileupto(av, *px); else set_avma(av);
     776          14 :     return 1;
     777             :   }
     778         567 :   z = nf_to_scalar_or_alg(nf, z);
     779         567 :   R = nfroots(nf, deg2pol_shallow(gen_m1, gen_0, z, v));
     780         567 :   delete_var(); if (lg(R) == 1) return gc_long(av, 0);
     781         546 :   if (px) *px = gerepilecopy(av, nf_to_scalar_or_basis(nf, gel(R,1)));
     782          14 :   else set_avma(av);
     783         546 :   return 1;
     784             : }
     785             : 
     786             : long
     787        7746 : nfispower(GEN nf, GEN z, long n, GEN *px)
     788             : {
     789        7746 :   pari_sp av = avma;
     790        7746 :   long v = fetch_var_higher();
     791             :   GEN R;
     792        7746 :   nf = checknf(nf);
     793        7746 :   if (nf_get_degree(nf) == 1)
     794             :   {
     795         329 :     z = algtobasis(nf, z);
     796         329 :     if (!ispower(gel(z,1), stoi(n), px)) return gc_long(av, 0);
     797         147 :     if (px) *px = gerepileupto(av, *px); else set_avma(av);
     798         147 :     return 1;
     799             :   }
     800        7417 :   if (n <= 0)
     801           0 :     pari_err_DOMAIN("nfeltispower","exponent","<=",gen_0,stoi(n));
     802        7417 :   z = nf_to_scalar_or_alg(nf, z);
     803        7417 :   if (n==1)
     804             :   {
     805           0 :     if (px) *px = gerepilecopy(av, z);
     806           0 :     return 1;
     807             :   }
     808        7417 :   R = nfroots(nf, gsub(pol_xn(n, v), z));
     809        7417 :   delete_var(); if (lg(R) == 1) return gc_long(av, 0);
     810        3157 :   if (px) *px = gerepilecopy(av, nf_to_scalar_or_basis(nf, gel(R,1)));
     811        3143 :   else set_avma(av);
     812        3157 :   return 1;
     813             : }
     814             : 
     815             : static GEN
     816          56 : idmulred(void *nf, GEN x, GEN y) { return idealmulred((GEN) nf, x, y); }
     817             : static GEN
     818         413 : idpowred(void *nf, GEN x, GEN n) { return idealpowred((GEN) nf, x, n); }
     819             : static GEN
     820       70361 : idmul(void *nf, GEN x, GEN y) { return idealmul((GEN) nf, x, y); }
     821             : static GEN
     822       86123 : idpow(void *nf, GEN x, GEN n) { return idealpow((GEN) nf, x, n); }
     823             : GEN
     824       85079 : idealfactorback(GEN nf, GEN L, GEN e, int red)
     825             : {
     826       85079 :   nf = checknf(nf);
     827       85079 :   if (red) return gen_factorback(L, e, (void*)nf, &idmulred, &idpowred, NULL);
     828       84722 :   if (!e && typ(L) == t_MAT && lg(L) == 3) { e = gel(L,2); L = gel(L,1); }
     829       84722 :   if (is_vec_t(typ(L)) && RgV_is_prV(L))
     830             :   { /* don't use gen_factorback since *= pr^v can be done more efficiently */
     831       64305 :     pari_sp av = avma;
     832       64305 :     long i, l = lg(L);
     833             :     GEN a;
     834       64305 :     if (!e) e = const_vec(l-1, gen_1);
     835       61449 :     else switch(typ(e))
     836             :     {
     837        7250 :       case t_VECSMALL: e = zv_to_ZV(e); break;
     838       54199 :       case t_VEC: case t_COL:
     839       54199 :         if (!RgV_is_ZV(e))
     840           0 :           pari_err_TYPE("factorback [not an exponent vector]", e);
     841       54199 :         break;
     842           0 :       default: pari_err_TYPE("idealfactorback", e);
     843             :     }
     844       64305 :     if (l != lg(e))
     845           0 :       pari_err_TYPE("factorback [not an exponent vector]", e);
     846       64305 :     if (l == 1 || ZV_equal0(e)) return gc_const(av, gen_1);
     847       20392 :     a = idealpow(nf, gel(L,1), gel(e,1));
     848      234853 :     for (i = 2; i < l; i++)
     849      214461 :       if (signe(gel(e,i))) a = idealmulpowprime(nf, a, gel(L,i), gel(e,i));
     850       20392 :     return gerepileupto(av, a);
     851             :   }
     852       20417 :   return gen_factorback(L, e, (void*)nf, &idmul, &idpow, NULL);
     853             : }
     854             : static GEN
     855      321229 : eltmul(void *nf, GEN x, GEN y) { return nfmul((GEN) nf, x, y); }
     856             : static GEN
     857      458838 : eltpow(void *nf, GEN x, GEN n) { return nfpow((GEN) nf, x, n); }
     858             : GEN
     859      265672 : nffactorback(GEN nf, GEN L, GEN e)
     860      265672 : { return gen_factorback(L, e, (void*)checknf(nf), &eltmul, &eltpow, NULL); }
     861             : 
     862             : static GEN
     863     3080880 : _nf_red(void *E, GEN x) { (void)E; return gcopy(x); }
     864             : 
     865             : static GEN
     866    12632962 : _nf_add(void *E, GEN x, GEN y) { return nfadd((GEN)E,x,y); }
     867             : 
     868             : static GEN
     869      747819 : _nf_neg(void *E, GEN x) { (void)E; return gneg(x); }
     870             : 
     871             : static GEN
     872    15164880 : _nf_mul(void *E, GEN x, GEN y) { return nfmul((GEN)E,x,y); }
     873             : 
     874             : static GEN
     875       53161 : _nf_inv(void *E, GEN x) { return nfinv((GEN)E,x); }
     876             : 
     877             : static GEN
     878       10841 : _nf_s(void *E, long x) { (void)E; return stoi(x); }
     879             : 
     880             : static const struct bb_field nf_field={_nf_red,_nf_add,_nf_mul,_nf_neg,
     881             :                                         _nf_inv,&gequal0,_nf_s };
     882             : 
     883      225825 : const struct bb_field *get_nf_field(void **E, GEN nf)
     884      225825 : { *E = (void*)nf; return &nf_field; }
     885             : 
     886             : GEN
     887          14 : nfM_det(GEN nf, GEN M)
     888             : {
     889             :   void *E;
     890          14 :   const struct bb_field *S = get_nf_field(&E, nf);
     891          14 :   return gen_det(M, E, S);
     892             : }
     893             : GEN
     894       10827 : nfM_inv(GEN nf, GEN M)
     895             : {
     896             :   void *E;
     897       10827 :   const struct bb_field *S = get_nf_field(&E, nf);
     898       10827 :   return gen_Gauss(M, matid(lg(M)-1), E, S);
     899             : }
     900             : 
     901             : GEN
     902           0 : nfM_ker(GEN nf, GEN M)
     903             : {
     904             :    void *E;
     905           0 :    const struct bb_field *S = get_nf_field(&E, nf);
     906           0 :    return gen_ker(M, 0, E, S);
     907             : }
     908             : 
     909             : GEN
     910       10358 : nfM_mul(GEN nf, GEN A, GEN B)
     911             : {
     912             :   void *E;
     913       10358 :   const struct bb_field *S = get_nf_field(&E, nf);
     914       10358 :   return gen_matmul(A, B, E, S);
     915             : }
     916             : GEN
     917      204626 : nfM_nfC_mul(GEN nf, GEN A, GEN B)
     918             : {
     919             :   void *E;
     920      204626 :   const struct bb_field *S = get_nf_field(&E, nf);
     921      204626 :   return gen_matcolmul(A, B, E, S);
     922             : }
     923             : 
     924             : /* valuation of integral x (ZV), with resp. to prime ideal pr */
     925             : long
     926    24141869 : ZC_nfvalrem(GEN x, GEN pr, GEN *newx)
     927             : {
     928    24141869 :   pari_sp av = avma;
     929             :   long i, v, l;
     930    24141869 :   GEN r, y, p = pr_get_p(pr), mul = pr_get_tau(pr);
     931             : 
     932             :   /* p inert */
     933    24141892 :   if (typ(mul) == t_INT) return newx? ZV_pvalrem(x, p, newx):ZV_pval(x, p);
     934    23153464 :   y = cgetg_copy(x, &l); /* will hold the new x */
     935    23153922 :   x = leafcopy(x);
     936    36996969 :   for(v=0;; v++)
     937             :   {
     938   142229109 :     for (i=1; i<l; i++)
     939             :     { /* is (x.b)[i] divisible by p ? */
     940   128380151 :       gel(y,i) = dvmdii(ZMrow_ZC_mul(mul,x,i),p,&r);
     941   128383901 :       if (r != gen_0) { if (newx) *newx = x; return v; }
     942             :     }
     943    13848958 :     swap(x, y);
     944    13848958 :     if (!newx && (v & 0xf) == 0xf) v += pr_get_e(pr) * ZV_pvalrem(x, p, &x);
     945    13848958 :     if (gc_needed(av,1))
     946             :     {
     947           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ZC_nfvalrem, v >= %ld", v);
     948           0 :       gerepileall(av, 2, &x, &y);
     949             :     }
     950             :   }
     951             : }
     952             : long
     953    19891226 : ZC_nfval(GEN x, GEN P)
     954    19891226 : { return ZC_nfvalrem(x, P, NULL); }
     955             : 
     956             : /* v_P(x) != 0, x a ZV. Simpler version of ZC_nfvalrem */
     957             : int
     958     1246457 : ZC_prdvd(GEN x, GEN P)
     959             : {
     960     1246457 :   pari_sp av = avma;
     961             :   long i, l;
     962     1246457 :   GEN p = pr_get_p(P), mul = pr_get_tau(P);
     963     1246476 :   if (typ(mul) == t_INT) return ZV_Z_dvd(x, p);
     964     1245937 :   l = lg(x);
     965     5050087 :   for (i=1; i<l; i++)
     966     4534431 :     if (!dvdii(ZMrow_ZC_mul(mul,x,i), p)) return gc_bool(av,0);
     967      515656 :   return gc_bool(av,1);
     968             : }
     969             : 
     970             : int
     971         357 : pr_equal(GEN P, GEN Q)
     972             : {
     973         357 :   GEN gQ, p = pr_get_p(P);
     974         357 :   long e = pr_get_e(P), f = pr_get_f(P), n;
     975         357 :   if (!equalii(p, pr_get_p(Q)) || e != pr_get_e(Q) || f != pr_get_f(Q))
     976         336 :     return 0;
     977          21 :   gQ = pr_get_gen(Q); n = lg(gQ)-1;
     978          21 :   if (2*e*f > n) return 1; /* room for only one such pr */
     979          14 :   return ZV_equal(pr_get_gen(P), gQ) || ZC_prdvd(gQ, P);
     980             : }
     981             : 
     982             : GEN
     983      420721 : famat_nfvalrem(GEN nf, GEN x, GEN pr, GEN *py)
     984             : {
     985      420721 :   pari_sp av = avma;
     986      420721 :   GEN P = gel(x,1), E = gel(x,2), V = gen_0, y = NULL;
     987      420721 :   long l = lg(P), simplify = 0, i;
     988      420721 :   if (py) { *py = gen_1; y = cgetg(l, t_COL); }
     989             : 
     990     2258510 :   for (i = 1; i < l; i++)
     991             :   {
     992     1837789 :     GEN e = gel(E,i);
     993             :     long v;
     994     1837789 :     if (!signe(e))
     995             :     {
     996           7 :       if (py) gel(y,i) = gen_1;
     997           7 :       simplify = 1; continue;
     998             :     }
     999     1837782 :     v = nfvalrem(nf, gel(P,i), pr, py? &gel(y,i): NULL);
    1000     1837782 :     if (v == LONG_MAX) { set_avma(av); if (py) *py = gen_0; return mkoo(); }
    1001     1837782 :     V = addmulii(V, stoi(v), e);
    1002             :   }
    1003      420721 :   if (!py) V = gerepileuptoint(av, V);
    1004             :   else
    1005             :   {
    1006          42 :     y = mkmat2(y, gel(x,2));
    1007          42 :     if (simplify) y = famat_remove_trivial(y);
    1008          42 :     gerepileall(av, 2, &V, &y); *py = y;
    1009             :   }
    1010      420721 :   return V;
    1011             : }
    1012             : long
    1013     5621929 : nfval(GEN nf, GEN x, GEN pr)
    1014             : {
    1015     5621929 :   pari_sp av = avma;
    1016             :   long w, e;
    1017             :   GEN cx, p;
    1018             : 
    1019     5621929 :   if (gequal0(x)) return LONG_MAX;
    1020     5608585 :   nf = checknf(nf);
    1021     5608582 :   checkprid(pr);
    1022     5608574 :   p = pr_get_p(pr);
    1023     5608572 :   e = pr_get_e(pr);
    1024     5608569 :   x = nf_to_scalar_or_basis(nf, x);
    1025     5608461 :   if (typ(x) != t_COL) return e*Q_pval(x,p);
    1026     2376554 :   x = Q_primitive_part(x, &cx);
    1027     2376633 :   w = ZC_nfval(x,pr);
    1028     2376545 :   if (cx) w += e*Q_pval(cx,p);
    1029     2376545 :   return gc_long(av,w);
    1030             : }
    1031             : 
    1032             : /* want to write p^v = uniformizer^(e*v) * z^v, z coprime to pr */
    1033             : /* z := tau^e / p^(e-1), algebraic integer coprime to pr; return z^v */
    1034             : static GEN
    1035      951048 : powp(GEN nf, GEN pr, long v)
    1036             : {
    1037             :   GEN b, z;
    1038             :   long e;
    1039      951048 :   if (!v) return gen_1;
    1040      424473 :   b = pr_get_tau(pr);
    1041      424473 :   if (typ(b) == t_INT) return gen_1;
    1042      121898 :   e = pr_get_e(pr);
    1043      121898 :   z = gel(b,1);
    1044      121898 :   if (e != 1) z = gdiv(nfpow_u(nf, z, e), powiu(pr_get_p(pr),e-1));
    1045      121898 :   if (v < 0) { v = -v; z = nfinv(nf, z); }
    1046      121898 :   if (v != 1) z = nfpow_u(nf, z, v);
    1047      121898 :   return z;
    1048             : }
    1049             : long
    1050     3639111 : nfvalrem(GEN nf, GEN x, GEN pr, GEN *py)
    1051             : {
    1052     3639111 :   pari_sp av = avma;
    1053             :   long w, e;
    1054             :   GEN cx, p, t;
    1055             : 
    1056     3639111 :   if (!py) return nfval(nf,x,pr);
    1057     1787938 :   if (gequal0(x)) { *py = gen_0; return LONG_MAX; }
    1058     1787881 :   nf = checknf(nf);
    1059     1787880 :   checkprid(pr);
    1060     1787880 :   p = pr_get_p(pr);
    1061     1787880 :   e = pr_get_e(pr);
    1062     1787880 :   x = nf_to_scalar_or_basis(nf, x);
    1063     1787880 :   if (typ(x) != t_COL) {
    1064      538531 :     w = Q_pvalrem(x,p, py);
    1065      538531 :     if (!w) { *py = gerepilecopy(av, x); return 0; }
    1066      330239 :     *py = gerepileupto(av, gmul(powp(nf, pr, w), *py));
    1067      330239 :     return e*w;
    1068             :   }
    1069     1249349 :   x = Q_primitive_part(x, &cx);
    1070     1249350 :   w = ZC_nfvalrem(x,pr, py);
    1071     1249335 :   if (cx)
    1072             :   {
    1073      620809 :     long v = Q_pvalrem(cx,p, &t);
    1074      620809 :     *py = nfmul(nf, *py, gmul(powp(nf,pr,v), t));
    1075      620809 :     *py = gerepileupto(av, *py);
    1076      620808 :     w += e*v;
    1077             :   }
    1078             :   else
    1079      628526 :     *py = gerepilecopy(av, *py);
    1080     1249349 :   return w;
    1081             : }
    1082             : GEN
    1083       15015 : gpnfvalrem(GEN nf, GEN x, GEN pr, GEN *py)
    1084             : {
    1085             :   long v;
    1086       15015 :   if (is_famat(x)) return famat_nfvalrem(nf, x, pr, py);
    1087       15008 :   v = nfvalrem(nf,x,pr,py);
    1088       15008 :   return v == LONG_MAX? mkoo(): stoi(v);
    1089             : }
    1090             : 
    1091             : /* true nf */
    1092             : GEN
    1093      305620 : coltoalg(GEN nf, GEN x)
    1094             : {
    1095      305620 :   return mkpolmod( nf_to_scalar_or_alg(nf, x), nf_get_pol(nf) );
    1096             : }
    1097             : 
    1098             : GEN
    1099      358712 : basistoalg(GEN nf, GEN x)
    1100             : {
    1101             :   GEN T;
    1102             : 
    1103      358712 :   nf = checknf(nf);
    1104      358712 :   switch(typ(x))
    1105             :   {
    1106      299355 :     case t_COL: {
    1107      299355 :       pari_sp av = avma;
    1108      299355 :       return gerepilecopy(av, coltoalg(nf, x));
    1109             :     }
    1110       33390 :     case t_POLMOD:
    1111       33390 :       T = nf_get_pol(nf);
    1112       33390 :       if (!RgX_equal_var(T,gel(x,1)))
    1113           0 :         pari_err_MODULUS("basistoalg", T,gel(x,1));
    1114       33390 :       return gcopy(x);
    1115        2359 :     case t_POL:
    1116        2359 :       T = nf_get_pol(nf);
    1117        2359 :       if (varn(T) != varn(x)) pari_err_VAR("basistoalg",x,T);
    1118        2359 :       retmkpolmod(RgX_rem(x, T), ZX_copy(T));
    1119       23608 :     case t_INT:
    1120             :     case t_FRAC:
    1121       23608 :       T = nf_get_pol(nf);
    1122       23608 :       retmkpolmod(gcopy(x), ZX_copy(T));
    1123           0 :     default:
    1124           0 :       pari_err_TYPE("basistoalg",x);
    1125             :       return NULL; /* LCOV_EXCL_LINE */
    1126             :   }
    1127             : }
    1128             : 
    1129             : /* true nf, x a t_POL */
    1130             : static GEN
    1131     4555866 : pol_to_scalar_or_basis(GEN nf, GEN x)
    1132             : {
    1133     4555866 :   GEN T = nf_get_pol(nf);
    1134     4555865 :   long l = lg(x);
    1135     4555865 :   if (varn(x) != varn(T)) pari_err_VAR("nf_to_scalar_or_basis", x,T);
    1136     4555760 :   if (l >= lg(T)) { x = RgX_rem(x, T); l = lg(x); }
    1137     4555760 :   if (l == 2) return gen_0;
    1138     3542559 :   if (l == 3)
    1139             :   {
    1140      818437 :     x = gel(x,2);
    1141      818437 :     if (!is_rational_t(typ(x))) pari_err_TYPE("nf_to_scalar_or_basis",x);
    1142      818430 :     return x;
    1143             :   }
    1144     2724122 :   return poltobasis(nf,x);
    1145             : }
    1146             : /* Assume nf is a genuine nf. */
    1147             : GEN
    1148   161129115 : nf_to_scalar_or_basis(GEN nf, GEN x)
    1149             : {
    1150   161129115 :   switch(typ(x))
    1151             :   {
    1152    96959732 :     case t_INT: case t_FRAC:
    1153    96959732 :       return x;
    1154      556253 :     case t_POLMOD:
    1155      556253 :       x = checknfelt_mod(nf,x,"nf_to_scalar_or_basis");
    1156      556116 :       switch(typ(x))
    1157             :       {
    1158       85427 :         case t_INT: case t_FRAC: return x;
    1159      470689 :         case t_POL: return pol_to_scalar_or_basis(nf,x);
    1160             :       }
    1161           0 :       break;
    1162     4085178 :     case t_POL: return pol_to_scalar_or_basis(nf,x);
    1163    59531505 :     case t_COL:
    1164    59531505 :       if (lg(x)-1 != nf_get_degree(nf)) break;
    1165    59531261 :       return QV_isscalar(x)? gel(x,1): x;
    1166             :   }
    1167          96 :   pari_err_TYPE("nf_to_scalar_or_basis",x);
    1168             :   return NULL; /* LCOV_EXCL_LINE */
    1169             : }
    1170             : /* Let x be a polynomial with coefficients in Q or nf. Return the same
    1171             :  * polynomial with coefficients expressed as vectors (on the integral basis).
    1172             :  * No consistency checks, not memory-clean. */
    1173             : GEN
    1174       28767 : RgX_to_nfX(GEN nf, GEN x)
    1175             : {
    1176             :   long i, l;
    1177       28767 :   GEN y = cgetg_copy(x, &l); y[1] = x[1];
    1178      235475 :   for (i=2; i<l; i++) gel(y,i) = nf_to_scalar_or_basis(nf, gel(x,i));
    1179       28767 :   return y;
    1180             : }
    1181             : 
    1182             : /* Assume nf is a genuine nf. */
    1183             : GEN
    1184     3877481 : nf_to_scalar_or_alg(GEN nf, GEN x)
    1185             : {
    1186     3877481 :   switch(typ(x))
    1187             :   {
    1188       84809 :     case t_INT: case t_FRAC:
    1189       84809 :       return x;
    1190         420 :     case t_POLMOD:
    1191         420 :       x = checknfelt_mod(nf,x,"nf_to_scalar_or_alg");
    1192         420 :       if (typ(x) != t_POL) return x;
    1193             :       /* fall through */
    1194             :     case t_POL:
    1195             :     {
    1196        5124 :       GEN T = nf_get_pol(nf);
    1197        5124 :       long l = lg(x);
    1198        5124 :       if (varn(x) != varn(T)) pari_err_VAR("nf_to_scalar_or_alg", x,T);
    1199        5124 :       if (l >= lg(T)) { x = RgX_rem(x, T); l = lg(x); }
    1200        5124 :       if (l == 2) return gen_0;
    1201        5124 :       if (l == 3) return gel(x,2);
    1202        3612 :       return x;
    1203             :     }
    1204     3787501 :     case t_COL:
    1205             :     {
    1206             :       GEN dx;
    1207     3787501 :       if (lg(x)-1 != nf_get_degree(nf)) break;
    1208     7498140 :       if (QV_isscalar(x)) return gel(x,1);
    1209     3710508 :       x = Q_remove_denom(x, &dx);
    1210     3710538 :       x = RgV_RgC_mul(nf_get_zkprimpart(nf), x);
    1211     3710652 :       dx = mul_denom(dx, nf_get_zkden(nf));
    1212     3710626 :       return gdiv(x,dx);
    1213             :     }
    1214             :   }
    1215          48 :   pari_err_TYPE("nf_to_scalar_or_alg",x);
    1216             :   return NULL; /* LCOV_EXCL_LINE */
    1217             : }
    1218             : 
    1219             : /* gmul(A, RgX_to_RgC(x)), A t_MAT of compatible dimensions */
    1220             : GEN
    1221        1365 : RgM_RgX_mul(GEN A, GEN x)
    1222             : {
    1223        1365 :   long i, l = lg(x)-1;
    1224             :   GEN z;
    1225        1365 :   if (l == 1) return zerocol(nbrows(A));
    1226        1351 :   z = gmul(gel(x,2), gel(A,1));
    1227        2555 :   for (i = 2; i < l; i++)
    1228        1204 :     if (!gequal0(gel(x,i+1))) z = gadd(z, gmul(gel(x,i+1), gel(A,i)));
    1229        1351 :   return z;
    1230             : }
    1231             : GEN
    1232    10304025 : ZM_ZX_mul(GEN A, GEN x)
    1233             : {
    1234    10304025 :   long i, l = lg(x)-1;
    1235             :   GEN z;
    1236    10304025 :   if (l == 1) return zerocol(nbrows(A));
    1237    10302891 :   z = ZC_Z_mul(gel(A,1), gel(x,2));
    1238    32158911 :   for (i = 2; i < l ; i++)
    1239    21858812 :     if (signe(gel(x,i+1))) z = ZC_add(z, ZC_Z_mul(gel(A,i), gel(x,i+1)));
    1240    10300099 :   return z;
    1241             : }
    1242             : /* x a t_POL, nf a genuine nf. No garbage collecting. No check.  */
    1243             : GEN
    1244     9706942 : poltobasis(GEN nf, GEN x)
    1245             : {
    1246     9706942 :   GEN d, T = nf_get_pol(nf);
    1247     9706914 :   if (varn(x) != varn(T)) pari_err_VAR( "poltobasis", x,T);
    1248     9706781 :   if (degpol(x) >= degpol(T)) x = RgX_rem(x,T);
    1249     9706770 :   x = Q_remove_denom(x, &d);
    1250     9707120 :   if (!RgX_is_ZX(x)) pari_err_TYPE("poltobasis",x);
    1251     9707017 :   x = ZM_ZX_mul(nf_get_invzk(nf), x);
    1252     9704817 :   if (d) x = RgC_Rg_div(x, d);
    1253     9704900 :   return x;
    1254             : }
    1255             : 
    1256             : GEN
    1257      920888 : algtobasis(GEN nf, GEN x)
    1258             : {
    1259             :   pari_sp av;
    1260             : 
    1261      920888 :   nf = checknf(nf);
    1262      920887 :   switch(typ(x))
    1263             :   {
    1264      113254 :     case t_POLMOD:
    1265      113254 :       if (!RgX_equal_var(nf_get_pol(nf),gel(x,1)))
    1266           7 :         pari_err_MODULUS("algtobasis", nf_get_pol(nf),gel(x,1));
    1267      113247 :       x = gel(x,2);
    1268      113247 :       switch(typ(x))
    1269             :       {
    1270        8197 :         case t_INT:
    1271        8197 :         case t_FRAC: return scalarcol(x, nf_get_degree(nf));
    1272      105050 :         case t_POL:
    1273      105050 :           av = avma;
    1274      105050 :           return gerepileupto(av,poltobasis(nf,x));
    1275             :       }
    1276           0 :       break;
    1277             : 
    1278      249489 :     case t_POL:
    1279      249489 :       av = avma;
    1280      249489 :       return gerepileupto(av,poltobasis(nf,x));
    1281             : 
    1282       83122 :     case t_COL:
    1283       83122 :       if (!RgV_is_QV(x)) pari_err_TYPE("nfalgtobasis",x);
    1284       83115 :       if (lg(x)-1 != nf_get_degree(nf)) pari_err_DIM("nfalgtobasis");
    1285       83115 :       return gcopy(x);
    1286             : 
    1287      475025 :     case t_INT:
    1288      475025 :     case t_FRAC: return scalarcol(x, nf_get_degree(nf));
    1289             :   }
    1290           0 :   pari_err_TYPE("algtobasis",x);
    1291             :   return NULL; /* LCOV_EXCL_LINE */
    1292             : }
    1293             : 
    1294             : GEN
    1295       47488 : rnfbasistoalg(GEN rnf,GEN x)
    1296             : {
    1297       47488 :   const char *f = "rnfbasistoalg";
    1298             :   long lx, i;
    1299       47488 :   pari_sp av = avma;
    1300             :   GEN z, nf, R, T;
    1301             : 
    1302       47488 :   checkrnf(rnf);
    1303       47488 :   nf = rnf_get_nf(rnf);
    1304       47488 :   T = nf_get_pol(nf);
    1305       47488 :   R = QXQX_to_mod_shallow(rnf_get_pol(rnf), T);
    1306       47488 :   switch(typ(x))
    1307             :   {
    1308         875 :     case t_COL:
    1309         875 :       z = cgetg_copy(x, &lx);
    1310        2597 :       for (i=1; i<lx; i++)
    1311             :       {
    1312        1778 :         GEN c = nf_to_scalar_or_alg(nf, gel(x,i));
    1313        1722 :         if (typ(c) == t_POL) c = mkpolmod(c,T);
    1314        1722 :         gel(z,i) = c;
    1315             :       }
    1316         819 :       z = RgV_RgC_mul(gel(rnf_get_zk(rnf),1), z);
    1317         735 :       return gerepileupto(av, gmodulo(z,R));
    1318             : 
    1319       31227 :     case t_POLMOD:
    1320       31227 :       x = polmod_nffix(f, rnf, x, 0);
    1321       30954 :       if (typ(x) != t_POL) break;
    1322       14261 :       retmkpolmod(RgX_copy(x), RgX_copy(R));
    1323        1274 :     case t_POL:
    1324        1274 :       if (varn(x) == varn(T)) { RgX_check_QX(x,f); x = gmodulo(x,T); break; }
    1325        1029 :       if (varn(x) == varn(R))
    1326             :       {
    1327         973 :         x = RgX_nffix(f,nf_get_pol(nf),x,0);
    1328         973 :         return gmodulo(x, R);
    1329             :       }
    1330          56 :       pari_err_VAR(f, x,R);
    1331             :   }
    1332       30994 :   retmkpolmod(scalarpol(x, varn(R)), RgX_copy(R));
    1333             : }
    1334             : 
    1335             : GEN
    1336        2275 : matbasistoalg(GEN nf,GEN x)
    1337             : {
    1338             :   long i, j, li, lx;
    1339        2275 :   GEN z = cgetg_copy(x, &lx);
    1340             : 
    1341        2275 :   if (lx == 1) return z;
    1342        2268 :   switch(typ(x))
    1343             :   {
    1344          77 :     case t_VEC: case t_COL:
    1345         273 :       for (i=1; i<lx; i++) gel(z,i) = basistoalg(nf, gel(x,i));
    1346          77 :       return z;
    1347        2191 :     case t_MAT: break;
    1348           0 :     default: pari_err_TYPE("matbasistoalg",x);
    1349             :   }
    1350        2191 :   li = lgcols(x);
    1351        8183 :   for (j=1; j<lx; j++)
    1352             :   {
    1353        5992 :     GEN c = cgetg(li,t_COL), xj = gel(x,j);
    1354        5992 :     gel(z,j) = c;
    1355       28077 :     for (i=1; i<li; i++) gel(c,i) = basistoalg(nf, gel(xj,i));
    1356             :   }
    1357        2191 :   return z;
    1358             : }
    1359             : 
    1360             : GEN
    1361       30638 : matalgtobasis(GEN nf,GEN x)
    1362             : {
    1363             :   long i, j, li, lx;
    1364       30638 :   GEN z = cgetg_copy(x, &lx);
    1365             : 
    1366       30638 :   if (lx == 1) return z;
    1367       30176 :   switch(typ(x))
    1368             :   {
    1369       30169 :     case t_VEC: case t_COL:
    1370       79039 :       for (i=1; i<lx; i++) gel(z,i) = algtobasis(nf, gel(x,i));
    1371       30169 :       return z;
    1372           7 :     case t_MAT: break;
    1373           0 :     default: pari_err_TYPE("matalgtobasis",x);
    1374             :   }
    1375           7 :   li = lgcols(x);
    1376          14 :   for (j=1; j<lx; j++)
    1377             :   {
    1378           7 :     GEN c = cgetg(li,t_COL), xj = gel(x,j);
    1379           7 :     gel(z,j) = c;
    1380          21 :     for (i=1; i<li; i++) gel(c,i) = algtobasis(nf, gel(xj,i));
    1381             :   }
    1382           7 :   return z;
    1383             : }
    1384             : GEN
    1385       10953 : RgM_to_nfM(GEN nf,GEN x)
    1386             : {
    1387             :   long i, j, li, lx;
    1388       10953 :   GEN z = cgetg_copy(x, &lx);
    1389             : 
    1390       10953 :   if (lx == 1) return z;
    1391       10953 :   li = lgcols(x);
    1392       81711 :   for (j=1; j<lx; j++)
    1393             :   {
    1394       70758 :     GEN c = cgetg(li,t_COL), xj = gel(x,j);
    1395       70758 :     gel(z,j) = c;
    1396      462951 :     for (i=1; i<li; i++) gel(c,i) = nf_to_scalar_or_basis(nf, gel(xj,i));
    1397             :   }
    1398       10953 :   return z;
    1399             : }
    1400             : GEN
    1401      148531 : RgC_to_nfC(GEN nf, GEN x)
    1402      908819 : { pari_APPLY_type(t_COL, nf_to_scalar_or_basis(nf, gel(x,i))) }
    1403             : 
    1404             : /* x a t_POLMOD, supposedly in rnf = K[z]/(T), K = Q[y]/(Tnf) */
    1405             : GEN
    1406      141436 : polmod_nffix(const char *f, GEN rnf, GEN x, int lift)
    1407      141436 : { return polmod_nffix2(f, rnf_get_nfpol(rnf), rnf_get_pol(rnf), x,lift); }
    1408             : GEN
    1409      141527 : polmod_nffix2(const char *f, GEN T, GEN R, GEN x, int lift)
    1410             : {
    1411      141527 :   if (RgX_equal_var(gel(x,1), R))
    1412             :   {
    1413      129087 :     x = gel(x,2);
    1414      129087 :     if (typ(x) == t_POL && varn(x) == varn(R))
    1415             :     {
    1416       98349 :       x = RgX_nffix(f, T, x, lift);
    1417       98349 :       switch(lg(x))
    1418             :       {
    1419        5817 :         case 2: return gen_0;
    1420       12197 :         case 3: return gel(x,2);
    1421             :       }
    1422       80335 :       return x;
    1423             :     }
    1424             :   }
    1425       43178 :   return Rg_nffix(f, T, x, lift);
    1426             : }
    1427             : GEN
    1428        1428 : rnfalgtobasis(GEN rnf,GEN x)
    1429             : {
    1430        1428 :   const char *f = "rnfalgtobasis";
    1431        1428 :   pari_sp av = avma;
    1432             :   GEN T, R;
    1433             : 
    1434        1428 :   checkrnf(rnf);
    1435        1428 :   R = rnf_get_pol(rnf);
    1436        1428 :   T = rnf_get_nfpol(rnf);
    1437        1428 :   switch(typ(x))
    1438             :   {
    1439          98 :     case t_COL:
    1440          98 :       if (lg(x)-1 != rnf_get_degree(rnf)) pari_err_DIM(f);
    1441          49 :       x = RgV_nffix(f, T, x, 0);
    1442          42 :       return gerepilecopy(av, x);
    1443             : 
    1444        1162 :     case t_POLMOD:
    1445        1162 :       x = polmod_nffix(f, rnf, x, 0);
    1446        1057 :       if (typ(x) != t_POL) break;
    1447         714 :       return gerepileupto(av, RgM_RgX_mul(rnf_get_invzk(rnf), x));
    1448         112 :     case t_POL:
    1449         112 :       if (varn(x) == varn(T))
    1450             :       {
    1451          42 :         RgX_check_QX(x,f);
    1452          28 :         if (degpol(x) >= degpol(T)) x = RgX_rem(x,T);
    1453          28 :         x = mkpolmod(x,T); break;
    1454             :       }
    1455          70 :       x = RgX_nffix(f, T, x, 0);
    1456          56 :       if (degpol(x) >= degpol(R)) x = RgX_rem(x, R);
    1457          56 :       return gerepileupto(av, RgM_RgX_mul(rnf_get_invzk(rnf), x));
    1458             :   }
    1459         427 :   return gerepileupto(av, scalarcol(x, rnf_get_degree(rnf)));
    1460             : }
    1461             : 
    1462             : /* Given a and b in nf, gives an algebraic integer y in nf such that a-b.y
    1463             :  * is "small" */
    1464             : GEN
    1465         259 : nfdiveuc(GEN nf, GEN a, GEN b)
    1466             : {
    1467         259 :   pari_sp av = avma;
    1468         259 :   a = nfdiv(nf,a,b);
    1469         259 :   return gerepileupto(av, ground(a));
    1470             : }
    1471             : 
    1472             : /* Given a and b in nf, gives a "small" algebraic integer r in nf
    1473             :  * of the form a-b.y */
    1474             : GEN
    1475         259 : nfmod(GEN nf, GEN a, GEN b)
    1476             : {
    1477         259 :   pari_sp av = avma;
    1478         259 :   GEN p1 = gneg_i(nfmul(nf,b,ground(nfdiv(nf,a,b))));
    1479         259 :   return gerepileupto(av, nfadd(nf,a,p1));
    1480             : }
    1481             : 
    1482             : /* Given a and b in nf, gives a two-component vector [y,r] in nf such
    1483             :  * that r=a-b.y is "small". */
    1484             : GEN
    1485         259 : nfdivrem(GEN nf, GEN a, GEN b)
    1486             : {
    1487         259 :   pari_sp av = avma;
    1488         259 :   GEN p1,z, y = ground(nfdiv(nf,a,b));
    1489             : 
    1490         259 :   p1 = gneg_i(nfmul(nf,b,y));
    1491         259 :   z = cgetg(3,t_VEC);
    1492         259 :   gel(z,1) = gcopy(y);
    1493         259 :   gel(z,2) = nfadd(nf,a,p1); return gerepileupto(av, z);
    1494             : }
    1495             : 
    1496             : /*************************************************************************/
    1497             : /**                                                                     **/
    1498             : /**                   LOGARITHMIC EMBEDDINGS                            **/
    1499             : /**                                                                     **/
    1500             : /*************************************************************************/
    1501             : 
    1502             : static int
    1503     4611594 : low_prec(GEN x)
    1504             : {
    1505     4611594 :   switch(typ(x))
    1506             :   {
    1507           0 :     case t_INT: return !signe(x);
    1508     4611594 :     case t_REAL: return !signe(x) || realprec(x) <= DEFAULTPREC;
    1509           0 :     default: return 0;
    1510             :   }
    1511             : }
    1512             : 
    1513             : static GEN
    1514       23163 : cxlog_1(GEN nf) { return zerocol(lg(nf_get_roots(nf))-1); }
    1515             : static GEN
    1516         545 : cxlog_m1(GEN nf, long prec)
    1517             : {
    1518         545 :   long i, l = lg(nf_get_roots(nf)), r1 = nf_get_r1(nf);
    1519         545 :   GEN v = cgetg(l, t_COL), p,  P;
    1520         545 :   p = mppi(prec); P = mkcomplex(gen_0, p);
    1521        1224 :   for (i = 1; i <= r1; i++) gel(v,i) = P; /* IPi*/
    1522         545 :   if (i < l) P = gmul2n(P,1);
    1523        1160 :   for (     ; i < l; i++) gel(v,i) = P; /* 2IPi */
    1524         545 :   return v;
    1525             : }
    1526             : static GEN
    1527     1714934 : ZC_cxlog(GEN nf, GEN x, long prec)
    1528             : {
    1529             :   long i, l, r1;
    1530             :   GEN v;
    1531     1714934 :   x = RgM_RgC_mul(nf_get_M(nf), Q_primpart(x));
    1532     1714934 :   l = lg(x); r1 = nf_get_r1(nf);
    1533     4330667 :   for (i = 1; i <= r1; i++)
    1534     2615733 :     if (low_prec(gel(x,i))) return NULL;
    1535     3513992 :   for (     ; i <  l;  i++)
    1536     1799058 :     if (low_prec(gnorm(gel(x,i)))) return NULL;
    1537     1714934 :   v = cgetg(l,t_COL);
    1538     4330665 :   for (i = 1; i <= r1; i++) gel(v,i) = glog(gel(x,i),prec);
    1539     3513988 :   for (     ; i <  l;  i++) gel(v,i) = gmul2n(glog(gel(x,i),prec),1);
    1540     1714933 :   return v;
    1541             : }
    1542             : static GEN
    1543      223252 : famat_cxlog(GEN nf, GEN fa, long prec)
    1544             : {
    1545      223252 :   GEN G, E, y = NULL;
    1546             :   long i, l;
    1547             : 
    1548      223252 :   if (typ(fa) != t_MAT) pari_err_TYPE("famat_cxlog",fa);
    1549      223252 :   if (lg(fa) == 1) return cxlog_1(nf);
    1550      223252 :   G = gel(fa,1);
    1551      223252 :   E = gel(fa,2); l = lg(E);
    1552     1119146 :   for (i = 1; i < l; i++)
    1553             :   {
    1554      895894 :     GEN t, e = gel(E,i), x = nf_to_scalar_or_basis(nf, gel(G,i));
    1555             :     /* multiplicative arch would be better (save logs), but exponents overflow
    1556             :      * [ could keep track of expo separately, but not worth it ] */
    1557      895894 :     switch(typ(x))
    1558             :     { /* ignore positive rationals */
    1559       16412 :       case t_FRAC: x = gel(x,1); /* fall through */
    1560      266204 :       case t_INT: if (signe(x) > 0) continue;
    1561          97 :         if (!mpodd(e)) continue;
    1562          41 :         t = cxlog_m1(nf, prec); /* we probably should not reach this line */
    1563          41 :         break;
    1564      629690 :       default: /* t_COL */
    1565      629690 :         t = ZC_cxlog(nf,x,prec); if (!t) return NULL;
    1566      629690 :         t = RgC_Rg_mul(t, e);
    1567             :     }
    1568      629731 :     y = y? RgV_add(y,t): t;
    1569             :   }
    1570      223252 :   return y ? y: cxlog_1(nf);
    1571             : }
    1572             : /* Archimedean components: [e_i Log( sigma_i(X) )], where X = primpart(x),
    1573             :  * and e_i = 1 (resp 2.) for i <= R1 (resp. > R1) */
    1574             : GEN
    1575     1309644 : nf_cxlog(GEN nf, GEN x, long prec)
    1576             : {
    1577     1309644 :   if (typ(x) == t_MAT) return famat_cxlog(nf,x,prec);
    1578     1086392 :   x = nf_to_scalar_or_basis(nf,x);
    1579     1086392 :   switch(typ(x))
    1580             :   {
    1581           0 :     case t_FRAC: x = gel(x,1); /* fall through */
    1582        1148 :     case t_INT:
    1583        1148 :       return signe(x) > 0? cxlog_1(nf): cxlog_m1(nf, prec);
    1584     1085244 :     default:
    1585     1085244 :       return ZC_cxlog(nf, x, prec);
    1586             :   }
    1587             : }
    1588             : GEN
    1589          97 : nfV_cxlog(GEN nf, GEN x, long prec)
    1590             : {
    1591             :   long i, l;
    1592          97 :   GEN v = cgetg_copy(x, &l);
    1593         167 :   for (i = 1; i < l; i++)
    1594          70 :     if (!(gel(v,i) = nf_cxlog(nf, gel(x,i), prec))) return NULL;
    1595          97 :   return v;
    1596             : }
    1597             : 
    1598             : static GEN
    1599       15232 : scalar_logembed(GEN nf, GEN u, GEN *emb)
    1600             : {
    1601             :   GEN v, logu;
    1602       15232 :   long i, s = signe(u), RU = lg(nf_get_roots(nf))-1, R1 = nf_get_r1(nf);
    1603             : 
    1604       15232 :   if (!s) pari_err_DOMAIN("nflogembed","argument","=",gen_0,u);
    1605       15232 :   v = cgetg(RU+1, t_COL); logu = logr_abs(u);
    1606       17213 :   for (i = 1; i <= R1; i++) gel(v,i) = logu;
    1607       15232 :   if (i <= RU)
    1608             :   {
    1609       14350 :     GEN logu2 = shiftr(logu,1);
    1610       55839 :     for (   ; i <= RU; i++) gel(v,i) = logu2;
    1611             :   }
    1612       15232 :   if (emb) *emb = const_col(RU, u);
    1613       15232 :   return v;
    1614             : }
    1615             : 
    1616             : static GEN
    1617        1309 : famat_logembed(GEN nf,GEN x,GEN *emb,long prec)
    1618             : {
    1619        1309 :   GEN A, M, T, a, t, g = gel(x,1), e = gel(x,2);
    1620        1309 :   long i, l = lg(e);
    1621             : 
    1622        1309 :   if (l == 1) return scalar_logembed(nf, real_1(prec), emb);
    1623        1309 :   A = NULL; T = emb? cgetg(l, t_COL): NULL;
    1624        1309 :   if (emb) *emb = M = mkmat2(T, e);
    1625       62132 :   for (i = 1; i < l; i++)
    1626             :   {
    1627       60823 :     a = nflogembed(nf, gel(g,i), &t, prec);
    1628       60823 :     if (!a) return NULL;
    1629       60823 :     a = RgC_Rg_mul(a, gel(e,i));
    1630       60823 :     A = A? RgC_add(A, a): a;
    1631       60823 :     if (emb) gel(T,i) = t;
    1632             :   }
    1633        1309 :   return A;
    1634             : }
    1635             : 
    1636             : /* Get archimedean components: [e_i log( | sigma_i(x) | )], with e_i = 1
    1637             :  * (resp 2.) for i <= R1 (resp. > R1) and set emb to the embeddings of x.
    1638             :  * Return NULL if precision problem */
    1639             : GEN
    1640       98651 : nflogembed(GEN nf, GEN x, GEN *emb, long prec)
    1641             : {
    1642             :   long i, l, r1;
    1643             :   GEN v, t;
    1644             : 
    1645       98651 :   if (typ(x) == t_MAT) return famat_logembed(nf,x,emb,prec);
    1646       97342 :   x = nf_to_scalar_or_basis(nf,x);
    1647       97342 :   if (typ(x) != t_COL) return scalar_logembed(nf, gtofp(x,prec), emb);
    1648       82110 :   x = RgM_RgC_mul(nf_get_M(nf), x);
    1649       82109 :   l = lg(x); r1 = nf_get_r1(nf); v = cgetg(l,t_COL);
    1650      109039 :   for (i = 1; i <= r1; i++)
    1651             :   {
    1652       26929 :     t = gabs(gel(x,i),prec); if (low_prec(t)) return NULL;
    1653       26929 :     gel(v,i) = glog(t,prec);
    1654             :   }
    1655      251986 :   for (   ; i < l; i++)
    1656             :   {
    1657      169876 :     t = gnorm(gel(x,i)); if (low_prec(t)) return NULL;
    1658      169875 :     gel(v,i) = glog(t,prec);
    1659             :   }
    1660       82110 :   if (emb) *emb = x;
    1661       82110 :   return v;
    1662             : }
    1663             : 
    1664             : /*************************************************************************/
    1665             : /**                                                                     **/
    1666             : /**                        REAL EMBEDDINGS                              **/
    1667             : /**                                                                     **/
    1668             : /*************************************************************************/
    1669             : static GEN
    1670      486215 : sarch_get_cyc(GEN sarch) { return gel(sarch,1); }
    1671             : static GEN
    1672      665705 : sarch_get_archp(GEN sarch) { return gel(sarch,2); }
    1673             : static GEN
    1674      163621 : sarch_get_MI(GEN sarch) { return gel(sarch,3); }
    1675             : static GEN
    1676      163620 : sarch_get_lambda(GEN sarch) { return gel(sarch,4); }
    1677             : static GEN
    1678      163621 : sarch_get_F(GEN sarch) { return gel(sarch,5); }
    1679             : 
    1680             : /* x not a scalar, true nf, return number of positive roots of char_x */
    1681             : static long
    1682        1262 : num_positive(GEN nf, GEN x)
    1683             : {
    1684        1262 :   GEN T = nf_get_pol(nf), B, charx;
    1685             :   long dnf, vnf, N;
    1686        1262 :   x = nf_to_scalar_or_alg(nf, x); /* not a scalar */
    1687        1262 :   charx = ZXQ_charpoly(x, T, 0);
    1688        1262 :   charx = ZX_radical(charx);
    1689        1262 :   N = degpol(T) / degpol(charx);
    1690             :   /* real places are unramified ? */
    1691        1262 :   if (N == 1 || ZX_sturm(charx) * N == nf_get_r1(nf))
    1692        1255 :     return ZX_sturmpart(charx, mkvec2(gen_0,mkoo())) * N;
    1693             :   /* painful case, multiply by random square until primitive */
    1694           7 :   dnf = nf_get_degree(nf);
    1695           7 :   vnf = varn(T);
    1696           7 :   B = int2n(10);
    1697             :   for(;;)
    1698           0 :   {
    1699           7 :     GEN y = RgXQ_sqr(random_FpX(dnf, vnf, B), T);
    1700           7 :     y = RgXQ_mul(x, y, T);
    1701           7 :     charx = ZXQ_charpoly(y, T, 0);
    1702           7 :     if (ZX_is_squarefree(charx))
    1703           7 :       return ZX_sturmpart(charx, mkvec2(gen_0,mkoo())) * N;
    1704             :   }
    1705             : }
    1706             : 
    1707             : /* x a QC: return sigma_k(x) where 1 <= k <= r1+r2; correct but inefficient
    1708             :  * if x in Q. M = nf_get_M(nf) */
    1709             : static GEN
    1710         664 : nfembed_i(GEN M, GEN x, long k)
    1711             : {
    1712         664 :   long i, l = lg(M);
    1713         664 :   GEN z = gel(x,1);
    1714        4098 :   for (i = 2; i < l; i++) z = gadd(z, gmul(gcoeff(M,k,i), gel(x,i)));
    1715         664 :   return z;
    1716             : }
    1717             : GEN
    1718           0 : nfembed(GEN nf, GEN x, long k)
    1719             : {
    1720           0 :   pari_sp av = avma;
    1721           0 :   nf = checknf(nf);
    1722           0 :   x = nf_to_scalar_or_basis(nf,x);
    1723           0 :   if (typ(x) != t_COL) return gerepilecopy(av, x);
    1724           0 :   return gerepileupto(av, nfembed_i(nf_get_M(nf),x,k));
    1725             : }
    1726             : 
    1727             : /* x a ZC */
    1728             : static GEN
    1729      894154 : zk_embed(GEN M, GEN x, long k)
    1730             : {
    1731      894154 :   long i, l = lg(x);
    1732      894154 :   GEN z = gel(x,1); /* times M[k,1], which is 1 */
    1733     2984234 :   for (i = 2; i < l; i++) z = mpadd(z, mpmul(gcoeff(M,k,i), gel(x,i)));
    1734      894138 :   return z;
    1735             : }
    1736             : 
    1737             : /* Given floating point approximation z of sigma_k(x), decide its sign
    1738             :  * [0/+, 1/- and -1 for FAIL] */
    1739             : static long
    1740      875867 : eval_sign_embed(GEN z)
    1741             : {
    1742      875867 :   if (typ(z) == t_REAL)
    1743             :   {
    1744      875868 :     long l = realprec(z);
    1745      875868 :     if (l <= LOWDEFAULTPREC
    1746      875868 :       || (l == LOWDEFAULTPREC + 1 && !z[l-1])) return -1; /* dubious, fail */
    1747      875069 :     if (expo(z) < 16 - l) return -1; /* same */
    1748             :   }
    1749      875025 :   return (signe(z) < 1)? 1: 0;
    1750             : }
    1751             : /* return v such that (-1)^v = sign(sigma_k(x)), x primitive ZC */
    1752             : static long
    1753      779285 : eval_sign(GEN M, GEN x, long k)
    1754      779285 : { return eval_sign_embed( zk_embed(M, x, k) ); }
    1755             : 
    1756             : /* check that signs[i..#signs] == s; signs = NULL encodes "totally positive" */
    1757             : static int
    1758           0 : oksigns(long l, GEN signs, long i, long s)
    1759             : {
    1760           0 :   if (!signs) return s == 0;
    1761           0 :   for (; i < l; i++)
    1762           0 :     if (signs[i] != s) return 0;
    1763           0 :   return 1;
    1764             : }
    1765             : /* check that signs[i] = s and signs[i+1..#signs] = 1-s */
    1766             : static int
    1767           0 : oksigns2(long l, GEN signs, long i, long s)
    1768             : {
    1769           0 :   if (!signs) return s == 0 && i == l-1;
    1770           0 :   return signs[i] == s && oksigns(l, signs, i+1, 1-s);
    1771             : }
    1772             : 
    1773             : /* true nf, x a ZC (primitive for efficiency) which is not a scalar; embx its
    1774             :  * embeddings or NULL */
    1775             : static int
    1776       80246 : nfchecksigns_i(GEN nf, GEN x, GEN embx, GEN signs, GEN archp)
    1777             : {
    1778       80246 :   long l = lg(archp), i;
    1779       80246 :   GEN M = nf_get_M(nf), sarch = NULL;
    1780       80246 :   long np = -1;
    1781      126450 :   for (i = 1; i < l; i++)
    1782             :   {
    1783             :     long s;
    1784       97958 :     if (embx)
    1785       96594 :       s = eval_sign_embed(gel(embx,i));
    1786             :     else
    1787        1364 :       s = eval_sign(M, x, archp[i]);
    1788             :     /* 0 / + or 1 / -; -1 for FAIL */
    1789       97958 :     if (s < 0) /* failure */
    1790             :     {
    1791           0 :       long ni, r1 = nf_get_r1(nf);
    1792             :       GEN xi;
    1793           0 :       if (np < 0)
    1794             :       {
    1795           0 :         np = num_positive(nf, x);
    1796           0 :         if (np == 0)  return oksigns(l, signs, i, 1);
    1797           0 :         if (np == r1) return oksigns(l, signs, i, 0);
    1798           0 :         sarch = nfarchstar(nf, NULL, identity_perm(r1));
    1799             :       }
    1800           0 :       xi = set_sign_mod_divisor(nf, vecsmall_ei(r1, archp[i]), gen_1, sarch);
    1801           0 :       xi = Q_primpart(xi);
    1802           0 :       ni = num_positive(nf, nfmuli(nf,x,xi));
    1803           0 :       if (ni == 0)  return oksigns2(l, signs, i, 0);
    1804           0 :       if (ni == r1) return oksigns2(l, signs, i, 1);
    1805           0 :       s = ni < np? 0: 1;
    1806             :     }
    1807       97958 :     if (s != (signs? signs[i]: 0)) return 0;
    1808             :   }
    1809       28492 :   return 1;
    1810             : }
    1811             : static void
    1812         775 : pl_convert(GEN pl, GEN *psigns, GEN *parchp)
    1813             : {
    1814         775 :   long i, j, l = lg(pl);
    1815         775 :   GEN signs = cgetg(l, t_VECSMALL);
    1816         775 :   GEN archp = cgetg(l, t_VECSMALL);
    1817        2576 :   for (i = j = 1; i < l; i++)
    1818             :   {
    1819        1801 :     if (!pl[i]) continue;
    1820        1403 :     archp[j] = i;
    1821        1403 :     signs[j] = (pl[i] < 0)? 1: 0;
    1822        1403 :     j++;
    1823             :   }
    1824         775 :   setlg(archp, j); *parchp = archp;
    1825         775 :   setlg(signs, j); *psigns = signs;
    1826         775 : }
    1827             : /* pl : requested signs for real embeddings, 0 = no sign constraint */
    1828             : int
    1829       14719 : nfchecksigns(GEN nf, GEN x, GEN pl)
    1830             : {
    1831       14719 :   pari_sp av = avma;
    1832             :   GEN signs, archp;
    1833       14719 :   nf = checknf(nf);
    1834       14719 :   x = nf_to_scalar_or_basis(nf,x);
    1835       14719 :   if (typ(x) != t_COL)
    1836             :   {
    1837       13944 :     long i, l = lg(pl), s = gsigne(x);
    1838       27853 :     for (i = 1; i < l; i++)
    1839       13909 :       if (pl[i] && pl[i] != s) return gc_bool(av,0);
    1840       13944 :     return gc_bool(av,1);
    1841             :   }
    1842         775 :   pl_convert(pl, &signs, &archp);
    1843         775 :   return gc_bool(av, nfchecksigns_i(nf, x, NULL, signs, archp));
    1844             : }
    1845             : 
    1846             : /* signs = NULL: totally positive, else sign[i] = 0 (+) or 1 (-) */
    1847             : static GEN
    1848      163620 : get_C(GEN lambda, long l, GEN signs)
    1849             : {
    1850             :   long i;
    1851             :   GEN C, mlambda;
    1852      163620 :   if (!signs) return const_vec(l-1, lambda);
    1853      133877 :   C = cgetg(l, t_COL); mlambda = gneg(lambda);
    1854      342866 :   for (i = 1; i < l; i++) gel(C,i) = signs[i]? mlambda: lambda;
    1855      133878 :   return C;
    1856             : }
    1857             : /* signs = NULL: totally positive at archp.
    1858             :  * Assume that a t_COL x is not a scalar */
    1859             : static GEN
    1860      277349 : nfsetsigns(GEN nf, GEN signs, GEN x, GEN sarch)
    1861             : {
    1862      277349 :   long i, l = lg(sarch_get_archp(sarch));
    1863             :   GEN ex;
    1864             :   /* Is signature already correct ? */
    1865      277348 :   if (typ(x) != t_COL)
    1866             :   {
    1867      197881 :     long s = gsigne(x);
    1868      197881 :     if (!s) i = 1;
    1869      197860 :     else if (!signs)
    1870        7427 :       i = (s < 0)? 1: l;
    1871             :     else
    1872             :     {
    1873      190433 :       s = s < 0? 1: 0;
    1874      324136 :       for (i = 1; i < l; i++)
    1875      245440 :         if (signs[i] != s) break;
    1876             :     }
    1877      197881 :     ex = (i < l)? const_col(l-1, x): NULL;
    1878             :   }
    1879             :   else
    1880             :   { /* inefficient if x scalar, wrong if x = 0 */
    1881       79467 :     pari_sp av = avma;
    1882       79467 :     GEN cex, M = nf_get_M(nf), archp = sarch_get_archp(sarch);
    1883       79471 :     GEN xp = Q_primitive_part(x,&cex);
    1884       79470 :     ex = cgetg(l,t_COL);
    1885      194340 :     for (i = 1; i < l; i++) gel(ex,i) = zk_embed(M,xp,archp[i]);
    1886       79471 :     if (nfchecksigns_i(nf, xp, ex, signs, archp)) { ex = NULL; set_avma(av); }
    1887       51717 :     else if (cex) ex = RgC_Rg_mul(ex, cex); /* put back content */
    1888             :   }
    1889      277354 :   if (ex)
    1890             :   { /* If no, fix it */
    1891      163621 :     GEN MI = sarch_get_MI(sarch), F = sarch_get_F(sarch);
    1892      163620 :     GEN lambda = sarch_get_lambda(sarch);
    1893      163620 :     GEN t = RgC_sub(get_C(lambda, l, signs), ex);
    1894      163620 :     t = grndtoi(RgM_RgC_mul(MI,t), NULL);
    1895      163620 :     if (lg(F) != 1) t = ZM_ZC_mul(F, t);
    1896      163614 :     x = typ(x) == t_COL? RgC_add(t, x): RgC_Rg_add(t, x);
    1897             :   }
    1898      277344 :   return x;
    1899             : }
    1900             : /* - true nf
    1901             :  * - sarch = nfarchstar(nf, F);
    1902             :  * - x encodes a vector of signs at arch.archp: either a t_VECSMALL
    1903             :  *   (vector of signs as {0,1}-vector), NULL (totally positive at archp),
    1904             :  *   or a nonzero number field element (replaced by its signature at archp);
    1905             :  * - y is a nonzero number field element
    1906             :  * Return z = y (mod F) with signs(y, archp) = signs(x) (a {0,1}-vector).
    1907             :  * Not stack-clean */
    1908             : GEN
    1909      308890 : set_sign_mod_divisor(GEN nf, GEN x, GEN y, GEN sarch)
    1910             : {
    1911      308890 :   GEN archp = sarch_get_archp(sarch);
    1912      308890 :   if (lg(archp) == 1) return y;
    1913      275584 :   if (x && typ(x) != t_VECSMALL) x = nfsign_arch(nf, x, archp);
    1914      275584 :   return nfsetsigns(nf, x, nf_to_scalar_or_basis(nf,y), sarch);
    1915             : }
    1916             : 
    1917             : static GEN
    1918       83458 : setsigns_init(GEN nf, GEN archp, GEN F, GEN DATA)
    1919             : {
    1920       83458 :   GEN lambda, Mr = rowpermute(nf_get_M(nf), archp), MI = F? RgM_mul(Mr,F): Mr;
    1921       83458 :   lambda = gmul2n(matrixnorm(MI,DEFAULTPREC), -1);
    1922       83459 :   if (typ(lambda) != t_REAL) lambda = gmul(lambda, uutoQ(1001,1000));
    1923       83459 :   if (lg(archp) < lg(MI))
    1924             :   {
    1925       58924 :     GEN perm = gel(indexrank(MI), 2);
    1926       58922 :     if (!F) F = matid(nf_get_degree(nf));
    1927       58922 :     MI = vecpermute(MI, perm);
    1928       58922 :     F = vecpermute(F, perm);
    1929             :   }
    1930       83457 :   if (!F) F = cgetg(1,t_MAT);
    1931       83457 :   MI = RgM_inv(MI);
    1932       83460 :   return mkvec5(DATA, archp, MI, lambda, F);
    1933             : }
    1934             : /* F nonzero integral ideal in HNF (or NULL: Z_K), compute elements in 1+F
    1935             :  * whose sign matrix at archp is identity; archp in 'indices' format */
    1936             : GEN
    1937      259442 : nfarchstar(GEN nf, GEN F, GEN archp)
    1938             : {
    1939      259442 :   long nba = lg(archp) - 1;
    1940      259442 :   if (!nba) return mkvec2(cgetg(1,t_VEC), archp);
    1941       81697 :   if (F && equali1(gcoeff(F,1,1))) F = NULL;
    1942       81697 :   if (F) F = idealpseudored(F, nf_get_roundG(nf));
    1943       81683 :   return setsigns_init(nf, archp, F, const_vec(nba, gen_2));
    1944             : }
    1945             : 
    1946             : /*************************************************************************/
    1947             : /**                                                                     **/
    1948             : /**                         IDEALCHINESE                                **/
    1949             : /**                                                                     **/
    1950             : /*************************************************************************/
    1951             : static int
    1952        4206 : isprfact(GEN x)
    1953             : {
    1954             :   long i, l;
    1955             :   GEN L, E;
    1956        4206 :   if (typ(x) != t_MAT || lg(x) != 3) return 0;
    1957        4206 :   L = gel(x,1); l = lg(L);
    1958        4206 :   E = gel(x,2);
    1959       13993 :   for(i=1; i<l; i++)
    1960             :   {
    1961        9787 :     checkprid(gel(L,i));
    1962        9787 :     if (typ(gel(E,i)) != t_INT) return 0;
    1963             :   }
    1964        4206 :   return 1;
    1965             : }
    1966             : 
    1967             : /* initialize projectors mod pr[i]^e[i] for idealchinese */
    1968             : static GEN
    1969        4206 : pr_init(GEN nf, GEN fa, GEN w, GEN dw)
    1970             : {
    1971        4206 :   GEN U, E, F, FZ, L = gel(fa,1), E0 = gel(fa,2);
    1972        4206 :   long i, r = lg(L);
    1973             : 
    1974        4206 :   if (w && lg(w) != r) pari_err_TYPE("idealchinese", w);
    1975        4206 :   if (r == 1 && !dw) return cgetg(1,t_VEC);
    1976        4192 :   E = leafcopy(E0); /* do not destroy fa[2] */
    1977       13979 :   for (i = 1; i < r; i++)
    1978        9787 :     if (signe(gel(E,i)) < 0) gel(E,i) = gen_0;
    1979        4192 :   F = factorbackprime(nf, L, E);
    1980        4192 :   if (dw)
    1981             :   {
    1982         693 :     F = ZM_Z_mul(F, dw);
    1983        1596 :     for (i = 1; i < r; i++)
    1984             :     {
    1985         903 :       GEN pr = gel(L,i);
    1986         903 :       long e = itos(gel(E0,i)), v = idealval(nf, dw, pr);
    1987         903 :       if (e >= 0)
    1988         896 :         gel(E,i) = addiu(gel(E,i), v);
    1989           7 :       else if (v + e <= 0)
    1990           0 :         F = idealmulpowprime(nf, F, pr, stoi(-v)); /* coprime to pr */
    1991             :       else
    1992             :       {
    1993           7 :         F = idealmulpowprime(nf, F, pr, stoi(e));
    1994           7 :         gel(E,i) = stoi(v + e);
    1995             :       }
    1996             :     }
    1997             :   }
    1998        4192 :   U = cgetg(r, t_VEC);
    1999       13979 :   for (i = 1; i < r; i++)
    2000             :   {
    2001             :     GEN u;
    2002        9787 :     if (w && gequal0(gel(w,i))) u = gen_0; /* unused */
    2003             :     else
    2004             :     {
    2005        9710 :       GEN pr = gel(L,i), e = gel(E,i), t;
    2006        9710 :       t = idealdivpowprime(nf,F, pr, e);
    2007        9710 :       u = hnfmerge_get_1(t, idealpow(nf, pr, e));
    2008        9710 :       if (!u) pari_err_COPRIME("idealchinese", t,pr);
    2009             :     }
    2010        9787 :     gel(U,i) = u;
    2011             :   }
    2012        4192 :   FZ = gcoeff(F, 1, 1);
    2013        4192 :   F = idealpseudored(F, nf_get_roundG(nf));
    2014        4192 :   return mkvec2(mkvec2(F, FZ), U);
    2015             : }
    2016             : 
    2017             : static GEN
    2018        2261 : pl_normalize(GEN nf, GEN pl)
    2019             : {
    2020        2261 :   const char *fun = "idealchinese";
    2021        2261 :   if (lg(pl)-1 != nf_get_r1(nf)) pari_err_TYPE(fun,pl);
    2022        2261 :   switch(typ(pl))
    2023             :   {
    2024         707 :     case t_VEC: RgV_check_ZV(pl,fun); pl = ZV_to_zv(pl);
    2025             :       /* fall through */
    2026        2261 :     case t_VECSMALL: break;
    2027           0 :     default: pari_err_TYPE(fun,pl);
    2028             :   }
    2029        2261 :   return pl;
    2030             : }
    2031             : 
    2032             : static int
    2033        9443 : is_chineseinit(GEN x)
    2034             : {
    2035             :   GEN fa, pl;
    2036             :   long l;
    2037        9443 :   if (typ(x) != t_VEC || lg(x)!=3) return 0;
    2038        7602 :   fa = gel(x,1);
    2039        7602 :   pl = gel(x,2);
    2040        7602 :   if (typ(fa) != t_VEC || typ(pl) != t_VEC) return 0;
    2041        4207 :   l = lg(fa);
    2042        4207 :   if (l != 1)
    2043             :   {
    2044             :     GEN z;
    2045        4165 :     if (l != 3) return 0;
    2046        4165 :     z = gel(fa, 1);
    2047        4165 :     if (typ(z) != t_VEC || lg(z) != 3 || typ(gel(z,1)) != t_MAT
    2048        4158 :                         || typ(gel(z,2)) != t_INT
    2049        4158 :                         || typ(gel(fa,2)) != t_VEC)
    2050           7 :       return 0;
    2051             :   }
    2052        4200 :   l = lg(pl);
    2053        4200 :   if (l != 1)
    2054             :   {
    2055         665 :     if (l != 6 || typ(gel(pl,3)) != t_MAT || typ(gel(pl,1)) != t_VECSMALL
    2056         665 :                                           || typ(gel(pl,2)) != t_VECSMALL)
    2057           0 :       return 0;
    2058             :   }
    2059        4200 :   return 1;
    2060             : }
    2061             : 
    2062             : /* nf a true 'nf' */
    2063             : static GEN
    2064        4661 : chineseinit_i(GEN nf, GEN fa, GEN w, GEN dw)
    2065             : {
    2066        4661 :   const char *fun = "idealchineseinit";
    2067        4661 :   GEN archp = NULL, pl = NULL;
    2068        4661 :   switch(typ(fa))
    2069             :   {
    2070        2261 :     case t_VEC:
    2071        2261 :       if (is_chineseinit(fa))
    2072             :       {
    2073           0 :         if (dw) pari_err_DOMAIN(fun, "denom(y)", "!=", gen_1, w);
    2074           0 :         return fa;
    2075             :       }
    2076        2261 :       if (lg(fa) != 3) pari_err_TYPE(fun, fa);
    2077             :       /* of the form [x,s] */
    2078        2261 :       pl = pl_normalize(nf, gel(fa,2));
    2079        2261 :       fa = gel(fa,1);
    2080        2261 :       archp = vecsmall01_to_indices(pl);
    2081             :       /* keep pr_init, reset pl */
    2082        2261 :       if (is_chineseinit(fa)) { fa = gel(fa,1); break; }
    2083             :       /* fall through */
    2084             :     case t_MAT: /* factorization? */
    2085        4206 :       if (isprfact(fa)) { fa = pr_init(nf, fa, w, dw); break; }
    2086           0 :     default: pari_err_TYPE(fun,fa);
    2087             :   }
    2088             : 
    2089        4661 :   if (!pl) pl = cgetg(1,t_VEC);
    2090             :   else
    2091             :   {
    2092        2261 :     long r = lg(archp);
    2093        2261 :     if (r == 1) pl = cgetg(1, t_VEC);
    2094             :     else
    2095             :     {
    2096        1757 :       GEN F = (lg(fa) == 1)? NULL: gmael(fa,1,1), signs = cgetg(r, t_VECSMALL);
    2097             :       long i;
    2098        5082 :       for (i = 1; i < r; i++) signs[i] = (pl[archp[i]] < 0)? 1: 0;
    2099        1757 :       pl = setsigns_init(nf, archp, F, signs);
    2100             :     }
    2101             :   }
    2102        4661 :   return mkvec2(fa, pl);
    2103             : }
    2104             : 
    2105             : /* Given a prime ideal factorization x, possibly with 0 or negative exponents,
    2106             :  * and a vector w of elements of nf, gives b such that
    2107             :  * v_p(b-w_p)>=v_p(x) for all prime ideals p in the ideal factorization
    2108             :  * and v_p(b)>=0 for all other p, using the standard proof given in GTM 138. */
    2109             : GEN
    2110        8406 : idealchinese(GEN nf, GEN x0, GEN w)
    2111             : {
    2112        8406 :   const char *fun = "idealchinese";
    2113        8406 :   pari_sp av = avma;
    2114        8406 :   GEN x = x0, x1, x2, s, dw, F;
    2115             : 
    2116        8406 :   nf = checknf(nf);
    2117        8406 :   if (!w) return gerepilecopy(av, chineseinit_i(nf,x,NULL,NULL));
    2118             : 
    2119        4921 :   if (typ(w) != t_VEC) pari_err_TYPE(fun,w);
    2120        4921 :   w = Q_remove_denom(matalgtobasis(nf,w), &dw);
    2121        4921 :   if (!is_chineseinit(x)) x = chineseinit_i(nf,x,w,dw);
    2122             :   /* x is a 'chineseinit' */
    2123        4921 :   x1 = gel(x,1); s = NULL;
    2124        4921 :   x2 = gel(x,2);
    2125        4921 :   if (lg(x1) == 1) { F = NULL; dw = NULL; }
    2126             :   else
    2127             :   {
    2128        4879 :     GEN  U = gel(x1,2), FZ;
    2129        4879 :     long i, r = lg(w);
    2130        4879 :     F = gmael(x1,1,1); FZ = gmael(x1,1,2);
    2131       17624 :     for (i=1; i<r; i++)
    2132       12745 :       if (!ZV_equal0(gel(w,i)))
    2133             :       {
    2134        9640 :         GEN t = nfmuli(nf, gel(U,i), gel(w,i));
    2135        9640 :         s = s? ZC_add(s,t): t;
    2136             :       }
    2137        4879 :     if (s)
    2138             :     {
    2139        4858 :       s = ZC_reducemodmatrix(s, F);
    2140        4858 :       if (dw && x == x0) /* input was a chineseinit */
    2141             :       {
    2142           7 :         dw = modii(dw, FZ);
    2143           7 :         s = FpC_Fp_mul(s, Fp_inv(dw, FZ), FZ);
    2144           7 :         dw = NULL;
    2145             :       }
    2146        4858 :       if (ZV_isscalar(s)) s = icopy(gel(s,1));
    2147             :     }
    2148             :   }
    2149        4921 :   if (lg(x2) != 1)
    2150             :   {
    2151        1764 :     s = nfsetsigns(nf, gel(x2,1), s? s: gen_0, x2);
    2152        1764 :     if (typ(s) == t_COL && QV_isscalar(s))
    2153             :     {
    2154         294 :       s = gel(s,1); if (!dw) s = gcopy(s);
    2155             :     }
    2156             :   }
    2157        3157 :   else if (!s) return gc_const(av, gen_0);
    2158        4872 :   return gerepileupto(av, dw? gdiv(s, dw): s);
    2159             : }
    2160             : 
    2161             : /*************************************************************************/
    2162             : /**                                                                     **/
    2163             : /**                           (Z_K/I)^*                                 **/
    2164             : /**                                                                     **/
    2165             : /*************************************************************************/
    2166             : GEN
    2167        2261 : vecsmall01_to_indices(GEN v)
    2168             : {
    2169        2261 :   long i, k, l = lg(v);
    2170        2261 :   GEN p = new_chunk(l) + l;
    2171        6636 :   for (k=1, i=l-1; i; i--)
    2172        4375 :     if (v[i]) { *--p = i; k++; }
    2173        2261 :   *--p = _evallg(k) | evaltyp(t_VECSMALL);
    2174        2261 :   set_avma((pari_sp)p); return p;
    2175             : }
    2176             : GEN
    2177     1087045 : vec01_to_indices(GEN v)
    2178             : {
    2179             :   long i, k, l;
    2180             :   GEN p;
    2181             : 
    2182     1087045 :   switch (typ(v))
    2183             :   {
    2184     1040314 :    case t_VECSMALL: return v;
    2185       46732 :    case t_VEC: break;
    2186           0 :    default: pari_err_TYPE("vec01_to_indices",v);
    2187             :   }
    2188       46732 :   l = lg(v);
    2189       46732 :   p = new_chunk(l) + l;
    2190      140553 :   for (k=1, i=l-1; i; i--)
    2191       93821 :     if (signe(gel(v,i))) { *--p = i; k++; }
    2192       46732 :   *--p = _evallg(k) | evaltyp(t_VECSMALL);
    2193       46732 :   set_avma((pari_sp)p); return p;
    2194             : }
    2195             : GEN
    2196      136892 : indices_to_vec01(GEN p, long r)
    2197             : {
    2198      136892 :   long i, l = lg(p);
    2199      136892 :   GEN v = zerovec(r);
    2200      206628 :   for (i = 1; i < l; i++) gel(v, p[i]) = gen_1;
    2201      136890 :   return v;
    2202             : }
    2203             : 
    2204             : /* return (column) vector of R1 signatures of x (0 or 1) */
    2205             : GEN
    2206     1040314 : nfsign_arch(GEN nf, GEN x, GEN arch)
    2207             : {
    2208     1040314 :   GEN sarch, M, V, archp = vec01_to_indices(arch);
    2209     1040314 :   long i, s, np, n = lg(archp)-1;
    2210             :   pari_sp av;
    2211             : 
    2212     1040314 :   if (!n) return cgetg(1,t_VECSMALL);
    2213      838711 :   if (typ(x) == t_MAT)
    2214             :   { /* factorisation */
    2215      276147 :     GEN g = gel(x,1), e = gel(x,2);
    2216      276147 :     long l = lg(g);
    2217      276147 :     V = zero_zv(n);
    2218      800487 :     for (i = 1; i < l; i++)
    2219      524339 :       if (mpodd(gel(e,i)))
    2220      430391 :         Flv_add_inplace(V, nfsign_arch(nf,gel(g,i),archp), 2);
    2221      276148 :     set_avma((pari_sp)V); return V;
    2222             :   }
    2223      562564 :   av = avma; V = cgetg(n+1,t_VECSMALL);
    2224      562564 :   x = nf_to_scalar_or_basis(nf, x);
    2225      562565 :   switch(typ(x))
    2226             :   {
    2227      179964 :     case t_INT:
    2228      179964 :       s = signe(x);
    2229      179964 :       if (!s) pari_err_DOMAIN("nfsign_arch","element","=",gen_0,x);
    2230      179964 :       set_avma(av); return const_vecsmall(n, (s < 0)? 1: 0);
    2231         644 :     case t_FRAC:
    2232         644 :       s = signe(gel(x,1));
    2233         644 :       set_avma(av); return const_vecsmall(n, (s < 0)? 1: 0);
    2234             :   }
    2235      381957 :   x = Q_primpart(x); M = nf_get_M(nf); sarch = NULL; np = -1;
    2236     1159067 :   for (i = 1; i <= n; i++)
    2237             :   {
    2238      777920 :     long s = eval_sign(M, x, archp[i]);
    2239      777914 :     if (s < 0) /* failure */
    2240             :     {
    2241         843 :       long ni, r1 = nf_get_r1(nf);
    2242             :       GEN xi;
    2243         842 :       if (np < 0)
    2244             :       {
    2245         842 :         np = num_positive(nf, x);
    2246         842 :         if (np == 0) { set_avma(av); return const_vecsmall(n, 1); }
    2247         799 :         if (np == r1){ set_avma(av); return const_vecsmall(n, 0); }
    2248         420 :         sarch = nfarchstar(nf, NULL, identity_perm(r1));
    2249             :       }
    2250         420 :       xi = set_sign_mod_divisor(nf, vecsmall_ei(r1, archp[i]), gen_1, sarch);
    2251         420 :       xi = Q_primpart(xi);
    2252         420 :       ni = num_positive(nf, nfmuli(nf,x,xi));
    2253         420 :       if (ni == 0) { set_avma(av); V = const_vecsmall(n, 1); V[i] = 0; return V; }
    2254         413 :       if (ni == r1){ set_avma(av); V = const_vecsmall(n, 0); V[i] = 1; return V; }
    2255          35 :       s = ni < np? 0: 1;
    2256             :     }
    2257      777106 :     V[i] = s;
    2258             :   }
    2259      381147 :   set_avma((pari_sp)V); return V;
    2260             : }
    2261             : static void
    2262       35483 : chk_ind(const char *s, long i, long r1)
    2263             : {
    2264       35483 :   if (i <= 0) pari_err_DOMAIN(s, "index", "<=", gen_0, stoi(i));
    2265       35469 :   if (i > r1) pari_err_DOMAIN(s, "index", ">", utoi(r1), utoi(i));
    2266       35434 : }
    2267             : static GEN
    2268      125944 : parse_embed(GEN ind, long r, const char *f)
    2269             : {
    2270             :   long l, i;
    2271      125944 :   if (!ind) return identity_perm(r);
    2272       33418 :   switch(typ(ind))
    2273             :   {
    2274          70 :     case t_INT: ind = mkvecsmall(itos(ind)); break;
    2275          84 :     case t_VEC: case t_COL: ind = vec_to_vecsmall(ind); break;
    2276       33264 :     case t_VECSMALL: break;
    2277           0 :     default: pari_err_TYPE(f, ind);
    2278             :   }
    2279       33418 :   l = lg(ind);
    2280       68852 :   for (i = 1; i < l; i++) chk_ind(f, ind[i], r);
    2281       33369 :   return ind;
    2282             : }
    2283             : GEN
    2284      124061 : nfeltsign(GEN nf, GEN x, GEN ind0)
    2285             : {
    2286      124061 :   pari_sp av = avma;
    2287             :   long i, l;
    2288             :   GEN v, ind;
    2289      124061 :   nf = checknf(nf);
    2290      124061 :   ind = parse_embed(ind0, nf_get_r1(nf), "nfeltsign");
    2291      124040 :   l = lg(ind);
    2292      124040 :   if (is_rational_t(typ(x)))
    2293             :   { /* nfsign_arch would test this, but avoid converting t_VECSMALL -> t_VEC */
    2294             :     GEN s;
    2295       30975 :     switch(gsigne(x))
    2296             :     {
    2297       16366 :       case -1:s = gen_m1; break;
    2298       14602 :       case 1: s = gen_1; break;
    2299           7 :       default: s = gen_0; break;
    2300             :     }
    2301       30975 :     set_avma(av);
    2302       30975 :     return (ind0 && typ(ind0) == t_INT)? s: const_vec(l-1, s);
    2303             :   }
    2304       93065 :   v = nfsign_arch(nf, x, ind);
    2305       93065 :   if (ind0 && typ(ind0) == t_INT) { set_avma(av); return v[1]? gen_m1: gen_1; }
    2306       93051 :   settyp(v, t_VEC);
    2307      262311 :   for (i = 1; i < l; i++) gel(v,i) = v[i]? gen_m1: gen_1;
    2308       93051 :   return gerepileupto(av, v);
    2309             : }
    2310             : 
    2311             : /* true nf */
    2312             : GEN
    2313         294 : nfeltembed_i(GEN *pnf, GEN x, GEN ind0, long prec0)
    2314             : {
    2315             :   long i, e, l, r1, r2, prec, prec1;
    2316         294 :   GEN v, ind, cx, nf = *pnf;
    2317         294 :   nf_get_sign(nf,&r1,&r2);
    2318         294 :   x = nf_to_scalar_or_basis(nf, x);
    2319         287 :   ind = parse_embed(ind0, r1+r2, "nfeltembed");
    2320         280 :   l = lg(ind);
    2321         280 :   if (typ(x) != t_COL)
    2322             :   {
    2323           0 :     if (!(ind0 && typ(ind0) == t_INT)) x = const_vec(l-1, x);
    2324           0 :     return x;
    2325             :   }
    2326         280 :   x = Q_primitive_part(x, &cx);
    2327         280 :   prec1 = prec0; e = gexpo(x);
    2328         280 :   if (e > 8) prec1 += nbits2extraprec(e);
    2329         280 :   prec = prec1;
    2330         280 :   if (nf_get_prec(nf) < prec) nf = nfnewprec_shallow(nf, prec);
    2331         280 :   v = cgetg(l, t_VEC);
    2332             :   for(;;)
    2333          41 :   {
    2334         321 :     GEN M = nf_get_M(nf);
    2335         944 :     for (i = 1; i < l; i++)
    2336             :     {
    2337         664 :       GEN t = nfembed_i(M, x, ind[i]);
    2338         664 :       long e = gexpo(t);
    2339         664 :       if (gequal0(t) || precision(t) < prec0
    2340         664 :                      || (e < 0 && prec < prec1 + nbits2extraprec(-e)) ) break;
    2341         623 :       if (cx) t = gmul(t, cx);
    2342         623 :       gel(v,i) = t;
    2343             :     }
    2344         321 :     if (i == l) break;
    2345          41 :     prec = precdbl(prec);
    2346          41 :     if (DEBUGLEVEL>1) pari_warn(warnprec,"eltnfembed", prec);
    2347          41 :     *pnf = nf = nfnewprec_shallow(nf, prec);
    2348             :   }
    2349         280 :   if (ind0 && typ(ind0) == t_INT) v = gel(v,1);
    2350         280 :   return v;
    2351             : }
    2352             : GEN
    2353         294 : nfeltembed(GEN nf, GEN x, GEN ind0, long prec0)
    2354             : {
    2355         294 :   pari_sp av = avma; nf = checknf(nf);
    2356         294 :   return gerepilecopy(av, nfeltembed_i(&nf, x, ind0, prec0));
    2357             : }
    2358             : 
    2359             : /* number of distinct roots of sigma(f) */
    2360             : GEN
    2361        1596 : nfpolsturm(GEN nf, GEN f, GEN ind0)
    2362             : {
    2363        1596 :   pari_sp av = avma;
    2364             :   long d, l, r1, single;
    2365             :   GEN ind, u, v, vr1, T, s, t;
    2366             : 
    2367        1596 :   nf = checknf(nf); T = nf_get_pol(nf); r1 = nf_get_r1(nf);
    2368        1596 :   ind = parse_embed(ind0, r1, "nfpolsturm");
    2369        1575 :   single = ind0 && typ(ind0) == t_INT;
    2370        1575 :   l = lg(ind);
    2371             : 
    2372        1575 :   if (gequal0(f)) pari_err_ROOTS0("nfpolsturm");
    2373        1568 :   if (typ(f) == t_POL && varn(f) != varn(T))
    2374             :   {
    2375        1547 :     f = RgX_nffix("nfpolsturm", T, f,1);
    2376        1547 :     if (lg(f) == 3) f = NULL;
    2377             :   }
    2378             :   else
    2379             :   {
    2380          21 :     (void)Rg_nffix("nfpolsturm", T, f, 0);
    2381          21 :     f = NULL;
    2382             :   }
    2383        1568 :   if (!f) { set_avma(av); return single? gen_0: zerovec(l-1); }
    2384        1547 :   d = degpol(f);
    2385        1547 :   if (d == 1) { set_avma(av); return single? gen_1: const_vec(l-1,gen_1); }
    2386             : 
    2387        1505 :   vr1 = const_vecsmall(l-1, 1);
    2388        1505 :   u = Q_primpart(f); s = ZV_to_zv(nfeltsign(nf, gel(u,d+2), ind));
    2389        1505 :   v = RgX_deriv(u); t = odd(d)? leafcopy(s): zv_neg(s);
    2390             :   for(;;)
    2391         182 :   {
    2392        1687 :     GEN r = RgX_neg( Q_primpart(RgX_pseudorem(u, v)) ), sr;
    2393        1687 :     long i, dr = degpol(r);
    2394        1687 :     if (dr < 0) break;
    2395        1687 :     sr = ZV_to_zv(nfeltsign(nf, gel(r,dr+2), ind));
    2396        4144 :     for (i = 1; i < l; i++)
    2397        2457 :       if (sr[i] != s[i]) { s[i] = sr[i], vr1[i]--; }
    2398        1687 :     if (odd(dr)) sr = zv_neg(sr);
    2399        4144 :     for (i = 1; i < l; i++)
    2400        2457 :       if (sr[i] != t[i]) { t[i] = sr[i], vr1[i]++; }
    2401        1687 :     if (!dr) break;
    2402         182 :     u = v; v = r;
    2403             :   }
    2404        1505 :   if (single) return gc_stoi(av,vr1[1]);
    2405        1498 :   return gerepileupto(av, zv_to_ZV(vr1));
    2406             : }
    2407             : 
    2408             : /* True nf; return the vector of signs of x; the matrix of such if x is a vector
    2409             :  * of nf elements */
    2410             : GEN
    2411       43959 : nfsign(GEN nf, GEN x)
    2412             : {
    2413             :   long i, l;
    2414             :   GEN archp, S;
    2415             : 
    2416       43959 :   archp = identity_perm( nf_get_r1(nf) );
    2417       43959 :   if (typ(x) != t_VEC) return nfsign_arch(nf, x, archp);
    2418       35938 :   l = lg(x); S = cgetg(l, t_MAT);
    2419      148062 :   for (i=1; i<l; i++) gel(S,i) = nfsign_arch(nf, gel(x,i), archp);
    2420       35937 :   return S;
    2421             : }
    2422             : 
    2423             : /* x integral elt, A integral ideal in HNF; reduce x mod A */
    2424             : static GEN
    2425     7784404 : zk_modHNF(GEN x, GEN A)
    2426     7784404 : { return (typ(x) == t_COL)?  ZC_hnfrem(x, A): modii(x, gcoeff(A,1,1)); }
    2427             : 
    2428             : /* given an element x in Z_K and an integral ideal y in HNF, coprime with x,
    2429             :    outputs an element inverse of x modulo y */
    2430             : GEN
    2431         189 : nfinvmodideal(GEN nf, GEN x, GEN y)
    2432             : {
    2433         189 :   pari_sp av = avma;
    2434         189 :   GEN a, yZ = gcoeff(y,1,1);
    2435             : 
    2436         189 :   if (equali1(yZ)) return gen_0;
    2437         189 :   x = nf_to_scalar_or_basis(nf, x);
    2438         189 :   if (typ(x) == t_INT) return gerepileupto(av, Fp_inv(x, yZ));
    2439             : 
    2440          79 :   a = hnfmerge_get_1(idealhnf_principal(nf,x), y);
    2441          79 :   if (!a) pari_err_INV("nfinvmodideal", x);
    2442          79 :   return gerepileupto(av, zk_modHNF(nfdiv(nf,a,x), y));
    2443             : }
    2444             : 
    2445             : static GEN
    2446     2685202 : nfsqrmodideal(GEN nf, GEN x, GEN id)
    2447     2685202 : { return zk_modHNF(nfsqri(nf,x), id); }
    2448             : static GEN
    2449     7266667 : nfmulmodideal(GEN nf, GEN x, GEN y, GEN id)
    2450     7266667 : { return x? zk_modHNF(nfmuli(nf,x,y), id): y; }
    2451             : /* assume x integral, k integer, A in HNF */
    2452             : GEN
    2453     5823076 : nfpowmodideal(GEN nf,GEN x,GEN k,GEN A)
    2454             : {
    2455     5823076 :   long s = signe(k);
    2456             :   pari_sp av;
    2457             :   GEN y;
    2458             : 
    2459     5823076 :   if (!s) return gen_1;
    2460     5823076 :   av = avma;
    2461     5823076 :   x = nf_to_scalar_or_basis(nf, x);
    2462     5823296 :   if (typ(x) != t_COL) return Fp_pow(x, k, gcoeff(A,1,1));
    2463     2619280 :   if (s < 0) { k = negi(k); x = nfinvmodideal(nf, x,A); }
    2464     2619280 :   if (equali1(k)) return gerepileupto(av, s > 0? zk_modHNF(x, A): x);
    2465     1148680 :   for(y = NULL;;)
    2466             :   {
    2467     3833932 :     if (mpodd(k)) y = nfmulmodideal(nf,y,x,A);
    2468     3833910 :     k = shifti(k,-1); if (!signe(k)) break;
    2469     2684870 :     x = nfsqrmodideal(nf,x,A);
    2470             :   }
    2471     1148667 :   return gerepileupto(av, y);
    2472             : }
    2473             : 
    2474             : /* a * g^n mod id */
    2475             : static GEN
    2476     4672767 : nfmulpowmodideal(GEN nf, GEN a, GEN g, GEN n, GEN id)
    2477             : {
    2478     4672767 :   return nfmulmodideal(nf, a, nfpowmodideal(nf,g,n,id), id);
    2479             : }
    2480             : 
    2481             : /* assume (num(g[i]), id) = 1 for all i. Return prod g[i]^e[i] mod id.
    2482             :  * EX = multiple of exponent of (O_K/id)^* */
    2483             : GEN
    2484     2621573 : famat_to_nf_modideal_coprime(GEN nf, GEN g, GEN e, GEN id, GEN EX)
    2485             : {
    2486     2621573 :   GEN EXo2, plus = NULL, minus = NULL, idZ = gcoeff(id,1,1);
    2487     2621573 :   long i, lx = lg(g);
    2488             : 
    2489     2621573 :   if (equali1(idZ)) return gen_1; /* id = Z_K */
    2490     2621076 :   EXo2 = (expi(EX) > 10)? shifti(EX,-1): NULL;
    2491     8283403 :   for (i = 1; i < lx; i++)
    2492             :   {
    2493     5662456 :     GEN h, n = centermodii(gel(e,i), EX, EXo2);
    2494     5661952 :     long sn = signe(n);
    2495     5661952 :     if (!sn) continue;
    2496             : 
    2497     4021256 :     h = nf_to_scalar_or_basis(nf, gel(g,i));
    2498     4021666 :     switch(typ(h))
    2499             :     {
    2500     2371154 :       case t_INT: break;
    2501           0 :       case t_FRAC:
    2502           0 :         h = Fp_div(gel(h,1), gel(h,2), idZ); break;
    2503     1650512 :       default:
    2504             :       {
    2505             :         GEN dh;
    2506     1650512 :         h = Q_remove_denom(h, &dh);
    2507     1650660 :         if (dh) h = FpC_Fp_mul(h, Fp_inv(dh,idZ), idZ);
    2508             :       }
    2509             :     }
    2510     4021722 :     if (sn > 0)
    2511     4019831 :       plus = nfmulpowmodideal(nf, plus, h, n, id);
    2512             :     else /* sn < 0 */
    2513        1891 :       minus = nfmulpowmodideal(nf, minus, h, negi(n), id);
    2514             :   }
    2515     2620947 :   if (minus) plus = nfmulmodideal(nf, plus, nfinvmodideal(nf,minus,id), id);
    2516     2621060 :   return plus? plus: gen_1;
    2517             : }
    2518             : 
    2519             : /* given 2 integral ideals x, y in HNF s.t x | y | x^2, compute (1+x)/(1+y) in
    2520             :  * the form [[cyc],[gen], U], where U := ux^-1 as a pair [ZM, denom(U)] */
    2521             : static GEN
    2522      236898 : zidealij(GEN x, GEN y)
    2523             : {
    2524      236898 :   GEN U, G, cyc, xp = gcoeff(x,1,1), xi = hnf_invscale(x, xp);
    2525             :   long j, N;
    2526             : 
    2527             :   /* x^(-1) y = relations between the 1 + x_i (HNF) */
    2528      236881 :   cyc = ZM_snf_group(ZM_Z_divexact(ZM_mul(xi, y), xp), &U, &G);
    2529      236880 :   N = lg(cyc); G = ZM_mul(x,G); settyp(G, t_VEC); /* new generators */
    2530      573719 :   for (j=1; j<N; j++)
    2531             :   {
    2532      336883 :     GEN c = gel(G,j);
    2533      336883 :     gel(c,1) = addiu(gel(c,1), 1); /* 1 + g_j */
    2534      336852 :     if (ZV_isscalar(c)) gel(G,j) = gel(c,1);
    2535             :   }
    2536      236836 :   return mkvec4(cyc, G, ZM_mul(U,xi), xp);
    2537             : }
    2538             : 
    2539             : /* lg(x) > 1, x + 1; shallow */
    2540             : static GEN
    2541      169710 : ZC_add1(GEN x)
    2542             : {
    2543      169710 :   long i, l = lg(x);
    2544      169710 :   GEN y = cgetg(l, t_COL);
    2545      396245 :   for (i = 2; i < l; i++) gel(y,i) = gel(x,i);
    2546      169713 :   gel(y,1) = addiu(gel(x,1), 1); return y;
    2547             : }
    2548             : /* lg(x) > 1, x - 1; shallow */
    2549             : static GEN
    2550       70497 : ZC_sub1(GEN x)
    2551             : {
    2552       70497 :   long i, l = lg(x);
    2553       70497 :   GEN y = cgetg(l, t_COL);
    2554      176939 :   for (i = 2; i < l; i++) gel(y,i) = gel(x,i);
    2555       70497 :   gel(y,1) = subiu(gel(x,1), 1); return y;
    2556             : }
    2557             : 
    2558             : /* x,y are t_INT or ZC */
    2559             : static GEN
    2560           0 : zkadd(GEN x, GEN y)
    2561             : {
    2562           0 :   long tx = typ(x);
    2563           0 :   if (tx == typ(y))
    2564           0 :     return tx == t_INT? addii(x,y): ZC_add(x,y);
    2565             :   else
    2566           0 :     return tx == t_INT? ZC_Z_add(y,x): ZC_Z_add(x,y);
    2567             : }
    2568             : /* x a t_INT or ZC, x+1; shallow */
    2569             : static GEN
    2570      255358 : zkadd1(GEN x)
    2571             : {
    2572      255358 :   long tx = typ(x);
    2573      255358 :   return tx == t_INT? addiu(x,1): ZC_add1(x);
    2574             : }
    2575             : /* x a t_INT or ZC, x-1; shallow */
    2576             : static GEN
    2577      255416 : zksub1(GEN x)
    2578             : {
    2579      255416 :   long tx = typ(x);
    2580      255416 :   return tx == t_INT? subiu(x,1): ZC_sub1(x);
    2581             : }
    2582             : /* x,y are t_INT or ZC; x - y */
    2583             : static GEN
    2584           0 : zksub(GEN x, GEN y)
    2585             : {
    2586           0 :   long tx = typ(x), ty = typ(y);
    2587           0 :   if (tx == ty)
    2588           0 :     return tx == t_INT? subii(x,y): ZC_sub(x,y);
    2589             :   else
    2590           0 :     return tx == t_INT? Z_ZC_sub(x,y): ZC_Z_sub(x,y);
    2591             : }
    2592             : /* x is t_INT or ZM (mult. map), y is t_INT or ZC; x * y */
    2593             : static GEN
    2594      255357 : zkmul(GEN x, GEN y)
    2595             : {
    2596      255357 :   long tx = typ(x), ty = typ(y);
    2597      255357 :   if (ty == t_INT)
    2598      184895 :     return tx == t_INT? mulii(x,y): ZC_Z_mul(gel(x,1),y);
    2599             :   else
    2600       70462 :     return tx == t_INT? ZC_Z_mul(y,x): ZM_ZC_mul(x,y);
    2601             : }
    2602             : 
    2603             : /* (U,V) = 1 coprime ideals. Want z = x mod U, = y mod V; namely
    2604             :  * z =vx + uy = v(x-y) + y, where u + v = 1, u in U, v in V.
    2605             :  * zkc = [v, UV], v a t_INT or ZM (mult. by v map), UV a ZM (ideal in HNF);
    2606             :  * shallow */
    2607             : GEN
    2608           0 : zkchinese(GEN zkc, GEN x, GEN y)
    2609             : {
    2610           0 :   GEN v = gel(zkc,1), UV = gel(zkc,2), z = zkadd(zkmul(v, zksub(x,y)), y);
    2611           0 :   return zk_modHNF(z, UV);
    2612             : }
    2613             : /* special case z = x mod U, = 1 mod V; shallow */
    2614             : GEN
    2615      255414 : zkchinese1(GEN zkc, GEN x)
    2616             : {
    2617      255414 :   GEN v = gel(zkc,1), UV = gel(zkc,2), z = zkadd1(zkmul(v, zksub1(x)));
    2618      255388 :   return (typ(z) == t_INT)? z: ZC_hnfrem(z, UV);
    2619             : }
    2620             : static GEN
    2621      237390 : zkVchinese1(GEN zkc, GEN v)
    2622             : {
    2623             :   long i, ly;
    2624      237390 :   GEN y = cgetg_copy(v, &ly);
    2625      492755 :   for (i=1; i<ly; i++) gel(y,i) = zkchinese1(zkc, gel(v,i));
    2626      237341 :   return y;
    2627             : }
    2628             : 
    2629             : /* prepare to solve z = x (mod A), z = y mod (B) [zkchinese or zkchinese1] */
    2630             : GEN
    2631      237153 : zkchineseinit(GEN nf, GEN A, GEN B, GEN AB)
    2632             : {
    2633      237153 :   GEN v = idealaddtoone_raw(nf, A, B);
    2634             :   long e;
    2635      237142 :   if ((e = gexpo(v)) > 5)
    2636             :   {
    2637       83282 :     GEN b = (typ(v) == t_COL)? v: scalarcol_shallow(v, nf_get_degree(nf));
    2638       83282 :     b= ZC_reducemodlll(b, AB);
    2639       83286 :     if (gexpo(b) < e) v = b;
    2640             :   }
    2641      237142 :   return mkvec2(zk_scalar_or_multable(nf,v), AB);
    2642             : }
    2643             : /* prepare to solve z = x (mod A), z = 1 mod (B)
    2644             :  * and then         z = 1 (mod A), z = y mod (B) [zkchinese1 twice] */
    2645             : static GEN
    2646         259 : zkchinese1init2(GEN nf, GEN A, GEN B, GEN AB)
    2647             : {
    2648         259 :   GEN zkc = zkchineseinit(nf, A, B, AB);
    2649         259 :   GEN mv = gel(zkc,1), mu;
    2650         259 :   if (typ(mv) == t_INT) return mkvec2(zkc, mkvec2(subui(1,mv),AB));
    2651          35 :   mu = RgM_Rg_add_shallow(ZM_neg(mv), gen_1);
    2652          35 :   return mkvec2(mkvec2(mv,AB), mkvec2(mu,AB));
    2653             : }
    2654             : 
    2655             : static GEN
    2656     2151382 : apply_U(GEN L, GEN a)
    2657             : {
    2658     2151382 :   GEN e, U = gel(L,3), dU = gel(L,4);
    2659     2151382 :   if (typ(a) == t_INT)
    2660      672124 :     e = ZC_Z_mul(gel(U,1), subiu(a, 1));
    2661             :   else
    2662             :   { /* t_COL */
    2663     1479258 :     GEN t = shallowcopy(a);
    2664     1479325 :     gel(t,1) = subiu(gel(t,1), 1); /* t = a - 1 */
    2665     1479226 :     e = ZM_ZC_mul(U, t);
    2666             :   }
    2667     2151332 :   return gdiv(e, dU);
    2668             : }
    2669             : 
    2670             : /* true nf; vectors of [[cyc],[g],U.X^-1]. Assume k > 1. */
    2671             : static GEN
    2672      169100 : principal_units(GEN nf, GEN pr, long k, GEN prk)
    2673             : {
    2674             :   GEN list, prb;
    2675      169100 :   ulong mask = quadratic_prec_mask(k);
    2676      169097 :   long a = 1;
    2677             : 
    2678      169097 :   prb = pr_hnf(nf,pr);
    2679      169099 :   list = vectrunc_init(k);
    2680      405991 :   while (mask > 1)
    2681             :   {
    2682      236899 :     GEN pra = prb;
    2683      236899 :     long b = a << 1;
    2684             : 
    2685      236899 :     if (mask & 1) b--;
    2686      236899 :     mask >>= 1;
    2687             :     /* compute 1 + pr^a / 1 + pr^b, 2a <= b */
    2688      236899 :     prb = (b >= k)? prk: idealpows(nf,pr,b);
    2689      236899 :     vectrunc_append(list, zidealij(pra, prb));
    2690      236892 :     a = b;
    2691             :   }
    2692      169092 :   return list;
    2693             : }
    2694             : /* a = 1 mod (pr) return log(a) on local-gens of 1+pr/1+pr^k */
    2695             : static GEN
    2696     1329255 : log_prk1(GEN nf, GEN a, long nh, GEN L2, GEN prk)
    2697             : {
    2698     1329255 :   GEN y = cgetg(nh+1, t_COL);
    2699     1329262 :   long j, iy, c = lg(L2)-1;
    2700     3480605 :   for (j = iy = 1; j <= c; j++)
    2701             :   {
    2702     2151377 :     GEN L = gel(L2,j), cyc = gel(L,1), gen = gel(L,2), E = apply_U(L,a);
    2703     2151230 :     long i, nc = lg(cyc)-1;
    2704     2151230 :     int last = (j == c);
    2705     5813953 :     for (i = 1; i <= nc; i++, iy++)
    2706             :     {
    2707     3662610 :       GEN t, e = gel(E,i);
    2708     3662610 :       if (typ(e) != t_INT) pari_err_COPRIME("zlog_prk1", a, prk);
    2709     3662603 :       t = Fp_neg(e, gel(cyc,i));
    2710     3662575 :       gel(y,iy) = negi(t);
    2711     3662711 :       if (!last && signe(t)) a = nfmulpowmodideal(nf, a, gel(gen,i), t, prk);
    2712             :     }
    2713             :   }
    2714     1329228 :   return y;
    2715             : }
    2716             : /* true nf */
    2717             : static GEN
    2718       56642 : principal_units_relations(GEN nf, GEN L2, GEN prk, long nh)
    2719             : {
    2720       56642 :   GEN h = cgetg(nh+1,t_MAT);
    2721       56642 :   long ih, j, c = lg(L2)-1;
    2722      181081 :   for (j = ih = 1; j <= c; j++)
    2723             :   {
    2724      124436 :     GEN L = gel(L2,j), F = gel(L,1), G = gel(L,2);
    2725      124436 :     long k, lG = lg(G);
    2726      304236 :     for (k = 1; k < lG; k++,ih++)
    2727             :     { /* log(g^f) mod pr^e */
    2728      179797 :       GEN a = nfpowmodideal(nf,gel(G,k),gel(F,k),prk);
    2729      179798 :       gel(h,ih) = ZC_neg(log_prk1(nf, a, nh, L2, prk));
    2730      179800 :       gcoeff(h,ih,ih) = gel(F,k);
    2731             :     }
    2732             :   }
    2733       56645 :   return h;
    2734             : }
    2735             : /* true nf; k > 1; multiplicative group (1 + pr) / (1 + pr^k) */
    2736             : static GEN
    2737      169098 : idealprincipalunits_i(GEN nf, GEN pr, long k, GEN *pU)
    2738             : {
    2739      169098 :   GEN cyc, gen, L2, prk = idealpows(nf, pr, k);
    2740             : 
    2741      169100 :   L2 = principal_units(nf, pr, k, prk);
    2742      169099 :   if (k == 2)
    2743             :   {
    2744      112457 :     GEN L = gel(L2,1);
    2745      112457 :     cyc = gel(L,1);
    2746      112457 :     gen = gel(L,2);
    2747      112457 :     if (pU) *pU = matid(lg(gen)-1);
    2748             :   }
    2749             :   else
    2750             :   {
    2751       56642 :     long c = lg(L2), j;
    2752       56642 :     GEN EX, h, Ui, vg = cgetg(c, t_VEC);
    2753      181077 :     for (j = 1; j < c; j++) gel(vg, j) = gmael(L2,j,2);
    2754       56642 :     vg = shallowconcat1(vg);
    2755       56642 :     h = principal_units_relations(nf, L2, prk, lg(vg)-1);
    2756       56644 :     h = ZM_hnfall_i(h, NULL, 0);
    2757       56643 :     cyc = ZM_snf_group(h, pU, &Ui);
    2758       56642 :     c = lg(Ui); gen = cgetg(c, t_VEC); EX = cyc_get_expo(cyc);
    2759      188253 :     for (j = 1; j < c; j++)
    2760      131610 :       gel(gen,j) = famat_to_nf_modideal_coprime(nf, vg, gel(Ui,j), prk, EX);
    2761             :   }
    2762      169099 :   return mkvec4(cyc, gen, prk, L2);
    2763             : }
    2764             : GEN
    2765         154 : idealprincipalunits(GEN nf, GEN pr, long k)
    2766             : {
    2767             :   pari_sp av;
    2768             :   GEN v;
    2769         154 :   nf = checknf(nf);
    2770         154 :   if (k == 1) { checkprid(pr); retmkvec3(gen_1,cgetg(1,t_VEC),cgetg(1,t_VEC)); }
    2771         147 :   av = avma; v = idealprincipalunits_i(nf, pr, k, NULL);
    2772         147 :   return gerepilecopy(av, mkvec3(powiu(pr_norm(pr), k-1), gel(v,1), gel(v,2)));
    2773             : }
    2774             : 
    2775             : /* true nf; given an ideal pr^k dividing an integral ideal x (in HNF form)
    2776             :  * compute an 'sprk', the structure of G = (Z_K/pr^k)^* [ x = NULL for x=pr^k ]
    2777             :  * Return a vector with at least 4 components [cyc],[gen],[HNF pr^k,pr,k],ff,
    2778             :  * where
    2779             :  * cyc : type of G as abelian group (SNF)
    2780             :  * gen : generators of G, coprime to x
    2781             :  * pr^k: in HNF
    2782             :  * ff  : data for log_g in (Z_K/pr)^*
    2783             :  * Two extra components are present iff k > 1: L2, U
    2784             :  * L2  : list of data structures to compute local DL in (Z_K/pr)^*,
    2785             :  *       and 1 + pr^a/ 1 + pr^b for various a < b <= min(2a, k)
    2786             :  * U   : base change matrices to convert a vector of local DL to DL wrt gen
    2787             :  * If MOD is not NULL, initialize G / G^MOD instead */
    2788             : static GEN
    2789      425838 : sprkinit(GEN nf, GEN pr, long k, GEN x, GEN MOD)
    2790             : {
    2791      425838 :   GEN T, p, Ld, modpr, cyc, gen, g, g0, A, prk, U, L2, ord0 = NULL;
    2792      425838 :   long f = pr_get_f(pr);
    2793             : 
    2794      425830 :   if(DEBUGLEVEL>3) err_printf("treating pr^%ld, pr = %Ps\n",k,pr);
    2795      425830 :   modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    2796      425856 :   if (MOD)
    2797             :   {
    2798      378312 :     GEN o = subiu(powiu(p,f), 1), d = gcdii(o, MOD), fa = Z_factor(d);
    2799      378289 :     ord0 = mkvec2(o, fa); /* true order, factorization of order in G/G^MOD */
    2800      378288 :     Ld = gel(fa,1);
    2801      378288 :     if (lg(Ld) > 1 && equaliu(gel(Ld,1),2)) Ld = vecslice(Ld,2,lg(Ld)-1);
    2802             :   }
    2803             :   /* (Z_K / pr)^* */
    2804      425827 :   if (f == 1)
    2805             :   {
    2806      336698 :     g0 = g = MOD? pgener_Fp_local(p, Ld): pgener_Fp(p);
    2807      336723 :     if (!ord0) ord0 = get_arith_ZZM(subiu(p,1));
    2808             :   }
    2809             :   else
    2810             :   {
    2811       89129 :     g0 = g = MOD? gener_FpXQ_local(T, p, Ld): gener_FpXQ(T,p, &ord0);
    2812       89130 :     g = Fq_to_nf(g, modpr);
    2813       89127 :     if (typ(g) == t_POL) g = poltobasis(nf, g);
    2814             :   }
    2815      425857 :   A = gel(ord0, 1); /* Norm(pr)-1 */
    2816             :   /* If MOD != NULL, d = gcd(A, MOD): g^(A/d) has order d */
    2817      425857 :   if (k == 1)
    2818             :   {
    2819      256905 :     cyc = mkvec(A);
    2820      256899 :     gen = mkvec(g);
    2821      256899 :     prk = pr_hnf(nf,pr);
    2822      256910 :     L2 = U = NULL;
    2823             :   }
    2824             :   else
    2825             :   { /* local-gens of (1 + pr)/(1 + pr^k) = SNF-gens * U */
    2826             :     GEN AB, B, u, v, w;
    2827             :     long j, l;
    2828      168952 :     w = idealprincipalunits_i(nf, pr, k, &U);
    2829             :     /* incorporate (Z_K/pr)^*, order A coprime to B = expo(1+pr/1+pr^k)*/
    2830      168950 :     cyc = leafcopy(gel(w,1)); B = cyc_get_expo(cyc); AB = mulii(A,B);
    2831      168939 :     gen = leafcopy(gel(w,2));
    2832      168933 :     prk = gel(w,3);
    2833      168933 :     g = nfpowmodideal(nf, g, B, prk);
    2834      168953 :     g0 = Fq_pow(g0, modii(B,A), T, p); /* update primitive root */
    2835      168942 :     L2 = mkvec3(A, g, gel(w,4));
    2836      168946 :     gel(cyc,1) = AB;
    2837      168946 :     gel(gen,1) = nfmulmodideal(nf, gel(gen,1), g, prk);
    2838      168940 :     u = mulii(Fp_inv(A,B), A);
    2839      168930 :     v = subui(1, u); l = lg(U);
    2840      505353 :     for (j = 1; j < l; j++) gcoeff(U,1,j) = Fp_mul(u, gcoeff(U,1,j), AB);
    2841      168941 :     U = mkvec2(Rg_col_ei(v, lg(gen)-1, 1), U);
    2842             :   }
    2843             :   /* local-gens of (Z_K/pr^k)^* = SNF-gens * U */
    2844      425861 :   if (x)
    2845             :   {
    2846      236902 :     GEN uv = zkchineseinit(nf, idealmulpowprime(nf,x,pr,utoineg(k)), prk, x);
    2847      236872 :     gen = zkVchinese1(uv, gen);
    2848             :   }
    2849      425800 :   return mkvecn(U? 6: 4, cyc, gen, prk, mkvec3(modpr,g0,ord0), L2, U);
    2850             : }
    2851             : GEN
    2852     3981580 : sprk_get_cyc(GEN s) { return gel(s,1); }
    2853             : GEN
    2854     1969596 : sprk_get_expo(GEN s) { return cyc_get_expo(sprk_get_cyc(s)); }
    2855             : GEN
    2856      335781 : sprk_get_gen(GEN s) { return gel(s,2); }
    2857             : GEN
    2858     4915542 : sprk_get_prk(GEN s) { return gel(s,3); }
    2859             : GEN
    2860     2543216 : sprk_get_ff(GEN s) { return gel(s,4); }
    2861             : GEN
    2862     2603885 : sprk_get_pr(GEN s) { GEN ff = gel(s,4); return modpr_get_pr(gel(ff,1)); }
    2863             : /* L2 to 1 + pr / 1 + pr^k */
    2864             : static GEN
    2865     1211715 : sprk_get_L2(GEN s) { return gmael(s,5,3); }
    2866             : /* lift to nf of primitive root of k(pr) */
    2867             : static GEN
    2868      318598 : sprk_get_gnf(GEN s) { return gmael(s,5,2); }
    2869             : /* A = Npr-1, <g> = (Z_K/pr)^*, L2 to 1 + pr / 1 + pr^k */
    2870             : void
    2871           0 : sprk_get_AgL2(GEN s, GEN *A, GEN *g, GEN *L2)
    2872           0 : { GEN v = gel(s,5); *A = gel(v,1); *g = gel(v,2); *L2 = gel(v,3); }
    2873             : void
    2874     1203040 : sprk_get_U2(GEN s, GEN *U1, GEN *U2)
    2875     1203040 : { GEN v = gel(s,6); *U1 = gel(v,1); *U2 = gel(v,2); }
    2876             : static int
    2877     2543227 : sprk_is_prime(GEN s) { return lg(s) == 5; }
    2878             : 
    2879             : GEN
    2880     1969400 : famat_zlog_pr(GEN nf, GEN g, GEN e, GEN sprk, GEN mod)
    2881             : {
    2882     1969400 :   GEN x, expo = sprk_get_expo(sprk);
    2883     1969400 :   if (mod) expo = gcdii(expo,mod);
    2884     1969377 :   x = famat_makecoprime(nf, g, e, sprk_get_pr(sprk), sprk_get_prk(sprk), expo);
    2885     1969401 :   return log_prk(nf, x, sprk, mod);
    2886             : }
    2887             : /* famat_zlog_pr assuming (g,sprk.pr) = 1 */
    2888             : static GEN
    2889         196 : famat_zlog_pr_coprime(GEN nf, GEN g, GEN e, GEN sprk, GEN MOD)
    2890             : {
    2891         196 :   GEN x = famat_to_nf_modideal_coprime(nf, g, e, sprk_get_prk(sprk),
    2892             :                                        sprk_get_expo(sprk));
    2893         196 :   return log_prk(nf, x, sprk, MOD);
    2894             : }
    2895             : 
    2896             : /* o t_INT, O = [ord,fa] format for multiple of o (for Fq_log);
    2897             :  * return o in [ord,fa] format */
    2898             : static GEN
    2899      560560 : order_update(GEN o, GEN O)
    2900             : {
    2901      560560 :   GEN p = gmael(O,2,1), z = o, P, E;
    2902      560560 :   long i, j, l = lg(p);
    2903      560560 :   P = cgetg(l, t_COL);
    2904      560557 :   E = cgetg(l, t_COL);
    2905      619607 :   for (i = j = 1; i < l; i++)
    2906             :   {
    2907      619607 :     long v = Z_pvalrem(z, gel(p,i), &z);
    2908      619566 :     if (v)
    2909             :     {
    2910      604625 :       gel(P,j) = gel(p,i);
    2911      604625 :       gel(E,j) = utoipos(v); j++;
    2912      604644 :       if (is_pm1(z)) break;
    2913             :     }
    2914             :   }
    2915      560532 :   setlg(P, j);
    2916      560532 :   setlg(E, j); return mkvec2(o, mkmat2(P,E));
    2917             : }
    2918             : 
    2919             : /* a in Z_K (t_COL or t_INT), pr prime ideal, sprk = sprkinit(nf,pr,k,x),
    2920             :  * mod positive t_INT or NULL (meaning mod=0).
    2921             :  * return log(a) modulo mod on SNF-generators of (Z_K/pr^k)^* */
    2922             : GEN
    2923     2616995 : log_prk(GEN nf, GEN a, GEN sprk, GEN mod)
    2924             : {
    2925             :   GEN e, prk, g, U1, U2, y, ff, O, o, oN, gN,  N, T, p, modpr, pr, cyc;
    2926             : 
    2927     2616995 :   if (typ(a) == t_MAT) return famat_zlog_pr(nf, gel(a,1), gel(a,2), sprk, mod);
    2928     2543201 :   N = NULL;
    2929     2543201 :   ff = sprk_get_ff(sprk);
    2930     2543215 :   pr = gel(ff,1); /* modpr */
    2931     2543215 :   g = gN = gel(ff,2);
    2932     2543215 :   O = gel(ff,3); /* order of g = |Fq^*|, in [ord, fa] format */
    2933     2543215 :   o = oN = gel(O,1); /* order as a t_INT */
    2934     2543215 :   prk = sprk_get_prk(sprk);
    2935     2543222 :   modpr = nf_to_Fq_init(nf, &pr, &T, &p);
    2936     2543238 :   if (mod)
    2937             :   {
    2938     2026883 :     GEN d = gcdii(o,mod);
    2939     2026628 :     if (!equalii(o, d))
    2940             :     {
    2941      750769 :       N = diviiexact(o,d); /* > 1, coprime to p */
    2942      750732 :       a = nfpowmodideal(nf, a, N, prk);
    2943      750895 :       oN = d; /* order of g^N mod pr */
    2944             :     }
    2945             :   }
    2946     2543056 :   if (equali1(oN))
    2947      397523 :     e = gen_0;
    2948             :   else
    2949             :   {
    2950     2145621 :     if (N) { O = order_update(oN, O); gN = Fq_pow(g, N, T, p); }
    2951     2145622 :     e = Fq_log(nf_to_Fq(nf,a,modpr), gN, O, T, p);
    2952             :   }
    2953             :   /* 0 <= e < oN is correct modulo oN */
    2954     2543254 :   if (sprk_is_prime(sprk)) return mkcol(e); /* k = 1 */
    2955             : 
    2956      800277 :   sprk_get_U2(sprk, &U1,&U2);
    2957      800382 :   cyc = sprk_get_cyc(sprk);
    2958      800385 :   if (mod)
    2959             :   {
    2960      379028 :     cyc = ZV_snf_gcd(cyc, mod);
    2961      378996 :     if (signe(remii(mod,p))) return ZV_ZV_mod(ZC_Z_mul(U1,e), cyc);
    2962             :   }
    2963      746751 :   if (signe(e))
    2964             :   {
    2965      318598 :     GEN E = N? mulii(e, N): e;
    2966      318598 :     a = nfmulpowmodideal(nf, a, sprk_get_gnf(sprk), Fp_neg(E, o), prk);
    2967             :   }
    2968             :   /* a = 1 mod pr */
    2969      746751 :   y = log_prk1(nf, a, lg(U2)-1, sprk_get_L2(sprk), prk);
    2970      746787 :   if (N)
    2971             :   { /* from DL(a^N) to DL(a) */
    2972      135235 :     GEN E = gel(sprk_get_cyc(sprk), 1), q = powiu(p, Z_pval(E, p));
    2973      135235 :     y = ZC_Z_mul(y, Fp_inv(N, q));
    2974             :   }
    2975      746787 :   y = ZC_lincomb(gen_1, e, ZM_ZC_mul(U2,y), U1);
    2976      746795 :   return ZV_ZV_mod(y, cyc);
    2977             : }
    2978             : /* true nf */
    2979             : GEN
    2980       90118 : log_prk_init(GEN nf, GEN pr, long k, GEN MOD)
    2981       90118 : { return sprkinit(nf,pr,k,NULL,MOD);}
    2982             : GEN
    2983         497 : veclog_prk(GEN nf, GEN v, GEN sprk)
    2984             : {
    2985         497 :   long l = lg(v), i;
    2986         497 :   GEN w = cgetg(l, t_MAT);
    2987        1232 :   for (i = 1; i < l; i++) gel(w,i) = log_prk(nf, gel(v,i), sprk, NULL);
    2988         497 :   return w;
    2989             : }
    2990             : 
    2991             : static GEN
    2992     1374216 : famat_zlog(GEN nf, GEN fa, GEN sgn, zlog_S *S)
    2993             : {
    2994     1374216 :   long i, l0, l = lg(S->U);
    2995     1374216 :   GEN g = gel(fa,1), e = gel(fa,2), y = cgetg(l, t_COL);
    2996     1374216 :   l0 = lg(S->sprk); /* = l (trivial arch. part), or l-1 */
    2997     2852342 :   for (i=1; i < l0; i++) gel(y,i) = famat_zlog_pr(nf, g, e, gel(S->sprk,i), S->mod);
    2998     1374220 :   if (l0 != l)
    2999             :   {
    3000      190754 :     if (!sgn) sgn = nfsign_arch(nf, fa, S->archp);
    3001      190755 :     gel(y,l0) = Flc_to_ZC(sgn);
    3002             :   }
    3003     1374221 :   return y;
    3004             : }
    3005             : 
    3006             : /* assume that cyclic factors are normalized, in particular != [1] */
    3007             : static GEN
    3008      257415 : split_U(GEN U, GEN Sprk)
    3009             : {
    3010      257415 :   long t = 0, k, n, l = lg(Sprk);
    3011      257415 :   GEN vU = cgetg(l+1, t_VEC);
    3012      592443 :   for (k = 1; k < l; k++)
    3013             :   {
    3014      335020 :     n = lg(sprk_get_cyc(gel(Sprk,k))) - 1; /* > 0 */
    3015      335022 :     gel(vU,k) = vecslice(U, t+1, t+n);
    3016      335031 :     t += n;
    3017             :   }
    3018             :   /* t+1 .. lg(U)-1 */
    3019      257423 :   n = lg(U) - t - 1; /* can be 0 */
    3020      257423 :   if (!n) setlg(vU,l); else gel(vU,l) = vecslice(U, t+1, t+n);
    3021      257426 :   return vU;
    3022             : }
    3023             : 
    3024             : static void
    3025     1990540 : init_zlog_mod(zlog_S *S, GEN bid, GEN mod)
    3026             : {
    3027     1990540 :   GEN fa2 = bid_get_fact2(bid), MOD = bid_get_MOD(bid);
    3028     1990541 :   S->U = bid_get_U(bid);
    3029     1990541 :   S->hU = lg(bid_get_cyc(bid))-1;
    3030     1990535 :   S->archp = bid_get_archp(bid);
    3031     1990531 :   S->sprk = bid_get_sprk(bid);
    3032     1990529 :   S->bid = bid;
    3033     1990529 :   if (MOD) mod = mod? gcdii(mod, MOD): MOD;
    3034     1990427 :   S->mod = mod;
    3035     1990427 :   S->P = gel(fa2,1);
    3036     1990427 :   S->k = gel(fa2,2);
    3037     1990427 :   S->no2 = lg(S->P) == lg(gel(bid_get_fact(bid),1));
    3038     1990446 : }
    3039             : void
    3040      380068 : init_zlog(zlog_S *S, GEN bid)
    3041             : {
    3042      380068 :   return init_zlog_mod(S, bid, NULL);
    3043             : }
    3044             : 
    3045             : /* a a t_FRAC/t_INT, reduce mod bid */
    3046             : static GEN
    3047          14 : Q_mod_bid(GEN bid, GEN a)
    3048             : {
    3049          14 :   GEN xZ = gcoeff(bid_get_ideal(bid),1,1);
    3050          14 :   GEN b = Rg_to_Fp(a, xZ);
    3051          14 :   if (gsigne(a) < 0) b = subii(b, xZ);
    3052          14 :   return signe(b)? b: xZ;
    3053             : }
    3054             : /* Return decomposition of a on the CRT generators blocks attached to the
    3055             :  * S->sprk and sarch; sgn = sign(a, S->arch), NULL if unknown */
    3056             : static GEN
    3057      381281 : zlog(GEN nf, GEN a, GEN sgn, zlog_S *S)
    3058             : {
    3059             :   long k, l;
    3060             :   GEN y;
    3061      381281 :   a = nf_to_scalar_or_basis(nf, a);
    3062      381260 :   switch(typ(a))
    3063             :   {
    3064      162478 :     case t_INT: break;
    3065          14 :     case t_FRAC: a = Q_mod_bid(S->bid, a); break;
    3066      218768 :     default: /* case t_COL: */
    3067             :     {
    3068             :       GEN den;
    3069      218768 :       check_nfelt(a, &den);
    3070      218791 :       if (den)
    3071             :       {
    3072         105 :         a = Q_muli_to_int(a, den);
    3073         105 :         a = mkmat2(mkcol2(a, den), mkcol2(gen_1, gen_m1));
    3074         105 :         return famat_zlog(nf, a, sgn, S);
    3075             :       }
    3076             :     }
    3077             :   }
    3078      381171 :   if (sgn)
    3079      374213 :     sgn = (lg(sgn) == 1)? NULL: leafcopy(sgn);
    3080             :   else
    3081        6958 :     sgn = (lg(S->archp) == 1)? NULL: nfsign_arch(nf, a, S->archp);
    3082      381180 :   l = lg(S->sprk);
    3083      381180 :   y = cgetg(sgn? l+1: l, t_COL);
    3084      922290 :   for (k = 1; k < l; k++)
    3085             :   {
    3086      541130 :     GEN sprk = gel(S->sprk,k);
    3087      541130 :     gel(y,k) = log_prk(nf, a, sprk, S->mod);
    3088             :   }
    3089      381160 :   if (sgn) gel(y,l) = Flc_to_ZC(sgn);
    3090      381166 :   return y;
    3091             : }
    3092             : 
    3093             : /* true nf */
    3094             : GEN
    3095       43813 : pr_basis_perm(GEN nf, GEN pr)
    3096             : {
    3097       43813 :   long f = pr_get_f(pr);
    3098             :   GEN perm;
    3099       43813 :   if (f == nf_get_degree(nf)) return identity_perm(f);
    3100       38164 :   perm = cgetg(f+1, t_VECSMALL);
    3101       38164 :   perm[1] = 1;
    3102       38164 :   if (f > 1)
    3103             :   {
    3104        2912 :     GEN H = pr_hnf(nf,pr);
    3105             :     long i, k;
    3106       10808 :     for (i = k = 2; k <= f; i++)
    3107        7896 :       if (!equali1(gcoeff(H,i,i))) perm[k++] = i;
    3108             :   }
    3109       38164 :   return perm;
    3110             : }
    3111             : 
    3112             : /* \sum U[i]*y[i], U[i] ZM, y[i] ZC. We allow lg(y) > lg(U). */
    3113             : static GEN
    3114     1755465 : ZMV_ZCV_mul(GEN U, GEN y)
    3115             : {
    3116     1755465 :   long i, l = lg(U);
    3117     1755465 :   GEN z = NULL;
    3118     1755465 :   if (l == 1) return cgetg(1,t_COL);
    3119     4139631 :   for (i = 1; i < l; i++)
    3120             :   {
    3121     2384254 :     GEN u = ZM_ZC_mul(gel(U,i), gel(y,i));
    3122     2384199 :     z = z? ZC_add(z, u): u;
    3123             :   }
    3124     1755377 :   return z;
    3125             : }
    3126             : /* A * (U[1], ..., U[d] */
    3127             : static GEN
    3128         518 : ZM_ZMV_mul(GEN A, GEN U)
    3129             : {
    3130             :   long i, l;
    3131         518 :   GEN V = cgetg_copy(U,&l);
    3132        1057 :   for (i = 1; i < l; i++) gel(V,i) = ZM_mul(A,gel(U,i));
    3133         518 :   return V;
    3134             : }
    3135             : 
    3136             : /* a = 1 mod pr, sprk mod pr^e, e >= 1 */
    3137             : static GEN
    3138      402692 : sprk_log_prk1_2(GEN nf, GEN a, GEN sprk)
    3139             : {
    3140      402692 :   GEN U1, U2, y, L2 = sprk_get_L2(sprk);
    3141      402691 :   sprk_get_U2(sprk, &U1,&U2);
    3142      402691 :   y = ZM_ZC_mul(U2, log_prk1(nf, a, lg(U2)-1, L2, sprk_get_prk(sprk)));
    3143      402688 :   return ZV_ZV_mod(y, sprk_get_cyc(sprk));
    3144             : }
    3145             : /* true nf; assume e >= 2 */
    3146             : GEN
    3147      105838 : sprk_log_gen_pr2(GEN nf, GEN sprk, long e)
    3148             : {
    3149      105838 :   GEN M, G, pr = sprk_get_pr(sprk);
    3150             :   long i, l;
    3151      105838 :   if (e == 2)
    3152             :   {
    3153       62279 :     GEN L2 = sprk_get_L2(sprk), L = gel(L2,1);
    3154       62279 :     G = gel(L,2); l = lg(G);
    3155             :   }
    3156             :   else
    3157             :   {
    3158       43559 :     GEN perm = pr_basis_perm(nf,pr), PI = nfpow_u(nf, pr_get_gen(pr), e-1);
    3159       43560 :     l = lg(perm);
    3160       43560 :     G = cgetg(l, t_VEC);
    3161       43560 :     if (typ(PI) == t_INT)
    3162             :     { /* zk_ei_mul doesn't allow t_INT */
    3163        5641 :       long N = nf_get_degree(nf);
    3164        5642 :       gel(G,1) = addiu(PI,1);
    3165        8645 :       for (i = 2; i < l; i++)
    3166             :       {
    3167        3003 :         GEN z = col_ei(N, 1);
    3168        3003 :         gel(G,i) = z; gel(z, perm[i]) = PI;
    3169             :       }
    3170             :     }
    3171             :     else
    3172             :     {
    3173       37919 :       gel(G,1) = nfadd(nf, gen_1, PI);
    3174       44702 :       for (i = 2; i < l; i++)
    3175        6783 :         gel(G,i) = nfadd(nf, gen_1, zk_ei_mul(nf, PI, perm[i]));
    3176             :     }
    3177             :   }
    3178      105840 :   M = cgetg(l, t_MAT);
    3179      234348 :   for (i = 1; i < l; i++) gel(M,i) = sprk_log_prk1_2(nf, gel(G,i), sprk);
    3180      105828 :   return M;
    3181             : }
    3182             : /* Log on bid.gen of generators of P_{1,I pr^{e-1}} / P_{1,I pr^e} (I,pr) = 1,
    3183             :  * defined implicitly via CRT. 'ind' is the index of pr in modulus
    3184             :  * factorization; true nf */
    3185             : GEN
    3186      413816 : log_gen_pr(zlog_S *S, long ind, GEN nf, long e)
    3187             : {
    3188      413816 :   GEN Uind = gel(S->U, ind);
    3189      413816 :   if (e == 1) retmkmat( gel(Uind,1) );
    3190      103142 :   return ZM_mul(Uind, sprk_log_gen_pr2(nf, gel(S->sprk,ind), e));
    3191             : }
    3192             : /* true nf */
    3193             : GEN
    3194        2037 : sprk_log_gen_pr(GEN nf, GEN sprk, long e)
    3195             : {
    3196        2037 :   if (e == 1)
    3197             :   {
    3198           0 :     long n = lg(sprk_get_cyc(sprk))-1;
    3199           0 :     retmkmat(col_ei(n, 1));
    3200             :   }
    3201        2037 :   return sprk_log_gen_pr2(nf, sprk, e);
    3202             : }
    3203             : /* a = 1 mod pr */
    3204             : GEN
    3205      274172 : sprk_log_prk1(GEN nf, GEN a, GEN sprk)
    3206             : {
    3207      274172 :   if (lg(sprk) == 5) return mkcol(gen_0); /* mod pr */
    3208      274172 :   return sprk_log_prk1_2(nf, a, sprk);
    3209             : }
    3210             : /* Log on bid.gen of generator of P_{1,f} / P_{1,f v[index]}
    3211             :  * v = vector of r1 real places */
    3212             : GEN
    3213       86248 : log_gen_arch(zlog_S *S, long index) { return gel(veclast(S->U), index); }
    3214             : 
    3215             : /* compute bid.clgp: [h,cyc] or [h,cyc,gen] */
    3216             : static GEN
    3217      258434 : bid_grp(GEN nf, GEN U, GEN cyc, GEN g, GEN F, GEN sarch)
    3218             : {
    3219      258434 :   GEN G, h = ZV_prod(cyc);
    3220             :   long c;
    3221      258443 :   if (!U) return mkvec2(h,cyc);
    3222      258086 :   c = lg(U);
    3223      258086 :   G = cgetg(c,t_VEC);
    3224      258090 :   if (c > 1)
    3225             :   {
    3226      228014 :     GEN U0, Uoo, EX = cyc_get_expo(cyc); /* exponent of bid */
    3227      228014 :     long i, hU = nbrows(U), nba = lg(sarch_get_cyc(sarch))-1; /* #f_oo */
    3228      228025 :     if (!nba) { U0 = U; Uoo = NULL; }
    3229       80416 :     else if (nba == hU) { U0 = NULL; Uoo = U; }
    3230             :     else
    3231             :     {
    3232       71274 :       U0 = rowslice(U, 1, hU-nba);
    3233       71278 :       Uoo = rowslice(U, hU-nba+1, hU);
    3234             :     }
    3235      695412 :     for (i = 1; i < c; i++)
    3236             :     {
    3237      467396 :       GEN t = gen_1;
    3238      467396 :       if (U0) t = famat_to_nf_modideal_coprime(nf, g, gel(U0,i), F, EX);
    3239      467382 :       if (Uoo) t = set_sign_mod_divisor(nf, ZV_to_Flv(gel(Uoo,i),2), t, sarch);
    3240      467385 :       gel(G,i) = t;
    3241             :     }
    3242             :   }
    3243      258092 :   return mkvec3(h, cyc, G);
    3244             : }
    3245             : 
    3246             : /* remove prime ideals of norm 2 with exponent 1 from factorization */
    3247             : static GEN
    3248      258786 : famat_strip2(GEN fa)
    3249             : {
    3250      258786 :   GEN P = gel(fa,1), E = gel(fa,2), Q, F;
    3251      258786 :   long l = lg(P), i, j;
    3252      258786 :   Q = cgetg(l, t_COL);
    3253      258783 :   F = cgetg(l, t_COL);
    3254      633811 :   for (i = j = 1; i < l; i++)
    3255             :   {
    3256      375028 :     GEN pr = gel(P,i), e = gel(E,i);
    3257      375028 :     if (!absequaliu(pr_get_p(pr), 2) || itou(e) != 1 || pr_get_f(pr) != 1)
    3258             :     {
    3259      336393 :       gel(Q,j) = pr;
    3260      336393 :       gel(F,j) = e; j++;
    3261             :     }
    3262             :   }
    3263      258783 :   setlg(Q,j);
    3264      258783 :   setlg(F,j); return mkmat2(Q,F);
    3265             : }
    3266             : static int
    3267      134092 : checkarchp(GEN v, long r1)
    3268             : {
    3269      134092 :   long i, l = lg(v);
    3270      134092 :   pari_sp av = avma;
    3271             :   GEN p;
    3272      134092 :   if (l == 1) return 1;
    3273       47156 :   if (l == 2) return v[1] > 0 && v[1] <= r1;
    3274       22020 :   p = zero_zv(r1);
    3275       66150 :   for (i = 1; i < l; i++)
    3276             :   {
    3277       44163 :     long j = v[i];
    3278       44163 :     if (j <= 0 || j > r1 || p[j]) return gc_long(av, 0);
    3279       44128 :     p[j] = 1;
    3280             :   }
    3281       21987 :   return gc_long(av, 1);
    3282             : }
    3283             : 
    3284             : /* True nf. Put ideal to form [[ideal,arch]] and set fa and fa2 to its
    3285             :  * factorization, archp to the indices of arch places */
    3286             : GEN
    3287      258774 : check_mod_factored(GEN nf, GEN ideal, GEN *fa_, GEN *fa2_, GEN *archp_, GEN MOD)
    3288             : {
    3289             :   GEN arch, x, fa, fa2, archp;
    3290             :   long R1;
    3291             : 
    3292      258774 :   R1 = nf_get_r1(nf);
    3293      258777 :   if (typ(ideal) == t_VEC && lg(ideal) == 3)
    3294             :   {
    3295      178689 :     arch = gel(ideal,2);
    3296      178689 :     ideal= gel(ideal,1);
    3297      178689 :     switch(typ(arch))
    3298             :     {
    3299       44597 :       case t_VEC:
    3300       44597 :         if (lg(arch) != R1+1)
    3301           7 :           pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
    3302       44590 :         archp = vec01_to_indices(arch);
    3303       44590 :         break;
    3304      134092 :       case t_VECSMALL:
    3305      134092 :         if (!checkarchp(arch, R1))
    3306          35 :           pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
    3307      134057 :         archp = arch;
    3308      134057 :         arch = indices_to_vec01(archp, R1);
    3309      134055 :         break;
    3310           0 :       default:
    3311           0 :         pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
    3312             :         return NULL;/*LCOV_EXCL_LINE*/
    3313             :     }
    3314             :   }
    3315             :   else
    3316             :   {
    3317       80088 :     arch = zerovec(R1);
    3318       80071 :     archp = cgetg(1, t_VECSMALL);
    3319             :   }
    3320      258720 :   if (MOD)
    3321             :   {
    3322      214151 :     if (typ(MOD) != t_INT) pari_err_TYPE("bnrinit [incorrect cycmod]", MOD);
    3323      214151 :     if (mpodd(MOD) && lg(archp) != 1)
    3324         231 :       MOD = shifti(MOD, 1); /* ensure elements of G^MOD are >> 0 */
    3325             :   }
    3326      258717 :   if (is_nf_factor(ideal))
    3327             :   {
    3328       40264 :     fa = ideal;
    3329       40264 :     x = factorbackprime(nf, gel(fa,1), gel(fa,2));
    3330             :   }
    3331             :   else
    3332             :   {
    3333      218458 :     fa = idealfactor(nf, ideal);
    3334      218482 :     x = ideal;
    3335             :   }
    3336      258746 :   if (typ(x) != t_MAT) x = idealhnf_shallow(nf, x);
    3337      258729 :   if (lg(x) == 1) pari_err_DOMAIN("Idealstar", "ideal","=",gen_0,x);
    3338      258729 :   if (typ(gcoeff(x,1,1)) != t_INT)
    3339           7 :     pari_err_DOMAIN("Idealstar","denominator(ideal)", "!=",gen_1,x);
    3340             : 
    3341      258722 :   fa2 = famat_strip2(fa);
    3342      258721 :   if (fa_ != NULL) *fa_ = fa;
    3343      258721 :   if (fa2_ != NULL) *fa2_ = fa2;
    3344      258721 :   if (fa2_ != NULL) *archp_ = archp;
    3345      258721 :   return mkvec2(x, arch);
    3346             : }
    3347             : 
    3348             : /* True nf. Compute [[ideal,arch], [h,[cyc],[gen]], idealfact, [liste], U]
    3349             :    flag may include nf_GEN | nf_INIT */
    3350             : static GEN
    3351      258138 : Idealstarmod_i(GEN nf, GEN ideal, long flag, GEN MOD)
    3352             : {
    3353             :   long i, nbp;
    3354      258138 :   GEN y, cyc, U, u1 = NULL, fa, fa2, sprk, x_arch, x, arch, archp, E, P, sarch, gen;
    3355             : 
    3356      258138 :   x_arch = check_mod_factored(nf, ideal, &fa, &fa2, &archp, MOD);
    3357      258091 :   x = gel(x_arch, 1);
    3358      258091 :   arch = gel(x_arch, 2);
    3359             : 
    3360      258091 :   sarch = nfarchstar(nf, x, archp);
    3361      258101 :   P = gel(fa2,1);
    3362      258101 :   E = gel(fa2,2);
    3363      258101 :   nbp = lg(P)-1;
    3364      258101 :   sprk = cgetg(nbp+1,t_VEC);
    3365      258109 :   if (nbp)
    3366             :   {
    3367      218846 :     GEN t = (lg(gel(fa,1))==2)? NULL: x; /* beware fa != fa2 */
    3368      218846 :     cyc = cgetg(nbp+2,t_VEC);
    3369      218833 :     gen = cgetg(nbp+1,t_VEC);
    3370      554569 :     for (i = 1; i <= nbp; i++)
    3371             :     {
    3372      335714 :       GEN L = sprkinit(nf, gel(P,i), itou(gel(E,i)), t, MOD);
    3373      335718 :       gel(sprk,i) = L;
    3374      335718 :       gel(cyc,i) = sprk_get_cyc(L);
    3375             :       /* true gens are congruent to those mod x AND positive at archp */
    3376      335719 :       gel(gen,i) = sprk_get_gen(L);
    3377             :     }
    3378      218855 :     gel(cyc,i) = sarch_get_cyc(sarch);
    3379      218853 :     cyc = shallowconcat1(cyc);
    3380      218856 :     gen = shallowconcat1(gen);
    3381      218861 :     cyc = ZV_snf_group(cyc, &U, (flag & nf_GEN)? &u1: NULL);
    3382             :   }
    3383             :   else
    3384             :   {
    3385       39263 :     cyc = sarch_get_cyc(sarch);
    3386       39263 :     gen = cgetg(1,t_VEC);
    3387       39263 :     U = matid(lg(cyc)-1);
    3388       39263 :     if (flag & nf_GEN) u1 = U;
    3389             :   }
    3390      258093 :   if (MOD) cyc = ZV_snf_gcd(cyc, MOD);
    3391      258073 :   y = bid_grp(nf, u1, cyc, gen, x, sarch);
    3392      258108 :   if (!(flag & nf_INIT)) return y;
    3393      257310 :   U = split_U(U, sprk);
    3394      514637 :   return mkvec5(mkvec2(x, arch), y, mkvec2(fa,fa2),
    3395      257318 :                 MOD? mkvec3(sprk, sarch, MOD): mkvec2(sprk, sarch),
    3396             :                 U);
    3397             : }
    3398             : 
    3399             : static long
    3400          63 : idealHNF_norm_pval(GEN x, GEN p)
    3401             : {
    3402          63 :   long i, v = 0, l = lg(x);
    3403         175 :   for (i = 1; i < l; i++) v += Z_pval(gcoeff(x,i,i), p);
    3404          63 :   return v;
    3405             : }
    3406             : static long
    3407          63 : sprk_get_k(GEN sprk)
    3408             : {
    3409             :   GEN pr, prk;
    3410          63 :   if (sprk_is_prime(sprk)) return 1;
    3411          63 :   pr = sprk_get_pr(sprk);
    3412          63 :   prk = sprk_get_prk(sprk);
    3413          63 :   return idealHNF_norm_pval(prk, pr_get_p(pr)) / pr_get_f(pr);
    3414             : }
    3415             : /* true nf, L a sprk */
    3416             : GEN
    3417          63 : sprk_to_bid(GEN nf, GEN L, long flag)
    3418             : {
    3419          63 :   GEN y, cyc, U, u1 = NULL, fa, fa2, arch, sarch, gen, sprk;
    3420             : 
    3421          63 :   arch = zerovec(nf_get_r1(nf));
    3422          63 :   fa = to_famat_shallow(sprk_get_pr(L), utoipos(sprk_get_k(L)));
    3423          63 :   sarch = nfarchstar(nf, NULL, cgetg(1, t_VECSMALL));
    3424          63 :   fa2 = famat_strip2(fa);
    3425          63 :   sprk = mkvec(L);
    3426          63 :   cyc = shallowconcat(sprk_get_cyc(L), sarch_get_cyc(sarch));
    3427          63 :   gen = sprk_get_gen(L);
    3428          63 :   cyc = ZV_snf_group(cyc, &U, (flag & nf_GEN)? &u1: NULL);
    3429          63 :   y = bid_grp(nf, u1, cyc, gen, NULL, sarch);
    3430          63 :   if (!(flag & nf_INIT)) return y;
    3431          63 :   return mkvec5(mkvec2(sprk_get_prk(L), arch), y, mkvec2(fa,fa2),
    3432             :                 mkvec2(sprk, sarch), split_U(U, sprk));
    3433             : }
    3434             : GEN
    3435      257869 : Idealstarmod(GEN nf, GEN ideal, long flag, GEN MOD)
    3436             : {
    3437      257869 :   pari_sp av = avma;
    3438      257869 :   nf = nf? checknf(nf): nfinit(pol_x(0), DEFAULTPREC);
    3439      257866 :   return gerepilecopy(av, Idealstarmod_i(nf, ideal, flag, MOD));
    3440             : }
    3441             : GEN
    3442         938 : Idealstar(GEN nf, GEN ideal, long flag) { return Idealstarmod(nf, ideal, flag, NULL); }
    3443             : GEN
    3444         273 : Idealstarprk(GEN nf, GEN pr, long k, long flag)
    3445             : {
    3446         273 :   pari_sp av = avma;
    3447         273 :   GEN z = Idealstarmod_i(nf, mkmat2(mkcol(pr),mkcols(k)), flag, NULL);
    3448         273 :   return gerepilecopy(av, z);
    3449             : }
    3450             : 
    3451             : /* FIXME: obsolete */
    3452             : GEN
    3453           0 : zidealstarinitgen(GEN nf, GEN ideal)
    3454           0 : { return Idealstar(nf,ideal, nf_INIT|nf_GEN); }
    3455             : GEN
    3456           0 : zidealstarinit(GEN nf, GEN ideal)
    3457           0 : { return Idealstar(nf,ideal, nf_INIT); }
    3458             : GEN
    3459           0 : zidealstar(GEN nf, GEN ideal)
    3460           0 : { return Idealstar(nf,ideal, nf_GEN); }
    3461             : 
    3462             : GEN
    3463         112 : idealstarmod(GEN nf, GEN ideal, long flag, GEN MOD)
    3464             : {
    3465         112 :   switch(flag)
    3466             :   {
    3467           0 :     case 0: return Idealstarmod(nf,ideal, nf_GEN, MOD);
    3468          98 :     case 1: return Idealstarmod(nf,ideal, nf_INIT, MOD);
    3469          14 :     case 2: return Idealstarmod(nf,ideal, nf_INIT|nf_GEN, MOD);
    3470           0 :     default: pari_err_FLAG("idealstar");
    3471             :   }
    3472             :   return NULL; /* LCOV_EXCL_LINE */
    3473             : }
    3474             : GEN
    3475           0 : idealstar0(GEN nf, GEN ideal,long flag) { return idealstarmod(nf, ideal, flag, NULL); }
    3476             : 
    3477             : void
    3478      218779 : check_nfelt(GEN x, GEN *den)
    3479             : {
    3480      218779 :   long l = lg(x), i;
    3481      218779 :   GEN t, d = NULL;
    3482      218779 :   if (typ(x) != t_COL) pari_err_TYPE("check_nfelt", x);
    3483      808327 :   for (i=1; i<l; i++)
    3484             :   {
    3485      589542 :     t = gel(x,i);
    3486      589542 :     switch (typ(t))
    3487             :     {
    3488      589311 :       case t_INT: break;
    3489         231 :       case t_FRAC:
    3490         231 :         if (!d) d = gel(t,2); else d = lcmii(d, gel(t,2));
    3491         231 :         break;
    3492           0 :       default: pari_err_TYPE("check_nfelt", x);
    3493             :     }
    3494             :   }
    3495      218785 :   *den = d;
    3496      218785 : }
    3497             : 
    3498             : GEN
    3499     1952555 : ZV_snf_gcd(GEN x, GEN mod)
    3500     4356944 : { pari_APPLY_same(gcdii(gel(x,i), mod)); }
    3501             : 
    3502             : /* assume a true bnf and bid */
    3503             : GEN
    3504      227016 : ideallog_units0(GEN bnf, GEN bid, GEN MOD)
    3505             : {
    3506      227016 :   GEN nf = bnf_get_nf(bnf), D, y, C, cyc;
    3507      227015 :   long j, lU = lg(bnf_get_logfu(bnf)); /* r1+r2 */
    3508             :   zlog_S S;
    3509      227015 :   init_zlog_mod(&S, bid, MOD);
    3510      226990 :   if (!S.hU) return zeromat(0,lU);
    3511      226990 :   cyc = bid_get_cyc(bid);
    3512      226985 :   D = nfsign_fu(bnf, bid_get_archp(bid));
    3513      227002 :   y = cgetg(lU, t_MAT);
    3514      226999 :   if ((C = bnf_build_cheapfu(bnf)))
    3515      374173 :   { for (j = 1; j < lU; j++) gel(y,j) = zlog(nf, gel(C,j), gel(D,j), &S); }
    3516             :   else
    3517             :   {
    3518          49 :     long i, l = lg(S.U), l0 = lg(S.sprk);
    3519             :     GEN X, U;
    3520          49 :     if (!(C = bnf_compactfu_mat(bnf))) bnf_build_units(bnf); /* error */
    3521          49 :     X = gel(C,1); U = gel(C,2);
    3522         147 :     for (j = 1; j < lU; j++) gel(y,j) = cgetg(l, t_COL);
    3523         126 :     for (i = 1; i < l0; i++)
    3524             :     {
    3525          77 :       GEN sprk = gel(S.sprk, i);
    3526          77 :       GEN Xi = sunits_makecoprime(X, sprk_get_pr(sprk), sprk_get_prk(sprk));
    3527         231 :       for (j = 1; j < lU; j++)
    3528         154 :         gcoeff(y,i,j) = famat_zlog_pr_coprime(nf, Xi, gel(U,j), sprk, MOD);
    3529             :     }
    3530          49 :     if (l0 != l)
    3531          56 :       for (j = 1; j < lU; j++) gcoeff(y,l0,j) = Flc_to_ZC(gel(D,j));
    3532             :   }
    3533      226995 :   y = vec_prepend(y, zlog(nf, bnf_get_tuU(bnf), nfsign_tu(bnf, S.archp), &S));
    3534      601297 :   for (j = 1; j <= lU; j++)
    3535      374306 :     gel(y,j) = ZV_ZV_mod(ZMV_ZCV_mul(S.U, gel(y,j)), cyc);
    3536      226991 :   return y;
    3537             : }
    3538             : GEN
    3539          84 : ideallog_units(GEN bnf, GEN bid)
    3540          84 : { return ideallog_units0(bnf, bid, NULL); }
    3541             : GEN
    3542         518 : log_prk_units(GEN nf, GEN D, GEN sprk)
    3543             : {
    3544         518 :   GEN L, Ltu = log_prk(nf, gel(D,1), sprk, NULL);
    3545         518 :   D = gel(D,2);
    3546         518 :   if (lg(D) != 3 || typ(gel(D,2)) != t_MAT) L = veclog_prk(nf, D, sprk);
    3547             :   else
    3548             :   {
    3549          21 :     GEN X = gel(D,1), U = gel(D,2);
    3550          21 :     long j, lU = lg(U);
    3551          21 :     X = sunits_makecoprime(X, sprk_get_pr(sprk), sprk_get_prk(sprk));
    3552          21 :     L = cgetg(lU, t_MAT);
    3553          63 :     for (j = 1; j < lU; j++)
    3554          42 :       gel(L,j) = famat_zlog_pr_coprime(nf, X, gel(U,j), sprk, NULL);
    3555             :   }
    3556         518 :   return vec_prepend(L, Ltu);
    3557             : }
    3558             : 
    3559             : static GEN
    3560     1383470 : ideallog_i(GEN nf, GEN x, zlog_S *S)
    3561             : {
    3562     1383470 :   pari_sp av = avma;
    3563             :   GEN y;
    3564     1383470 :   if (!S->hU) return cgetg(1, t_COL);
    3565     1381174 :   if (typ(x) == t_MAT)
    3566     1374111 :     y = famat_zlog(nf, x, NULL, S);
    3567             :   else
    3568        7063 :     y = zlog(nf, x, NULL, S);
    3569     1381171 :   y = ZMV_ZCV_mul(S->U, y);
    3570     1381168 :   return gerepileupto(av, ZV_ZV_mod(y, bid_get_cyc(S->bid)));
    3571             : }
    3572             : GEN
    3573     1390151 : ideallogmod(GEN nf, GEN x, GEN bid, GEN mod)
    3574             : {
    3575             :   zlog_S S;
    3576     1390151 :   if (!nf)
    3577             :   {
    3578        6671 :     if (mod) pari_err_IMPL("Zideallogmod");
    3579        6671 :     return Zideallog(bid, x);
    3580             :   }
    3581     1383480 :   checkbid(bid); init_zlog_mod(&S, bid, mod);
    3582     1383470 :   return ideallog_i(checknf(nf), x, &S);
    3583             : }
    3584             : GEN
    3585       13769 : ideallog(GEN nf, GEN x, GEN bid) { return ideallogmod(nf, x, bid, NULL); }
    3586             : 
    3587             : /*************************************************************************/
    3588             : /**                                                                     **/
    3589             : /**               JOIN BID STRUCTURES, IDEAL LISTS                      **/
    3590             : /**                                                                     **/
    3591             : /*************************************************************************/
    3592             : /* bid1, bid2: for coprime modules m1 and m2 (without arch. part).
    3593             :  * Output: bid for m1 m2 */
    3594             : static GEN
    3595         469 : join_bid(GEN nf, GEN bid1, GEN bid2)
    3596             : {
    3597         469 :   pari_sp av = avma;
    3598             :   long nbgen, l1,l2;
    3599             :   GEN I1,I2, G1,G2, sprk1,sprk2, cyc1,cyc2, sarch;
    3600         469 :   GEN sprk, fa,fa2, U, cyc, y, u1 = NULL, x, gen;
    3601             : 
    3602         469 :   I1 = bid_get_ideal(bid1);
    3603         469 :   I2 = bid_get_ideal(bid2);
    3604         469 :   if (gequal1(gcoeff(I1,1,1))) return bid2; /* frequent trivial case */
    3605         259 :   G1 = bid_get_grp(bid1);
    3606         259 :   G2 = bid_get_grp(bid2);
    3607         259 :   x = idealmul(nf, I1,I2);
    3608         259 :   fa = famat_mul_shallow(bid_get_fact(bid1), bid_get_fact(bid2));
    3609         259 :   fa2= famat_mul_shallow(bid_get_fact2(bid1), bid_get_fact2(bid2));
    3610         259 :   sprk1 = bid_get_sprk(bid1);
    3611         259 :   sprk2 = bid_get_sprk(bid2);
    3612         259 :   sprk = shallowconcat(sprk1, sprk2);
    3613             : 
    3614         259 :   cyc1 = abgrp_get_cyc(G1); l1 = lg(cyc1);
    3615         259 :   cyc2 = abgrp_get_cyc(G2); l2 = lg(cyc2);
    3616         259 :   gen = (lg(G1)>3 && lg(G2)>3)? gen_1: NULL;
    3617         259 :   nbgen = l1+l2-2;
    3618         259 :   cyc = ZV_snf_group(shallowconcat(cyc1,cyc2), &U, gen? &u1: NULL);
    3619         259 :   if (nbgen)
    3620             :   {
    3621         259 :     GEN U1 = bid_get_U(bid1), U2 = bid_get_U(bid2);
    3622           0 :     U1 = l1==1? const_vec(lg(sprk1), cgetg(1,t_MAT))
    3623         259 :               : ZM_ZMV_mul(vecslice(U, 1, l1-1),   U1);
    3624           0 :     U2 = l2==1? const_vec(lg(sprk2), cgetg(1,t_MAT))
    3625         259 :               : ZM_ZMV_mul(vecslice(U, l1, nbgen), U2);
    3626         259 :     U = shallowconcat(U1, U2);
    3627             :   }
    3628             :   else
    3629           0 :     U = const_vec(lg(sprk), cgetg(1,t_MAT));
    3630             : 
    3631         259 :   if (gen)
    3632             :   {
    3633         259 :     GEN uv = zkchinese1init2(nf, I2, I1, x);
    3634         518 :     gen = shallowconcat(zkVchinese1(gel(uv,1), abgrp_get_gen(G1)),
    3635         259 :                         zkVchinese1(gel(uv,2), abgrp_get_gen(G2)));
    3636             :   }
    3637         259 :   sarch = bid_get_sarch(bid1); /* trivial */
    3638         259 :   y = bid_grp(nf, u1, cyc, gen, x, sarch);
    3639         259 :   x = mkvec2(x, bid_get_arch(bid1));
    3640         259 :   y = mkvec5(x, y, mkvec2(fa, fa2), mkvec2(sprk, sarch), U);
    3641         259 :   return gerepilecopy(av,y);
    3642             : }
    3643             : 
    3644             : typedef struct _ideal_data {
    3645             :   GEN nf, emb, L, pr, prL, sgnU, archp;
    3646             : } ideal_data;
    3647             : 
    3648             : /* z <- ( z | f(v[i])_{i=1..#v} ) */
    3649             : static void
    3650      758212 : concat_join(GEN *pz, GEN v, GEN (*f)(ideal_data*,GEN), ideal_data *data)
    3651             : {
    3652      758212 :   long i, nz, lv = lg(v);
    3653             :   GEN z, Z;
    3654      758212 :   if (lv == 1) return;
    3655      222779 :   z = *pz; nz = lg(z)-1;
    3656      222779 :   *pz = Z = cgetg(lv + nz, typ(z));
    3657      371646 :   for (i = 1; i <=nz; i++) gel(Z,i) = gel(z,i);
    3658      223307 :   Z += nz;
    3659      491890 :   for (i = 1; i < lv; i++) gel(Z,i) = f(data, gel(v,i));
    3660             : }
    3661             : static GEN
    3662         469 : join_idealinit(ideal_data *D, GEN x)
    3663         469 : { return join_bid(D->nf, x, D->prL); }
    3664             : static GEN
    3665      268440 : join_ideal(ideal_data *D, GEN x)
    3666      268440 : { return idealmulpowprime(D->nf, x, D->pr, D->L); }
    3667             : static GEN
    3668         448 : join_unit(ideal_data *D, GEN x)
    3669             : {
    3670         448 :   GEN bid = join_idealinit(D, gel(x,1)), u = gel(x,2), v = mkvec(D->emb);
    3671         448 :   if (lg(u) != 1) v = shallowconcat(u, v);
    3672         448 :   return mkvec2(bid, v);
    3673             : }
    3674             : 
    3675             : GEN
    3676          49 : log_prk_units_init(GEN bnf)
    3677             : {
    3678          49 :   GEN U = bnf_has_fu(bnf);
    3679          49 :   if (U) U = matalgtobasis(bnf_get_nf(bnf), U);
    3680          21 :   else if (!(U = bnf_compactfu_mat(bnf))) (void)bnf_build_units(bnf);
    3681          49 :   return mkvec2(bnf_get_tuU(bnf), U);
    3682             : }
    3683             : /*  flag & nf_GEN : generators, otherwise no
    3684             :  *  flag &2 : units, otherwise no
    3685             :  *  flag &4 : ideals in HNF, otherwise bid
    3686             :  *  flag &8 : omit ideals which cannot be conductors (pr^1 with Npr=2) */
    3687             : static GEN
    3688       11333 : Ideallist(GEN bnf, ulong bound, long flag)
    3689             : {
    3690       11333 :   const long do_units = flag & 2, big_id = !(flag & 4), cond = flag & 8;
    3691       11333 :   const long istar_flag = (flag & nf_GEN) | nf_INIT;
    3692             :   pari_sp av;
    3693             :   long i, j;
    3694       11333 :   GEN nf, z, p, fa, id, BOUND, U, empty = cgetg(1,t_VEC);
    3695             :   forprime_t S;
    3696             :   ideal_data ID;
    3697             :   GEN (*join_z)(ideal_data*, GEN);
    3698             : 
    3699       11333 :   if (do_units)
    3700             :   {
    3701          21 :     bnf = checkbnf(bnf);
    3702          21 :     nf = bnf_get_nf(bnf);
    3703          21 :     join_z = &join_unit;
    3704             :   }
    3705             :   else
    3706             :   {
    3707       11312 :     nf = checknf(bnf);
    3708       11312 :     join_z = big_id? &join_idealinit: &join_ideal;
    3709             :   }
    3710       11333 :   if ((long)bound <= 0) return empty;
    3711       11333 :   id = matid(nf_get_degree(nf));
    3712       11333 :   if (big_id) id = Idealstar(nf,id, istar_flag);
    3713             : 
    3714             :   /* z[i] will contain all "objects" of norm i. Depending on flag, this means
    3715             :    * an ideal, a bid, or a couple [bid, log(units)]. Such objects are stored
    3716             :    * in vectors, computed one primary component at a time; join_z
    3717             :    * reconstructs the global object */
    3718       11333 :   BOUND = utoipos(bound);
    3719       11333 :   z = const_vec(bound, empty);
    3720       11333 :   U = do_units? log_prk_units_init(bnf): NULL;
    3721       11333 :   gel(z,1) = mkvec(U? mkvec2(id, empty): id);
    3722       11333 :   ID.nf = nf;
    3723             : 
    3724       11333 :   p = cgetipos(3);
    3725       11333 :   u_forprime_init(&S, 2, bound);
    3726       11333 :   av = avma;
    3727       92807 :   while ((p[2] = u_forprime_next(&S)))
    3728             :   {
    3729       81610 :     if (DEBUGLEVEL>1) err_printf("%ld ",p[2]);
    3730       81610 :     fa = idealprimedec_limit_norm(nf, p, BOUND);
    3731      162985 :     for (j = 1; j < lg(fa); j++)
    3732             :     {
    3733       81511 :       GEN pr = gel(fa,j), z2 = leafcopy(z);
    3734       81514 :       ulong Q, q = upr_norm(pr);
    3735             :       long l;
    3736       81512 :       ID.pr = ID.prL = pr;
    3737       81512 :       if (cond && q == 2) { l = 2; Q = 4; } else { l = 1; Q = q; }
    3738      184411 :       for (; Q <= bound; l++, Q *= q) /* add pr^l */
    3739             :       {
    3740             :         ulong iQ;
    3741      103040 :         ID.L = utoipos(l);
    3742      103040 :         if (big_id) {
    3743         211 :           ID.prL = Idealstarprk(nf, pr, l, istar_flag);
    3744         210 :           if (U)
    3745         189 :             ID.emb = Q == 2? empty
    3746         189 :                            : log_prk_units(nf, U, gel(bid_get_sprk(ID.prL),1));
    3747             :         }
    3748      861129 :         for (iQ = Q,i = 1; iQ <= bound; iQ += Q,i++)
    3749      758230 :           concat_join(&gel(z,iQ), gel(z2,i), join_z, &ID);
    3750             :       }
    3751             :     }
    3752       81474 :     if (gc_needed(av,1))
    3753             :     {
    3754          18 :       if(DEBUGMEM>1) pari_warn(warnmem,"Ideallist");
    3755          18 :       z = gerepilecopy(av, z);
    3756             :     }
    3757             :   }
    3758       11333 :   return z;
    3759             : }
    3760             : GEN
    3761          63 : gideallist(GEN bnf, GEN B, long flag)
    3762             : {
    3763          63 :   pari_sp av = avma;
    3764          63 :   if (typ(B) != t_INT)
    3765             :   {
    3766           0 :     B = gfloor(B);
    3767           0 :     if (typ(B) != t_INT) pari_err_TYPE("ideallist", B);
    3768           0 :     if (signe(B) < 0) B = gen_0;
    3769             :   }
    3770          63 :   if (signe(B) < 0)
    3771             :   {
    3772          28 :     if (flag != 4) pari_err_IMPL("ideallist with bid for single norm");
    3773          28 :     return gerepilecopy(av, ideals_by_norm(checknf(bnf), absi(B)));
    3774             :   }
    3775          35 :   if (flag < 0 || flag > 15) pari_err_FLAG("ideallist");
    3776          35 :   return gerepilecopy(av, Ideallist(bnf, itou(B), flag));
    3777             : }
    3778             : GEN
    3779       11298 : ideallist0(GEN bnf, long bound, long flag)
    3780             : {
    3781       11298 :   pari_sp av = avma;
    3782       11298 :   if (flag < 0 || flag > 15) pari_err_FLAG("ideallist");
    3783       11298 :   return gerepilecopy(av, Ideallist(bnf, bound, flag));
    3784             : }
    3785             : GEN
    3786       10563 : ideallist(GEN bnf,long bound) { return ideallist0(bnf,bound,4); }
    3787             : 
    3788             : /* bid = for module m (without arch. part), arch = archimedean part.
    3789             :  * Output: bid for [m,arch] */
    3790             : static GEN
    3791          42 : join_bid_arch(GEN nf, GEN bid, GEN archp)
    3792             : {
    3793          42 :   pari_sp av = avma;
    3794             :   GEN G, U;
    3795          42 :   GEN sprk, cyc, y, u1 = NULL, x, sarch, gen;
    3796             : 
    3797          42 :   checkbid(bid);
    3798          42 :   G = bid_get_grp(bid);
    3799          42 :   x = bid_get_ideal(bid);
    3800          42 :   sarch = nfarchstar(nf, bid_get_ideal(bid), archp);
    3801          42 :   sprk = bid_get_sprk(bid);
    3802             : 
    3803          42 :   gen = (lg(G)>3)? gel(G,3): NULL;
    3804          42 :   cyc = diagonal_shallow(shallowconcat(gel(G,2), sarch_get_cyc(sarch)));
    3805          42 :   cyc = ZM_snf_group(cyc, &U, gen? &u1: NULL);
    3806          42 :   y = bid_grp(nf, u1, cyc, gen, x, sarch);
    3807          42 :   U = split_U(U, sprk);
    3808          42 :   y = mkvec5(mkvec2(x, archp), y, gel(bid,3), mkvec2(sprk, sarch), U);
    3809          42 :   return gerepilecopy(av,y);
    3810             : }
    3811             : static GEN
    3812          42 : join_arch(ideal_data *D, GEN x) {
    3813          42 :   return join_bid_arch(D->nf, x, D->archp);
    3814             : }
    3815             : static GEN
    3816          14 : join_archunit(ideal_data *D, GEN x) {
    3817          14 :   GEN bid = join_arch(D, gel(x,1)), u = gel(x,2), v = mkvec(D->emb);
    3818          14 :   if (lg(u) != 1) v = shallowconcat(u, v);
    3819          14 :   return mkvec2(bid, v);
    3820             : }
    3821             : 
    3822             : /* L from ideallist, add archimedean part */
    3823             : GEN
    3824          14 : ideallistarch(GEN bnf, GEN L, GEN arch)
    3825             : {
    3826             :   pari_sp av;
    3827          14 :   long i, j, l = lg(L), lz;
    3828             :   GEN v, z, V, nf;
    3829             :   ideal_data ID;
    3830             :   GEN (*join_z)(ideal_data*, GEN);
    3831             : 
    3832          14 :   if (typ(L) != t_VEC) pari_err_TYPE("ideallistarch",L);
    3833          14 :   if (l == 1) return cgetg(1,t_VEC);
    3834          14 :   z = gel(L,1);
    3835          14 :   if (typ(z) != t_VEC) pari_err_TYPE("ideallistarch",z);
    3836          14 :   z = gel(z,1); /* either a bid or [bid,U] */
    3837          14 :   ID.archp = vec01_to_indices(arch);
    3838          14 :   if (lg(z) == 3)
    3839             :   { /* [bid,U]: do units */
    3840           7 :     bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    3841           7 :     if (typ(z) != t_VEC) pari_err_TYPE("ideallistarch",z);
    3842           7 :     ID.emb = zm_to_ZM( rowpermute(nfsign_units(bnf,NULL,1), ID.archp) );
    3843           7 :     join_z = &join_archunit;
    3844             :   }
    3845             :   else
    3846             :   {
    3847           7 :     join_z = &join_arch;
    3848           7 :     nf = checknf(bnf);
    3849             :   }
    3850          14 :   ID.nf = nf;
    3851          14 :   av = avma; V = cgetg(l, t_VEC);
    3852          63 :   for (i = 1; i < l; i++)
    3853             :   {
    3854          49 :     z = gel(L,i); lz = lg(z);
    3855          49 :     gel(V,i) = v = cgetg(lz,t_VEC);
    3856          91 :     for (j=1; j<lz; j++) gel(v,j) = join_z(&ID, gel(z,j));
    3857             :   }
    3858          14 :   return gerepilecopy(av,V);
    3859             : }

Generated by: LCOV version 1.14