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 20277-2bd9113) Lines: 1557 1661 93.7 %
Date: 2017-02-21 05:49:51 Functions: 176 186 94.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. 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     8642409 : get_tab(GEN nf, long *N)
      30             : {
      31     8642409 :   GEN tab = (typ(nf) == t_MAT)? nf: gel(nf,9);
      32     8642409 :   *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   356005555 : _mulii(GEN x, GEN y) {
      38   905175825 :   return is_pm1(x)? (signe(x) < 0)? negi(y): y
      39   549170270 :                   : 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     7449196 : zk_ei_mul(GEN nf, GEN x, long i)
      80             : {
      81             :   long j, k, N;
      82             :   GEN v, tab;
      83             : 
      84     7449196 :   if (i==1) return ZC_copy(x);
      85     7449182 :   tab = get_tab(nf, &N); tab += (i-1)*N;
      86     7449182 :   v = cgetg(N+1,t_COL);
      87    56301578 :   for (k=1; k<=N; k++)
      88             :   {
      89    48852396 :     pari_sp av = avma;
      90    48852396 :     GEN s = gen_0;
      91   621674370 :     for (j=1; j<=N; j++)
      92             :     {
      93   572821974 :       GEN c = gcoeff(tab,k,j);
      94   572821974 :       if (signe(c)) s = addii(s, _mulii(c, gel(x,j)));
      95             :     }
      96    48852396 :     gel(v,k) = gerepileuptoint(av, s);
      97             :   }
      98     7449182 :   return v;
      99             : }
     100             : 
     101             : /* table of multiplication by wi in R[w1,..., wN] */
     102             : GEN
     103         490 : ei_multable(GEN TAB, long i)
     104             : {
     105             :   long k,N;
     106         490 :   GEN m, tab = get_tab(TAB, &N);
     107         490 :   tab += (i-1)*N;
     108         490 :   m = cgetg(N+1,t_MAT);
     109         490 :   for (k=1; k<=N; k++) gel(m,k) = gel(tab,k);
     110         490 :   return m;
     111             : }
     112             : 
     113             : GEN
     114     2990366 : zk_multable(GEN nf, GEN x)
     115             : {
     116     2990366 :   long i, l = lg(x);
     117     2990366 :   GEN mul = cgetg(l,t_MAT);
     118     2990366 :   gel(mul,1) = x; /* assume w_1 = 1 */
     119     2990366 :   for (i=2; i<l; i++) gel(mul,i) = zk_ei_mul(nf,x,i);
     120     2990366 :   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     1920710 : zk_scalar_or_multable(GEN nf, GEN x)
     140             : {
     141     1920710 :   long tx = typ(x);
     142     1920710 :   if (tx == t_MAT || tx == t_INT) return x;
     143     1900923 :   x = nf_to_scalar_or_basis(nf, x);
     144     1900923 :   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       20394 : nfnorm(GEN nf, GEN x)
     181             : {
     182       20394 :   pari_sp av = avma;
     183       20394 :   nf = checknf(nf);
     184       20394 :   if (typ(x) == t_MAT) return famat_norm(nf, x);
     185       20387 :   x = nf_to_scalar_or_alg(nf, x);
     186       59782 :   x = (typ(x) == t_POL)? RgXQ_norm(x, nf_get_pol(nf))
     187       39395 :                        : gpowgs(x, nf_get_degree(nf));
     188       20387 :   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     1607557 : nfadd(GEN nf, GEN x, GEN y)
     205             : {
     206     1607557 :   pari_sp av = avma;
     207             :   GEN z;
     208             : 
     209     1607557 :   nf = checknf(nf);
     210     1607557 :   x = nf_to_scalar_or_basis(nf, x);
     211     1607557 :   y = nf_to_scalar_or_basis(nf, y);
     212     1607557 :   if (typ(x) != t_COL)
     213     1127834 :   { 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     1607557 :   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     3651217 : nfmul(GEN nf, GEN x, GEN y)
     238             : {
     239             :   GEN z;
     240     3651217 :   pari_sp av = avma;
     241             : 
     242     3651217 :   if (x == y) return nfsqr(nf,x);
     243             : 
     244     3190589 :   nf = checknf(nf);
     245     3190589 :   x = nf_to_scalar_or_basis(nf, x);
     246     3190589 :   y = nf_to_scalar_or_basis(nf, y);
     247     3190589 :   if (typ(x) != t_COL)
     248             :   {
     249     2493325 :     if (isintzero(x)) return gen_0;
     250     1939380 :     z = (typ(y) == t_COL)? RgC_Rg_mul(y, x): gmul(x,y); }
     251             :   else
     252             :   {
     253      697264 :     if (typ(y) != t_COL)
     254             :     {
     255      477260 :       if (isintzero(y)) return gen_0;
     256      130697 :       z = RgC_Rg_mul(x, y);
     257             :     }
     258             :     else
     259             :     {
     260             :       GEN dx, dy;
     261      220004 :       x = Q_remove_denom(x, &dx);
     262      220004 :       y = Q_remove_denom(y, &dy);
     263      220004 :       z = nfmuli(nf,x,y);
     264      220004 :       dx = mul_denom(dx,dy);
     265      220004 :       if (dx) z = RgC_Rg_div(z, dx);
     266             :     }
     267             :   }
     268     2290081 :   return gerepileupto(av, z);
     269             : }
     270             : /* square of x in nf */
     271             : GEN
     272      583166 : nfsqr(GEN nf, GEN x)
     273             : {
     274      583166 :   pari_sp av = avma;
     275             :   GEN z;
     276             : 
     277      583166 :   nf = checknf(nf);
     278      583166 :   x = nf_to_scalar_or_basis(nf, x);
     279      583166 :   if (typ(x) != t_COL) z = gsqr(x);
     280             :   else
     281             :   {
     282             :     GEN dx;
     283       75330 :     x = Q_remove_denom(x, &dx);
     284       75330 :     z = nfsqri(nf,x);
     285       75330 :     if (dx) z = RgC_Rg_div(z, sqri(dx));
     286             :   }
     287      583166 :   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       73675 : nfC_nf_mul(GEN nf, GEN v, GEN x)
     331             : {
     332             :   long tx;
     333             :   GEN y;
     334             : 
     335       73675 :   x = nf_to_scalar_or_basis(nf, x);
     336       73675 :   tx = typ(x);
     337       73675 :   if (tx != t_COL)
     338             :   {
     339             :     long l, i;
     340       50080 :     if (tx == t_INT)
     341             :     {
     342       48918 :       long s = signe(x);
     343       48918 :       if (!s) return zerocol(lg(v)-1);
     344       45772 :       if (is_pm1(x)) return s > 0? leafcopy(v): RgC_neg(v);
     345             :     }
     346       12753 :     l = lg(v); y = cgetg(l, t_COL);
     347       99807 :     for (i=1; i < l; i++)
     348             :     {
     349       87054 :       GEN c = gel(v,i);
     350       87054 :       if (typ(c) != t_COL) c = gmul(c, x); else c = RgC_Rg_mul(c, x);
     351       87054 :       gel(y,i) = c;
     352             :     }
     353       12753 :     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      330920 : zkmultable_capZ(GEN mx) { return Q_denom(zkmultable_inv(mx)); }
     394             : 
     395             : GEN
     396      358234 : zkmultable_inv(GEN mx)
     397      358234 : { return ZM_gauss(mx, col_ei(lg(mx)-1,1)); }
     398             : 
     399             : /* nf a true nf, x a ZC */
     400             : GEN
     401       27314 : zk_inv(GEN nf, GEN x) { return zkmultable_inv(zk_multable(nf,x)); }
     402             : 
     403             : /* inverse of x in nf */
     404             : GEN
     405       60277 : nfinv(GEN nf, GEN x)
     406             : {
     407       60277 :   pari_sp av = avma;
     408             :   GEN z;
     409             : 
     410       60277 :   nf = checknf(nf);
     411       60277 :   x = nf_to_scalar_or_basis(nf, x);
     412       60277 :   if (typ(x) == t_COL)
     413             :   {
     414             :     GEN d;
     415        5684 :     x = Q_remove_denom(x, &d);
     416        5684 :     z = zk_inv(nf, x);
     417        5684 :     if (d) z = RgC_Rg_mul(z, d);
     418             :   }
     419             :   else
     420       54593 :     z = ginv(x);
     421       60277 :   return gerepileupto(av, z);
     422             : }
     423             : 
     424             : /* quotient of x and y in nf */
     425             : GEN
     426       19831 : nfdiv(GEN nf, GEN x, GEN y)
     427             : {
     428       19831 :   pari_sp av = avma;
     429             :   GEN z;
     430             : 
     431       19831 :   nf = checknf(nf);
     432       19831 :   y = nf_to_scalar_or_basis(nf, y);
     433       19831 :   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       17522 :     y = Q_remove_denom(y, &d);
     442       17522 :     z = nfmul(nf, x, zk_inv(nf,y));
     443       17522 :     if (d) z = RgC_Rg_mul(z, d);
     444             :   }
     445       19831 :   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      604756 : nfmuli(GEN nf, GEN x, GEN y)
     452             : {
     453             :   long i, j, k, N;
     454      604756 :   GEN s, v, TAB = get_tab(nf, &N);
     455             : 
     456      604756 :   if (typ(x) == t_INT) return (typ(y) == t_COL)? ZC_Z_mul(y, x): mulii(x,y);
     457      529089 :   if (typ(y) == t_INT) return ZC_Z_mul(x, y);
     458             :   /* both x and y are ZV */
     459      503301 :   v = cgetg(N+1,t_COL);
     460     2509897 :   for (k=1; k<=N; k++)
     461             :   {
     462     2006596 :     pari_sp av = avma;
     463     2006596 :     GEN TABi = TAB;
     464     2006596 :     if (k == 1)
     465      503301 :       s = mulii(gel(x,1),gel(y,1));
     466             :     else
     467     3006590 :       s = addii(mulii(gel(x,1),gel(y,k)),
     468     3006590 :                 mulii(gel(x,k),gel(y,1)));
     469    12627012 :     for (i=2; i<=N; i++)
     470             :     {
     471    10620416 :       GEN t, xi = gel(x,i);
     472    10620416 :       TABi += N;
     473    10620416 :       if (!signe(xi)) continue;
     474             : 
     475     6223511 :       t = NULL;
     476    76102071 :       for (j=2; j<=N; j++)
     477             :       {
     478    69878560 :         GEN p1, c = gcoeff(TABi, k, j); /* m^{i,j}_k */
     479    69878560 :         if (!signe(c)) continue;
     480    33759392 :         p1 = _mulii(c, gel(y,j));
     481    33759392 :         t = t? addii(t, p1): p1;
     482             :       }
     483     6223511 :       if (t) s = addii(s, mulii(xi, t));
     484             :     }
     485     2006596 :     gel(v,k) = gerepileuptoint(av,s);
     486             :   }
     487      503301 :   return v;
     488             : }
     489             : /* square of INTEGER (t_INT or ZC) x in nf */
     490             : GEN
     491      582402 : nfsqri(GEN nf, GEN x)
     492             : {
     493             :   long i, j, k, N;
     494      582402 :   GEN s, v, TAB = get_tab(nf, &N);
     495             : 
     496      582402 :   if (typ(x) == t_INT) return sqri(x);
     497      582402 :   v = cgetg(N+1,t_COL);
     498     4550674 :   for (k=1; k<=N; k++)
     499             :   {
     500     3968272 :     pari_sp av = avma;
     501     3968272 :     GEN TABi = TAB;
     502     3968272 :     if (k == 1)
     503      582402 :       s = sqri(gel(x,1));
     504             :     else
     505     3385870 :       s = shifti(mulii(gel(x,1),gel(x,k)), 1);
     506    48669950 :     for (i=2; i<=N; i++)
     507             :     {
     508    44701678 :       GEN p1, c, t, xi = gel(x,i);
     509    44701678 :       TABi += N;
     510    44701678 :       if (!signe(xi)) continue;
     511             : 
     512    14121410 :       c = gcoeff(TABi, k, i);
     513    14121410 :       t = signe(c)? _mulii(c,xi): NULL;
     514   233377592 :       for (j=i+1; j<=N; j++)
     515             :       {
     516   219256182 :         c = gcoeff(TABi, k, j);
     517   219256182 :         if (!signe(c)) continue;
     518   117755040 :         p1 = _mulii(c, shifti(gel(x,j),1));
     519   117755040 :         t = t? addii(t, p1): p1;
     520             :       }
     521    14121410 :       if (t) s = addii(s, mulii(xi, t));
     522             :     }
     523     3968272 :     gel(v,k) = gerepileuptoint(av,s);
     524             :   }
     525      582402 :   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       25595 : _mul(void *data, GEN x, GEN y) { return nfmuli((GEN)data,x,y); }
     609             : static GEN
     610       93130 : _sqr(void *data, GEN x) { return nfsqri((GEN)data,x); }
     611             : 
     612             : /* Compute z^n in nf, left-shift binary powering */
     613             : GEN
     614      115562 : nfpow(GEN nf, GEN z, GEN n)
     615             : {
     616      115562 :   pari_sp av = avma;
     617             :   long s;
     618             :   GEN x, cx;
     619             : 
     620      115562 :   if (typ(n)!=t_INT) pari_err_TYPE("nfpow",n);
     621      115562 :   nf = checknf(nf);
     622      115562 :   s = signe(n); if (!s) return gen_1;
     623      115562 :   x = nf_to_scalar_or_basis(nf, z);
     624      115562 :   if (typ(x) != t_COL) return powgi(x,n);
     625      101548 :   if (s < 0)
     626             :   { /* simplified nfinv */
     627             :     GEN d;
     628        1409 :     x = Q_remove_denom(x, &d);
     629        1409 :     x = zk_inv(nf, x);
     630        1409 :     x = primitive_part(x, &cx);
     631        1409 :     cx = mul_content(cx, d);
     632        1409 :     n = absi(n);
     633             :   }
     634             :   else
     635      100139 :     x = primitive_part(x, &cx);
     636      101548 :   x = gen_pow(x, n, (void*)nf, _sqr, _mul);
     637      101548 :   if (cx) x = gmul(x, powgi(cx, n));
     638      101548 :   return av==avma? gcopy(x): gerepileupto(av,x);
     639             : }
     640             : /* Compute z^n in nf, left-shift binary powering */
     641             : GEN
     642       31990 : nfpow_u(GEN nf, GEN z, ulong n)
     643             : {
     644       31990 :   pari_sp av = avma;
     645             :   GEN x, cx;
     646             : 
     647       31990 :   nf = checknf(nf);
     648       31990 :   if (!n) return gen_1;
     649       31990 :   x = nf_to_scalar_or_basis(nf, z);
     650       31990 :   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     5037852 : ZC_nfvalrem(GEN x, GEN pr, GEN *newx)
     713             : {
     714             :   long i, v, l;
     715     5037852 :   GEN r, y, p = pr_get_p(pr), mul = pr_get_tau(pr);
     716             : 
     717             :   /* p inert */
     718     5037852 :   if (typ(mul) == t_INT) return newx? ZV_pvalrem(x, p, newx):ZV_pval(x, p);
     719     5032056 :   y = cgetg_copy(x, &l); /* will hold the new x */
     720     5032056 :   x = leafcopy(x);
     721     7421374 :   for(v=0;; v++)
     722             :   {
     723    25482522 :     for (i=1; i<l; i++)
     724             :     { /* is (x.b)[i] divisible by p ? */
     725    23093204 :       gel(y,i) = dvmdii(ZMrow_ZC_mul(mul,x,i),p,&r);
     726    23093204 :       if (r != gen_0) { if (newx) *newx = x; return v; }
     727             :     }
     728     2389318 :     swap(x, y);
     729     2389318 :   }
     730             : }
     731             : long
     732     4814198 : ZC_nfval(GEN x, GEN P)
     733     4814198 : { return ZC_nfvalrem(x, P, NULL); }
     734             : 
     735             : /* v_P(x) != 0, x a ZV. Simpler version of ZC_nfvalrem */
     736             : int
     737      210770 : ZC_prdvd(GEN x, GEN P)
     738             : {
     739      210770 :   pari_sp av = avma;
     740             :   long i, l;
     741      210770 :   GEN p = pr_get_p(P), mul = pr_get_tau(P);
     742      210770 :   if (typ(mul) == t_INT) return ZV_Z_dvd(x, p);
     743      210679 :   l = lg(x);
     744      863100 :   for (i=1; i<l; i++)
     745      787987 :     if (remii(ZMrow_ZC_mul(mul,x,i), p) != gen_0) { avma = av; return 0; }
     746       75113 :   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     1312591 : nfval(GEN nf, GEN x, GEN pr)
     763             : {
     764     1312591 :   pari_sp av = avma;
     765             :   long w, e;
     766             :   GEN cx, p;
     767             : 
     768     1312591 :   if (gequal0(x)) return LONG_MAX;
     769     1312045 :   nf = checknf(nf);
     770     1312045 :   checkprid(pr);
     771     1312045 :   p = pr_get_p(pr);
     772     1312045 :   e = pr_get_e(pr);
     773     1312045 :   x = nf_to_scalar_or_basis(nf, x);
     774     1312045 :   if (typ(x) != t_COL) return e*Q_pval(x,p);
     775      128947 :   x = Q_primitive_part(x, &cx);
     776      128947 :   w = ZC_nfval(x,pr);
     777      128947 :   if (cx) w += e*Q_pval(cx,p);
     778      128947 :   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       88557 : coltoalg(GEN nf, GEN x)
     838             : {
     839       88557 :   return mkpolmod( coltoliftalg(nf, x), nf_get_pol(nf) );
     840             : }
     841             : 
     842             : GEN
     843       91756 : basistoalg(GEN nf, GEN x)
     844             : {
     845             :   GEN z, T;
     846             : 
     847       91756 :   nf = checknf(nf);
     848       91756 :   switch(typ(x))
     849             :   {
     850             :     case t_COL: {
     851       82439 :       pari_sp av = avma;
     852       82439 :       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         665 :       T = nf_get_pol(nf);
     861         665 :       if (varn(T) != varn(x)) pari_err_VAR("basistoalg",x,T);
     862         665 :       z = cgetg(3,t_POLMOD);
     863         665 :       gel(z,1) = ZX_copy(T);
     864         665 :       gel(z,2) = RgX_rem(x, T); return z;
     865             :     case t_INT:
     866             :     case t_FRAC:
     867        8533 :       T = nf_get_pol(nf);
     868        8533 :       z = cgetg(3,t_POLMOD);
     869        8533 :       gel(z,1) = ZX_copy(T);
     870        8533 :       gel(z,2) = gcopy(x); return z;
     871             :     default:
     872           0 :       pari_err_TYPE("basistoalg",x);
     873             :       return NULL; /* LCOV_EXCL_LINE */
     874             :   }
     875             : }
     876             : 
     877             : /* Assume nf is a genuine nf. */
     878             : GEN
     879    16270719 : nf_to_scalar_or_basis(GEN nf, GEN x)
     880             : {
     881    16270719 :   switch(typ(x))
     882             :   {
     883             :     case t_INT: case t_FRAC:
     884     9875870 :       return x;
     885             :     case t_POLMOD:
     886      201803 :       x = checknfelt_mod(nf,x,"nf_to_scalar_or_basis");
     887      201733 :       if (typ(x) != t_POL) return x;
     888             :       /* fall through */
     889             :     case t_POL:
     890             :     {
     891      317668 :       GEN T = nf_get_pol(nf);
     892      317668 :       long l = lg(x);
     893      317668 :       if (varn(x) != varn(T)) pari_err_VAR("nf_to_scalar_or_basis", x,T);
     894      317605 :       if (l >= lg(T)) { x = RgX_rem(x, T); l = lg(x); }
     895      317605 :       if (l == 2) return gen_0;
     896      270145 :       if (l == 3) return gel(x,2);
     897      229510 :       return poltobasis(nf,x);
     898             :     }
     899             :     case t_COL:
     900     6025766 :       if (lg(x) != lg(nf_get_zk(nf))) break;
     901     6025703 :       return QV_isscalar(x)? gel(x,1): x;
     902             :   }
     903          70 :   pari_err_TYPE("nf_to_scalar_or_basis",x);
     904             :   return NULL; /* LCOV_EXCL_LINE */
     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        2444 : RgX_to_nfX(GEN nf, GEN x)
     911             : {
     912             :   long i, l;
     913        2444 :   GEN y = cgetg_copy(x, &l); y[1] = x[1];
     914        2444 :   for (i=2; i<l; i++) gel(y,i) = nf_to_scalar_or_basis(nf, gel(x,i));
     915        2444 :   return y;
     916             : }
     917             : 
     918             : /* Assume nf is a genuine nf. */
     919             : GEN
     920      119440 : nf_to_scalar_or_alg(GEN nf, GEN x)
     921             : {
     922      119440 :   switch(typ(x))
     923             :   {
     924             :     case t_INT: case t_FRAC:
     925        5886 :       return x;
     926             :     case t_POLMOD:
     927        1344 :       x = checknfelt_mod(nf,x,"nf_to_scalar_or_alg");
     928        1344 :       if (typ(x) != t_POL) return x;
     929             :       /* fall through */
     930             :     case t_POL:
     931             :     {
     932       13525 :       GEN T = nf_get_pol(nf);
     933       13525 :       long l = lg(x);
     934       13525 :       if (varn(x) != varn(T)) pari_err_VAR("nf_to_scalar_or_alg", x,T);
     935       13525 :       if (l >= lg(T)) { x = RgX_rem(x, T); l = lg(x); }
     936       13525 :       if (l == 2) return gen_0;
     937       13525 :       if (l == 3) return gel(x,2);
     938       13308 :       return x;
     939             :     }
     940             :     case t_COL:
     941       99973 :       if (lg(x) != lg(nf_get_zk(nf))) break;
     942       99973 :       return QV_isscalar(x)? gel(x,1): coltoliftalg(nf, x);
     943             :   }
     944          49 :   pari_err_TYPE("nf_to_scalar_or_alg",x);
     945             :   return NULL; /* LCOV_EXCL_LINE */
     946             : }
     947             : 
     948             : /* gmul(A, RgX_to_RgC(x)), A t_MAT (or t_VEC) of compatible dimensions */
     949             : GEN
     950     1778577 : mulmat_pol(GEN A, GEN x)
     951             : {
     952             :   long i,l;
     953             :   GEN z;
     954     1778577 :   if (typ(x) != t_POL) return gmul(x,gel(A,1)); /* scalar */
     955     1778458 :   l=lg(x)-1; if (l == 1) return typ(A)==t_VEC? gen_0: zerocol(nbrows(A));
     956     1777971 :   x++; z = gmul(gel(x,1), gel(A,1));
     957     7705837 :   for (i=2; i<l ; i++)
     958     5927866 :     if (!gequal0(gel(x,i))) z = gadd(z, gmul(gel(x,i), gel(A,i)));
     959     1777971 :   return z;
     960             : }
     961             : 
     962             : /* x a t_POL, nf a genuine nf. No garbage collecting. No check.  */
     963             : GEN
     964     1646136 : poltobasis(GEN nf, GEN x)
     965             : {
     966     1646136 :   GEN P = nf_get_pol(nf);
     967     1646136 :   if (varn(x) != varn(P)) pari_err_VAR( "poltobasis", x,P);
     968     1646080 :   if (degpol(x) >= degpol(P)) x = RgX_rem(x,P);
     969     1646080 :   return mulmat_pol(nf_get_invzk(nf), x);
     970             : }
     971             : 
     972             : GEN
     973       94658 : algtobasis(GEN nf, GEN x)
     974             : {
     975             :   pari_sp av;
     976             : 
     977       94658 :   nf = checknf(nf);
     978       94658 :   switch(typ(x))
     979             :   {
     980             :     case t_POLMOD:
     981       39480 :       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       39473 :       x = gel(x,2);
     984       39473 :       switch(typ(x))
     985             :       {
     986             :         case t_INT:
     987        2933 :         case t_FRAC: return scalarcol(x, nf_get_degree(nf));
     988             :         case t_POL:
     989       36540 :           av = avma;
     990       36540 :           return gerepileupto(av,poltobasis(nf,x));
     991             :       }
     992           0 :       break;
     993             : 
     994             :     case t_POL:
     995       35498 :       av = avma;
     996       35498 :       return gerepileupto(av,poltobasis(nf,x));
     997             : 
     998             :     case t_COL:
     999        7416 :       if (lg(x)-1 != nf_get_degree(nf)) pari_err_DIM("nfalgtobasis");
    1000        7416 :       return gcopy(x);
    1001             : 
    1002             :     case t_INT:
    1003       12264 :     case t_FRAC: return scalarcol(x, nf_get_degree(nf));
    1004             :   }
    1005           0 :   pari_err_TYPE("algtobasis",x);
    1006             :   return NULL; /* LCOV_EXCL_LINE */
    1007             : }
    1008             : 
    1009             : GEN
    1010       35700 : rnfbasistoalg(GEN rnf,GEN x)
    1011             : {
    1012       35700 :   const char *f = "rnfbasistoalg";
    1013             :   long lx, i;
    1014       35700 :   pari_sp av = avma;
    1015             :   GEN z, nf, relpol, T;
    1016             : 
    1017       35700 :   checkrnf(rnf);
    1018       35700 :   nf = rnf_get_nf(rnf);
    1019       35700 :   T = nf_get_pol(nf);
    1020       35700 :   relpol = QXQX_to_mod_shallow(rnf_get_pol(rnf), T);
    1021       35700 :   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       23744 :       x = polmod_nffix(f, rnf, x, 0);
    1036       23534 :       if (typ(x) != t_POL) break;
    1037        9569 :       retmkpolmod(RgX_copy(x), RgX_copy(relpol));
    1038             :     case t_POL:
    1039         826 :       if (varn(x) == varn(T)) { RgX_check_QX(x,f); x = gmodulo(x,T); break; }
    1040         602 :       if (varn(x) == varn(relpol))
    1041             :       {
    1042         553 :         x = RgX_nffix(f,nf_get_pol(nf),x,0);
    1043         553 :         return gmodulo(x, relpol);
    1044             :       }
    1045          49 :       pari_err_VAR(f, x,relpol);
    1046             :   }
    1047       24472 :   retmkpolmod(scalarpol(x, varn(relpol)), RgX_copy(relpol));
    1048             : }
    1049             : 
    1050             : GEN
    1051         840 : matbasistoalg(GEN nf,GEN x)
    1052             : {
    1053             :   long i, j, li, lx;
    1054         840 :   GEN z = cgetg_copy(x, &lx);
    1055             : 
    1056         840 :   if (lx == 1) return z;
    1057         833 :   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         805 :     case t_MAT: break;
    1063           0 :     default: pari_err_TYPE("matbasistoalg",x);
    1064             :   }
    1065         805 :   li = lgcols(x);
    1066        2954 :   for (j=1; j<lx; j++)
    1067             :   {
    1068        2149 :     GEN c = cgetg(li,t_COL), xj = gel(x,j);
    1069        2149 :     gel(z,j) = c;
    1070        2149 :     for (i=1; i<li; i++) gel(c,i) = basistoalg(nf, gel(xj,i));
    1071             :   }
    1072         805 :   return z;
    1073             : }
    1074             : 
    1075             : GEN
    1076        2457 : matalgtobasis(GEN nf,GEN x)
    1077             : {
    1078             :   long i, j, li, lx;
    1079        2457 :   GEN z = cgetg_copy(x, &lx);
    1080             : 
    1081        2457 :   if (lx == 1) return z;
    1082        2401 :   switch(typ(x))
    1083             :   {
    1084             :     case t_VEC: case t_COL:
    1085        2394 :       for (i=1; i<lx; i++) gel(z,i) = algtobasis(nf, gel(x,i));
    1086        2394 :       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        3143 : RgM_to_nfM(GEN nf,GEN x)
    1101             : {
    1102             :   long i, j, li, lx;
    1103        3143 :   GEN z = cgetg_copy(x, &lx);
    1104             : 
    1105        3143 :   if (lx == 1) return z;
    1106        3143 :   li = lgcols(x);
    1107       22743 :   for (j=1; j<lx; j++)
    1108             :   {
    1109       19600 :     GEN c = cgetg(li,t_COL), xj = gel(x,j);
    1110       19600 :     gel(z,j) = c;
    1111       19600 :     for (i=1; i<li; i++) gel(c,i) = nf_to_scalar_or_basis(nf, gel(xj,i));
    1112             :   }
    1113        3143 :   return z;
    1114             : }
    1115             : GEN
    1116       36792 : RgC_to_nfC(GEN nf,GEN x)
    1117             : {
    1118       36792 :   long i, lx = lg(x);
    1119       36792 :   GEN z = cgetg(lx, t_COL);
    1120       36792 :   for (i=1; i<lx; i++) gel(z,i) = nf_to_scalar_or_basis(nf, gel(x,i));
    1121       36792 :   return z;
    1122             : }
    1123             : 
    1124             : /* x a t_POLMOD, supposedly in rnf = K[z]/(T), K = Q[y]/(Tnf) */
    1125             : GEN
    1126       60697 : polmod_nffix(const char *f, GEN rnf, GEN x, int lift)
    1127       60697 : { return polmod_nffix2(f, rnf_get_nfpol(rnf), rnf_get_pol(rnf), x,lift); }
    1128             : GEN
    1129       60788 : polmod_nffix2(const char *f, GEN T, GEN relpol, GEN x, int lift)
    1130             : {
    1131       60788 :   if (RgX_equal_var(gel(x,1),relpol))
    1132             :   {
    1133       55482 :     x = gel(x,2);
    1134       55482 :     if (typ(x) == t_POL && varn(x) == varn(relpol))
    1135             :     {
    1136       40033 :       x = RgX_nffix(f, T, x, lift);
    1137       40033 :       switch(lg(x))
    1138             :       {
    1139       11158 :         case 2: return gen_0;
    1140        3927 :         case 3: return gel(x,2);
    1141             :       }
    1142       24948 :       return x;
    1143             :     }
    1144             :   }
    1145       20755 :   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       12026 : sarch_get_cyc(GEN sarch) { return gel(sarch,1); }
    1218             : static GEN
    1219      205528 : sarch_get_archp(GEN sarch) { return gel(sarch,2); }
    1220             : static GEN
    1221       44944 : sarch_get_MI(GEN sarch) { return gel(sarch,3); }
    1222             : static GEN
    1223       44944 : sarch_get_lambda(GEN sarch) { return gel(sarch,4); }
    1224             : static GEN
    1225       44944 : sarch_get_F(GEN sarch) { return gel(sarch,5); }
    1226             : 
    1227             : /* true nf, return number of positive roots of char_x */
    1228             : static long
    1229        1434 : num_positive(GEN nf, GEN x)
    1230             : {
    1231        1434 :   GEN T = nf_get_pol(nf);
    1232        1434 :   GEN charx = ZXQ_charpoly(coltoliftalg(nf,x), T, 0);
    1233             :   long np;
    1234        1434 :   charx = ZX_radical(charx);
    1235        1434 :   np = ZX_sturmpart(charx, mkvec2(gen_0,mkoo()));
    1236        1434 :   return np * (degpol(T) / degpol(charx));
    1237             : }
    1238             : 
    1239             : /* x a QC: return sigma_k(x) where 1 <= k <= r1+r2; correct but inefficient
    1240             :  * if x in Q. M = nf_get_M(nf) */
    1241             : static GEN
    1242         168 : nfembed_i(GEN M, GEN x, long k)
    1243             : {
    1244         168 :   long i, l = lg(M);
    1245         168 :   GEN z = gel(x,1);
    1246         168 :   for (i = 2; i < l; i++) z = gadd(z, gmul(gcoeff(M,k,i), gel(x,i)));
    1247         168 :   return z;
    1248             : }
    1249             : GEN
    1250        1589 : nfembed(GEN nf, GEN x, long k)
    1251             : {
    1252        1589 :   pari_sp av = avma;
    1253        1589 :   nf = checknf(nf);
    1254        1589 :   x = nf_to_scalar_or_basis(nf,x);
    1255        1589 :   if (typ(x) != t_COL) return gerepilecopy(av, x);
    1256           0 :   return gerepileupto(av, nfembed_i(nf_get_M(nf),x,k));
    1257             : }
    1258             : 
    1259             : /* x a ZC */
    1260             : static GEN
    1261      384321 : zk_embed(GEN M, GEN x, long k)
    1262             : {
    1263      384321 :   long i, l = lg(x);
    1264      384321 :   GEN z = gel(x,1); /* times M[k,1], which is 1 */
    1265      384321 :   for (i = 2; i < l; i++) z = mpadd(z, mpmul(gcoeff(M,k,i), gel(x,i)));
    1266      384321 :   return z;
    1267             : }
    1268             : 
    1269             : /* Given floating point approximation z of sigma_k(x), decide its sign
    1270             :  * [0/+, 1/- and -1 for FAIL] */
    1271             : static long
    1272      373030 : eval_sign_embed(GEN z)
    1273             : { /* dubious, fail */
    1274      373030 :   if (typ(z) == t_REAL && realprec(z) <= LOWDEFAULTPREC) return -1;
    1275      372090 :   return (signe(z) < 1)? 1: 0;
    1276             : }
    1277             : /* return v such that (-1)^v = sign(sigma_k(x)), x primitive ZC */
    1278             : static long
    1279      304857 : eval_sign(GEN M, GEN x, long k)
    1280      304857 : { return eval_sign_embed( zk_embed(M, x, k) ); }
    1281             : 
    1282             : /* check that signs[i..#signs] == s; signs = NULL encodes "totally positive" */
    1283             : static int
    1284           0 : oksigns(long l, GEN signs, long i, long s)
    1285             : {
    1286           0 :   if (!signs) return s == 0;
    1287           0 :   for (; i < l; i++)
    1288           0 :     if (signs[i] != s) return 0;
    1289           0 :   return 1;
    1290             : }
    1291             : /* check that signs[i] = s and signs[i+1..#signs] = 1-s */
    1292             : static int
    1293           0 : oksigns2(long l, GEN signs, long i, long s)
    1294             : {
    1295           0 :   if (!signs) return s == 0 && i == l-1;
    1296           0 :   return signs[i] == s && oksigns(l, signs, i+1, 1-s);
    1297             : }
    1298             : 
    1299             : /* true nf, x a ZC (primitive for efficiency), embx its embeddings or NULL */
    1300             : static int
    1301       59633 : nfchecksigns_i(GEN nf, GEN x, GEN embx, GEN signs, GEN archp)
    1302             : {
    1303       59633 :   long l = lg(archp), i;
    1304       59633 :   GEN M = nf_get_M(nf), sarch = NULL;
    1305       59633 :   long np = -1;
    1306       87479 :   for (i = 1; i < l; i++)
    1307             :   {
    1308             :     long s;
    1309       68362 :     if (embx)
    1310       68173 :       s = eval_sign_embed(gel(embx,i));
    1311             :     else
    1312         189 :       s = eval_sign(M, x, archp[i]);
    1313             :     /* 0 / + or 1 / -; -1 for FAIL */
    1314       68362 :     if (s < 0) /* failure */
    1315             :     {
    1316           0 :       long ni, r1 = nf_get_r1(nf);
    1317             :       GEN xi;
    1318           0 :       if (np < 0)
    1319             :       {
    1320           0 :         np = num_positive(nf, x);
    1321           0 :         if (np == 0)  return oksigns(l, signs, i, 1);
    1322           0 :         if (np == r1) return oksigns(l, signs, i, 0);
    1323           0 :         sarch = nfarchstar(nf, NULL, identity_perm(r1));
    1324             :       }
    1325           0 :       xi = set_sign_mod_divisor(nf, vecsmall_ei(r1, archp[i]), gen_1, sarch);
    1326           0 :       xi = Q_primpart(xi);
    1327           0 :       ni = num_positive(nf, nfmuli(nf,x,xi));
    1328           0 :       if (ni == 0)  return oksigns2(l, signs, i, 0);
    1329           0 :       if (ni == r1) return oksigns2(l, signs, i, 1);
    1330           0 :       s = ni < np? 0: 1;
    1331             :     }
    1332       68362 :     if (s != (signs? signs[i]: 0)) return 0;
    1333             :   }
    1334       19117 :   return 1;
    1335             : }
    1336             : static void
    1337         161 : pl_convert(GEN pl, GEN *psigns, GEN *parchp)
    1338             : {
    1339         161 :   long i, j, l = lg(pl);
    1340         161 :   GEN signs = cgetg(l, t_VECSMALL);
    1341         161 :   GEN archp = cgetg(l, t_VECSMALL);
    1342         406 :   for (i = j = 1; i < l; i++)
    1343             :   {
    1344         245 :     if (!pl[i]) continue;
    1345         231 :     archp[j] = i;
    1346         231 :     signs[j] = (pl[i] < 0)? 1: 0;
    1347         231 :     j++;
    1348             :   }
    1349         161 :   setlg(archp, j); *parchp = archp;
    1350         161 :   setlg(signs, j); *psigns = signs;
    1351         161 : }
    1352             : /* pl : requested signs for real embeddings, 0 = no sign constraint */
    1353             : int
    1354         511 : nfchecksigns(GEN nf, GEN x, GEN pl)
    1355             : {
    1356         511 :   pari_sp av = avma;
    1357             :   GEN signs, archp;
    1358             :   int res;
    1359         511 :   nf = checknf(nf);
    1360         511 :   x = nf_to_scalar_or_basis(nf,x);
    1361         511 :   if (typ(x) != t_COL)
    1362             :   {
    1363         350 :     long i, l = lg(pl), s = gsigne(x);
    1364         777 :     for (i = 1; i < l; i++)
    1365         427 :       if (pl[i] && pl[i] != s) { avma = av; return 0; }
    1366         350 :     avma = av; return 1;
    1367             :   }
    1368         161 :   pl_convert(pl, &signs, &archp);
    1369         161 :   res = nfchecksigns_i(nf, x, NULL, signs, archp);
    1370         161 :   avma = av; return res;
    1371             : }
    1372             : 
    1373             : /* signs = NULL: totally positive, else sign[i] = 0 (+) or 1 (-) */
    1374             : static GEN
    1375       44944 : get_C(GEN lambda, long l, GEN signs)
    1376             : {
    1377             :   long i;
    1378             :   GEN C, mlambda;
    1379       44944 :   if (!signs) return const_vec(l-1, lambda);
    1380        5485 :   C = cgetg(l, t_COL); mlambda = gneg(lambda);
    1381        5485 :   for (i = 1; i < l; i++) gel(C,i) = signs[i]? mlambda: lambda;
    1382        5485 :   return C;
    1383             : }
    1384             : /* signs = NULL: totally positive at archp */
    1385             : static GEN
    1386       69276 : nfsetsigns(GEN nf, GEN signs, GEN x, GEN sarch)
    1387             : {
    1388       69276 :   long i, l = lg(sarch_get_archp(sarch));
    1389             :   GEN ex;
    1390             :   /* Is signature already correct ? */
    1391       69276 :   if (typ(x) != t_COL)
    1392             :   {
    1393        9804 :     long s = gsigne(x) < 0? 1: 0;
    1394        9804 :     if (!signs)
    1395        2373 :       i = (s == 1)? 1: l;
    1396             :     else
    1397             :     {
    1398       11988 :       for (i = 1; i < l; i++)
    1399        9013 :         if (signs[i] != s) break;
    1400             :     }
    1401        9804 :     ex = (i < l)? const_col(l-1, x): NULL;
    1402             :   }
    1403             :   else
    1404             :   {
    1405       59472 :     pari_sp av = avma;
    1406       59472 :     GEN cex, M = nf_get_M(nf), archp = sarch_get_archp(sarch);
    1407       59472 :     GEN xp = Q_primitive_part(x,&cex);
    1408       59472 :     ex = cgetg(l,t_COL);
    1409       59472 :     for (i = 1; i < l; i++) gel(ex,i) = zk_embed(M,xp,archp[i]);
    1410       59472 :     if (nfchecksigns_i(nf, xp, ex, signs, archp)) { ex = NULL; avma = av; }
    1411       40474 :     else if (cex) ex = RgC_Rg_mul(ex, cex); /* put back content */
    1412             :   }
    1413       69276 :   if (ex)
    1414             :   { /* If no, fix it */
    1415       44944 :     GEN lambda = sarch_get_lambda(sarch);
    1416       44944 :     GEN MI = sarch_get_MI(sarch);
    1417       44944 :     GEN F = sarch_get_F(sarch);
    1418       44944 :     GEN t = RgC_sub(get_C(lambda, l, signs), ex);
    1419             :     long e;
    1420       44944 :     t = grndtoi(RgM_RgC_mul(MI,t), &e);
    1421       44944 :     if (lg(F) != 1) t = ZM_ZC_mul(F, t);
    1422       44944 :     x = typ(x) == t_COL? RgC_add(t, x): RgC_Rg_add(t, x);
    1423             :   }
    1424       69276 :   return x;
    1425             : }
    1426             : /* - sarch = nfarchstar(nf, F);
    1427             :  * - x encodes a vector of signs at arch.archp: either a t_VECSMALL
    1428             :  *   (vector of signs as {0,1}-vector), NULL (totally positive at archp),
    1429             :  *   or a non-zero number field element (replaced by its signature at archp);
    1430             :  * - y is a non-zero number field element
    1431             :  * Return z = y (mod F) with signs(y, archp) = signs(x) (a {0,1}-vector) */
    1432             : GEN
    1433       76780 : set_sign_mod_divisor(GEN nf, GEN x, GEN y, GEN sarch)
    1434             : {
    1435       76780 :   GEN archp = sarch_get_archp(sarch);
    1436       76780 :   if (lg(archp) == 1) return y;
    1437       68002 :   nf = checknf(nf);
    1438       68002 :   if (x && typ(x) != t_VECSMALL) x = nfsign_arch(nf, x, archp);
    1439       68002 :   y = nf_to_scalar_or_basis(nf,y);
    1440       68002 :   return nfsetsigns(nf, x, y, sarch);
    1441             : }
    1442             : 
    1443             : static GEN
    1444        5695 : setsigns_init(GEN nf, GEN archp, GEN F, GEN DATA)
    1445             : {
    1446        5695 :   GEN lambda, Mr = rowpermute(nf_get_M(nf), archp), MI = F? RgM_mul(Mr,F): Mr;
    1447        5695 :   lambda = gmul2n(matrixnorm(MI,DEFAULTPREC), -1);
    1448        5695 :   if (lg(archp) < lg(MI))
    1449             :   {
    1450        4081 :     GEN perm = gel(indexrank(MI), 2);
    1451        4081 :     if (!F) F = matid(nf_get_degree(nf));
    1452        4081 :     MI = vecpermute(MI, perm);
    1453        4081 :     F = vecpermute(F, perm);
    1454             :   }
    1455        5695 :   if (!F) F = cgetg(1,t_MAT);
    1456        5695 :   MI = RgM_inv(MI);
    1457        5695 :   return mkvec5(DATA, archp, MI, lambda, F);
    1458             : }
    1459             : /* F non-0 integral ideal in HNF (or NULL: Z_K), compute elements in 1+F
    1460             :  * whose sign matrix at archp is identity; archp in 'indices' format */
    1461             : GEN
    1462        7802 : nfarchstar(GEN nf, GEN F, GEN archp)
    1463             : {
    1464        7802 :   long nba = lg(archp) - 1;
    1465        7802 :   if (!nba) return mkvec2(cgetg(1,t_VEC), archp);
    1466        4428 :   if (F && equali1(gcoeff(F,1,1))) F = NULL;
    1467        4428 :   if (F) F = idealpseudored(F, nf_get_roundG(nf));
    1468        4428 :   return setsigns_init(nf, archp, F, const_vec(nba, gen_2));
    1469             : }
    1470             : 
    1471             : /*************************************************************************/
    1472             : /**                                                                     **/
    1473             : /**                         IDEALCHINESE                                **/
    1474             : /**                                                                     **/
    1475             : /*************************************************************************/
    1476             : static int
    1477        2016 : isprfact(GEN x)
    1478             : {
    1479             :   long i, l;
    1480             :   GEN L, E;
    1481        2016 :   if (typ(x) != t_MAT || lg(x) != 3) return 0;
    1482        2016 :   L = gel(x,1); l = lg(L);
    1483        2016 :   E = gel(x,2);
    1484        4774 :   for(i=1; i<l; i++)
    1485             :   {
    1486        2758 :     checkprid(gel(L,i));
    1487        2758 :     if (typ(gel(E,i)) != t_INT) return 0;
    1488             :   }
    1489        2016 :   return 1;
    1490             : }
    1491             : 
    1492             : /* initialize projectors mod pr[i]^e[i] for idealchinese */
    1493             : static GEN
    1494        2016 : pr_init(GEN nf, GEN fa, GEN w, GEN dw)
    1495             : {
    1496        2016 :   GEN U, E, F, L = gel(fa,1), E0 = gel(fa,2);
    1497        2016 :   long i, r = lg(L);
    1498             : 
    1499        2016 :   if (w && lg(w) != r) pari_err_TYPE("idealchinese", w);
    1500        2016 :   if (r == 1 && !dw) return cgetg(1,t_VEC);
    1501        2009 :   E = leafcopy(E0); /* do not destroy fa[2] */
    1502        4767 :   for (i = 1; i < r; i++)
    1503        2758 :     if (signe(gel(E,i)) < 0) gel(E,i) = gen_0;
    1504        2009 :   F = factorbackprime(nf, L, E);
    1505        2009 :   if (dw)
    1506             :   {
    1507         686 :     F = ZM_Z_mul(F, dw);
    1508        1568 :     for (i = 1; i < r; i++)
    1509             :     {
    1510         882 :       GEN pr = gel(L,i);
    1511         882 :       long e = itos(gel(E0,i)), v = idealval(nf, dw, pr);
    1512         882 :       if (e >= 0)
    1513         875 :         gel(E,i) = addiu(gel(E,i), v);
    1514           7 :       else if (v + e <= 0)
    1515           0 :         F = idealmulpowprime(nf, F, pr, stoi(-v)); /* coprime to pr */
    1516             :       else
    1517             :       {
    1518           7 :         F = idealmulpowprime(nf, F, pr, stoi(e));
    1519           7 :         gel(E,i) = stoi(v + e);
    1520             :       }
    1521             :     }
    1522             :   }
    1523        2009 :   U = cgetg(r, t_VEC);
    1524        4767 :   for (i = 1; i < r; i++)
    1525             :   {
    1526             :     GEN u;
    1527        2758 :     if (w && gequal0(gel(w,i))) u = gen_0; /* unused */
    1528             :     else
    1529             :     {
    1530        2702 :       GEN pr = gel(L,i), e = gel(E,i), t;
    1531        2702 :       t = idealdivpowprime(nf,F, pr, e);
    1532        2702 :       u = hnfmerge_get_1(t, idealpow(nf, pr, e));
    1533        2702 :       if (!u) pari_err_COPRIME("idealchinese", t,pr);
    1534             :     }
    1535        2758 :     gel(U,i) = u;
    1536             :   }
    1537        2009 :   F = idealpseudored(F, nf_get_roundG(nf));
    1538        2009 :   return mkvec2(F, U);
    1539             : }
    1540             : 
    1541             : static GEN
    1542        1267 : pl_normalize(GEN nf, GEN pl)
    1543             : {
    1544        1267 :   const char *fun = "idealchinese";
    1545        1267 :   if (lg(pl)-1 != nf_get_r1(nf)) pari_err_TYPE(fun,pl);
    1546        1267 :   switch(typ(pl))
    1547             :   {
    1548         679 :     case t_VEC: RgV_check_ZV(pl,fun); pl = ZV_to_zv(pl);
    1549             :       /* fall through */
    1550        1267 :     case t_VECSMALL: break;
    1551           0 :     default: pari_err_TYPE(fun,pl);
    1552             :   }
    1553        1267 :   return pl;
    1554             : }
    1555             : 
    1556             : static int
    1557        4788 : is_chineseinit(GEN x)
    1558             : {
    1559             :   GEN fa, pl;
    1560             :   long l;
    1561        4788 :   if (typ(x) != t_VEC || lg(x)!=3) return 0;
    1562        3556 :   fa = gel(x,1);
    1563        3556 :   pl = gel(x,2);
    1564        3556 :   if (typ(fa) != t_VEC || typ(pl) != t_VEC) return 0;
    1565        1505 :   l = lg(fa);
    1566        1505 :   if (l != 1)
    1567             :   {
    1568        1484 :     if (l != 3 || typ(gel(fa,1)) != t_MAT || typ(gel(fa,2)) != t_VEC)
    1569           7 :       return 0;
    1570             :   }
    1571        1498 :   l = lg(pl);
    1572        1498 :   if (l != 1)
    1573             :   {
    1574         483 :     if (l != 6 || typ(gel(pl,3)) != t_MAT || typ(gel(pl,1)) != t_VECSMALL
    1575         483 :                                           || typ(gel(pl,2)) != t_VECSMALL)
    1576           0 :       return 0;
    1577             :   }
    1578        1498 :   return 1;
    1579             : }
    1580             : 
    1581             : /* nf a true 'nf' */
    1582             : static GEN
    1583        2079 : chineseinit_i(GEN nf, GEN fa, GEN w, GEN dw)
    1584             : {
    1585        2079 :   const char *fun = "idealchineseinit";
    1586        2079 :   GEN archp = NULL, pl = NULL;
    1587        2079 :   switch(typ(fa))
    1588             :   {
    1589             :     case t_VEC:
    1590        1267 :       if (is_chineseinit(fa))
    1591             :       {
    1592           0 :         if (dw) pari_err_DOMAIN(fun, "denom(y)", "!=", gen_1, w);
    1593           0 :         return fa;
    1594             :       }
    1595        1267 :       if (lg(fa) != 3) pari_err_TYPE(fun, fa);
    1596             :       /* of the form [x,s] */
    1597        1267 :       pl = pl_normalize(nf, gel(fa,2));
    1598        1267 :       fa = gel(fa,1);
    1599        1267 :       archp = vecsmall01_to_indices(pl);
    1600             :       /* keep pr_init, reset pl */
    1601        1267 :       if (is_chineseinit(fa)) { fa = gel(fa,1); break; }
    1602             :       /* fall through */
    1603             :     case t_MAT: /* factorization? */
    1604        2016 :       if (isprfact(fa)) { fa = pr_init(nf, fa, w, dw); break; }
    1605           0 :     default: pari_err_TYPE(fun,fa);
    1606             :   }
    1607             : 
    1608        2079 :   if (pl)
    1609             :   {
    1610        1267 :     GEN F = (lg(fa) == 1)? NULL: gel(fa,1);
    1611        1267 :     long i, r = lg(archp);
    1612        1267 :     GEN signs = cgetg(r, t_VECSMALL);
    1613        1267 :     for (i = 1; i < r; i++) signs[i] = (pl[archp[i]] < 0)? 1: 0;
    1614        1267 :     pl = setsigns_init(nf, archp, F, signs);
    1615             :   }
    1616             :   else
    1617         812 :     pl = cgetg(1,t_VEC);
    1618        2079 :   return mkvec2(fa, pl);
    1619             : }
    1620             : 
    1621             : /* Given a prime ideal factorization x, possibly with 0 or negative exponents,
    1622             :  * and a vector w of elements of nf, gives b such that
    1623             :  * v_p(b-w_p)>=v_p(x) for all prime ideals p in the ideal factorization
    1624             :  * and v_p(b)>=0 for all other p, using the standard proof given in GTM 138. */
    1625             : GEN
    1626        3514 : idealchinese(GEN nf, GEN x, GEN w)
    1627             : {
    1628        3514 :   const char *fun = "idealchinese";
    1629        3514 :   pari_sp av = avma;
    1630             :   GEN x1, x2, s, dw, F;
    1631             : 
    1632        3514 :   nf = checknf(nf);
    1633        3514 :   if (!w) return gerepilecopy(av, chineseinit_i(nf,x,NULL,NULL));
    1634             : 
    1635        2254 :   if (typ(w) != t_VEC) pari_err_TYPE(fun,w);
    1636        2254 :   w = Q_remove_denom(matalgtobasis(nf,w), &dw);
    1637        2254 :   if (!is_chineseinit(x)) x = chineseinit_i(nf,x,w,dw);
    1638             :   /* x is a 'chineseinit' */
    1639        2254 :   x1 = gel(x,1); s = NULL;
    1640        2254 :   if (lg(x1) == 1) F = NULL;
    1641             :   else
    1642             :   {
    1643        2233 :     GEN  U = gel(x1,2);
    1644        2233 :     long i, r = lg(w);
    1645        2233 :     F = gel(x1,1);
    1646        5481 :     for (i=1; i<r; i++)
    1647        3248 :       if (!gequal0(gel(w,i)))
    1648             :       {
    1649        2723 :         GEN t = nfmuli(nf, gel(U,i), gel(w,i));
    1650        2723 :         s = s? ZC_add(s,t): t;
    1651             :       }
    1652        2233 :     if (s) s = ZC_reducemodmatrix(s, F);
    1653             :   }
    1654        2254 :   if (!s) { s = zerocol(nf_get_degree(nf)); dw = NULL; }
    1655             : 
    1656        2254 :   x2 = gel(x,2);
    1657        2254 :   if (lg(x2) != 1) s = nfsetsigns(nf, gel(x2,1), s, x2);
    1658        2254 :   if (dw) s = RgC_Rg_div(s,dw);
    1659        2254 :   return gerepileupto(av, s);
    1660             : }
    1661             : 
    1662             : /*************************************************************************/
    1663             : /**                                                                     **/
    1664             : /**                           (Z_K/I)^*                                 **/
    1665             : /**                                                                     **/
    1666             : /*************************************************************************/
    1667             : GEN
    1668        1267 : vecsmall01_to_indices(GEN v)
    1669             : {
    1670        1267 :   long i, k, l = lg(v);
    1671        1267 :   GEN p = new_chunk(l) + l;
    1672        3682 :   for (k=1, i=l-1; i; i--)
    1673        2415 :     if (v[i]) { *--p = i; k++; }
    1674        1267 :   *--p = evallg(k) | evaltyp(t_VECSMALL);
    1675        1267 :   avma = (pari_sp)p; return p;
    1676             : }
    1677             : GEN
    1678      322021 : vec01_to_indices(GEN v)
    1679             : {
    1680             :   long i, k, l;
    1681             :   GEN p;
    1682             : 
    1683      322021 :   switch (typ(v))
    1684             :   {
    1685      315980 :    case t_VECSMALL: return v;
    1686        6041 :    case t_VEC: break;
    1687           0 :    default: pari_err_TYPE("vec01_to_indices",v);
    1688             :   }
    1689        6041 :   l = lg(v);
    1690        6041 :   p = new_chunk(l) + l;
    1691       16513 :   for (k=1, i=l-1; i; i--)
    1692       10472 :     if (signe(gel(v,i))) { *--p = i; k++; }
    1693        6041 :   *--p = evallg(k) | evaltyp(t_VECSMALL);
    1694        6041 :   avma = (pari_sp)p; return p;
    1695             : }
    1696             : GEN
    1697        4809 : indices_to_vec01(GEN p, long r)
    1698             : {
    1699        4809 :   long i, l = lg(p);
    1700        4809 :   GEN v = zerovec(r);
    1701        4809 :   for (i = 1; i < l; i++) gel(v, p[i]) = gen_1;
    1702        4809 :   return v;
    1703             : }
    1704             : 
    1705             : /* return (column) vector of R1 signatures of x (0 or 1) */
    1706             : GEN
    1707      315980 : nfsign_arch(GEN nf, GEN x, GEN arch)
    1708             : {
    1709      315980 :   GEN sarch, M, V, archp = vec01_to_indices(arch);
    1710      315980 :   long i, s, np, n = lg(archp)-1;
    1711             :   pari_sp av;
    1712             : 
    1713      315980 :   if (!n) return cgetg(1,t_VECSMALL);
    1714      315896 :   nf = checknf(nf);
    1715      315896 :   if (typ(x) == t_MAT)
    1716             :   { /* factorisation */
    1717       92054 :     GEN g = gel(x,1), e = gel(x,2);
    1718       92054 :     V = zero_zv(n);
    1719      274839 :     for (i=1; i<lg(g); i++)
    1720      182785 :       if (mpodd(gel(e,i)))
    1721      157662 :         Flv_add_inplace(V, nfsign_arch(nf,gel(g,i),archp), 2);
    1722       92054 :     avma = (pari_sp)V; return V;
    1723             :   }
    1724      223842 :   av = avma; V = cgetg(n+1,t_VECSMALL);
    1725      223842 :   x = nf_to_scalar_or_basis(nf, x);
    1726      223842 :   switch(typ(x))
    1727             :   {
    1728             :     case t_INT:
    1729       46400 :       s = signe(x);
    1730       46400 :       if (!s) pari_err_DOMAIN("nfsign_arch","element","=",gen_0,x);
    1731       46400 :       avma = av; return const_vecsmall(n, (s < 0)? 1: 0);
    1732             :     case t_FRAC:
    1733           0 :       s = signe(gel(x,1));
    1734           0 :       avma = av; return const_vecsmall(n, (s < 0)? 1: 0);
    1735             :   }
    1736      177442 :   x = Q_primpart(x); M = nf_get_M(nf); sarch = NULL; np = -1;
    1737      481170 :   for (i = 1; i <= n; i++)
    1738             :   {
    1739      304668 :     long s = eval_sign(M, x, archp[i]);
    1740      304668 :     if (s < 0) /* failure */
    1741             :     {
    1742         940 :       long ni, r1 = nf_get_r1(nf);
    1743             :       GEN xi;
    1744         940 :       if (np < 0)
    1745             :       {
    1746         940 :         np = num_positive(nf, x);
    1747         940 :         if (np == 0) { avma = av; return const_vecsmall(n, 1); }
    1748         802 :         if (np == r1){ avma = av; return const_vecsmall(n, 0); }
    1749         494 :         sarch = nfarchstar(nf, NULL, identity_perm(r1));
    1750             :       }
    1751         494 :       xi = set_sign_mod_divisor(nf, vecsmall_ei(r1, archp[i]), gen_1, sarch);
    1752         494 :       xi = Q_primpart(xi);
    1753         494 :       ni = num_positive(nf, nfmuli(nf,x,xi));
    1754         494 :       if (ni == 0) { avma = av; V = const_vecsmall(n, 1); V[i] = 0; return V; }
    1755         356 :       if (ni == r1){ avma = av; V = const_vecsmall(n, 0); V[i] = 1; return V; }
    1756           0 :       s = ni < np? 0: 1;
    1757             :     }
    1758      303728 :     V[i] = s;
    1759             :   }
    1760      176502 :   avma = (pari_sp)V; return V;
    1761             : }
    1762             : static void
    1763        1855 : chk_ind(const char *s, long i, long r1)
    1764             : {
    1765        1855 :   if (i <= 0)
    1766           7 :     pari_err_DOMAIN(s, "index", "<=", gen_0, stoi(i));
    1767        1848 :   if (i > r1)
    1768          21 :     pari_err_DOMAIN(s, "index", ">", stoi(r1), stoi(i));
    1769        1827 : }
    1770             : GEN
    1771         770 : nfeltsign(GEN nf, GEN x, GEN ind0)
    1772             : {
    1773         770 :   pari_sp av = avma;
    1774             :   long i, l, r1;
    1775             :   GEN v, ind;
    1776         770 :   nf = checknf(nf);
    1777         770 :   r1 = nf_get_r1(nf);
    1778         770 :   x = nf_to_scalar_or_basis(nf, x);
    1779         770 :   if (!ind0) ind0 = identity_perm(r1);
    1780         770 :   switch(typ(ind0))
    1781             :   {
    1782             :     case t_INT: case t_VEC: case t_COL:
    1783          56 :       ind = gtovecsmall(ind0); break;
    1784             :     case t_VECSMALL:
    1785         714 :       ind = ind0; break;
    1786             :     default:
    1787           0 :       pari_err_TYPE("nfeltsign",ind0);
    1788             :       return NULL; /* LCOV_EXCL_LINE */
    1789             :   }
    1790         770 :   l = lg(ind);
    1791         770 :   for (i = 1; i < l; i++) chk_ind("nfeltsign", ind[i], r1);
    1792         749 :   if (typ(x) != t_COL)
    1793             :   {
    1794             :     GEN s;
    1795          21 :     switch(gsigne(x))
    1796             :     {
    1797           7 :       case -1:s = gen_m1; break;
    1798           7 :       case 1: s = gen_1; break;
    1799           7 :       default: s = gen_0; break;
    1800             :     }
    1801          21 :     avma = av;
    1802          21 :     return typ(ind0) == t_INT? s: const_vec(l-1, s);
    1803             :   }
    1804         728 :   v = nfsign_arch(nf, x, ind);
    1805         728 :   if (typ(ind0) == t_INT) { avma = av; return v[1]? gen_m1: gen_1; }
    1806         721 :   settyp(v, t_VEC);
    1807         721 :   for (i = 1; i < l; i++) gel(v,i) = v[i]? gen_m1: gen_1;
    1808         721 :   return gerepileupto(av, v);
    1809             : 
    1810             : }
    1811             : 
    1812             : GEN
    1813         161 : nfeltembed(GEN nf, GEN x, GEN ind0)
    1814             : {
    1815         161 :   pari_sp av = avma;
    1816             :   long i, l, r1, r2;
    1817             :   GEN v, ind, cx, M;
    1818         161 :   nf = checknf(nf);
    1819         161 :   r1 = nf_get_r1(nf);
    1820         161 :   r2 = nf_get_r2(nf);
    1821         161 :   x = nf_to_scalar_or_basis(nf, x);
    1822         154 :   if (!ind0) ind0 = identity_perm(r1+r2);
    1823         154 :   switch(typ(ind0))
    1824             :   {
    1825             :     case t_INT: case t_VEC: case t_COL:
    1826          42 :       ind = gtovecsmall(ind0); break;
    1827             :     case t_VECSMALL:
    1828         112 :       ind = ind0; break;
    1829             :     default:
    1830           0 :       pari_err_TYPE("nfeltsign",ind0);
    1831             :       return NULL; /* LCOV_EXCL_LINE */
    1832             :   }
    1833         154 :   l = lg(ind);
    1834         154 :   for (i = 1; i < l; i++) chk_ind("nfeltembed", ind[i], r1+r2);
    1835         147 :   if (typ(x) != t_COL)
    1836             :   {
    1837          42 :     if (typ(ind0) != t_INT) x = const_vec(l-1, x);
    1838          42 :     return gerepilecopy(av, x);
    1839             :   }
    1840         105 :   x = Q_primitive_part(x, &cx); M = nf_get_M(nf);
    1841         105 :   v = cgetg(l, t_VEC);
    1842         273 :   for (i = 1; i < l; i++)
    1843             :   {
    1844         168 :     GEN t = nfembed_i(M, x, ind[i]);
    1845         168 :     if (cx) t = gmul(t, cx);
    1846         168 :     gel(v,i) = t;
    1847             :   }
    1848         105 :   if (typ(ind0) == t_INT) v = gel(v,1);
    1849         105 :   return gerepilecopy(av, v);
    1850             : }
    1851             : 
    1852             : /* return the vector of signs of x; the matrix of such if x is a vector
    1853             :  * of nf elements */
    1854             : GEN
    1855         539 : nfsign(GEN nf, GEN x)
    1856             : {
    1857             :   long i, l;
    1858             :   GEN archp, S;
    1859             : 
    1860         539 :   nf = checknf(nf);
    1861         539 :   archp = identity_perm( nf_get_r1(nf) );
    1862         539 :   if (typ(x) != t_VEC) return nfsign_arch(nf, x, archp);
    1863         182 :   l = lg(x); S = cgetg(l, t_MAT);
    1864         182 :   for (i=1; i<l; i++) gel(S,i) = nfsign_arch(nf, gel(x,i), archp);
    1865         182 :   return S;
    1866             : }
    1867             : 
    1868             : /* x integral elt, A integral ideal in HNF; reduce x mod A */
    1869             : static GEN
    1870      561875 : zk_modHNF(GEN x, GEN A)
    1871      561875 : { return (typ(x) == t_COL)?  ZC_hnfrem(x, A): modii(x, gcoeff(A,1,1)); }
    1872             : 
    1873             : /* given an element x in Z_K and an integral ideal y in HNF, coprime with x,
    1874             :    outputs an element inverse of x modulo y */
    1875             : GEN
    1876         126 : nfinvmodideal(GEN nf, GEN x, GEN y)
    1877             : {
    1878         126 :   pari_sp av = avma;
    1879         126 :   GEN a, yZ = gcoeff(y,1,1);
    1880             : 
    1881         126 :   if (is_pm1(yZ)) return gen_0;
    1882         126 :   x = nf_to_scalar_or_basis(nf, x);
    1883         126 :   if (typ(x) == t_INT) return gerepileupto(av, Fp_inv(x, yZ));
    1884             : 
    1885          56 :   a = hnfmerge_get_1(idealhnf_principal(nf,x), y);
    1886          56 :   if (!a) pari_err_INV("nfinvmodideal", x);
    1887          56 :   return gerepileupto(av, zk_modHNF(nfdiv(nf,a,x), y));
    1888             : }
    1889             : 
    1890             : static GEN
    1891      269583 : nfsqrmodideal(GEN nf, GEN x, GEN id)
    1892      269583 : { return zk_modHNF(nfsqri(nf,x), id); }
    1893             : static GEN
    1894      636156 : nfmulmodideal(GEN nf, GEN x, GEN y, GEN id)
    1895      636156 : { return x? zk_modHNF(nfmuli(nf,x,y), id): y; }
    1896             : /* assume x integral, k integer, A in HNF */
    1897             : GEN
    1898      396571 : nfpowmodideal(GEN nf,GEN x,GEN k,GEN A)
    1899             : {
    1900      396571 :   long s = signe(k);
    1901             :   pari_sp av;
    1902             :   GEN y;
    1903             : 
    1904      396571 :   if (!s) return gen_1;
    1905      396571 :   av = avma;
    1906      396571 :   x = nf_to_scalar_or_basis(nf, x);
    1907      396571 :   if (typ(x) != t_COL) return Fp_pow(x, k, gcoeff(A,1,1));
    1908      205351 :   if (s < 0) { x = nfinvmodideal(nf, x,A); k = absi(k); }
    1909      205351 :   for(y = NULL;;)
    1910             :   {
    1911      474934 :     if (mpodd(k)) y = nfmulmodideal(nf,y,x,A);
    1912      474934 :     k = shifti(k,-1); if (!signe(k)) break;
    1913      269583 :     x = nfsqrmodideal(nf,x,A);
    1914      269583 :   }
    1915      205351 :   return gerepileupto(av, y);
    1916             : }
    1917             : 
    1918             : /* a * g^n mod id */
    1919             : static GEN
    1920      353668 : nfmulpowmodideal(GEN nf, GEN a, GEN g, GEN n, GEN id)
    1921             : {
    1922      353668 :   return nfmulmodideal(nf, a, nfpowmodideal(nf,g,n,id), id);
    1923             : }
    1924             : 
    1925             : /* assume (num(g[i]), id) = 1 for all i. Return prod g[i]^e[i] mod id.
    1926             :  * EX = multiple of exponent of (O_K/id)^* */
    1927             : GEN
    1928      141362 : famat_to_nf_modideal_coprime(GEN nf, GEN g, GEN e, GEN id, GEN EX)
    1929             : {
    1930      141362 :   GEN EXo2, plus = NULL, minus = NULL, idZ = gcoeff(id,1,1);
    1931      141362 :   long i, lx = lg(g);
    1932             : 
    1933      141362 :   if (is_pm1(idZ)) return gen_1; /* id = Z_K */
    1934      141362 :   EXo2 = (expi(EX) > 10)? shifti(EX,-1): NULL;
    1935      731743 :   for (i = 1; i < lx; i++)
    1936             :   {
    1937      590381 :     GEN h, n = centermodii(gel(e,i), EX, EXo2);
    1938      590381 :     long sn = signe(n);
    1939      590381 :     if (!sn) continue;
    1940             : 
    1941      267058 :     h = nf_to_scalar_or_basis(nf, gel(g,i));
    1942      267058 :     switch(typ(h))
    1943             :     {
    1944      159664 :       case t_INT: break;
    1945             :       case t_FRAC:
    1946           0 :         h = Fp_div(gel(h,1), gel(h,2), idZ); break;
    1947             :       default:
    1948             :       {
    1949             :         GEN dh;
    1950      107394 :         h = Q_remove_denom(h, &dh);
    1951      107394 :         if (dh) h = FpC_Fp_mul(h, Fp_inv(dh,idZ), idZ);
    1952             :       }
    1953             :     }
    1954      267058 :     if (sn > 0)
    1955      265840 :       plus = nfmulpowmodideal(nf, plus, h, n, id);
    1956             :     else /* sn < 0 */
    1957        1218 :       minus = nfmulpowmodideal(nf, minus, h, absi(n), id);
    1958             :   }
    1959      141362 :   if (minus) plus = nfmulmodideal(nf, plus, nfinvmodideal(nf,minus,id), id);
    1960      141362 :   return plus? plus: gen_1;
    1961             : }
    1962             : 
    1963             : /* given 2 integral ideals x, y in HNF s.t x | y | x^2, compute (1+x)/(1+y) in
    1964             :  * the form [[cyc],[gen], U], where U := ux^-1 as a pair [ZM, denom(U)] */
    1965             : static GEN
    1966       12971 : zidealij(GEN x, GEN y)
    1967             : {
    1968       12971 :   GEN U, G, cyc, xp = gcoeff(x,1,1), xi = hnf_invscale(x, xp);
    1969             :   long j, N;
    1970             : 
    1971             :   /* x^(-1) y = relations between the 1 + x_i (HNF) */
    1972       12971 :   cyc = ZM_snf_group(ZM_Z_divexact(ZM_mul(xi, y), xp), &U, &G);
    1973       12971 :   N = lg(cyc); G = ZM_mul(x,G); settyp(G, t_VEC); /* new generators */
    1974       61208 :   for (j=1; j<N; j++)
    1975             :   {
    1976       48237 :     GEN c = gel(G,j);
    1977       48237 :     gel(c,1) = addiu(gel(c,1), 1); /* 1 + g_j */
    1978       48237 :     if (ZV_isscalar(c)) gel(G,j) = gel(c,1);
    1979             :   }
    1980       12971 :   return mkvec4(cyc, G, ZM_mul(U,xi), xp);
    1981             : }
    1982             : 
    1983             : /* lg(x) > 1, x + 1; shallow */
    1984             : static GEN
    1985        2688 : ZC_add1(GEN x)
    1986             : {
    1987        2688 :   long i, l = lg(x);
    1988        2688 :   GEN y = cgetg(l, t_COL);
    1989        2688 :   for (i = 2; i < l; i++) gel(y,i) = gel(x,i);
    1990        2688 :   gel(y,1) = addiu(gel(x,1), 1); return y;
    1991             : }
    1992             : /* lg(x) > 1, x - 1; shallow */
    1993             : static GEN
    1994        1540 : ZC_sub1(GEN x)
    1995             : {
    1996        1540 :   long i, l = lg(x);
    1997        1540 :   GEN y = cgetg(l, t_COL);
    1998        1540 :   for (i = 2; i < l; i++) gel(y,i) = gel(x,i);
    1999        1540 :   gel(y,1) = subiu(gel(x,1), 1); return y;
    2000             : }
    2001             : 
    2002             : /* x,y are t_INT or ZC */
    2003             : static GEN
    2004           0 : zkadd(GEN x, GEN y)
    2005             : {
    2006           0 :   long tx = typ(x);
    2007           0 :   if (tx == typ(y))
    2008           0 :     return tx == t_INT? addii(x,y): ZC_add(x,y);
    2009             :   else
    2010           0 :     return tx == t_INT? ZC_Z_add(y,x): ZC_Z_add(x,y);
    2011             : }
    2012             : /* x a t_INT or ZC, x+1; shallow */
    2013             : static GEN
    2014        3192 : zkadd1(GEN x)
    2015             : {
    2016        3192 :   long tx = typ(x);
    2017        3192 :   return tx == t_INT? addiu(x,1): ZC_add1(x);
    2018             : }
    2019             : /* x a t_INT or ZC, x-1; shallow */
    2020             : static GEN
    2021        3192 : zksub1(GEN x)
    2022             : {
    2023        3192 :   long tx = typ(x);
    2024        3192 :   return tx == t_INT? subiu(x,1): ZC_sub1(x);
    2025             : }
    2026             : /* x,y are t_INT or ZC; x - y */
    2027             : static GEN
    2028           0 : zksub(GEN x, GEN y)
    2029             : {
    2030           0 :   long tx = typ(x), ty = typ(y);
    2031           0 :   if (tx == ty)
    2032           0 :     return tx == t_INT? subii(x,y): ZC_sub(x,y);
    2033             :   else
    2034           0 :     return tx == t_INT? Z_ZC_sub(x,y): ZC_Z_sub(x,y);
    2035             : }
    2036             : /* x is t_INT or ZM (mult. map), y is t_INT or ZC; x * y */
    2037             : static GEN
    2038        3192 : zkmul(GEN x, GEN y)
    2039             : {
    2040        3192 :   long tx = typ(x), ty = typ(y);
    2041        3192 :   if (ty == t_INT)
    2042        1652 :     return tx == t_INT? mulii(x,y): ZC_Z_mul(gel(x,1),y);
    2043             :   else
    2044        1540 :     return tx == t_INT? ZC_Z_mul(y,x): ZM_ZC_mul(x,y);
    2045             : }
    2046             : 
    2047             : /* (U,V) = 1 coprime ideals. Want z = x mod U, = y mod V; namely
    2048             :  * z =vx + uy = v(x-y) + y, where u + v = 1, u in U, v in V.
    2049             :  * zkc = [v, UV], v a t_INT or ZM (mult. by v map), UV a ZM (ideal in HNF);
    2050             :  * shallow */
    2051             : GEN
    2052           0 : zkchinese(GEN zkc, GEN x, GEN y)
    2053             : {
    2054           0 :   GEN v = gel(zkc,1), UV = gel(zkc,2), z = zkadd(zkmul(v, zksub(x,y)), y);
    2055           0 :   return zk_modHNF(z, UV);
    2056             : }
    2057             : /* special case z = x mod U, = 1 mod V; shallow */
    2058             : GEN
    2059        3192 : zkchinese1(GEN zkc, GEN x)
    2060             : {
    2061        3192 :   GEN v = gel(zkc,1), UV = gel(zkc,2), z = zkadd1(zkmul(v, zksub1(x)));
    2062        3192 :   return (typ(z) == t_INT)? z: ZC_hnfrem(z, UV);
    2063             : }
    2064             : static GEN
    2065        2457 : zkVchinese1(GEN zkc, GEN v)
    2066             : {
    2067             :   long i, ly;
    2068        2457 :   GEN y = cgetg_copy(v, &ly);
    2069        2457 :   for (i=1; i<ly; i++) gel(y,i) = zkchinese1(zkc, gel(v,i));
    2070        2457 :   return y;
    2071             : }
    2072             : 
    2073             : /* prepare to solve z = x (mod A), z = y mod (B) [zkchinese or zkchinese1] */
    2074             : GEN
    2075        2198 : zkchineseinit(GEN nf, GEN A, GEN B, GEN AB)
    2076             : {
    2077             :   GEN v;
    2078        2198 :   nf = checknf(nf);
    2079        2198 :   v = idealaddtoone_i(nf, A, B);
    2080        2198 :   return mkvec2(zk_scalar_or_multable(nf,v), AB);
    2081             : }
    2082             : /* prepare to solve z = x (mod A), z = 1 mod (B)
    2083             :  * and then         z = 1 (mod A), z = y mod (B) [zkchinese1 twice] */
    2084             : static GEN
    2085         259 : zkchinese1init2(GEN nf, GEN A, GEN B, GEN AB)
    2086             : {
    2087         259 :   GEN zkc = zkchineseinit(nf, A, B, AB);
    2088         259 :   GEN mv = gel(zkc,1), mu;
    2089         259 :   if (typ(mv) == t_INT) return mkvec2(zkc, mkvec2(subui(1,mv),AB));
    2090         238 :   mu = RgM_Rg_add_shallow(ZM_neg(mv), gen_1);
    2091         238 :   return mkvec2(mkvec2(mv,AB), mkvec2(mu,AB));
    2092             : }
    2093             : 
    2094             : static GEN
    2095      277128 : apply_U(GEN L, GEN a)
    2096             : {
    2097      277128 :   GEN e, U = gel(L,3), dU = gel(L,4);
    2098      277128 :   if (typ(a) == t_INT)
    2099       84718 :     e = ZC_Z_mul(gel(U,1), subiu(a, 1));
    2100             :   else
    2101             :   { /* t_COL */
    2102      192410 :     GEN t = gel(a,1);
    2103      192410 :     gel(a,1) = subiu(gel(a,1), 1); /* a -= 1 */
    2104      192410 :     e = ZM_ZC_mul(U, a);
    2105      192410 :     gel(a,1) = t; /* restore */
    2106             :   }
    2107      277128 :   return gdiv(e, dU);
    2108             : }
    2109             : 
    2110             : /* vectors of [[cyc],[g],U.X^-1]. Assume k > 1. */
    2111             : static GEN
    2112        8498 : principal_units(GEN nf, GEN pr, long k, GEN prk)
    2113             : {
    2114             :   GEN list, prb;
    2115        8498 :   ulong mask = quadratic_prec_mask(k);
    2116        8498 :   long a = 1;
    2117             : 
    2118        8498 :   if (DEBUGLEVEL>3) err_printf("treating pr^%ld, pr = %Ps\n",k,pr);
    2119        8498 :   prb = idealhnf_two(nf,pr);
    2120        8498 :   list = vectrunc_init(k);
    2121       29967 :   while (mask > 1)
    2122             :   {
    2123       12971 :     GEN pra = prb;
    2124       12971 :     long b = a << 1;
    2125             : 
    2126       12971 :     if (mask & 1) b--;
    2127       12971 :     mask >>= 1;
    2128             :     /* compute 1 + pr^a / 1 + pr^b, 2a <= b */
    2129       12971 :     if(DEBUGLEVEL>3) err_printf("  treating a = %ld, b = %ld\n",a,b);
    2130       12971 :     prb = (b >= k)? prk: idealpows(nf,pr,b);
    2131       12971 :     vectrunc_append(list, zidealij(pra, prb));
    2132       12971 :     a = b;
    2133             :   }
    2134        8498 :   return list;
    2135             : }
    2136             : /* a = 1 mod (pr) return log(a) on local-gens of 1+pr/1+pr^k */
    2137             : static GEN
    2138      173283 : log_prk1(GEN nf, GEN a, long nh, GEN L2, GEN prk)
    2139             : {
    2140      173283 :   GEN y = cgetg(nh+1, t_COL);
    2141      173283 :   long j, iy, c = lg(L2)-1;
    2142      450404 :   for (j = iy = 1; j <= c; j++)
    2143             :   {
    2144      277128 :     GEN L = gel(L2,j), cyc = gel(L,1), gen = gel(L,2), E = apply_U(L,a);
    2145      277128 :     long i, nc = lg(cyc)-1;
    2146      277128 :     int last = (j == c);
    2147     1076260 :     for (i = 1; i <= nc; i++, iy++)
    2148             :     {
    2149      799139 :       GEN t, e = gel(E,i);
    2150      799139 :       if (typ(e) != t_INT) pari_err_COPRIME("zlog_prk1", a, prk);
    2151      799132 :       t = Fp_neg(e, gel(cyc,i));
    2152      799132 :       gel(y,iy) = negi(t);
    2153      799132 :       if (!last && signe(t)) a = nfmulpowmodideal(nf, a, gel(gen,i), t, prk);
    2154             :     }
    2155             :   }
    2156      173276 :   return y;
    2157             : }
    2158             : static GEN
    2159        3822 : principal_units_relations(GEN nf, GEN L2, GEN prk, long nh)
    2160             : {
    2161        3822 :   GEN h = cgetg(nh+1,t_MAT);
    2162        3822 :   long ih, j, c = lg(L2)-1;
    2163       12117 :   for (j = ih = 1; j <= c; j++)
    2164             :   {
    2165        8295 :     GEN L = gel(L2,j), F = gel(L,1), G = gel(L,2);
    2166        8295 :     long k, lG = lg(G);
    2167       42791 :     for (k = 1; k < lG; k++,ih++)
    2168             :     { /* log(g^f) mod pr^e */
    2169       34496 :       GEN a = nfpowmodideal(nf,gel(G,k),gel(F,k),prk);
    2170       34496 :       gel(h,ih) = ZC_neg(log_prk1(nf, a, nh, L2, prk));
    2171       34496 :       gcoeff(h,ih,ih) = gel(F,k);
    2172             :     }
    2173             :   }
    2174        3822 :   return h;
    2175             : }
    2176             : /* e > 1; multiplicative group (1 + pr) / (1 + pr^k), prk = pr^k or NULL */
    2177             : static GEN
    2178        8498 : idealprincipalunits_i(GEN nf, GEN pr, long k, GEN *pU)
    2179             : {
    2180        8498 :   GEN cyc, gen, L2, prk = idealpows(nf, pr, k);
    2181             : 
    2182        8498 :   L2 = principal_units(nf, pr, k, prk);
    2183        8498 :   if (k == 2)
    2184             :   {
    2185        4676 :     GEN L = gel(L2,1);
    2186        4676 :     cyc = gel(L,1);
    2187        4676 :     gen = gel(L,2);
    2188        4676 :     if (pU) *pU = matid(lg(gen)-1);
    2189             :   }
    2190             :   else
    2191             :   {
    2192        3822 :     long c = lg(L2), j;
    2193        3822 :     GEN EX, h, Ui, vg = cgetg(c, t_VEC);
    2194        3822 :     for (j = 1; j < c; j++) gel(vg, j) = gmael(L2,j,2);
    2195        3822 :     vg = shallowconcat1(vg);
    2196        3822 :     h = principal_units_relations(nf, L2, prk, lg(vg)-1);
    2197        3822 :     h = ZM_hnfall_i(h, NULL, 0);
    2198        3822 :     cyc = ZM_snf_group(h, pU, &Ui);
    2199        3822 :     c = lg(Ui); gen = cgetg(c, t_VEC); EX = gel(cyc,1);
    2200       27055 :     for (j = 1; j < c; j++)
    2201       23233 :       gel(gen,j) = famat_to_nf_modideal_coprime(nf, vg, gel(Ui,j), prk, EX);
    2202             :   }
    2203        8498 :   return mkvec4(cyc, gen, prk, L2);
    2204             : }
    2205             : GEN
    2206         112 : idealprincipalunits(GEN nf, GEN pr, long k)
    2207             : {
    2208             :   pari_sp av;
    2209             :   GEN v;
    2210         112 :   nf = checknf(nf);
    2211         112 :   if (k == 1) { checkprid(pr); retmkvec3(gen_1,cgetg(1,t_VEC),cgetg(1,t_VEC)); }
    2212         105 :   av = avma; v = idealprincipalunits_i(nf, pr, k, NULL);
    2213         105 :   return gerepilecopy(av, mkvec3(powiu(pr_norm(pr), k-1), gel(v,1), gel(v,2)));
    2214             : }
    2215             : 
    2216             : /* Given an ideal pr^k dividing an integral ideal x (in HNF form) compute
    2217             :  * an 'sprk', the structure of G = (Z_K/pr^k)^* [ x = NULL for x = pr^k ]
    2218             :  * Return a vector with at least 4 components [cyc],[gen],[HNF pr^k,pr,k],ff,
    2219             :  * where
    2220             :  * cyc : type of G as abelian group (SNF)
    2221             :  * gen : generators of G, coprime to x
    2222             :  * pr^k: in HNF
    2223             :  * ff  : data for log_g in (Z_K/pr)^*
    2224             :  * Two extra components are present iff k > 1: L2, U
    2225             :  * L2  : list of data structures to compute local DL in (Z_K/pr)^*,
    2226             :  *       and 1 + pr^a/ 1 + pr^b for various a < b <= min(2a, k)
    2227             :  * U   : base change matrices to convert a vector of local DL to DL wrt gen */
    2228             : static GEN
    2229       11956 : sprkinit(GEN nf, GEN pr, GEN gk, GEN x)
    2230             : {
    2231             :   GEN T, p, modpr, cyc, gen, g, g0, ord0, A, prk, U, L2;
    2232       11956 :   long k = itos(gk), f = pr_get_f(pr);
    2233             : 
    2234       11956 :   if(DEBUGLEVEL>3) err_printf("treating pr^%ld, pr = %Ps\n",k,pr);
    2235       11956 :   modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    2236             :   /* (Z_K / pr)^* */
    2237       11956 :   if (f == 1)
    2238             :   {
    2239        5145 :     g0 = g = pgener_Fp(p);
    2240        5145 :     ord0 = get_arith_ZZM(subiu(p,1));
    2241             :   }
    2242             :   else
    2243             :   {
    2244        6811 :     g0 = g = gener_FpXQ(T,p, &ord0);
    2245        6811 :     g = Fq_to_nf(g, modpr);
    2246        6811 :     if (typ(g) == t_POL) g = poltobasis(nf, g);
    2247             :   }
    2248       11956 :   A = gel(ord0, 1); /* Norm(pr)-1 */
    2249       11956 :   if (k == 1)
    2250             :   {
    2251        3563 :     cyc = mkvec(A);
    2252        3563 :     gen = mkvec(g);
    2253        3563 :     prk = idealhnf_two(nf,pr);
    2254        3563 :     L2 = U = NULL;
    2255             :   }
    2256             :   else
    2257             :   { /* local-gens of (1 + pr)/(1 + pr^k) = SNF-gens * U */
    2258             :     GEN AB, B, u, v, w;
    2259             :     long j, l;
    2260        8393 :     w = idealprincipalunits_i(nf, pr, k, &U);
    2261             :     /* incorporate (Z_K/pr)^*, order A coprime to B = expo(1+pr/1+pr^k)*/
    2262        8393 :     cyc = leafcopy(gel(w,1)); B = gel(cyc,1); AB = mulii(A,B);
    2263        8393 :     gen = leafcopy(gel(w,2));
    2264        8393 :     prk = gel(w,3);
    2265        8393 :     g = nfpowmodideal(nf, g, B, prk);
    2266        8393 :     g0 = Fq_pow(g0, modii(B,A), T, p); /* update primitive root */
    2267        8393 :     L2 = mkvec3(A, g, gel(w,4));
    2268        8393 :     gel(cyc,1) = AB;
    2269        8393 :     gel(gen,1) = nfmulmodideal(nf, gel(gen,1), g, prk);
    2270        8393 :     u = mulii(Fp_inv(A,B), A);
    2271        8393 :     v = subui(1, u); l = lg(U);
    2272        8393 :     for (j = 1; j < l; j++) gcoeff(U,1,j) = Fp_mul(u, gcoeff(U,1,j), AB);
    2273        8393 :     U = mkvec2(Rg_col_ei(v, lg(gen)-1, 1), U);
    2274             :   }
    2275             :   /* local-gens of (Z_K/pr^k)^* = SNF-gens * U */
    2276       11956 :   if (x)
    2277             :   {
    2278        1939 :     GEN uv = zkchineseinit(nf, idealdivpowprime(nf,x,pr,gk), prk, x);
    2279        1939 :     gen = zkVchinese1(uv, gen);
    2280             :   }
    2281       11956 :   return mkvecn(U? 6: 4, cyc, gen, prk, mkvec3(modpr,g0,ord0), L2, U);
    2282             : }
    2283             : static GEN
    2284      253955 : sprk_get_cyc(GEN s) { return gel(s,1); }
    2285             : static GEN
    2286      104157 : sprk_get_expo(GEN s)
    2287             : {
    2288      104157 :   GEN cyc = sprk_get_cyc(s);
    2289      104157 :   return lg(cyc) == 1? gen_1: gel(cyc, 1);
    2290             : }
    2291             : static GEN
    2292        5894 : sprk_get_gen(GEN s) { return gel(s,2); }
    2293             : static GEN
    2294      242944 : sprk_get_prk(GEN s) { return gel(s,3); }
    2295             : static GEN
    2296      267729 : sprk_get_ff(GEN s) { return gel(s,4); }
    2297             : static GEN
    2298      109421 : sprk_get_pr(GEN s) { GEN ff = gel(s,4); return modpr_get_pr(gel(ff,1)); }
    2299             : /* A = Npr-1, <g> = (Z_K/pr)^*, L2 to 1 + pr / 1 + pr^k */
    2300             : static void
    2301      141825 : sprk_get_L2(GEN s, GEN *A, GEN *g, GEN *L2)
    2302      141825 : { GEN v = gel(s,5); *A = gel(v,1); *g = gel(v,2); *L2 = gel(v,3); }
    2303             : static void
    2304      138787 : sprk_get_U2(GEN s, GEN *U1, GEN *U2)
    2305      138787 : { GEN v = gel(s,6); *U1 = gel(v,1); *U2 = gel(v,2); }
    2306             : static int
    2307      267729 : sprk_is_prime(GEN s) { return lg(s) == 5; }
    2308             : 
    2309             : static GEN
    2310      104157 : famat_zlog_pr(GEN nf, GEN g, GEN e, GEN sprk)
    2311             : {
    2312      104157 :   GEN pr = sprk_get_pr(sprk);
    2313      104157 :   GEN prk = sprk_get_prk(sprk);
    2314      104157 :   GEN x = famat_makecoprime(nf, g, e, pr, prk, sprk_get_expo(sprk));
    2315      104157 :   return zlog_pr(nf, x, sprk);
    2316             : }
    2317             : /* log_g(a) in (Z_K/pr)^* */
    2318             : static GEN
    2319      267729 : nf_log(GEN nf, GEN a, GEN ff)
    2320             : {
    2321      267729 :   GEN pr = gel(ff,1), g = gel(ff,2), ord = gel(ff,3);
    2322      267729 :   GEN T,p, modpr = nf_to_Fq_init(nf, &pr, &T, &p);
    2323      267729 :   return Fq_log(nf_to_Fq(nf,a,modpr), g, ord, T, p);
    2324             : }
    2325             : /* a in Z_K (t_COL or t_INT), pr prime ideal, sprk = sprkinit(nf,pr,k,x).
    2326             :  * return log(a) on SNF-generators of (Z_K/pr^k)^**/
    2327             : GEN
    2328      268765 : zlog_pr(GEN nf, GEN a, GEN sprk)
    2329             : {
    2330             :   GEN e, prk, A, g, L2, U1, U2, y;
    2331             : 
    2332      268765 :   if (typ(a) == t_MAT) return famat_zlog_pr(nf, gel(a,1), gel(a,2), sprk);
    2333             : 
    2334      267729 :   e = nf_log(nf, a, sprk_get_ff(sprk));
    2335      267729 :   if (sprk_is_prime(sprk)) return mkcol(e); /* k = 1 */
    2336      138787 :   prk = sprk_get_prk(sprk);
    2337      138787 :   sprk_get_L2(sprk, &A,&g,&L2);
    2338      138787 :   if (signe(e))
    2339             :   {
    2340       40068 :     e = Fp_neg(e, A);
    2341       40068 :     a = nfmulpowmodideal(nf, a, g, e, prk);
    2342             :   }
    2343      138787 :   sprk_get_U2(sprk, &U1,&U2);
    2344      138787 :   y = ZM_ZC_mul(U2, log_prk1(nf, a, lg(U2)-1, L2, prk));
    2345      138780 :   if (signe(e)) y = ZC_sub(y, ZC_Z_mul(U1,e));
    2346      138780 :   return vecmodii(y, sprk_get_cyc(sprk));
    2347             : }
    2348             : GEN
    2349        6062 : zlog_pr_init(GEN nf, GEN pr, long k) { return sprkinit(nf,pr,utoipos(k),NULL);}
    2350             : GEN
    2351         378 : vzlog_pr(GEN nf, GEN v, GEN sprk)
    2352             : {
    2353         378 :   long l = lg(v), i;
    2354         378 :   GEN w = cgetg(l, t_MAT);
    2355         378 :   for (i = 1; i < l; i++) gel(w,i) = zlog_pr(nf, gel(v,i), sprk);
    2356         378 :   return w;
    2357             : }
    2358             : 
    2359             : static GEN
    2360      107839 : famat_zlog(GEN nf, GEN fa, GEN sgn, zlog_S *S)
    2361             : {
    2362      107839 :   long i, n0, n = lg(S->U)-1;
    2363             :   GEN g, e, y;
    2364      107839 :   if (lg(fa) == 1) return zerocol(n);
    2365      107839 :   g = gel(fa,1);
    2366      107839 :   e = gel(fa,2);
    2367      107839 :   y = cgetg(n+1, t_COL);
    2368      107839 :   n0 = lg(S->sprk)-1; /* n0 = n (trivial arch. part), or n-1 */
    2369      107839 :   for (i=1; i <= n0; i++) gel(y,i) = famat_zlog_pr(nf, g, e, gel(S->sprk,i));
    2370      107839 :   if (n0 != n)
    2371             :   {
    2372       91480 :     if (!sgn) sgn = nfsign_arch(nf, fa, S->archp);
    2373       91480 :     gel(y,n) = Flc_to_ZC(sgn);
    2374             :   }
    2375      107839 :   return y;
    2376             : }
    2377             : 
    2378             : /* assume that cyclic factors are normalized, in particular != [1] */
    2379             : static GEN
    2380        5684 : split_U(GEN U, GEN Sprk)
    2381             : {
    2382        5684 :   long t = 0, k, n, l = lg(Sprk);
    2383        5684 :   GEN vU = cgetg(l+1, t_VEC);
    2384       10808 :   for (k = 1; k < l; k++)
    2385             :   {
    2386        5124 :     n = lg(sprk_get_cyc(gel(Sprk,k))) - 1; /* > 0 */
    2387        5124 :     gel(vU,k) = vecslice(U, t+1, t+n);
    2388        5124 :     t += n;
    2389             :   }
    2390             :   /* t+1 .. lg(U)-1 */
    2391        5684 :   n = lg(U) - t - 1; /* can be 0 */
    2392        5684 :   if (!n) setlg(vU,l); else gel(vU,l) = vecslice(U, t+1, t+n);
    2393        5684 :   return vU;
    2394             : }
    2395             : 
    2396             : void
    2397      230621 : init_zlog(zlog_S *S, GEN bid)
    2398             : {
    2399      230621 :   GEN fa2 = bid_get_fact2(bid);
    2400      230621 :   S->U = bid_get_U(bid);
    2401      230621 :   S->hU = lg(bid_get_cyc(bid))-1;
    2402      230621 :   S->archp = bid_get_archp(bid);
    2403      230621 :   S->sprk = bid_get_sprk(bid);
    2404      230621 :   S->bid = bid;
    2405      230621 :   S->P = gel(fa2,1);
    2406      230621 :   S->k = gel(fa2,2);
    2407      230621 :   S->no2 = lg(S->P) == lg(gel(bid_get_fact(bid),1));
    2408      230621 : }
    2409             : 
    2410             : /* a a t_FRAC/t_INT, reduce mod bid */
    2411             : static GEN
    2412           7 : Q_mod_bid(GEN bid, GEN a)
    2413             : {
    2414           7 :   GEN xZ = gcoeff(bid_get_ideal(bid),1,1);
    2415           7 :   GEN b = Rg_to_Fp(a, xZ);
    2416           7 :   if (gsigne(a) < 0) b = subii(b, xZ);
    2417           7 :   return b;
    2418             : }
    2419             : /* Return decomposition of a on the CRT generators blocks attached to the
    2420             :  * S->sprk and sarch; sgn = sign(a, S->arch), NULL if unknown */
    2421             : static GEN
    2422      159795 : zlog(GEN nf, GEN a, GEN sgn, zlog_S *S)
    2423             : {
    2424             :   long k, l;
    2425             :   GEN y;
    2426      159795 :   a = nf_to_scalar_or_basis(nf, a);
    2427      159795 :   switch(typ(a))
    2428             :   {
    2429       15743 :     case t_INT: break;
    2430           7 :     case t_FRAC: a = Q_mod_bid(S->bid, a); break;
    2431             :     default: /* case t_COL: */
    2432             :     {
    2433             :       GEN den;
    2434      144045 :       check_nfelt(a, &den);
    2435      144045 :       if (den)
    2436             :       {
    2437       41465 :         a = Q_muli_to_int(a, den);
    2438       41465 :         a = mkmat2(mkcol2(a, den), mkcol2(gen_1, gen_m1));
    2439       41465 :         return famat_zlog(nf, a, sgn, S);
    2440             :       }
    2441             :     }
    2442             :   }
    2443      118330 :   if (sgn)
    2444        9058 :     sgn = (lg(sgn) == 1)? NULL: leafcopy(sgn);
    2445             :   else
    2446      109272 :     sgn = (lg(S->archp) == 1)? NULL: nfsign_arch(nf, a, S->archp);
    2447      118330 :   l = lg(S->sprk);
    2448      118330 :   y = cgetg(sgn? l+1: l, t_COL);
    2449      262246 :   for (k = 1; k < l; k++)
    2450             :   {
    2451      143923 :     GEN sprk = gel(S->sprk,k);
    2452      143923 :     gel(y,k) = zlog_pr(nf, a, sprk);
    2453             :   }
    2454      118323 :   if (sgn) gel(y,l) = Flc_to_ZC(sgn);
    2455      118323 :   return y;
    2456             : }
    2457             : 
    2458             : /* true nf */
    2459             : GEN
    2460        2478 : pr_basis_perm(GEN nf, GEN pr)
    2461             : {
    2462        2478 :   long f = pr_get_f(pr);
    2463             :   GEN perm;
    2464        2478 :   if (f == nf_get_degree(nf)) return identity_perm(f);
    2465        1316 :   perm = cgetg(f+1, t_VECSMALL);
    2466        1316 :   perm[1] = 1;
    2467        1316 :   if (f > 1)
    2468             :   {
    2469         399 :     GEN H = idealhnf_two(nf,pr);
    2470             :     long i, k;
    2471        1463 :     for (i = k = 2; k <= f; i++)
    2472             :     {
    2473        1064 :       if (is_pm1(gcoeff(H,i,i))) continue;
    2474         840 :       perm[k++] = i;
    2475             :     }
    2476             :   }
    2477        1316 :   return perm;
    2478             : }
    2479             : 
    2480             : /* \sum U[i]*y[i], U[i] ZM, y[i] ZC. We allow lg(y) > lg(U). */
    2481             : static GEN
    2482      226162 : ZMV_ZCV_mul(GEN U, GEN y)
    2483             : {
    2484      226162 :   long i, l = lg(U);
    2485      226162 :   GEN z = NULL;
    2486      226162 :   if (l == 1) return cgetg(1,t_COL);
    2487      635451 :   for (i = 1; i < l; i++)
    2488             :   {
    2489      409289 :     GEN u = ZM_ZC_mul(gel(U,i), gel(y,i));
    2490      409289 :     z = z? ZC_add(z, u): u;
    2491             :   }
    2492      226162 :   return z;
    2493             : }
    2494             : /* A * (U[1], ..., U[d] */
    2495             : static GEN
    2496         518 : ZM_ZMV_mul(GEN A, GEN U)
    2497             : {
    2498             :   long i, l;
    2499         518 :   GEN V = cgetg_copy(U,&l);
    2500         518 :   for (i = 1; i < l; i++) gel(V,i) = ZM_mul(A,gel(U,i));
    2501         518 :   return V;
    2502             : }
    2503             : 
    2504             : /* Log on bid.gen of generators of P_{1,I pr^{e-1}} / P_{1,I pr^e} (I,pr) = 1,
    2505             :  * defined implicitly via CRT. 'ind' is the index of pr in modulus
    2506             :  * factorization */
    2507             : GEN
    2508       10346 : log_gen_pr(zlog_S *S, long ind, GEN nf, long e)
    2509             : {
    2510       10346 :   GEN A, sprk = gel(S->sprk,ind), Uind = gel(S->U, ind);
    2511             : 
    2512       10346 :   if (e == 1) retmkmat( gel(Uind,1) );
    2513             :   else
    2514             :   {
    2515        5264 :     GEN G, pr = sprk_get_pr(sprk);
    2516             :     long i, l;
    2517        5264 :     if (e == 2)
    2518             :     {
    2519        3038 :       GEN A, g, L, L2; sprk_get_L2(sprk,&A,&g,&L2); L = gel(L2,1);
    2520        3038 :       G = gel(L,2); l = lg(G);
    2521             :     }
    2522             :     else
    2523             :     {
    2524        2226 :       GEN perm = pr_basis_perm(nf,pr), PI = nfpow_u(nf, pr_get_gen(pr), e-1);
    2525        2226 :       l = lg(perm);
    2526        2226 :       G = cgetg(l, t_VEC);
    2527        2226 :       if (typ(PI) == t_INT)
    2528             :       { /* zk_ei_mul doesn't allow t_INT */
    2529        1155 :         long N = nf_get_degree(nf);
    2530        1155 :         gel(G,1) = addiu(PI,1);
    2531        1785 :         for (i = 2; i < l; i++)
    2532             :         {
    2533         630 :           GEN z = col_ei(N, 1);
    2534         630 :           gel(G,i) = z; gel(z, perm[i]) = PI;
    2535             :         }
    2536             :       }
    2537             :       else
    2538             :       {
    2539        1071 :         gel(G,1) = nfadd(nf, gen_1, PI);
    2540        1281 :         for (i = 2; i < l; i++)
    2541         210 :           gel(G,i) = nfadd(nf, gen_1, zk_ei_mul(nf, PI, perm[i]));
    2542             :       }
    2543             :     }
    2544        5264 :     A = cgetg(l, t_MAT);
    2545       12082 :     for (i = 1; i < l; i++)
    2546        6818 :       gel(A,i) = ZM_ZC_mul(Uind, zlog_pr(nf, gel(G,i), sprk));
    2547        5264 :     return A;
    2548             :   }
    2549             : }
    2550             : /* Log on bid.gen of generator of P_{1,f} / P_{1,f v[index]}
    2551             :  * v = vector of r1 real places */
    2552             : GEN
    2553        6230 : log_gen_arch(zlog_S *S, long index)
    2554             : {
    2555        6230 :   GEN U = gel(S->U, lg(S->U)-1);
    2556        6230 :   return gel(U, index);
    2557             : }
    2558             : 
    2559             : /* compute bid.clgp: [h,cyc] or [h,cyc,gen] */
    2560             : static GEN
    2561        6741 : bid_grp(GEN nf, GEN U, GEN cyc, GEN g, GEN F, GEN sarch)
    2562             : {
    2563        6741 :   GEN G, h = ZV_prod(cyc);
    2564             :   long c;
    2565        6741 :   if (!U) return mkvec2(h,cyc);
    2566        6496 :   c = lg(U);
    2567        6496 :   G = cgetg(c,t_VEC);
    2568        6496 :   if (c > 1)
    2569             :   {
    2570        5544 :     GEN U0, Uoo, EX = gel(cyc,1); /* exponent of bid */
    2571        5544 :     long i, hU = nbrows(U), nba = lg(sarch_get_cyc(sarch))-1; /* #f_oo */
    2572        5544 :     if (!nba) { U0 = U; Uoo = NULL; }
    2573        3080 :     else if (nba == hU) { U0 = NULL; Uoo = U; }
    2574             :     else
    2575             :     {
    2576        2401 :       U0 = rowslice(U, 1, hU-nba);
    2577        2401 :       Uoo = rowslice(U, hU-nba+1, hU);
    2578             :     }
    2579       17633 :     for (i = 1; i < c; i++)
    2580             :     {
    2581       12089 :       GEN t = gen_1;
    2582       12089 :       if (U0) t = famat_to_nf_modideal_coprime(nf, g, gel(U0,i), F, EX);
    2583       12089 :       if (Uoo) t = set_sign_mod_divisor(nf, ZV_to_Flv(gel(Uoo,i),2), t, sarch);
    2584       12089 :       gel(G,i) = t;
    2585             :     }
    2586             :   }
    2587        6496 :   return mkvec3(h, cyc, G);
    2588             : }
    2589             : 
    2590             : /* remove prime ideals of norm 2 with exponent 1 from factorization */
    2591             : static GEN
    2592        6426 : famat_strip2(GEN fa)
    2593             : {
    2594        6426 :   GEN P = gel(fa,1), E = gel(fa,2), Q, F;
    2595        6426 :   long l = lg(P), i, j;
    2596        6426 :   Q = cgetg(l, t_COL);
    2597        6426 :   F = cgetg(l, t_COL);
    2598       12362 :   for (i = j = 1; i < l; i++)
    2599             :   {
    2600        5936 :     GEN pr = gel(P,i), e = gel(E,i);
    2601        5936 :     if (!absequaliu(pr_get_p(pr), 2) || itou(e) != 1 || pr_get_f(pr) != 1)
    2602             :     {
    2603        5894 :       gel(Q,j) = pr;
    2604        5894 :       gel(F,j) = e; j++;
    2605             :     }
    2606             :   }
    2607        6426 :   setlg(Q,j);
    2608        6426 :   setlg(F,j); return mkmat2(Q,F);
    2609             : }
    2610             : 
    2611             : /* Compute [[ideal,arch], [h,[cyc],[gen]], idealfact, [liste], U]
    2612             :    flag may include nf_GEN | nf_INIT */
    2613             : static GEN
    2614        6447 : Idealstar_i(GEN nf, GEN ideal, long flag)
    2615             : {
    2616             :   long i, k, nbp, R1;
    2617        6447 :   GEN y, cyc, U, u1 = NULL, fa, fa2, sprk, x, arch, archp, E, P, sarch, gen;
    2618             : 
    2619        6447 :   nf = checknf(nf);
    2620        6447 :   R1 = nf_get_r1(nf);
    2621        6447 :   if (typ(ideal) == t_VEC && lg(ideal) == 3)
    2622             :   {
    2623        4312 :     arch = gel(ideal,2);
    2624        4312 :     ideal= gel(ideal,1);
    2625        4312 :     switch(typ(arch))
    2626             :     {
    2627             :       case t_VEC:
    2628        4277 :         if (lg(arch) != R1+1)
    2629           0 :           pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
    2630        4277 :         archp = vec01_to_indices(arch);
    2631        4277 :         break;
    2632             :       case t_VECSMALL:
    2633          35 :         archp = arch;
    2634          35 :         k = lg(archp)-1;
    2635          35 :         if (k && archp[k] > R1)
    2636           7 :           pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
    2637          28 :         arch = indices_to_vec01(archp, R1);
    2638          28 :         break;
    2639             :       default:
    2640           0 :         pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
    2641           0 :         return NULL;
    2642             :     }
    2643        4305 :   }
    2644             :   else
    2645             :   {
    2646        2135 :     arch = zerovec(R1);
    2647        2135 :     archp = cgetg(1, t_VECSMALL);
    2648             :   }
    2649        6440 :   if (is_nf_factor(ideal))
    2650             :   {
    2651         343 :     fa = ideal;
    2652         343 :     x = idealfactorback(nf, gel(fa,1), gel(fa,2), 0);
    2653             :   }
    2654             :   else
    2655             :   {
    2656        6097 :     fa = idealfactor(nf, ideal);
    2657        6090 :     x = ideal;
    2658             :   }
    2659        6433 :   if (typ(x) != t_MAT)  x = idealhnf_shallow(nf, x);
    2660        6433 :   if (lg(x) == 1) pari_err_DOMAIN("Idealstar", "ideal","=",gen_0,x);
    2661        6433 :   if (typ(gcoeff(x,1,1)) != t_INT)
    2662           7 :     pari_err_DOMAIN("Idealstar","denominator(ideal)", "!=",gen_1,x);
    2663        6426 :   sarch = nfarchstar(nf, x, archp);
    2664        6426 :   fa2 = famat_strip2(fa);
    2665        6426 :   P = gel(fa2,1);
    2666        6426 :   E = gel(fa2,2);
    2667        6426 :   nbp = lg(P)-1;
    2668        6426 :   sprk = cgetg(nbp+1,t_VEC);
    2669        6426 :   if (nbp)
    2670             :   {
    2671        4781 :     GEN t = (nbp==1)? NULL: x;
    2672        4781 :     cyc = cgetg(nbp+2,t_VEC);
    2673        4781 :     gen = cgetg(nbp+1,t_VEC);
    2674       10675 :     for (i = 1; i <= nbp; i++)
    2675             :     {
    2676        5894 :       GEN L = sprkinit(nf, gel(P,i), gel(E,i), t);
    2677        5894 :       gel(sprk,i) = L;
    2678        5894 :       gel(cyc,i) = sprk_get_cyc(L);
    2679             :       /* true gens are congruent to those mod x AND positive at archp */
    2680        5894 :       gel(gen,i) = sprk_get_gen(L);
    2681             :     }
    2682        4781 :     gel(cyc,i) = sarch_get_cyc(sarch);
    2683        4781 :     cyc = shallowconcat1(cyc);
    2684        4781 :     gen = shallowconcat1(gen);
    2685        4781 :     cyc = ZV_snf_group(cyc, &U, (flag & nf_GEN)? &u1: NULL);
    2686             :   }
    2687             :   else
    2688             :   {
    2689        1645 :     cyc = sarch_get_cyc(sarch);
    2690        1645 :     gen = cgetg(1,t_VEC);
    2691        1645 :     U = matid(lg(cyc)-1);
    2692        1645 :     if (flag & nf_GEN) u1 = U;
    2693             :   }
    2694        6426 :   y = bid_grp(nf, u1, cyc, gen, x, sarch);
    2695        6426 :   if (!(flag & nf_INIT)) return y;
    2696        5628 :   U = split_U(U, sprk);
    2697        5628 :   return mkvec5(mkvec2(x, arch), y, mkvec2(fa,fa2), mkvec2(sprk, sarch), U);
    2698             : }
    2699             : GEN
    2700        6524 : Idealstar(GEN nf, GEN ideal, long flag)
    2701             : {
    2702             :   pari_sp av;
    2703        6524 :   if (!nf) return znstar0(ideal, (flag & nf_INIT)? 1: 0);
    2704        6181 :   av = avma;
    2705        6181 :   return gerepilecopy(av, Idealstar_i(nf, ideal, flag));
    2706             : }
    2707             : GEN
    2708         266 : Idealstarprk(GEN nf, GEN pr, long k, long flag)
    2709             : {
    2710         266 :   pari_sp av = avma;
    2711         266 :   GEN z = Idealstar_i(nf, mkmat2(mkcol(pr),mkcols(k)), flag);
    2712         266 :   return gerepilecopy(av, z);
    2713             : }
    2714             : 
    2715             : /* FIXME: obsolete */
    2716             : GEN
    2717           0 : zidealstarinitgen(GEN nf, GEN ideal)
    2718           0 : { return Idealstar(nf,ideal, nf_INIT|nf_GEN); }
    2719             : GEN
    2720           0 : zidealstarinit(GEN nf, GEN ideal)
    2721           0 : { return Idealstar(nf,ideal, nf_INIT); }
    2722             : GEN
    2723           0 : zidealstar(GEN nf, GEN ideal)
    2724           0 : { return Idealstar(nf,ideal, nf_GEN); }
    2725             : 
    2726             : GEN
    2727         406 : idealstar0(GEN nf, GEN ideal,long flag)
    2728             : {
    2729         406 :   switch(flag)
    2730             :   {
    2731           0 :     case 0: return Idealstar(nf,ideal, nf_GEN);
    2732         371 :     case 1: return Idealstar(nf,ideal, nf_INIT);
    2733          35 :     case 2: return Idealstar(nf,ideal, nf_INIT|nf_GEN);
    2734           0 :     default: pari_err_FLAG("idealstar");
    2735             :   }
    2736             :   return NULL; /* LCOV_EXCL_LINE */
    2737             : }
    2738             : 
    2739             : void
    2740      144045 : check_nfelt(GEN x, GEN *den)
    2741             : {
    2742      144045 :   long l = lg(x), i;
    2743      144045 :   GEN t, d = NULL;
    2744      144045 :   if (typ(x) != t_COL) pari_err_TYPE("check_nfelt", x);
    2745      557169 :   for (i=1; i<l; i++)
    2746             :   {
    2747      413124 :     t = gel(x,i);
    2748      413124 :     switch (typ(t))
    2749             :     {
    2750      325065 :       case t_INT: break;
    2751             :       case t_FRAC:
    2752       88059 :         if (!d) d = gel(t,2); else d = lcmii(d, gel(t,2));
    2753       88059 :         break;
    2754           0 :       default: pari_err_TYPE("check_nfelt", x);
    2755             :     }
    2756             :   }
    2757      144045 :   *den = d;
    2758      144045 : }
    2759             : 
    2760             : GEN
    2761      646761 : vecmodii(GEN a, GEN b)
    2762             : {
    2763             :   long i, l;
    2764      646761 :   GEN c = cgetg_copy(a, &l);
    2765      646761 :   for (i = 1; i < l; i++) gel(c,i) = modii(gel(a,i), gel(b,i));
    2766      646761 :   return c;
    2767             : }
    2768             : 
    2769             : static GEN
    2770      227744 : ideallog_i(GEN nf, GEN x, GEN sgn, zlog_S *S)
    2771             : {
    2772      227744 :   pari_sp av = avma;
    2773             :   GEN y, cyc;
    2774      227744 :   if (!S->hU) return cgetg(1, t_COL);
    2775      226176 :   cyc = bid_get_cyc(S->bid);
    2776      226176 :   if (typ(x) == t_MAT)
    2777             :   {
    2778       66381 :     if (lg(x) == 1) return zerocol(lg(cyc)-1);
    2779       66374 :     y = famat_zlog(nf, x, sgn, S);
    2780             :   }
    2781             :   else
    2782      159795 :     y = zlog(nf, x, sgn, S);
    2783      226162 :   y = ZMV_ZCV_mul(S->U, y);
    2784      226162 :   return gerepileupto(av, vecmodii(y, cyc));
    2785             : }
    2786             : 
    2787             : /* Given x (not necessarily integral), and bid as output by zidealstarinit,
    2788             :  * compute the vector of components on the generators bid[2].
    2789             :  * Assume (x,bid) = 1 and sgn is either NULL or nfsign_arch(x, bid) */
    2790             : GEN
    2791      223180 : ideallog_sgn(GEN nf, GEN x, GEN sgn, GEN bid)
    2792             : {
    2793             :   zlog_S S;
    2794      223180 :   nf = checknf(nf); checkbid(bid);
    2795      223173 :   init_zlog(&S, bid);
    2796      223173 :   if (sgn && typ(x) == t_VEC) /* vector of elements and signatures */
    2797             :   {
    2798        4487 :     long i, l = lg(x);
    2799        4487 :     GEN y = cgetg(l, t_MAT);
    2800        4487 :     for (i = 1; i < l; i++) gel(y,i) = ideallog_i(nf, gel(x,i), gel(sgn,i), &S);
    2801        4487 :     return y;
    2802             :   }
    2803      218686 :   return ideallog_i(nf, x, sgn, &S);
    2804             : }
    2805             : GEN
    2806      225399 : ideallog(GEN nf, GEN x, GEN bid)
    2807             : {
    2808      225399 :   if (!nf) return Zideallog(bid, x);
    2809      218693 :   return ideallog_sgn(nf, x, NULL, bid);
    2810             : }
    2811             : 
    2812             : /*************************************************************************/
    2813             : /**                                                                     **/
    2814             : /**               JOIN BID STRUCTURES, IDEAL LISTS                      **/
    2815             : /**                                                                     **/
    2816             : /*************************************************************************/
    2817             : /* bid1, bid2: for coprime modules m1 and m2 (without arch. part).
    2818             :  * Output: bid for m1 m2 */
    2819             : static GEN
    2820         476 : join_bid(GEN nf, GEN bid1, GEN bid2)
    2821             : {
    2822         476 :   pari_sp av = avma;
    2823             :   long nbgen, l1,l2;
    2824             :   GEN I1,I2, G1,G2, sprk1,sprk2, cyc1,cyc2, sarch;
    2825         476 :   GEN sprk, fa,fa2, U, cyc, y, u1 = NULL, x, gen;
    2826             : 
    2827         476 :   I1 = bid_get_ideal(bid1);
    2828         476 :   I2 = bid_get_ideal(bid2);
    2829         476 :   if (gequal1(gcoeff(I1,1,1))) return bid2; /* frequent trivial case */
    2830         259 :   G1 = bid_get_grp(bid1);
    2831         259 :   G2 = bid_get_grp(bid2);
    2832         259 :   x = idealmul(nf, I1,I2);
    2833         259 :   fa = famat_mul_shallow(bid_get_fact(bid1), bid_get_fact(bid2));
    2834         259 :   fa2= famat_mul_shallow(bid_get_fact2(bid1), bid_get_fact2(bid2));
    2835         259 :   sprk1 = bid_get_sprk(bid1);
    2836         259 :   sprk2 = bid_get_sprk(bid2);
    2837         259 :   sprk = shallowconcat(sprk1, sprk2);
    2838             : 
    2839         259 :   cyc1 = abgrp_get_cyc(G1); l1 = lg(cyc1);
    2840         259 :   cyc2 = abgrp_get_cyc(G2); l2 = lg(cyc2);
    2841         259 :   gen = (lg(G1)>3 && lg(G2)>3)? gen_1: NULL;
    2842         259 :   nbgen = l1+l2-2;
    2843         259 :   cyc = ZV_snf_group(shallowconcat(cyc1,cyc2), &U, gen? &u1: NULL);
    2844         259 :   if (nbgen)
    2845             :   {
    2846         259 :     GEN U1 = bid_get_U(bid1), U2 = bid_get_U(bid2);
    2847         259 :     U1 = l1==1? const_vec(lg(sprk1), cgetg(1,t_MAT))
    2848         259 :               : ZM_ZMV_mul(vecslice(U, 1, l1-1),   U1);
    2849         259 :     U2 = l2==1? const_vec(lg(sprk2), cgetg(1,t_MAT))
    2850         259 :               : ZM_ZMV_mul(vecslice(U, l1, nbgen), U2);
    2851         259 :     U = shallowconcat(U1, U2);
    2852             :   }
    2853             :   else
    2854           0 :     U = const_vec(lg(sprk), cgetg(1,t_MAT));
    2855             : 
    2856         259 :   if (gen)
    2857             :   {
    2858         259 :     GEN uv = zkchinese1init2(nf, I2, I1, x);
    2859         518 :     gen = shallowconcat(zkVchinese1(gel(uv,1), abgrp_get_gen(G1)),
    2860         259 :                         zkVchinese1(gel(uv,2), abgrp_get_gen(G2)));
    2861             :   }
    2862         259 :   sarch = bid_get_sarch(bid1); /* trivial */
    2863         259 :   y = bid_grp(nf, u1, cyc, gen, x, sarch);
    2864         259 :   x = mkvec2(x, bid_get_arch(bid1));
    2865         259 :   y = mkvec5(x, y, mkvec2(fa, fa2), mkvec2(sprk, sarch), U);
    2866         259 :   return gerepilecopy(av,y);
    2867             : }
    2868             : 
    2869             : typedef struct _ideal_data {
    2870             :   GEN nf, emb, L, pr, prL, sgnU, archp;
    2871             : } ideal_data;
    2872             : 
    2873             : /* z <- ( z | f(v[i])_{i=1..#v} ) */
    2874             : static void
    2875       43414 : concat_join(GEN *pz, GEN v, GEN (*f)(ideal_data*,GEN), ideal_data *data)
    2876             : {
    2877       43414 :   long i, nz, lv = lg(v);
    2878             :   GEN z, Z;
    2879       86828 :   if (lv == 1) return;
    2880       18942 :   z = *pz; nz = lg(z)-1;
    2881       18942 :   *pz = Z = cgetg(lv + nz, typ(z));
    2882       18942 :   for (i = 1; i <=nz; i++) gel(Z,i) = gel(z,i);
    2883       18942 :   Z += nz;
    2884       18942 :   for (i = 1; i < lv; i++) gel(Z,i) = f(data, gel(v,i));
    2885             : }
    2886             : static GEN
    2887         476 : join_idealinit(ideal_data *D, GEN x)
    2888         476 : { return join_bid(D->nf, x, D->prL); }
    2889             : static GEN
    2890       26222 : join_ideal(ideal_data *D, GEN x)
    2891       26222 : { return idealmulpowprime(D->nf, x, D->pr, D->L); }
    2892             : static GEN
    2893         455 : join_unit(ideal_data *D, GEN x)
    2894             : {
    2895         455 :   GEN bid = join_idealinit(D, gel(x,1)), u = gel(x,2), v = mkvec(D->emb);
    2896         455 :   if (lg(u) != 1) v = shallowconcat(u, v);
    2897         455 :   return mkvec2(bid, v);
    2898             : }
    2899             : 
    2900             : /*  flag & nf_GEN : generators, otherwise no
    2901             :  *  flag &2 : units, otherwise no
    2902             :  *  flag &4 : ideals in HNF, otherwise bid
    2903             :  *  flag &8 : omit ideals which cannot be conductors (pr^1 with Npr=2) */
    2904             : static GEN
    2905         350 : Ideallist(GEN bnf, ulong bound, long flag)
    2906             : {
    2907         350 :   const long cond = flag & 8;
    2908         350 :   const long do_units = flag & 2, big_id = !(flag & 4);
    2909         350 :   const long istar_flag = (flag & nf_GEN) | nf_INIT;
    2910         350 :   pari_sp av, av0 = avma;
    2911             :   long i, j;
    2912         350 :   GEN nf, z, p, fa, id, BOUND, U, empty = cgetg(1,t_VEC);
    2913             :   forprime_t S;
    2914             :   ideal_data ID;
    2915         350 :   GEN (*join_z)(ideal_data*, GEN) =
    2916             :     do_units? &join_unit
    2917         350 :               : (big_id? &join_idealinit: &join_ideal);
    2918             : 
    2919         350 :   nf = checknf(bnf);
    2920         350 :   if ((long)bound <= 0) return empty;
    2921         350 :   id = matid(nf_get_degree(nf));
    2922         350 :   if (big_id) id = Idealstar(nf,id, istar_flag);
    2923             : 
    2924             :   /* z[i] will contain all "objects" of norm i. Depending on flag, this means
    2925             :    * an ideal, a bid, or a couple [bid, log(units)]. Such objects are stored
    2926             :    * in vectors, computed one primary component at a time; join_z
    2927             :    * reconstructs the global object */
    2928         350 :   BOUND = utoipos(bound);
    2929         350 :   z = cgetg(bound+1,t_VEC);
    2930         350 :   if (do_units) {
    2931          14 :     U = bnf_build_units(bnf);
    2932          14 :     gel(z,1) = mkvec( mkvec2(id, cgetg(1,t_VEC)) );
    2933             :   } else {
    2934         336 :     U = NULL; /* -Wall */
    2935         336 :     gel(z,1) = mkvec(id);
    2936             :   }
    2937         350 :   for (i=2; i<=(long)bound; i++) gel(z,i) = empty;
    2938         350 :   ID.nf = nf;
    2939             : 
    2940         350 :   p = cgetipos(3);
    2941         350 :   u_forprime_init(&S, 2, bound);
    2942         350 :   av = avma;
    2943        5726 :   while ((p[2] = u_forprime_next(&S)))
    2944             :   {
    2945        5026 :     if (DEBUGLEVEL>1) { err_printf("%ld ",p[2]); err_flush(); }
    2946        5026 :     fa = idealprimedec_limit_norm(nf, p, BOUND);
    2947       10073 :     for (j=1; j<lg(fa); j++)
    2948             :     {
    2949        5047 :       GEN pr = gel(fa,j), z2 = leafcopy(z);
    2950        5047 :       ulong Q, q = upr_norm(pr);
    2951        5047 :       long l = (cond && q == 2)? 2: 1;
    2952             : 
    2953        5047 :       ID.pr = ID.prL = pr;
    2954       13370 :       for (Q = q; Q <= bound; l++, Q *= q) /* add pr^l */
    2955             :       {
    2956             :         ulong iQ;
    2957        8323 :         ID.L = utoipos(l);
    2958        8323 :         if (big_id) {
    2959         217 :           ID.prL = Idealstarprk(nf, pr, l, istar_flag);
    2960         217 :           if (do_units)
    2961             :           {
    2962         196 :             GEN sprk = bid_get_sprk(ID.prL);
    2963         392 :             ID.emb = lg(sprk) == 1? cgetg(1,t_VEC)
    2964         196 :                                   : vzlog_pr(nf, U, gel(sprk,1));
    2965             :           }
    2966             :         }
    2967       51737 :         for (iQ = Q,i = 1; iQ <= bound; iQ += Q,i++)
    2968       43414 :           concat_join(&gel(z,iQ), gel(z2,i), join_z, &ID);
    2969             :       }
    2970             :     }
    2971        5026 :     if (gc_needed(av,1))
    2972             :     {
    2973           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"Ideallist");
    2974           0 :       z = gerepilecopy(av, z);
    2975             :     }
    2976             :   }
    2977         350 :   return gerepilecopy(av0, z);
    2978             : }
    2979             : GEN
    2980         350 : ideallist0(GEN bnf,long bound, long flag) {
    2981         350 :   if (flag<0 || flag>15) pari_err_FLAG("ideallist");
    2982         350 :   return Ideallist(bnf,bound,flag);
    2983             : }
    2984             : GEN
    2985           0 : ideallist(GEN bnf,long bound) { return Ideallist(bnf,bound,4); }
    2986             : 
    2987             : /* bid = for module m (without arch. part), arch = archimedean part.
    2988             :  * Output: bid for [m,arch] */
    2989             : static GEN
    2990          56 : join_bid_arch(GEN nf, GEN bid, GEN archp)
    2991             : {
    2992          56 :   pari_sp av = avma;
    2993             :   GEN G, U;
    2994          56 :   GEN sprk, cyc, y, u1 = NULL, x, sarch, gen;
    2995             : 
    2996          56 :   checkbid(bid);
    2997          56 :   G = bid_get_grp(bid);
    2998          56 :   x = bid_get_ideal(bid);
    2999          56 :   sarch = nfarchstar(nf, bid_get_ideal(bid), archp);
    3000          56 :   sprk = bid_get_sprk(bid);
    3001             : 
    3002          56 :   gen = (lg(G)>3)? gel(G,3): NULL;
    3003          56 :   cyc = diagonal_shallow(shallowconcat(gel(G,2), sarch_get_cyc(sarch)));
    3004          56 :   cyc = ZM_snf_group(cyc, &U, gen? &u1: NULL);
    3005          56 :   y = bid_grp(nf, u1, cyc, gen, x, sarch);
    3006          56 :   U = split_U(U, sprk);
    3007          56 :   y = mkvec5(mkvec2(x, archp), y, gel(bid,3), mkvec2(sprk, sarch), U);
    3008          56 :   return gerepilecopy(av,y);
    3009             : }
    3010             : static GEN
    3011          56 : join_arch(ideal_data *D, GEN x) {
    3012          56 :   return join_bid_arch(D->nf, x, D->archp);
    3013             : }
    3014             : static GEN
    3015          28 : join_archunit(ideal_data *D, GEN x) {
    3016          28 :   GEN bid = join_arch(D, gel(x,1)), u = gel(x,2), v = mkvec(D->emb);
    3017          28 :   if (lg(u) != 1) v = shallowconcat(u, v);
    3018          28 :   return mkvec2(bid, v);
    3019             : }
    3020             : 
    3021             : /* L from ideallist, add archimedean part */
    3022             : GEN
    3023          14 : ideallistarch(GEN bnf, GEN L, GEN arch)
    3024             : {
    3025             :   pari_sp av;
    3026          14 :   long i, j, l = lg(L), lz;
    3027             :   GEN v, z, V;
    3028             :   ideal_data ID;
    3029             :   GEN (*join_z)(ideal_data*, GEN);
    3030             : 
    3031          14 :   if (typ(L) != t_VEC) pari_err_TYPE("ideallistarch",L);
    3032          14 :   if (l == 1) return cgetg(1,t_VEC);
    3033          14 :   z = gel(L,1);
    3034          14 :   if (typ(z) != t_VEC) pari_err_TYPE("ideallistarch",z);
    3035          14 :   z = gel(z,1); /* either a bid or [bid,U] */
    3036          14 :   ID.nf = checknf(bnf);
    3037          14 :   ID.archp = vec01_to_indices(arch);
    3038          14 :   if (lg(z) == 3) { /* the latter: do units */
    3039           7 :     if (typ(z) != t_VEC) pari_err_TYPE("ideallistarch",z);
    3040           7 :     ID.emb = zm_to_ZM( rowpermute(nfsign_units(bnf,NULL,1), ID.archp) );
    3041           7 :     join_z = &join_archunit;
    3042             :   } else
    3043           7 :     join_z = &join_arch;
    3044          14 :   av = avma; V = cgetg(l, t_VEC);
    3045          70 :   for (i = 1; i < l; i++)
    3046             :   {
    3047          56 :     z = gel(L,i); lz = lg(z);
    3048          56 :     gel(V,i) = v = cgetg(lz,t_VEC);
    3049          56 :     for (j=1; j<lz; j++) gel(v,j) = join_z(&ID, gel(z,j));
    3050             :   }
    3051          14 :   return gerepilecopy(av,V);
    3052             : }

Generated by: LCOV version 1.11