Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - base3.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 19832-7f23dbb) Lines: 1482 1569 94.5 %
Date: 2016-12-08 05:49:22 Functions: 168 176 95.5 %
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. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /*******************************************************************/
      15             : /*                                                                 */
      16             : /*                       BASIC NF OPERATIONS                       */
      17             : /*                                                                 */
      18             : /*******************************************************************/
      19             : #include "pari.h"
      20             : #include "paripriv.h"
      21             : 
      22             : /*******************************************************************/
      23             : /*                                                                 */
      24             : /*                OPERATIONS OVER NUMBER FIELD ELEMENTS.           */
      25             : /*     represented as column vectors over the integral basis       */
      26             : /*                                                                 */
      27             : /*******************************************************************/
      28             : static GEN
      29     8313198 : get_tab(GEN nf, long *N)
      30             : {
      31     8313198 :   GEN tab = (typ(nf) == t_MAT)? nf: gel(nf,9);
      32     8313198 :   *N = nbrows(tab); return tab;
      33             : }
      34             : 
      35             : /* x != 0, y t_INT. Return x * y (not memory clean if x = 1) */
      36             : static GEN
      37   348839467 : _mulii(GEN x, GEN y) {
      38   883941437 :   return is_pm1(x)? (signe(x) < 0)? negi(y): y
      39   535101970 :                   : mulii(x, y);
      40             : }
      41             : 
      42             : GEN
      43        2345 : tablemul_ei_ej(GEN M, long i, long j)
      44             : {
      45             :   long N;
      46        2345 :   GEN tab = get_tab(M, &N);
      47        2345 :   tab += (i-1)*N; return gel(tab,j);
      48             : }
      49             : 
      50             : /* Outputs x.ei, where ei is the i-th elt of the algebra basis.
      51             :  * x an RgV of correct length and arbitrary content (polynomials, scalars...).
      52             :  * M is the multiplication table ei ej = sum_k M_k^(i,j) ek */
      53             : GEN
      54        3234 : tablemul_ei(GEN M, GEN x, long i)
      55             : {
      56             :   long j, k, N;
      57             :   GEN v, tab;
      58             : 
      59        3234 :   if (i==1) return gcopy(x);
      60        3234 :   tab = get_tab(M, &N);
      61        3234 :   if (typ(x) != t_COL) { v = zerocol(N); gel(v,i) = gcopy(x); return v; }
      62        3234 :   tab += (i-1)*N; v = cgetg(N+1,t_COL);
      63             :   /* wi . x = [ sum_j tab[k,j] x[j] ]_k */
      64       21252 :   for (k=1; k<=N; k++)
      65             :   {
      66       18018 :     pari_sp av = avma;
      67       18018 :     GEN s = gen_0;
      68      126462 :     for (j=1; j<=N; j++)
      69             :     {
      70      108444 :       GEN c = gcoeff(tab,k,j);
      71      108444 :       if (!gequal0(c)) s = gadd(s, gmul(c, gel(x,j)));
      72             :     }
      73       18018 :     gel(v,k) = gerepileupto(av,s);
      74             :   }
      75        3234 :   return v;
      76             : }
      77             : /* as tablemul_ei, assume x a ZV of correct length */
      78             : GEN
      79     7136763 : zk_ei_mul(GEN nf, GEN x, long i)
      80             : {
      81             :   long j, k, N;
      82             :   GEN v, tab;
      83             : 
      84     7136763 :   if (i==1) return ZC_copy(x);
      85     7136749 :   tab = get_tab(nf, &N); tab += (i-1)*N;
      86     7136749 :   v = cgetg(N+1,t_COL);
      87    53769285 :   for (k=1; k<=N; k++)
      88             :   {
      89    46632536 :     pari_sp av = avma;
      90    46632536 :     GEN s = gen_0;
      91   601505274 :     for (j=1; j<=N; j++)
      92             :     {
      93   554872738 :       GEN c = gcoeff(tab,k,j);
      94   554872738 :       if (signe(c)) s = addii(s, _mulii(c, gel(x,j)));
      95             :     }
      96    46632536 :     gel(v,k) = gerepileuptoint(av, s);
      97             :   }
      98     7136749 :   return v;
      99             : }
     100             : 
     101             : /* table of multiplication by wi in R[w1,..., wN] */
     102             : GEN
     103         392 : ei_multable(GEN TAB, long i)
     104             : {
     105             :   long k,N;
     106         392 :   GEN m, tab = get_tab(TAB, &N);
     107         392 :   tab += (i-1)*N;
     108         392 :   m = cgetg(N+1,t_MAT);
     109         392 :   for (k=1; k<=N; k++) gel(m,k) = gel(tab,k);
     110         392 :   return m;
     111             : }
     112             : 
     113             : GEN
     114     2935374 : zk_multable(GEN nf, GEN x)
     115             : {
     116     2935374 :   long i, l = lg(x);
     117     2935374 :   GEN mul = cgetg(l,t_MAT);
     118     2935374 :   gel(mul,1) = x; /* assume w_1 = 1 */
     119     2935374 :   for (i=2; i<l; i++) gel(mul,i) = zk_ei_mul(nf,x,i);
     120     2935374 :   return mul;
     121             : }
     122             : GEN
     123         721 : multable(GEN M, GEN x)
     124             : {
     125             :   long i, N;
     126             :   GEN mul;
     127         721 :   if (typ(x) == t_MAT) return x;
     128           0 :   M = get_tab(M, &N);
     129           0 :   if (typ(x) != t_COL) return scalarmat(x, N);
     130           0 :   mul = cgetg(N+1,t_MAT);
     131           0 :   gel(mul,1) = x; /* assume w_1 = 1 */
     132           0 :   for (i=2; i<=N; i++) gel(mul,i) = tablemul_ei(M,x,i);
     133           0 :   return mul;
     134             : }
     135             : 
     136             : /* x integral in nf; table of multiplication by x in ZK = Z[w1,..., wN].
     137             :  * Return a t_INT if x is scalar, and a ZM otherwise */
     138             : GEN
     139     1919965 : zk_scalar_or_multable(GEN nf, GEN x)
     140             : {
     141     1919965 :   long tx = typ(x);
     142     1919965 :   if (tx == t_MAT || tx == t_INT) return x;
     143     1900199 :   x = nf_to_scalar_or_basis(nf, x);
     144     1900199 :   return (typ(x) == t_COL)? zk_multable(nf, x): x;
     145             : }
     146             : 
     147             : GEN
     148          42 : nftrace(GEN nf, GEN x)
     149             : {
     150          42 :   pari_sp av = avma;
     151          42 :   nf = checknf(nf);
     152          42 :   x = nf_to_scalar_or_basis(nf, x);
     153         105 :   x = (typ(x) == t_COL)? RgV_dotproduct(x, gel(nf_get_Tr(nf),1))
     154          63 :                        : gmulgs(x, nf_get_degree(nf));
     155          42 :   return gerepileupto(av, x);
     156             : }
     157             : GEN
     158         567 : rnfelttrace(GEN rnf, GEN x)
     159             : {
     160         567 :   pari_sp av = avma;
     161         567 :   checkrnf(rnf);
     162         567 :   x = rnfeltabstorel(rnf, x);
     163        1344 :   x = (typ(x) == t_POLMOD)? rnfeltdown(rnf, gtrace(x))
     164         868 :                           : gmulgs(x, rnf_get_degree(rnf));
     165         476 :   return gerepileupto(av, x);
     166             : }
     167             : 
     168             : /* assume nf is a genuine nf, fa a famat */
     169             : static GEN
     170           7 : famat_norm(GEN nf, GEN fa)
     171             : {
     172           7 :   pari_sp av = avma;
     173           7 :   GEN g = gel(fa,1), e = gel(fa,2), N = gen_1;
     174           7 :   long i, l = lg(g);
     175          21 :   for (i = 1; i < l; i++)
     176          14 :     N = gmul(N, powgi(nfnorm(nf, gel(g,i)), gel(e,i)));
     177           7 :   return gerepileupto(av, N);
     178             : }
     179             : GEN
     180       20380 : nfnorm(GEN nf, GEN x)
     181             : {
     182       20380 :   pari_sp av = avma;
     183       20380 :   nf = checknf(nf);
     184       20380 :   if (typ(x) == t_MAT) return famat_norm(nf, x);
     185       20373 :   x = nf_to_scalar_or_alg(nf, x);
     186       59747 :   x = (typ(x) == t_POL)? RgXQ_norm(x, nf_get_pol(nf))
     187       39374 :                        : gpowgs(x, nf_get_degree(nf));
     188       20373 :   return gerepileupto(av, x);
     189             : }
     190             : 
     191             : GEN
     192         231 : rnfeltnorm(GEN rnf, GEN x)
     193             : {
     194         231 :   pari_sp av = avma;
     195         231 :   checkrnf(rnf);
     196         231 :   x = rnfeltabstorel(rnf, x);
     197         378 :   x = (typ(x) == t_POLMOD)? rnfeltdown(rnf, gnorm(x))
     198         238 :                           : gpowgs(x, rnf_get_degree(rnf));
     199         140 :   return gerepileupto(av, x);
     200             : }
     201             : 
     202             : /* x + y in nf */
     203             : GEN
     204     1607543 : nfadd(GEN nf, GEN x, GEN y)
     205             : {
     206     1607543 :   pari_sp av = avma;
     207             :   GEN z;
     208             : 
     209     1607543 :   nf = checknf(nf);
     210     1607543 :   x = nf_to_scalar_or_basis(nf, x);
     211     1607543 :   y = nf_to_scalar_or_basis(nf, y);
     212     1607543 :   if (typ(x) != t_COL)
     213     1127820 :   { z = (typ(y) == t_COL)? RgC_Rg_add(y, x): gadd(x,y); }
     214             :   else
     215      479723 :   { z = (typ(y) == t_COL)? RgC_add(x, y): RgC_Rg_add(x, y); }
     216     1607543 :   return gerepileupto(av, z);
     217             : }
     218             : /* x - y in nf */
     219             : GEN
     220       70980 : nfsub(GEN nf, GEN x, GEN y)
     221             : {
     222       70980 :   pari_sp av = avma;
     223             :   GEN z;
     224             : 
     225       70980 :   nf = checknf(nf);
     226       70980 :   x = nf_to_scalar_or_basis(nf, x);
     227       70980 :   y = nf_to_scalar_or_basis(nf, y);
     228       70980 :   if (typ(x) != t_COL)
     229       23982 :   { z = (typ(y) == t_COL)? Rg_RgC_sub(x,y): gsub(x,y); }
     230             :   else
     231       46998 :   { z = (typ(y) == t_COL)? RgC_sub(x,y): RgC_Rg_sub(x,y); }
     232       70980 :   return gerepileupto(av, z);
     233             : }
     234             : 
     235             : /* product of x and y in nf */
     236             : GEN
     237     4198624 : nfmul(GEN nf, GEN x, GEN y)
     238             : {
     239             :   GEN z;
     240     4198624 :   pari_sp av = avma;
     241             : 
     242     4198624 :   if (x == y) return nfsqr(nf,x);
     243             : 
     244     3737996 :   nf = checknf(nf);
     245     3737996 :   x = nf_to_scalar_or_basis(nf, x);
     246     3737996 :   y = nf_to_scalar_or_basis(nf, y);
     247     3737996 :   if (typ(x) != t_COL)
     248             :   {
     249     3050840 :     if (isintzero(x)) return gen_0;
     250     2494032 :     z = (typ(y) == t_COL)? RgC_Rg_mul(y, x): gmul(x,y); }
     251             :   else
     252             :   {
     253      687156 :     if (typ(y) != t_COL)
     254             :     {
     255      478009 :       if (isintzero(y)) return gen_0;
     256      131446 :       z = RgC_Rg_mul(x, y);
     257             :     }
     258             :     else
     259             :     {
     260             :       GEN dx, dy;
     261      209147 :       x = Q_remove_denom(x, &dx);
     262      209147 :       y = Q_remove_denom(y, &dy);
     263      209147 :       z = nfmuli(nf,x,y);
     264      209147 :       dx = mul_denom(dx,dy);
     265      209147 :       if (dx) z = RgC_Rg_div(z, dx);
     266             :     }
     267             :   }
     268     2834625 :   return gerepileupto(av, z);
     269             : }
     270             : /* square of x in nf */
     271             : GEN
     272      583271 : nfsqr(GEN nf, GEN x)
     273             : {
     274      583271 :   pari_sp av = avma;
     275             :   GEN z;
     276             : 
     277      583271 :   nf = checknf(nf);
     278      583271 :   x = nf_to_scalar_or_basis(nf, x);
     279      583271 :   if (typ(x) != t_COL) z = gsqr(x);
     280             :   else
     281             :   {
     282             :     GEN dx;
     283       75442 :     x = Q_remove_denom(x, &dx);
     284       75442 :     z = nfsqri(nf,x);
     285       75442 :     if (dx) z = RgC_Rg_div(z, sqri(dx));
     286             :   }
     287      583271 :   return gerepileupto(av, z);
     288             : }
     289             : 
     290             : /* x a ZC, v a t_COL of ZC/Z */
     291             : GEN
     292      111102 : zkC_multable_mul(GEN v, GEN x)
     293             : {
     294      111102 :   long i, l = lg(v);
     295      111102 :   GEN y = cgetg(l, t_COL);
     296      402235 :   for (i = 1; i < l; i++)
     297             :   {
     298      291133 :     GEN c = gel(v,i);
     299      291133 :     if (typ(c)!=t_COL) {
     300           0 :       if (!isintzero(c)) c = ZC_Z_mul(gel(x,1), c);
     301             :     } else {
     302      291133 :       c = ZM_ZC_mul(x,c);
     303      291133 :       if (ZV_isscalar(c)) c = gel(c,1);
     304             :     }
     305      291133 :     gel(y,i) = c;
     306             :   }
     307      111102 :   return y;
     308             : }
     309             : 
     310             : GEN
     311       24484 : nfC_multable_mul(GEN v, GEN x)
     312             : {
     313       24484 :   long i, l = lg(v);
     314       24484 :   GEN y = cgetg(l, t_COL);
     315      142788 :   for (i = 1; i < l; i++)
     316             :   {
     317      118304 :     GEN c = gel(v,i);
     318      118304 :     if (typ(c)!=t_COL) {
     319       94773 :       if (!isintzero(c)) c = RgC_Rg_mul(gel(x,1), c);
     320             :     } else {
     321       23531 :       c = RgM_RgC_mul(x,c);
     322       23531 :       if (QV_isscalar(c)) c = gel(c,1);
     323             :     }
     324      118304 :     gel(y,i) = c;
     325             :   }
     326       24484 :   return y;
     327             : }
     328             : 
     329             : GEN
     330       73640 : nfC_nf_mul(GEN nf, GEN v, GEN x)
     331             : {
     332             :   long tx;
     333             :   GEN y;
     334             : 
     335       73640 :   x = nf_to_scalar_or_basis(nf, x);
     336       73640 :   tx = typ(x);
     337       73640 :   if (tx != t_COL)
     338             :   {
     339             :     long l, i;
     340       50045 :     if (tx == t_INT)
     341             :     {
     342       48883 :       long s = signe(x);
     343       48883 :       if (!s) return zerocol(lg(v)-1);
     344       45744 :       if (is_pm1(x)) return s > 0? leafcopy(v): RgC_neg(v);
     345             :     }
     346       12746 :     l = lg(v); y = cgetg(l, t_COL);
     347       99786 :     for (i=1; i < l; i++)
     348             :     {
     349       87040 :       GEN c = gel(v,i);
     350       87040 :       if (typ(c) != t_COL) c = gmul(c, x); else c = RgC_Rg_mul(c, x);
     351       87040 :       gel(y,i) = c;
     352             :     }
     353       12746 :     return y;
     354             :   }
     355             :   else
     356             :   {
     357             :     GEN dx;
     358       23595 :     x = zk_multable(nf, Q_remove_denom(x,&dx));
     359       23595 :     y = nfC_multable_mul(v, x);
     360       23595 :     return dx? RgC_Rg_div(y, dx): y;
     361             :   }
     362             : }
     363             : static GEN
     364        3248 : mulbytab(GEN M, GEN c)
     365        3248 : { return typ(c) == t_COL? RgM_RgC_mul(M,c): RgC_Rg_mul(gel(M,1), c); }
     366             : GEN
     367         721 : tablemulvec(GEN M, GEN x, GEN v)
     368             : {
     369             :   long l, i;
     370             :   GEN y;
     371             : 
     372         721 :   if (typ(x) == t_COL && RgV_isscalar(x))
     373             :   {
     374           0 :     x = gel(x,1);
     375           0 :     return typ(v) == t_POL? RgX_Rg_mul(v,x): RgV_Rg_mul(v,x);
     376             :   }
     377         721 :   x = multable(M, x); /* multiplication table by x */
     378         721 :   y = cgetg_copy(v, &l);
     379         721 :   if (typ(v) == t_POL)
     380             :   {
     381         721 :     y[1] = v[1];
     382         721 :     for (i=2; i < l; i++) gel(y,i) = mulbytab(x, gel(v,i));
     383         721 :     y = normalizepol(y);
     384             :   }
     385             :   else
     386             :   {
     387           0 :     for (i=1; i < l; i++) gel(y,i) = mulbytab(x, gel(v,i));
     388             :   }
     389         721 :   return y;
     390             : }
     391             : 
     392             : GEN
     393      311908 : zkmultable_capZ(GEN mx) { return Q_denom(zkmultable_inv(mx)); }
     394             : 
     395             : GEN
     396      328694 : zkmultable_inv(GEN mx)
     397      328694 : { return ZM_gauss(mx, col_ei(lg(mx)-1,1)); }
     398             : 
     399             : /* nf a true nf, x a ZC */
     400             : GEN
     401       16786 : zk_inv(GEN nf, GEN x) { return zkmultable_inv(zk_multable(nf,x)); }
     402             : 
     403             : /* inverse of x in nf */
     404             : GEN
     405       60256 : nfinv(GEN nf, GEN x)
     406             : {
     407       60256 :   pari_sp av = avma;
     408             :   GEN z;
     409             : 
     410       60256 :   nf = checknf(nf);
     411       60256 :   x = nf_to_scalar_or_basis(nf, x);
     412       60256 :   if (typ(x) == t_COL)
     413             :   {
     414             :     GEN d;
     415        5663 :     x = Q_remove_denom(x, &d);
     416        5663 :     z = zk_inv(nf, x);
     417        5663 :     if (d) z = RgC_Rg_mul(z, d);
     418             :   }
     419             :   else
     420       54593 :     z = ginv(x);
     421       60256 :   return gerepileupto(av, z);
     422             : }
     423             : 
     424             : /* quotient of x and y in nf */
     425             : GEN
     426        9205 : nfdiv(GEN nf, GEN x, GEN y)
     427             : {
     428        9205 :   pari_sp av = avma;
     429             :   GEN z;
     430             : 
     431        9205 :   nf = checknf(nf);
     432        9205 :   y = nf_to_scalar_or_basis(nf, y);
     433        9205 :   if (typ(y) != t_COL)
     434             :   {
     435        2309 :     x = nf_to_scalar_or_basis(nf, x);
     436        2309 :     z = (typ(x) == t_COL)? RgC_Rg_div(x, y): gdiv(x,y);
     437             :   }
     438             :   else
     439             :   {
     440             :     GEN d;
     441        6896 :     y = Q_remove_denom(y, &d);
     442        6896 :     z = nfmul(nf, x, zk_inv(nf,y));
     443        6896 :     if (d) z = RgC_Rg_mul(z, d);
     444             :   }
     445        9205 :   return gerepileupto(av, z);
     446             : }
     447             : 
     448             : /* product of INTEGERS (t_INT or ZC) x and y in nf
     449             :  * compute xy as ( sum_i x_i sum_j y_j m^{i,j}_k )_k */
     450             : GEN
     451      593438 : nfmuli(GEN nf, GEN x, GEN y)
     452             : {
     453             :   long i, j, k, N;
     454      593438 :   GEN s, v, TAB = get_tab(nf, &N);
     455             : 
     456      593438 :   if (typ(x) == t_INT) return (typ(y) == t_COL)? ZC_Z_mul(y, x): mulii(x,y);
     457      517925 :   if (typ(y) == t_INT) return ZC_Z_mul(x, y);
     458             :   /* both x and y are ZV */
     459      492137 :   v = cgetg(N+1,t_COL);
     460     2454299 :   for (k=1; k<=N; k++)
     461             :   {
     462     1962162 :     pari_sp av = avma;
     463     1962162 :     GEN TABi = TAB;
     464     1962162 :     if (k == 1)
     465      492137 :       s = mulii(gel(x,1),gel(y,1));
     466             :     else
     467     2940050 :       s = addii(mulii(gel(x,1),gel(y,k)),
     468     2940050 :                 mulii(gel(x,k),gel(y,1)));
     469    12449650 :     for (i=2; i<=N; i++)
     470             :     {
     471    10487488 :       GEN t, xi = gel(x,i);
     472    10487488 :       TABi += N;
     473    10487488 :       if (!signe(xi)) continue;
     474             : 
     475     6131792 :       t = NULL;
     476    75735548 :       for (j=2; j<=N; j++)
     477             :       {
     478    69603756 :         GEN p1, c = gcoeff(TABi, k, j); /* m^{i,j}_k */
     479    69603756 :         if (!signe(c)) continue;
     480    33622389 :         p1 = _mulii(c, gel(y,j));
     481    33622389 :         t = t? addii(t, p1): p1;
     482             :       }
     483     6131792 :       if (t) s = addii(s, mulii(xi, t));
     484             :     }
     485     1962162 :     gel(v,k) = gerepileuptoint(av,s);
     486             :   }
     487      492137 :   return v;
     488             : }
     489             : /* square of INTEGER (t_INT or ZC) x in nf */
     490             : GEN
     491      577040 : nfsqri(GEN nf, GEN x)
     492             : {
     493             :   long i, j, k, N;
     494      577040 :   GEN s, v, TAB = get_tab(nf, &N);
     495             : 
     496      577040 :   if (typ(x) == t_INT) return sqri(x);
     497      577040 :   v = cgetg(N+1,t_COL);
     498     4493603 :   for (k=1; k<=N; k++)
     499             :   {
     500     3916563 :     pari_sp av = avma;
     501     3916563 :     GEN TABi = TAB;
     502     3916563 :     if (k == 1)
     503      577040 :       s = sqri(gel(x,1));
     504             :     else
     505     3339523 :       s = shifti(mulii(gel(x,1),gel(x,k)), 1);
     506    48157683 :     for (i=2; i<=N; i++)
     507             :     {
     508    44241120 :       GEN p1, c, t, xi = gel(x,i);
     509    44241120 :       TABi += N;
     510    44241120 :       if (!signe(xi)) continue;
     511             : 
     512    13985694 :       c = gcoeff(TABi, k, i);
     513    13985694 :       t = signe(c)? _mulii(c,xi): NULL;
     514   232616307 :       for (j=i+1; j<=N; j++)
     515             :       {
     516   218630613 :         c = gcoeff(TABi, k, j);
     517   218630613 :         if (!signe(c)) continue;
     518   117509767 :         p1 = _mulii(c, shifti(gel(x,j),1));
     519   117509767 :         t = t? addii(t, p1): p1;
     520             :       }
     521    13985694 :       if (t) s = addii(s, mulii(xi, t));
     522             :     }
     523     3916563 :     gel(v,k) = gerepileuptoint(av,s);
     524             :   }
     525      577040 :   return v;
     526             : }
     527             : 
     528             : /* both x and y are RgV */
     529             : GEN
     530           0 : tablemul(GEN TAB, GEN x, GEN y)
     531             : {
     532             :   long i, j, k, N;
     533             :   GEN s, v;
     534           0 :   if (typ(x) != t_COL) return gmul(x, y);
     535           0 :   if (typ(y) != t_COL) return gmul(y, x);
     536           0 :   N = lg(x)-1;
     537           0 :   v = cgetg(N+1,t_COL);
     538           0 :   for (k=1; k<=N; k++)
     539             :   {
     540           0 :     pari_sp av = avma;
     541           0 :     GEN TABi = TAB;
     542           0 :     if (k == 1)
     543           0 :       s = gmul(gel(x,1),gel(y,1));
     544             :     else
     545           0 :       s = gadd(gmul(gel(x,1),gel(y,k)),
     546           0 :                gmul(gel(x,k),gel(y,1)));
     547           0 :     for (i=2; i<=N; i++)
     548             :     {
     549           0 :       GEN t, xi = gel(x,i);
     550           0 :       TABi += N;
     551           0 :       if (gequal0(xi)) continue;
     552             : 
     553           0 :       t = NULL;
     554           0 :       for (j=2; j<=N; j++)
     555             :       {
     556           0 :         GEN p1, c = gcoeff(TABi, k, j); /* m^{i,j}_k */
     557           0 :         if (gequal0(c)) continue;
     558           0 :         p1 = gmul(c, gel(y,j));
     559           0 :         t = t? gadd(t, p1): p1;
     560             :       }
     561           0 :       if (t) s = gadd(s, gmul(xi, t));
     562             :     }
     563           0 :     gel(v,k) = gerepileupto(av,s);
     564             :   }
     565           0 :   return v;
     566             : }
     567             : GEN
     568        5432 : tablesqr(GEN TAB, GEN x)
     569             : {
     570             :   long i, j, k, N;
     571             :   GEN s, v;
     572             : 
     573        5432 :   if (typ(x) != t_COL) return gsqr(x);
     574        5432 :   N = lg(x)-1;
     575        5432 :   v = cgetg(N+1,t_COL);
     576             : 
     577       41398 :   for (k=1; k<=N; k++)
     578             :   {
     579       35966 :     pari_sp av = avma;
     580       35966 :     GEN TABi = TAB;
     581       35966 :     if (k == 1)
     582        5432 :       s = gsqr(gel(x,1));
     583             :     else
     584       30534 :       s = gmul2n(gmul(gel(x,1),gel(x,k)), 1);
     585      251776 :     for (i=2; i<=N; i++)
     586             :     {
     587      215810 :       GEN p1, c, t, xi = gel(x,i);
     588      215810 :       TABi += N;
     589      215810 :       if (gequal0(xi)) continue;
     590             : 
     591       72282 :       c = gcoeff(TABi, k, i);
     592       72282 :       t = !gequal0(c)? gmul(c,xi): NULL;
     593      327495 :       for (j=i+1; j<=N; j++)
     594             :       {
     595      255213 :         c = gcoeff(TABi, k, j);
     596      255213 :         if (gequal0(c)) continue;
     597      134442 :         p1 = gmul(gmul2n(c,1), gel(x,j));
     598      134442 :         t = t? gadd(t, p1): p1;
     599             :       }
     600       72282 :       if (t) s = gadd(s, gmul(xi, t));
     601             :     }
     602       35966 :     gel(v,k) = gerepileupto(av,s);
     603             :   }
     604        5432 :   return v;
     605             : }
     606             : 
     607             : static GEN
     608       25399 : _mul(void *data, GEN x, GEN y) { return nfmuli((GEN)data,x,y); }
     609             : static GEN
     610       92696 : _sqr(void *data, GEN x) { return nfsqri((GEN)data,x); }
     611             : 
     612             : /* Compute z^n in nf, left-shift binary powering */
     613             : GEN
     614       88787 : nfpow(GEN nf, GEN z, GEN n)
     615             : {
     616       88787 :   pari_sp av = avma;
     617             :   long s;
     618             :   GEN x, cx;
     619             : 
     620       88787 :   if (typ(n)!=t_INT) pari_err_TYPE("nfpow",n);
     621       88787 :   nf = checknf(nf);
     622       88787 :   s = signe(n); if (!s) return gen_1;
     623       88787 :   x = nf_to_scalar_or_basis(nf, z);
     624       88787 :   if (typ(x) != t_COL) return powgi(x,n);
     625       77678 :   if (s < 0)
     626             :   { /* simplified nfinv */
     627             :     GEN d;
     628        1535 :     x = Q_remove_denom(x, &d);
     629        1535 :     x = zk_inv(nf, x);
     630        1535 :     x = primitive_part(x, &cx);
     631        1535 :     cx = mul_content(cx, d);
     632        1535 :     n = absi(n);
     633             :   }
     634             :   else
     635       76143 :     x = primitive_part(x, &cx);
     636       77678 :   x = gen_pow(x, n, (void*)nf, _sqr, _mul);
     637       77678 :   if (cx) x = gmul(x, powgi(cx, n));
     638       77678 :   return av==avma? gcopy(x): gerepileupto(av,x);
     639             : }
     640             : /* Compute z^n in nf, left-shift binary powering */
     641             : GEN
     642       31948 : nfpow_u(GEN nf, GEN z, ulong n)
     643             : {
     644       31948 :   pari_sp av = avma;
     645             :   GEN x, cx;
     646             : 
     647       31948 :   nf = checknf(nf);
     648       31948 :   if (!n) return gen_1;
     649       31948 :   x = nf_to_scalar_or_basis(nf, z);
     650       31948 :   if (typ(x) != t_COL) return gpowgs(x,n);
     651        3990 :   x = primitive_part(x, &cx);
     652        3990 :   x = gen_powu(x, n, (void*)nf, _sqr, _mul);
     653        3990 :   if (cx) x = gmul(x, powgi(cx, utoipos(n)));
     654        3990 :   return av==avma? gcopy(x): gerepileupto(av,x);
     655             : }
     656             : 
     657             : static GEN
     658      331079 : _nf_red(void *E, GEN x) { (void)E; return x; }
     659             : 
     660             : static GEN
     661     1498665 : _nf_add(void *E, GEN x, GEN y) { return nfadd((GEN)E,x,y); }
     662             : 
     663             : static GEN
     664       84315 : _nf_neg(void *E, GEN x) { (void)E; return gneg(x); }
     665             : 
     666             : static GEN
     667     1768298 : _nf_mul(void *E, GEN x, GEN y) { return nfmul((GEN)E,x,y); }
     668             : 
     669             : static GEN
     670        5572 : _nf_inv(void *E, GEN x) { return nfinv((GEN)E,x); }
     671             : 
     672             : static GEN
     673        1253 : _nf_s(void *E, long x) { (void)E; return stoi(x); }
     674             : 
     675             : static const struct bb_field nf_field={_nf_red,_nf_add,_nf_mul,_nf_neg,
     676             :                                         _nf_inv,&gequal0,_nf_s };
     677             : 
     678       23324 : const struct bb_field *get_nf_field(void **E, GEN nf)
     679       23324 : { *E = (void*)nf; return &nf_field; }
     680             : 
     681             : GEN
     682          14 : nfM_det(GEN nf, GEN M)
     683             : {
     684             :   void *E;
     685          14 :   const struct bb_field *S = get_nf_field(&E, nf);
     686          14 :   return gen_det(M, E, S);
     687             : }
     688             : GEN
     689        1239 : nfM_inv(GEN nf, GEN M)
     690             : {
     691             :   void *E;
     692        1239 :   const struct bb_field *S = get_nf_field(&E, nf);
     693        1239 :   return gen_Gauss(M, matid(lg(M)-1), E, S);
     694             : }
     695             : GEN
     696        1148 : nfM_mul(GEN nf, GEN A, GEN B)
     697             : {
     698             :   void *E;
     699        1148 :   const struct bb_field *S = get_nf_field(&E, nf);
     700        1148 :   return gen_matmul(A, B, E, S);
     701             : }
     702             : GEN
     703       20923 : nfM_nfC_mul(GEN nf, GEN A, GEN B)
     704             : {
     705             :   void *E;
     706       20923 :   const struct bb_field *S = get_nf_field(&E, nf);
     707       20923 :   return gen_matcolmul(A, B, E, S);
     708             : }
     709             : 
     710             : /* valuation of integral x (ZV), with resp. to prime ideal pr */
     711             : long
     712     5035360 : ZC_nfvalrem(GEN x, GEN pr, GEN *newx)
     713             : {
     714             :   long i, v, l;
     715     5035360 :   GEN r, y, p = pr_get_p(pr), mul = pr_get_tau(pr);
     716             : 
     717             :   /* p inert */
     718     5035360 :   if (typ(mul) == t_INT) return newx? ZV_pvalrem(x, p, newx):ZV_pval(x, p);
     719     5029585 :   y = cgetg_copy(x, &l); /* will hold the new x */
     720     5029585 :   x = leafcopy(x);
     721     7417958 :   for(v=0;; v++)
     722             :   {
     723    25475732 :     for (i=1; i<l; i++)
     724             :     { /* is (x.b)[i] divisible by p ? */
     725    23087359 :       gel(y,i) = dvmdii(ZMrow_ZC_mul(mul,x,i),p,&r);
     726    23087359 :       if (r != gen_0) { if (newx) *newx = x; return v; }
     727             :     }
     728     2388373 :     swap(x, y);
     729     2388373 :   }
     730             : }
     731             : long
     732     4821310 : ZC_nfval(GEN x, GEN P)
     733     4821310 : { return ZC_nfvalrem(x, P, NULL); }
     734             : 
     735             : /* v_P(x) != 0, x a ZV. Simpler version of ZC_nfvalrem */
     736             : int
     737      202440 : ZC_prdvd(GEN x, GEN P)
     738             : {
     739      202440 :   pari_sp av = avma;
     740             :   long i, l;
     741      202440 :   GEN p = pr_get_p(P), mul = pr_get_tau(P);
     742      202440 :   if (typ(mul) == t_INT) return ZV_Z_dvd(x, p);
     743      202363 :   l = lg(x);
     744      827260 :   for (i=1; i<l; i++)
     745      754933 :     if (remii(ZMrow_ZC_mul(mul,x,i), p) != gen_0) { avma = av; return 0; }
     746       72327 :   avma = av; return 1;
     747             : }
     748             : 
     749             : int
     750          28 : pr_equal(GEN P, GEN Q)
     751             : {
     752          28 :   GEN gQ, p = pr_get_p(P);
     753          28 :   long e = pr_get_e(P), f = pr_get_f(P), n;
     754          28 :   if (!equalii(p, pr_get_p(Q)) || e != pr_get_e(Q) || f != pr_get_f(Q))
     755          14 :     return 0;
     756          14 :   gQ = pr_get_gen(Q); n = lg(gQ)-1;
     757          14 :   if (2*e*f > n) return 1; /* room for only one such pr */
     758           7 :   return ZV_equal(pr_get_gen(P), gQ) || ZC_prdvd(gQ, P);
     759             : }
     760             : 
     761             : long
     762     1320837 : nfval(GEN nf, GEN x, GEN pr)
     763             : {
     764     1320837 :   pari_sp av = avma;
     765             :   long w, e;
     766             :   GEN cx, p;
     767             : 
     768     1320837 :   if (gequal0(x)) return LONG_MAX;
     769     1320298 :   nf = checknf(nf);
     770     1320298 :   checkprid(pr);
     771     1320298 :   p = pr_get_p(pr);
     772     1320298 :   e = pr_get_e(pr);
     773     1320298 :   x = nf_to_scalar_or_basis(nf, x);
     774     1320298 :   if (typ(x) != t_COL) return e*Q_pval(x,p);
     775      137032 :   x = Q_primitive_part(x, &cx);
     776      137032 :   w = ZC_nfval(x,pr);
     777      137032 :   if (cx) w += e*Q_pval(cx,p);
     778      137032 :   avma = av; return w;
     779             : }
     780             : 
     781             : /* want to write p^v = uniformizer^(e*v) * z^v, z coprime to pr */
     782             : /* z := tau^e / p^(e-1), algebraic integer coprime to pr; return z^v */
     783             : static GEN
     784        4333 : powp(GEN nf, GEN pr, long v)
     785             : {
     786             :   GEN b, z;
     787             :   long e;
     788        4333 :   if (!v) return gen_1;
     789        4312 :   b = pr_get_tau(pr);
     790        4312 :   if (typ(b) == t_INT) return gen_1;
     791        1085 :   e = pr_get_e(pr);
     792        1085 :   z = gel(b,1);
     793        1085 :   if (e != 1) z = gdiv(nfpow_u(nf, z, e), powiu(pr_get_p(pr),e-1));
     794        1085 :   return nfpow_u(nf, z, v);
     795             : }
     796             : long
     797       15351 : nfvalrem(GEN nf, GEN x, GEN pr, GEN *py)
     798             : {
     799       15351 :   pari_sp av = avma;
     800             :   long w, e;
     801             :   GEN cx, p, t;
     802             : 
     803       15351 :   if (!py) return nfval(nf,x,pr);
     804       15232 :   if (gequal0(x)) { *py = gcopy(x); return LONG_MAX; }
     805       15218 :   nf = checknf(nf);
     806       15218 :   checkprid(pr);
     807       15218 :   p = pr_get_p(pr);
     808       15218 :   e = pr_get_e(pr);
     809       15218 :   x = nf_to_scalar_or_basis(nf, x);
     810       15218 :   if (typ(x) != t_COL) {
     811        3500 :     w = Q_pvalrem(x,p, py);
     812        3500 :     if (!w) { *py = gerepilecopy(av, x); return 0; }
     813        3346 :     *py = gerepileupto(av, gmul(powp(nf, pr, w), *py));
     814        3346 :     return e*w;
     815             :   }
     816       11718 :   x = Q_primitive_part(x, &cx);
     817       11718 :   w = ZC_nfvalrem(x,pr, py);
     818       11718 :   if (cx)
     819             :   {
     820         987 :     long v = Q_pvalrem(cx,p, &t);
     821         987 :     *py = nfmul(nf, *py, gmul(powp(nf,pr,v), t));
     822         987 :     *py = gerepileupto(av, *py);
     823         987 :     w += e*v;
     824             :   }
     825             :   else
     826       10731 :     *py = gerepilecopy(av, *py);
     827       11718 :   return w;
     828             : }
     829             : GEN
     830         147 : gpnfvalrem(GEN nf, GEN x, GEN pr, GEN *py)
     831             : {
     832         147 :   long v = nfvalrem(nf,x,pr,py);
     833         147 :   return v == LONG_MAX? mkoo(): stoi(v);
     834             : }
     835             : 
     836             : GEN
     837       49371 : coltoalg(GEN nf, GEN x)
     838             : {
     839       49371 :   return mkpolmod( coltoliftalg(nf, x), nf_get_pol(nf) );
     840             : }
     841             : 
     842             : GEN
     843       52444 : basistoalg(GEN nf, GEN x)
     844             : {
     845             :   GEN z, T;
     846             : 
     847       52444 :   nf = checknf(nf);
     848       52444 :   switch(typ(x))
     849             :   {
     850             :     case t_COL: {
     851       43253 :       pari_sp av = avma;
     852       43253 :       return gerepilecopy(av, coltoalg(nf, x));
     853             :     }
     854             :     case t_POLMOD:
     855         119 :       T = nf_get_pol(nf);
     856         119 :       if (!RgX_equal_var(T,gel(x,1)))
     857           0 :         pari_err_MODULUS("basistoalg", T,gel(x,1));
     858         119 :       return gcopy(x);
     859             :     case t_POL:
     860         574 :       T = nf_get_pol(nf);
     861         574 :       if (varn(T) != varn(x)) pari_err_VAR("basistoalg",x,T);
     862         574 :       z = cgetg(3,t_POLMOD);
     863         574 :       gel(z,1) = ZX_copy(T);
     864         574 :       gel(z,2) = RgX_rem(x, T); return z;
     865             :     case t_INT:
     866             :     case t_FRAC:
     867        8498 :       T = nf_get_pol(nf);
     868        8498 :       z = cgetg(3,t_POLMOD);
     869        8498 :       gel(z,1) = ZX_copy(T);
     870        8498 :       gel(z,2) = gcopy(x); return z;
     871             :     default:
     872           0 :       pari_err_TYPE("basistoalg",x);
     873           0 :       return NULL; /* not reached */
     874             :   }
     875             : }
     876             : 
     877             : /* Assume nf is a genuine nf. */
     878             : GEN
     879    17384511 : nf_to_scalar_or_basis(GEN nf, GEN x)
     880             : {
     881    17384511 :   switch(typ(x))
     882             :   {
     883             :     case t_INT: case t_FRAC:
     884    11129179 :       return x;
     885             :     case t_POLMOD:
     886       72611 :       x = checknfelt_mod(nf,x,"nf_to_scalar_or_basis");
     887       72541 :       if (typ(x) != t_POL) return x;
     888             :       /* fall through */
     889             :     case t_POL:
     890             :     {
     891      187391 :       GEN T = nf_get_pol(nf);
     892      187391 :       long l = lg(x);
     893      187391 :       if (varn(x) != varn(T)) pari_err_VAR("nf_to_scalar_or_basis", x,T);
     894      187335 :       if (l >= lg(T)) { x = RgX_rem(x, T); l = lg(x); }
     895      187335 :       if (l == 2) return gen_0;
     896      139875 :       if (l == 3) return gel(x,2);
     897      108746 :       return poltobasis(nf,x);
     898             :     }
     899             :     case t_COL:
     900     6016113 :       if (lg(x) != lg(nf_get_zk(nf))) break;
     901     6016050 :       return QV_isscalar(x)? gel(x,1): x;
     902             :   }
     903          70 :   pari_err_TYPE("nf_to_scalar_or_basis",x);
     904           0 :   return NULL; /* not reached */
     905             : }
     906             : /* Let x be a polynomial with coefficients in Q or nf. Return the same
     907             :  * polynomial with coefficients expressed as vectors (on the integral basis).
     908             :  * No consistency checks, not memory-clean. */
     909             : GEN
     910        2234 : RgX_to_nfX(GEN nf, GEN x)
     911             : {
     912             :   long i, l;
     913        2234 :   GEN y = cgetg_copy(x, &l); y[1] = x[1];
     914        2234 :   for (i=2; i<l; i++) gel(y,i) = nf_to_scalar_or_basis(nf, gel(x,i));
     915        2234 :   return y;
     916             : }
     917             : 
     918             : /* Assume nf is a genuine nf. */
     919             : GEN
     920       85595 : nf_to_scalar_or_alg(GEN nf, GEN x)
     921             : {
     922       85595 :   switch(typ(x))
     923             :   {
     924             :     case t_INT: case t_FRAC:
     925        5886 :       return x;
     926             :     case t_POLMOD:
     927        1141 :       x = checknfelt_mod(nf,x,"nf_to_scalar_or_alg");
     928        1141 :       if (typ(x) != t_POL) return x;
     929             :       /* fall through */
     930             :     case t_POL:
     931             :     {
     932       13315 :       GEN T = nf_get_pol(nf);
     933       13315 :       long l = lg(x);
     934       13315 :       if (varn(x) != varn(T)) pari_err_VAR("nf_to_scalar_or_alg", x,T);
     935       13315 :       if (l >= lg(T)) { x = RgX_rem(x, T); l = lg(x); }
     936       13315 :       if (l == 2) return gen_0;
     937       13315 :       if (l == 3) return gel(x,2);
     938       13105 :       return x;
     939             :     }
     940             :     case t_COL:
     941       66338 :       if (lg(x) != lg(nf_get_zk(nf))) break;
     942       66338 :       return QV_isscalar(x)? gel(x,1): coltoliftalg(nf, x);
     943             :   }
     944          49 :   pari_err_TYPE("nf_to_scalar_or_alg",x);
     945           0 :   return NULL; /* not reached */
     946             : }
     947             : 
     948             : /* gmul(A, RgX_to_RgC(x)), A t_MAT (or t_VEC) of compatible dimensions */
     949             : GEN
     950     1410348 : mulmat_pol(GEN A, GEN x)
     951             : {
     952             :   long i,l;
     953             :   GEN z;
     954     1410348 :   if (typ(x) != t_POL) return gmul(x,gel(A,1)); /* scalar */
     955     1410229 :   l=lg(x)-1; if (l == 1) return typ(A)==t_VEC? gen_0: zerocol(nbrows(A));
     956     1409742 :   x++; z = gmul(gel(x,1), gel(A,1));
     957     6791981 :   for (i=2; i<l ; i++)
     958     5382239 :     if (!gequal0(gel(x,i))) z = gadd(z, gmul(gel(x,i), gel(A,i)));
     959     1409742 :   return z;
     960             : }
     961             : 
     962             : /* x a t_POL, nf a genuine nf. No garbage collecting. No check.  */
     963             : GEN
     964     1277886 : poltobasis(GEN nf, GEN x)
     965             : {
     966     1277886 :   GEN P = nf_get_pol(nf);
     967     1277886 :   if (varn(x) != varn(P)) pari_err_VAR( "poltobasis", x,P);
     968     1277830 :   if (degpol(x) >= degpol(P)) x = RgX_rem(x,P);
     969     1277830 :   return mulmat_pol(nf_get_invzk(nf), x);
     970             : }
     971             : 
     972             : GEN
     973       64901 : algtobasis(GEN nf, GEN x)
     974             : {
     975             :   pari_sp av;
     976             : 
     977       64901 :   nf = checknf(nf);
     978       64901 :   switch(typ(x))
     979             :   {
     980             :     case t_POLMOD:
     981       39361 :       if (!RgX_equal_var(nf_get_pol(nf),gel(x,1)))
     982           7 :         pari_err_MODULUS("algtobasis", nf_get_pol(nf),gel(x,1));
     983       39354 :       x = gel(x,2);
     984       39354 :       switch(typ(x))
     985             :       {
     986             :         case t_INT:
     987        2898 :         case t_FRAC: return scalarcol(x, nf_get_degree(nf));
     988             :         case t_POL:
     989       36456 :           av = avma;
     990       36456 :           return gerepileupto(av,poltobasis(nf,x));
     991             :       }
     992           0 :       break;
     993             : 
     994             :     case t_POL:
     995        9402 :       av = avma;
     996        9402 :       return gerepileupto(av,poltobasis(nf,x));
     997             : 
     998             :     case t_COL:
     999        7381 :       if (lg(x)-1 != nf_get_degree(nf)) pari_err_DIM("nfalgtobasis");
    1000        7381 :       return gcopy(x);
    1001             : 
    1002             :     case t_INT:
    1003        8757 :     case t_FRAC: return scalarcol(x, nf_get_degree(nf));
    1004             :   }
    1005           0 :   pari_err_TYPE("algtobasis",x);
    1006           0 :   return NULL; /* not reached */
    1007             : }
    1008             : 
    1009             : GEN
    1010       35637 : rnfbasistoalg(GEN rnf,GEN x)
    1011             : {
    1012       35637 :   const char *f = "rnfbasistoalg";
    1013             :   long lx, i;
    1014       35637 :   pari_sp av = avma;
    1015             :   GEN z, nf, relpol, T;
    1016             : 
    1017       35637 :   checkrnf(rnf);
    1018       35637 :   nf = rnf_get_nf(rnf);
    1019       35637 :   T = nf_get_pol(nf);
    1020       35637 :   relpol = QXQX_to_mod_shallow(rnf_get_pol(rnf), T);
    1021       35637 :   switch(typ(x))
    1022             :   {
    1023             :     case t_COL:
    1024         798 :       z = cgetg_copy(x, &lx);
    1025        2338 :       for (i=1; i<lx; i++)
    1026             :       {
    1027        1589 :         GEN c = nf_to_scalar_or_alg(nf, gel(x,i));
    1028        1540 :         if (typ(c) == t_POL) c = mkpolmod(c,T);
    1029        1540 :         gel(z,i) = c;
    1030             :       }
    1031         749 :       z = RgV_RgC_mul(gel(rnf_get_zk(rnf),1), z);
    1032         686 :       return gerepileupto(av, gmodulo(z,relpol));
    1033             : 
    1034             :     case t_POLMOD:
    1035       23688 :       x = polmod_nffix(f, rnf, x, 0);
    1036       23478 :       if (typ(x) != t_POL) break;
    1037        9555 :       retmkpolmod(RgX_copy(x), RgX_copy(relpol));
    1038             :     case t_POL:
    1039         819 :       if (varn(x) == varn(T)) { RgX_check_QX(x,f); x = gmodulo(x,T); break; }
    1040         595 :       if (varn(x) == varn(relpol))
    1041             :       {
    1042         546 :         x = RgX_nffix(f,nf_get_pol(nf),x,0);
    1043         546 :         return gmodulo(x, relpol);
    1044             :       }
    1045          49 :       pari_err_VAR(f, x,relpol);
    1046             :   }
    1047       24430 :   retmkpolmod(scalarpol(x, varn(relpol)), RgX_copy(relpol));
    1048             : }
    1049             : 
    1050             : GEN
    1051         833 : matbasistoalg(GEN nf,GEN x)
    1052             : {
    1053             :   long i, j, li, lx;
    1054         833 :   GEN z = cgetg_copy(x, &lx);
    1055             : 
    1056         833 :   if (lx == 1) return z;
    1057         826 :   switch(typ(x))
    1058             :   {
    1059             :     case t_VEC: case t_COL:
    1060          28 :       for (i=1; i<lx; i++) gel(z,i) = basistoalg(nf, gel(x,i));
    1061          28 :       return z;
    1062         798 :     case t_MAT: break;
    1063           0 :     default: pari_err_TYPE("matbasistoalg",x);
    1064             :   }
    1065         798 :   li = lgcols(x);
    1066        2933 :   for (j=1; j<lx; j++)
    1067             :   {
    1068        2135 :     GEN c = cgetg(li,t_COL), xj = gel(x,j);
    1069        2135 :     gel(z,j) = c;
    1070        2135 :     for (i=1; i<li; i++) gel(c,i) = basistoalg(nf, gel(xj,i));
    1071             :   }
    1072         798 :   return z;
    1073             : }
    1074             : 
    1075             : GEN
    1076        2436 : matalgtobasis(GEN nf,GEN x)
    1077             : {
    1078             :   long i, j, li, lx;
    1079        2436 :   GEN z = cgetg_copy(x, &lx);
    1080             : 
    1081        2436 :   if (lx == 1) return z;
    1082        2380 :   switch(typ(x))
    1083             :   {
    1084             :     case t_VEC: case t_COL:
    1085        2373 :       for (i=1; i<lx; i++) gel(z,i) = algtobasis(nf, gel(x,i));
    1086        2373 :       return z;
    1087           7 :     case t_MAT: break;
    1088           0 :     default: pari_err_TYPE("matalgtobasis",x);
    1089             :   }
    1090           7 :   li = lgcols(x);
    1091          14 :   for (j=1; j<lx; j++)
    1092             :   {
    1093           7 :     GEN c = cgetg(li,t_COL), xj = gel(x,j);
    1094           7 :     gel(z,j) = c;
    1095           7 :     for (i=1; i<li; i++) gel(c,i) = algtobasis(nf, gel(xj,i));
    1096             :   }
    1097           7 :   return z;
    1098             : }
    1099             : GEN
    1100        3136 : RgM_to_nfM(GEN nf,GEN x)
    1101             : {
    1102             :   long i, j, li, lx;
    1103        3136 :   GEN z = cgetg_copy(x, &lx);
    1104             : 
    1105        3136 :   if (lx == 1) return z;
    1106        3136 :   li = lgcols(x);
    1107       22715 :   for (j=1; j<lx; j++)
    1108             :   {
    1109       19579 :     GEN c = cgetg(li,t_COL), xj = gel(x,j);
    1110       19579 :     gel(z,j) = c;
    1111       19579 :     for (i=1; i<li; i++) gel(c,i) = nf_to_scalar_or_basis(nf, gel(xj,i));
    1112             :   }
    1113        3136 :   return z;
    1114             : }
    1115             : GEN
    1116       36778 : RgC_to_nfC(GEN nf,GEN x)
    1117             : {
    1118       36778 :   long i, lx = lg(x);
    1119       36778 :   GEN z = cgetg(lx, t_COL);
    1120       36778 :   for (i=1; i<lx; i++) gel(z,i) = nf_to_scalar_or_basis(nf, gel(x,i));
    1121       36778 :   return z;
    1122             : }
    1123             : 
    1124             : /* x a t_POLMOD, supposedly in rnf = K[z]/(T), K = Q[y]/(Tnf) */
    1125             : GEN
    1126       60529 : polmod_nffix(const char *f, GEN rnf, GEN x, int lift)
    1127       60529 : { return polmod_nffix2(f, rnf_get_nfpol(rnf), rnf_get_pol(rnf), x,lift); }
    1128             : GEN
    1129       60620 : polmod_nffix2(const char *f, GEN T, GEN relpol, GEN x, int lift)
    1130             : {
    1131       60620 :   if (RgX_equal_var(gel(x,1),relpol))
    1132             :   {
    1133       55370 :     x = gel(x,2);
    1134       55370 :     if (typ(x) == t_POL && varn(x) == varn(relpol))
    1135             :     {
    1136       39963 :       x = RgX_nffix(f, T, x, lift);
    1137       39963 :       switch(lg(x))
    1138             :       {
    1139       11130 :         case 2: return gen_0;
    1140        3913 :         case 3: return gel(x,2);
    1141             :       }
    1142       24920 :       return x;
    1143             :     }
    1144             :   }
    1145       20657 :   return Rg_nffix(f, T, x, lift);
    1146             : }
    1147             : GEN
    1148        1176 : rnfalgtobasis(GEN rnf,GEN x)
    1149             : {
    1150        1176 :   const char *f = "rnfalgtobasis";
    1151        1176 :   pari_sp av = avma;
    1152             :   GEN T, relpol;
    1153             : 
    1154        1176 :   checkrnf(rnf);
    1155        1176 :   relpol = rnf_get_pol(rnf);
    1156        1176 :   T = rnf_get_nfpol(rnf);
    1157        1176 :   switch(typ(x))
    1158             :   {
    1159             :     case t_COL:
    1160          49 :       if (lg(x)-1 != rnf_get_degree(rnf)) pari_err_DIM(f);
    1161          28 :       x = RgV_nffix(f, T, x, 0);
    1162          21 :       return gerepilecopy(av, x);
    1163             : 
    1164             :     case t_POLMOD:
    1165        1043 :       x = polmod_nffix(f, rnf, x, 0);
    1166        1001 :       if (typ(x) != t_POL) break;
    1167         707 :       return gerepileupto(av, mulmat_pol(rnf_get_invzk(rnf), x));
    1168             :     case t_POL:
    1169          56 :       if (varn(x) == varn(T)) { RgX_check_QX(x,f); x = mkpolmod(x,T); break; }
    1170          35 :       x = RgX_nffix(f, T, x, 0);
    1171          28 :       if (degpol(x) >= degpol(relpol)) x = RgX_rem(x,relpol);
    1172          28 :       return gerepileupto(av, mulmat_pol(rnf_get_invzk(rnf), x));
    1173             :   }
    1174         336 :   return gerepileupto(av, scalarcol(x, rnf_get_degree(rnf)));
    1175             : }
    1176             : 
    1177             : /* Given a and b in nf, gives an algebraic integer y in nf such that a-b.y
    1178             :  * is "small" */
    1179             : GEN
    1180         259 : nfdiveuc(GEN nf, GEN a, GEN b)
    1181             : {
    1182         259 :   pari_sp av = avma;
    1183         259 :   a = nfdiv(nf,a,b);
    1184         259 :   return gerepileupto(av, ground(a));
    1185             : }
    1186             : 
    1187             : /* Given a and b in nf, gives a "small" algebraic integer r in nf
    1188             :  * of the form a-b.y */
    1189             : GEN
    1190         259 : nfmod(GEN nf, GEN a, GEN b)
    1191             : {
    1192         259 :   pari_sp av = avma;
    1193         259 :   GEN p1 = gneg_i(nfmul(nf,b,ground(nfdiv(nf,a,b))));
    1194         259 :   return gerepileupto(av, nfadd(nf,a,p1));
    1195             : }
    1196             : 
    1197             : /* Given a and b in nf, gives a two-component vector [y,r] in nf such
    1198             :  * that r=a-b.y is "small". */
    1199             : GEN
    1200         259 : nfdivrem(GEN nf, GEN a, GEN b)
    1201             : {
    1202         259 :   pari_sp av = avma;
    1203         259 :   GEN p1,z, y = ground(nfdiv(nf,a,b));
    1204             : 
    1205         259 :   p1 = gneg_i(nfmul(nf,b,y));
    1206         259 :   z = cgetg(3,t_VEC);
    1207         259 :   gel(z,1) = gcopy(y);
    1208         259 :   gel(z,2) = nfadd(nf,a,p1); return gerepileupto(av, z);
    1209             : }
    1210             : 
    1211             : /*************************************************************************/
    1212             : /**                                                                     **/
    1213             : /**                        REAL EMBEDDINGS                              **/
    1214             : /**                                                                     **/
    1215             : /*************************************************************************/
    1216             : static GEN
    1217       11830 : sarch_get_cyc(GEN sarch) { return gel(sarch,1); }
    1218             : static GEN
    1219      204865 : sarch_get_archp(GEN sarch) { return gel(sarch,2); }
    1220             : static GEN
    1221       44721 : sarch_get_MI(GEN sarch) { return gel(sarch,3); }
    1222             : static GEN
    1223       44721 : sarch_get_lambda(GEN sarch) { return gel(sarch,4); }
    1224             : static GEN
    1225       44721 : sarch_get_F(GEN sarch) { return gel(sarch,5); }
    1226             : 
    1227             : /* sigma_k(x) where 1 <= k <= r1+r2; correct but inefficient if x in Q.
    1228             :  * M = nf_get_M(nf) */
    1229             : static GEN
    1230       79646 : nfembed_i(GEN M, GEN x, long k)
    1231             : {
    1232       79646 :   long i, l = lg(M);
    1233       79646 :   GEN z = gel(x,1);
    1234       79646 :   for (i = 2; i < l; i++) z = gadd(z, gmul(gcoeff(M,k,i), gel(x,i)));
    1235       79646 :   return z;
    1236             : }
    1237             : GEN
    1238        1568 : nfembed(GEN nf, GEN x, long k)
    1239             : {
    1240        1568 :   pari_sp av = avma;
    1241        1568 :   nf = checknf(nf);
    1242        1568 :   x = nf_to_scalar_or_basis(nf,x);
    1243        1568 :   if (typ(x) != t_COL) return gerepilecopy(av, x);
    1244           0 :   return gerepileupto(av, nfembed_i(nf_get_M(nf),x,k));
    1245             : }
    1246             : 
    1247             : /* pl : requested signs for real embeddings, 0 = no sign constraint */
    1248             : /* FIXME: not rigorous */
    1249             : int
    1250         504 : nfchecksigns(GEN nf, GEN x, GEN pl)
    1251             : {
    1252         504 :   pari_sp av = avma;
    1253         504 :   long l = lg(pl), i;
    1254         504 :   nf = checknf(nf);
    1255         504 :   x = nf_to_scalar_or_basis(nf,x);
    1256         504 :   if (typ(x) != t_COL)
    1257             :   {
    1258         343 :     long s = gsigne(x);
    1259         763 :     for (i = 1; i < l; i++)
    1260         420 :       if (pl[i] && pl[i] != s) { avma = av; return 0; }
    1261             :   }
    1262             :   else
    1263             :   {
    1264         161 :     GEN M = nf_get_M(nf);
    1265         322 :     for (i = 1; i < l; i++)
    1266         203 :       if (pl[i] && pl[i] != gsigne(nfembed_i(M,x,i))) { avma = av; return 0; }
    1267             :   }
    1268         462 :   avma = av; return 1;
    1269             : }
    1270             : 
    1271             : /* signs = NULL: totally positive, else sign[i] = 0 (+) or 1 (-) */
    1272             : static GEN
    1273       44721 : get_C(GEN lambda, long l, GEN signs)
    1274             : {
    1275             :   long i;
    1276             :   GEN C, mlambda;
    1277       44721 :   if (!signs) return const_vec(l-1, lambda);
    1278        5262 :   C = cgetg(l, t_COL); mlambda = gneg(lambda);
    1279        5262 :   for (i = 1; i < l; i++) gel(C,i) = signs[i]? mlambda: lambda;
    1280        5262 :   return C;
    1281             : }
    1282             : static GEN
    1283       68955 : nfsetsigns(GEN nf, GEN signs, GEN x, GEN sarch)
    1284             : {
    1285       68955 :   long i, l = lg(sarch_get_archp(sarch));
    1286             :   GEN ex;
    1287             :   /* Is signature already correct ? */
    1288       68955 :   if (typ(x) != t_COL)
    1289             :   {
    1290        9490 :     long s = gsigne(x) < 0? 1: 0;
    1291        9490 :     if (!signs)
    1292        2373 :       i = (s == 1)? 1: l;
    1293             :     else
    1294             :     {
    1295       11583 :       for (i = 1; i < l; i++)
    1296        8699 :         if (signs[i] != s) break;
    1297             :     }
    1298        9490 :     ex = (i < l)? const_col(l-1, x): NULL;
    1299             :   }
    1300             :   else
    1301             :   {
    1302       59465 :     pari_sp av = avma;
    1303       59465 :     GEN M = nf_get_M(nf), archp = sarch_get_archp(sarch);
    1304       59465 :     ex = cgetg(l,t_COL);
    1305       59465 :     for (i = 1; i < l; i++) gel(ex,i) = nfembed_i(M,x,archp[i]);
    1306       59465 :     if (!signs)
    1307             :     {
    1308       84133 :       for (i = 1; i < l; i++)
    1309       66073 :         if (gsigne(gel(ex,i)) < 0) break;
    1310             :     }
    1311             :     else
    1312             :     {
    1313        3024 :       for (i = 1; i < l; i++)
    1314        2093 :         if (signs[i] != (gsigne(gel(ex,i)) < 0? 1: 0)) break;
    1315             :     }
    1316       59465 :     if (i == l) { ex = NULL; avma = av; }
    1317             :   }
    1318       68955 :   if (ex)
    1319             :   { /* If no, fix it */
    1320       44721 :     GEN lambda = sarch_get_lambda(sarch);
    1321       44721 :     GEN MI = sarch_get_MI(sarch);
    1322       44721 :     GEN F = sarch_get_F(sarch);
    1323       44721 :     GEN t = RgC_sub(get_C(lambda, l, signs), ex);
    1324             :     long e;
    1325       44721 :     t = grndtoi(RgM_RgC_mul(MI,t), &e);
    1326       44721 :     if (lg(F) != 1) t = ZM_ZC_mul(F, t);
    1327       44721 :     x = typ(x) == t_COL? RgC_add(t, x): RgC_Rg_add(t, x);
    1328             :   }
    1329       68955 :   return x;
    1330             : }
    1331             : /* - sarch = nfarchstar(nf, F);
    1332             :  * - x encodes a vector of signs at arch.archp: either a t_VECSMALL
    1333             :  *   (vector of signs as {0,1}-vector), NULL (totally positive at archp),
    1334             :  *   or a non-zero number field element (replaced by its signature at archp);
    1335             :  * - y is a non-zero number field element
    1336             :  * Return z = y (mod F) with signs(y, archp) = signs(x) (a {0,1}-vector) */
    1337             : GEN
    1338       76445 : set_sign_mod_divisor(GEN nf, GEN x, GEN y, GEN sarch)
    1339             : {
    1340       76445 :   GEN archp = sarch_get_archp(sarch);
    1341       76445 :   if (lg(archp) == 1) return y;
    1342       67688 :   nf = checknf(nf);
    1343       67688 :   if (x && typ(x) != t_VECSMALL) x = nfsign_arch(nf, x, archp);
    1344       67688 :   y = nf_to_scalar_or_basis(nf,y);
    1345       67688 :   return nfsetsigns(nf, x, y, sarch);
    1346             : }
    1347             : 
    1348             : static GEN
    1349        5465 : setsigns_init(GEN nf, GEN archp, GEN F, GEN DATA)
    1350             : {
    1351        5465 :   GEN lambda, Mr = rowpermute(nf_get_M(nf), archp), MI = F? RgM_mul(Mr,F): Mr;
    1352        5465 :   lambda = gmul2n(matrixnorm(MI,DEFAULTPREC), -1);
    1353        5465 :   if (lg(archp) < lg(MI))
    1354             :   {
    1355        4081 :     GEN perm = gel(indexrank(MI), 2);
    1356        4081 :     if (!F) F = matid(nf_get_degree(nf));
    1357        4081 :     MI = vecpermute(MI, perm);
    1358        4081 :     F = vecpermute(F, perm);
    1359             :   }
    1360        5465 :   if (!F) F = cgetg(1,t_MAT);
    1361        5465 :   MI = RgM_inv(MI);
    1362        5465 :   return mkvec5(DATA, archp, MI, lambda, F);
    1363             : }
    1364             : /* F non-0 integral ideal in HNF (or NULL: Z_K), compute elements in 1+F
    1365             :  * whose sign matrix at archp is identity; archp in 'indices' format */
    1366             : GEN
    1367        7502 : nfarchstar(GEN nf, GEN F, GEN archp)
    1368             : {
    1369        7502 :   long nba = lg(archp) - 1;
    1370        7502 :   if (!nba) return mkvec2(cgetg(1,t_VEC), archp);
    1371        4205 :   if (F && equali1(gcoeff(F,1,1))) F = NULL;
    1372        4205 :   if (F) F = idealpseudored(F, nf_get_roundG(nf));
    1373        4205 :   return setsigns_init(nf, archp, F, const_vec(nba, gen_2));
    1374             : }
    1375             : 
    1376             : /*************************************************************************/
    1377             : /**                                                                     **/
    1378             : /**                         IDEALCHINESE                                **/
    1379             : /**                                                                     **/
    1380             : /*************************************************************************/
    1381             : static int
    1382        1995 : isprfact(GEN x)
    1383             : {
    1384             :   long i, l;
    1385             :   GEN L, E;
    1386        1995 :   if (typ(x) != t_MAT || lg(x) != 3) return 0;
    1387        1995 :   L = gel(x,1); l = lg(L);
    1388        1995 :   E = gel(x,2);
    1389        4732 :   for(i=1; i<l; i++)
    1390             :   {
    1391        2737 :     checkprid(gel(L,i));
    1392        2737 :     if (typ(gel(E,i)) != t_INT) return 0;
    1393             :   }
    1394        1995 :   return 1;
    1395             : }
    1396             : 
    1397             : /* initialize projectors mod pr[i]^e[i] for idealchinese */
    1398             : static GEN
    1399        1995 : pr_init(GEN nf, GEN fa, GEN w, GEN dw)
    1400             : {
    1401        1995 :   GEN U, E, F, L = gel(fa,1), E0 = gel(fa,2);
    1402        1995 :   long i, r = lg(L);
    1403             : 
    1404        1995 :   if (w && lg(w) != r) pari_err_TYPE("idealchinese", w);
    1405        1995 :   if (r == 1 && !dw) return cgetg(1,t_VEC);
    1406        1988 :   E = leafcopy(E0); /* do not destroy fa[2] */
    1407        4725 :   for (i = 1; i < r; i++)
    1408        2737 :     if (signe(gel(E,i)) < 0) gel(E,i) = gen_0;
    1409        1988 :   F = factorbackprime(nf, L, E);
    1410        1988 :   if (dw)
    1411             :   {
    1412         686 :     F = ZM_Z_mul(F, dw);
    1413        1568 :     for (i = 1; i < r; i++)
    1414             :     {
    1415         882 :       GEN pr = gel(L,i);
    1416         882 :       long e = itos(gel(E0,i)), v = idealval(nf, dw, pr);
    1417         882 :       if (e >= 0)
    1418         875 :         gel(E,i) = addiu(gel(E,i), v);
    1419           7 :       else if (v + e <= 0)
    1420           0 :         F = idealmulpowprime(nf, F, pr, stoi(-v)); /* coprime to pr */
    1421             :       else
    1422             :       {
    1423           7 :         F = idealmulpowprime(nf, F, pr, stoi(e));
    1424           7 :         gel(E,i) = stoi(v + e);
    1425             :       }
    1426             :     }
    1427             :   }
    1428        1988 :   U = cgetg(r, t_VEC);
    1429        4725 :   for (i = 1; i < r; i++)
    1430             :   {
    1431             :     GEN u;
    1432        2737 :     if (w && gequal0(gel(w,i))) u = gen_0; /* unused */
    1433             :     else
    1434             :     {
    1435        2681 :       GEN pr = gel(L,i), e = gel(E,i), t;
    1436        2681 :       t = idealdivpowprime(nf,F, pr, e);
    1437        2681 :       u = hnfmerge_get_1(t, idealpow(nf, pr, e));
    1438        2681 :       if (!u) pari_err_COPRIME("idealchinese", t,pr);
    1439             :     }
    1440        2737 :     gel(U,i) = u;
    1441             :   }
    1442        1988 :   F = idealpseudored(F, nf_get_roundG(nf));
    1443        1988 :   return mkvec2(F, U);
    1444             : }
    1445             : 
    1446             : static GEN
    1447        1260 : pl_normalize(GEN nf, GEN pl)
    1448             : {
    1449        1260 :   const char *fun = "idealchinese";
    1450        1260 :   if (lg(pl)-1 != nf_get_r1(nf)) pari_err_TYPE(fun,pl);
    1451        1260 :   switch(typ(pl))
    1452             :   {
    1453         679 :     case t_VEC: RgV_check_ZV(pl,fun); pl = ZV_to_zv(pl);
    1454             :       /* fall through */
    1455        1260 :     case t_VECSMALL: break;
    1456           0 :     default: pari_err_TYPE(fun,pl);
    1457             :   }
    1458        1260 :   return pl;
    1459             : }
    1460             : 
    1461             : static int
    1462        4753 : is_chineseinit(GEN x)
    1463             : {
    1464             :   GEN fa, pl;
    1465             :   long l;
    1466        4753 :   if (typ(x) != t_VEC || lg(x)!=3) return 0;
    1467        3528 :   fa = gel(x,1);
    1468        3528 :   pl = gel(x,2);
    1469        3528 :   if (typ(fa) != t_VEC || typ(pl) != t_VEC) return 0;
    1470        1484 :   l = lg(fa);
    1471        1484 :   if (l != 1)
    1472             :   {
    1473        1463 :     if (l != 3 || typ(gel(fa,1)) != t_MAT || typ(gel(fa,2)) != t_VEC)
    1474           7 :       return 0;
    1475             :   }
    1476        1477 :   l = lg(pl);
    1477        1477 :   if (l != 1)
    1478             :   {
    1479         476 :     if (l != 6 || typ(gel(pl,3)) != t_MAT || typ(gel(pl,1)) != t_VECSMALL
    1480         476 :                                           || typ(gel(pl,2)) != t_VECSMALL)
    1481           0 :       return 0;
    1482             :   }
    1483        1477 :   return 1;
    1484             : }
    1485             : 
    1486             : /* nf a true 'nf' */
    1487             : static GEN
    1488        2058 : chineseinit_i(GEN nf, GEN fa, GEN w, GEN dw)
    1489             : {
    1490        2058 :   const char *fun = "idealchineseinit";
    1491        2058 :   GEN archp = NULL, pl = NULL;
    1492        2058 :   switch(typ(fa))
    1493             :   {
    1494             :     case t_VEC:
    1495        1260 :       if (is_chineseinit(fa))
    1496             :       {
    1497           0 :         if (dw) pari_err_DOMAIN(fun, "denom(y)", "!=", gen_1, w);
    1498           0 :         return fa;
    1499             :       }
    1500        1260 :       if (lg(fa) != 3) pari_err_TYPE(fun, fa);
    1501             :       /* of the form [x,s] */
    1502        1260 :       pl = pl_normalize(nf, gel(fa,2));
    1503        1260 :       fa = gel(fa,1);
    1504        1260 :       archp = vecsmall01_to_indices(pl);
    1505             :       /* keep pr_init, reset pl */
    1506        1260 :       if (is_chineseinit(fa)) { fa = gel(fa,1); break; }
    1507             :       /* fall through */
    1508             :     case t_MAT: /* factorization? */
    1509        1995 :       if (isprfact(fa)) { fa = pr_init(nf, fa, w, dw); break; }
    1510           0 :     default: pari_err_TYPE(fun,fa);
    1511             :   }
    1512             : 
    1513        2058 :   if (pl)
    1514             :   {
    1515        1260 :     GEN F = (lg(fa) == 1)? NULL: gel(fa,1);
    1516        1260 :     long i, r = lg(archp);
    1517        1260 :     GEN signs = cgetg(r, t_VECSMALL);
    1518        1260 :     for (i = 1; i < r; i++) signs[i] = (pl[archp[i]] < 0)? 1: 0;
    1519        1260 :     pl = setsigns_init(nf, archp, F, signs);
    1520             :   }
    1521             :   else
    1522         798 :     pl = cgetg(1,t_VEC);
    1523        2058 :   return mkvec2(fa, pl);
    1524             : }
    1525             : 
    1526             : /* Given a prime ideal factorization x, possibly with 0 or negative exponents,
    1527             :  * and a vector w of elements of nf, gives b such that
    1528             :  * v_p(b-w_p)>=v_p(x) for all prime ideals p in the ideal factorization
    1529             :  * and v_p(b)>=0 for all other p, using the standard proof given in GTM 138. */
    1530             : GEN
    1531        3472 : idealchinese(GEN nf, GEN x, GEN w)
    1532             : {
    1533        3472 :   const char *fun = "idealchinese";
    1534        3472 :   pari_sp av = avma;
    1535             :   GEN x1, x2, s, dw, F;
    1536             : 
    1537        3472 :   nf = checknf(nf);
    1538        3472 :   if (!w) return gerepilecopy(av, chineseinit_i(nf,x,NULL,NULL));
    1539             : 
    1540        2233 :   if (typ(w) != t_VEC) pari_err_TYPE(fun,w);
    1541        2233 :   w = Q_remove_denom(matalgtobasis(nf,w), &dw);
    1542        2233 :   if (!is_chineseinit(x)) x = chineseinit_i(nf,x,w,dw);
    1543             :   /* x is a 'chineseinit' */
    1544        2233 :   x1 = gel(x,1); s = NULL;
    1545        2233 :   if (lg(x1) == 1) F = NULL;
    1546             :   else
    1547             :   {
    1548        2212 :     GEN  U = gel(x1,2);
    1549        2212 :     long i, r = lg(w);
    1550        2212 :     F = gel(x1,1);
    1551        5439 :     for (i=1; i<r; i++)
    1552        3227 :       if (!gequal0(gel(w,i)))
    1553             :       {
    1554        2702 :         GEN t = nfmuli(nf, gel(U,i), gel(w,i));
    1555        2702 :         s = s? ZC_add(s,t): t;
    1556             :       }
    1557        2212 :     if (s) s = ZC_reducemodmatrix(s, F);
    1558             :   }
    1559        2233 :   if (!s) { s = zerocol(nf_get_degree(nf)); dw = NULL; }
    1560             : 
    1561        2233 :   x2 = gel(x,2);
    1562        2233 :   if (lg(x2) != 1) s = nfsetsigns(nf, gel(x2,1), s, x2);
    1563        2233 :   if (dw) s = RgC_Rg_div(s,dw);
    1564        2233 :   return gerepileupto(av, s);
    1565             : }
    1566             : 
    1567             : /*************************************************************************/
    1568             : /**                                                                     **/
    1569             : /**                           (Z_K/I)^*                                 **/
    1570             : /**                                                                     **/
    1571             : /*************************************************************************/
    1572             : GEN
    1573        1260 : vecsmall01_to_indices(GEN v)
    1574             : {
    1575        1260 :   long i, k, l = lg(v);
    1576        1260 :   GEN p = new_chunk(l) + l;
    1577        3668 :   for (k=1, i=l-1; i; i--)
    1578        2408 :     if (v[i]) { *--p = i; k++; }
    1579        1260 :   *--p = evallg(k) | evaltyp(t_VECSMALL);
    1580        1260 :   avma = (pari_sp)p; return p;
    1581             : }
    1582             : GEN
    1583      320971 : vec01_to_indices(GEN v)
    1584             : {
    1585             :   long i, k, l;
    1586             :   GEN p;
    1587             : 
    1588      320971 :   switch (typ(v))
    1589             :   {
    1590      315098 :    case t_VECSMALL: return v;
    1591        5873 :    case t_VEC: break;
    1592           0 :    default: pari_err_TYPE("vec01_to_indices",v);
    1593             :   }
    1594        5873 :   l = lg(v);
    1595        5873 :   p = new_chunk(l) + l;
    1596       16184 :   for (k=1, i=l-1; i; i--)
    1597       10311 :     if (signe(gel(v,i))) { *--p = i; k++; }
    1598        5873 :   *--p = evallg(k) | evaltyp(t_VECSMALL);
    1599        5873 :   avma = (pari_sp)p; return p;
    1600             : }
    1601             : GEN
    1602        4718 : indices_to_vec01(GEN p, long r)
    1603             : {
    1604        4718 :   long i, l = lg(p);
    1605        4718 :   GEN v = zerovec(r);
    1606        4718 :   for (i = 1; i < l; i++) gel(v, p[i]) = gen_1;
    1607        4718 :   return v;
    1608             : }
    1609             : 
    1610             : /* return v such that (-1)^v = sign(sigma_k(x)), x primitive ZC */
    1611             : static long
    1612      303512 : eval_sign(GEN M, GEN x, long k)
    1613             : {
    1614      303512 :   long i, l = lg(x);
    1615      303512 :   GEN z = gel(x,1); /* times M[k,1], which is 1 */
    1616      303512 :   for (i = 2; i < l; i++) z = mpadd(z, mpmul(gcoeff(M,k,i), gel(x,i)));
    1617      303512 :   if (realprec(z) == LOWDEFAULTPREC) return -1; /* dubious, fail */
    1618      302942 :   return (signe(z) < 1)? 1: 0;
    1619             : }
    1620             : /* return (column) vector of R1 signatures of x (0 or 1) */
    1621             : GEN
    1622      315098 : nfsign_arch(GEN nf, GEN x, GEN arch)
    1623             : {
    1624      315098 :   GEN sarch, charx, M, V, archp = vec01_to_indices(arch);
    1625      315098 :   long i, s, np, n = lg(archp)-1;
    1626             :   pari_sp av;
    1627             : 
    1628      315098 :   if (!n) return cgetg(1,t_VECSMALL);
    1629      315028 :   nf = checknf(nf);
    1630      315028 :   if (typ(x) == t_MAT)
    1631             :   { /* factorisation */
    1632       92012 :     GEN g = gel(x,1), e = gel(x,2);
    1633       92012 :     V = zero_zv(n);
    1634      274755 :     for (i=1; i<lg(g); i++)
    1635      182743 :       if (mpodd(gel(e,i)))
    1636      157620 :         Flv_add_inplace(V, nfsign_arch(nf,gel(g,i),archp), 2);
    1637       92012 :     avma = (pari_sp)V; return V;
    1638             :   }
    1639      223016 :   av = avma; V = cgetg(n+1,t_VECSMALL);
    1640      223016 :   x = nf_to_scalar_or_basis(nf, x);
    1641      223016 :   switch(typ(x))
    1642             :   {
    1643             :     case t_INT:
    1644       46330 :       s = signe(x);
    1645       46330 :       if (!s) pari_err_DOMAIN("nfsign_arch","element","=",gen_0,x);
    1646       46330 :       avma = av; return const_vecsmall(n, (s < 0)? 1: 0);
    1647             :     case t_FRAC:
    1648           0 :       s = signe(gel(x,1));
    1649           0 :       avma = av; return const_vecsmall(n, (s < 0)? 1: 0);
    1650             :   }
    1651      176686 :   x = Q_primpart(x); M = nf_get_M(nf); sarch = charx = NULL; np = 0;
    1652      479628 :   for (i = 1; i <= n; i++)
    1653             :   {
    1654      303512 :     long s = eval_sign(M, x, archp[i]);
    1655      303512 :     if (s < 0) /* failure */
    1656             :     {
    1657         570 :       GEN xi, chari, T = nf_get_pol(nf);
    1658         570 :       long ni, N = degpol(T), r1 = nf_get_r1(nf);
    1659         570 :       if (!charx)
    1660             :       {
    1661         570 :         charx = ZXQ_charpoly(coltoliftalg(nf,x), T, 0);
    1662         570 :         charx = ZX_radical(charx);
    1663         570 :         np = ZX_sturmpart(charx, mkvec2(gen_0,mkoo()));
    1664         570 :         np *= N / degpol(charx);
    1665         570 :         if (np == 0) { avma = av; return const_vecsmall(n, 1); }
    1666         432 :         if (np == r1){ avma = av; return const_vecsmall(n, 0); }
    1667             :       }
    1668         306 :       if (!sarch) sarch = nfarchstar(nf, NULL, identity_perm(r1));
    1669         306 :       xi = set_sign_mod_divisor(nf, vecsmall_ei(r1, archp[i]), gen_1, sarch);
    1670         306 :       xi = Q_primpart(xi);
    1671         306 :       chari = ZXQ_charpoly(coltoliftalg(nf,nfmuli(nf,x,xi)), T, 0);
    1672         306 :       chari = ZX_radical(chari);
    1673         306 :       ni = ZX_sturmpart(chari, mkvec2(gen_0,mkoo()));
    1674         306 :       ni *= N / degpol(chari);
    1675         306 :       if (ni == 0) { avma = av; V = const_vecsmall(n, 1); V[i] = 0; return V; }
    1676         168 :       if (ni == r1){ avma = av; V = const_vecsmall(n, 0); V[i] = 1; return V; }
    1677           0 :       s = ni < np? 0: 1;
    1678             :     }
    1679      302942 :     V[i] = s;
    1680             :   }
    1681      176116 :   avma = (pari_sp)V; return V;
    1682             : }
    1683             : 
    1684             : /* return the vector of signs of x; the matrix of such if x is a vector
    1685             :  * of nf elements */
    1686             : GEN
    1687         490 : nfsign(GEN nf, GEN x)
    1688             : {
    1689             :   long i, l;
    1690             :   GEN archp, S;
    1691             : 
    1692         490 :   nf = checknf(nf);
    1693         490 :   archp = identity_perm( nf_get_r1(nf) );
    1694         490 :   if (typ(x) != t_VEC) return nfsign_arch(nf, x, archp);
    1695         182 :   l = lg(x); S = cgetg(l, t_MAT);
    1696         182 :   for (i=1; i<l; i++) gel(S,i) = nfsign_arch(nf, gel(x,i), archp);
    1697         182 :   return S;
    1698             : }
    1699             : 
    1700             : /* x integral elt, A integral ideal in HNF; reduce x mod A */
    1701             : static GEN
    1702      561896 : zk_modHNF(GEN x, GEN A)
    1703      561896 : { return (typ(x) == t_COL)?  ZC_hnfrem(x, A): modii(x, gcoeff(A,1,1)); }
    1704             : 
    1705             : /* given an element x in Z_K and an integral ideal y in HNF, coprime with x,
    1706             :    outputs an element inverse of x modulo y */
    1707             : GEN
    1708         126 : nfinvmodideal(GEN nf, GEN x, GEN y)
    1709             : {
    1710         126 :   pari_sp av = avma;
    1711         126 :   GEN a, yZ = gcoeff(y,1,1);
    1712             : 
    1713         126 :   if (is_pm1(yZ)) return gen_0;
    1714         126 :   x = nf_to_scalar_or_basis(nf, x);
    1715         126 :   if (typ(x) == t_INT) return gerepileupto(av, Fp_inv(x, yZ));
    1716             : 
    1717          56 :   a = hnfmerge_get_1(idealhnf_principal(nf,x), y);
    1718          56 :   if (!a) pari_err_INV("nfinvmodideal", x);
    1719          56 :   return gerepileupto(av, zk_modHNF(nfdiv(nf,a,x), y));
    1720             : }
    1721             : 
    1722             : static GEN
    1723      269660 : nfsqrmodideal(GEN nf, GEN x, GEN id)
    1724      269660 : { return zk_modHNF(nfsqri(nf,x), id); }
    1725             : static GEN
    1726      622870 : nfmulmodideal(GEN nf, GEN x, GEN y, GEN id)
    1727      622870 : { return x? zk_modHNF(nfmuli(nf,x,y), id): y; }
    1728             : /* assume x integral, k integer, A in HNF */
    1729             : GEN
    1730      383103 : nfpowmodideal(GEN nf,GEN x,GEN k,GEN A)
    1731             : {
    1732      383103 :   long s = signe(k);
    1733             :   pari_sp av;
    1734             :   GEN y;
    1735             : 
    1736      383103 :   if (!s) return gen_1;
    1737      383103 :   av = avma;
    1738      383103 :   x = nf_to_scalar_or_basis(nf, x);
    1739      383103 :   if (typ(x) != t_COL) return Fp_pow(x, k, gcoeff(A,1,1));
    1740      205414 :   if (s < 0) { x = nfinvmodideal(nf, x,A); k = absi(k); }
    1741      205414 :   for(y = NULL;;)
    1742             :   {
    1743      475074 :     if (mpodd(k)) y = nfmulmodideal(nf,y,x,A);
    1744      475074 :     k = shifti(k,-1); if (!signe(k)) break;
    1745      269660 :     x = nfsqrmodideal(nf,x,A);
    1746      269660 :   }
    1747      205414 :   return gerepileupto(av, y);
    1748             : }
    1749             : 
    1750             : /* a * g^n mod id */
    1751             : static GEN
    1752      340326 : nfmulpowmodideal(GEN nf, GEN a, GEN g, GEN n, GEN id)
    1753             : {
    1754      340326 :   return nfmulmodideal(nf, a, nfpowmodideal(nf,g,n,id), id);
    1755             : }
    1756             : 
    1757             : /* assume (num(g[i]), id) = 1 for all i. Return prod g[i]^e[i] mod id.
    1758             :  * EX = multiple of exponent of (O_K/id)^* */
    1759             : GEN
    1760      128034 : famat_to_nf_modideal_coprime(GEN nf, GEN g, GEN e, GEN id, GEN EX)
    1761             : {
    1762      128034 :   GEN EXo2, plus = NULL, minus = NULL, idZ = gcoeff(id,1,1);
    1763      128034 :   long i, lx = lg(g);
    1764             : 
    1765      128034 :   if (is_pm1(idZ)) return gen_1; /* id = Z_K */
    1766      128034 :   EXo2 = (expi(EX) > 10)? shifti(EX,-1): NULL;
    1767      704905 :   for (i = 1; i < lx; i++)
    1768             :   {
    1769      576871 :     GEN h, n = centermodii(gel(e,i), EX, EXo2);
    1770      576871 :     long sn = signe(n);
    1771      576871 :     if (!sn) continue;
    1772             : 
    1773      253765 :     h = nf_to_scalar_or_basis(nf, gel(g,i));
    1774      253765 :     switch(typ(h))
    1775             :     {
    1776      146371 :       case t_INT: break;
    1777             :       case t_FRAC:
    1778           0 :         h = Fp_div(gel(h,1), gel(h,2), idZ); break;
    1779             :       default:
    1780             :       {
    1781             :         GEN dh;
    1782      107394 :         h = Q_remove_denom(h, &dh);
    1783      107394 :         if (dh) h = FpC_Fp_mul(h, Fp_inv(dh,idZ), idZ);
    1784             :       }
    1785             :     }
    1786      253765 :     if (sn > 0)
    1787      252547 :       plus = nfmulpowmodideal(nf, plus, h, n, id);
    1788             :     else /* sn < 0 */
    1789        1218 :       minus = nfmulpowmodideal(nf, minus, h, absi(n), id);
    1790             :   }
    1791      128034 :   if (minus) plus = nfmulmodideal(nf, plus, nfinvmodideal(nf,minus,id), id);
    1792      128034 :   return plus? plus: gen_1;
    1793             : }
    1794             : 
    1795             : /* given 2 integral ideals x, y in HNF s.t x | y | x^2, compute (1+x)/(1+y) in
    1796             :  * the form [[cyc],[gen], U], where U := ux^-1 as a pair [ZM, denom(U)] */
    1797             : static GEN
    1798       12887 : zidealij(GEN x, GEN y)
    1799             : {
    1800       12887 :   GEN U, G, cyc, xp = gcoeff(x,1,1), xi = hnf_invscale(x, xp);
    1801             :   long j, N;
    1802             : 
    1803             :   /* x^(-1) y = relations between the 1 + x_i (HNF) */
    1804       12887 :   cyc = ZM_snf_group(ZM_Z_divexact(ZM_mul(xi, y), xp), &U, &G);
    1805       12887 :   N = lg(cyc); G = ZM_mul(x,G); settyp(G, t_VEC); /* new generators */
    1806       61040 :   for (j=1; j<N; j++)
    1807             :   {
    1808       48153 :     GEN c = gel(G,j);
    1809       48153 :     gel(c,1) = addiu(gel(c,1), 1); /* 1 + g_j */
    1810       48153 :     if (ZV_isscalar(c)) gel(G,j) = gel(c,1);
    1811             :   }
    1812       12887 :   return mkvec4(cyc, G, ZM_mul(U,xi), xp);
    1813             : }
    1814             : 
    1815             : /* lg(x) > 1, x + 1; shallow */
    1816             : static GEN
    1817        2688 : ZC_add1(GEN x)
    1818             : {
    1819        2688 :   long i, l = lg(x);
    1820        2688 :   GEN y = cgetg(l, t_COL);
    1821        2688 :   for (i = 2; i < l; i++) gel(y,i) = gel(x,i);
    1822        2688 :   gel(y,1) = addiu(gel(x,1), 1); return y;
    1823             : }
    1824             : /* lg(x) > 1, x - 1; shallow */
    1825             : static GEN
    1826        1540 : ZC_sub1(GEN x)
    1827             : {
    1828        1540 :   long i, l = lg(x);
    1829        1540 :   GEN y = cgetg(l, t_COL);
    1830        1540 :   for (i = 2; i < l; i++) gel(y,i) = gel(x,i);
    1831        1540 :   gel(y,1) = subiu(gel(x,1), 1); return y;
    1832             : }
    1833             : 
    1834             : /* x,y are t_INT or ZC */
    1835             : static GEN
    1836           0 : zkadd(GEN x, GEN y)
    1837             : {
    1838           0 :   long tx = typ(x);
    1839           0 :   if (tx == typ(y))
    1840           0 :     return tx == t_INT? addii(x,y): ZC_add(x,y);
    1841             :   else
    1842           0 :     return tx == t_INT? ZC_Z_add(y,x): ZC_Z_add(x,y);
    1843             : }
    1844             : /* x a t_INT or ZC, x+1; shallow */
    1845             : static GEN
    1846        3192 : zkadd1(GEN x)
    1847             : {
    1848        3192 :   long tx = typ(x);
    1849        3192 :   return tx == t_INT? addiu(x,1): ZC_add1(x);
    1850             : }
    1851             : /* x a t_INT or ZC, x-1; shallow */
    1852             : static GEN
    1853        3192 : zksub1(GEN x)
    1854             : {
    1855        3192 :   long tx = typ(x);
    1856        3192 :   return tx == t_INT? subiu(x,1): ZC_sub1(x);
    1857             : }
    1858             : /* x,y are t_INT or ZC; x - y */
    1859             : static GEN
    1860           0 : zksub(GEN x, GEN y)
    1861             : {
    1862           0 :   long tx = typ(x), ty = typ(y);
    1863           0 :   if (tx == ty)
    1864           0 :     return tx == t_INT? subii(x,y): ZC_sub(x,y);
    1865             :   else
    1866           0 :     return tx == t_INT? Z_ZC_sub(x,y): ZC_Z_sub(x,y);
    1867             : }
    1868             : /* x is t_INT or ZM (mult. map), y is t_INT or ZC; x * y */
    1869             : static GEN
    1870        3192 : zkmul(GEN x, GEN y)
    1871             : {
    1872        3192 :   long tx = typ(x), ty = typ(y);
    1873        3192 :   if (ty == t_INT)
    1874        1652 :     return tx == t_INT? mulii(x,y): ZC_Z_mul(gel(x,1),y);
    1875             :   else
    1876        1540 :     return tx == t_INT? ZC_Z_mul(y,x): ZM_ZC_mul(x,y);
    1877             : }
    1878             : 
    1879             : /* (U,V) = 1 coprime ideals. Want z = x mod U, = y mod V; namely
    1880             :  * z =vx + uy = v(x-y) + y, where u + v = 1, u in U, v in V.
    1881             :  * zkc = [v, UV], v a t_INT or ZM (mult. by v map), UV a ZM (ideal in HNF);
    1882             :  * shallow */
    1883             : GEN
    1884           0 : zkchinese(GEN zkc, GEN x, GEN y)
    1885             : {
    1886           0 :   GEN v = gel(zkc,1), UV = gel(zkc,2), z = zkadd(zkmul(v, zksub(x,y)), y);
    1887           0 :   return zk_modHNF(z, UV);
    1888             : }
    1889             : /* special case z = x mod U, = 1 mod V; shallow */
    1890             : GEN
    1891        3192 : zkchinese1(GEN zkc, GEN x)
    1892             : {
    1893        3192 :   GEN v = gel(zkc,1), UV = gel(zkc,2), z = zkadd1(zkmul(v, zksub1(x)));
    1894        3192 :   return (typ(z) == t_INT)? z: ZC_hnfrem(z, UV);
    1895             : }
    1896             : static GEN
    1897        2457 : zkVchinese1(GEN zkc, GEN v)
    1898             : {
    1899             :   long i, ly;
    1900        2457 :   GEN y = cgetg_copy(v, &ly);
    1901        2457 :   for (i=1; i<ly; i++) gel(y,i) = zkchinese1(zkc, gel(v,i));
    1902        2457 :   return y;
    1903             : }
    1904             : 
    1905             : /* prepare to solve z = x (mod A), z = y mod (B) [zkchinese or zkchinese1] */
    1906             : GEN
    1907        2198 : zkchineseinit(GEN nf, GEN A, GEN B, GEN AB)
    1908             : {
    1909             :   GEN v;
    1910        2198 :   nf = checknf(nf);
    1911        2198 :   v = idealaddtoone_i(nf, A, B);
    1912        2198 :   return mkvec2(zk_scalar_or_multable(nf,v), AB);
    1913             : }
    1914             : /* prepare to solve z = x (mod A), z = 1 mod (B)
    1915             :  * and then         z = 1 (mod A), z = y mod (B) [zkchinese1 twice] */
    1916             : static GEN
    1917         259 : zkchinese1init2(GEN nf, GEN A, GEN B, GEN AB)
    1918             : {
    1919         259 :   GEN zkc = zkchineseinit(nf, A, B, AB);
    1920         259 :   GEN mv = gel(zkc,1), mu;
    1921         259 :   if (typ(mv) == t_INT) return mkvec2(zkc, mkvec2(subui(1,mv),AB));
    1922         238 :   mu = RgM_Rg_add_shallow(ZM_neg(mv), gen_1);
    1923         238 :   return mkvec2(mkvec2(mv,AB), mkvec2(mu,AB));
    1924             : }
    1925             : 
    1926             : static GEN
    1927      276645 : apply_U(GEN L, GEN a)
    1928             : {
    1929      276645 :   GEN e, U = gel(L,3), dU = gel(L,4);
    1930      276645 :   if (typ(a) == t_INT)
    1931       84158 :     e = ZC_Z_mul(gel(U,1), subiu(a, 1));
    1932             :   else
    1933             :   { /* t_COL */
    1934      192487 :     GEN t = gel(a,1);
    1935      192487 :     gel(a,1) = subiu(gel(a,1), 1); /* a -= 1 */
    1936      192487 :     e = ZM_ZC_mul(U, a);
    1937      192487 :     gel(a,1) = t; /* restore */
    1938             :   }
    1939      276645 :   return gdiv(e, dU);
    1940             : }
    1941             : 
    1942             : /* vectors of [[cyc],[g],U.X^-1]. Assume k > 1. */
    1943             : static GEN
    1944        8456 : principal_units(GEN nf, GEN pr, long k, GEN prk)
    1945             : {
    1946             :   GEN list, prb;
    1947        8456 :   ulong mask = quadratic_prec_mask(k);
    1948        8456 :   long a = 1;
    1949             : 
    1950        8456 :   if (DEBUGLEVEL>3) err_printf("treating pr^%ld, pr = %Ps\n",k,pr);
    1951        8456 :   prb = idealhnf_two(nf,pr);
    1952        8456 :   list = vectrunc_init(k);
    1953       29799 :   while (mask > 1)
    1954             :   {
    1955       12887 :     GEN pra = prb;
    1956       12887 :     long b = a << 1;
    1957             : 
    1958       12887 :     if (mask & 1) b--;
    1959       12887 :     mask >>= 1;
    1960             :     /* compute 1 + pr^a / 1 + pr^b, 2a <= b */
    1961       12887 :     if(DEBUGLEVEL>3) err_printf("  treating a = %ld, b = %ld\n",a,b);
    1962       12887 :     prb = (b >= k)? prk: idealpows(nf,pr,b);
    1963       12887 :     vectrunc_append(list, zidealij(pra, prb));
    1964       12887 :     a = b;
    1965             :   }
    1966        8456 :   return list;
    1967             : }
    1968             : /* a = 1 mod (pr) return log(a) on local-gens of 1+pr/1+pr^k */
    1969             : static GEN
    1970      173080 : log_prk1(GEN nf, GEN a, long nh, GEN L2, GEN prk)
    1971             : {
    1972      173080 :   GEN y = cgetg(nh+1, t_COL);
    1973      173080 :   long j, iy, c = lg(L2)-1;
    1974      449718 :   for (j = iy = 1; j <= c; j++)
    1975             :   {
    1976      276645 :     GEN L = gel(L2,j), cyc = gel(L,1), gen = gel(L,2), E = apply_U(L,a);
    1977      276645 :     long i, nc = lg(cyc)-1;
    1978      276645 :     int last = (j == c);
    1979     1075448 :     for (i = 1; i <= nc; i++, iy++)
    1980             :     {
    1981      798810 :       GEN t, e = gel(E,i);
    1982      798810 :       if (typ(e) != t_INT) pari_err_COPRIME("zlog_prk1", a, prk);
    1983      798803 :       t = Fp_neg(e, gel(cyc,i));
    1984      798803 :       gel(y,iy) = negi(t);
    1985      798803 :       if (!last && signe(t)) a = nfmulpowmodideal(nf, a, gel(gen,i), t, prk);
    1986             :     }
    1987             :   }
    1988      173073 :   return y;
    1989             : }
    1990             : static GEN
    1991        3780 : principal_units_relations(GEN nf, GEN L2, GEN prk, long nh)
    1992             : {
    1993        3780 :   GEN h = cgetg(nh+1,t_MAT);
    1994        3780 :   long ih, j, c = lg(L2)-1;
    1995       11991 :   for (j = ih = 1; j <= c; j++)
    1996             :   {
    1997        8211 :     GEN L = gel(L2,j), F = gel(L,1), G = gel(L,2);
    1998        8211 :     long k, lG = lg(G);
    1999       42623 :     for (k = 1; k < lG; k++,ih++)
    2000             :     { /* log(g^f) mod pr^e */
    2001       34412 :       GEN a = nfpowmodideal(nf,gel(G,k),gel(F,k),prk);
    2002       34412 :       gel(h,ih) = ZC_neg(log_prk1(nf, a, nh, L2, prk));
    2003       34412 :       gcoeff(h,ih,ih) = gel(F,k);
    2004             :     }
    2005             :   }
    2006        3780 :   return h;
    2007             : }
    2008             : /* e > 1; multiplicative group (1 + pr) / (1 + pr^k), prk = pr^k or NULL */
    2009             : static GEN
    2010        8456 : idealprincipalunits_i(GEN nf, GEN pr, long k, GEN *pU)
    2011             : {
    2012        8456 :   GEN cyc, gen, L2, prk = idealpows(nf, pr, k);
    2013             : 
    2014        8456 :   L2 = principal_units(nf, pr, k, prk);
    2015        8456 :   if (k == 2)
    2016             :   {
    2017        4676 :     GEN L = gel(L2,1);
    2018        4676 :     cyc = gel(L,1);
    2019        4676 :     gen = gel(L,2);
    2020        4676 :     if (pU) *pU = matid(lg(gen)-1);
    2021             :   }
    2022             :   else
    2023             :   {
    2024        3780 :     long c = lg(L2), j;
    2025        3780 :     GEN EX, h, Ui, vg = cgetg(c, t_VEC);
    2026        3780 :     for (j = 1; j < c; j++) gel(vg, j) = gmael(L2,j,2);
    2027        3780 :     vg = shallowconcat1(vg);
    2028        3780 :     h = principal_units_relations(nf, L2, prk, lg(vg)-1);
    2029        3780 :     h = ZM_hnfall_i(h, NULL, 0);
    2030        3780 :     cyc = ZM_snf_group(h, pU, &Ui);
    2031        3780 :     c = lg(Ui); gen = cgetg(c, t_VEC); EX = gel(cyc,1);
    2032       26929 :     for (j = 1; j < c; j++)
    2033       23149 :       gel(gen,j) = famat_to_nf_modideal_coprime(nf, vg, gel(Ui,j), prk, EX);
    2034             :   }
    2035        8456 :   return mkvec4(cyc, gen, prk, L2);
    2036             : }
    2037             : GEN
    2038         112 : idealprincipalunits(GEN nf, GEN pr, long k)
    2039             : {
    2040             :   pari_sp av;
    2041             :   GEN v;
    2042         112 :   nf = checknf(nf);
    2043         112 :   if (k == 1) { checkprid(pr); retmkvec3(gen_1,cgetg(1,t_VEC),cgetg(1,t_VEC)); }
    2044         105 :   av = avma; v = idealprincipalunits_i(nf, pr, k, NULL);
    2045         105 :   return gerepilecopy(av, mkvec3(powiu(pr_norm(pr), k-1), gel(v,1), gel(v,2)));
    2046             : }
    2047             : 
    2048             : /* Given an ideal pr^k dividing an integral ideal x (in HNF form) compute
    2049             :  * an 'sprk', the structure of G = (Z_K/pr^k)^* [ x = NULL for x = pr^k ]
    2050             :  * Return a vector with at least 4 components [cyc],[gen],[HNF pr^k,pr,k],ff,
    2051             :  * where
    2052             :  * cyc : type of G as abelian group (SNF)
    2053             :  * gen : generators of G, coprime to x
    2054             :  * pr^k: in HNF
    2055             :  * ff  : data for log_g in (Z_K/pr)^*
    2056             :  * Two extra components are present iff k > 1: L2, U
    2057             :  * L2  : list of data structures to compute local DL in (Z_K/pr)^*,
    2058             :  *       and 1 + pr^a/ 1 + pr^b for various a < b <= min(2a, k)
    2059             :  * U   : base change matrices to convert a vector of local DL to DL wrt gen */
    2060             : static GEN
    2061       11879 : sprkinit(GEN nf, GEN pr, GEN gk, GEN x)
    2062             : {
    2063             :   GEN T, p, modpr, cyc, gen, g, g0, ord0, A, prk, U, L2;
    2064       11879 :   long k = itos(gk), f = pr_get_f(pr);
    2065             : 
    2066       11879 :   if(DEBUGLEVEL>3) err_printf("treating pr^%ld, pr = %Ps\n",k,pr);
    2067       11879 :   modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    2068             :   /* (Z_K / pr)^* */
    2069       11879 :   if (f == 1)
    2070             :   {
    2071        5068 :     g0 = g = pgener_Fp(p);
    2072        5068 :     ord0 = get_arith_ZZM(subiu(p,1));
    2073             :   }
    2074             :   else
    2075             :   {
    2076        6811 :     g0 = g = gener_FpXQ(T,p, &ord0);
    2077        6811 :     g = Fq_to_nf(g, modpr);
    2078        6811 :     if (typ(g) == t_POL) g = poltobasis(nf, g);
    2079             :   }
    2080       11879 :   A = gel(ord0, 1); /* Norm(pr)-1 */
    2081       11879 :   if (k == 1)
    2082             :   {
    2083        3528 :     cyc = mkvec(A);
    2084        3528 :     gen = mkvec(g);
    2085        3528 :     prk = idealhnf_two(nf,pr);
    2086        3528 :     L2 = U = NULL;
    2087             :   }
    2088             :   else
    2089             :   { /* local-gens of (1 + pr)/(1 + pr^k) = SNF-gens * U */
    2090             :     GEN AB, B, u, v, w;
    2091             :     long j, l;
    2092        8351 :     w = idealprincipalunits_i(nf, pr, k, &U);
    2093             :     /* incorporate (Z_K/pr)^*, order A coprime to B = expo(1+pr/1+pr^k)*/
    2094        8351 :     cyc = leafcopy(gel(w,1)); B = gel(cyc,1); AB = mulii(A,B);
    2095        8351 :     gen = leafcopy(gel(w,2));
    2096        8351 :     prk = gel(w,3);
    2097        8351 :     g = nfpowmodideal(nf, g, B, prk);
    2098        8351 :     g0 = Fq_pow(g0, modii(B,A), T, p); /* update primitive root */
    2099        8351 :     L2 = mkvec3(A, g, gel(w,4));
    2100        8351 :     gel(cyc,1) = AB;
    2101        8351 :     gel(gen,1) = nfmulmodideal(nf, gel(gen,1), g, prk);
    2102        8351 :     u = mulii(Fp_inv(A,B), A);
    2103        8351 :     v = subui(1, u); l = lg(U);
    2104        8351 :     for (j = 1; j < l; j++) gcoeff(U,1,j) = Fp_mul(u, gcoeff(U,1,j), AB);
    2105        8351 :     U = mkvec2(Rg_col_ei(v, lg(gen)-1, 1), U);
    2106             :   }
    2107             :   /* local-gens of (Z_K/pr^k)^* = SNF-gens * U */
    2108       11879 :   if (x)
    2109             :   {
    2110        1939 :     GEN uv = zkchineseinit(nf, idealdivpowprime(nf,x,pr,gk), prk, x);
    2111        1939 :     gen = zkVchinese1(uv, gen);
    2112             :   }
    2113       11879 :   return mkvecn(U? 6: 4, cyc, gen, prk, mkvec3(modpr,g0,ord0), L2, U);
    2114             : }
    2115             : static GEN
    2116      240662 : sprk_get_cyc(GEN s) { return gel(s,1); }
    2117             : static GEN
    2118       91137 : sprk_get_expo(GEN s)
    2119             : {
    2120       91137 :   GEN cyc = sprk_get_cyc(s);
    2121       91137 :   return lg(cyc) == 1? gen_1: gel(cyc, 1);
    2122             : }
    2123             : static GEN
    2124        5817 : sprk_get_gen(GEN s) { return gel(s,2); }
    2125             : static GEN
    2126      229805 : sprk_get_prk(GEN s) { return gel(s,3); }
    2127             : static GEN
    2128      242466 : sprk_get_ff(GEN s) { return gel(s,4); }
    2129             : static GEN
    2130       96345 : sprk_get_pr(GEN s) { GEN ff = gel(s,4); return modpr_get_pr(gel(ff,1)); }
    2131             : /* A = Npr-1, <g> = (Z_K/pr)^*, L2 to 1 + pr / 1 + pr^k */
    2132             : static void
    2133      141692 : sprk_get_L2(GEN s, GEN *A, GEN *g, GEN *L2)
    2134      141692 : { GEN v = gel(s,5); *A = gel(v,1); *g = gel(v,2); *L2 = gel(v,3); }
    2135             : static void
    2136      138668 : sprk_get_U2(GEN s, GEN *U1, GEN *U2)
    2137      138668 : { GEN v = gel(s,6); *U1 = gel(v,1); *U2 = gel(v,2); }
    2138             : static int
    2139      242466 : sprk_is_prime(GEN s) { return lg(s) == 5; }
    2140             : 
    2141             : static GEN
    2142       91137 : famat_zlog_pr(GEN nf, GEN g, GEN e, GEN sprk)
    2143             : {
    2144       91137 :   GEN pr = sprk_get_pr(sprk);
    2145       91137 :   GEN prk = sprk_get_prk(sprk);
    2146       91137 :   GEN x = famat_makecoprime(nf, g, e, pr, prk, sprk_get_expo(sprk));
    2147       91137 :   return zlog_pr(nf, x, sprk);
    2148             : }
    2149             : /* log_g(a) in (Z_K/pr)^* */
    2150             : static GEN
    2151      242466 : nf_log(GEN nf, GEN a, GEN ff)
    2152             : {
    2153      242466 :   GEN pr = gel(ff,1), g = gel(ff,2), ord = gel(ff,3);
    2154      242466 :   GEN T,p, modpr = nf_to_Fq_init(nf, &pr, &T, &p);
    2155      242466 :   return Fq_log(nf_to_Fq(nf,a,modpr), g, ord, T, p);
    2156             : }
    2157             : /* a in Z_K (t_COL or t_INT), pr prime ideal, sprk = sprkinit(nf,pr,k,x).
    2158             :  * return log(a) on SNF-generators of (Z_K/pr^k)^**/
    2159             : GEN
    2160      243502 : zlog_pr(GEN nf, GEN a, GEN sprk)
    2161             : {
    2162             :   GEN e, prk, A, g, L2, U1, U2, y;
    2163             : 
    2164      243502 :   if (typ(a) == t_MAT) return famat_zlog_pr(nf, gel(a,1), gel(a,2), sprk);
    2165             : 
    2166      242466 :   e = nf_log(nf, a, sprk_get_ff(sprk));
    2167      242466 :   if (sprk_is_prime(sprk)) return mkcol(e); /* k = 1 */
    2168      138668 :   prk = sprk_get_prk(sprk);
    2169      138668 :   sprk_get_L2(sprk, &A,&g,&L2);
    2170      138668 :   if (signe(e))
    2171             :   {
    2172       40131 :     e = Fp_neg(e, A);
    2173       40131 :     a = nfmulpowmodideal(nf, a, g, e, prk);
    2174             :   }
    2175      138668 :   sprk_get_U2(sprk, &U1,&U2);
    2176      138668 :   y = ZM_ZC_mul(U2, log_prk1(nf, a, lg(U2)-1, L2, prk));
    2177      138661 :   if (signe(e)) y = ZC_sub(y, ZC_Z_mul(U1,e));
    2178      138661 :   return vecmodii(y, sprk_get_cyc(sprk));
    2179             : }
    2180             : GEN
    2181        6062 : zlog_pr_init(GEN nf, GEN pr, long k) { return sprkinit(nf,pr,utoipos(k),NULL);}
    2182             : GEN
    2183         378 : vzlog_pr(GEN nf, GEN v, GEN sprk)
    2184             : {
    2185         378 :   long l = lg(v), i;
    2186         378 :   GEN w = cgetg(l, t_MAT);
    2187         378 :   for (i = 1; i < l; i++) gel(w,i) = zlog_pr(nf, gel(v,i), sprk);
    2188         378 :   return w;
    2189             : }
    2190             : 
    2191             : static GEN
    2192       94819 : famat_zlog(GEN nf, GEN fa, GEN sgn, zlog_S *S)
    2193             : {
    2194       94819 :   long i, n0, n = lg(S->U)-1;
    2195             :   GEN g, e, y;
    2196       94819 :   if (lg(fa) == 1) return zerocol(n);
    2197       94819 :   g = gel(fa,1);
    2198       94819 :   e = gel(fa,2);
    2199       94819 :   y = cgetg(n+1, t_COL);
    2200       94819 :   n0 = lg(S->sprk)-1; /* n0 = n (trivial arch. part), or n-1 */
    2201       94819 :   for (i=1; i <= n0; i++) gel(y,i) = famat_zlog_pr(nf, g, e, gel(S->sprk,i));
    2202       94819 :   if (n0 != n)
    2203             :   {
    2204       91480 :     if (!sgn) sgn = nfsign_arch(nf, fa, S->archp);
    2205       91480 :     gel(y,n) = Flc_to_ZC(sgn);
    2206             :   }
    2207       94819 :   return y;
    2208             : }
    2209             : 
    2210             : /* assume that cyclic factors are normalized, in particular != [1] */
    2211             : static GEN
    2212        5572 : split_U(GEN U, GEN Sprk)
    2213             : {
    2214        5572 :   long t = 0, k, n, l = lg(Sprk);
    2215        5572 :   GEN vU = cgetg(l+1, t_VEC);
    2216       10619 :   for (k = 1; k < l; k++)
    2217             :   {
    2218        5047 :     n = lg(sprk_get_cyc(gel(Sprk,k))) - 1; /* > 0 */
    2219        5047 :     gel(vU,k) = vecslice(U, t+1, t+n);
    2220        5047 :     t += n;
    2221             :   }
    2222             :   /* t+1 .. lg(U)-1 */
    2223        5572 :   n = lg(U) - t - 1; /* can be 0 */
    2224        5572 :   if (!n) setlg(vU,l); else gel(vU,l) = vecslice(U, t+1, t+n);
    2225        5572 :   return vU;
    2226             : }
    2227             : 
    2228             : void
    2229      205281 : init_zlog(zlog_S *S, GEN bid)
    2230             : {
    2231      205281 :   GEN fa2 = bid_get_fact2(bid);
    2232      205281 :   S->U = bid_get_U(bid);
    2233      205281 :   S->hU = lg(bid_get_cyc(bid))-1;
    2234      205281 :   S->archp = bid_get_archp(bid);
    2235      205281 :   S->sprk = bid_get_sprk(bid);
    2236      205281 :   S->bid = bid;
    2237      205281 :   S->P = gel(fa2,1);
    2238      205281 :   S->k = gel(fa2,2);
    2239      205281 :   S->no2 = lg(S->P) == lg(gel(bid_get_fact(bid),1));
    2240      205281 : }
    2241             : 
    2242             : /* a a t_FRAC/t_INT, reduce mod bid */
    2243             : static GEN
    2244           7 : Q_mod_bid(GEN bid, GEN a)
    2245             : {
    2246           7 :   GEN xZ = gcoeff(bid_get_ideal(bid),1,1);
    2247           7 :   GEN b = Rg_to_Fp(a, xZ);
    2248           7 :   if (gsigne(a) < 0) b = subii(b, xZ);
    2249           7 :   return b;
    2250             : }
    2251             : /* Return decomposition of a on the CRT generators blocks attached to the
    2252             :  * S->sprk and sarch; sgn = sign(a, S->arch), NULL if unknown */
    2253             : static GEN
    2254      147608 : zlog(GEN nf, GEN a, GEN sgn, zlog_S *S)
    2255             : {
    2256             :   long k, l;
    2257             :   GEN y;
    2258      147608 :   a = nf_to_scalar_or_basis(nf, a);
    2259      147608 :   switch(typ(a))
    2260             :   {
    2261       13097 :     case t_INT: break;
    2262           7 :     case t_FRAC: a = Q_mod_bid(S->bid, a); break;
    2263             :     default: /* case t_COL: */
    2264             :     {
    2265             :       GEN den;
    2266      134504 :       check_nfelt(a, &den);
    2267      134504 :       if (den)
    2268             :       {
    2269       41465 :         a = Q_muli_to_int(a, den);
    2270       41465 :         a = mkmat2(mkcol2(a, den), mkcol2(gen_1, gen_m1));
    2271       41465 :         return famat_zlog(nf, a, sgn, S);
    2272             :       }
    2273             :     }
    2274             :   }
    2275      106143 :   if (sgn)
    2276        8967 :     sgn = (lg(sgn) == 1)? NULL: leafcopy(sgn);
    2277             :   else
    2278       97176 :     sgn = (lg(S->archp) == 1)? NULL: nfsign_arch(nf, a, S->archp);
    2279      106143 :   l = lg(S->sprk);
    2280      106143 :   y = cgetg(sgn? l+1: l, t_COL);
    2281      237872 :   for (k = 1; k < l; k++)
    2282             :   {
    2283      131736 :     GEN sprk = gel(S->sprk,k);
    2284      131736 :     gel(y,k) = zlog_pr(nf, a, sprk);
    2285             :   }
    2286      106136 :   if (sgn) gel(y,l) = Flc_to_ZC(sgn);
    2287      106136 :   return y;
    2288             : }
    2289             : 
    2290             : /* true nf */
    2291             : GEN
    2292        2436 : pr_basis_perm(GEN nf, GEN pr)
    2293             : {
    2294        2436 :   long f = pr_get_f(pr);
    2295             :   GEN perm;
    2296        2436 :   if (f == nf_get_degree(nf)) return identity_perm(f);
    2297        1316 :   perm = cgetg(f+1, t_VECSMALL);
    2298        1316 :   perm[1] = 1;
    2299        1316 :   if (f > 1)
    2300             :   {
    2301         399 :     GEN H = idealhnf_two(nf,pr);
    2302             :     long i, k;
    2303        1463 :     for (i = k = 2; k <= f; i++)
    2304             :     {
    2305        1064 :       if (is_pm1(gcoeff(H,i,i))) continue;
    2306         840 :       perm[k++] = i;
    2307             :     }
    2308             :   }
    2309        1316 :   return perm;
    2310             : }
    2311             : 
    2312             : /* \sum U[i]*y[i], U[i] ZM, y[i] ZC. We allow lg(y) > lg(U). */
    2313             : static GEN
    2314      200955 : ZMV_ZCV_mul(GEN U, GEN y)
    2315             : {
    2316      200955 :   long i, l = lg(U);
    2317      200955 :   GEN z = NULL;
    2318      200955 :   if (l == 1) return cgetg(1,t_COL);
    2319      584981 :   for (i = 1; i < l; i++)
    2320             :   {
    2321      384026 :     GEN u = ZM_ZC_mul(gel(U,i), gel(y,i));
    2322      384026 :     z = z? ZC_add(z, u): u;
    2323             :   }
    2324      200955 :   return z;
    2325             : }
    2326             : /* A * (U[1], ..., U[d] */
    2327             : static GEN
    2328         518 : ZM_ZMV_mul(GEN A, GEN U)
    2329             : {
    2330             :   long i, l;
    2331         518 :   GEN V = cgetg_copy(U,&l);
    2332         518 :   for (i = 1; i < l; i++) gel(V,i) = ZM_mul(A,gel(U,i));
    2333         518 :   return V;
    2334             : }
    2335             : 
    2336             : /* Log on bid.gen of generators of P_{1,I pr^{e-1}} / P_{1,I pr^e} (I,pr) = 1,
    2337             :  * defined implicitly via CRT. 'ind' is the index of pr in modulus
    2338             :  * factorization */
    2339             : GEN
    2340       10234 : log_gen_pr(zlog_S *S, long ind, GEN nf, long e)
    2341             : {
    2342       10234 :   GEN A, sprk = gel(S->sprk,ind), Uind = gel(S->U, ind);
    2343             : 
    2344       10234 :   if (e == 1) retmkmat( gel(Uind,1) );
    2345             :   else
    2346             :   {
    2347        5208 :     GEN G, pr = sprk_get_pr(sprk);
    2348             :     long i, l;
    2349        5208 :     if (e == 2)
    2350             :     {
    2351        3024 :       GEN A, g, L, L2; sprk_get_L2(sprk,&A,&g,&L2); L = gel(L2,1);
    2352        3024 :       G = gel(L,2); l = lg(G);
    2353             :     }
    2354             :     else
    2355             :     {
    2356        2184 :       GEN perm = pr_basis_perm(nf,pr), PI = nfpow_u(nf, pr_get_gen(pr), e-1);
    2357        2184 :       l = lg(perm);
    2358        2184 :       G = cgetg(l, t_VEC);
    2359        2184 :       if (typ(PI) == t_INT)
    2360             :       { /* zk_ei_mul doesn't allow t_INT */
    2361        1113 :         long N = nf_get_degree(nf);
    2362        1113 :         gel(G,1) = addiu(PI,1);
    2363        1743 :         for (i = 2; i < l; i++)
    2364             :         {
    2365         630 :           GEN z = col_ei(N, 1);
    2366         630 :           gel(G,i) = z; gel(z, perm[i]) = PI;
    2367             :         }
    2368             :       }
    2369             :       else
    2370             :       {
    2371        1071 :         gel(G,1) = nfadd(nf, gen_1, PI);
    2372        1281 :         for (i = 2; i < l; i++)
    2373         210 :           gel(G,i) = nfadd(nf, gen_1, zk_ei_mul(nf, PI, perm[i]));
    2374             :       }
    2375             :     }
    2376        5208 :     A = cgetg(l, t_MAT);
    2377       11970 :     for (i = 1; i < l; i++)
    2378        6762 :       gel(A,i) = ZM_ZC_mul(Uind, zlog_pr(nf, gel(G,i), sprk));
    2379        5208 :     return A;
    2380             :   }
    2381             : }
    2382             : /* Log on bid.gen of generator of P_{1,f} / P_{1,f v[index]}
    2383             :  * v = vector of r1 real places */
    2384             : GEN
    2385        6188 : log_gen_arch(zlog_S *S, long index)
    2386             : {
    2387        6188 :   GEN U = gel(S->U, lg(S->U)-1);
    2388        6188 :   return gel(U, index);
    2389             : }
    2390             : 
    2391             : /* compute bid.clgp: [h,cyc] or [h,cyc,gen] */
    2392             : static GEN
    2393        6629 : bid_grp(GEN nf, GEN U, GEN cyc, GEN g, GEN F, GEN sarch)
    2394             : {
    2395        6629 :   GEN G, h = ZV_prod(cyc);
    2396             :   long c;
    2397        6629 :   if (!U) return mkvec2(h,cyc);
    2398        6377 :   c = lg(U);
    2399        6377 :   G = cgetg(c,t_VEC);
    2400        6377 :   if (c > 1)
    2401             :   {
    2402        5460 :     GEN U0, Uoo, EX = gel(cyc,1); /* exponent of bid */
    2403        5460 :     long i, hU = nbrows(U), nba = lg(sarch_get_cyc(sarch))-1; /* #f_oo */
    2404        5460 :     if (!nba) { U0 = U; Uoo = NULL; }
    2405        3045 :     else if (nba == hU) { U0 = NULL; Uoo = U; }
    2406             :     else
    2407             :     {
    2408        2366 :       U0 = rowslice(U, 1, hU-nba);
    2409        2366 :       Uoo = rowslice(U, hU-nba+1, hU);
    2410             :     }
    2411       17388 :     for (i = 1; i < c; i++)
    2412             :     {
    2413       11928 :       GEN t = gen_1;
    2414       11928 :       if (U0) t = famat_to_nf_modideal_coprime(nf, g, gel(U0,i), F, EX);
    2415       11928 :       if (Uoo) t = set_sign_mod_divisor(nf, ZV_to_Flv(gel(Uoo,i),2), t, sarch);
    2416       11928 :       gel(G,i) = t;
    2417             :     }
    2418             :   }
    2419        6377 :   return mkvec3(h, cyc, G);
    2420             : }
    2421             : 
    2422             : /* remove prime ideals of norm 2 with exponent 1 from factorization */
    2423             : static GEN
    2424        6314 : famat_strip2(GEN fa)
    2425             : {
    2426        6314 :   GEN P = gel(fa,1), E = gel(fa,2), Q, F;
    2427        6314 :   long l = lg(P), i, j;
    2428        6314 :   Q = cgetg(l, t_COL);
    2429        6314 :   F = cgetg(l, t_COL);
    2430       12173 :   for (i = j = 1; i < l; i++)
    2431             :   {
    2432        5859 :     GEN pr = gel(P,i), e = gel(E,i);
    2433        5859 :     if (!absequaliu(pr_get_p(pr), 2) || itou(e) != 1 || pr_get_f(pr) != 1)
    2434             :     {
    2435        5817 :       gel(Q,j) = pr;
    2436        5817 :       gel(F,j) = e; j++;
    2437             :     }
    2438             :   }
    2439        6314 :   setlg(Q,j);
    2440        6314 :   setlg(F,j); return mkmat2(Q,F);
    2441             : }
    2442             : 
    2443             : /* Compute [[ideal,arch], [h,[cyc],[gen]], idealfact, [liste], U]
    2444             :    flag may include nf_GEN | nf_INIT */
    2445             : static GEN
    2446        6335 : Idealstar_i(GEN nf, GEN ideal, long flag)
    2447             : {
    2448             :   long i, k, nbp, R1;
    2449        6335 :   GEN y, cyc, U, u1 = NULL, fa, fa2, sprk, x, arch, archp, E, P, sarch, gen;
    2450             : 
    2451        6335 :   nf = checknf(nf);
    2452        6335 :   R1 = nf_get_r1(nf);
    2453        6335 :   if (typ(ideal) == t_VEC && lg(ideal) == 3)
    2454             :   {
    2455        4207 :     arch = gel(ideal,2);
    2456        4207 :     ideal= gel(ideal,1);
    2457        4207 :     switch(typ(arch))
    2458             :     {
    2459             :       case t_VEC:
    2460        4172 :         if (lg(arch) != R1+1)
    2461           0 :           pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
    2462        4172 :         archp = vec01_to_indices(arch);
    2463        4172 :         break;
    2464             :       case t_VECSMALL:
    2465          35 :         archp = arch;
    2466          35 :         k = lg(archp)-1;
    2467          35 :         if (k && archp[k] > R1)
    2468           7 :           pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
    2469          28 :         arch = indices_to_vec01(archp, R1);
    2470          28 :         break;
    2471             :       default:
    2472           0 :         pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
    2473           0 :         return NULL;
    2474             :     }
    2475        4200 :   }
    2476             :   else
    2477             :   {
    2478        2128 :     arch = zerovec(R1);
    2479        2128 :     archp = cgetg(1, t_VECSMALL);
    2480             :   }
    2481        6328 :   if (is_nf_factor(ideal))
    2482             :   {
    2483         336 :     fa = ideal;
    2484         336 :     x = idealfactorback(nf, gel(fa,1), gel(fa,2), 0);
    2485             :   }
    2486             :   else
    2487             :   {
    2488        5992 :     fa = idealfactor(nf, ideal);
    2489        5985 :     x = idealhnf_shallow(nf, ideal);
    2490             :   }
    2491        6321 :   if (lg(x) == 1) pari_err_DOMAIN("Idealstar", "ideal","=",gen_0,x);
    2492        6321 :   if (typ(gcoeff(x,1,1)) != t_INT)
    2493           7 :     pari_err_DOMAIN("Idealstar","denominator(ideal)", "!=",gen_1,x);
    2494        6314 :   sarch = nfarchstar(nf, x, archp);
    2495        6314 :   fa2 = famat_strip2(fa);
    2496        6314 :   P = gel(fa2,1);
    2497        6314 :   E = gel(fa2,2);
    2498        6314 :   nbp = lg(P)-1;
    2499        6314 :   sprk = cgetg(nbp+1,t_VEC);
    2500        6314 :   if (nbp)
    2501             :   {
    2502        4704 :     GEN t = (nbp==1)? NULL: x;
    2503        4704 :     cyc = cgetg(nbp+2,t_VEC);
    2504        4704 :     gen = cgetg(nbp+1,t_VEC);
    2505       10521 :     for (i = 1; i <= nbp; i++)
    2506             :     {
    2507        5817 :       GEN L = sprkinit(nf, gel(P,i), gel(E,i), t);
    2508        5817 :       gel(sprk,i) = L;
    2509        5817 :       gel(cyc,i) = sprk_get_cyc(L);
    2510             :       /* true gens are congruent to those mod x AND positive at archp */
    2511        5817 :       gel(gen,i) = sprk_get_gen(L);
    2512             :     }
    2513        4704 :     gel(cyc,i) = sarch_get_cyc(sarch);
    2514        4704 :     cyc = shallowconcat1(cyc);
    2515        4704 :     gen = shallowconcat1(gen);
    2516        4704 :     cyc = ZV_snf_group(cyc, &U, (flag & nf_GEN)? &u1: NULL);
    2517             :   }
    2518             :   else
    2519             :   {
    2520        1610 :     cyc = sarch_get_cyc(sarch);
    2521        1610 :     gen = cgetg(1,t_VEC);
    2522        1610 :     U = matid(lg(cyc)-1);
    2523        1610 :     if (flag & nf_GEN) u1 = U;
    2524             :   }
    2525        6314 :   y = bid_grp(nf, u1, cyc, gen, x, sarch);
    2526        6314 :   if (!(flag & nf_INIT)) return y;
    2527        5516 :   U = split_U(U, sprk);
    2528        5516 :   return mkvec5(mkvec2(x, arch), y, mkvec2(fa,fa2), mkvec2(sprk, sarch), U);
    2529             : }
    2530             : GEN
    2531        6356 : Idealstar(GEN nf, GEN ideal, long flag)
    2532             : {
    2533             :   pari_sp av;
    2534        6356 :   if (!nf) return ZNstar(ideal, flag);
    2535        6069 :   av = avma;
    2536        6069 :   return gerepilecopy(av, Idealstar_i(nf, ideal, flag));
    2537             : }
    2538             : GEN
    2539         266 : Idealstarprk(GEN nf, GEN pr, long k, long flag)
    2540             : {
    2541         266 :   pari_sp av = avma;
    2542         266 :   GEN z = Idealstar_i(nf, mkmat2(mkcol(pr),mkcols(k)), flag);
    2543         266 :   return gerepilecopy(av, z);
    2544             : }
    2545             : 
    2546             : /* FIXME: obsolete */
    2547             : GEN
    2548           0 : zidealstarinitgen(GEN nf, GEN ideal)
    2549           0 : { return Idealstar(nf,ideal, nf_INIT|nf_GEN); }
    2550             : GEN
    2551           0 : zidealstarinit(GEN nf, GEN ideal)
    2552           0 : { return Idealstar(nf,ideal, nf_INIT); }
    2553             : GEN
    2554           0 : zidealstar(GEN nf, GEN ideal)
    2555           0 : { return Idealstar(nf,ideal, nf_GEN); }
    2556             : 
    2557             : GEN
    2558         357 : idealstar0(GEN nf, GEN ideal,long flag)
    2559             : {
    2560         357 :   switch(flag)
    2561             :   {
    2562           0 :     case 0: return Idealstar(nf,ideal, nf_GEN);
    2563         322 :     case 1: return Idealstar(nf,ideal, nf_INIT);
    2564          35 :     case 2: return Idealstar(nf,ideal, nf_INIT|nf_GEN);
    2565           0 :     default: pari_err_FLAG("idealstar");
    2566             :   }
    2567           0 :   return NULL; /* not reached */
    2568             : }
    2569             : 
    2570             : void
    2571      134504 : check_nfelt(GEN x, GEN *den)
    2572             : {
    2573      134504 :   long l = lg(x), i;
    2574      134504 :   GEN t, d = NULL;
    2575      134504 :   if (typ(x) != t_COL) pari_err_TYPE("check_nfelt", x);
    2576      509387 :   for (i=1; i<l; i++)
    2577             :   {
    2578      374883 :     t = gel(x,i);
    2579      374883 :     switch (typ(t))
    2580             :     {
    2581      286824 :       case t_INT: break;
    2582             :       case t_FRAC:
    2583       88059 :         if (!d) d = gel(t,2); else d = lcmii(d, gel(t,2));
    2584       88059 :         break;
    2585           0 :       default: pari_err_TYPE("check_nfelt", x);
    2586             :     }
    2587             :   }
    2588      134504 :   *den = d;
    2589      134504 : }
    2590             : 
    2591             : GEN
    2592      607806 : vecmodii(GEN a, GEN b)
    2593             : {
    2594             :   long i, l;
    2595      607806 :   GEN c = cgetg_copy(a, &l);
    2596      607806 :   for (i = 1; i < l; i++) gel(c,i) = modii(gel(a,i), gel(b,i));
    2597      607806 :   return c;
    2598             : }
    2599             : 
    2600             : static GEN
    2601      202488 : ideallog_i(GEN nf, GEN x, GEN sgn, zlog_S *S)
    2602             : {
    2603      202488 :   pari_sp av = avma;
    2604             :   GEN y, cyc;
    2605      202488 :   if (!S->hU) return cgetg(1, t_COL);
    2606      200969 :   cyc = bid_get_cyc(S->bid);
    2607      200969 :   if (typ(x) == t_MAT)
    2608             :   {
    2609       53361 :     if (lg(x) == 1) return zerocol(lg(cyc)-1);
    2610       53354 :     y = famat_zlog(nf, x, sgn, S);
    2611             :   }
    2612             :   else
    2613      147608 :     y = zlog(nf, x, sgn, S);
    2614      200955 :   y = ZMV_ZCV_mul(S->U, y);
    2615      200955 :   return gerepileupto(av, vecmodii(y, cyc));
    2616             : }
    2617             : 
    2618             : /* Given x (not necessarily integral), and bid as output by zidealstarinit,
    2619             :  * compute the vector of components on the generators bid[2].
    2620             :  * Assume (x,bid) = 1 and sgn is either NULL or nfsign_arch(x, bid) */
    2621             : GEN
    2622      197931 : ideallog_sgn(GEN nf, GEN x, GEN sgn, GEN bid)
    2623             : {
    2624             :   zlog_S S;
    2625      197931 :   nf = checknf(nf); checkbid(bid);
    2626      197924 :   init_zlog(&S, bid);
    2627      197924 :   if (sgn && typ(x) == t_VEC) /* vector of elements and signatures */
    2628             :   {
    2629        4403 :     long i, l = lg(x);
    2630        4403 :     GEN y = cgetg(l, t_MAT);
    2631        4403 :     for (i = 1; i < l; i++) gel(y,i) = ideallog_i(nf, gel(x,i), gel(sgn,i), &S);
    2632        4403 :     return y;
    2633             :   }
    2634      193521 :   return ideallog_i(nf, x, sgn, &S);
    2635             : }
    2636             : GEN
    2637      200234 : ideallog(GEN nf, GEN x, GEN bid)
    2638             : {
    2639      200234 :   if (!nf) return Zideallog(bid, x);
    2640      193528 :   return ideallog_sgn(nf, x, NULL, bid);
    2641             : }
    2642             : 
    2643             : /*************************************************************************/
    2644             : /**                                                                     **/
    2645             : /**               JOIN BID STRUCTURES, IDEAL LISTS                      **/
    2646             : /**                                                                     **/
    2647             : /*************************************************************************/
    2648             : /* bid1, bid2: for coprime modules m1 and m2 (without arch. part).
    2649             :  * Output: bid for m1 m2 */
    2650             : static GEN
    2651         476 : join_bid(GEN nf, GEN bid1, GEN bid2)
    2652             : {
    2653         476 :   pari_sp av = avma;
    2654             :   long nbgen, l1,l2;
    2655             :   GEN I1,I2, G1,G2, sprk1,sprk2, cyc1,cyc2, sarch;
    2656         476 :   GEN sprk, fa,fa2, U, cyc, y, u1 = NULL, x, gen;
    2657             : 
    2658         476 :   I1 = bid_get_ideal(bid1);
    2659         476 :   I2 = bid_get_ideal(bid2);
    2660         476 :   if (gequal1(gcoeff(I1,1,1))) return bid2; /* frequent trivial case */
    2661         259 :   G1 = bid_get_grp(bid1);
    2662         259 :   G2 = bid_get_grp(bid2);
    2663         259 :   x = idealmul(nf, I1,I2);
    2664         259 :   fa = famat_mul_shallow(bid_get_fact(bid1), bid_get_fact(bid2));
    2665         259 :   fa2= famat_mul_shallow(bid_get_fact2(bid1), bid_get_fact2(bid2));
    2666         259 :   sprk1 = bid_get_sprk(bid1);
    2667         259 :   sprk2 = bid_get_sprk(bid2);
    2668         259 :   sprk = shallowconcat(sprk1, sprk2);
    2669             : 
    2670         259 :   cyc1 = abgrp_get_cyc(G1); l1 = lg(cyc1);
    2671         259 :   cyc2 = abgrp_get_cyc(G2); l2 = lg(cyc2);
    2672         259 :   gen = (lg(G1)>3 && lg(G2)>3)? gen_1: NULL;
    2673         259 :   nbgen = l1+l2-2;
    2674         259 :   cyc = ZV_snf_group(shallowconcat(cyc1,cyc2), &U, gen? &u1: NULL);
    2675         259 :   if (nbgen)
    2676             :   {
    2677         259 :     GEN U1 = bid_get_U(bid1), U2 = bid_get_U(bid2);
    2678         259 :     U1 = l1==1? const_vec(lg(sprk1), cgetg(1,t_MAT))
    2679         259 :               : ZM_ZMV_mul(vecslice(U, 1, l1-1),   U1);
    2680         259 :     U2 = l2==1? const_vec(lg(sprk2), cgetg(1,t_MAT))
    2681         259 :               : ZM_ZMV_mul(vecslice(U, l1, nbgen), U2);
    2682         259 :     U = shallowconcat(U1, U2);
    2683             :   }
    2684             :   else
    2685           0 :     U = const_vec(lg(sprk), cgetg(1,t_MAT));
    2686             : 
    2687         259 :   if (gen)
    2688             :   {
    2689         259 :     GEN uv = zkchinese1init2(nf, I2, I1, x);
    2690         518 :     gen = shallowconcat(zkVchinese1(gel(uv,1), abgrp_get_gen(G1)),
    2691         259 :                         zkVchinese1(gel(uv,2), abgrp_get_gen(G2)));
    2692             :   }
    2693         259 :   sarch = bid_get_sarch(bid1); /* trivial */
    2694         259 :   y = bid_grp(nf, u1, cyc, gen, x, sarch);
    2695         259 :   x = mkvec2(x, bid_get_arch(bid1));
    2696         259 :   y = mkvec5(x, y, mkvec2(fa, fa2), mkvec2(sprk, sarch), U);
    2697         259 :   return gerepilecopy(av,y);
    2698             : }
    2699             : 
    2700             : typedef struct _ideal_data {
    2701             :   GEN nf, emb, L, pr, prL, sgnU, archp;
    2702             : } ideal_data;
    2703             : 
    2704             : /* z <- ( z | f(v[i])_{i=1..#v} ) */
    2705             : static void
    2706       43414 : concat_join(GEN *pz, GEN v, GEN (*f)(ideal_data*,GEN), ideal_data *data)
    2707             : {
    2708       43414 :   long i, nz, lv = lg(v);
    2709             :   GEN z, Z;
    2710       86828 :   if (lv == 1) return;
    2711       18942 :   z = *pz; nz = lg(z)-1;
    2712       18942 :   *pz = Z = cgetg(lv + nz, typ(z));
    2713       18942 :   for (i = 1; i <=nz; i++) gel(Z,i) = gel(z,i);
    2714       18942 :   Z += nz;
    2715       18942 :   for (i = 1; i < lv; i++) gel(Z,i) = f(data, gel(v,i));
    2716             : }
    2717             : static GEN
    2718         476 : join_idealinit(ideal_data *D, GEN x)
    2719         476 : { return join_bid(D->nf, x, D->prL); }
    2720             : static GEN
    2721       26222 : join_ideal(ideal_data *D, GEN x)
    2722       26222 : { return idealmulpowprime(D->nf, x, D->pr, D->L); }
    2723             : static GEN
    2724         455 : join_unit(ideal_data *D, GEN x)
    2725             : {
    2726         455 :   GEN bid = join_idealinit(D, gel(x,1)), u = gel(x,2), v = mkvec(D->emb);
    2727         455 :   if (lg(u) != 1) v = shallowconcat(u, v);
    2728         455 :   return mkvec2(bid, v);
    2729             : }
    2730             : 
    2731             : /*  flag & nf_GEN : generators, otherwise no
    2732             :  *  flag &2 : units, otherwise no
    2733             :  *  flag &4 : ideals in HNF, otherwise bid
    2734             :  *  flag &8 : omit ideals which cannot be conductors (pr^1 with Npr=2) */
    2735             : static GEN
    2736         350 : Ideallist(GEN bnf, ulong bound, long flag)
    2737             : {
    2738         350 :   const long cond = flag & 8;
    2739         350 :   const long do_units = flag & 2, big_id = !(flag & 4);
    2740         350 :   const long istar_flag = (flag & nf_GEN) | nf_INIT;
    2741         350 :   pari_sp av, av0 = avma;
    2742             :   long i, j;
    2743         350 :   GEN nf, z, p, fa, id, BOUND, U, empty = cgetg(1,t_VEC);
    2744             :   forprime_t S;
    2745             :   ideal_data ID;
    2746         350 :   GEN (*join_z)(ideal_data*, GEN) =
    2747             :     do_units? &join_unit
    2748         350 :               : (big_id? &join_idealinit: &join_ideal);
    2749             : 
    2750         350 :   nf = checknf(bnf);
    2751         350 :   if ((long)bound <= 0) return empty;
    2752         350 :   id = matid(nf_get_degree(nf));
    2753         350 :   if (big_id) id = Idealstar(nf,id, istar_flag);
    2754             : 
    2755             :   /* z[i] will contain all "objects" of norm i. Depending on flag, this means
    2756             :    * an ideal, a bid, or a couple [bid, log(units)]. Such objects are stored
    2757             :    * in vectors, computed one primary component at a time; join_z
    2758             :    * reconstructs the global object */
    2759         350 :   BOUND = utoipos(bound);
    2760         350 :   z = cgetg(bound+1,t_VEC);
    2761         350 :   if (do_units) {
    2762          14 :     U = bnf_build_units(bnf);
    2763          14 :     gel(z,1) = mkvec( mkvec2(id, cgetg(1,t_VEC)) );
    2764             :   } else {
    2765         336 :     U = NULL; /* -Wall */
    2766         336 :     gel(z,1) = mkvec(id);
    2767             :   }
    2768         350 :   for (i=2; i<=(long)bound; i++) gel(z,i) = empty;
    2769         350 :   ID.nf = nf;
    2770             : 
    2771         350 :   p = cgetipos(3);
    2772         350 :   u_forprime_init(&S, 2, bound);
    2773         350 :   av = avma;
    2774        5726 :   while ((p[2] = u_forprime_next(&S)))
    2775             :   {
    2776        5026 :     if (DEBUGLEVEL>1) { err_printf("%ld ",p[2]); err_flush(); }
    2777        5026 :     fa = idealprimedec_limit_norm(nf, p, BOUND);
    2778       10073 :     for (j=1; j<lg(fa); j++)
    2779             :     {
    2780        5047 :       GEN pr = gel(fa,j), z2 = leafcopy(z);
    2781        5047 :       ulong Q, q = upr_norm(pr);
    2782        5047 :       long l = (cond && q == 2)? 2: 1;
    2783             : 
    2784        5047 :       ID.pr = ID.prL = pr;
    2785       13370 :       for (Q = q; Q <= bound; l++, Q *= q) /* add pr^l */
    2786             :       {
    2787             :         ulong iQ;
    2788        8323 :         ID.L = utoipos(l);
    2789        8323 :         if (big_id) {
    2790         217 :           ID.prL = Idealstarprk(nf, pr, l, istar_flag);
    2791         217 :           if (do_units)
    2792             :           {
    2793         196 :             GEN sprk = bid_get_sprk(ID.prL);
    2794         392 :             ID.emb = lg(sprk) == 1? cgetg(1,t_VEC)
    2795         196 :                                   : vzlog_pr(nf, U, gel(sprk,1));
    2796             :           }
    2797             :         }
    2798       51737 :         for (iQ = Q,i = 1; iQ <= bound; iQ += Q,i++)
    2799       43414 :           concat_join(&gel(z,iQ), gel(z2,i), join_z, &ID);
    2800             :       }
    2801             :     }
    2802        5026 :     if (gc_needed(av,1))
    2803             :     {
    2804           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"Ideallist");
    2805           0 :       z = gerepilecopy(av, z);
    2806             :     }
    2807             :   }
    2808         350 :   return gerepilecopy(av0, z);
    2809             : }
    2810             : GEN
    2811         350 : ideallist0(GEN bnf,long bound, long flag) {
    2812         350 :   if (flag<0 || flag>15) pari_err_FLAG("ideallist");
    2813         350 :   return Ideallist(bnf,bound,flag);
    2814             : }
    2815             : GEN
    2816           0 : ideallist(GEN bnf,long bound) { return Ideallist(bnf,bound,4); }
    2817             : 
    2818             : /* bid = for module m (without arch. part), arch = archimedean part.
    2819             :  * Output: bid for [m,arch] */
    2820             : static GEN
    2821          56 : join_bid_arch(GEN nf, GEN bid, GEN archp)
    2822             : {
    2823          56 :   pari_sp av = avma;
    2824             :   GEN G, U;
    2825          56 :   GEN sprk, cyc, y, u1 = NULL, x, sarch, gen;
    2826             : 
    2827          56 :   checkbid(bid);
    2828          56 :   G = bid_get_grp(bid);
    2829          56 :   x = bid_get_ideal(bid);
    2830          56 :   sarch = nfarchstar(nf, bid_get_ideal(bid), archp);
    2831          56 :   sprk = bid_get_sprk(bid);
    2832             : 
    2833          56 :   gen = (lg(G)>3)? gel(G,3): NULL;
    2834          56 :   cyc = diagonal_shallow(shallowconcat(gel(G,2), sarch_get_cyc(sarch)));
    2835          56 :   cyc = ZM_snf_group(cyc, &U, gen? &u1: NULL);
    2836          56 :   y = bid_grp(nf, u1, cyc, gen, x, sarch);
    2837          56 :   U = split_U(U, sprk);
    2838          56 :   y = mkvec5(mkvec2(x, archp), y, gel(bid,3), mkvec2(sprk, sarch), U);
    2839          56 :   return gerepilecopy(av,y);
    2840             : }
    2841             : static GEN
    2842          56 : join_arch(ideal_data *D, GEN x) {
    2843          56 :   return join_bid_arch(D->nf, x, D->archp);
    2844             : }
    2845             : static GEN
    2846          28 : join_archunit(ideal_data *D, GEN x) {
    2847          28 :   GEN bid = join_arch(D, gel(x,1)), u = gel(x,2), v = mkvec(D->emb);
    2848          28 :   if (lg(u) != 1) v = shallowconcat(u, v);
    2849          28 :   return mkvec2(bid, v);
    2850             : }
    2851             : 
    2852             : /* L from ideallist, add archimedean part */
    2853             : GEN
    2854          14 : ideallistarch(GEN bnf, GEN L, GEN arch)
    2855             : {
    2856             :   pari_sp av;
    2857          14 :   long i, j, l = lg(L), lz;
    2858             :   GEN v, z, V;
    2859             :   ideal_data ID;
    2860             :   GEN (*join_z)(ideal_data*, GEN);
    2861             : 
    2862          14 :   if (typ(L) != t_VEC) pari_err_TYPE("ideallistarch",L);
    2863          14 :   if (l == 1) return cgetg(1,t_VEC);
    2864          14 :   z = gel(L,1);
    2865          14 :   if (typ(z) != t_VEC) pari_err_TYPE("ideallistarch",z);
    2866          14 :   z = gel(z,1); /* either a bid or [bid,U] */
    2867          14 :   ID.nf = checknf(bnf);
    2868          14 :   ID.archp = vec01_to_indices(arch);
    2869          14 :   if (lg(z) == 3) { /* the latter: do units */
    2870           7 :     if (typ(z) != t_VEC) pari_err_TYPE("ideallistarch",z);
    2871           7 :     ID.emb = zm_to_ZM( rowpermute(nfsign_units(bnf,NULL,1), ID.archp) );
    2872           7 :     join_z = &join_archunit;
    2873             :   } else
    2874           7 :     join_z = &join_arch;
    2875          14 :   av = avma; V = cgetg(l, t_VEC);
    2876          70 :   for (i = 1; i < l; i++)
    2877             :   {
    2878          56 :     z = gel(L,i); lz = lg(z);
    2879          56 :     gel(V,i) = v = cgetg(lz,t_VEC);
    2880          56 :     for (j=1; j<lz; j++) gel(v,j) = join_z(&ID, gel(z,j));
    2881             :   }
    2882          14 :   return gerepilecopy(av,V);
    2883             : }

Generated by: LCOV version 1.11