Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - base3.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23036-b751c0af5) Lines: 1616 1724 93.7 %
Date: 2018-09-26 05:46:06 Functions: 181 191 94.8 %
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    11594736 : get_tab(GEN nf, long *N)
      30             : {
      31    11594736 :   GEN tab = (typ(nf) == t_MAT)? nf: gel(nf,9);
      32    11594736 :   *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   397393536 : _mulii(GEN x, GEN y) {
      38   630645302 :   return is_pm1(x)? (signe(x) < 0)? negi(y): y
      39   630645302 :                   : mulii(x, y);
      40             : }
      41             : 
      42             : GEN
      43       16989 : tablemul_ei_ej(GEN M, long i, long j)
      44             : {
      45             :   long N;
      46       16989 :   GEN tab = get_tab(M, &N);
      47       16989 :   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       10213 : tablemul_ei(GEN M, GEN x, long i)
      55             : {
      56             :   long j, k, N;
      57             :   GEN v, tab;
      58             : 
      59       10213 :   if (i==1) return gcopy(x);
      60       10213 :   tab = get_tab(M, &N);
      61       10213 :   if (typ(x) != t_COL) { v = zerocol(N); gel(v,i) = gcopy(x); return v; }
      62       10213 :   tab += (i-1)*N; v = cgetg(N+1,t_COL);
      63             :   /* wi . x = [ sum_j tab[k,j] x[j] ]_k */
      64       69839 :   for (k=1; k<=N; k++)
      65             :   {
      66       59626 :     pari_sp av = avma;
      67       59626 :     GEN s = gen_0;
      68      417060 :     for (j=1; j<=N; j++)
      69             :     {
      70      357434 :       GEN c = gcoeff(tab,k,j);
      71      357434 :       if (!gequal0(c)) s = gadd(s, gmul(c, gel(x,j)));
      72             :     }
      73       59626 :     gel(v,k) = gerepileupto(av,s);
      74             :   }
      75       10213 :   return v;
      76             : }
      77             : /* as tablemul_ei, assume x a ZV of correct length */
      78             : GEN
      79     9270907 : zk_ei_mul(GEN nf, GEN x, long i)
      80             : {
      81             :   long j, k, N;
      82             :   GEN v, tab;
      83             : 
      84     9270907 :   if (i==1) return ZC_copy(x);
      85     9270893 :   tab = get_tab(nf, &N); tab += (i-1)*N;
      86     9270893 :   v = cgetg(N+1,t_COL);
      87    63411435 :   for (k=1; k<=N; k++)
      88             :   {
      89    54140542 :     pari_sp av = avma;
      90    54140542 :     GEN s = gen_0;
      91   656636670 :     for (j=1; j<=N; j++)
      92             :     {
      93   602496128 :       GEN c = gcoeff(tab,k,j);
      94   602496128 :       if (signe(c)) s = addii(s, _mulii(c, gel(x,j)));
      95             :     }
      96    54140542 :     gel(v,k) = gerepileuptoint(av, s);
      97             :   }
      98     9270893 :   return v;
      99             : }
     100             : 
     101             : /* table of multiplication by wi in R[w1,..., wN] */
     102             : GEN
     103        2422 : ei_multable(GEN TAB, long i)
     104             : {
     105             :   long k,N;
     106        2422 :   GEN m, tab = get_tab(TAB, &N);
     107        2422 :   tab += (i-1)*N;
     108        2422 :   m = cgetg(N+1,t_MAT);
     109        2422 :   for (k=1; k<=N; k++) gel(m,k) = gel(tab,k);
     110        2422 :   return m;
     111             : }
     112             : 
     113             : GEN
     114     4865580 : zk_multable(GEN nf, GEN x)
     115             : {
     116     4865580 :   long i, l = lg(x);
     117     4865580 :   GEN mul = cgetg(l,t_MAT);
     118     4865580 :   gel(mul,1) = x; /* assume w_1 = 1 */
     119     4865580 :   for (i=2; i<l; i++) gel(mul,i) = zk_ei_mul(nf,x,i);
     120     4865580 :   return mul;
     121             : }
     122             : GEN
     123        1813 : multable(GEN M, GEN x)
     124             : {
     125             :   long i, N;
     126             :   GEN mul;
     127        1813 :   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     3488511 : zk_scalar_or_multable(GEN nf, GEN x)
     140             : {
     141     3488511 :   long tx = typ(x);
     142     3488511 :   if (tx == t_MAT || tx == t_INT) return x;
     143     3439786 :   x = nf_to_scalar_or_basis(nf, x);
     144     3439786 :   return (typ(x) == t_COL)? zk_multable(nf, x): x;
     145             : }
     146             : 
     147             : GEN
     148       23583 : nftrace(GEN nf, GEN x)
     149             : {
     150       23583 :   pari_sp av = avma;
     151       23583 :   nf = checknf(nf);
     152       23583 :   x = nf_to_scalar_or_basis(nf, x);
     153       70728 :   x = (typ(x) == t_COL)? RgV_dotproduct(x, gel(nf_get_Tr(nf),1))
     154       47145 :                        : gmulgs(x, nf_get_degree(nf));
     155       23583 :   return gerepileupto(av, x);
     156             : }
     157             : GEN
     158         784 : rnfelttrace(GEN rnf, GEN x)
     159             : {
     160         784 :   pari_sp av = avma;
     161         784 :   checkrnf(rnf);
     162         784 :   x = rnfeltabstorel(rnf, x);
     163        2002 :   x = (typ(x) == t_POLMOD)? rnfeltdown(rnf, gtrace(x))
     164        1309 :                           : gmulgs(x, rnf_get_degree(rnf));
     165         693 :   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       31381 : nfnorm(GEN nf, GEN x)
     181             : {
     182       31381 :   pari_sp av = avma;
     183       31381 :   nf = checknf(nf);
     184       31381 :   if (typ(x) == t_MAT) return famat_norm(nf, x);
     185       31374 :   x = nf_to_scalar_or_alg(nf, x);
     186       87549 :   x = (typ(x) == t_POL)? RgXQ_norm(x, nf_get_pol(nf))
     187       56175 :                        : gpowgs(x, nf_get_degree(nf));
     188       31374 :   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    15589533 : nfadd(GEN nf, GEN x, GEN y)
     205             : {
     206    15589533 :   pari_sp av = avma;
     207             :   GEN z;
     208             : 
     209    15589533 :   nf = checknf(nf);
     210    15589533 :   x = nf_to_scalar_or_basis(nf, x);
     211    15589533 :   y = nf_to_scalar_or_basis(nf, y);
     212    15589533 :   if (typ(x) != t_COL)
     213    12683855 :   { z = (typ(y) == t_COL)? RgC_Rg_add(y, x): gadd(x,y); }
     214             :   else
     215     2905678 :   { z = (typ(y) == t_COL)? RgC_add(x, y): RgC_Rg_add(x, y); }
     216    15589533 :   return gerepileupto(av, z);
     217             : }
     218             : /* x - y in nf */
     219             : GEN
     220     1193969 : nfsub(GEN nf, GEN x, GEN y)
     221             : {
     222     1193969 :   pari_sp av = avma;
     223             :   GEN z;
     224             : 
     225     1193969 :   nf = checknf(nf);
     226     1193969 :   x = nf_to_scalar_or_basis(nf, x);
     227     1193969 :   y = nf_to_scalar_or_basis(nf, y);
     228     1193969 :   if (typ(x) != t_COL)
     229      884534 :   { z = (typ(y) == t_COL)? Rg_RgC_sub(x,y): gsub(x,y); }
     230             :   else
     231      309435 :   { z = (typ(y) == t_COL)? RgC_sub(x,y): RgC_Rg_sub(x,y); }
     232     1193969 :   return gerepileupto(av, z);
     233             : }
     234             : 
     235             : /* product of x and y in nf */
     236             : GEN
     237    20605654 : nfmul(GEN nf, GEN x, GEN y)
     238             : {
     239             :   GEN z;
     240    20605654 :   pari_sp av = avma;
     241             : 
     242    20605654 :   if (x == y) return nfsqr(nf,x);
     243             : 
     244    17749612 :   nf = checknf(nf);
     245    17749612 :   x = nf_to_scalar_or_basis(nf, x);
     246    17749612 :   y = nf_to_scalar_or_basis(nf, y);
     247    17749612 :   if (typ(x) != t_COL)
     248             :   {
     249    13738325 :     if (isintzero(x)) return gen_0;
     250     9854375 :     z = (typ(y) == t_COL)? RgC_Rg_mul(y, x): gmul(x,y); }
     251             :   else
     252             :   {
     253     4011287 :     if (typ(y) != t_COL)
     254             :     {
     255     2846382 :       if (isintzero(y)) return gen_0;
     256      640640 :       z = RgC_Rg_mul(x, y);
     257             :     }
     258             :     else
     259             :     {
     260             :       GEN dx, dy;
     261     1164905 :       x = Q_remove_denom(x, &dx);
     262     1164905 :       y = Q_remove_denom(y, &dy);
     263     1164905 :       z = nfmuli(nf,x,y);
     264     1164905 :       dx = mul_denom(dx,dy);
     265     1164905 :       if (dx) z = ZC_Z_div(z, dx);
     266             :     }
     267             :   }
     268    11659920 :   return gerepileupto(av, z);
     269             : }
     270             : /* square of x in nf */
     271             : GEN
     272     4715630 : nfsqr(GEN nf, GEN x)
     273             : {
     274     4715630 :   pari_sp av = avma;
     275             :   GEN z;
     276             : 
     277     4715630 :   nf = checknf(nf);
     278     4715630 :   x = nf_to_scalar_or_basis(nf, x);
     279     4715630 :   if (typ(x) != t_COL) z = gsqr(x);
     280             :   else
     281             :   {
     282             :     GEN dx;
     283       80209 :     x = Q_remove_denom(x, &dx);
     284       80209 :     z = nfsqri(nf,x);
     285       80209 :     if (dx) z = RgC_Rg_div(z, sqri(dx));
     286             :   }
     287     4715630 :   return gerepileupto(av, z);
     288             : }
     289             : 
     290             : /* x a ZC, v a t_COL of ZC/Z */
     291             : GEN
     292      129750 : zkC_multable_mul(GEN v, GEN x)
     293             : {
     294      129750 :   long i, l = lg(v);
     295      129750 :   GEN y = cgetg(l, t_COL);
     296      458116 :   for (i = 1; i < l; i++)
     297             :   {
     298      328366 :     GEN c = gel(v,i);
     299      328366 :     if (typ(c)!=t_COL) {
     300           0 :       if (!isintzero(c)) c = ZC_Z_mul(gel(x,1), c);
     301             :     } else {
     302      328366 :       c = ZM_ZC_mul(x,c);
     303      328366 :       if (ZV_isscalar(c)) c = gel(c,1);
     304             :     }
     305      328366 :     gel(y,i) = c;
     306             :   }
     307      129750 :   return y;
     308             : }
     309             : 
     310             : GEN
     311       51093 : nfC_multable_mul(GEN v, GEN x)
     312             : {
     313       51093 :   long i, l = lg(v);
     314       51093 :   GEN y = cgetg(l, t_COL);
     315      322105 :   for (i = 1; i < l; i++)
     316             :   {
     317      271012 :     GEN c = gel(v,i);
     318      271012 :     if (typ(c)!=t_COL) {
     319      221607 :       if (!isintzero(c)) c = RgC_Rg_mul(gel(x,1), c);
     320             :     } else {
     321       49405 :       c = RgM_RgC_mul(x,c);
     322       49405 :       if (QV_isscalar(c)) c = gel(c,1);
     323             :     }
     324      271012 :     gel(y,i) = c;
     325             :   }
     326       51093 :   return y;
     327             : }
     328             : 
     329             : GEN
     330      167909 : nfC_nf_mul(GEN nf, GEN v, GEN x)
     331             : {
     332             :   long tx;
     333             :   GEN y;
     334             : 
     335      167909 :   x = nf_to_scalar_or_basis(nf, x);
     336      167909 :   tx = typ(x);
     337      167909 :   if (tx != t_COL)
     338             :   {
     339             :     long l, i;
     340      123445 :     if (tx == t_INT)
     341             :     {
     342      115423 :       long s = signe(x);
     343      115423 :       if (!s) return zerocol(lg(v)-1);
     344      109043 :       if (is_pm1(x)) return s > 0? leafcopy(v): RgC_neg(v);
     345             :     }
     346       39498 :     l = lg(v); y = cgetg(l, t_COL);
     347      277721 :     for (i=1; i < l; i++)
     348             :     {
     349      238223 :       GEN c = gel(v,i);
     350      238223 :       if (typ(c) != t_COL) c = gmul(c, x); else c = RgC_Rg_mul(c, x);
     351      238223 :       gel(y,i) = c;
     352             :     }
     353       39498 :     return y;
     354             :   }
     355             :   else
     356             :   {
     357             :     GEN dx;
     358       44464 :     x = zk_multable(nf, Q_remove_denom(x,&dx));
     359       44464 :     y = nfC_multable_mul(v, x);
     360       44464 :     return dx? RgC_Rg_div(y, dx): y;
     361             :   }
     362             : }
     363             : static GEN
     364        7784 : mulbytab(GEN M, GEN c)
     365        7784 : { return typ(c) == t_COL? RgM_RgC_mul(M,c): RgC_Rg_mul(gel(M,1), c); }
     366             : GEN
     367        1813 : tablemulvec(GEN M, GEN x, GEN v)
     368             : {
     369             :   long l, i;
     370             :   GEN y;
     371             : 
     372        1813 :   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        1813 :   x = multable(M, x); /* multiplication table by x */
     378        1813 :   y = cgetg_copy(v, &l);
     379        1813 :   if (typ(v) == t_POL)
     380             :   {
     381        1813 :     y[1] = v[1];
     382        1813 :     for (i=2; i < l; i++) gel(y,i) = mulbytab(x, gel(v,i));
     383        1813 :     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        1813 :   return y;
     390             : }
     391             : 
     392             : GEN
     393      359479 : zkmultable_capZ(GEN mx) { return Q_denom(zkmultable_inv(mx)); }
     394             : 
     395             : GEN
     396      401368 : zkmultable_inv(GEN mx)
     397      401368 : { return ZM_gauss(mx, col_ei(lg(mx)-1,1)); }
     398             : 
     399             : /* nf a true nf, x a ZC */
     400             : GEN
     401       41889 : zk_inv(GEN nf, GEN x) { return zkmultable_inv(zk_multable(nf,x)); }
     402             : 
     403             : /* inverse of x in nf */
     404             : GEN
     405       64155 : nfinv(GEN nf, GEN x)
     406             : {
     407       64155 :   pari_sp av = avma;
     408             :   GEN z;
     409             : 
     410       64155 :   nf = checknf(nf);
     411       64155 :   x = nf_to_scalar_or_basis(nf, x);
     412       64155 :   if (typ(x) == t_COL)
     413             :   {
     414             :     GEN d;
     415       24724 :     x = Q_remove_denom(x, &d);
     416       24724 :     z = zk_inv(nf, x);
     417       24724 :     if (d) z = RgC_Rg_mul(z, d);
     418             :   }
     419             :   else
     420       39431 :     z = ginv(x);
     421       64155 :   return gerepileupto(av, z);
     422             : }
     423             : 
     424             : /* quotient of x and y in nf */
     425             : GEN
     426       21980 : nfdiv(GEN nf, GEN x, GEN y)
     427             : {
     428       21980 :   pari_sp av = avma;
     429             :   GEN z;
     430             : 
     431       21980 :   nf = checknf(nf);
     432       21980 :   y = nf_to_scalar_or_basis(nf, y);
     433       21980 :   if (typ(y) != t_COL)
     434             :   {
     435       12207 :     x = nf_to_scalar_or_basis(nf, x);
     436       12207 :     z = (typ(x) == t_COL)? RgC_Rg_div(x, y): gdiv(x,y);
     437             :   }
     438             :   else
     439             :   {
     440             :     GEN d;
     441        9773 :     y = Q_remove_denom(y, &d);
     442        9773 :     z = nfmul(nf, x, zk_inv(nf,y));
     443        9773 :     if (d) z = RgC_Rg_mul(z, d);
     444             :   }
     445       21980 :   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     1614076 : nfmuli(GEN nf, GEN x, GEN y)
     452             : {
     453             :   long i, j, k, N;
     454     1614076 :   GEN s, v, TAB = get_tab(nf, &N);
     455             : 
     456     1614076 :   if (typ(x) == t_INT) return (typ(y) == t_COL)? ZC_Z_mul(y, x): mulii(x,y);
     457     1518060 :   if (typ(y) == t_INT) return ZC_Z_mul(x, y);
     458             :   /* both x and y are ZV */
     459     1484262 :   v = cgetg(N+1,t_COL);
     460     6050256 :   for (k=1; k<=N; k++)
     461             :   {
     462     4565994 :     pari_sp av = avma;
     463     4565994 :     GEN TABi = TAB;
     464     4565994 :     if (k == 1)
     465     1484262 :       s = mulii(gel(x,1),gel(y,1));
     466             :     else
     467     6163464 :       s = addii(mulii(gel(x,1),gel(y,k)),
     468     6163464 :                 mulii(gel(x,k),gel(y,1)));
     469    23326640 :     for (i=2; i<=N; i++)
     470             :     {
     471    18760646 :       GEN t, xi = gel(x,i);
     472    18760646 :       TABi += N;
     473    18760646 :       if (!signe(xi)) continue;
     474             : 
     475    13028849 :       t = NULL;
     476   133748173 :       for (j=2; j<=N; j++)
     477             :       {
     478   120719324 :         GEN p1, c = gcoeff(TABi, k, j); /* m^{i,j}_k */
     479   120719324 :         if (!signe(c)) continue;
     480    54577699 :         p1 = _mulii(c, gel(y,j));
     481    54577699 :         t = t? addii(t, p1): p1;
     482             :       }
     483    13028849 :       if (t) s = addii(s, mulii(xi, t));
     484             :     }
     485     4565994 :     gel(v,k) = gerepileuptoint(av,s);
     486             :   }
     487     1484262 :   return v;
     488             : }
     489             : /* square of INTEGER (t_INT or ZC) x in nf */
     490             : GEN
     491      680143 : nfsqri(GEN nf, GEN x)
     492             : {
     493             :   long i, j, k, N;
     494      680143 :   GEN s, v, TAB = get_tab(nf, &N);
     495             : 
     496      680143 :   if (typ(x) == t_INT) return sqri(x);
     497      680143 :   v = cgetg(N+1,t_COL);
     498     5361359 :   for (k=1; k<=N; k++)
     499             :   {
     500     4681216 :     pari_sp av = avma;
     501     4681216 :     GEN TABi = TAB;
     502     4681216 :     if (k == 1)
     503      680143 :       s = sqri(gel(x,1));
     504             :     else
     505     4001073 :       s = shifti(mulii(gel(x,1),gel(x,k)), 1);
     506    56476098 :     for (i=2; i<=N; i++)
     507             :     {
     508    51794882 :       GEN p1, c, t, xi = gel(x,i);
     509    51794882 :       TABi += N;
     510    51794882 :       if (!signe(xi)) continue;
     511             : 
     512    17527938 :       c = gcoeff(TABi, k, i);
     513    17527938 :       t = signe(c)? _mulii(c,xi): NULL;
     514   250613565 :       for (j=i+1; j<=N; j++)
     515             :       {
     516   233085627 :         c = gcoeff(TABi, k, j);
     517   233085627 :         if (!signe(c)) continue;
     518   121691758 :         p1 = _mulii(c, shifti(gel(x,j),1));
     519   121691758 :         t = t? addii(t, p1): p1;
     520             :       }
     521    17527938 :       if (t) s = addii(s, mulii(xi, t));
     522             :     }
     523     4681216 :     gel(v,k) = gerepileuptoint(av,s);
     524             :   }
     525      680143 :   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       40040 : tablesqr(GEN TAB, GEN x)
     569             : {
     570             :   long i, j, k, N;
     571             :   GEN s, v;
     572             : 
     573       40040 :   if (typ(x) != t_COL) return gsqr(x);
     574       40040 :   N = lg(x)-1;
     575       40040 :   v = cgetg(N+1,t_COL);
     576             : 
     577      278922 :   for (k=1; k<=N; k++)
     578             :   {
     579      238882 :     pari_sp av = avma;
     580      238882 :     GEN TABi = TAB;
     581      238882 :     if (k == 1)
     582       40040 :       s = gsqr(gel(x,1));
     583             :     else
     584      198842 :       s = gmul2n(gmul(gel(x,1),gel(x,k)), 1);
     585     1455440 :     for (i=2; i<=N; i++)
     586             :     {
     587     1216558 :       GEN p1, c, t, xi = gel(x,i);
     588     1216558 :       TABi += N;
     589     1216558 :       if (gequal0(xi)) continue;
     590             : 
     591      321069 :       c = gcoeff(TABi, k, i);
     592      321069 :       t = !gequal0(c)? gmul(c,xi): NULL;
     593     1241723 :       for (j=i+1; j<=N; j++)
     594             :       {
     595      920654 :         c = gcoeff(TABi, k, j);
     596      920654 :         if (gequal0(c)) continue;
     597      482706 :         p1 = gmul(gmul2n(c,1), gel(x,j));
     598      482706 :         t = t? gadd(t, p1): p1;
     599             :       }
     600      321069 :       if (t) s = gadd(s, gmul(xi, t));
     601             :     }
     602      238882 :     gel(v,k) = gerepileupto(av,s);
     603             :   }
     604       40040 :   return v;
     605             : }
     606             : 
     607             : static GEN
     608       47235 : _mul(void *data, GEN x, GEN y) { return nfmuli((GEN)data,x,y); }
     609             : static GEN
     610      142265 : _sqr(void *data, GEN x) { return nfsqri((GEN)data,x); }
     611             : 
     612             : /* Compute z^n in nf, left-shift binary powering */
     613             : GEN
     614      117894 : nfpow(GEN nf, GEN z, GEN n)
     615             : {
     616      117894 :   pari_sp av = avma;
     617             :   long s;
     618             :   GEN x, cx;
     619             : 
     620      117894 :   if (typ(n)!=t_INT) pari_err_TYPE("nfpow",n);
     621      117894 :   nf = checknf(nf);
     622      117894 :   s = signe(n); if (!s) return gen_1;
     623      117894 :   x = nf_to_scalar_or_basis(nf, z);
     624      117894 :   if (typ(x) != t_COL) return powgi(x,n);
     625      117271 :   if (s < 0)
     626             :   { /* simplified nfinv */
     627             :     GEN d;
     628        3451 :     x = Q_remove_denom(x, &d);
     629        3451 :     x = zk_inv(nf, x);
     630        3451 :     x = primitive_part(x, &cx);
     631        3451 :     cx = mul_content(cx, d);
     632        3451 :     n = negi(n);
     633             :   }
     634             :   else
     635      113820 :     x = primitive_part(x, &cx);
     636      117271 :   x = gen_pow(x, n, (void*)nf, _sqr, _mul);
     637      117271 :   if (cx) x = gmul(x, powgi(cx, n));
     638      117271 :   return av==avma? gcopy(x): gerepileupto(av,x);
     639             : }
     640             : /* Compute z^n in nf, left-shift binary powering */
     641             : GEN
     642       45941 : nfpow_u(GEN nf, GEN z, ulong n)
     643             : {
     644       45941 :   pari_sp av = avma;
     645             :   GEN x, cx;
     646             : 
     647       45941 :   nf = checknf(nf);
     648       45941 :   if (!n) return gen_1;
     649       45941 :   x = nf_to_scalar_or_basis(nf, z);
     650       45941 :   if (typ(x) != t_COL) return gpowgs(x,n);
     651       17675 :   x = primitive_part(x, &cx);
     652       17675 :   x = gen_powu(x, n, (void*)nf, _sqr, _mul);
     653       17675 :   if (cx) x = gmul(x, powgi(cx, utoipos(n)));
     654       17675 :   return av==avma? gcopy(x): gerepileupto(av,x);
     655             : }
     656             : 
     657             : static GEN
     658     2397969 : _nf_red(void *E, GEN x) { (void)E; return x; }
     659             : 
     660             : static GEN
     661     9326373 : _nf_add(void *E, GEN x, GEN y) { return nfadd((GEN)E,x,y); }
     662             : 
     663             : static GEN
     664      570948 : _nf_neg(void *E, GEN x) { (void)E; return gneg(x); }
     665             : 
     666             : static GEN
     667    11290489 : _nf_mul(void *E, GEN x, GEN y) { return nfmul((GEN)E,x,y); }
     668             : 
     669             : static GEN
     670       41566 : _nf_inv(void *E, GEN x) { return nfinv((GEN)E,x); }
     671             : 
     672             : static GEN
     673        8274 : _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      177583 : const struct bb_field *get_nf_field(void **E, GEN nf)
     679      177583 : { *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        8260 : nfM_inv(GEN nf, GEN M)
     690             : {
     691             :   void *E;
     692        8260 :   const struct bb_field *S = get_nf_field(&E, nf);
     693        8260 :   return gen_Gauss(M, matid(lg(M)-1), E, S);
     694             : }
     695             : GEN
     696        8050 : nfM_mul(GEN nf, GEN A, GEN B)
     697             : {
     698             :   void *E;
     699        8050 :   const struct bb_field *S = get_nf_field(&E, nf);
     700        8050 :   return gen_matmul(A, B, E, S);
     701             : }
     702             : GEN
     703      161259 : nfM_nfC_mul(GEN nf, GEN A, GEN B)
     704             : {
     705             :   void *E;
     706      161259 :   const struct bb_field *S = get_nf_field(&E, nf);
     707      161259 :   return gen_matcolmul(A, B, E, S);
     708             : }
     709             : 
     710             : /* valuation of integral x (ZV), with resp. to prime ideal pr */
     711             : long
     712     5377240 : ZC_nfvalrem(GEN x, GEN pr, GEN *newx)
     713             : {
     714             :   long i, v, l;
     715     5377240 :   GEN r, y, p = pr_get_p(pr), mul = pr_get_tau(pr);
     716             : 
     717             :   /* p inert */
     718     5377240 :   if (typ(mul) == t_INT) return newx? ZV_pvalrem(x, p, newx):ZV_pval(x, p);
     719     5366082 :   y = cgetg_copy(x, &l); /* will hold the new x */
     720     5366082 :   x = leafcopy(x);
     721     8193787 :   for(v=0;; v++)
     722             :   {
     723    31338006 :     for (i=1; i<l; i++)
     724             :     { /* is (x.b)[i] divisible by p ? */
     725    25682596 :       gel(y,i) = dvmdii(ZMrow_ZC_mul(mul,x,i),p,&r);
     726    25682596 :       if (r != gen_0) { if (newx) *newx = x; return v; }
     727             :     }
     728     2827705 :     swap(x, y);
     729             :   }
     730             : }
     731             : long
     732     5140538 : ZC_nfval(GEN x, GEN P)
     733     5140538 : { return ZC_nfvalrem(x, P, NULL); }
     734             : 
     735             : /* v_P(x) != 0, x a ZV. Simpler version of ZC_nfvalrem */
     736             : int
     737      260666 : ZC_prdvd(GEN x, GEN P)
     738             : {
     739      260666 :   pari_sp av = avma;
     740             :   long i, l;
     741      260666 :   GEN p = pr_get_p(P), mul = pr_get_tau(P);
     742      260666 :   if (typ(mul) == t_INT) return ZV_Z_dvd(x, p);
     743      260421 :   l = lg(x);
     744     1014930 :   for (i=1; i<l; i++)
     745      900296 :     if (!dvdii(ZMrow_ZC_mul(mul,x,i), p)) return gc_bool(av,0);
     746      114634 :   return gc_bool(av,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     1303701 : nfval(GEN nf, GEN x, GEN pr)
     763             : {
     764     1303701 :   pari_sp av = avma;
     765             :   long w, e;
     766             :   GEN cx, p;
     767             : 
     768     1303701 :   if (gequal0(x)) return LONG_MAX;
     769     1302238 :   nf = checknf(nf);
     770     1302238 :   checkprid(pr);
     771     1302238 :   p = pr_get_p(pr);
     772     1302238 :   e = pr_get_e(pr);
     773     1302238 :   x = nf_to_scalar_or_basis(nf, x);
     774     1302238 :   if (typ(x) != t_COL) return e*Q_pval(x,p);
     775      107331 :   x = Q_primitive_part(x, &cx);
     776      107331 :   w = ZC_nfval(x,pr);
     777      107331 :   if (cx) w += e*Q_pval(cx,p);
     778      107331 :   return gc_long(av,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       20104 : powp(GEN nf, GEN pr, long v)
     785             : {
     786             :   GEN b, z;
     787             :   long e;
     788       20104 :   if (!v) return gen_1;
     789       19978 :   b = pr_get_tau(pr);
     790       19978 :   if (typ(b) == t_INT) return gen_1;
     791        1274 :   e = pr_get_e(pr);
     792        1274 :   z = gel(b,1);
     793        1274 :   if (e != 1) z = gdiv(nfpow_u(nf, z, e), powiu(pr_get_p(pr),e-1));
     794        1274 :   if (v < 0) { v = -v; z = nfinv(nf, z); }
     795        1274 :   if (v != 1) z = nfpow_u(nf, z, v);
     796        1274 :   return z;
     797             : }
     798             : long
     799       64925 : nfvalrem(GEN nf, GEN x, GEN pr, GEN *py)
     800             : {
     801       64925 :   pari_sp av = avma;
     802             :   long w, e;
     803             :   GEN cx, p, t;
     804             : 
     805       64925 :   if (!py) return nfval(nf,x,pr);
     806       64806 :   if (gequal0(x)) { *py = gcopy(x); return LONG_MAX; }
     807       64750 :   nf = checknf(nf);
     808       64750 :   checkprid(pr);
     809       64750 :   p = pr_get_p(pr);
     810       64750 :   e = pr_get_e(pr);
     811       64750 :   x = nf_to_scalar_or_basis(nf, x);
     812       64750 :   if (typ(x) != t_COL) {
     813       52871 :     w = Q_pvalrem(x,p, py);
     814       52871 :     if (!w) { *py = gerepilecopy(av, x); return 0; }
     815       18977 :     *py = gerepileupto(av, gmul(powp(nf, pr, w), *py));
     816       18977 :     return e*w;
     817             :   }
     818       11879 :   x = Q_primitive_part(x, &cx);
     819       11879 :   w = ZC_nfvalrem(x,pr, py);
     820       11879 :   if (cx)
     821             :   {
     822        1127 :     long v = Q_pvalrem(cx,p, &t);
     823        1127 :     *py = nfmul(nf, *py, gmul(powp(nf,pr,v), t));
     824        1127 :     *py = gerepileupto(av, *py);
     825        1127 :     w += e*v;
     826             :   }
     827             :   else
     828       10752 :     *py = gerepilecopy(av, *py);
     829       11879 :   return w;
     830             : }
     831             : GEN
     832         147 : gpnfvalrem(GEN nf, GEN x, GEN pr, GEN *py)
     833             : {
     834         147 :   long v = nfvalrem(nf,x,pr,py);
     835         147 :   return v == LONG_MAX? mkoo(): stoi(v);
     836             : }
     837             : 
     838             : /* true nf */
     839             : GEN
     840      105252 : coltoalg(GEN nf, GEN x)
     841             : {
     842      105252 :   return mkpolmod( nf_to_scalar_or_alg(nf, x), nf_get_pol(nf) );
     843             : }
     844             : 
     845             : GEN
     846      148715 : basistoalg(GEN nf, GEN x)
     847             : {
     848             :   GEN T;
     849             : 
     850      148715 :   nf = checknf(nf);
     851      148715 :   switch(typ(x))
     852             :   {
     853             :     case t_COL: {
     854       99134 :       pari_sp av = avma;
     855       99134 :       return gerepilecopy(av, coltoalg(nf, x));
     856             :     }
     857             :     case t_POLMOD:
     858       32473 :       T = nf_get_pol(nf);
     859       32473 :       if (!RgX_equal_var(T,gel(x,1)))
     860           0 :         pari_err_MODULUS("basistoalg", T,gel(x,1));
     861       32473 :       return gcopy(x);
     862             :     case t_POL:
     863        1778 :       T = nf_get_pol(nf);
     864        1778 :       if (varn(T) != varn(x)) pari_err_VAR("basistoalg",x,T);
     865        1778 :       retmkpolmod(RgX_rem(x, T), ZX_copy(T));
     866             :     case t_INT:
     867             :     case t_FRAC:
     868       15330 :       T = nf_get_pol(nf);
     869       15330 :       retmkpolmod(gcopy(x), ZX_copy(T));
     870             :     default:
     871           0 :       pari_err_TYPE("basistoalg",x);
     872             :       return NULL; /* LCOV_EXCL_LINE */
     873             :   }
     874             : }
     875             : 
     876             : /* true nf, x a t_POL */
     877             : static GEN
     878     1463028 : pol_to_scalar_or_basis(GEN nf, GEN x)
     879             : {
     880     1463028 :   GEN T = nf_get_pol(nf);
     881     1463028 :   long l = lg(x);
     882     1463028 :   if (varn(x) != varn(T)) pari_err_VAR("nf_to_scalar_or_basis", x,T);
     883     1462965 :   if (l >= lg(T)) { x = RgX_rem(x, T); l = lg(x); }
     884     1462965 :   if (l == 2) return gen_0;
     885      867440 :   if (l == 3)
     886             :   {
     887      201026 :     x = gel(x,2);
     888      201026 :     if (!is_rational_t(typ(x))) pari_err_TYPE("nf_to_scalar_or_basis",x);
     889      201019 :     return x;
     890             :   }
     891      666414 :   return poltobasis(nf,x);
     892             : }
     893             : /* Assume nf is a genuine nf. */
     894             : GEN
     895    83522501 : nf_to_scalar_or_basis(GEN nf, GEN x)
     896             : {
     897    83522501 :   switch(typ(x))
     898             :   {
     899             :     case t_INT: case t_FRAC:
     900    62907606 :       return x;
     901             :     case t_POLMOD:
     902      196091 :       x = checknfelt_mod(nf,x,"nf_to_scalar_or_basis");
     903      196035 :       switch(typ(x))
     904             :       {
     905       34258 :         case t_INT: case t_FRAC: return x;
     906      161777 :         case t_POL: return pol_to_scalar_or_basis(nf,x);
     907             :       }
     908           0 :       break;
     909     1301251 :     case t_POL: return pol_to_scalar_or_basis(nf,x);
     910             :     case t_COL:
     911    19117553 :       if (lg(x)-1 != nf_get_degree(nf)) break;
     912    19117490 :       return QV_isscalar(x)? gel(x,1): x;
     913             :   }
     914          63 :   pari_err_TYPE("nf_to_scalar_or_basis",x);
     915             :   return NULL; /* LCOV_EXCL_LINE */
     916             : }
     917             : /* Let x be a polynomial with coefficients in Q or nf. Return the same
     918             :  * polynomial with coefficients expressed as vectors (on the integral basis).
     919             :  * No consistency checks, not memory-clean. */
     920             : GEN
     921        5923 : RgX_to_nfX(GEN nf, GEN x)
     922             : {
     923             :   long i, l;
     924        5923 :   GEN y = cgetg_copy(x, &l); y[1] = x[1];
     925        5923 :   for (i=2; i<l; i++) gel(y,i) = nf_to_scalar_or_basis(nf, gel(x,i));
     926        5923 :   return y;
     927             : }
     928             : 
     929             : /* Assume nf is a genuine nf. */
     930             : GEN
     931      216904 : nf_to_scalar_or_alg(GEN nf, GEN x)
     932             : {
     933      216904 :   switch(typ(x))
     934             :   {
     935             :     case t_INT: case t_FRAC:
     936       17170 :       return x;
     937             :     case t_POLMOD:
     938        1372 :       x = checknfelt_mod(nf,x,"nf_to_scalar_or_alg");
     939        1372 :       if (typ(x) != t_POL) return x;
     940             :       /* fall through */
     941             :     case t_POL:
     942             :     {
     943       14890 :       GEN T = nf_get_pol(nf);
     944       14890 :       long l = lg(x);
     945       14890 :       if (varn(x) != varn(T)) pari_err_VAR("nf_to_scalar_or_alg", x,T);
     946       14890 :       if (l >= lg(T)) { x = RgX_rem(x, T); l = lg(x); }
     947       14890 :       if (l == 2) return gen_0;
     948       14890 :       if (l == 3) return gel(x,2);
     949       14666 :       return x;
     950             :     }
     951             :     case t_COL:
     952             :     {
     953             :       GEN dx;
     954      184788 :       if (lg(x)-1 != nf_get_degree(nf)) break;
     955      369576 :       if (QV_isscalar(x)) return gel(x,1);
     956      148752 :       x = Q_remove_denom(x, &dx);
     957      148752 :       x = RgV_RgC_mul(nf_get_zkprimpart(nf), x);
     958      148752 :       dx = mul_denom(dx, nf_get_zkden(nf));
     959      148752 :       return gdiv(x,dx);
     960             :     }
     961             :   }
     962          49 :   pari_err_TYPE("nf_to_scalar_or_alg",x);
     963             :   return NULL; /* LCOV_EXCL_LINE */
     964             : }
     965             : 
     966             : /* gmul(A, RgX_to_RgC(x)), A t_MAT of compatible dimensions */
     967             : GEN
     968        1337 : RgM_RgX_mul(GEN A, GEN x)
     969             : {
     970        1337 :   long i, l = lg(x)-1;
     971             :   GEN z;
     972        1337 :   if (l == 1) return zerocol(nbrows(A));
     973        1337 :   z = gmul(gel(x,2), gel(A,1));
     974        2541 :   for (i = 2; i < l; i++)
     975        1204 :     if (!gequal0(gel(x,i+1))) z = gadd(z, gmul(gel(x,i+1), gel(A,i)));
     976        1337 :   return z;
     977             : }
     978             : GEN
     979     2483651 : ZM_ZX_mul(GEN A, GEN x)
     980             : {
     981     2483651 :   long i, l = lg(x)-1;
     982             :   GEN z;
     983     2483651 :   if (l == 1) return zerocol(nbrows(A));
     984     2482870 :   z = ZC_Z_mul(gel(A,1), gel(x,2));
     985     9720909 :   for (i = 2; i < l ; i++)
     986     7238043 :     if (signe(gel(x,i+1))) z = ZC_add(z, ZC_Z_mul(gel(A,i), gel(x,i+1)));
     987     2482866 :   return z;
     988             : }
     989             : /* x a t_POL, nf a genuine nf. No garbage collecting. No check.  */
     990             : GEN
     991     2315868 : poltobasis(GEN nf, GEN x)
     992             : {
     993     2315868 :   GEN d, T = nf_get_pol(nf);
     994     2315868 :   if (varn(x) != varn(T)) pari_err_VAR( "poltobasis", x,T);
     995     2315812 :   if (degpol(x) >= degpol(T)) x = RgX_rem(x,T);
     996     2315812 :   x = Q_remove_denom(x, &d);
     997     2315812 :   if (!RgX_is_ZX(x)) pari_err_TYPE("poltobasis",x);
     998     2315791 :   x = ZM_ZX_mul(nf_get_invzk(nf), x);
     999     2315791 :   if (d) x = RgC_Rg_div(x, d);
    1000     2315791 :   return x;
    1001             : }
    1002             : 
    1003             : GEN
    1004      267697 : algtobasis(GEN nf, GEN x)
    1005             : {
    1006             :   pari_sp av;
    1007             : 
    1008      267697 :   nf = checknf(nf);
    1009      267697 :   switch(typ(x))
    1010             :   {
    1011             :     case t_POLMOD:
    1012      112518 :       if (!RgX_equal_var(nf_get_pol(nf),gel(x,1)))
    1013           7 :         pari_err_MODULUS("algtobasis", nf_get_pol(nf),gel(x,1));
    1014      112511 :       x = gel(x,2);
    1015      112511 :       switch(typ(x))
    1016             :       {
    1017             :         case t_INT:
    1018        7497 :         case t_FRAC: return scalarcol(x, nf_get_degree(nf));
    1019             :         case t_POL:
    1020      105014 :           av = avma;
    1021      105014 :           return gerepileupto(av,poltobasis(nf,x));
    1022             :       }
    1023           0 :       break;
    1024             : 
    1025             :     case t_POL:
    1026       73191 :       av = avma;
    1027       73191 :       return gerepileupto(av,poltobasis(nf,x));
    1028             : 
    1029             :     case t_COL:
    1030       14879 :       if (!RgV_is_QV(x)) pari_err_TYPE("nfalgtobasis",x);
    1031       14872 :       if (lg(x)-1 != nf_get_degree(nf)) pari_err_DIM("nfalgtobasis");
    1032       14872 :       return gcopy(x);
    1033             : 
    1034             :     case t_INT:
    1035       67109 :     case t_FRAC: return scalarcol(x, nf_get_degree(nf));
    1036             :   }
    1037           0 :   pari_err_TYPE("algtobasis",x);
    1038             :   return NULL; /* LCOV_EXCL_LINE */
    1039             : }
    1040             : 
    1041             : GEN
    1042       44212 : rnfbasistoalg(GEN rnf,GEN x)
    1043             : {
    1044       44212 :   const char *f = "rnfbasistoalg";
    1045             :   long lx, i;
    1046       44212 :   pari_sp av = avma;
    1047             :   GEN z, nf, relpol, T;
    1048             : 
    1049       44212 :   checkrnf(rnf);
    1050       44212 :   nf = rnf_get_nf(rnf);
    1051       44212 :   T = nf_get_pol(nf);
    1052       44212 :   relpol = QXQX_to_mod_shallow(rnf_get_pol(rnf), T);
    1053       44212 :   switch(typ(x))
    1054             :   {
    1055             :     case t_COL:
    1056         826 :       z = cgetg_copy(x, &lx);
    1057        2478 :       for (i=1; i<lx; i++)
    1058             :       {
    1059        1701 :         GEN c = nf_to_scalar_or_alg(nf, gel(x,i));
    1060        1652 :         if (typ(c) == t_POL) c = mkpolmod(c,T);
    1061        1652 :         gel(z,i) = c;
    1062             :       }
    1063         777 :       z = RgV_RgC_mul(gel(rnf_get_zk(rnf),1), z);
    1064         714 :       return gerepileupto(av, gmodulo(z,relpol));
    1065             : 
    1066             :     case t_POLMOD:
    1067       29715 :       x = polmod_nffix(f, rnf, x, 0);
    1068       29512 :       if (typ(x) != t_POL) break;
    1069       13286 :       retmkpolmod(RgX_copy(x), RgX_copy(relpol));
    1070             :     case t_POL:
    1071        1099 :       if (varn(x) == varn(T)) { RgX_check_QX(x,f); x = gmodulo(x,T); break; }
    1072         875 :       if (varn(x) == varn(relpol))
    1073             :       {
    1074         826 :         x = RgX_nffix(f,nf_get_pol(nf),x,0);
    1075         826 :         return gmodulo(x, relpol);
    1076             :       }
    1077          49 :       pari_err_VAR(f, x,relpol);
    1078             :   }
    1079       28973 :   retmkpolmod(scalarpol(x, varn(relpol)), RgX_copy(relpol));
    1080             : }
    1081             : 
    1082             : GEN
    1083        1589 : matbasistoalg(GEN nf,GEN x)
    1084             : {
    1085             :   long i, j, li, lx;
    1086        1589 :   GEN z = cgetg_copy(x, &lx);
    1087             : 
    1088        1589 :   if (lx == 1) return z;
    1089        1582 :   switch(typ(x))
    1090             :   {
    1091             :     case t_VEC: case t_COL:
    1092          42 :       for (i=1; i<lx; i++) gel(z,i) = basistoalg(nf, gel(x,i));
    1093          42 :       return z;
    1094        1540 :     case t_MAT: break;
    1095           0 :     default: pari_err_TYPE("matbasistoalg",x);
    1096             :   }
    1097        1540 :   li = lgcols(x);
    1098        5726 :   for (j=1; j<lx; j++)
    1099             :   {
    1100        4186 :     GEN c = cgetg(li,t_COL), xj = gel(x,j);
    1101        4186 :     gel(z,j) = c;
    1102        4186 :     for (i=1; i<li; i++) gel(c,i) = basistoalg(nf, gel(xj,i));
    1103             :   }
    1104        1540 :   return z;
    1105             : }
    1106             : 
    1107             : GEN
    1108        3906 : matalgtobasis(GEN nf,GEN x)
    1109             : {
    1110             :   long i, j, li, lx;
    1111        3906 :   GEN z = cgetg_copy(x, &lx);
    1112             : 
    1113        3906 :   if (lx == 1) return z;
    1114        3850 :   switch(typ(x))
    1115             :   {
    1116             :     case t_VEC: case t_COL:
    1117        3843 :       for (i=1; i<lx; i++) gel(z,i) = algtobasis(nf, gel(x,i));
    1118        3843 :       return z;
    1119           7 :     case t_MAT: break;
    1120           0 :     default: pari_err_TYPE("matalgtobasis",x);
    1121             :   }
    1122           7 :   li = lgcols(x);
    1123          14 :   for (j=1; j<lx; j++)
    1124             :   {
    1125           7 :     GEN c = cgetg(li,t_COL), xj = gel(x,j);
    1126           7 :     gel(z,j) = c;
    1127           7 :     for (i=1; i<li; i++) gel(c,i) = algtobasis(nf, gel(xj,i));
    1128             :   }
    1129           7 :   return z;
    1130             : }
    1131             : GEN
    1132        8589 : RgM_to_nfM(GEN nf,GEN x)
    1133             : {
    1134             :   long i, j, li, lx;
    1135        8589 :   GEN z = cgetg_copy(x, &lx);
    1136             : 
    1137        8589 :   if (lx == 1) return z;
    1138        8589 :   li = lgcols(x);
    1139       65779 :   for (j=1; j<lx; j++)
    1140             :   {
    1141       57190 :     GEN c = cgetg(li,t_COL), xj = gel(x,j);
    1142       57190 :     gel(z,j) = c;
    1143       57190 :     for (i=1; i<li; i++) gel(c,i) = nf_to_scalar_or_basis(nf, gel(xj,i));
    1144             :   }
    1145        8589 :   return z;
    1146             : }
    1147             : GEN
    1148       78617 : RgC_to_nfC(GEN nf, GEN x)
    1149       78617 : { pari_APPLY_type(t_COL, nf_to_scalar_or_basis(nf, gel(x,i))) }
    1150             : 
    1151             : /* x a t_POLMOD, supposedly in rnf = K[z]/(T), K = Q[y]/(Tnf) */
    1152             : GEN
    1153      134449 : polmod_nffix(const char *f, GEN rnf, GEN x, int lift)
    1154      134449 : { return polmod_nffix2(f, rnf_get_nfpol(rnf), rnf_get_pol(rnf), x,lift); }
    1155             : GEN
    1156      134540 : polmod_nffix2(const char *f, GEN T, GEN relpol, GEN x, int lift)
    1157             : {
    1158      134540 :   if (RgX_equal_var(gel(x,1),relpol))
    1159             :   {
    1160      124376 :     x = gel(x,2);
    1161      124376 :     if (typ(x) == t_POL && varn(x) == varn(relpol))
    1162             :     {
    1163       94780 :       x = RgX_nffix(f, T, x, lift);
    1164       94780 :       switch(lg(x))
    1165             :       {
    1166         343 :         case 2: return gen_0;
    1167       21931 :         case 3: return gel(x,2);
    1168             :       }
    1169       72506 :       return x;
    1170             :     }
    1171             :   }
    1172       39760 :   return Rg_nffix(f, T, x, lift);
    1173             : }
    1174             : GEN
    1175        1204 : rnfalgtobasis(GEN rnf,GEN x)
    1176             : {
    1177        1204 :   const char *f = "rnfalgtobasis";
    1178        1204 :   pari_sp av = avma;
    1179             :   GEN T, relpol;
    1180             : 
    1181        1204 :   checkrnf(rnf);
    1182        1204 :   relpol = rnf_get_pol(rnf);
    1183        1204 :   T = rnf_get_nfpol(rnf);
    1184        1204 :   switch(typ(x))
    1185             :   {
    1186             :     case t_COL:
    1187          49 :       if (lg(x)-1 != rnf_get_degree(rnf)) pari_err_DIM(f);
    1188          28 :       x = RgV_nffix(f, T, x, 0);
    1189          21 :       return gerepilecopy(av, x);
    1190             : 
    1191             :     case t_POLMOD:
    1192        1071 :       x = polmod_nffix(f, rnf, x, 0);
    1193        1036 :       if (typ(x) != t_POL) break;
    1194         714 :       return gerepileupto(av, RgM_RgX_mul(rnf_get_invzk(rnf), x));
    1195             :     case t_POL:
    1196          56 :       if (varn(x) == varn(T))
    1197             :       {
    1198          21 :         RgX_check_QX(x,f);
    1199          14 :         if (degpol(x) >= degpol(T)) x = RgX_rem(x,T);
    1200          14 :         x = mkpolmod(x,T); break;
    1201             :       }
    1202          35 :       x = RgX_nffix(f, T, x, 0);
    1203          28 :       if (degpol(x) >= degpol(relpol)) x = RgX_rem(x,relpol);
    1204          28 :       return gerepileupto(av, RgM_RgX_mul(rnf_get_invzk(rnf), x));
    1205             :   }
    1206         364 :   return gerepileupto(av, scalarcol(x, rnf_get_degree(rnf)));
    1207             : }
    1208             : 
    1209             : /* Given a and b in nf, gives an algebraic integer y in nf such that a-b.y
    1210             :  * is "small" */
    1211             : GEN
    1212         259 : nfdiveuc(GEN nf, GEN a, GEN b)
    1213             : {
    1214         259 :   pari_sp av = avma;
    1215         259 :   a = nfdiv(nf,a,b);
    1216         259 :   return gerepileupto(av, ground(a));
    1217             : }
    1218             : 
    1219             : /* Given a and b in nf, gives a "small" algebraic integer r in nf
    1220             :  * of the form a-b.y */
    1221             : GEN
    1222         259 : nfmod(GEN nf, GEN a, GEN b)
    1223             : {
    1224         259 :   pari_sp av = avma;
    1225         259 :   GEN p1 = gneg_i(nfmul(nf,b,ground(nfdiv(nf,a,b))));
    1226         259 :   return gerepileupto(av, nfadd(nf,a,p1));
    1227             : }
    1228             : 
    1229             : /* Given a and b in nf, gives a two-component vector [y,r] in nf such
    1230             :  * that r=a-b.y is "small". */
    1231             : GEN
    1232         259 : nfdivrem(GEN nf, GEN a, GEN b)
    1233             : {
    1234         259 :   pari_sp av = avma;
    1235         259 :   GEN p1,z, y = ground(nfdiv(nf,a,b));
    1236             : 
    1237         259 :   p1 = gneg_i(nfmul(nf,b,y));
    1238         259 :   z = cgetg(3,t_VEC);
    1239         259 :   gel(z,1) = gcopy(y);
    1240         259 :   gel(z,2) = nfadd(nf,a,p1); return gerepileupto(av, z);
    1241             : }
    1242             : 
    1243             : /*************************************************************************/
    1244             : /**                                                                     **/
    1245             : /**                        REAL EMBEDDINGS                              **/
    1246             : /**                                                                     **/
    1247             : /*************************************************************************/
    1248             : static GEN
    1249       49406 : sarch_get_cyc(GEN sarch) { return gel(sarch,1); }
    1250             : static GEN
    1251      272411 : sarch_get_archp(GEN sarch) { return gel(sarch,2); }
    1252             : static GEN
    1253       55037 : sarch_get_MI(GEN sarch) { return gel(sarch,3); }
    1254             : static GEN
    1255       55037 : sarch_get_lambda(GEN sarch) { return gel(sarch,4); }
    1256             : static GEN
    1257       55037 : sarch_get_F(GEN sarch) { return gel(sarch,5); }
    1258             : 
    1259             : /* true nf, return number of positive roots of char_x */
    1260             : static long
    1261        1999 : num_positive(GEN nf, GEN x)
    1262             : {
    1263        1999 :   GEN T = nf_get_pol(nf);
    1264        1999 :   GEN charx = ZXQ_charpoly(nf_to_scalar_or_alg(nf,x), T, 0);
    1265             :   long np;
    1266        1999 :   charx = ZX_radical(charx);
    1267        1999 :   np = ZX_sturmpart(charx, mkvec2(gen_0,mkoo()));
    1268        1999 :   return np * (degpol(T) / degpol(charx));
    1269             : }
    1270             : 
    1271             : /* x a QC: return sigma_k(x) where 1 <= k <= r1+r2; correct but inefficient
    1272             :  * if x in Q. M = nf_get_M(nf) */
    1273             : static GEN
    1274          91 : nfembed_i(GEN M, GEN x, long k)
    1275             : {
    1276          91 :   long i, l = lg(M);
    1277          91 :   GEN z = gel(x,1);
    1278          91 :   for (i = 2; i < l; i++) z = gadd(z, gmul(gcoeff(M,k,i), gel(x,i)));
    1279          91 :   return z;
    1280             : }
    1281             : GEN
    1282           0 : nfembed(GEN nf, GEN x, long k)
    1283             : {
    1284           0 :   pari_sp av = avma;
    1285           0 :   nf = checknf(nf);
    1286           0 :   x = nf_to_scalar_or_basis(nf,x);
    1287           0 :   if (typ(x) != t_COL) return gerepilecopy(av, x);
    1288           0 :   return gerepileupto(av, nfembed_i(nf_get_M(nf),x,k));
    1289             : }
    1290             : 
    1291             : /* x a ZC */
    1292             : static GEN
    1293      397055 : zk_embed(GEN M, GEN x, long k)
    1294             : {
    1295      397055 :   long i, l = lg(x);
    1296      397055 :   GEN z = gel(x,1); /* times M[k,1], which is 1 */
    1297      397055 :   for (i = 2; i < l; i++) z = mpadd(z, mpmul(gcoeff(M,k,i), gel(x,i)));
    1298      397055 :   return z;
    1299             : }
    1300             : 
    1301             : /* Given floating point approximation z of sigma_k(x), decide its sign
    1302             :  * [0/+, 1/- and -1 for FAIL] */
    1303             : static long
    1304      386408 : eval_sign_embed(GEN z)
    1305             : { /* dubious, fail */
    1306      386408 :   if (typ(z) == t_REAL && realprec(z) <= LOWDEFAULTPREC) return -1;
    1307      385091 :   return (signe(z) < 1)? 1: 0;
    1308             : }
    1309             : /* return v such that (-1)^v = sign(sigma_k(x)), x primitive ZC */
    1310             : static long
    1311      313454 : eval_sign(GEN M, GEN x, long k)
    1312      313454 : { return eval_sign_embed( zk_embed(M, x, k) ); }
    1313             : 
    1314             : /* check that signs[i..#signs] == s; signs = NULL encodes "totally positive" */
    1315             : static int
    1316           0 : oksigns(long l, GEN signs, long i, long s)
    1317             : {
    1318           0 :   if (!signs) return s == 0;
    1319           0 :   for (; i < l; i++)
    1320           0 :     if (signs[i] != s) return 0;
    1321           0 :   return 1;
    1322             : }
    1323             : /* check that signs[i] = s and signs[i+1..#signs] = 1-s */
    1324             : static int
    1325           0 : oksigns2(long l, GEN signs, long i, long s)
    1326             : {
    1327           0 :   if (!signs) return s == 0 && i == l-1;
    1328           0 :   return signs[i] == s && oksigns(l, signs, i+1, 1-s);
    1329             : }
    1330             : 
    1331             : /* true nf, x a ZC (primitive for efficiency), embx its embeddings or NULL */
    1332             : static int
    1333       63602 : nfchecksigns_i(GEN nf, GEN x, GEN embx, GEN signs, GEN archp)
    1334             : {
    1335       63602 :   long l = lg(archp), i;
    1336       63602 :   GEN M = nf_get_M(nf), sarch = NULL;
    1337       63602 :   long np = -1;
    1338       95333 :   for (i = 1; i < l; i++)
    1339             :   {
    1340             :     long s;
    1341       73423 :     if (embx)
    1342       72954 :       s = eval_sign_embed(gel(embx,i));
    1343             :     else
    1344         469 :       s = eval_sign(M, x, archp[i]);
    1345             :     /* 0 / + or 1 / -; -1 for FAIL */
    1346       73423 :     if (s < 0) /* failure */
    1347             :     {
    1348           0 :       long ni, r1 = nf_get_r1(nf);
    1349             :       GEN xi;
    1350           0 :       if (np < 0)
    1351             :       {
    1352           0 :         np = num_positive(nf, x);
    1353           0 :         if (np == 0)  return oksigns(l, signs, i, 1);
    1354           0 :         if (np == r1) return oksigns(l, signs, i, 0);
    1355           0 :         sarch = nfarchstar(nf, NULL, identity_perm(r1));
    1356             :       }
    1357           0 :       xi = set_sign_mod_divisor(nf, vecsmall_ei(r1, archp[i]), gen_1, sarch);
    1358           0 :       xi = Q_primpart(xi);
    1359           0 :       ni = num_positive(nf, nfmuli(nf,x,xi));
    1360           0 :       if (ni == 0)  return oksigns2(l, signs, i, 0);
    1361           0 :       if (ni == r1) return oksigns2(l, signs, i, 1);
    1362           0 :       s = ni < np? 0: 1;
    1363             :     }
    1364       73423 :     if (s != (signs? signs[i]: 0)) return 0;
    1365             :   }
    1366       21910 :   return 1;
    1367             : }
    1368             : static void
    1369         343 : pl_convert(GEN pl, GEN *psigns, GEN *parchp)
    1370             : {
    1371         343 :   long i, j, l = lg(pl);
    1372         343 :   GEN signs = cgetg(l, t_VECSMALL);
    1373         343 :   GEN archp = cgetg(l, t_VECSMALL);
    1374        1092 :   for (i = j = 1; i < l; i++)
    1375             :   {
    1376         749 :     if (!pl[i]) continue;
    1377         511 :     archp[j] = i;
    1378         511 :     signs[j] = (pl[i] < 0)? 1: 0;
    1379         511 :     j++;
    1380             :   }
    1381         343 :   setlg(archp, j); *parchp = archp;
    1382         343 :   setlg(signs, j); *psigns = signs;
    1383         343 : }
    1384             : /* pl : requested signs for real embeddings, 0 = no sign constraint */
    1385             : int
    1386         861 : nfchecksigns(GEN nf, GEN x, GEN pl)
    1387             : {
    1388         861 :   pari_sp av = avma;
    1389             :   GEN signs, archp;
    1390         861 :   nf = checknf(nf);
    1391         861 :   x = nf_to_scalar_or_basis(nf,x);
    1392         861 :   if (typ(x) != t_COL)
    1393             :   {
    1394         518 :     long i, l = lg(pl), s = gsigne(x);
    1395        1050 :     for (i = 1; i < l; i++)
    1396         532 :       if (pl[i] && pl[i] != s) return gc_bool(av,0);
    1397         518 :     return gc_bool(av,1);
    1398             :   }
    1399         343 :   pl_convert(pl, &signs, &archp);
    1400         343 :   return gc_bool(av, nfchecksigns_i(nf, x, NULL, signs, archp));
    1401             : }
    1402             : 
    1403             : /* signs = NULL: totally positive, else sign[i] = 0 (+) or 1 (-) */
    1404             : static GEN
    1405       55037 : get_C(GEN lambda, long l, GEN signs)
    1406             : {
    1407             :   long i;
    1408             :   GEN C, mlambda;
    1409       55037 :   if (!signs) return const_vec(l-1, lambda);
    1410       16194 :   C = cgetg(l, t_COL); mlambda = gneg(lambda);
    1411       16194 :   for (i = 1; i < l; i++) gel(C,i) = signs[i]? mlambda: lambda;
    1412       16194 :   return C;
    1413             : }
    1414             : /* signs = NULL: totally positive at archp */
    1415             : static GEN
    1416       94230 : nfsetsigns(GEN nf, GEN signs, GEN x, GEN sarch)
    1417             : {
    1418       94230 :   long i, l = lg(sarch_get_archp(sarch));
    1419             :   GEN ex;
    1420             :   /* Is signature already correct ? */
    1421       94230 :   if (typ(x) != t_COL)
    1422             :   {
    1423       30971 :     long s = gsigne(x);
    1424       30971 :     if (!s) i = 1;
    1425       30957 :     else if (!signs)
    1426        3605 :       i = (s < 0)? 1: l;
    1427             :     else
    1428             :     {
    1429       27352 :       s = s < 0? 1: 0;
    1430       42990 :       for (i = 1; i < l; i++)
    1431       28941 :         if (signs[i] != s) break;
    1432             :     }
    1433       30971 :     ex = (i < l)? const_col(l-1, x): NULL;
    1434             :   }
    1435             :   else
    1436             :   {
    1437       63259 :     pari_sp av = avma;
    1438       63259 :     GEN cex, M = nf_get_M(nf), archp = sarch_get_archp(sarch);
    1439       63259 :     GEN xp = Q_primitive_part(x,&cex);
    1440       63259 :     ex = cgetg(l,t_COL);
    1441       63259 :     for (i = 1; i < l; i++) gel(ex,i) = zk_embed(M,xp,archp[i]);
    1442       63259 :     if (nfchecksigns_i(nf, xp, ex, signs, archp)) { ex = NULL; set_avma(av); }
    1443       41650 :     else if (cex) ex = RgC_Rg_mul(ex, cex); /* put back content */
    1444             :   }
    1445       94230 :   if (ex)
    1446             :   { /* If no, fix it */
    1447       55037 :     GEN MI = sarch_get_MI(sarch), F = sarch_get_F(sarch);
    1448       55037 :     GEN lambda = sarch_get_lambda(sarch);
    1449       55037 :     GEN t = RgC_sub(get_C(lambda, l, signs), ex);
    1450             :     long e;
    1451       55037 :     t = grndtoi(RgM_RgC_mul(MI,t), &e);
    1452       55037 :     if (lg(F) != 1) t = ZM_ZC_mul(F, t);
    1453       55037 :     x = typ(x) == t_COL? RgC_add(t, x): RgC_Rg_add(t, x);
    1454             :   }
    1455       94230 :   return x;
    1456             : }
    1457             : /* - sarch = nfarchstar(nf, F);
    1458             :  * - x encodes a vector of signs at arch.archp: either a t_VECSMALL
    1459             :  *   (vector of signs as {0,1}-vector), NULL (totally positive at archp),
    1460             :  *   or a non-zero number field element (replaced by its signature at archp);
    1461             :  * - y is a non-zero number field element
    1462             :  * Return z = y (mod F) with signs(y, archp) = signs(x) (a {0,1}-vector) */
    1463             : GEN
    1464      114922 : set_sign_mod_divisor(GEN nf, GEN x, GEN y, GEN sarch)
    1465             : {
    1466      114922 :   GEN archp = sarch_get_archp(sarch);
    1467      114922 :   if (lg(archp) == 1) return y;
    1468       92872 :   nf = checknf(nf);
    1469       92872 :   if (x && typ(x) != t_VECSMALL) x = nfsign_arch(nf, x, archp);
    1470       92872 :   y = nf_to_scalar_or_basis(nf,y);
    1471       92872 :   return nfsetsigns(nf, x, y, sarch);
    1472             : }
    1473             : 
    1474             : static GEN
    1475       14661 : setsigns_init(GEN nf, GEN archp, GEN F, GEN DATA)
    1476             : {
    1477       14661 :   GEN lambda, Mr = rowpermute(nf_get_M(nf), archp), MI = F? RgM_mul(Mr,F): Mr;
    1478       14661 :   lambda = gmul2n(matrixnorm(MI,DEFAULTPREC), -1);
    1479       14661 :   if (typ(lambda) != t_REAL) lambda = gmul(lambda, sstoQ(1001,1000));
    1480       14661 :   if (lg(archp) < lg(MI))
    1481             :   {
    1482       12558 :     GEN perm = gel(indexrank(MI), 2);
    1483       12558 :     if (!F) F = matid(nf_get_degree(nf));
    1484       12558 :     MI = vecpermute(MI, perm);
    1485       12558 :     F = vecpermute(F, perm);
    1486             :   }
    1487       14661 :   if (!F) F = cgetg(1,t_MAT);
    1488       14661 :   MI = RgM_inv(MI);
    1489       14661 :   return mkvec5(DATA, archp, MI, lambda, F);
    1490             : }
    1491             : /* F non-0 integral ideal in HNF (or NULL: Z_K), compute elements in 1+F
    1492             :  * whose sign matrix at archp is identity; archp in 'indices' format */
    1493             : GEN
    1494       28416 : nfarchstar(GEN nf, GEN F, GEN archp)
    1495             : {
    1496       28416 :   long nba = lg(archp) - 1;
    1497       28416 :   if (!nba) return mkvec2(cgetg(1,t_VEC), archp);
    1498       13310 :   if (F && equali1(gcoeff(F,1,1))) F = NULL;
    1499       13310 :   if (F) F = idealpseudored(F, nf_get_roundG(nf));
    1500       13310 :   return setsigns_init(nf, archp, F, const_vec(nba, gen_2));
    1501             : }
    1502             : 
    1503             : /*************************************************************************/
    1504             : /**                                                                     **/
    1505             : /**                         IDEALCHINESE                                **/
    1506             : /**                                                                     **/
    1507             : /*************************************************************************/
    1508             : static int
    1509        2989 : isprfact(GEN x)
    1510             : {
    1511             :   long i, l;
    1512             :   GEN L, E;
    1513        2989 :   if (typ(x) != t_MAT || lg(x) != 3) return 0;
    1514        2989 :   L = gel(x,1); l = lg(L);
    1515        2989 :   E = gel(x,2);
    1516        7168 :   for(i=1; i<l; i++)
    1517             :   {
    1518        4179 :     checkprid(gel(L,i));
    1519        4179 :     if (typ(gel(E,i)) != t_INT) return 0;
    1520             :   }
    1521        2989 :   return 1;
    1522             : }
    1523             : 
    1524             : /* initialize projectors mod pr[i]^e[i] for idealchinese */
    1525             : static GEN
    1526        2989 : pr_init(GEN nf, GEN fa, GEN w, GEN dw)
    1527             : {
    1528        2989 :   GEN U, E, F, L = gel(fa,1), E0 = gel(fa,2);
    1529        2989 :   long i, r = lg(L);
    1530             : 
    1531        2989 :   if (w && lg(w) != r) pari_err_TYPE("idealchinese", w);
    1532        2989 :   if (r == 1 && !dw) return cgetg(1,t_VEC);
    1533        2982 :   E = leafcopy(E0); /* do not destroy fa[2] */
    1534        7161 :   for (i = 1; i < r; i++)
    1535        4179 :     if (signe(gel(E,i)) < 0) gel(E,i) = gen_0;
    1536        2982 :   F = factorbackprime(nf, L, E);
    1537        2982 :   if (dw)
    1538             :   {
    1539         693 :     F = ZM_Z_mul(F, dw);
    1540        1582 :     for (i = 1; i < r; i++)
    1541             :     {
    1542         889 :       GEN pr = gel(L,i);
    1543         889 :       long e = itos(gel(E0,i)), v = idealval(nf, dw, pr);
    1544         889 :       if (e >= 0)
    1545         882 :         gel(E,i) = addiu(gel(E,i), v);
    1546           7 :       else if (v + e <= 0)
    1547           0 :         F = idealmulpowprime(nf, F, pr, stoi(-v)); /* coprime to pr */
    1548             :       else
    1549             :       {
    1550           7 :         F = idealmulpowprime(nf, F, pr, stoi(e));
    1551           7 :         gel(E,i) = stoi(v + e);
    1552             :       }
    1553             :     }
    1554             :   }
    1555        2982 :   U = cgetg(r, t_VEC);
    1556        7161 :   for (i = 1; i < r; i++)
    1557             :   {
    1558             :     GEN u;
    1559        4179 :     if (w && gequal0(gel(w,i))) u = gen_0; /* unused */
    1560             :     else
    1561             :     {
    1562        4102 :       GEN pr = gel(L,i), e = gel(E,i), t;
    1563        4102 :       t = idealdivpowprime(nf,F, pr, e);
    1564        4102 :       u = hnfmerge_get_1(t, idealpow(nf, pr, e));
    1565        4102 :       if (!u) pari_err_COPRIME("idealchinese", t,pr);
    1566             :     }
    1567        4179 :     gel(U,i) = u;
    1568             :   }
    1569        2982 :   F = idealpseudored(F, nf_get_roundG(nf));
    1570        2982 :   return mkvec2(F, U);
    1571             : }
    1572             : 
    1573             : static GEN
    1574        1771 : pl_normalize(GEN nf, GEN pl)
    1575             : {
    1576        1771 :   const char *fun = "idealchinese";
    1577        1771 :   if (lg(pl)-1 != nf_get_r1(nf)) pari_err_TYPE(fun,pl);
    1578        1771 :   switch(typ(pl))
    1579             :   {
    1580         707 :     case t_VEC: RgV_check_ZV(pl,fun); pl = ZV_to_zv(pl);
    1581             :       /* fall through */
    1582        1771 :     case t_VECSMALL: break;
    1583           0 :     default: pari_err_TYPE(fun,pl);
    1584             :   }
    1585        1771 :   return pl;
    1586             : }
    1587             : 
    1588             : static int
    1589        7091 : is_chineseinit(GEN x)
    1590             : {
    1591             :   GEN fa, pl;
    1592             :   long l;
    1593        7091 :   if (typ(x) != t_VEC || lg(x)!=3) return 0;
    1594        5425 :   fa = gel(x,1);
    1595        5425 :   pl = gel(x,2);
    1596        5425 :   if (typ(fa) != t_VEC || typ(pl) != t_VEC) return 0;
    1597        2681 :   l = lg(fa);
    1598        2681 :   if (l != 1)
    1599             :   {
    1600        2660 :     if (l != 3 || typ(gel(fa,1)) != t_MAT || typ(gel(fa,2)) != t_VEC)
    1601           7 :       return 0;
    1602             :   }
    1603        2674 :   l = lg(pl);
    1604        2674 :   if (l != 1)
    1605             :   {
    1606         511 :     if (l != 6 || typ(gel(pl,3)) != t_MAT || typ(gel(pl,1)) != t_VECSMALL
    1607         511 :                                           || typ(gel(pl,2)) != t_VECSMALL)
    1608           0 :       return 0;
    1609             :   }
    1610        2674 :   return 1;
    1611             : }
    1612             : 
    1613             : /* nf a true 'nf' */
    1614             : static GEN
    1615        3122 : chineseinit_i(GEN nf, GEN fa, GEN w, GEN dw)
    1616             : {
    1617        3122 :   const char *fun = "idealchineseinit";
    1618        3122 :   GEN archp = NULL, pl = NULL;
    1619        3122 :   switch(typ(fa))
    1620             :   {
    1621             :     case t_VEC:
    1622        1771 :       if (is_chineseinit(fa))
    1623             :       {
    1624           0 :         if (dw) pari_err_DOMAIN(fun, "denom(y)", "!=", gen_1, w);
    1625           0 :         return fa;
    1626             :       }
    1627        1771 :       if (lg(fa) != 3) pari_err_TYPE(fun, fa);
    1628             :       /* of the form [x,s] */
    1629        1771 :       pl = pl_normalize(nf, gel(fa,2));
    1630        1771 :       fa = gel(fa,1);
    1631        1771 :       archp = vecsmall01_to_indices(pl);
    1632             :       /* keep pr_init, reset pl */
    1633        1771 :       if (is_chineseinit(fa)) { fa = gel(fa,1); break; }
    1634             :       /* fall through */
    1635             :     case t_MAT: /* factorization? */
    1636        2989 :       if (isprfact(fa)) { fa = pr_init(nf, fa, w, dw); break; }
    1637           0 :     default: pari_err_TYPE(fun,fa);
    1638             :   }
    1639             : 
    1640        3122 :   if (!pl) pl = cgetg(1,t_VEC);
    1641             :   else
    1642             :   {
    1643        1771 :     long r = lg(archp);
    1644        1771 :     if (r == 1) pl = cgetg(1, t_VEC);
    1645             :     else
    1646             :     {
    1647        1351 :       GEN F = (lg(fa) == 1)? NULL: gel(fa,1), signs = cgetg(r, t_VECSMALL);
    1648             :       long i;
    1649        1351 :       for (i = 1; i < r; i++) signs[i] = (pl[archp[i]] < 0)? 1: 0;
    1650        1351 :       pl = setsigns_init(nf, archp, F, signs);
    1651             :     }
    1652             :   }
    1653        3122 :   return mkvec2(fa, pl);
    1654             : }
    1655             : 
    1656             : /* Given a prime ideal factorization x, possibly with 0 or negative exponents,
    1657             :  * and a vector w of elements of nf, gives b such that
    1658             :  * v_p(b-w_p)>=v_p(x) for all prime ideals p in the ideal factorization
    1659             :  * and v_p(b)>=0 for all other p, using the standard proof given in GTM 138. */
    1660             : GEN
    1661        5663 : idealchinese(GEN nf, GEN x, GEN w)
    1662             : {
    1663        5663 :   const char *fun = "idealchinese";
    1664        5663 :   pari_sp av = avma;
    1665             :   GEN x1, x2, s, dw, F;
    1666             : 
    1667        5663 :   nf = checknf(nf);
    1668        5663 :   if (!w) return gerepilecopy(av, chineseinit_i(nf,x,NULL,NULL));
    1669             : 
    1670        3549 :   if (typ(w) != t_VEC) pari_err_TYPE(fun,w);
    1671        3549 :   w = Q_remove_denom(matalgtobasis(nf,w), &dw);
    1672        3549 :   if (!is_chineseinit(x)) x = chineseinit_i(nf,x,w,dw);
    1673             :   /* x is a 'chineseinit' */
    1674        3549 :   x1 = gel(x,1); s = NULL;
    1675        3549 :   x2 = gel(x,2);
    1676        3549 :   if (lg(x1) == 1) F = NULL;
    1677             :   else
    1678             :   {
    1679        3528 :     GEN  U = gel(x1,2);
    1680        3528 :     long i, r = lg(w);
    1681        3528 :     F = gel(x1,1);
    1682       10115 :     for (i=1; i<r; i++)
    1683        6587 :       if (!gequal0(gel(w,i)))
    1684             :       {
    1685        4123 :         GEN t = nfmuli(nf, gel(U,i), gel(w,i));
    1686        4123 :         s = s? ZC_add(s,t): t;
    1687             :       }
    1688        3528 :     if (s) s = ZC_reducemodmatrix(s, F);
    1689             :   }
    1690        3549 :   if (lg(x2) != 1) s = nfsetsigns(nf, gel(x2,1), s? s: gen_0, x2);
    1691        3549 :   if (!s) { s = zerocol(nf_get_degree(nf)); dw = NULL; }
    1692             : 
    1693        3549 :   if (dw) s = RgC_Rg_div(s,dw);
    1694        3549 :   return gerepileupto(av, s);
    1695             : }
    1696             : 
    1697             : /*************************************************************************/
    1698             : /**                                                                     **/
    1699             : /**                           (Z_K/I)^*                                 **/
    1700             : /**                                                                     **/
    1701             : /*************************************************************************/
    1702             : GEN
    1703        1771 : vecsmall01_to_indices(GEN v)
    1704             : {
    1705        1771 :   long i, k, l = lg(v);
    1706        1771 :   GEN p = new_chunk(l) + l;
    1707        4753 :   for (k=1, i=l-1; i; i--)
    1708        2982 :     if (v[i]) { *--p = i; k++; }
    1709        1771 :   *--p = evallg(k) | evaltyp(t_VECSMALL);
    1710        1771 :   avma = (pari_sp)p; return p;
    1711             : }
    1712             : GEN
    1713      363615 : vec01_to_indices(GEN v)
    1714             : {
    1715             :   long i, k, l;
    1716             :   GEN p;
    1717             : 
    1718      363615 :   switch (typ(v))
    1719             :   {
    1720      349377 :    case t_VECSMALL: return v;
    1721       14238 :    case t_VEC: break;
    1722           0 :    default: pari_err_TYPE("vec01_to_indices",v);
    1723             :   }
    1724       14238 :   l = lg(v);
    1725       14238 :   p = new_chunk(l) + l;
    1726       41615 :   for (k=1, i=l-1; i; i--)
    1727       27377 :     if (signe(gel(v,i))) { *--p = i; k++; }
    1728       14238 :   *--p = evallg(k) | evaltyp(t_VECSMALL);
    1729       14238 :   avma = (pari_sp)p; return p;
    1730             : }
    1731             : GEN
    1732        5159 : indices_to_vec01(GEN p, long r)
    1733             : {
    1734        5159 :   long i, l = lg(p);
    1735        5159 :   GEN v = zerovec(r);
    1736        5159 :   for (i = 1; i < l; i++) gel(v, p[i]) = gen_1;
    1737        5159 :   return v;
    1738             : }
    1739             : 
    1740             : /* return (column) vector of R1 signatures of x (0 or 1) */
    1741             : GEN
    1742      349377 : nfsign_arch(GEN nf, GEN x, GEN arch)
    1743             : {
    1744      349377 :   GEN sarch, M, V, archp = vec01_to_indices(arch);
    1745      349377 :   long i, s, np, n = lg(archp)-1;
    1746             :   pari_sp av;
    1747             : 
    1748      349377 :   if (!n) return cgetg(1,t_VECSMALL);
    1749      348348 :   nf = checknf(nf);
    1750      348348 :   if (typ(x) == t_MAT)
    1751             :   { /* factorisation */
    1752       99418 :     GEN g = gel(x,1), e = gel(x,2);
    1753       99418 :     V = zero_zv(n);
    1754      289007 :     for (i=1; i<lg(g); i++)
    1755      189589 :       if (mpodd(gel(e,i)))
    1756      164214 :         Flv_add_inplace(V, nfsign_arch(nf,gel(g,i),archp), 2);
    1757       99418 :     avma = (pari_sp)V; return V;
    1758             :   }
    1759      248930 :   av = avma; V = cgetg(n+1,t_VECSMALL);
    1760      248930 :   x = nf_to_scalar_or_basis(nf, x);
    1761      248930 :   switch(typ(x))
    1762             :   {
    1763             :     case t_INT:
    1764       65041 :       s = signe(x);
    1765       65041 :       if (!s) pari_err_DOMAIN("nfsign_arch","element","=",gen_0,x);
    1766       65041 :       set_avma(av); return const_vecsmall(n, (s < 0)? 1: 0);
    1767             :     case t_FRAC:
    1768          35 :       s = signe(gel(x,1));
    1769          35 :       set_avma(av); return const_vecsmall(n, (s < 0)? 1: 0);
    1770             :   }
    1771      183854 :   x = Q_primpart(x); M = nf_get_M(nf); sarch = NULL; np = -1;
    1772      495522 :   for (i = 1; i <= n; i++)
    1773             :   {
    1774      312985 :     long s = eval_sign(M, x, archp[i]);
    1775      312985 :     if (s < 0) /* failure */
    1776             :     {
    1777        1317 :       long ni, r1 = nf_get_r1(nf);
    1778             :       GEN xi;
    1779        1317 :       if (np < 0)
    1780             :       {
    1781        1317 :         np = num_positive(nf, x);
    1782        1317 :         if (np == 0) { set_avma(av); return const_vecsmall(n, 1); }
    1783        1179 :         if (np == r1){ set_avma(av); return const_vecsmall(n, 0); }
    1784         682 :         sarch = nfarchstar(nf, NULL, identity_perm(r1));
    1785             :       }
    1786         682 :       xi = set_sign_mod_divisor(nf, vecsmall_ei(r1, archp[i]), gen_1, sarch);
    1787         682 :       xi = Q_primpart(xi);
    1788         682 :       ni = num_positive(nf, nfmuli(nf,x,xi));
    1789         682 :       if (ni == 0) { set_avma(av); V = const_vecsmall(n, 1); V[i] = 0; return V; }
    1790         544 :       if (ni == r1){ set_avma(av); V = const_vecsmall(n, 0); V[i] = 1; return V; }
    1791           0 :       s = ni < np? 0: 1;
    1792             :     }
    1793      311668 :     V[i] = s;
    1794             :   }
    1795      182537 :   avma = (pari_sp)V; return V;
    1796             : }
    1797             : static void
    1798        6797 : chk_ind(const char *s, long i, long r1)
    1799             : {
    1800        6797 :   if (i <= 0) pari_err_DOMAIN(s, "index", "<=", gen_0, stoi(i));
    1801        6783 :   if (i > r1) pari_err_DOMAIN(s, "index", ">", utoi(r1), utoi(i));
    1802        6748 : }
    1803             : static GEN
    1804        6286 : parse_embed(GEN ind, long r, const char *f)
    1805             : {
    1806             :   long l, i;
    1807        6286 :   if (!ind) return identity_perm(r);
    1808        4823 :   switch(typ(ind))
    1809             :   {
    1810         861 :     case t_INT: case t_VEC: case t_COL: ind = gtovecsmall(ind); break;
    1811        3962 :     case t_VECSMALL: break;
    1812           0 :     default: pari_err_TYPE(f, ind);
    1813             :   }
    1814        4823 :   l = lg(ind);
    1815        4823 :   for (i = 1; i < l; i++) chk_ind(f, ind[i], r);
    1816        4774 :   return ind;
    1817             : }
    1818             : GEN
    1819        4732 : nfeltsign(GEN nf, GEN x, GEN ind0)
    1820             : {
    1821        4732 :   pari_sp av = avma;
    1822             :   long i, l, r1;
    1823             :   GEN v, ind;
    1824        4732 :   nf = checknf(nf); r1 = nf_get_r1(nf);
    1825        4732 :   x = nf_to_scalar_or_basis(nf, x);
    1826        4732 :   ind = parse_embed(ind0, r1, "nfeltsign");
    1827        4711 :   l = lg(ind);
    1828        4711 :   if (typ(x) != t_COL)
    1829             :   {
    1830             :     GEN s;
    1831        2163 :     switch(gsigne(x))
    1832             :     {
    1833         532 :       case -1:s = gen_m1; break;
    1834        1624 :       case 1: s = gen_1; break;
    1835           7 :       default: s = gen_0; break;
    1836             :     }
    1837        2163 :     set_avma(av);
    1838        2163 :     return (ind0 && typ(ind0) == t_INT)? s: const_vec(l-1, s);
    1839             :   }
    1840        2548 :   v = nfsign_arch(nf, x, ind);
    1841        2548 :   if (ind0 && typ(ind0) == t_INT) { set_avma(av); return v[1]? gen_m1: gen_1; }
    1842        2541 :   settyp(v, t_VEC);
    1843        2541 :   for (i = 1; i < l; i++) gel(v,i) = v[i]? gen_m1: gen_1;
    1844        2541 :   return gerepileupto(av, v);
    1845             : }
    1846             : 
    1847             : GEN
    1848          63 : nfeltembed(GEN nf, GEN x, GEN ind0, long prec0)
    1849             : {
    1850          63 :   pari_sp av = avma;
    1851             :   long i, e, l, r1, r2, prec, prec1;
    1852             :   GEN v, ind, cx;
    1853          63 :   nf = checknf(nf); nf_get_sign(nf,&r1,&r2);
    1854          63 :   x = nf_to_scalar_or_basis(nf, x);
    1855          56 :   ind = parse_embed(ind0, r1+r2, "nfeltembed");
    1856          49 :   l = lg(ind);
    1857          49 :   if (typ(x) != t_COL)
    1858             :   {
    1859           0 :     if (!(ind0 && typ(ind0) == t_INT)) x = const_vec(l-1, x);
    1860           0 :     return gerepilecopy(av, x);
    1861             :   }
    1862          49 :   x = Q_primitive_part(x, &cx);
    1863          49 :   prec1 = prec0; e = gexpo(x);
    1864          49 :   if (e > 8) prec1 += nbits2extraprec(e);
    1865          49 :   prec = prec1;
    1866          49 :   if (nf_get_prec(nf) < prec) nf = nfnewprec_shallow(nf, prec);
    1867          49 :   v = cgetg(l, t_VEC);
    1868             :   for(;;)
    1869           7 :   {
    1870          56 :     GEN M = nf_get_M(nf);
    1871         140 :     for (i = 1; i < l; i++)
    1872             :     {
    1873          91 :       GEN t = nfembed_i(M, x, ind[i]);
    1874          91 :       long e = gexpo(t);
    1875          91 :       if (gequal0(t) || precision(t) < prec0
    1876          91 :                      || (e < 0 && prec < prec1 + nbits2extraprec(-e)) ) break;
    1877          84 :       if (cx) t = gmul(t, cx);
    1878          84 :       gel(v,i) = t;
    1879             :     }
    1880          56 :     if (i == l) break;
    1881           7 :     prec = precdbl(prec);
    1882           7 :     if (DEBUGLEVEL>1) pari_warn(warnprec,"eltnfembed", prec);
    1883           7 :     nf = nfnewprec_shallow(nf, prec);
    1884             :   }
    1885          49 :   if (ind0 && typ(ind0) == t_INT) v = gel(v,1);
    1886          49 :   return gerepilecopy(av, v);
    1887             : }
    1888             : 
    1889             : /* number of distinct roots of sigma(f) */
    1890             : GEN
    1891        1498 : nfpolsturm(GEN nf, GEN f, GEN ind0)
    1892             : {
    1893        1498 :   pari_sp av = avma;
    1894             :   long d, l, r1, single;
    1895             :   GEN ind, u, v, vr1, T, s, t;
    1896             : 
    1897        1498 :   nf = checknf(nf); T = nf_get_pol(nf); r1 = nf_get_r1(nf);
    1898        1498 :   ind = parse_embed(ind0, r1, "nfpolsturm");
    1899        1477 :   single = ind0 && typ(ind0) == t_INT;
    1900        1477 :   l = lg(ind);
    1901             : 
    1902        1477 :   if (gequal0(f)) pari_err_ROOTS0("nfpolsturm");
    1903        1470 :   if (typ(f) == t_POL && varn(f) != varn(T))
    1904             :   {
    1905        1449 :     f = RgX_nffix("nfsturn", T, f,1);
    1906        1449 :     if (lg(f) == 3) f = NULL;
    1907             :   }
    1908             :   else
    1909             :   {
    1910          21 :     (void)Rg_nffix("nfpolsturm", T, f, 0);
    1911          21 :     f = NULL;
    1912             :   }
    1913        1470 :   if (!f) { set_avma(av); return single? gen_0: zerovec(l-1); }
    1914        1449 :   d = degpol(f);
    1915        1449 :   if (d == 1) { set_avma(av); return single? gen_1: const_vec(l-1,gen_1); }
    1916             : 
    1917        1428 :   vr1 = const_vecsmall(l-1, 1);
    1918        1428 :   u = Q_primpart(f); s = ZV_to_zv(nfeltsign(nf, gel(u,d+2), ind));
    1919        1428 :   v = RgX_deriv(u); t = odd(d)? leafcopy(s): zv_neg(s);
    1920             :   for(;;)
    1921         154 :   {
    1922        1582 :     GEN r = RgX_neg( Q_primpart(RgX_pseudorem(u, v)) ), sr;
    1923        1582 :     long i, dr = degpol(r);
    1924        1582 :     if (dr < 0) break;
    1925        1582 :     sr = ZV_to_zv(nfeltsign(nf, gel(r,dr+2), ind));
    1926        3941 :     for (i = 1; i < l; i++)
    1927        2359 :       if (sr[i] != s[i]) { s[i] = sr[i], vr1[i]--; }
    1928        1582 :     if (odd(dr)) sr = zv_neg(sr);
    1929        3941 :     for (i = 1; i < l; i++)
    1930        2359 :       if (sr[i] != t[i]) { t[i] = sr[i], vr1[i]++; }
    1931        1582 :     if (!dr) break;
    1932         154 :     u = v; v = r;
    1933             :   }
    1934        1428 :   if (single) { set_avma(av); return stoi(vr1[1]); }
    1935         721 :   return gerepileupto(av, zv_to_ZV(vr1));
    1936             : }
    1937             : 
    1938             : 
    1939             : /* return the vector of signs of x; the matrix of such if x is a vector
    1940             :  * of nf elements */
    1941             : GEN
    1942        1456 : nfsign(GEN nf, GEN x)
    1943             : {
    1944             :   long i, l;
    1945             :   GEN archp, S;
    1946             : 
    1947        1456 :   nf = checknf(nf);
    1948        1456 :   archp = identity_perm( nf_get_r1(nf) );
    1949        1456 :   if (typ(x) != t_VEC) return nfsign_arch(nf, x, archp);
    1950         252 :   l = lg(x); S = cgetg(l, t_MAT);
    1951         252 :   for (i=1; i<l; i++) gel(S,i) = nfsign_arch(nf, gel(x,i), archp);
    1952         252 :   return S;
    1953             : }
    1954             : 
    1955             : /* x integral elt, A integral ideal in HNF; reduce x mod A */
    1956             : static GEN
    1957      604002 : zk_modHNF(GEN x, GEN A)
    1958      604002 : { return (typ(x) == t_COL)?  ZC_hnfrem(x, A): modii(x, gcoeff(A,1,1)); }
    1959             : 
    1960             : /* given an element x in Z_K and an integral ideal y in HNF, coprime with x,
    1961             :    outputs an element inverse of x modulo y */
    1962             : GEN
    1963         154 : nfinvmodideal(GEN nf, GEN x, GEN y)
    1964             : {
    1965         154 :   pari_sp av = avma;
    1966         154 :   GEN a, yZ = gcoeff(y,1,1);
    1967             : 
    1968         154 :   if (equali1(yZ)) return gen_0;
    1969         154 :   x = nf_to_scalar_or_basis(nf, x);
    1970         154 :   if (typ(x) == t_INT) return gerepileupto(av, Fp_inv(x, yZ));
    1971             : 
    1972          84 :   a = hnfmerge_get_1(idealhnf_principal(nf,x), y);
    1973          84 :   if (!a) pari_err_INV("nfinvmodideal", x);
    1974          84 :   return gerepileupto(av, zk_modHNF(nfdiv(nf,a,x), y));
    1975             : }
    1976             : 
    1977             : static GEN
    1978      277327 : nfsqrmodideal(GEN nf, GEN x, GEN id)
    1979      277327 : { return zk_modHNF(nfsqri(nf,x), id); }
    1980             : static GEN
    1981      735440 : nfmulmodideal(GEN nf, GEN x, GEN y, GEN id)
    1982      735440 : { return x? zk_modHNF(nfmuli(nf,x,y), id): y; }
    1983             : /* assume x integral, k integer, A in HNF */
    1984             : GEN
    1985      475929 : nfpowmodideal(GEN nf,GEN x,GEN k,GEN A)
    1986             : {
    1987      475929 :   long s = signe(k);
    1988             :   pari_sp av;
    1989             :   GEN y;
    1990             : 
    1991      475929 :   if (!s) return gen_1;
    1992      475929 :   av = avma;
    1993      475929 :   x = nf_to_scalar_or_basis(nf, x);
    1994      475929 :   if (typ(x) != t_COL) return Fp_pow(x, k, gcoeff(A,1,1));
    1995      226131 :   if (s < 0) { x = nfinvmodideal(nf, x,A); k = negi(k); }
    1996      226131 :   for(y = NULL;;)
    1997             :   {
    1998      780785 :     if (mpodd(k)) y = nfmulmodideal(nf,y,x,A);
    1999      503458 :     k = shifti(k,-1); if (!signe(k)) break;
    2000      277327 :     x = nfsqrmodideal(nf,x,A);
    2001             :   }
    2002      226131 :   return gerepileupto(av, y);
    2003             : }
    2004             : 
    2005             : /* a * g^n mod id */
    2006             : static GEN
    2007      422736 : nfmulpowmodideal(GEN nf, GEN a, GEN g, GEN n, GEN id)
    2008             : {
    2009      422736 :   return nfmulmodideal(nf, a, nfpowmodideal(nf,g,n,id), id);
    2010             : }
    2011             : 
    2012             : /* assume (num(g[i]), id) = 1 for all i. Return prod g[i]^e[i] mod id.
    2013             :  * EX = multiple of exponent of (O_K/id)^* */
    2014             : GEN
    2015      194842 : famat_to_nf_modideal_coprime(GEN nf, GEN g, GEN e, GEN id, GEN EX)
    2016             : {
    2017      194842 :   GEN EXo2, plus = NULL, minus = NULL, idZ = gcoeff(id,1,1);
    2018      194842 :   long i, lx = lg(g);
    2019             : 
    2020      194842 :   if (equali1(idZ)) return gen_1; /* id = Z_K */
    2021      194611 :   EXo2 = (expi(EX) > 10)? shifti(EX,-1): NULL;
    2022      874172 :   for (i = 1; i < lx; i++)
    2023             :   {
    2024      679561 :     GEN h, n = centermodii(gel(e,i), EX, EXo2);
    2025      679561 :     long sn = signe(n);
    2026      679561 :     if (!sn) continue;
    2027             : 
    2028      324374 :     h = nf_to_scalar_or_basis(nf, gel(g,i));
    2029      324374 :     switch(typ(h))
    2030             :     {
    2031      199921 :       case t_INT: break;
    2032             :       case t_FRAC:
    2033           0 :         h = Fp_div(gel(h,1), gel(h,2), idZ); break;
    2034             :       default:
    2035             :       {
    2036             :         GEN dh;
    2037      124453 :         h = Q_remove_denom(h, &dh);
    2038      124453 :         if (dh) h = FpC_Fp_mul(h, Fp_inv(dh,idZ), idZ);
    2039             :       }
    2040             :     }
    2041      324374 :     if (sn > 0)
    2042      322953 :       plus = nfmulpowmodideal(nf, plus, h, n, id);
    2043             :     else /* sn < 0 */
    2044        1421 :       minus = nfmulpowmodideal(nf, minus, h, negi(n), id);
    2045             :   }
    2046      194611 :   if (minus) plus = nfmulmodideal(nf, plus, nfinvmodideal(nf,minus,id), id);
    2047      194611 :   return plus? plus: gen_1;
    2048             : }
    2049             : 
    2050             : /* given 2 integral ideals x, y in HNF s.t x | y | x^2, compute (1+x)/(1+y) in
    2051             :  * the form [[cyc],[gen], U], where U := ux^-1 as a pair [ZM, denom(U)] */
    2052             : static GEN
    2053       20839 : zidealij(GEN x, GEN y)
    2054             : {
    2055       20839 :   GEN U, G, cyc, xp = gcoeff(x,1,1), xi = hnf_invscale(x, xp);
    2056             :   long j, N;
    2057             : 
    2058             :   /* x^(-1) y = relations between the 1 + x_i (HNF) */
    2059       20839 :   cyc = ZM_snf_group(ZM_Z_divexact(ZM_mul(xi, y), xp), &U, &G);
    2060       20839 :   N = lg(cyc); G = ZM_mul(x,G); settyp(G, t_VEC); /* new generators */
    2061       77700 :   for (j=1; j<N; j++)
    2062             :   {
    2063       56861 :     GEN c = gel(G,j);
    2064       56861 :     gel(c,1) = addiu(gel(c,1), 1); /* 1 + g_j */
    2065       56861 :     if (ZV_isscalar(c)) gel(G,j) = gel(c,1);
    2066             :   }
    2067       20839 :   return mkvec4(cyc, G, ZM_mul(U,xi), xp);
    2068             : }
    2069             : 
    2070             : /* lg(x) > 1, x + 1; shallow */
    2071             : static GEN
    2072        7098 : ZC_add1(GEN x)
    2073             : {
    2074        7098 :   long i, l = lg(x);
    2075        7098 :   GEN y = cgetg(l, t_COL);
    2076        7098 :   for (i = 2; i < l; i++) gel(y,i) = gel(x,i);
    2077        7098 :   gel(y,1) = addiu(gel(x,1), 1); return y;
    2078             : }
    2079             : /* lg(x) > 1, x - 1; shallow */
    2080             : static GEN
    2081        3948 : ZC_sub1(GEN x)
    2082             : {
    2083        3948 :   long i, l = lg(x);
    2084        3948 :   GEN y = cgetg(l, t_COL);
    2085        3948 :   for (i = 2; i < l; i++) gel(y,i) = gel(x,i);
    2086        3948 :   gel(y,1) = subiu(gel(x,1), 1); return y;
    2087             : }
    2088             : 
    2089             : /* x,y are t_INT or ZC */
    2090             : static GEN
    2091           0 : zkadd(GEN x, GEN y)
    2092             : {
    2093           0 :   long tx = typ(x);
    2094           0 :   if (tx == typ(y))
    2095           0 :     return tx == t_INT? addii(x,y): ZC_add(x,y);
    2096             :   else
    2097           0 :     return tx == t_INT? ZC_Z_add(y,x): ZC_Z_add(x,y);
    2098             : }
    2099             : /* x a t_INT or ZC, x+1; shallow */
    2100             : static GEN
    2101       14812 : zkadd1(GEN x)
    2102             : {
    2103       14812 :   long tx = typ(x);
    2104       14812 :   return tx == t_INT? addiu(x,1): ZC_add1(x);
    2105             : }
    2106             : /* x a t_INT or ZC, x-1; shallow */
    2107             : static GEN
    2108       14812 : zksub1(GEN x)
    2109             : {
    2110       14812 :   long tx = typ(x);
    2111       14812 :   return tx == t_INT? subiu(x,1): ZC_sub1(x);
    2112             : }
    2113             : /* x,y are t_INT or ZC; x - y */
    2114             : static GEN
    2115           0 : zksub(GEN x, GEN y)
    2116             : {
    2117           0 :   long tx = typ(x), ty = typ(y);
    2118           0 :   if (tx == ty)
    2119           0 :     return tx == t_INT? subii(x,y): ZC_sub(x,y);
    2120             :   else
    2121           0 :     return tx == t_INT? Z_ZC_sub(x,y): ZC_Z_sub(x,y);
    2122             : }
    2123             : /* x is t_INT or ZM (mult. map), y is t_INT or ZC; x * y */
    2124             : static GEN
    2125       14812 : zkmul(GEN x, GEN y)
    2126             : {
    2127       14812 :   long tx = typ(x), ty = typ(y);
    2128       14812 :   if (ty == t_INT)
    2129       10864 :     return tx == t_INT? mulii(x,y): ZC_Z_mul(gel(x,1),y);
    2130             :   else
    2131        3948 :     return tx == t_INT? ZC_Z_mul(y,x): ZM_ZC_mul(x,y);
    2132             : }
    2133             : 
    2134             : /* (U,V) = 1 coprime ideals. Want z = x mod U, = y mod V; namely
    2135             :  * z =vx + uy = v(x-y) + y, where u + v = 1, u in U, v in V.
    2136             :  * zkc = [v, UV], v a t_INT or ZM (mult. by v map), UV a ZM (ideal in HNF);
    2137             :  * shallow */
    2138             : GEN
    2139           0 : zkchinese(GEN zkc, GEN x, GEN y)
    2140             : {
    2141           0 :   GEN v = gel(zkc,1), UV = gel(zkc,2), z = zkadd(zkmul(v, zksub(x,y)), y);
    2142           0 :   return zk_modHNF(z, UV);
    2143             : }
    2144             : /* special case z = x mod U, = 1 mod V; shallow */
    2145             : GEN
    2146       14812 : zkchinese1(GEN zkc, GEN x)
    2147             : {
    2148       14812 :   GEN v = gel(zkc,1), UV = gel(zkc,2), z = zkadd1(zkmul(v, zksub1(x)));
    2149       14812 :   return (typ(z) == t_INT)? z: ZC_hnfrem(z, UV);
    2150             : }
    2151             : static GEN
    2152       13440 : zkVchinese1(GEN zkc, GEN v)
    2153             : {
    2154             :   long i, ly;
    2155       13440 :   GEN y = cgetg_copy(v, &ly);
    2156       13440 :   for (i=1; i<ly; i++) gel(y,i) = zkchinese1(zkc, gel(v,i));
    2157       13440 :   return y;
    2158             : }
    2159             : 
    2160             : /* prepare to solve z = x (mod A), z = y mod (B) [zkchinese or zkchinese1] */
    2161             : GEN
    2162       13181 : zkchineseinit(GEN nf, GEN A, GEN B, GEN AB)
    2163             : {
    2164             :   GEN v;
    2165             :   long e;
    2166       13181 :   nf = checknf(nf);
    2167       13181 :   v = idealaddtoone_raw(nf, A, B);
    2168       13181 :   if ((e = gexpo(v)) > 5)
    2169             :   {
    2170         588 :     GEN b = (typ(v) == t_COL)? v: scalarcol_shallow(v, nf_get_degree(nf));
    2171         588 :     b= ZC_reducemodlll(b, AB);
    2172         588 :     if (gexpo(b) < e) v = b;
    2173             :   }
    2174       13181 :   return mkvec2(zk_scalar_or_multable(nf,v), AB);
    2175             : }
    2176             : /* prepare to solve z = x (mod A), z = 1 mod (B)
    2177             :  * and then         z = 1 (mod A), z = y mod (B) [zkchinese1 twice] */
    2178             : static GEN
    2179         259 : zkchinese1init2(GEN nf, GEN A, GEN B, GEN AB)
    2180             : {
    2181         259 :   GEN zkc = zkchineseinit(nf, A, B, AB);
    2182         259 :   GEN mv = gel(zkc,1), mu;
    2183         259 :   if (typ(mv) == t_INT) return mkvec2(zkc, mkvec2(subui(1,mv),AB));
    2184          35 :   mu = RgM_Rg_add_shallow(ZM_neg(mv), gen_1);
    2185          35 :   return mkvec2(mkvec2(mv,AB), mkvec2(mu,AB));
    2186             : }
    2187             : 
    2188             : static GEN
    2189      354772 : apply_U(GEN L, GEN a)
    2190             : {
    2191      354772 :   GEN e, U = gel(L,3), dU = gel(L,4);
    2192      354772 :   if (typ(a) == t_INT)
    2193      130904 :     e = ZC_Z_mul(gel(U,1), subiu(a, 1));
    2194             :   else
    2195             :   { /* t_COL */
    2196      223868 :     GEN t = gel(a,1);
    2197      223868 :     gel(a,1) = subiu(gel(a,1), 1); /* a -= 1 */
    2198      223868 :     e = ZM_ZC_mul(U, a);
    2199      223868 :     gel(a,1) = t; /* restore */
    2200             :   }
    2201      354772 :   return gdiv(e, dU);
    2202             : }
    2203             : 
    2204             : /* true nf; vectors of [[cyc],[g],U.X^-1]. Assume k > 1. */
    2205             : static GEN
    2206       14154 : principal_units(GEN nf, GEN pr, long k, GEN prk)
    2207             : {
    2208             :   GEN list, prb;
    2209       14154 :   ulong mask = quadratic_prec_mask(k);
    2210       14154 :   long a = 1;
    2211             : 
    2212       14154 :   if (DEBUGLEVEL>3) err_printf("treating pr^%ld, pr = %Ps\n",k,pr);
    2213       14154 :   prb = pr_hnf(nf,pr);
    2214       14154 :   list = vectrunc_init(k);
    2215       49147 :   while (mask > 1)
    2216             :   {
    2217       20839 :     GEN pra = prb;
    2218       20839 :     long b = a << 1;
    2219             : 
    2220       20839 :     if (mask & 1) b--;
    2221       20839 :     mask >>= 1;
    2222             :     /* compute 1 + pr^a / 1 + pr^b, 2a <= b */
    2223       20839 :     if(DEBUGLEVEL>3) err_printf("  treating a = %ld, b = %ld\n",a,b);
    2224       20839 :     prb = (b >= k)? prk: idealpows(nf,pr,b);
    2225       20839 :     vectrunc_append(list, zidealij(pra, prb));
    2226       20839 :     a = b;
    2227             :   }
    2228       14154 :   return list;
    2229             : }
    2230             : /* a = 1 mod (pr) return log(a) on local-gens of 1+pr/1+pr^k */
    2231             : static GEN
    2232      224950 : log_prk1(GEN nf, GEN a, long nh, GEN L2, GEN prk)
    2233             : {
    2234      224950 :   GEN y = cgetg(nh+1, t_COL);
    2235      224950 :   long j, iy, c = lg(L2)-1;
    2236      579715 :   for (j = iy = 1; j <= c; j++)
    2237             :   {
    2238      354772 :     GEN L = gel(L2,j), cyc = gel(L,1), gen = gel(L,2), E = apply_U(L,a);
    2239      354772 :     long i, nc = lg(cyc)-1;
    2240      354772 :     int last = (j == c);
    2241     1242006 :     for (i = 1; i <= nc; i++, iy++)
    2242             :     {
    2243      887241 :       GEN t, e = gel(E,i);
    2244      887241 :       if (typ(e) != t_INT) pari_err_COPRIME("zlog_prk1", a, prk);
    2245      887234 :       t = Fp_neg(e, gel(cyc,i));
    2246      887234 :       gel(y,iy) = negi(t);
    2247      887234 :       if (!last && signe(t)) a = nfmulpowmodideal(nf, a, gel(gen,i), t, prk);
    2248             :     }
    2249             :   }
    2250      224943 :   return y;
    2251             : }
    2252             : /* true nf */
    2253             : static GEN
    2254        5768 : principal_units_relations(GEN nf, GEN L2, GEN prk, long nh)
    2255             : {
    2256        5768 :   GEN h = cgetg(nh+1,t_MAT);
    2257        5768 :   long ih, j, c = lg(L2)-1;
    2258       18221 :   for (j = ih = 1; j <= c; j++)
    2259             :   {
    2260       12453 :     GEN L = gel(L2,j), F = gel(L,1), G = gel(L,2);
    2261       12453 :     long k, lG = lg(G);
    2262       51583 :     for (k = 1; k < lG; k++,ih++)
    2263             :     { /* log(g^f) mod pr^e */
    2264       39130 :       GEN a = nfpowmodideal(nf,gel(G,k),gel(F,k),prk);
    2265       39130 :       gel(h,ih) = ZC_neg(log_prk1(nf, a, nh, L2, prk));
    2266       39130 :       gcoeff(h,ih,ih) = gel(F,k);
    2267             :     }
    2268             :   }
    2269        5768 :   return h;
    2270             : }
    2271             : /* true nf; e > 1; multiplicative group (1 + pr) / (1 + pr^k),
    2272             :  * prk = pr^k or NULL */
    2273             : static GEN
    2274       14154 : idealprincipalunits_i(GEN nf, GEN pr, long k, GEN *pU)
    2275             : {
    2276       14154 :   GEN cyc, gen, L2, prk = idealpows(nf, pr, k);
    2277             : 
    2278       14154 :   L2 = principal_units(nf, pr, k, prk);
    2279       14154 :   if (k == 2)
    2280             :   {
    2281        8386 :     GEN L = gel(L2,1);
    2282        8386 :     cyc = gel(L,1);
    2283        8386 :     gen = gel(L,2);
    2284        8386 :     if (pU) *pU = matid(lg(gen)-1);
    2285             :   }
    2286             :   else
    2287             :   {
    2288        5768 :     long c = lg(L2), j;
    2289        5768 :     GEN EX, h, Ui, vg = cgetg(c, t_VEC);
    2290        5768 :     for (j = 1; j < c; j++) gel(vg, j) = gmael(L2,j,2);
    2291        5768 :     vg = shallowconcat1(vg);
    2292        5768 :     h = principal_units_relations(nf, L2, prk, lg(vg)-1);
    2293        5768 :     h = ZM_hnfall_i(h, NULL, 0);
    2294        5768 :     cyc = ZM_snf_group(h, pU, &Ui);
    2295        5768 :     c = lg(Ui); gen = cgetg(c, t_VEC); EX = gel(cyc,1);
    2296       32585 :     for (j = 1; j < c; j++)
    2297       26817 :       gel(gen,j) = famat_to_nf_modideal_coprime(nf, vg, gel(Ui,j), prk, EX);
    2298             :   }
    2299       14154 :   return mkvec4(cyc, gen, prk, L2);
    2300             : }
    2301             : GEN
    2302         112 : idealprincipalunits(GEN nf, GEN pr, long k)
    2303             : {
    2304             :   pari_sp av;
    2305             :   GEN v;
    2306         112 :   nf = checknf(nf);
    2307         112 :   if (k == 1) { checkprid(pr); retmkvec3(gen_1,cgetg(1,t_VEC),cgetg(1,t_VEC)); }
    2308         105 :   av = avma; v = idealprincipalunits_i(nf, pr, k, NULL);
    2309         105 :   return gerepilecopy(av, mkvec3(powiu(pr_norm(pr), k-1), gel(v,1), gel(v,2)));
    2310             : }
    2311             : 
    2312             : /* true nf; given an ideal pr^k dividing an integral ideal x (in HNF form)
    2313             :  * compute an 'sprk', the structure of G = (Z_K/pr^k)^* [ x = NULL for x=pr^k ]
    2314             :  * Return a vector with at least 4 components [cyc],[gen],[HNF pr^k,pr,k],ff,
    2315             :  * where
    2316             :  * cyc : type of G as abelian group (SNF)
    2317             :  * gen : generators of G, coprime to x
    2318             :  * pr^k: in HNF
    2319             :  * ff  : data for log_g in (Z_K/pr)^*
    2320             :  * Two extra components are present iff k > 1: L2, U
    2321             :  * L2  : list of data structures to compute local DL in (Z_K/pr)^*,
    2322             :  *       and 1 + pr^a/ 1 + pr^b for various a < b <= min(2a, k)
    2323             :  * U   : base change matrices to convert a vector of local DL to DL wrt gen */
    2324             : static GEN
    2325       31815 : sprkinit(GEN nf, GEN pr, GEN gk, GEN x)
    2326             : {
    2327             :   GEN T, p, modpr, cyc, gen, g, g0, ord0, A, prk, U, L2;
    2328       31815 :   long k = itos(gk), f = pr_get_f(pr);
    2329             : 
    2330       31815 :   if(DEBUGLEVEL>3) err_printf("treating pr^%ld, pr = %Ps\n",k,pr);
    2331       31815 :   modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    2332             :   /* (Z_K / pr)^* */
    2333       31815 :   if (f == 1)
    2334             :   {
    2335       22827 :     g0 = g = pgener_Fp(p);
    2336       22827 :     ord0 = get_arith_ZZM(subiu(p,1));
    2337             :   }
    2338             :   else
    2339             :   {
    2340        8988 :     g0 = g = gener_FpXQ(T,p, &ord0);
    2341        8988 :     g = Fq_to_nf(g, modpr);
    2342        8988 :     if (typ(g) == t_POL) g = poltobasis(nf, g);
    2343             :   }
    2344       31815 :   A = gel(ord0, 1); /* Norm(pr)-1 */
    2345       31815 :   if (k == 1)
    2346             :   {
    2347       17766 :     cyc = mkvec(A);
    2348       17766 :     gen = mkvec(g);
    2349       17766 :     prk = pr_hnf(nf,pr);
    2350       17766 :     L2 = U = NULL;
    2351             :   }
    2352             :   else
    2353             :   { /* local-gens of (1 + pr)/(1 + pr^k) = SNF-gens * U */
    2354             :     GEN AB, B, u, v, w;
    2355             :     long j, l;
    2356       14049 :     w = idealprincipalunits_i(nf, pr, k, &U);
    2357             :     /* incorporate (Z_K/pr)^*, order A coprime to B = expo(1+pr/1+pr^k)*/
    2358       14049 :     cyc = leafcopy(gel(w,1)); B = gel(cyc,1); AB = mulii(A,B);
    2359       14049 :     gen = leafcopy(gel(w,2));
    2360       14049 :     prk = gel(w,3);
    2361       14049 :     g = nfpowmodideal(nf, g, B, prk);
    2362       14049 :     g0 = Fq_pow(g0, modii(B,A), T, p); /* update primitive root */
    2363       14049 :     L2 = mkvec3(A, g, gel(w,4));
    2364       14049 :     gel(cyc,1) = AB;
    2365       14049 :     gel(gen,1) = nfmulmodideal(nf, gel(gen,1), g, prk);
    2366       14049 :     u = mulii(Fp_inv(A,B), A);
    2367       14049 :     v = subui(1, u); l = lg(U);
    2368       14049 :     for (j = 1; j < l; j++) gcoeff(U,1,j) = Fp_mul(u, gcoeff(U,1,j), AB);
    2369       14049 :     U = mkvec2(Rg_col_ei(v, lg(gen)-1, 1), U);
    2370             :   }
    2371             :   /* local-gens of (Z_K/pr^k)^* = SNF-gens * U */
    2372       31815 :   if (x)
    2373             :   {
    2374       12922 :     GEN uv = zkchineseinit(nf, idealdivpowprime(nf,x,pr,gk), prk, x);
    2375       12922 :     gen = zkVchinese1(uv, gen);
    2376             :   }
    2377       31815 :   return mkvecn(U? 6: 4, cyc, gen, prk, mkvec3(modpr,g0,ord0), L2, U);
    2378             : }
    2379             : static GEN
    2380      346677 : sprk_get_cyc(GEN s) { return gel(s,1); }
    2381             : static GEN
    2382      110275 : sprk_get_expo(GEN s)
    2383             : {
    2384      110275 :   GEN cyc = sprk_get_cyc(s);
    2385      110275 :   return lg(cyc) == 1? gen_1: gel(cyc, 1);
    2386             : }
    2387             : static GEN
    2388       25683 : sprk_get_gen(GEN s) { return gel(s,2); }
    2389             : static GEN
    2390      296095 : sprk_get_prk(GEN s) { return gel(s,3); }
    2391             : static GEN
    2392      386974 : sprk_get_ff(GEN s) { return gel(s,4); }
    2393             : static GEN
    2394      129224 : sprk_get_pr(GEN s) { GEN ff = gel(s,4); return modpr_get_pr(gel(ff,1)); }
    2395             : /* A = Npr-1, <g> = (Z_K/pr)^*, L2 to 1 + pr / 1 + pr^k */
    2396             : static void
    2397      196677 : sprk_get_L2(GEN s, GEN *A, GEN *g, GEN *L2)
    2398      196677 : { GEN v = gel(s,5); *A = gel(v,1); *g = gel(v,2); *L2 = gel(v,3); }
    2399             : static void
    2400      185820 : sprk_get_U2(GEN s, GEN *U1, GEN *U2)
    2401      185820 : { GEN v = gel(s,6); *U1 = gel(v,1); *U2 = gel(v,2); }
    2402             : static int
    2403      386974 : sprk_is_prime(GEN s) { return lg(s) == 5; }
    2404             : 
    2405             : static GEN
    2406      110275 : famat_zlog_pr(GEN nf, GEN g, GEN e, GEN sprk)
    2407             : {
    2408      110275 :   GEN pr = sprk_get_pr(sprk);
    2409      110275 :   GEN prk = sprk_get_prk(sprk);
    2410      110275 :   GEN x = famat_makecoprime(nf, g, e, pr, prk, sprk_get_expo(sprk));
    2411      110275 :   return log_prk(nf, x, sprk);
    2412             : }
    2413             : /* log_g(a) in (Z_K/pr)^* */
    2414             : static GEN
    2415      386974 : nf_log(GEN nf, GEN a, GEN ff)
    2416             : {
    2417      386974 :   GEN pr = gel(ff,1), g = gel(ff,2), ord = gel(ff,3);
    2418      386974 :   GEN T,p, modpr = nf_to_Fq_init(nf, &pr, &T, &p);
    2419      386974 :   return Fq_log(nf_to_Fq(nf,a,modpr), g, ord, T, p);
    2420             : }
    2421             : /* a in Z_K (t_COL or t_INT), pr prime ideal, sprk = sprkinit(nf,pr,k,x).
    2422             :  * return log(a) on SNF-generators of (Z_K/pr^k)^**/
    2423             : GEN
    2424      388010 : log_prk(GEN nf, GEN a, GEN sprk)
    2425             : {
    2426             :   GEN e, prk, A, g, L2, U1, U2, y;
    2427             : 
    2428      388010 :   if (typ(a) == t_MAT) return famat_zlog_pr(nf, gel(a,1), gel(a,2), sprk);
    2429             : 
    2430      386974 :   e = nf_log(nf, a, sprk_get_ff(sprk));
    2431      386974 :   if (sprk_is_prime(sprk)) return mkcol(e); /* k = 1 */
    2432      185820 :   prk = sprk_get_prk(sprk);
    2433      185820 :   sprk_get_L2(sprk, &A,&g,&L2);
    2434      185820 :   if (signe(e))
    2435             :   {
    2436       45957 :     e = Fp_neg(e, A);
    2437       45957 :     a = nfmulpowmodideal(nf, a, g, e, prk);
    2438             :   }
    2439      185820 :   sprk_get_U2(sprk, &U1,&U2);
    2440      185820 :   y = ZM_ZC_mul(U2, log_prk1(nf, a, lg(U2)-1, L2, prk));
    2441      185813 :   if (signe(e)) y = ZC_sub(y, ZC_Z_mul(U1,e));
    2442      185813 :   return vecmodii(y, sprk_get_cyc(sprk));
    2443             : }
    2444             : GEN
    2445        6132 : log_prk_init(GEN nf, GEN pr, long k)
    2446        6132 : { return sprkinit(checknf(nf),pr,utoipos(k),NULL);}
    2447             : GEN
    2448         378 : veclog_prk(GEN nf, GEN v, GEN sprk)
    2449             : {
    2450         378 :   long l = lg(v), i;
    2451         378 :   GEN w = cgetg(l, t_MAT);
    2452         378 :   for (i = 1; i < l; i++) gel(w,i) = log_prk(nf, gel(v,i), sprk);
    2453         378 :   return w;
    2454             : }
    2455             : 
    2456             : static GEN
    2457      113215 : famat_zlog(GEN nf, GEN fa, GEN sgn, zlog_S *S)
    2458             : {
    2459      113215 :   long i, n0, n = lg(S->U)-1;
    2460             :   GEN g, e, y;
    2461      113215 :   if (lg(fa) == 1) return zerocol(n);
    2462      113215 :   g = gel(fa,1);
    2463      113215 :   e = gel(fa,2);
    2464      113215 :   y = cgetg(n+1, t_COL);
    2465      113215 :   n0 = lg(S->sprk)-1; /* n0 = n (trivial arch. part), or n-1 */
    2466      113215 :   for (i=1; i <= n0; i++) gel(y,i) = famat_zlog_pr(nf, g, e, gel(S->sprk,i));
    2467      113215 :   if (n0 != n)
    2468             :   {
    2469       93174 :     if (!sgn) sgn = nfsign_arch(nf, fa, S->archp);
    2470       93174 :     gel(y,n) = Flc_to_ZC(sgn);
    2471             :   }
    2472      113215 :   return y;
    2473             : }
    2474             : 
    2475             : /* assume that cyclic factors are normalized, in particular != [1] */
    2476             : static GEN
    2477       26096 : split_U(GEN U, GEN Sprk)
    2478             : {
    2479       26096 :   long t = 0, k, n, l = lg(Sprk);
    2480       26096 :   GEN vU = cgetg(l+1, t_VEC);
    2481       51002 :   for (k = 1; k < l; k++)
    2482             :   {
    2483       24906 :     n = lg(sprk_get_cyc(gel(Sprk,k))) - 1; /* > 0 */
    2484       24906 :     gel(vU,k) = vecslice(U, t+1, t+n);
    2485       24906 :     t += n;
    2486             :   }
    2487             :   /* t+1 .. lg(U)-1 */
    2488       26096 :   n = lg(U) - t - 1; /* can be 0 */
    2489       26096 :   if (!n) setlg(vU,l); else gel(vU,l) = vecslice(U, t+1, t+n);
    2490       26096 :   return vU;
    2491             : }
    2492             : 
    2493             : void
    2494      353268 : init_zlog(zlog_S *S, GEN bid)
    2495             : {
    2496      353268 :   GEN fa2 = bid_get_fact2(bid);
    2497      353268 :   S->U = bid_get_U(bid);
    2498      353268 :   S->hU = lg(bid_get_cyc(bid))-1;
    2499      353268 :   S->archp = bid_get_archp(bid);
    2500      353268 :   S->sprk = bid_get_sprk(bid);
    2501      353268 :   S->bid = bid;
    2502      353268 :   S->P = gel(fa2,1);
    2503      353268 :   S->k = gel(fa2,2);
    2504      353268 :   S->no2 = lg(S->P) == lg(gel(bid_get_fact(bid),1));
    2505      353268 : }
    2506             : 
    2507             : /* a a t_FRAC/t_INT, reduce mod bid */
    2508             : static GEN
    2509          14 : Q_mod_bid(GEN bid, GEN a)
    2510             : {
    2511          14 :   GEN xZ = gcoeff(bid_get_ideal(bid),1,1);
    2512          14 :   GEN b = Rg_to_Fp(a, xZ);
    2513          14 :   if (gsigne(a) < 0) b = subii(b, xZ);
    2514          14 :   return signe(b)? b: xZ;
    2515             : }
    2516             : /* Return decomposition of a on the CRT generators blocks attached to the
    2517             :  * S->sprk and sarch; sgn = sign(a, S->arch), NULL if unknown */
    2518             : static GEN
    2519      246147 : zlog(GEN nf, GEN a, GEN sgn, zlog_S *S)
    2520             : {
    2521             :   long k, l;
    2522             :   GEN y;
    2523      246147 :   a = nf_to_scalar_or_basis(nf, a);
    2524      246147 :   switch(typ(a))
    2525             :   {
    2526       64148 :     case t_INT: break;
    2527          14 :     case t_FRAC: a = Q_mod_bid(S->bid, a); break;
    2528             :     default: /* case t_COL: */
    2529             :     {
    2530             :       GEN den;
    2531      181985 :       check_nfelt(a, &den);
    2532      181985 :       if (den)
    2533             :       {
    2534       46407 :         a = Q_muli_to_int(a, den);
    2535       46407 :         a = mkmat2(mkcol2(a, den), mkcol2(gen_1, gen_m1));
    2536       46407 :         return famat_zlog(nf, a, sgn, S);
    2537             :       }
    2538             :     }
    2539             :   }
    2540      199740 :   if (sgn)
    2541       34608 :     sgn = (lg(sgn) == 1)? NULL: leafcopy(sgn);
    2542             :   else
    2543      165132 :     sgn = (lg(S->archp) == 1)? NULL: nfsign_arch(nf, a, S->archp);
    2544      199740 :   l = lg(S->sprk);
    2545      199740 :   y = cgetg(sgn? l+1: l, t_COL);
    2546      441761 :   for (k = 1; k < l; k++)
    2547             :   {
    2548      242028 :     GEN sprk = gel(S->sprk,k);
    2549      242028 :     gel(y,k) = log_prk(nf, a, sprk);
    2550             :   }
    2551      199733 :   if (sgn) gel(y,l) = Flc_to_ZC(sgn);
    2552      199733 :   return y;
    2553             : }
    2554             : 
    2555             : /* true nf */
    2556             : GEN
    2557        8344 : pr_basis_perm(GEN nf, GEN pr)
    2558             : {
    2559        8344 :   long f = pr_get_f(pr);
    2560             :   GEN perm;
    2561        8344 :   if (f == nf_get_degree(nf)) return identity_perm(f);
    2562        6916 :   perm = cgetg(f+1, t_VECSMALL);
    2563        6916 :   perm[1] = 1;
    2564        6916 :   if (f > 1)
    2565             :   {
    2566         399 :     GEN H = pr_hnf(nf,pr);
    2567             :     long i, k;
    2568        1463 :     for (i = k = 2; k <= f; i++)
    2569        1064 :       if (!equali1(gcoeff(H,i,i))) perm[k++] = i;
    2570             :   }
    2571        6916 :   return perm;
    2572             : }
    2573             : 
    2574             : /* \sum U[i]*y[i], U[i] ZM, y[i] ZC. We allow lg(y) > lg(U). */
    2575             : static GEN
    2576      312948 : ZMV_ZCV_mul(GEN U, GEN y)
    2577             : {
    2578      312948 :   long i, l = lg(U);
    2579      312948 :   GEN z = NULL;
    2580      312948 :   if (l == 1) return cgetg(1,t_COL);
    2581      862125 :   for (i = 1; i < l; i++)
    2582             :   {
    2583      549177 :     GEN u = ZM_ZC_mul(gel(U,i), gel(y,i));
    2584      549177 :     z = z? ZC_add(z, u): u;
    2585             :   }
    2586      312948 :   return z;
    2587             : }
    2588             : /* A * (U[1], ..., U[d] */
    2589             : static GEN
    2590         518 : ZM_ZMV_mul(GEN A, GEN U)
    2591             : {
    2592             :   long i, l;
    2593         518 :   GEN V = cgetg_copy(U,&l);
    2594         518 :   for (i = 1; i < l; i++) gel(V,i) = ZM_mul(A,gel(U,i));
    2595         518 :   return V;
    2596             : }
    2597             : 
    2598             : /* Log on bid.gen of generators of P_{1,I pr^{e-1}} / P_{1,I pr^e} (I,pr) = 1,
    2599             :  * defined implicitly via CRT. 'ind' is the index of pr in modulus
    2600             :  * factorization */
    2601             : GEN
    2602       50771 : log_gen_pr(zlog_S *S, long ind, GEN nf, long e)
    2603             : {
    2604       50771 :   GEN A, sprk = gel(S->sprk,ind), Uind = gel(S->U, ind);
    2605             : 
    2606       50771 :   if (e == 1) retmkmat( gel(Uind,1) );
    2607             :   else
    2608             :   {
    2609       18949 :     GEN G, pr = sprk_get_pr(sprk);
    2610             :     long i, l;
    2611       18949 :     if (e == 2)
    2612             :     {
    2613       10857 :       GEN A, g, L, L2; sprk_get_L2(sprk,&A,&g,&L2); L = gel(L2,1);
    2614       10857 :       G = gel(L,2); l = lg(G);
    2615             :     }
    2616             :     else
    2617             :     {
    2618        8092 :       GEN perm = pr_basis_perm(nf,pr), PI = nfpow_u(nf, pr_get_gen(pr), e-1);
    2619        8092 :       l = lg(perm);
    2620        8092 :       G = cgetg(l, t_VEC);
    2621        8092 :       if (typ(PI) == t_INT)
    2622             :       { /* zk_ei_mul doesn't allow t_INT */
    2623        1421 :         long N = nf_get_degree(nf);
    2624        1421 :         gel(G,1) = addiu(PI,1);
    2625        2289 :         for (i = 2; i < l; i++)
    2626             :         {
    2627         868 :           GEN z = col_ei(N, 1);
    2628         868 :           gel(G,i) = z; gel(z, perm[i]) = PI;
    2629             :         }
    2630             :       }
    2631             :       else
    2632             :       {
    2633        6671 :         gel(G,1) = nfadd(nf, gen_1, PI);
    2634        6881 :         for (i = 2; i < l; i++)
    2635         210 :           gel(G,i) = nfadd(nf, gen_1, zk_ei_mul(nf, PI, perm[i]));
    2636             :       }
    2637             :     }
    2638       18949 :     A = cgetg(l, t_MAT);
    2639       40649 :     for (i = 1; i < l; i++)
    2640       21700 :       gel(A,i) = ZM_ZC_mul(Uind, log_prk(nf, gel(G,i), sprk));
    2641       18949 :     return A;
    2642             :   }
    2643             : }
    2644             : /* Log on bid.gen of generator of P_{1,f} / P_{1,f v[index]}
    2645             :  * v = vector of r1 real places */
    2646             : GEN
    2647       10003 : log_gen_arch(zlog_S *S, long index)
    2648             : {
    2649       10003 :   GEN U = gel(S->U, lg(S->U)-1);
    2650       10003 :   return gel(U, index);
    2651             : }
    2652             : 
    2653             : /* compute bid.clgp: [h,cyc] or [h,cyc,gen] */
    2654             : static GEN
    2655       27160 : bid_grp(GEN nf, GEN U, GEN cyc, GEN g, GEN F, GEN sarch)
    2656             : {
    2657       27160 :   GEN G, h = ZV_prod(cyc);
    2658             :   long c;
    2659       27160 :   if (!U) return mkvec2(h,cyc);
    2660       26901 :   c = lg(U);
    2661       26901 :   G = cgetg(c,t_VEC);
    2662       26901 :   if (c > 1)
    2663             :   {
    2664       22505 :     GEN U0, Uoo, EX = gel(cyc,1); /* exponent of bid */
    2665       22505 :     long i, hU = nbrows(U), nba = lg(sarch_get_cyc(sarch))-1; /* #f_oo */
    2666       22505 :     if (!nba) { U0 = U; Uoo = NULL; }
    2667       11760 :     else if (nba == hU) { U0 = NULL; Uoo = U; }
    2668             :     else
    2669             :     {
    2670        9527 :       U0 = rowslice(U, 1, hU-nba);
    2671        9527 :       Uoo = rowslice(U, hU-nba+1, hU);
    2672             :     }
    2673       64526 :     for (i = 1; i < c; i++)
    2674             :     {
    2675       42021 :       GEN t = gen_1;
    2676       42021 :       if (U0) t = famat_to_nf_modideal_coprime(nf, g, gel(U0,i), F, EX);
    2677       42021 :       if (Uoo) t = set_sign_mod_divisor(nf, ZV_to_Flv(gel(Uoo,i),2), t, sarch);
    2678       42021 :       gel(G,i) = t;
    2679             :     }
    2680             :   }
    2681       26901 :   return mkvec3(h, cyc, G);
    2682             : }
    2683             : 
    2684             : /* remove prime ideals of norm 2 with exponent 1 from factorization */
    2685             : static GEN
    2686       26845 : famat_strip2(GEN fa)
    2687             : {
    2688       26845 :   GEN P = gel(fa,1), E = gel(fa,2), Q, F;
    2689       26845 :   long l = lg(P), i, j;
    2690       26845 :   Q = cgetg(l, t_COL);
    2691       26845 :   F = cgetg(l, t_COL);
    2692       56567 :   for (i = j = 1; i < l; i++)
    2693             :   {
    2694       29722 :     GEN pr = gel(P,i), e = gel(E,i);
    2695       29722 :     if (!absequaliu(pr_get_p(pr), 2) || itou(e) != 1 || pr_get_f(pr) != 1)
    2696             :     {
    2697       25683 :       gel(Q,j) = pr;
    2698       25683 :       gel(F,j) = e; j++;
    2699             :     }
    2700             :   }
    2701       26845 :   setlg(Q,j);
    2702       26845 :   setlg(F,j); return mkmat2(Q,F);
    2703             : }
    2704             : 
    2705             : /* Compute [[ideal,arch], [h,[cyc],[gen]], idealfact, [liste], U]
    2706             :    flag may include nf_GEN | nf_INIT */
    2707             : static GEN
    2708       26866 : Idealstar_i(GEN nf, GEN ideal, long flag)
    2709             : {
    2710             :   long i, k, nbp, R1;
    2711       26866 :   GEN y, cyc, U, u1 = NULL, fa, fa2, sprk, x, arch, archp, E, P, sarch, gen;
    2712             : 
    2713       26866 :   nf = checknf(nf);
    2714       26866 :   R1 = nf_get_r1(nf);
    2715       26866 :   if (typ(ideal) == t_VEC && lg(ideal) == 3)
    2716             :   {
    2717       12936 :     arch = gel(ideal,2);
    2718       12936 :     ideal= gel(ideal,1);
    2719       12936 :     switch(typ(arch))
    2720             :     {
    2721             :       case t_VEC:
    2722       12537 :         if (lg(arch) != R1+1)
    2723           0 :           pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
    2724       12537 :         archp = vec01_to_indices(arch);
    2725       12537 :         break;
    2726             :       case t_VECSMALL:
    2727         399 :         archp = arch;
    2728         399 :         k = lg(archp)-1;
    2729         399 :         if (k && archp[k] > R1)
    2730           7 :           pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
    2731         392 :         arch = indices_to_vec01(archp, R1);
    2732         392 :         break;
    2733             :       default:
    2734           0 :         pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
    2735           0 :         return NULL;
    2736             :     }
    2737       12929 :   }
    2738             :   else
    2739             :   {
    2740       13930 :     arch = zerovec(R1);
    2741       13930 :     archp = cgetg(1, t_VECSMALL);
    2742             :   }
    2743       26859 :   if (is_nf_factor(ideal))
    2744             :   {
    2745         721 :     fa = ideal;
    2746         721 :     x = idealfactorback(nf, gel(fa,1), gel(fa,2), 0);
    2747             :   }
    2748             :   else
    2749             :   {
    2750       26138 :     fa = idealfactor(nf, ideal);
    2751       26131 :     x = ideal;
    2752             :   }
    2753       26852 :   if (typ(x) != t_MAT)  x = idealhnf_shallow(nf, x);
    2754       26852 :   if (lg(x) == 1) pari_err_DOMAIN("Idealstar", "ideal","=",gen_0,x);
    2755       26852 :   if (typ(gcoeff(x,1,1)) != t_INT)
    2756           7 :     pari_err_DOMAIN("Idealstar","denominator(ideal)", "!=",gen_1,x);
    2757       26845 :   sarch = nfarchstar(nf, x, archp);
    2758       26845 :   fa2 = famat_strip2(fa);
    2759       26845 :   P = gel(fa2,1);
    2760       26845 :   E = gel(fa2,2);
    2761       26845 :   nbp = lg(P)-1;
    2762       26845 :   sprk = cgetg(nbp+1,t_VEC);
    2763       26845 :   if (nbp)
    2764             :   {
    2765       20188 :     GEN t = (lg(gel(fa,1))==2)? NULL: x; /* beware fa != fa2 */
    2766       20188 :     cyc = cgetg(nbp+2,t_VEC);
    2767       20188 :     gen = cgetg(nbp+1,t_VEC);
    2768       45871 :     for (i = 1; i <= nbp; i++)
    2769             :     {
    2770       25683 :       GEN L = sprkinit(nf, gel(P,i), gel(E,i), t);
    2771       25683 :       gel(sprk,i) = L;
    2772       25683 :       gel(cyc,i) = sprk_get_cyc(L);
    2773             :       /* true gens are congruent to those mod x AND positive at archp */
    2774       25683 :       gel(gen,i) = sprk_get_gen(L);
    2775             :     }
    2776       20188 :     gel(cyc,i) = sarch_get_cyc(sarch);
    2777       20188 :     cyc = shallowconcat1(cyc);
    2778       20188 :     gen = shallowconcat1(gen);
    2779       20188 :     cyc = ZV_snf_group(cyc, &U, (flag & nf_GEN)? &u1: NULL);
    2780             :   }
    2781             :   else
    2782             :   {
    2783        6657 :     cyc = sarch_get_cyc(sarch);
    2784        6657 :     gen = cgetg(1,t_VEC);
    2785        6657 :     U = matid(lg(cyc)-1);
    2786        6657 :     if (flag & nf_GEN) u1 = U;
    2787             :   }
    2788       26845 :   y = bid_grp(nf, u1, cyc, gen, x, sarch);
    2789       26845 :   if (!(flag & nf_INIT)) return y;
    2790       26040 :   U = split_U(U, sprk);
    2791       26040 :   return mkvec5(mkvec2(x, arch), y, mkvec2(fa,fa2), mkvec2(sprk, sarch), U);
    2792             : }
    2793             : GEN
    2794       26593 : Idealstar(GEN nf, GEN ideal, long flag)
    2795             : {
    2796       26593 :   pari_sp av = avma;
    2797       26593 :   if (!nf) nf = nfinit(pol_x(0), DEFAULTPREC);
    2798       26593 :   return gerepilecopy(av, Idealstar_i(nf, ideal, flag));
    2799             : }
    2800             : GEN
    2801         273 : Idealstarprk(GEN nf, GEN pr, long k, long flag)
    2802             : {
    2803         273 :   pari_sp av = avma;
    2804         273 :   GEN z = Idealstar_i(nf, mkmat2(mkcol(pr),mkcols(k)), flag);
    2805         273 :   return gerepilecopy(av, z);
    2806             : }
    2807             : 
    2808             : /* FIXME: obsolete */
    2809             : GEN
    2810           0 : zidealstarinitgen(GEN nf, GEN ideal)
    2811           0 : { return Idealstar(nf,ideal, nf_INIT|nf_GEN); }
    2812             : GEN
    2813           0 : zidealstarinit(GEN nf, GEN ideal)
    2814           0 : { return Idealstar(nf,ideal, nf_INIT); }
    2815             : GEN
    2816           0 : zidealstar(GEN nf, GEN ideal)
    2817           0 : { return Idealstar(nf,ideal, nf_GEN); }
    2818             : 
    2819             : GEN
    2820          70 : idealstar0(GEN nf, GEN ideal,long flag)
    2821             : {
    2822          70 :   switch(flag)
    2823             :   {
    2824           0 :     case 0: return Idealstar(nf,ideal, nf_GEN);
    2825          56 :     case 1: return Idealstar(nf,ideal, nf_INIT);
    2826          14 :     case 2: return Idealstar(nf,ideal, nf_INIT|nf_GEN);
    2827           0 :     default: pari_err_FLAG("idealstar");
    2828             :   }
    2829             :   return NULL; /* LCOV_EXCL_LINE */
    2830             : }
    2831             : 
    2832             : void
    2833      181985 : check_nfelt(GEN x, GEN *den)
    2834             : {
    2835      181985 :   long l = lg(x), i;
    2836      181985 :   GEN t, d = NULL;
    2837      181985 :   if (typ(x) != t_COL) pari_err_TYPE("check_nfelt", x);
    2838      663618 :   for (i=1; i<l; i++)
    2839             :   {
    2840      481633 :     t = gel(x,i);
    2841      481633 :     switch (typ(t))
    2842             :     {
    2843      386539 :       case t_INT: break;
    2844             :       case t_FRAC:
    2845       95094 :         if (!d) d = gel(t,2); else d = lcmii(d, gel(t,2));
    2846       95094 :         break;
    2847           0 :       default: pari_err_TYPE("check_nfelt", x);
    2848             :     }
    2849             :   }
    2850      181985 :   *den = d;
    2851      181985 : }
    2852             : 
    2853             : GEN
    2854     1207048 : vecmodii(GEN x, GEN y)
    2855     1207048 : { pari_APPLY_same(modii(gel(x,i), gel(y,i))) }
    2856             : 
    2857             : GEN
    2858       95004 : vecmoduu(GEN x, GEN y)
    2859       95004 : { pari_APPLY_ulong(uel(x,i) % uel(y,i)) }
    2860             : 
    2861             : static GEN
    2862      314467 : ideallog_i(GEN nf, GEN x, GEN sgn, zlog_S *S)
    2863             : {
    2864      314467 :   pari_sp av = avma;
    2865             :   GEN y, cyc;
    2866      314467 :   if (!S->hU) return cgetg(1, t_COL);
    2867      312955 :   cyc = bid_get_cyc(S->bid);
    2868      312955 :   if (typ(x) == t_MAT)
    2869             :   {
    2870       66808 :     if (lg(x) == 1) return zerocol(lg(cyc)-1);
    2871       66808 :     y = famat_zlog(nf, x, sgn, S);
    2872             :   }
    2873             :   else
    2874      246147 :     y = zlog(nf, x, sgn, S);
    2875      312948 :   y = ZMV_ZCV_mul(S->U, y);
    2876      312948 :   return gerepileupto(av, vecmodii(y, cyc));
    2877             : }
    2878             : 
    2879             : /* Given x (not necessarily integral), and bid as output by zidealstarinit,
    2880             :  * compute the vector of components on the generators bid[2].
    2881             :  * Assume (x,bid) = 1 and sgn is either NULL or nfsign_arch(x, bid) */
    2882             : GEN
    2883      301300 : ideallog_sgn(GEN nf, GEN x, GEN sgn, GEN bid)
    2884             : {
    2885             :   zlog_S S;
    2886      301300 :   nf = checknf(nf); checkbid(bid);
    2887      301293 :   init_zlog(&S, bid);
    2888      301293 :   if (sgn && typ(x) == t_VEC) /* vector of elements and signatures */
    2889             :   {
    2890       21434 :     long i, l = lg(x);
    2891       21434 :     GEN y = cgetg(l, t_MAT);
    2892       21434 :     for (i = 1; i < l; i++) gel(y,i) = ideallog_i(nf, gel(x,i), gel(sgn,i), &S);
    2893       21434 :     return y;
    2894             :   }
    2895      279859 :   return ideallog_i(nf, x, sgn, &S);
    2896             : }
    2897             : GEN
    2898      286537 : ideallog(GEN nf, GEN x, GEN bid)
    2899             : {
    2900      286537 :   if (!nf) return Zideallog(bid, x);
    2901      279866 :   return ideallog_sgn(nf, x, NULL, bid);
    2902             : }
    2903             : 
    2904             : /*************************************************************************/
    2905             : /**                                                                     **/
    2906             : /**               JOIN BID STRUCTURES, IDEAL LISTS                      **/
    2907             : /**                                                                     **/
    2908             : /*************************************************************************/
    2909             : /* bid1, bid2: for coprime modules m1 and m2 (without arch. part).
    2910             :  * Output: bid for m1 m2 */
    2911             : static GEN
    2912         476 : join_bid(GEN nf, GEN bid1, GEN bid2)
    2913             : {
    2914         476 :   pari_sp av = avma;
    2915             :   long nbgen, l1,l2;
    2916             :   GEN I1,I2, G1,G2, sprk1,sprk2, cyc1,cyc2, sarch;
    2917         476 :   GEN sprk, fa,fa2, U, cyc, y, u1 = NULL, x, gen;
    2918             : 
    2919         476 :   I1 = bid_get_ideal(bid1);
    2920         476 :   I2 = bid_get_ideal(bid2);
    2921         476 :   if (gequal1(gcoeff(I1,1,1))) return bid2; /* frequent trivial case */
    2922         259 :   G1 = bid_get_grp(bid1);
    2923         259 :   G2 = bid_get_grp(bid2);
    2924         259 :   x = idealmul(nf, I1,I2);
    2925         259 :   fa = famat_mul_shallow(bid_get_fact(bid1), bid_get_fact(bid2));
    2926         259 :   fa2= famat_mul_shallow(bid_get_fact2(bid1), bid_get_fact2(bid2));
    2927         259 :   sprk1 = bid_get_sprk(bid1);
    2928         259 :   sprk2 = bid_get_sprk(bid2);
    2929         259 :   sprk = shallowconcat(sprk1, sprk2);
    2930             : 
    2931         259 :   cyc1 = abgrp_get_cyc(G1); l1 = lg(cyc1);
    2932         259 :   cyc2 = abgrp_get_cyc(G2); l2 = lg(cyc2);
    2933         259 :   gen = (lg(G1)>3 && lg(G2)>3)? gen_1: NULL;
    2934         259 :   nbgen = l1+l2-2;
    2935         259 :   cyc = ZV_snf_group(shallowconcat(cyc1,cyc2), &U, gen? &u1: NULL);
    2936         259 :   if (nbgen)
    2937             :   {
    2938         259 :     GEN U1 = bid_get_U(bid1), U2 = bid_get_U(bid2);
    2939         259 :     U1 = l1==1? const_vec(lg(sprk1), cgetg(1,t_MAT))
    2940         259 :               : ZM_ZMV_mul(vecslice(U, 1, l1-1),   U1);
    2941         259 :     U2 = l2==1? const_vec(lg(sprk2), cgetg(1,t_MAT))
    2942         259 :               : ZM_ZMV_mul(vecslice(U, l1, nbgen), U2);
    2943         259 :     U = shallowconcat(U1, U2);
    2944             :   }
    2945             :   else
    2946           0 :     U = const_vec(lg(sprk), cgetg(1,t_MAT));
    2947             : 
    2948         259 :   if (gen)
    2949             :   {
    2950         259 :     GEN uv = zkchinese1init2(nf, I2, I1, x);
    2951         518 :     gen = shallowconcat(zkVchinese1(gel(uv,1), abgrp_get_gen(G1)),
    2952         259 :                         zkVchinese1(gel(uv,2), abgrp_get_gen(G2)));
    2953             :   }
    2954         259 :   sarch = bid_get_sarch(bid1); /* trivial */
    2955         259 :   y = bid_grp(nf, u1, cyc, gen, x, sarch);
    2956         259 :   x = mkvec2(x, bid_get_arch(bid1));
    2957         259 :   y = mkvec5(x, y, mkvec2(fa, fa2), mkvec2(sprk, sarch), U);
    2958         259 :   return gerepilecopy(av,y);
    2959             : }
    2960             : 
    2961             : typedef struct _ideal_data {
    2962             :   GEN nf, emb, L, pr, prL, sgnU, archp;
    2963             : } ideal_data;
    2964             : 
    2965             : /* z <- ( z | f(v[i])_{i=1..#v} ) */
    2966             : static void
    2967       86065 : concat_join(GEN *pz, GEN v, GEN (*f)(ideal_data*,GEN), ideal_data *data)
    2968             : {
    2969       86065 :   long i, nz, lv = lg(v);
    2970             :   GEN z, Z;
    2971       86065 :   if (lv == 1) return;
    2972       38143 :   z = *pz; nz = lg(z)-1;
    2973       38143 :   *pz = Z = cgetg(lv + nz, typ(z));
    2974       38143 :   for (i = 1; i <=nz; i++) gel(Z,i) = gel(z,i);
    2975       38143 :   Z += nz;
    2976       38143 :   for (i = 1; i < lv; i++) gel(Z,i) = f(data, gel(v,i));
    2977             : }
    2978             : static GEN
    2979         476 : join_idealinit(ideal_data *D, GEN x)
    2980         476 : { return join_bid(D->nf, x, D->prL); }
    2981             : static GEN
    2982       47698 : join_ideal(ideal_data *D, GEN x)
    2983       47698 : { return idealmulpowprime(D->nf, x, D->pr, D->L); }
    2984             : static GEN
    2985         455 : join_unit(ideal_data *D, GEN x)
    2986             : {
    2987         455 :   GEN bid = join_idealinit(D, gel(x,1)), u = gel(x,2), v = mkvec(D->emb);
    2988         455 :   if (lg(u) != 1) v = shallowconcat(u, v);
    2989         455 :   return mkvec2(bid, v);
    2990             : }
    2991             : 
    2992             : /*  flag & nf_GEN : generators, otherwise no
    2993             :  *  flag &2 : units, otherwise no
    2994             :  *  flag &4 : ideals in HNF, otherwise bid
    2995             :  *  flag &8 : omit ideals which cannot be conductors (pr^1 with Npr=2) */
    2996             : static GEN
    2997        3192 : Ideallist(GEN bnf, ulong bound, long flag)
    2998             : {
    2999        3192 :   const long cond = flag & 8;
    3000        3192 :   const long do_units = flag & 2, big_id = !(flag & 4);
    3001        3192 :   const long istar_flag = (flag & nf_GEN) | nf_INIT;
    3002        3192 :   pari_sp av, av0 = avma;
    3003             :   long i, j;
    3004        3192 :   GEN nf, z, p, fa, id, BOUND, U, empty = cgetg(1,t_VEC);
    3005             :   forprime_t S;
    3006             :   ideal_data ID;
    3007        3192 :   GEN (*join_z)(ideal_data*, GEN) =
    3008             :     do_units? &join_unit
    3009        3192 :               : (big_id? &join_idealinit: &join_ideal);
    3010             : 
    3011        3192 :   nf = checknf(bnf);
    3012        3192 :   if ((long)bound <= 0) return empty;
    3013        3192 :   id = matid(nf_get_degree(nf));
    3014        3192 :   if (big_id) id = Idealstar(nf,id, istar_flag);
    3015             : 
    3016             :   /* z[i] will contain all "objects" of norm i. Depending on flag, this means
    3017             :    * an ideal, a bid, or a couple [bid, log(units)]. Such objects are stored
    3018             :    * in vectors, computed one primary component at a time; join_z
    3019             :    * reconstructs the global object */
    3020        3192 :   BOUND = utoipos(bound);
    3021        3192 :   z = cgetg(bound+1,t_VEC);
    3022        3192 :   if (do_units) {
    3023          14 :     U = bnf_build_units(bnf);
    3024          14 :     gel(z,1) = mkvec( mkvec2(id, cgetg(1,t_VEC)) );
    3025             :   } else {
    3026        3178 :     U = NULL; /* -Wall */
    3027        3178 :     gel(z,1) = mkvec(id);
    3028             :   }
    3029        3192 :   for (i=2; i<=(long)bound; i++) gel(z,i) = empty;
    3030        3192 :   ID.nf = nf;
    3031             : 
    3032        3192 :   p = cgetipos(3);
    3033        3192 :   u_forprime_init(&S, 2, bound);
    3034        3192 :   av = avma;
    3035       19600 :   while ((p[2] = u_forprime_next(&S)))
    3036             :   {
    3037       13216 :     if (DEBUGLEVEL>1) { err_printf("%ld ",p[2]); err_flush(); }
    3038       13216 :     fa = idealprimedec_limit_norm(nf, p, BOUND);
    3039       26859 :     for (j=1; j<lg(fa); j++)
    3040             :     {
    3041       13643 :       GEN pr = gel(fa,j), z2 = leafcopy(z);
    3042       13643 :       ulong Q, q = upr_norm(pr);
    3043       13643 :       long l = (cond && q == 2)? 2: 1;
    3044             : 
    3045       13643 :       ID.pr = ID.prL = pr;
    3046       33775 :       for (Q = q; Q <= bound; l++, Q *= q) /* add pr^l */
    3047             :       {
    3048             :         ulong iQ;
    3049       20132 :         ID.L = utoipos(l);
    3050       20132 :         if (big_id) {
    3051         217 :           ID.prL = Idealstarprk(nf, pr, l, istar_flag);
    3052         217 :           if (do_units)
    3053             :           {
    3054         196 :             GEN sprk = bid_get_sprk(ID.prL);
    3055         392 :             ID.emb = lg(sprk) == 1? cgetg(1,t_VEC)
    3056         196 :                                   : veclog_prk(nf, U, gel(sprk,1));
    3057             :           }
    3058             :         }
    3059      106197 :         for (iQ = Q,i = 1; iQ <= bound; iQ += Q,i++)
    3060       86065 :           concat_join(&gel(z,iQ), gel(z2,i), join_z, &ID);
    3061             :       }
    3062             :     }
    3063       13216 :     if (gc_needed(av,1))
    3064             :     {
    3065           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"Ideallist");
    3066           0 :       z = gerepilecopy(av, z);
    3067             :     }
    3068             :   }
    3069        3192 :   return gerepilecopy(av0, z);
    3070             : }
    3071             : GEN
    3072         350 : ideallist0(GEN bnf,long bound, long flag) {
    3073         350 :   if (flag<0 || flag>15) pari_err_FLAG("ideallist");
    3074         350 :   return Ideallist(bnf,bound,flag);
    3075             : }
    3076             : GEN
    3077        2842 : ideallist(GEN bnf,long bound) { return Ideallist(bnf,bound,4); }
    3078             : 
    3079             : /* bid = for module m (without arch. part), arch = archimedean part.
    3080             :  * Output: bid for [m,arch] */
    3081             : static GEN
    3082          56 : join_bid_arch(GEN nf, GEN bid, GEN archp)
    3083             : {
    3084          56 :   pari_sp av = avma;
    3085             :   GEN G, U;
    3086          56 :   GEN sprk, cyc, y, u1 = NULL, x, sarch, gen;
    3087             : 
    3088          56 :   checkbid(bid);
    3089          56 :   G = bid_get_grp(bid);
    3090          56 :   x = bid_get_ideal(bid);
    3091          56 :   sarch = nfarchstar(nf, bid_get_ideal(bid), archp);
    3092          56 :   sprk = bid_get_sprk(bid);
    3093             : 
    3094          56 :   gen = (lg(G)>3)? gel(G,3): NULL;
    3095          56 :   cyc = diagonal_shallow(shallowconcat(gel(G,2), sarch_get_cyc(sarch)));
    3096          56 :   cyc = ZM_snf_group(cyc, &U, gen? &u1: NULL);
    3097          56 :   y = bid_grp(nf, u1, cyc, gen, x, sarch);
    3098          56 :   U = split_U(U, sprk);
    3099          56 :   y = mkvec5(mkvec2(x, archp), y, gel(bid,3), mkvec2(sprk, sarch), U);
    3100          56 :   return gerepilecopy(av,y);
    3101             : }
    3102             : static GEN
    3103          56 : join_arch(ideal_data *D, GEN x) {
    3104          56 :   return join_bid_arch(D->nf, x, D->archp);
    3105             : }
    3106             : static GEN
    3107          28 : join_archunit(ideal_data *D, GEN x) {
    3108          28 :   GEN bid = join_arch(D, gel(x,1)), u = gel(x,2), v = mkvec(D->emb);
    3109          28 :   if (lg(u) != 1) v = shallowconcat(u, v);
    3110          28 :   return mkvec2(bid, v);
    3111             : }
    3112             : 
    3113             : /* L from ideallist, add archimedean part */
    3114             : GEN
    3115          14 : ideallistarch(GEN bnf, GEN L, GEN arch)
    3116             : {
    3117             :   pari_sp av;
    3118          14 :   long i, j, l = lg(L), lz;
    3119             :   GEN v, z, V;
    3120             :   ideal_data ID;
    3121             :   GEN (*join_z)(ideal_data*, GEN);
    3122             : 
    3123          14 :   if (typ(L) != t_VEC) pari_err_TYPE("ideallistarch",L);
    3124          14 :   if (l == 1) return cgetg(1,t_VEC);
    3125          14 :   z = gel(L,1);
    3126          14 :   if (typ(z) != t_VEC) pari_err_TYPE("ideallistarch",z);
    3127          14 :   z = gel(z,1); /* either a bid or [bid,U] */
    3128          14 :   ID.nf = checknf(bnf);
    3129          14 :   ID.archp = vec01_to_indices(arch);
    3130          14 :   if (lg(z) == 3) { /* the latter: do units */
    3131           7 :     if (typ(z) != t_VEC) pari_err_TYPE("ideallistarch",z);
    3132           7 :     ID.emb = zm_to_ZM( rowpermute(nfsign_units(bnf,NULL,1), ID.archp) );
    3133           7 :     join_z = &join_archunit;
    3134             :   } else
    3135           7 :     join_z = &join_arch;
    3136          14 :   av = avma; V = cgetg(l, t_VEC);
    3137          70 :   for (i = 1; i < l; i++)
    3138             :   {
    3139          56 :     z = gel(L,i); lz = lg(z);
    3140          56 :     gel(V,i) = v = cgetg(lz,t_VEC);
    3141          56 :     for (j=1; j<lz; j++) gel(v,j) = join_z(&ID, gel(z,j));
    3142             :   }
    3143          14 :   return gerepilecopy(av,V);
    3144             : }

Generated by: LCOV version 1.13