Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - base3.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.1 lcov report (development 22708-0f0e6fe44) Lines: 1618 1726 93.7 %
Date: 2018-06-18 05:36:21 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    11587731 : get_tab(GEN nf, long *N)
      30             : {
      31    11587731 :   GEN tab = (typ(nf) == t_MAT)? nf: gel(nf,9);
      32    11587731 :   *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   396556836 : _mulii(GEN x, GEN y) {
      38   629732588 :   return is_pm1(x)? (signe(x) < 0)? negi(y): y
      39   629732588 :                   : 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     9265217 : zk_ei_mul(GEN nf, GEN x, long i)
      80             : {
      81             :   long j, k, N;
      82             :   GEN v, tab;
      83             : 
      84     9265217 :   if (i==1) return ZC_copy(x);
      85     9265203 :   tab = get_tab(nf, &N); tab += (i-1)*N;
      86     9265203 :   v = cgetg(N+1,t_COL);
      87    63333975 :   for (k=1; k<=N; k++)
      88             :   {
      89    54068772 :     pari_sp av = avma;
      90    54068772 :     GEN s = gen_0;
      91   655259100 :     for (j=1; j<=N; j++)
      92             :     {
      93   601190328 :       GEN c = gcoeff(tab,k,j);
      94   601190328 :       if (signe(c)) s = addii(s, _mulii(c, gel(x,j)));
      95             :     }
      96    54068772 :     gel(v,k) = gerepileuptoint(av, s);
      97             :   }
      98     9265203 :   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     4863796 : zk_multable(GEN nf, GEN x)
     115             : {
     116     4863796 :   long i, l = lg(x);
     117     4863796 :   GEN mul = cgetg(l,t_MAT);
     118     4863796 :   gel(mul,1) = x; /* assume w_1 = 1 */
     119     4863796 :   for (i=2; i<l; i++) gel(mul,i) = zk_ei_mul(nf,x,i);
     120     4863796 :   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     3485175 : zk_scalar_or_multable(GEN nf, GEN x)
     140             : {
     141     3485175 :   long tx = typ(x);
     142     3485175 :   if (tx == t_MAT || tx == t_INT) return x;
     143     3436519 :   x = nf_to_scalar_or_basis(nf, x);
     144     3436519 :   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       31374 : nfnorm(GEN nf, GEN x)
     181             : {
     182       31374 :   pari_sp av = avma;
     183       31374 :   nf = checknf(nf);
     184       31374 :   if (typ(x) == t_MAT) return famat_norm(nf, x);
     185       31367 :   x = nf_to_scalar_or_alg(nf, x);
     186       87528 :   x = (typ(x) == t_POL)? RgXQ_norm(x, nf_get_pol(nf))
     187       56161 :                        : gpowgs(x, nf_get_degree(nf));
     188       31367 :   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    20605667 : nfmul(GEN nf, GEN x, GEN y)
     238             : {
     239             :   GEN z;
     240    20605667 :   pari_sp av = avma;
     241             : 
     242    20605667 :   if (x == y) return nfsqr(nf,x);
     243             : 
     244    17749625 :   nf = checknf(nf);
     245    17749625 :   x = nf_to_scalar_or_basis(nf, x);
     246    17749625 :   y = nf_to_scalar_or_basis(nf, y);
     247    17749625 :   if (typ(x) != t_COL)
     248             :   {
     249    13738428 :     if (isintzero(x)) return gen_0;
     250     9854478 :     z = (typ(y) == t_COL)? RgC_Rg_mul(y, x): gmul(x,y); }
     251             :   else
     252             :   {
     253     4011197 :     if (typ(y) != t_COL)
     254             :     {
     255     2846375 :       if (isintzero(y)) return gen_0;
     256      640633 :       z = RgC_Rg_mul(x, y);
     257             :     }
     258             :     else
     259             :     {
     260             :       GEN dx, dy;
     261     1164822 :       x = Q_remove_denom(x, &dx);
     262     1164822 :       y = Q_remove_denom(y, &dy);
     263     1164822 :       z = nfmuli(nf,x,y);
     264     1164822 :       dx = mul_denom(dx,dy);
     265     1164822 :       if (dx) z = ZC_Z_div(z, dx);
     266             :     }
     267             :   }
     268    11659933 :   return gerepileupto(av, z);
     269             : }
     270             : /* square of x in nf */
     271             : GEN
     272     4715616 : nfsqr(GEN nf, GEN x)
     273             : {
     274     4715616 :   pari_sp av = avma;
     275             :   GEN z;
     276             : 
     277     4715616 :   nf = checknf(nf);
     278     4715616 :   x = nf_to_scalar_or_basis(nf, x);
     279     4715616 :   if (typ(x) != t_COL) z = gsqr(x);
     280             :   else
     281             :   {
     282             :     GEN dx;
     283       80202 :     x = Q_remove_denom(x, &dx);
     284       80202 :     z = nfsqri(nf,x);
     285       80202 :     if (dx) z = RgC_Rg_div(z, sqri(dx));
     286             :   }
     287     4715616 :   return gerepileupto(av, z);
     288             : }
     289             : 
     290             : /* x a ZC, v a t_COL of ZC/Z */
     291             : GEN
     292      129764 : zkC_multable_mul(GEN v, GEN x)
     293             : {
     294      129764 :   long i, l = lg(v);
     295      129764 :   GEN y = cgetg(l, t_COL);
     296      458158 :   for (i = 1; i < l; i++)
     297             :   {
     298      328394 :     GEN c = gel(v,i);
     299      328394 :     if (typ(c)!=t_COL) {
     300           0 :       if (!isintzero(c)) c = ZC_Z_mul(gel(x,1), c);
     301             :     } else {
     302      328394 :       c = ZM_ZC_mul(x,c);
     303      328394 :       if (ZV_isscalar(c)) c = gel(c,1);
     304             :     }
     305      328394 :     gel(y,i) = c;
     306             :   }
     307      129764 :   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      359465 : 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       41903 : zk_inv(GEN nf, GEN x) { return zkmultable_inv(zk_multable(nf,x)); }
     402             : 
     403             : /* inverse of x in nf */
     404             : GEN
     405       64190 : nfinv(GEN nf, GEN x)
     406             : {
     407       64190 :   pari_sp av = avma;
     408             :   GEN z;
     409             : 
     410       64190 :   nf = checknf(nf);
     411       64190 :   x = nf_to_scalar_or_basis(nf, x);
     412       64190 :   if (typ(x) == t_COL)
     413             :   {
     414             :     GEN d;
     415       24759 :     x = Q_remove_denom(x, &d);
     416       24759 :     z = zk_inv(nf, x);
     417       24759 :     if (d) z = RgC_Rg_mul(z, d);
     418             :   }
     419             :   else
     420       39431 :     z = ginv(x);
     421       64190 :   return gerepileupto(av, z);
     422             : }
     423             : 
     424             : /* quotient of x and y in nf */
     425             : GEN
     426       21966 : nfdiv(GEN nf, GEN x, GEN y)
     427             : {
     428       21966 :   pari_sp av = avma;
     429             :   GEN z;
     430             : 
     431       21966 :   nf = checknf(nf);
     432       21966 :   y = nf_to_scalar_or_basis(nf, y);
     433       21966 :   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        9759 :     y = Q_remove_denom(y, &d);
     442        9759 :     z = nfmul(nf, x, zk_inv(nf,y));
     443        9759 :     if (d) z = RgC_Rg_mul(z, d);
     444             :   }
     445       21966 :   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     1613594 : nfmuli(GEN nf, GEN x, GEN y)
     452             : {
     453             :   long i, j, k, N;
     454     1613594 :   GEN s, v, TAB = get_tab(nf, &N);
     455             : 
     456     1613594 :   if (typ(x) == t_INT) return (typ(y) == t_COL)? ZC_Z_mul(y, x): mulii(x,y);
     457     1517606 :   if (typ(y) == t_INT) return ZC_Z_mul(x, y);
     458             :   /* both x and y are ZV */
     459     1483850 :   v = cgetg(N+1,t_COL);
     460     6048227 :   for (k=1; k<=N; k++)
     461             :   {
     462     4564377 :     pari_sp av = avma;
     463     4564377 :     GEN TABi = TAB;
     464     4564377 :     if (k == 1)
     465     1483850 :       s = mulii(gel(x,1),gel(y,1));
     466             :     else
     467     6161054 :       s = addii(mulii(gel(x,1),gel(y,k)),
     468     6161054 :                 mulii(gel(x,k),gel(y,1)));
     469    23310053 :     for (i=2; i<=N; i++)
     470             :     {
     471    18745676 :       GEN t, xi = gel(x,i);
     472    18745676 :       TABi += N;
     473    18745676 :       if (!signe(xi)) continue;
     474             : 
     475    13019359 :       t = NULL;
     476   133576297 :       for (j=2; j<=N; j++)
     477             :       {
     478   120556938 :         GEN p1, c = gcoeff(TABi, k, j); /* m^{i,j}_k */
     479   120556938 :         if (!signe(c)) continue;
     480    54485671 :         p1 = _mulii(c, gel(y,j));
     481    54485671 :         t = t? addii(t, p1): p1;
     482             :       }
     483    13019359 :       if (t) s = addii(s, mulii(xi, t));
     484             :     }
     485     4564377 :     gel(v,k) = gerepileuptoint(av,s);
     486             :   }
     487     1483850 :   return v;
     488             : }
     489             : /* square of INTEGER (t_INT or ZC) x in nf */
     490             : GEN
     491      679310 : nfsqri(GEN nf, GEN x)
     492             : {
     493             :   long i, j, k, N;
     494      679310 :   GEN s, v, TAB = get_tab(nf, &N);
     495             : 
     496      679310 :   if (typ(x) == t_INT) return sqri(x);
     497      679310 :   v = cgetg(N+1,t_COL);
     498     5358832 :   for (k=1; k<=N; k++)
     499             :   {
     500     4679522 :     pari_sp av = avma;
     501     4679522 :     GEN TABi = TAB;
     502     4679522 :     if (k == 1)
     503      679310 :       s = sqri(gel(x,1));
     504             :     else
     505     4000212 :       s = shifti(mulii(gel(x,1),gel(x,k)), 1);
     506    56472542 :     for (i=2; i<=N; i++)
     507             :     {
     508    51793020 :       GEN p1, c, t, xi = gel(x,i);
     509    51793020 :       TABi += N;
     510    51793020 :       if (!signe(xi)) continue;
     511             : 
     512    17526174 :       c = gcoeff(TABi, k, i);
     513    17526174 :       t = signe(c)? _mulii(c,xi): NULL;
     514   250611675 :       for (j=i+1; j<=N; j++)
     515             :       {
     516   233085501 :         c = gcoeff(TABi, k, j);
     517   233085501 :         if (!signe(c)) continue;
     518   121691730 :         p1 = _mulii(c, shifti(gel(x,j),1));
     519   121691730 :         t = t? addii(t, p1): p1;
     520             :       }
     521    17526174 :       if (t) s = addii(s, mulii(xi, t));
     522             :     }
     523     4679522 :     gel(v,k) = gerepileuptoint(av,s);
     524             :   }
     525      679310 :   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       47074 : _mul(void *data, GEN x, GEN y) { return nfmuli((GEN)data,x,y); }
     609             : static GEN
     610      141726 : _sqr(void *data, GEN x) { return nfsqri((GEN)data,x); }
     611             : 
     612             : /* Compute z^n in nf, left-shift binary powering */
     613             : GEN
     614      117292 : nfpow(GEN nf, GEN z, GEN n)
     615             : {
     616      117292 :   pari_sp av = avma;
     617             :   long s;
     618             :   GEN x, cx;
     619             : 
     620      117292 :   if (typ(n)!=t_INT) pari_err_TYPE("nfpow",n);
     621      117292 :   nf = checknf(nf);
     622      117292 :   s = signe(n); if (!s) return gen_1;
     623      117292 :   x = nf_to_scalar_or_basis(nf, z);
     624      117292 :   if (typ(x) != t_COL) return powgi(x,n);
     625      116676 :   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 = absi(n);
     633             :   }
     634             :   else
     635      113225 :     x = primitive_part(x, &cx);
     636      116676 :   x = gen_pow(x, n, (void*)nf, _sqr, _mul);
     637      116676 :   if (cx) x = gmul(x, powgi(cx, n));
     638      116676 :   return av==avma? gcopy(x): gerepileupto(av,x);
     639             : }
     640             : /* Compute z^n in nf, left-shift binary powering */
     641             : GEN
     642       45997 : nfpow_u(GEN nf, GEN z, ulong n)
     643             : {
     644       45997 :   pari_sp av = avma;
     645             :   GEN x, cx;
     646             : 
     647       45997 :   nf = checknf(nf);
     648       45997 :   if (!n) return gen_1;
     649       45997 :   x = nf_to_scalar_or_basis(nf, z);
     650       45997 :   if (typ(x) != t_COL) return gpowgs(x,n);
     651       17731 :   x = primitive_part(x, &cx);
     652       17731 :   x = gen_powu(x, n, (void*)nf, _sqr, _mul);
     653       17731 :   if (cx) x = gmul(x, powgi(cx, utoipos(n)));
     654       17731 :   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     5377243 : ZC_nfvalrem(GEN x, GEN pr, GEN *newx)
     713             : {
     714             :   long i, v, l;
     715     5377243 :   GEN r, y, p = pr_get_p(pr), mul = pr_get_tau(pr);
     716             : 
     717             :   /* p inert */
     718     5377243 :   if (typ(mul) == t_INT) return newx? ZV_pvalrem(x, p, newx):ZV_pval(x, p);
     719     5366085 :   y = cgetg_copy(x, &l); /* will hold the new x */
     720     5366085 :   x = leafcopy(x);
     721     8193919 :   for(v=0;; v++)
     722             :   {
     723    31339006 :     for (i=1; i<l; i++)
     724             :     { /* is (x.b)[i] divisible by p ? */
     725    25683338 :       gel(y,i) = dvmdii(ZMrow_ZC_mul(mul,x,i),p,&r);
     726    25683338 :       if (r != gen_0) { if (newx) *newx = x; return v; }
     727             :     }
     728     2827834 :     swap(x, y);
     729             :   }
     730             : }
     731             : long
     732     5140541 : ZC_nfval(GEN x, GEN P)
     733     5140541 : { return ZC_nfvalrem(x, P, NULL); }
     734             : 
     735             : /* v_P(x) != 0, x a ZV. Simpler version of ZC_nfvalrem */
     736             : int
     737      260631 : ZC_prdvd(GEN x, GEN P)
     738             : {
     739      260631 :   pari_sp av = avma;
     740             :   long i, l;
     741      260631 :   GEN p = pr_get_p(P), mul = pr_get_tau(P);
     742      260631 :   if (typ(mul) == t_INT) return ZV_Z_dvd(x, p);
     743      260386 :   l = lg(x);
     744     1014839 :   for (i=1; i<l; i++)
     745      900233 :     if (!dvdii(ZMrow_ZC_mul(mul,x,i), p)) { avma = av; return 0; }
     746      114606 :   avma = av; return 1;
     747             : }
     748             : 
     749             : int
     750          28 : pr_equal(GEN P, GEN Q)
     751             : {
     752          28 :   GEN gQ, p = pr_get_p(P);
     753          28 :   long e = pr_get_e(P), f = pr_get_f(P), n;
     754          28 :   if (!equalii(p, pr_get_p(Q)) || e != pr_get_e(Q) || f != pr_get_f(Q))
     755          14 :     return 0;
     756          14 :   gQ = pr_get_gen(Q); n = lg(gQ)-1;
     757          14 :   if (2*e*f > n) return 1; /* room for only one such pr */
     758           7 :   return ZV_equal(pr_get_gen(P), gQ) || ZC_prdvd(gQ, P);
     759             : }
     760             : 
     761             : long
     762     1303687 : nfval(GEN nf, GEN x, GEN pr)
     763             : {
     764     1303687 :   pari_sp av = avma;
     765             :   long w, e;
     766             :   GEN cx, p;
     767             : 
     768     1303687 :   if (gequal0(x)) return LONG_MAX;
     769     1302224 :   nf = checknf(nf);
     770     1302224 :   checkprid(pr);
     771     1302224 :   p = pr_get_p(pr);
     772     1302224 :   e = pr_get_e(pr);
     773     1302224 :   x = nf_to_scalar_or_basis(nf, x);
     774     1302224 :   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 :   avma = av; return w;
     779             : }
     780             : 
     781             : /* want to write p^v = uniformizer^(e*v) * z^v, z coprime to pr */
     782             : /* z := tau^e / p^(e-1), algebraic integer coprime to pr; return z^v */
     783             : static GEN
     784       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      148687 : basistoalg(GEN nf, GEN x)
     847             : {
     848             :   GEN z, T;
     849             : 
     850      148687 :   nf = checknf(nf);
     851      148687 :   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 :       z = cgetg(3,t_POLMOD);
     866        1778 :       gel(z,1) = ZX_copy(T);
     867        1778 :       gel(z,2) = RgX_rem(x, T); return z;
     868             :     case t_INT:
     869             :     case t_FRAC:
     870       15302 :       T = nf_get_pol(nf);
     871       15302 :       z = cgetg(3,t_POLMOD);
     872       15302 :       gel(z,1) = ZX_copy(T);
     873       15302 :       gel(z,2) = gcopy(x); return z;
     874             :     default:
     875           0 :       pari_err_TYPE("basistoalg",x);
     876             :       return NULL; /* LCOV_EXCL_LINE */
     877             :   }
     878             : }
     879             : 
     880             : /* true nf, x a t_POL */
     881             : static GEN
     882     1462853 : pol_to_scalar_or_basis(GEN nf, GEN x)
     883             : {
     884     1462853 :   GEN T = nf_get_pol(nf);
     885     1462853 :   long l = lg(x);
     886     1462853 :   if (varn(x) != varn(T)) pari_err_VAR("nf_to_scalar_or_basis", x,T);
     887     1462790 :   if (l >= lg(T)) { x = RgX_rem(x, T); l = lg(x); }
     888     1462790 :   if (l == 2) return gen_0;
     889      867265 :   if (l == 3)
     890             :   {
     891      201012 :     x = gel(x,2);
     892      201012 :     if (!is_rational_t(typ(x))) pari_err_TYPE("nf_to_scalar_or_basis",x);
     893      201012 :     return x;
     894             :   }
     895      666253 :   return poltobasis(nf,x);
     896             : }
     897             : /* Assume nf is a genuine nf. */
     898             : GEN
     899    83517303 : nf_to_scalar_or_basis(GEN nf, GEN x)
     900             : {
     901    83517303 :   switch(typ(x))
     902             :   {
     903             :     case t_INT: case t_FRAC:
     904    62907450 :       return x;
     905             :     case t_POLMOD:
     906      196084 :       x = checknfelt_mod(nf,x,"nf_to_scalar_or_basis");
     907      196028 :       switch(typ(x))
     908             :       {
     909       34258 :         case t_INT: case t_FRAC: return x;
     910      161770 :         case t_POL: return pol_to_scalar_or_basis(nf,x);
     911             :       }
     912           0 :       break;
     913     1301083 :     case t_POL: return pol_to_scalar_or_basis(nf,x);
     914             :     case t_COL:
     915    19112686 :       if (lg(x)-1 != nf_get_degree(nf)) break;
     916    19112623 :       return QV_isscalar(x)? gel(x,1): x;
     917             :   }
     918          63 :   pari_err_TYPE("nf_to_scalar_or_basis",x);
     919             :   return NULL; /* LCOV_EXCL_LINE */
     920             : }
     921             : /* Let x be a polynomial with coefficients in Q or nf. Return the same
     922             :  * polynomial with coefficients expressed as vectors (on the integral basis).
     923             :  * No consistency checks, not memory-clean. */
     924             : GEN
     925        5902 : RgX_to_nfX(GEN nf, GEN x)
     926             : {
     927             :   long i, l;
     928        5902 :   GEN y = cgetg_copy(x, &l); y[1] = x[1];
     929        5902 :   for (i=2; i<l; i++) gel(y,i) = nf_to_scalar_or_basis(nf, gel(x,i));
     930        5902 :   return y;
     931             : }
     932             : 
     933             : /* Assume nf is a genuine nf. */
     934             : GEN
     935      216995 : nf_to_scalar_or_alg(GEN nf, GEN x)
     936             : {
     937      216995 :   switch(typ(x))
     938             :   {
     939             :     case t_INT: case t_FRAC:
     940       17163 :       return x;
     941             :     case t_POLMOD:
     942        1372 :       x = checknfelt_mod(nf,x,"nf_to_scalar_or_alg");
     943        1372 :       if (typ(x) != t_POL) return x;
     944             :       /* fall through */
     945             :     case t_POL:
     946             :     {
     947       14890 :       GEN T = nf_get_pol(nf);
     948       14890 :       long l = lg(x);
     949       14890 :       if (varn(x) != varn(T)) pari_err_VAR("nf_to_scalar_or_alg", x,T);
     950       14890 :       if (l >= lg(T)) { x = RgX_rem(x, T); l = lg(x); }
     951       14890 :       if (l == 2) return gen_0;
     952       14890 :       if (l == 3) return gel(x,2);
     953       14666 :       return x;
     954             :     }
     955             :     case t_COL:
     956             :     {
     957             :       GEN dx;
     958      184886 :       if (lg(x)-1 != nf_get_degree(nf)) break;
     959      369772 :       if (QV_isscalar(x)) return gel(x,1);
     960      148836 :       x = Q_remove_denom(x, &dx);
     961      148836 :       x = RgV_RgC_mul(nf_get_zkprimpart(nf), x);
     962      148836 :       dx = mul_denom(dx, nf_get_zkden(nf));
     963      148836 :       return gdiv(x,dx);
     964             :     }
     965             :   }
     966          49 :   pari_err_TYPE("nf_to_scalar_or_alg",x);
     967             :   return NULL; /* LCOV_EXCL_LINE */
     968             : }
     969             : 
     970             : /* gmul(A, RgX_to_RgC(x)), A t_MAT of compatible dimensions */
     971             : GEN
     972        1337 : RgM_RgX_mul(GEN A, GEN x)
     973             : {
     974        1337 :   long i, l = lg(x)-1;
     975             :   GEN z;
     976        1337 :   if (l == 1) return zerocol(nbrows(A));
     977        1337 :   z = gmul(gel(x,2), gel(A,1));
     978        2541 :   for (i = 2; i < l; i++)
     979        1204 :     if (!gequal0(gel(x,i+1))) z = gadd(z, gmul(gel(x,i+1), gel(A,i)));
     980        1337 :   return z;
     981             : }
     982             : GEN
     983     2481320 : ZM_ZX_mul(GEN A, GEN x)
     984             : {
     985     2481320 :   long i, l = lg(x)-1;
     986             :   GEN z;
     987     2481320 :   if (l == 1) return zerocol(nbrows(A));
     988     2480539 :   z = ZC_Z_mul(gel(A,1), gel(x,2));
     989     9679820 :   for (i = 2; i < l ; i++)
     990     7199287 :     if (signe(gel(x,i+1))) z = ZC_add(z, ZC_Z_mul(gel(A,i), gel(x,i+1)));
     991     2480533 :   return z;
     992             : }
     993             : /* x a t_POL, nf a genuine nf. No garbage collecting. No check.  */
     994             : GEN
     995     2315494 : poltobasis(GEN nf, GEN x)
     996             : {
     997     2315494 :   GEN d, T = nf_get_pol(nf);
     998     2315494 :   if (varn(x) != varn(T)) pari_err_VAR( "poltobasis", x,T);
     999     2315438 :   if (degpol(x) >= degpol(T)) x = RgX_rem(x,T);
    1000     2315438 :   x = Q_remove_denom(x, &d);
    1001     2315438 :   if (!RgX_is_ZX(x)) pari_err_TYPE("poltobasis",x);
    1002     2315417 :   x = ZM_ZX_mul(nf_get_invzk(nf), x);
    1003     2315417 :   if (d) x = RgC_Rg_div(x, d);
    1004     2315417 :   return x;
    1005             : }
    1006             : 
    1007             : GEN
    1008      267550 : algtobasis(GEN nf, GEN x)
    1009             : {
    1010             :   pari_sp av;
    1011             : 
    1012      267550 :   nf = checknf(nf);
    1013      267550 :   switch(typ(x))
    1014             :   {
    1015             :     case t_POLMOD:
    1016      112518 :       if (!RgX_equal_var(nf_get_pol(nf),gel(x,1)))
    1017           7 :         pari_err_MODULUS("algtobasis", nf_get_pol(nf),gel(x,1));
    1018      112511 :       x = gel(x,2);
    1019      112511 :       switch(typ(x))
    1020             :       {
    1021             :         case t_INT:
    1022        7497 :         case t_FRAC: return scalarcol(x, nf_get_degree(nf));
    1023             :         case t_POL:
    1024      105014 :           av = avma;
    1025      105014 :           return gerepileupto(av,poltobasis(nf,x));
    1026             :       }
    1027           0 :       break;
    1028             : 
    1029             :     case t_POL:
    1030       73170 :       av = avma;
    1031       73170 :       return gerepileupto(av,poltobasis(nf,x));
    1032             : 
    1033             :     case t_COL:
    1034       14865 :       if (lg(x)-1 != nf_get_degree(nf)) pari_err_DIM("nfalgtobasis");
    1035       14865 :       return gcopy(x);
    1036             : 
    1037             :     case t_INT:
    1038       66997 :     case t_FRAC: return scalarcol(x, nf_get_degree(nf));
    1039             :   }
    1040           0 :   pari_err_TYPE("algtobasis",x);
    1041             :   return NULL; /* LCOV_EXCL_LINE */
    1042             : }
    1043             : 
    1044             : GEN
    1045       44212 : rnfbasistoalg(GEN rnf,GEN x)
    1046             : {
    1047       44212 :   const char *f = "rnfbasistoalg";
    1048             :   long lx, i;
    1049       44212 :   pari_sp av = avma;
    1050             :   GEN z, nf, relpol, T;
    1051             : 
    1052       44212 :   checkrnf(rnf);
    1053       44212 :   nf = rnf_get_nf(rnf);
    1054       44212 :   T = nf_get_pol(nf);
    1055       44212 :   relpol = QXQX_to_mod_shallow(rnf_get_pol(rnf), T);
    1056       44212 :   switch(typ(x))
    1057             :   {
    1058             :     case t_COL:
    1059         826 :       z = cgetg_copy(x, &lx);
    1060        2478 :       for (i=1; i<lx; i++)
    1061             :       {
    1062        1701 :         GEN c = nf_to_scalar_or_alg(nf, gel(x,i));
    1063        1652 :         if (typ(c) == t_POL) c = mkpolmod(c,T);
    1064        1652 :         gel(z,i) = c;
    1065             :       }
    1066         777 :       z = RgV_RgC_mul(gel(rnf_get_zk(rnf),1), z);
    1067         714 :       return gerepileupto(av, gmodulo(z,relpol));
    1068             : 
    1069             :     case t_POLMOD:
    1070       29715 :       x = polmod_nffix(f, rnf, x, 0);
    1071       29512 :       if (typ(x) != t_POL) break;
    1072       13286 :       retmkpolmod(RgX_copy(x), RgX_copy(relpol));
    1073             :     case t_POL:
    1074        1099 :       if (varn(x) == varn(T)) { RgX_check_QX(x,f); x = gmodulo(x,T); break; }
    1075         875 :       if (varn(x) == varn(relpol))
    1076             :       {
    1077         826 :         x = RgX_nffix(f,nf_get_pol(nf),x,0);
    1078         826 :         return gmodulo(x, relpol);
    1079             :       }
    1080          49 :       pari_err_VAR(f, x,relpol);
    1081             :   }
    1082       28973 :   retmkpolmod(scalarpol(x, varn(relpol)), RgX_copy(relpol));
    1083             : }
    1084             : 
    1085             : GEN
    1086        1582 : matbasistoalg(GEN nf,GEN x)
    1087             : {
    1088             :   long i, j, li, lx;
    1089        1582 :   GEN z = cgetg_copy(x, &lx);
    1090             : 
    1091        1582 :   if (lx == 1) return z;
    1092        1575 :   switch(typ(x))
    1093             :   {
    1094             :     case t_VEC: case t_COL:
    1095          42 :       for (i=1; i<lx; i++) gel(z,i) = basistoalg(nf, gel(x,i));
    1096          42 :       return z;
    1097        1533 :     case t_MAT: break;
    1098           0 :     default: pari_err_TYPE("matbasistoalg",x);
    1099             :   }
    1100        1533 :   li = lgcols(x);
    1101        5705 :   for (j=1; j<lx; j++)
    1102             :   {
    1103        4172 :     GEN c = cgetg(li,t_COL), xj = gel(x,j);
    1104        4172 :     gel(z,j) = c;
    1105        4172 :     for (i=1; i<li; i++) gel(c,i) = basistoalg(nf, gel(xj,i));
    1106             :   }
    1107        1533 :   return z;
    1108             : }
    1109             : 
    1110             : GEN
    1111        3899 : matalgtobasis(GEN nf,GEN x)
    1112             : {
    1113             :   long i, j, li, lx;
    1114        3899 :   GEN z = cgetg_copy(x, &lx);
    1115             : 
    1116        3899 :   if (lx == 1) return z;
    1117        3843 :   switch(typ(x))
    1118             :   {
    1119             :     case t_VEC: case t_COL:
    1120        3836 :       for (i=1; i<lx; i++) gel(z,i) = algtobasis(nf, gel(x,i));
    1121        3836 :       return z;
    1122           7 :     case t_MAT: break;
    1123           0 :     default: pari_err_TYPE("matalgtobasis",x);
    1124             :   }
    1125           7 :   li = lgcols(x);
    1126          14 :   for (j=1; j<lx; j++)
    1127             :   {
    1128           7 :     GEN c = cgetg(li,t_COL), xj = gel(x,j);
    1129           7 :     gel(z,j) = c;
    1130           7 :     for (i=1; i<li; i++) gel(c,i) = algtobasis(nf, gel(xj,i));
    1131             :   }
    1132           7 :   return z;
    1133             : }
    1134             : GEN
    1135        8582 : RgM_to_nfM(GEN nf,GEN x)
    1136             : {
    1137             :   long i, j, li, lx;
    1138        8582 :   GEN z = cgetg_copy(x, &lx);
    1139             : 
    1140        8582 :   if (lx == 1) return z;
    1141        8582 :   li = lgcols(x);
    1142       65758 :   for (j=1; j<lx; j++)
    1143             :   {
    1144       57176 :     GEN c = cgetg(li,t_COL), xj = gel(x,j);
    1145       57176 :     gel(z,j) = c;
    1146       57176 :     for (i=1; i<li; i++) gel(c,i) = nf_to_scalar_or_basis(nf, gel(xj,i));
    1147             :   }
    1148        8582 :   return z;
    1149             : }
    1150             : GEN
    1151       78617 : RgC_to_nfC(GEN nf, GEN x)
    1152       78617 : { pari_APPLY_type(t_COL, nf_to_scalar_or_basis(nf, gel(x,i))) }
    1153             : 
    1154             : /* x a t_POLMOD, supposedly in rnf = K[z]/(T), K = Q[y]/(Tnf) */
    1155             : GEN
    1156      134449 : polmod_nffix(const char *f, GEN rnf, GEN x, int lift)
    1157      134449 : { return polmod_nffix2(f, rnf_get_nfpol(rnf), rnf_get_pol(rnf), x,lift); }
    1158             : GEN
    1159      134540 : polmod_nffix2(const char *f, GEN T, GEN relpol, GEN x, int lift)
    1160             : {
    1161      134540 :   if (RgX_equal_var(gel(x,1),relpol))
    1162             :   {
    1163      124376 :     x = gel(x,2);
    1164      124376 :     if (typ(x) == t_POL && varn(x) == varn(relpol))
    1165             :     {
    1166       94780 :       x = RgX_nffix(f, T, x, lift);
    1167       94780 :       switch(lg(x))
    1168             :       {
    1169         343 :         case 2: return gen_0;
    1170       21931 :         case 3: return gel(x,2);
    1171             :       }
    1172       72506 :       return x;
    1173             :     }
    1174             :   }
    1175       39760 :   return Rg_nffix(f, T, x, lift);
    1176             : }
    1177             : GEN
    1178        1204 : rnfalgtobasis(GEN rnf,GEN x)
    1179             : {
    1180        1204 :   const char *f = "rnfalgtobasis";
    1181        1204 :   pari_sp av = avma;
    1182             :   GEN T, relpol;
    1183             : 
    1184        1204 :   checkrnf(rnf);
    1185        1204 :   relpol = rnf_get_pol(rnf);
    1186        1204 :   T = rnf_get_nfpol(rnf);
    1187        1204 :   switch(typ(x))
    1188             :   {
    1189             :     case t_COL:
    1190          49 :       if (lg(x)-1 != rnf_get_degree(rnf)) pari_err_DIM(f);
    1191          28 :       x = RgV_nffix(f, T, x, 0);
    1192          21 :       return gerepilecopy(av, x);
    1193             : 
    1194             :     case t_POLMOD:
    1195        1071 :       x = polmod_nffix(f, rnf, x, 0);
    1196        1036 :       if (typ(x) != t_POL) break;
    1197         714 :       return gerepileupto(av, RgM_RgX_mul(rnf_get_invzk(rnf), x));
    1198             :     case t_POL:
    1199          56 :       if (varn(x) == varn(T)) { RgX_check_QX(x,f); x = mkpolmod(x,T); break; }
    1200          35 :       x = RgX_nffix(f, T, x, 0);
    1201          28 :       if (degpol(x) >= degpol(relpol)) x = RgX_rem(x,relpol);
    1202          28 :       return gerepileupto(av, RgM_RgX_mul(rnf_get_invzk(rnf), x));
    1203             :   }
    1204         364 :   return gerepileupto(av, scalarcol(x, rnf_get_degree(rnf)));
    1205             : }
    1206             : 
    1207             : /* Given a and b in nf, gives an algebraic integer y in nf such that a-b.y
    1208             :  * is "small" */
    1209             : GEN
    1210         259 : nfdiveuc(GEN nf, GEN a, GEN b)
    1211             : {
    1212         259 :   pari_sp av = avma;
    1213         259 :   a = nfdiv(nf,a,b);
    1214         259 :   return gerepileupto(av, ground(a));
    1215             : }
    1216             : 
    1217             : /* Given a and b in nf, gives a "small" algebraic integer r in nf
    1218             :  * of the form a-b.y */
    1219             : GEN
    1220         259 : nfmod(GEN nf, GEN a, GEN b)
    1221             : {
    1222         259 :   pari_sp av = avma;
    1223         259 :   GEN p1 = gneg_i(nfmul(nf,b,ground(nfdiv(nf,a,b))));
    1224         259 :   return gerepileupto(av, nfadd(nf,a,p1));
    1225             : }
    1226             : 
    1227             : /* Given a and b in nf, gives a two-component vector [y,r] in nf such
    1228             :  * that r=a-b.y is "small". */
    1229             : GEN
    1230         259 : nfdivrem(GEN nf, GEN a, GEN b)
    1231             : {
    1232         259 :   pari_sp av = avma;
    1233         259 :   GEN p1,z, y = ground(nfdiv(nf,a,b));
    1234             : 
    1235         259 :   p1 = gneg_i(nfmul(nf,b,y));
    1236         259 :   z = cgetg(3,t_VEC);
    1237         259 :   gel(z,1) = gcopy(y);
    1238         259 :   gel(z,2) = nfadd(nf,a,p1); return gerepileupto(av, z);
    1239             : }
    1240             : 
    1241             : /*************************************************************************/
    1242             : /**                                                                     **/
    1243             : /**                        REAL EMBEDDINGS                              **/
    1244             : /**                                                                     **/
    1245             : /*************************************************************************/
    1246             : static GEN
    1247       49343 : sarch_get_cyc(GEN sarch) { return gel(sarch,1); }
    1248             : static GEN
    1249      272110 : sarch_get_archp(GEN sarch) { return gel(sarch,2); }
    1250             : static GEN
    1251       55002 : sarch_get_MI(GEN sarch) { return gel(sarch,3); }
    1252             : static GEN
    1253       55002 : sarch_get_lambda(GEN sarch) { return gel(sarch,4); }
    1254             : static GEN
    1255       55002 : sarch_get_F(GEN sarch) { return gel(sarch,5); }
    1256             : 
    1257             : /* true nf, return number of positive roots of char_x */
    1258             : static long
    1259        1999 : num_positive(GEN nf, GEN x)
    1260             : {
    1261        1999 :   GEN T = nf_get_pol(nf);
    1262        1999 :   GEN charx = ZXQ_charpoly(nf_to_scalar_or_alg(nf,x), T, 0);
    1263             :   long np;
    1264        1999 :   charx = ZX_radical(charx);
    1265        1999 :   np = ZX_sturmpart(charx, mkvec2(gen_0,mkoo()));
    1266        1999 :   return np * (degpol(T) / degpol(charx));
    1267             : }
    1268             : 
    1269             : /* x a QC: return sigma_k(x) where 1 <= k <= r1+r2; correct but inefficient
    1270             :  * if x in Q. M = nf_get_M(nf) */
    1271             : static GEN
    1272          91 : nfembed_i(GEN M, GEN x, long k)
    1273             : {
    1274          91 :   long i, l = lg(M);
    1275          91 :   GEN z = gel(x,1);
    1276          91 :   for (i = 2; i < l; i++) z = gadd(z, gmul(gcoeff(M,k,i), gel(x,i)));
    1277          91 :   return z;
    1278             : }
    1279             : GEN
    1280           0 : nfembed(GEN nf, GEN x, long k)
    1281             : {
    1282           0 :   pari_sp av = avma;
    1283           0 :   nf = checknf(nf);
    1284           0 :   x = nf_to_scalar_or_basis(nf,x);
    1285           0 :   if (typ(x) != t_COL) return gerepilecopy(av, x);
    1286           0 :   return gerepileupto(av, nfembed_i(nf_get_M(nf),x,k));
    1287             : }
    1288             : 
    1289             : /* x a ZC */
    1290             : static GEN
    1291      396936 : zk_embed(GEN M, GEN x, long k)
    1292             : {
    1293      396936 :   long i, l = lg(x);
    1294      396936 :   GEN z = gel(x,1); /* times M[k,1], which is 1 */
    1295      396936 :   for (i = 2; i < l; i++) z = mpadd(z, mpmul(gcoeff(M,k,i), gel(x,i)));
    1296      396936 :   return z;
    1297             : }
    1298             : 
    1299             : /* Given floating point approximation z of sigma_k(x), decide its sign
    1300             :  * [0/+, 1/- and -1 for FAIL] */
    1301             : static long
    1302      386310 : eval_sign_embed(GEN z)
    1303             : { /* dubious, fail */
    1304      386310 :   if (typ(z) == t_REAL && realprec(z) <= LOWDEFAULTPREC) return -1;
    1305      384993 :   return (signe(z) < 1)? 1: 0;
    1306             : }
    1307             : /* return v such that (-1)^v = sign(sigma_k(x)), x primitive ZC */
    1308             : static long
    1309      313412 : eval_sign(GEN M, GEN x, long k)
    1310      313412 : { return eval_sign_embed( zk_embed(M, x, k) ); }
    1311             : 
    1312             : /* check that signs[i..#signs] == s; signs = NULL encodes "totally positive" */
    1313             : static int
    1314           0 : oksigns(long l, GEN signs, long i, long s)
    1315             : {
    1316           0 :   if (!signs) return s == 0;
    1317           0 :   for (; i < l; i++)
    1318           0 :     if (signs[i] != s) return 0;
    1319           0 :   return 1;
    1320             : }
    1321             : /* check that signs[i] = s and signs[i+1..#signs] = 1-s */
    1322             : static int
    1323           0 : oksigns2(long l, GEN signs, long i, long s)
    1324             : {
    1325           0 :   if (!signs) return s == 0 && i == l-1;
    1326           0 :   return signs[i] == s && oksigns(l, signs, i+1, 1-s);
    1327             : }
    1328             : 
    1329             : /* true nf, x a ZC (primitive for efficiency), embx its embeddings or NULL */
    1330             : static int
    1331       63560 : nfchecksigns_i(GEN nf, GEN x, GEN embx, GEN signs, GEN archp)
    1332             : {
    1333       63560 :   long l = lg(archp), i;
    1334       63560 :   GEN M = nf_get_M(nf), sarch = NULL;
    1335       63560 :   long np = -1;
    1336       95263 :   for (i = 1; i < l; i++)
    1337             :   {
    1338             :     long s;
    1339       73367 :     if (embx)
    1340       72898 :       s = eval_sign_embed(gel(embx,i));
    1341             :     else
    1342         469 :       s = eval_sign(M, x, archp[i]);
    1343             :     /* 0 / + or 1 / -; -1 for FAIL */
    1344       73367 :     if (s < 0) /* failure */
    1345             :     {
    1346           0 :       long ni, r1 = nf_get_r1(nf);
    1347             :       GEN xi;
    1348           0 :       if (np < 0)
    1349             :       {
    1350           0 :         np = num_positive(nf, x);
    1351           0 :         if (np == 0)  return oksigns(l, signs, i, 1);
    1352           0 :         if (np == r1) return oksigns(l, signs, i, 0);
    1353           0 :         sarch = nfarchstar(nf, NULL, identity_perm(r1));
    1354             :       }
    1355           0 :       xi = set_sign_mod_divisor(nf, vecsmall_ei(r1, archp[i]), gen_1, sarch);
    1356           0 :       xi = Q_primpart(xi);
    1357           0 :       ni = num_positive(nf, nfmuli(nf,x,xi));
    1358           0 :       if (ni == 0)  return oksigns2(l, signs, i, 0);
    1359           0 :       if (ni == r1) return oksigns2(l, signs, i, 1);
    1360           0 :       s = ni < np? 0: 1;
    1361             :     }
    1362       73367 :     if (s != (signs? signs[i]: 0)) return 0;
    1363             :   }
    1364       21896 :   return 1;
    1365             : }
    1366             : static void
    1367         343 : pl_convert(GEN pl, GEN *psigns, GEN *parchp)
    1368             : {
    1369         343 :   long i, j, l = lg(pl);
    1370         343 :   GEN signs = cgetg(l, t_VECSMALL);
    1371         343 :   GEN archp = cgetg(l, t_VECSMALL);
    1372        1092 :   for (i = j = 1; i < l; i++)
    1373             :   {
    1374         749 :     if (!pl[i]) continue;
    1375         511 :     archp[j] = i;
    1376         511 :     signs[j] = (pl[i] < 0)? 1: 0;
    1377         511 :     j++;
    1378             :   }
    1379         343 :   setlg(archp, j); *parchp = archp;
    1380         343 :   setlg(signs, j); *psigns = signs;
    1381         343 : }
    1382             : /* pl : requested signs for real embeddings, 0 = no sign constraint */
    1383             : int
    1384         861 : nfchecksigns(GEN nf, GEN x, GEN pl)
    1385             : {
    1386         861 :   pari_sp av = avma;
    1387             :   GEN signs, archp;
    1388             :   int res;
    1389         861 :   nf = checknf(nf);
    1390         861 :   x = nf_to_scalar_or_basis(nf,x);
    1391         861 :   if (typ(x) != t_COL)
    1392             :   {
    1393         518 :     long i, l = lg(pl), s = gsigne(x);
    1394        1050 :     for (i = 1; i < l; i++)
    1395         532 :       if (pl[i] && pl[i] != s) { avma = av; return 0; }
    1396         518 :     avma = av; return 1;
    1397             :   }
    1398         343 :   pl_convert(pl, &signs, &archp);
    1399         343 :   res = nfchecksigns_i(nf, x, NULL, signs, archp);
    1400         343 :   avma = av; return res;
    1401             : }
    1402             : 
    1403             : /* signs = NULL: totally positive, else sign[i] = 0 (+) or 1 (-) */
    1404             : static GEN
    1405       55002 : get_C(GEN lambda, long l, GEN signs)
    1406             : {
    1407             :   long i;
    1408             :   GEN C, mlambda;
    1409       55002 :   if (!signs) return const_vec(l-1, lambda);
    1410       16159 :   C = cgetg(l, t_COL); mlambda = gneg(lambda);
    1411       16159 :   for (i = 1; i < l; i++) gel(C,i) = signs[i]? mlambda: lambda;
    1412       16159 :   return C;
    1413             : }
    1414             : /* signs = NULL: totally positive at archp */
    1415             : static GEN
    1416       94097 : nfsetsigns(GEN nf, GEN signs, GEN x, GEN sarch)
    1417             : {
    1418       94097 :   long i, l = lg(sarch_get_archp(sarch));
    1419             :   GEN ex;
    1420             :   /* Is signature already correct ? */
    1421       94097 :   if (typ(x) != t_COL)
    1422             :   {
    1423       30880 :     long s = gsigne(x);
    1424       30880 :     if (!s) i = 1;
    1425       30866 :     else if (!signs)
    1426        3577 :       i = (s < 0)? 1: l;
    1427             :     else
    1428             :     {
    1429       27289 :       s = s < 0? 1: 0;
    1430       42871 :       for (i = 1; i < l; i++)
    1431       28878 :         if (signs[i] != s) break;
    1432             :     }
    1433       30880 :     ex = (i < l)? const_col(l-1, x): NULL;
    1434             :   }
    1435             :   else
    1436             :   {
    1437       63217 :     pari_sp av = avma;
    1438       63217 :     GEN cex, M = nf_get_M(nf), archp = sarch_get_archp(sarch);
    1439       63217 :     GEN xp = Q_primitive_part(x,&cex);
    1440       63217 :     ex = cgetg(l,t_COL);
    1441       63217 :     for (i = 1; i < l; i++) gel(ex,i) = zk_embed(M,xp,archp[i]);
    1442       63217 :     if (nfchecksigns_i(nf, xp, ex, signs, archp)) { ex = NULL; avma = av; }
    1443       41622 :     else if (cex) ex = RgC_Rg_mul(ex, cex); /* put back content */
    1444             :   }
    1445       94097 :   if (ex)
    1446             :   { /* If no, fix it */
    1447       55002 :     GEN MI = sarch_get_MI(sarch), F = sarch_get_F(sarch);
    1448       55002 :     GEN lambda = sarch_get_lambda(sarch);
    1449       55002 :     GEN t = RgC_sub(get_C(lambda, l, signs), ex);
    1450             :     long e;
    1451       55002 :     t = grndtoi(RgM_RgC_mul(MI,t), &e);
    1452       55002 :     if (lg(F) != 1) t = ZM_ZC_mul(F, t);
    1453       55002 :     x = typ(x) == t_COL? RgC_add(t, x): RgC_Rg_add(t, x);
    1454             :   }
    1455       94097 :   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      114796 : set_sign_mod_divisor(GEN nf, GEN x, GEN y, GEN sarch)
    1465             : {
    1466      114796 :   GEN archp = sarch_get_archp(sarch);
    1467      114796 :   if (lg(archp) == 1) return y;
    1468       92746 :   nf = checknf(nf);
    1469       92746 :   if (x && typ(x) != t_VECSMALL) x = nfsign_arch(nf, x, archp);
    1470       92746 :   y = nf_to_scalar_or_basis(nf,y);
    1471       92746 :   return nfsetsigns(nf, x, y, sarch);
    1472             : }
    1473             : 
    1474             : static GEN
    1475       14626 : setsigns_init(GEN nf, GEN archp, GEN F, GEN DATA)
    1476             : {
    1477       14626 :   GEN lambda, Mr = rowpermute(nf_get_M(nf), archp), MI = F? RgM_mul(Mr,F): Mr;
    1478       14626 :   lambda = gmul2n(matrixnorm(MI,DEFAULTPREC), -1);
    1479       14626 :   if (typ(lambda) != t_REAL) lambda = gmul(lambda, sstoQ(1001,1000));
    1480       14626 :   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       14626 :   if (!F) F = cgetg(1,t_MAT);
    1488       14626 :   MI = RgM_inv(MI);
    1489       14626 :   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       28374 : nfarchstar(GEN nf, GEN F, GEN archp)
    1495             : {
    1496       28374 :   long nba = lg(archp) - 1;
    1497       28374 :   if (!nba) return mkvec2(cgetg(1,t_VEC), archp);
    1498       13282 :   if (F && equali1(gcoeff(F,1,1))) F = NULL;
    1499       13282 :   if (F) F = idealpseudored(F, nf_get_roundG(nf));
    1500       13282 :   return setsigns_init(nf, archp, F, const_vec(nba, gen_2));
    1501             : }
    1502             : 
    1503             : /*************************************************************************/
    1504             : /**                                                                     **/
    1505             : /**                         IDEALCHINESE                                **/
    1506             : /**                                                                     **/
    1507             : /*************************************************************************/
    1508             : static int
    1509        2982 : isprfact(GEN x)
    1510             : {
    1511             :   long i, l;
    1512             :   GEN L, E;
    1513        2982 :   if (typ(x) != t_MAT || lg(x) != 3) return 0;
    1514        2982 :   L = gel(x,1); l = lg(L);
    1515        2982 :   E = gel(x,2);
    1516        7154 :   for(i=1; i<l; i++)
    1517             :   {
    1518        4172 :     checkprid(gel(L,i));
    1519        4172 :     if (typ(gel(E,i)) != t_INT) return 0;
    1520             :   }
    1521        2982 :   return 1;
    1522             : }
    1523             : 
    1524             : /* initialize projectors mod pr[i]^e[i] for idealchinese */
    1525             : static GEN
    1526        2982 : pr_init(GEN nf, GEN fa, GEN w, GEN dw)
    1527             : {
    1528        2982 :   GEN U, E, F, L = gel(fa,1), E0 = gel(fa,2);
    1529        2982 :   long i, r = lg(L);
    1530             : 
    1531        2982 :   if (w && lg(w) != r) pari_err_TYPE("idealchinese", w);
    1532        2982 :   if (r == 1 && !dw) return cgetg(1,t_VEC);
    1533        2975 :   E = leafcopy(E0); /* do not destroy fa[2] */
    1534        7147 :   for (i = 1; i < r; i++)
    1535        4172 :     if (signe(gel(E,i)) < 0) gel(E,i) = gen_0;
    1536        2975 :   F = factorbackprime(nf, L, E);
    1537        2975 :   if (dw)
    1538             :   {
    1539         686 :     F = ZM_Z_mul(F, dw);
    1540        1568 :     for (i = 1; i < r; i++)
    1541             :     {
    1542         882 :       GEN pr = gel(L,i);
    1543         882 :       long e = itos(gel(E0,i)), v = idealval(nf, dw, pr);
    1544         882 :       if (e >= 0)
    1545         875 :         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        2975 :   U = cgetg(r, t_VEC);
    1556        7147 :   for (i = 1; i < r; i++)
    1557             :   {
    1558             :     GEN u;
    1559        4172 :     if (w && gequal0(gel(w,i))) u = gen_0; /* unused */
    1560             :     else
    1561             :     {
    1562        4095 :       GEN pr = gel(L,i), e = gel(E,i), t;
    1563        4095 :       t = idealdivpowprime(nf,F, pr, e);
    1564        4095 :       u = hnfmerge_get_1(t, idealpow(nf, pr, e));
    1565        4095 :       if (!u) pari_err_COPRIME("idealchinese", t,pr);
    1566             :     }
    1567        4172 :     gel(U,i) = u;
    1568             :   }
    1569        2975 :   F = idealpseudored(F, nf_get_roundG(nf));
    1570        2975 :   return mkvec2(F, U);
    1571             : }
    1572             : 
    1573             : static GEN
    1574        1764 : pl_normalize(GEN nf, GEN pl)
    1575             : {
    1576        1764 :   const char *fun = "idealchinese";
    1577        1764 :   if (lg(pl)-1 != nf_get_r1(nf)) pari_err_TYPE(fun,pl);
    1578        1764 :   switch(typ(pl))
    1579             :   {
    1580         700 :     case t_VEC: RgV_check_ZV(pl,fun); pl = ZV_to_zv(pl);
    1581             :       /* fall through */
    1582        1764 :     case t_VECSMALL: break;
    1583           0 :     default: pari_err_TYPE(fun,pl);
    1584             :   }
    1585        1764 :   return pl;
    1586             : }
    1587             : 
    1588             : static int
    1589        7070 : is_chineseinit(GEN x)
    1590             : {
    1591             :   GEN fa, pl;
    1592             :   long l;
    1593        7070 :   if (typ(x) != t_VEC || lg(x)!=3) return 0;
    1594        5411 :   fa = gel(x,1);
    1595        5411 :   pl = gel(x,2);
    1596        5411 :   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        3115 : chineseinit_i(GEN nf, GEN fa, GEN w, GEN dw)
    1616             : {
    1617        3115 :   const char *fun = "idealchineseinit";
    1618        3115 :   GEN archp = NULL, pl = NULL;
    1619        3115 :   switch(typ(fa))
    1620             :   {
    1621             :     case t_VEC:
    1622        1764 :       if (is_chineseinit(fa))
    1623             :       {
    1624           0 :         if (dw) pari_err_DOMAIN(fun, "denom(y)", "!=", gen_1, w);
    1625           0 :         return fa;
    1626             :       }
    1627        1764 :       if (lg(fa) != 3) pari_err_TYPE(fun, fa);
    1628             :       /* of the form [x,s] */
    1629        1764 :       pl = pl_normalize(nf, gel(fa,2));
    1630        1764 :       fa = gel(fa,1);
    1631        1764 :       archp = vecsmall01_to_indices(pl);
    1632             :       /* keep pr_init, reset pl */
    1633        1764 :       if (is_chineseinit(fa)) { fa = gel(fa,1); break; }
    1634             :       /* fall through */
    1635             :     case t_MAT: /* factorization? */
    1636        2982 :       if (isprfact(fa)) { fa = pr_init(nf, fa, w, dw); break; }
    1637           0 :     default: pari_err_TYPE(fun,fa);
    1638             :   }
    1639             : 
    1640        3115 :   if (!pl) pl = cgetg(1,t_VEC);
    1641             :   else
    1642             :   {
    1643        1764 :     long r = lg(archp);
    1644        1764 :     if (r == 1) pl = cgetg(1, t_VEC);
    1645             :     else
    1646             :     {
    1647        1344 :       GEN F = (lg(fa) == 1)? NULL: gel(fa,1), signs = cgetg(r, t_VECSMALL);
    1648             :       long i;
    1649        1344 :       for (i = 1; i < r; i++) signs[i] = (pl[archp[i]] < 0)? 1: 0;
    1650        1344 :       pl = setsigns_init(nf, archp, F, signs);
    1651             :     }
    1652             :   }
    1653        3115 :   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        5656 : idealchinese(GEN nf, GEN x, GEN w)
    1662             : {
    1663        5656 :   const char *fun = "idealchinese";
    1664        5656 :   pari_sp av = avma;
    1665             :   GEN x1, x2, s, dw, F;
    1666             : 
    1667        5656 :   nf = checknf(nf);
    1668        5656 :   if (!w) return gerepilecopy(av, chineseinit_i(nf,x,NULL,NULL));
    1669             : 
    1670        3542 :   if (typ(w) != t_VEC) pari_err_TYPE(fun,w);
    1671        3542 :   w = Q_remove_denom(matalgtobasis(nf,w), &dw);
    1672        3542 :   if (!is_chineseinit(x)) x = chineseinit_i(nf,x,w,dw);
    1673             :   /* x is a 'chineseinit' */
    1674        3542 :   x1 = gel(x,1); s = NULL;
    1675        3542 :   x2 = gel(x,2);
    1676        3542 :   if (lg(x1) == 1) F = NULL;
    1677             :   else
    1678             :   {
    1679        3521 :     GEN  U = gel(x1,2);
    1680        3521 :     long i, r = lg(w);
    1681        3521 :     F = gel(x1,1);
    1682       10101 :     for (i=1; i<r; i++)
    1683        6580 :       if (!gequal0(gel(w,i)))
    1684             :       {
    1685        4116 :         GEN t = nfmuli(nf, gel(U,i), gel(w,i));
    1686        4116 :         s = s? ZC_add(s,t): t;
    1687             :       }
    1688        3521 :     if (s) s = ZC_reducemodmatrix(s, F);
    1689             :   }
    1690        3542 :   if (lg(x2) != 1) s = nfsetsigns(nf, gel(x2,1), s? s: gen_0, x2);
    1691        3542 :   if (!s) { s = zerocol(nf_get_degree(nf)); dw = NULL; }
    1692             : 
    1693        3542 :   if (dw) s = RgC_Rg_div(s,dw);
    1694        3542 :   return gerepileupto(av, s);
    1695             : }
    1696             : 
    1697             : /*************************************************************************/
    1698             : /**                                                                     **/
    1699             : /**                           (Z_K/I)^*                                 **/
    1700             : /**                                                                     **/
    1701             : /*************************************************************************/
    1702             : GEN
    1703        1764 : vecsmall01_to_indices(GEN v)
    1704             : {
    1705        1764 :   long i, k, l = lg(v);
    1706        1764 :   GEN p = new_chunk(l) + l;
    1707        4739 :   for (k=1, i=l-1; i; i--)
    1708        2975 :     if (v[i]) { *--p = i; k++; }
    1709        1764 :   *--p = evallg(k) | evaltyp(t_VECSMALL);
    1710        1764 :   avma = (pari_sp)p; return p;
    1711             : }
    1712             : GEN
    1713      363349 : vec01_to_indices(GEN v)
    1714             : {
    1715             :   long i, k, l;
    1716             :   GEN p;
    1717             : 
    1718      363349 :   switch (typ(v))
    1719             :   {
    1720      349146 :    case t_VECSMALL: return v;
    1721       14203 :    case t_VEC: break;
    1722           0 :    default: pari_err_TYPE("vec01_to_indices",v);
    1723             :   }
    1724       14203 :   l = lg(v);
    1725       14203 :   p = new_chunk(l) + l;
    1726       41531 :   for (k=1, i=l-1; i; i--)
    1727       27328 :     if (signe(gel(v,i))) { *--p = i; k++; }
    1728       14203 :   *--p = evallg(k) | evaltyp(t_VECSMALL);
    1729       14203 :   avma = (pari_sp)p; return p;
    1730             : }
    1731             : GEN
    1732        5145 : indices_to_vec01(GEN p, long r)
    1733             : {
    1734        5145 :   long i, l = lg(p);
    1735        5145 :   GEN v = zerovec(r);
    1736        5145 :   for (i = 1; i < l; i++) gel(v, p[i]) = gen_1;
    1737        5145 :   return v;
    1738             : }
    1739             : 
    1740             : /* return (column) vector of R1 signatures of x (0 or 1) */
    1741             : GEN
    1742      349146 : nfsign_arch(GEN nf, GEN x, GEN arch)
    1743             : {
    1744      349146 :   GEN sarch, M, V, archp = vec01_to_indices(arch);
    1745      349146 :   long i, s, np, n = lg(archp)-1;
    1746             :   pari_sp av;
    1747             : 
    1748      349146 :   if (!n) return cgetg(1,t_VECSMALL);
    1749      348117 :   nf = checknf(nf);
    1750      348117 :   if (typ(x) == t_MAT)
    1751             :   { /* factorisation */
    1752       99376 :     GEN g = gel(x,1), e = gel(x,2);
    1753       99376 :     V = zero_zv(n);
    1754      288909 :     for (i=1; i<lg(g); i++)
    1755      189533 :       if (mpodd(gel(e,i)))
    1756      164165 :         Flv_add_inplace(V, nfsign_arch(nf,gel(g,i),archp), 2);
    1757       99376 :     avma = (pari_sp)V; return V;
    1758             :   }
    1759      248741 :   av = avma; V = cgetg(n+1,t_VECSMALL);
    1760      248741 :   x = nf_to_scalar_or_basis(nf, x);
    1761      248741 :   switch(typ(x))
    1762             :   {
    1763             :     case t_INT:
    1764       64873 :       s = signe(x);
    1765       64873 :       if (!s) pari_err_DOMAIN("nfsign_arch","element","=",gen_0,x);
    1766       64873 :       avma = av; return const_vecsmall(n, (s < 0)? 1: 0);
    1767             :     case t_FRAC:
    1768          35 :       s = signe(gel(x,1));
    1769          35 :       avma = av; return const_vecsmall(n, (s < 0)? 1: 0);
    1770             :   }
    1771      183833 :   x = Q_primpart(x); M = nf_get_M(nf); sarch = NULL; np = -1;
    1772      495459 :   for (i = 1; i <= n; i++)
    1773             :   {
    1774      312943 :     long s = eval_sign(M, x, archp[i]);
    1775      312943 :     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) { avma = av; return const_vecsmall(n, 1); }
    1783        1179 :         if (np == r1){ 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) { avma = av; V = const_vecsmall(n, 1); V[i] = 0; return V; }
    1790         544 :       if (ni == r1){ avma = av; V = const_vecsmall(n, 0); V[i] = 1; return V; }
    1791           0 :       s = ni < np? 0: 1;
    1792             :     }
    1793      311626 :     V[i] = s;
    1794             :   }
    1795      182516 :   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 :     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) { 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) { avma = av; return single? gen_0: zerovec(l-1); }
    1914        1449 :   d = degpol(f);
    1915        1449 :   if (d == 1) { 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) { 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      603519 : zk_modHNF(GEN x, GEN A)
    1958      603519 : { 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         147 : nfinvmodideal(GEN nf, GEN x, GEN y)
    1964             : {
    1965         147 :   pari_sp av = avma;
    1966         147 :   GEN a, yZ = gcoeff(y,1,1);
    1967             : 
    1968         147 :   if (is_pm1(yZ)) return gen_0;
    1969         147 :   x = nf_to_scalar_or_basis(nf, x);
    1970         147 :   if (typ(x) == t_INT) return gerepileupto(av, Fp_inv(x, yZ));
    1971             : 
    1972          70 :   a = hnfmerge_get_1(idealhnf_principal(nf,x), y);
    1973          70 :   if (!a) pari_err_INV("nfinvmodideal", x);
    1974          70 :   return gerepileupto(av, zk_modHNF(nfdiv(nf,a,x), y));
    1975             : }
    1976             : 
    1977             : static GEN
    1978      277061 : nfsqrmodideal(GEN nf, GEN x, GEN id)
    1979      277061 : { return zk_modHNF(nfsqri(nf,x), id); }
    1980             : static GEN
    1981      734999 : nfmulmodideal(GEN nf, GEN x, GEN y, GEN id)
    1982      734999 : { return x? zk_modHNF(nfmuli(nf,x,y), id): y; }
    1983             : /* assume x integral, k integer, A in HNF */
    1984             : GEN
    1985      475670 : nfpowmodideal(GEN nf,GEN x,GEN k,GEN A)
    1986             : {
    1987      475670 :   long s = signe(k);
    1988             :   pari_sp av;
    1989             :   GEN y;
    1990             : 
    1991      475670 :   if (!s) return gen_1;
    1992      475670 :   av = avma;
    1993      475670 :   x = nf_to_scalar_or_basis(nf, x);
    1994      475670 :   if (typ(x) != t_COL) return Fp_pow(x, k, gcoeff(A,1,1));
    1995      226033 :   if (s < 0) { x = nfinvmodideal(nf, x,A); k = absi(k); }
    1996      226033 :   for(y = NULL;;)
    1997             :   {
    1998      780155 :     if (mpodd(k)) y = nfmulmodideal(nf,y,x,A);
    1999      503094 :     k = shifti(k,-1); if (!signe(k)) break;
    2000      277061 :     x = nfsqrmodideal(nf,x,A);
    2001             :   }
    2002      226033 :   return gerepileupto(av, y);
    2003             : }
    2004             : 
    2005             : /* a * g^n mod id */
    2006             : static GEN
    2007      422519 : nfmulpowmodideal(GEN nf, GEN a, GEN g, GEN n, GEN id)
    2008             : {
    2009      422519 :   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      194702 : famat_to_nf_modideal_coprime(GEN nf, GEN g, GEN e, GEN id, GEN EX)
    2016             : {
    2017      194702 :   GEN EXo2, plus = NULL, minus = NULL, idZ = gcoeff(id,1,1);
    2018      194702 :   long i, lx = lg(g);
    2019             : 
    2020      194702 :   if (is_pm1(idZ)) return gen_1; /* id = Z_K */
    2021      194471 :   EXo2 = (expi(EX) > 10)? shifti(EX,-1): NULL;
    2022      873507 :   for (i = 1; i < lx; i++)
    2023             :   {
    2024      679036 :     GEN h, n = centermodii(gel(e,i), EX, EXo2);
    2025      679036 :     long sn = signe(n);
    2026      679036 :     if (!sn) continue;
    2027             : 
    2028      324192 :     h = nf_to_scalar_or_basis(nf, gel(g,i));
    2029      324192 :     switch(typ(h))
    2030             :     {
    2031      199774 :       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      124418 :         h = Q_remove_denom(h, &dh);
    2038      124418 :         if (dh) h = FpC_Fp_mul(h, Fp_inv(dh,idZ), idZ);
    2039             :       }
    2040             :     }
    2041      324192 :     if (sn > 0)
    2042      322785 :       plus = nfmulpowmodideal(nf, plus, h, n, id);
    2043             :     else /* sn < 0 */
    2044        1407 :       minus = nfmulpowmodideal(nf, minus, h, absi(n), id);
    2045             :   }
    2046      194471 :   if (minus) plus = nfmulmodideal(nf, plus, nfinvmodideal(nf,minus,id), id);
    2047      194471 :   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       20825 : zidealij(GEN x, GEN y)
    2054             : {
    2055       20825 :   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       20825 :   cyc = ZM_snf_group(ZM_Z_divexact(ZM_mul(xi, y), xp), &U, &G);
    2060       20825 :   N = lg(cyc); G = ZM_mul(x,G); settyp(G, t_VEC); /* new generators */
    2061       77651 :   for (j=1; j<N; j++)
    2062             :   {
    2063       56826 :     GEN c = gel(G,j);
    2064       56826 :     gel(c,1) = addiu(gel(c,1), 1); /* 1 + g_j */
    2065       56826 :     if (ZV_isscalar(c)) gel(G,j) = gel(c,1);
    2066             :   }
    2067       20825 :   return mkvec4(cyc, G, ZM_mul(U,xi), xp);
    2068             : }
    2069             : 
    2070             : /* lg(x) > 1, x + 1; shallow */
    2071             : static GEN
    2072        6153 : ZC_add1(GEN x)
    2073             : {
    2074        6153 :   long i, l = lg(x);
    2075        6153 :   GEN y = cgetg(l, t_COL);
    2076        6153 :   for (i = 2; i < l; i++) gel(y,i) = gel(x,i);
    2077        6153 :   gel(y,1) = addiu(gel(x,1), 1); return y;
    2078             : }
    2079             : /* lg(x) > 1, x - 1; shallow */
    2080             : static GEN
    2081        3654 : ZC_sub1(GEN x)
    2082             : {
    2083        3654 :   long i, l = lg(x);
    2084        3654 :   GEN y = cgetg(l, t_COL);
    2085        3654 :   for (i = 2; i < l; i++) gel(y,i) = gel(x,i);
    2086        3654 :   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       12131 : zkadd1(GEN x)
    2102             : {
    2103       12131 :   long tx = typ(x);
    2104       12131 :   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       12131 : zksub1(GEN x)
    2109             : {
    2110       12131 :   long tx = typ(x);
    2111       12131 :   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       12131 : zkmul(GEN x, GEN y)
    2126             : {
    2127       12131 :   long tx = typ(x), ty = typ(y);
    2128       12131 :   if (ty == t_INT)
    2129        8477 :     return tx == t_INT? mulii(x,y): ZC_Z_mul(gel(x,1),y);
    2130             :   else
    2131        3654 :     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       12131 : zkchinese1(GEN zkc, GEN x)
    2147             : {
    2148       12131 :   GEN v = gel(zkc,1), UV = gel(zkc,2), z = zkadd1(zkmul(v, zksub1(x)));
    2149       12131 :   return (typ(z) == t_INT)? z: ZC_hnfrem(z, UV);
    2150             : }
    2151             : static GEN
    2152       10969 : zkVchinese1(GEN zkc, GEN v)
    2153             : {
    2154             :   long i, ly;
    2155       10969 :   GEN y = cgetg_copy(v, &ly);
    2156       10969 :   for (i=1; i<ly; i++) gel(y,i) = zkchinese1(zkc, gel(v,i));
    2157       10969 :   return y;
    2158             : }
    2159             : 
    2160             : /* prepare to solve z = x (mod A), z = y mod (B) [zkchinese or zkchinese1] */
    2161             : GEN
    2162       10710 : zkchineseinit(GEN nf, GEN A, GEN B, GEN AB)
    2163             : {
    2164             :   GEN v;
    2165             :   long e;
    2166       10710 :   nf = checknf(nf);
    2167       10710 :   v = idealaddtoone_raw(nf, A, B);
    2168       10710 :   if ((e = gexpo(v)) > 5)
    2169             :   {
    2170         490 :     GEN b = (typ(v) == t_COL)? v: scalarcol_shallow(v, nf_get_degree(nf));
    2171         490 :     b= ZC_reducemodlll(b, AB);
    2172         490 :     if (gexpo(b) < e) v = b;
    2173             :   }
    2174       10710 :   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      354443 : apply_U(GEN L, GEN a)
    2190             : {
    2191      354443 :   GEN e, U = gel(L,3), dU = gel(L,4);
    2192      354443 :   if (typ(a) == t_INT)
    2193      130715 :     e = ZC_Z_mul(gel(U,1), subiu(a, 1));
    2194             :   else
    2195             :   { /* t_COL */
    2196      223728 :     GEN t = gel(a,1);
    2197      223728 :     gel(a,1) = subiu(gel(a,1), 1); /* a -= 1 */
    2198      223728 :     e = ZM_ZC_mul(U, a);
    2199      223728 :     gel(a,1) = t; /* restore */
    2200             :   }
    2201      354443 :   return gdiv(e, dU);
    2202             : }
    2203             : 
    2204             : /* true nf; vectors of [[cyc],[g],U.X^-1]. Assume k > 1. */
    2205             : static GEN
    2206       14161 : principal_units(GEN nf, GEN pr, long k, GEN prk)
    2207             : {
    2208             :   GEN list, prb;
    2209       14161 :   ulong mask = quadratic_prec_mask(k);
    2210       14161 :   long a = 1;
    2211             : 
    2212       14161 :   if (DEBUGLEVEL>3) err_printf("treating pr^%ld, pr = %Ps\n",k,pr);
    2213       14161 :   prb = pr_hnf(nf,pr);
    2214       14161 :   list = vectrunc_init(k);
    2215       49147 :   while (mask > 1)
    2216             :   {
    2217       20825 :     GEN pra = prb;
    2218       20825 :     long b = a << 1;
    2219             : 
    2220       20825 :     if (mask & 1) b--;
    2221       20825 :     mask >>= 1;
    2222             :     /* compute 1 + pr^a / 1 + pr^b, 2a <= b */
    2223       20825 :     if(DEBUGLEVEL>3) err_printf("  treating a = %ld, b = %ld\n",a,b);
    2224       20825 :     prb = (b >= k)? prk: idealpows(nf,pr,b);
    2225       20825 :     vectrunc_append(list, zidealij(pra, prb));
    2226       20825 :     a = b;
    2227             :   }
    2228       14161 :   return list;
    2229             : }
    2230             : /* a = 1 mod (pr) return log(a) on local-gens of 1+pr/1+pr^k */
    2231             : static GEN
    2232      224908 : log_prk1(GEN nf, GEN a, long nh, GEN L2, GEN prk)
    2233             : {
    2234      224908 :   GEN y = cgetg(nh+1, t_COL);
    2235      224908 :   long j, iy, c = lg(L2)-1;
    2236      579344 :   for (j = iy = 1; j <= c; j++)
    2237             :   {
    2238      354443 :     GEN L = gel(L2,j), cyc = gel(L,1), gen = gel(L,2), E = apply_U(L,a);
    2239      354443 :     long i, nc = lg(cyc)-1;
    2240      354443 :     int last = (j == c);
    2241     1241159 :     for (i = 1; i <= nc; i++, iy++)
    2242             :     {
    2243      886723 :       GEN t, e = gel(E,i);
    2244      886723 :       if (typ(e) != t_INT) pari_err_COPRIME("zlog_prk1", a, prk);
    2245      886716 :       t = Fp_neg(e, gel(cyc,i));
    2246      886716 :       gel(y,iy) = negi(t);
    2247      886716 :       if (!last && signe(t)) a = nfmulpowmodideal(nf, a, gel(gen,i), t, prk);
    2248             :     }
    2249             :   }
    2250      224901 :   return y;
    2251             : }
    2252             : /* true nf */
    2253             : static GEN
    2254        5761 : principal_units_relations(GEN nf, GEN L2, GEN prk, long nh)
    2255             : {
    2256        5761 :   GEN h = cgetg(nh+1,t_MAT);
    2257        5761 :   long ih, j, c = lg(L2)-1;
    2258       18186 :   for (j = ih = 1; j <= c; j++)
    2259             :   {
    2260       12425 :     GEN L = gel(L2,j), F = gel(L,1), G = gel(L,2);
    2261       12425 :     long k, lG = lg(G);
    2262       51506 :     for (k = 1; k < lG; k++,ih++)
    2263             :     { /* log(g^f) mod pr^e */
    2264       39081 :       GEN a = nfpowmodideal(nf,gel(G,k),gel(F,k),prk);
    2265       39081 :       gel(h,ih) = ZC_neg(log_prk1(nf, a, nh, L2, prk));
    2266       39081 :       gcoeff(h,ih,ih) = gel(F,k);
    2267             :     }
    2268             :   }
    2269        5761 :   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       14161 : idealprincipalunits_i(GEN nf, GEN pr, long k, GEN *pU)
    2275             : {
    2276       14161 :   GEN cyc, gen, L2, prk = idealpows(nf, pr, k);
    2277             : 
    2278       14161 :   L2 = principal_units(nf, pr, k, prk);
    2279       14161 :   if (k == 2)
    2280             :   {
    2281        8400 :     GEN L = gel(L2,1);
    2282        8400 :     cyc = gel(L,1);
    2283        8400 :     gen = gel(L,2);
    2284        8400 :     if (pU) *pU = matid(lg(gen)-1);
    2285             :   }
    2286             :   else
    2287             :   {
    2288        5761 :     long c = lg(L2), j;
    2289        5761 :     GEN EX, h, Ui, vg = cgetg(c, t_VEC);
    2290        5761 :     for (j = 1; j < c; j++) gel(vg, j) = gmael(L2,j,2);
    2291        5761 :     vg = shallowconcat1(vg);
    2292        5761 :     h = principal_units_relations(nf, L2, prk, lg(vg)-1);
    2293        5761 :     h = ZM_hnfall_i(h, NULL, 0);
    2294        5761 :     cyc = ZM_snf_group(h, pU, &Ui);
    2295        5761 :     c = lg(Ui); gen = cgetg(c, t_VEC); EX = gel(cyc,1);
    2296       32550 :     for (j = 1; j < c; j++)
    2297       26789 :       gel(gen,j) = famat_to_nf_modideal_coprime(nf, vg, gel(Ui,j), prk, EX);
    2298             :   }
    2299       14161 :   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       31773 : 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       31773 :   long k = itos(gk), f = pr_get_f(pr);
    2329             : 
    2330       31773 :   if(DEBUGLEVEL>3) err_printf("treating pr^%ld, pr = %Ps\n",k,pr);
    2331       31773 :   modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    2332             :   /* (Z_K / pr)^* */
    2333       31773 :   if (f == 1)
    2334             :   {
    2335       22785 :     g0 = g = pgener_Fp(p);
    2336       22785 :     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       31773 :   A = gel(ord0, 1); /* Norm(pr)-1 */
    2345       31773 :   if (k == 1)
    2346             :   {
    2347       17717 :     cyc = mkvec(A);
    2348       17717 :     gen = mkvec(g);
    2349       17717 :     prk = pr_hnf(nf,pr);
    2350       17717 :     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       14056 :     w = idealprincipalunits_i(nf, pr, k, &U);
    2357             :     /* incorporate (Z_K/pr)^*, order A coprime to B = expo(1+pr/1+pr^k)*/
    2358       14056 :     cyc = leafcopy(gel(w,1)); B = gel(cyc,1); AB = mulii(A,B);
    2359       14056 :     gen = leafcopy(gel(w,2));
    2360       14056 :     prk = gel(w,3);
    2361       14056 :     g = nfpowmodideal(nf, g, B, prk);
    2362       14056 :     g0 = Fq_pow(g0, modii(B,A), T, p); /* update primitive root */
    2363       14056 :     L2 = mkvec3(A, g, gel(w,4));
    2364       14056 :     gel(cyc,1) = AB;
    2365       14056 :     gel(gen,1) = nfmulmodideal(nf, gel(gen,1), g, prk);
    2366       14056 :     u = mulii(Fp_inv(A,B), A);
    2367       14056 :     v = subui(1, u); l = lg(U);
    2368       14056 :     for (j = 1; j < l; j++) gcoeff(U,1,j) = Fp_mul(u, gcoeff(U,1,j), AB);
    2369       14056 :     U = mkvec2(Rg_col_ei(v, lg(gen)-1, 1), U);
    2370             :   }
    2371             :   /* local-gens of (Z_K/pr^k)^* = SNF-gens * U */
    2372       31773 :   if (x)
    2373             :   {
    2374       10451 :     GEN uv = zkchineseinit(nf, idealdivpowprime(nf,x,pr,gk), prk, x);
    2375       10451 :     gen = zkVchinese1(uv, gen);
    2376             :   }
    2377       31773 :   return mkvecn(U? 6: 4, cyc, gen, prk, mkvec3(modpr,g0,ord0), L2, U);
    2378             : }
    2379             : static GEN
    2380      346607 : 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       25641 : sprk_get_gen(GEN s) { return gel(s,2); }
    2389             : static GEN
    2390      296102 : sprk_get_prk(GEN s) { return gel(s,3); }
    2391             : static GEN
    2392      386519 : sprk_get_ff(GEN s) { return gel(s,4); }
    2393             : static GEN
    2394      129238 : 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      196698 : sprk_get_L2(GEN s, GEN *A, GEN *g, GEN *L2)
    2398      196698 : { GEN v = gel(s,5); *A = gel(v,1); *g = gel(v,2); *L2 = gel(v,3); }
    2399             : static void
    2400      185827 : sprk_get_U2(GEN s, GEN *U1, GEN *U2)
    2401      185827 : { GEN v = gel(s,6); *U1 = gel(v,1); *U2 = gel(v,2); }
    2402             : static int
    2403      386519 : 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 zlog_pr(nf, x, sprk);
    2412             : }
    2413             : /* log_g(a) in (Z_K/pr)^* */
    2414             : static GEN
    2415      386519 : nf_log(GEN nf, GEN a, GEN ff)
    2416             : {
    2417      386519 :   GEN pr = gel(ff,1), g = gel(ff,2), ord = gel(ff,3);
    2418      386519 :   GEN T,p, modpr = nf_to_Fq_init(nf, &pr, &T, &p);
    2419      386519 :   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      387555 : zlog_pr(GEN nf, GEN a, GEN sprk)
    2425             : {
    2426             :   GEN e, prk, A, g, L2, U1, U2, y;
    2427             : 
    2428      387555 :   if (typ(a) == t_MAT) return famat_zlog_pr(nf, gel(a,1), gel(a,2), sprk);
    2429             : 
    2430      386519 :   e = nf_log(nf, a, sprk_get_ff(sprk));
    2431      386519 :   if (sprk_is_prime(sprk)) return mkcol(e); /* k = 1 */
    2432      185827 :   prk = sprk_get_prk(sprk);
    2433      185827 :   sprk_get_L2(sprk, &A,&g,&L2);
    2434      185827 :   if (signe(e))
    2435             :   {
    2436       46027 :     e = Fp_neg(e, A);
    2437       46027 :     a = nfmulpowmodideal(nf, a, g, e, prk);
    2438             :   }
    2439      185827 :   sprk_get_U2(sprk, &U1,&U2);
    2440      185827 :   y = ZM_ZC_mul(U2, log_prk1(nf, a, lg(U2)-1, L2, prk));
    2441      185820 :   if (signe(e)) y = ZC_sub(y, ZC_Z_mul(U1,e));
    2442      185820 :   return vecmodii(y, sprk_get_cyc(sprk));
    2443             : }
    2444             : GEN
    2445        6132 : zlog_pr_init(GEN nf, GEN pr, long k)
    2446        6132 : { return sprkinit(checknf(nf),pr,utoipos(k),NULL);}
    2447             : GEN
    2448         378 : vzlog_pr(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) = zlog_pr(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       26068 : split_U(GEN U, GEN Sprk)
    2478             : {
    2479       26068 :   long t = 0, k, n, l = lg(Sprk);
    2480       26068 :   GEN vU = cgetg(l+1, t_VEC);
    2481       50939 :   for (k = 1; k < l; k++)
    2482             :   {
    2483       24871 :     n = lg(sprk_get_cyc(gel(Sprk,k))) - 1; /* > 0 */
    2484       24871 :     gel(vU,k) = vecslice(U, t+1, t+n);
    2485       24871 :     t += n;
    2486             :   }
    2487             :   /* t+1 .. lg(U)-1 */
    2488       26068 :   n = lg(U) - t - 1; /* can be 0 */
    2489       26068 :   if (!n) setlg(vU,l); else gel(vU,l) = vecslice(U, t+1, t+n);
    2490       26068 :   return vU;
    2491             : }
    2492             : 
    2493             : void
    2494      353079 : init_zlog(zlog_S *S, GEN bid)
    2495             : {
    2496      353079 :   GEN fa2 = bid_get_fact2(bid);
    2497      353079 :   S->U = bid_get_U(bid);
    2498      353079 :   S->hU = lg(bid_get_cyc(bid))-1;
    2499      353079 :   S->archp = bid_get_archp(bid);
    2500      353079 :   S->sprk = bid_get_sprk(bid);
    2501      353079 :   S->bid = bid;
    2502      353079 :   S->P = gel(fa2,1);
    2503      353079 :   S->k = gel(fa2,2);
    2504      353079 :   S->no2 = lg(S->P) == lg(gel(bid_get_fact(bid),1));
    2505      353079 : }
    2506             : 
    2507             : /* a a t_FRAC/t_INT, reduce mod bid */
    2508             : static GEN
    2509           7 : Q_mod_bid(GEN bid, GEN a)
    2510             : {
    2511           7 :   GEN xZ = gcoeff(bid_get_ideal(bid),1,1);
    2512           7 :   GEN b = Rg_to_Fp(a, xZ);
    2513           7 :   if (gsigne(a) < 0) b = subii(b, xZ);
    2514           7 :   return b;
    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      245965 : zlog(GEN nf, GEN a, GEN sgn, zlog_S *S)
    2520             : {
    2521             :   long k, l;
    2522             :   GEN y;
    2523      245965 :   a = nf_to_scalar_or_basis(nf, a);
    2524      245965 :   switch(typ(a))
    2525             :   {
    2526       63980 :     case t_INT: break;
    2527           7 :     case t_FRAC: a = Q_mod_bid(S->bid, a); break;
    2528             :     default: /* case t_COL: */
    2529             :     {
    2530             :       GEN den;
    2531      181978 :       check_nfelt(a, &den);
    2532      181978 :       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      199558 :   if (sgn)
    2541       34580 :     sgn = (lg(sgn) == 1)? NULL: leafcopy(sgn);
    2542             :   else
    2543      164978 :     sgn = (lg(S->archp) == 1)? NULL: nfsign_arch(nf, a, S->archp);
    2544      199558 :   l = lg(S->sprk);
    2545      199558 :   y = cgetg(sgn? l+1: l, t_COL);
    2546      441110 :   for (k = 1; k < l; k++)
    2547             :   {
    2548      241559 :     GEN sprk = gel(S->sprk,k);
    2549      241559 :     gel(y,k) = zlog_pr(nf, a, sprk);
    2550             :   }
    2551      199551 :   if (sgn) gel(y,l) = Flc_to_ZC(sgn);
    2552      199551 :   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             :     {
    2570        1064 :       if (is_pm1(gcoeff(H,i,i))) continue;
    2571         840 :       perm[k++] = i;
    2572             :     }
    2573             :   }
    2574        6916 :   return perm;
    2575             : }
    2576             : 
    2577             : /* \sum U[i]*y[i], U[i] ZM, y[i] ZC. We allow lg(y) > lg(U). */
    2578             : static GEN
    2579      312766 : ZMV_ZCV_mul(GEN U, GEN y)
    2580             : {
    2581      312766 :   long i, l = lg(U);
    2582      312766 :   GEN z = NULL;
    2583      312766 :   if (l == 1) return cgetg(1,t_COL);
    2584      861313 :   for (i = 1; i < l; i++)
    2585             :   {
    2586      548547 :     GEN u = ZM_ZC_mul(gel(U,i), gel(y,i));
    2587      548547 :     z = z? ZC_add(z, u): u;
    2588             :   }
    2589      312766 :   return z;
    2590             : }
    2591             : /* A * (U[1], ..., U[d] */
    2592             : static GEN
    2593         518 : ZM_ZMV_mul(GEN A, GEN U)
    2594             : {
    2595             :   long i, l;
    2596         518 :   GEN V = cgetg_copy(U,&l);
    2597         518 :   for (i = 1; i < l; i++) gel(V,i) = ZM_mul(A,gel(U,i));
    2598         518 :   return V;
    2599             : }
    2600             : 
    2601             : /* Log on bid.gen of generators of P_{1,I pr^{e-1}} / P_{1,I pr^e} (I,pr) = 1,
    2602             :  * defined implicitly via CRT. 'ind' is the index of pr in modulus
    2603             :  * factorization */
    2604             : GEN
    2605       50757 : log_gen_pr(zlog_S *S, long ind, GEN nf, long e)
    2606             : {
    2607       50757 :   GEN A, sprk = gel(S->sprk,ind), Uind = gel(S->U, ind);
    2608             : 
    2609       50757 :   if (e == 1) retmkmat( gel(Uind,1) );
    2610             :   else
    2611             :   {
    2612       18963 :     GEN G, pr = sprk_get_pr(sprk);
    2613             :     long i, l;
    2614       18963 :     if (e == 2)
    2615             :     {
    2616       10871 :       GEN A, g, L, L2; sprk_get_L2(sprk,&A,&g,&L2); L = gel(L2,1);
    2617       10871 :       G = gel(L,2); l = lg(G);
    2618             :     }
    2619             :     else
    2620             :     {
    2621        8092 :       GEN perm = pr_basis_perm(nf,pr), PI = nfpow_u(nf, pr_get_gen(pr), e-1);
    2622        8092 :       l = lg(perm);
    2623        8092 :       G = cgetg(l, t_VEC);
    2624        8092 :       if (typ(PI) == t_INT)
    2625             :       { /* zk_ei_mul doesn't allow t_INT */
    2626        1421 :         long N = nf_get_degree(nf);
    2627        1421 :         gel(G,1) = addiu(PI,1);
    2628        2289 :         for (i = 2; i < l; i++)
    2629             :         {
    2630         868 :           GEN z = col_ei(N, 1);
    2631         868 :           gel(G,i) = z; gel(z, perm[i]) = PI;
    2632             :         }
    2633             :       }
    2634             :       else
    2635             :       {
    2636        6671 :         gel(G,1) = nfadd(nf, gen_1, PI);
    2637        6881 :         for (i = 2; i < l; i++)
    2638         210 :           gel(G,i) = nfadd(nf, gen_1, zk_ei_mul(nf, PI, perm[i]));
    2639             :       }
    2640             :     }
    2641       18963 :     A = cgetg(l, t_MAT);
    2642       40677 :     for (i = 1; i < l; i++)
    2643       21714 :       gel(A,i) = ZM_ZC_mul(Uind, zlog_pr(nf, gel(G,i), sprk));
    2644       18963 :     return A;
    2645             :   }
    2646             : }
    2647             : /* Log on bid.gen of generator of P_{1,f} / P_{1,f v[index]}
    2648             :  * v = vector of r1 real places */
    2649             : GEN
    2650        9989 : log_gen_arch(zlog_S *S, long index)
    2651             : {
    2652        9989 :   GEN U = gel(S->U, lg(S->U)-1);
    2653        9989 :   return gel(U, index);
    2654             : }
    2655             : 
    2656             : /* compute bid.clgp: [h,cyc] or [h,cyc,gen] */
    2657             : static GEN
    2658       27125 : bid_grp(GEN nf, GEN U, GEN cyc, GEN g, GEN F, GEN sarch)
    2659             : {
    2660       27125 :   GEN G, h = ZV_prod(cyc);
    2661             :   long c;
    2662       27125 :   if (!U) return mkvec2(h,cyc);
    2663       26873 :   c = lg(U);
    2664       26873 :   G = cgetg(c,t_VEC);
    2665       26873 :   if (c > 1)
    2666             :   {
    2667       22477 :     GEN U0, Uoo, EX = gel(cyc,1); /* exponent of bid */
    2668       22477 :     long i, hU = nbrows(U), nba = lg(sarch_get_cyc(sarch))-1; /* #f_oo */
    2669       22477 :     if (!nba) { U0 = U; Uoo = NULL; }
    2670       11746 :     else if (nba == hU) { U0 = NULL; Uoo = U; }
    2671             :     else
    2672             :     {
    2673        9513 :       U0 = rowslice(U, 1, hU-nba);
    2674        9513 :       Uoo = rowslice(U, hU-nba+1, hU);
    2675             :     }
    2676       64428 :     for (i = 1; i < c; i++)
    2677             :     {
    2678       41951 :       GEN t = gen_1;
    2679       41951 :       if (U0) t = famat_to_nf_modideal_coprime(nf, g, gel(U0,i), F, EX);
    2680       41951 :       if (Uoo) t = set_sign_mod_divisor(nf, ZV_to_Flv(gel(Uoo,i),2), t, sarch);
    2681       41951 :       gel(G,i) = t;
    2682             :     }
    2683             :   }
    2684       26873 :   return mkvec3(h, cyc, G);
    2685             : }
    2686             : 
    2687             : /* remove prime ideals of norm 2 with exponent 1 from factorization */
    2688             : static GEN
    2689       26810 : famat_strip2(GEN fa)
    2690             : {
    2691       26810 :   GEN P = gel(fa,1), E = gel(fa,2), Q, F;
    2692       26810 :   long l = lg(P), i, j;
    2693       26810 :   Q = cgetg(l, t_COL);
    2694       26810 :   F = cgetg(l, t_COL);
    2695       56483 :   for (i = j = 1; i < l; i++)
    2696             :   {
    2697       29673 :     GEN pr = gel(P,i), e = gel(E,i);
    2698       29673 :     if (!absequaliu(pr_get_p(pr), 2) || itou(e) != 1 || pr_get_f(pr) != 1)
    2699             :     {
    2700       25641 :       gel(Q,j) = pr;
    2701       25641 :       gel(F,j) = e; j++;
    2702             :     }
    2703             :   }
    2704       26810 :   setlg(Q,j);
    2705       26810 :   setlg(F,j); return mkmat2(Q,F);
    2706             : }
    2707             : 
    2708             : /* Compute [[ideal,arch], [h,[cyc],[gen]], idealfact, [liste], U]
    2709             :    flag may include nf_GEN | nf_INIT */
    2710             : static GEN
    2711       26831 : Idealstar_i(GEN nf, GEN ideal, long flag)
    2712             : {
    2713             :   long i, k, nbp, R1;
    2714       26831 :   GEN y, cyc, U, u1 = NULL, fa, fa2, sprk, x, arch, archp, E, P, sarch, gen;
    2715             : 
    2716       26831 :   nf = checknf(nf);
    2717       26831 :   R1 = nf_get_r1(nf);
    2718       26831 :   if (typ(ideal) == t_VEC && lg(ideal) == 3)
    2719             :   {
    2720       12908 :     arch = gel(ideal,2);
    2721       12908 :     ideal= gel(ideal,1);
    2722       12908 :     switch(typ(arch))
    2723             :     {
    2724             :       case t_VEC:
    2725       12509 :         if (lg(arch) != R1+1)
    2726           0 :           pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
    2727       12509 :         archp = vec01_to_indices(arch);
    2728       12509 :         break;
    2729             :       case t_VECSMALL:
    2730         399 :         archp = arch;
    2731         399 :         k = lg(archp)-1;
    2732         399 :         if (k && archp[k] > R1)
    2733           7 :           pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
    2734         392 :         arch = indices_to_vec01(archp, R1);
    2735         392 :         break;
    2736             :       default:
    2737           0 :         pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
    2738           0 :         return NULL;
    2739             :     }
    2740       12901 :   }
    2741             :   else
    2742             :   {
    2743       13923 :     arch = zerovec(R1);
    2744       13923 :     archp = cgetg(1, t_VECSMALL);
    2745             :   }
    2746       26824 :   if (is_nf_factor(ideal))
    2747             :   {
    2748         714 :     fa = ideal;
    2749         714 :     x = idealfactorback(nf, gel(fa,1), gel(fa,2), 0);
    2750             :   }
    2751             :   else
    2752             :   {
    2753       26110 :     fa = idealfactor(nf, ideal);
    2754       26103 :     x = ideal;
    2755             :   }
    2756       26817 :   if (typ(x) != t_MAT)  x = idealhnf_shallow(nf, x);
    2757       26817 :   if (lg(x) == 1) pari_err_DOMAIN("Idealstar", "ideal","=",gen_0,x);
    2758       26817 :   if (typ(gcoeff(x,1,1)) != t_INT)
    2759           7 :     pari_err_DOMAIN("Idealstar","denominator(ideal)", "!=",gen_1,x);
    2760       26810 :   sarch = nfarchstar(nf, x, archp);
    2761       26810 :   fa2 = famat_strip2(fa);
    2762       26810 :   P = gel(fa2,1);
    2763       26810 :   E = gel(fa2,2);
    2764       26810 :   nbp = lg(P)-1;
    2765       26810 :   sprk = cgetg(nbp+1,t_VEC);
    2766       26810 :   if (nbp)
    2767             :   {
    2768       20160 :     GEN t = (nbp==1)? NULL: x;
    2769       20160 :     cyc = cgetg(nbp+2,t_VEC);
    2770       20160 :     gen = cgetg(nbp+1,t_VEC);
    2771       45801 :     for (i = 1; i <= nbp; i++)
    2772             :     {
    2773       25641 :       GEN L = sprkinit(nf, gel(P,i), gel(E,i), t);
    2774       25641 :       gel(sprk,i) = L;
    2775       25641 :       gel(cyc,i) = sprk_get_cyc(L);
    2776             :       /* true gens are congruent to those mod x AND positive at archp */
    2777       25641 :       gel(gen,i) = sprk_get_gen(L);
    2778             :     }
    2779       20160 :     gel(cyc,i) = sarch_get_cyc(sarch);
    2780       20160 :     cyc = shallowconcat1(cyc);
    2781       20160 :     gen = shallowconcat1(gen);
    2782       20160 :     cyc = ZV_snf_group(cyc, &U, (flag & nf_GEN)? &u1: NULL);
    2783             :   }
    2784             :   else
    2785             :   {
    2786        6650 :     cyc = sarch_get_cyc(sarch);
    2787        6650 :     gen = cgetg(1,t_VEC);
    2788        6650 :     U = matid(lg(cyc)-1);
    2789        6650 :     if (flag & nf_GEN) u1 = U;
    2790             :   }
    2791       26810 :   y = bid_grp(nf, u1, cyc, gen, x, sarch);
    2792       26810 :   if (!(flag & nf_INIT)) return y;
    2793       26012 :   U = split_U(U, sprk);
    2794       26012 :   return mkvec5(mkvec2(x, arch), y, mkvec2(fa,fa2), mkvec2(sprk, sarch), U);
    2795             : }
    2796             : GEN
    2797       26558 : Idealstar(GEN nf, GEN ideal, long flag)
    2798             : {
    2799       26558 :   pari_sp av = avma;
    2800       26558 :   if (!nf) nf = nfinit(pol_x(0), DEFAULTPREC);
    2801       26558 :   return gerepilecopy(av, Idealstar_i(nf, ideal, flag));
    2802             : }
    2803             : GEN
    2804         273 : Idealstarprk(GEN nf, GEN pr, long k, long flag)
    2805             : {
    2806         273 :   pari_sp av = avma;
    2807         273 :   GEN z = Idealstar_i(nf, mkmat2(mkcol(pr),mkcols(k)), flag);
    2808         273 :   return gerepilecopy(av, z);
    2809             : }
    2810             : 
    2811             : /* FIXME: obsolete */
    2812             : GEN
    2813           0 : zidealstarinitgen(GEN nf, GEN ideal)
    2814           0 : { return Idealstar(nf,ideal, nf_INIT|nf_GEN); }
    2815             : GEN
    2816           0 : zidealstarinit(GEN nf, GEN ideal)
    2817           0 : { return Idealstar(nf,ideal, nf_INIT); }
    2818             : GEN
    2819           0 : zidealstar(GEN nf, GEN ideal)
    2820           0 : { return Idealstar(nf,ideal, nf_GEN); }
    2821             : 
    2822             : GEN
    2823          63 : idealstar0(GEN nf, GEN ideal,long flag)
    2824             : {
    2825          63 :   switch(flag)
    2826             :   {
    2827           0 :     case 0: return Idealstar(nf,ideal, nf_GEN);
    2828          49 :     case 1: return Idealstar(nf,ideal, nf_INIT);
    2829          14 :     case 2: return Idealstar(nf,ideal, nf_INIT|nf_GEN);
    2830           0 :     default: pari_err_FLAG("idealstar");
    2831             :   }
    2832             :   return NULL; /* LCOV_EXCL_LINE */
    2833             : }
    2834             : 
    2835             : void
    2836      181978 : check_nfelt(GEN x, GEN *den)
    2837             : {
    2838      181978 :   long l = lg(x), i;
    2839      181978 :   GEN t, d = NULL;
    2840      181978 :   if (typ(x) != t_COL) pari_err_TYPE("check_nfelt", x);
    2841      663597 :   for (i=1; i<l; i++)
    2842             :   {
    2843      481619 :     t = gel(x,i);
    2844      481619 :     switch (typ(t))
    2845             :     {
    2846      386525 :       case t_INT: break;
    2847             :       case t_FRAC:
    2848       95094 :         if (!d) d = gel(t,2); else d = lcmii(d, gel(t,2));
    2849       95094 :         break;
    2850           0 :       default: pari_err_TYPE("check_nfelt", x);
    2851             :     }
    2852             :   }
    2853      181978 :   *den = d;
    2854      181978 : }
    2855             : 
    2856             : GEN
    2857     1206600 : vecmodii(GEN x, GEN y)
    2858     1206600 : { pari_APPLY_same(modii(gel(x,i), gel(y,i))) }
    2859             : 
    2860             : GEN
    2861       95004 : vecmoduu(GEN x, GEN y)
    2862       95004 : { pari_APPLY_ulong(uel(x,i) % uel(y,i)) }
    2863             : 
    2864             : static GEN
    2865      314285 : ideallog_i(GEN nf, GEN x, GEN sgn, zlog_S *S)
    2866             : {
    2867      314285 :   pari_sp av = avma;
    2868             :   GEN y, cyc;
    2869      314285 :   if (!S->hU) return cgetg(1, t_COL);
    2870      312773 :   cyc = bid_get_cyc(S->bid);
    2871      312773 :   if (typ(x) == t_MAT)
    2872             :   {
    2873       66808 :     if (lg(x) == 1) return zerocol(lg(cyc)-1);
    2874       66808 :     y = famat_zlog(nf, x, sgn, S);
    2875             :   }
    2876             :   else
    2877      245965 :     y = zlog(nf, x, sgn, S);
    2878      312766 :   y = ZMV_ZCV_mul(S->U, y);
    2879      312766 :   return gerepileupto(av, vecmodii(y, cyc));
    2880             : }
    2881             : 
    2882             : /* Given x (not necessarily integral), and bid as output by zidealstarinit,
    2883             :  * compute the vector of components on the generators bid[2].
    2884             :  * Assume (x,bid) = 1 and sgn is either NULL or nfsign_arch(x, bid) */
    2885             : GEN
    2886      301125 : ideallog_sgn(GEN nf, GEN x, GEN sgn, GEN bid)
    2887             : {
    2888             :   zlog_S S;
    2889      301125 :   nf = checknf(nf); checkbid(bid);
    2890      301118 :   init_zlog(&S, bid);
    2891      301118 :   if (sgn && typ(x) == t_VEC) /* vector of elements and signatures */
    2892             :   {
    2893       21413 :     long i, l = lg(x);
    2894       21413 :     GEN y = cgetg(l, t_MAT);
    2895       21413 :     for (i = 1; i < l; i++) gel(y,i) = ideallog_i(nf, gel(x,i), gel(sgn,i), &S);
    2896       21413 :     return y;
    2897             :   }
    2898      279705 :   return ideallog_i(nf, x, sgn, &S);
    2899             : }
    2900             : GEN
    2901      286383 : ideallog(GEN nf, GEN x, GEN bid)
    2902             : {
    2903      286383 :   if (!nf) return Zideallog(bid, x);
    2904      279712 :   return ideallog_sgn(nf, x, NULL, bid);
    2905             : }
    2906             : 
    2907             : /*************************************************************************/
    2908             : /**                                                                     **/
    2909             : /**               JOIN BID STRUCTURES, IDEAL LISTS                      **/
    2910             : /**                                                                     **/
    2911             : /*************************************************************************/
    2912             : /* bid1, bid2: for coprime modules m1 and m2 (without arch. part).
    2913             :  * Output: bid for m1 m2 */
    2914             : static GEN
    2915         476 : join_bid(GEN nf, GEN bid1, GEN bid2)
    2916             : {
    2917         476 :   pari_sp av = avma;
    2918             :   long nbgen, l1,l2;
    2919             :   GEN I1,I2, G1,G2, sprk1,sprk2, cyc1,cyc2, sarch;
    2920         476 :   GEN sprk, fa,fa2, U, cyc, y, u1 = NULL, x, gen;
    2921             : 
    2922         476 :   I1 = bid_get_ideal(bid1);
    2923         476 :   I2 = bid_get_ideal(bid2);
    2924         476 :   if (gequal1(gcoeff(I1,1,1))) return bid2; /* frequent trivial case */
    2925         259 :   G1 = bid_get_grp(bid1);
    2926         259 :   G2 = bid_get_grp(bid2);
    2927         259 :   x = idealmul(nf, I1,I2);
    2928         259 :   fa = famat_mul_shallow(bid_get_fact(bid1), bid_get_fact(bid2));
    2929         259 :   fa2= famat_mul_shallow(bid_get_fact2(bid1), bid_get_fact2(bid2));
    2930         259 :   sprk1 = bid_get_sprk(bid1);
    2931         259 :   sprk2 = bid_get_sprk(bid2);
    2932         259 :   sprk = shallowconcat(sprk1, sprk2);
    2933             : 
    2934         259 :   cyc1 = abgrp_get_cyc(G1); l1 = lg(cyc1);
    2935         259 :   cyc2 = abgrp_get_cyc(G2); l2 = lg(cyc2);
    2936         259 :   gen = (lg(G1)>3 && lg(G2)>3)? gen_1: NULL;
    2937         259 :   nbgen = l1+l2-2;
    2938         259 :   cyc = ZV_snf_group(shallowconcat(cyc1,cyc2), &U, gen? &u1: NULL);
    2939         259 :   if (nbgen)
    2940             :   {
    2941         259 :     GEN U1 = bid_get_U(bid1), U2 = bid_get_U(bid2);
    2942         259 :     U1 = l1==1? const_vec(lg(sprk1), cgetg(1,t_MAT))
    2943         259 :               : ZM_ZMV_mul(vecslice(U, 1, l1-1),   U1);
    2944         259 :     U2 = l2==1? const_vec(lg(sprk2), cgetg(1,t_MAT))
    2945         259 :               : ZM_ZMV_mul(vecslice(U, l1, nbgen), U2);
    2946         259 :     U = shallowconcat(U1, U2);
    2947             :   }
    2948             :   else
    2949           0 :     U = const_vec(lg(sprk), cgetg(1,t_MAT));
    2950             : 
    2951         259 :   if (gen)
    2952             :   {
    2953         259 :     GEN uv = zkchinese1init2(nf, I2, I1, x);
    2954         518 :     gen = shallowconcat(zkVchinese1(gel(uv,1), abgrp_get_gen(G1)),
    2955         259 :                         zkVchinese1(gel(uv,2), abgrp_get_gen(G2)));
    2956             :   }
    2957         259 :   sarch = bid_get_sarch(bid1); /* trivial */
    2958         259 :   y = bid_grp(nf, u1, cyc, gen, x, sarch);
    2959         259 :   x = mkvec2(x, bid_get_arch(bid1));
    2960         259 :   y = mkvec5(x, y, mkvec2(fa, fa2), mkvec2(sprk, sarch), U);
    2961         259 :   return gerepilecopy(av,y);
    2962             : }
    2963             : 
    2964             : typedef struct _ideal_data {
    2965             :   GEN nf, emb, L, pr, prL, sgnU, archp;
    2966             : } ideal_data;
    2967             : 
    2968             : /* z <- ( z | f(v[i])_{i=1..#v} ) */
    2969             : static void
    2970       86065 : concat_join(GEN *pz, GEN v, GEN (*f)(ideal_data*,GEN), ideal_data *data)
    2971             : {
    2972       86065 :   long i, nz, lv = lg(v);
    2973             :   GEN z, Z;
    2974       86065 :   if (lv == 1) return;
    2975       38143 :   z = *pz; nz = lg(z)-1;
    2976       38143 :   *pz = Z = cgetg(lv + nz, typ(z));
    2977       38143 :   for (i = 1; i <=nz; i++) gel(Z,i) = gel(z,i);
    2978       38143 :   Z += nz;
    2979       38143 :   for (i = 1; i < lv; i++) gel(Z,i) = f(data, gel(v,i));
    2980             : }
    2981             : static GEN
    2982         476 : join_idealinit(ideal_data *D, GEN x)
    2983         476 : { return join_bid(D->nf, x, D->prL); }
    2984             : static GEN
    2985       47698 : join_ideal(ideal_data *D, GEN x)
    2986       47698 : { return idealmulpowprime(D->nf, x, D->pr, D->L); }
    2987             : static GEN
    2988         455 : join_unit(ideal_data *D, GEN x)
    2989             : {
    2990         455 :   GEN bid = join_idealinit(D, gel(x,1)), u = gel(x,2), v = mkvec(D->emb);
    2991         455 :   if (lg(u) != 1) v = shallowconcat(u, v);
    2992         455 :   return mkvec2(bid, v);
    2993             : }
    2994             : 
    2995             : /*  flag & nf_GEN : generators, otherwise no
    2996             :  *  flag &2 : units, otherwise no
    2997             :  *  flag &4 : ideals in HNF, otherwise bid
    2998             :  *  flag &8 : omit ideals which cannot be conductors (pr^1 with Npr=2) */
    2999             : static GEN
    3000        3192 : Ideallist(GEN bnf, ulong bound, long flag)
    3001             : {
    3002        3192 :   const long cond = flag & 8;
    3003        3192 :   const long do_units = flag & 2, big_id = !(flag & 4);
    3004        3192 :   const long istar_flag = (flag & nf_GEN) | nf_INIT;
    3005        3192 :   pari_sp av, av0 = avma;
    3006             :   long i, j;
    3007        3192 :   GEN nf, z, p, fa, id, BOUND, U, empty = cgetg(1,t_VEC);
    3008             :   forprime_t S;
    3009             :   ideal_data ID;
    3010        3192 :   GEN (*join_z)(ideal_data*, GEN) =
    3011             :     do_units? &join_unit
    3012        3192 :               : (big_id? &join_idealinit: &join_ideal);
    3013             : 
    3014        3192 :   nf = checknf(bnf);
    3015        3192 :   if ((long)bound <= 0) return empty;
    3016        3192 :   id = matid(nf_get_degree(nf));
    3017        3192 :   if (big_id) id = Idealstar(nf,id, istar_flag);
    3018             : 
    3019             :   /* z[i] will contain all "objects" of norm i. Depending on flag, this means
    3020             :    * an ideal, a bid, or a couple [bid, log(units)]. Such objects are stored
    3021             :    * in vectors, computed one primary component at a time; join_z
    3022             :    * reconstructs the global object */
    3023        3192 :   BOUND = utoipos(bound);
    3024        3192 :   z = cgetg(bound+1,t_VEC);
    3025        3192 :   if (do_units) {
    3026          14 :     U = bnf_build_units(bnf);
    3027          14 :     gel(z,1) = mkvec( mkvec2(id, cgetg(1,t_VEC)) );
    3028             :   } else {
    3029        3178 :     U = NULL; /* -Wall */
    3030        3178 :     gel(z,1) = mkvec(id);
    3031             :   }
    3032        3192 :   for (i=2; i<=(long)bound; i++) gel(z,i) = empty;
    3033        3192 :   ID.nf = nf;
    3034             : 
    3035        3192 :   p = cgetipos(3);
    3036        3192 :   u_forprime_init(&S, 2, bound);
    3037        3192 :   av = avma;
    3038       19600 :   while ((p[2] = u_forprime_next(&S)))
    3039             :   {
    3040       13216 :     if (DEBUGLEVEL>1) { err_printf("%ld ",p[2]); err_flush(); }
    3041       13216 :     fa = idealprimedec_limit_norm(nf, p, BOUND);
    3042       26859 :     for (j=1; j<lg(fa); j++)
    3043             :     {
    3044       13643 :       GEN pr = gel(fa,j), z2 = leafcopy(z);
    3045       13643 :       ulong Q, q = upr_norm(pr);
    3046       13643 :       long l = (cond && q == 2)? 2: 1;
    3047             : 
    3048       13643 :       ID.pr = ID.prL = pr;
    3049       33775 :       for (Q = q; Q <= bound; l++, Q *= q) /* add pr^l */
    3050             :       {
    3051             :         ulong iQ;
    3052       20132 :         ID.L = utoipos(l);
    3053       20132 :         if (big_id) {
    3054         217 :           ID.prL = Idealstarprk(nf, pr, l, istar_flag);
    3055         217 :           if (do_units)
    3056             :           {
    3057         196 :             GEN sprk = bid_get_sprk(ID.prL);
    3058         392 :             ID.emb = lg(sprk) == 1? cgetg(1,t_VEC)
    3059         196 :                                   : vzlog_pr(nf, U, gel(sprk,1));
    3060             :           }
    3061             :         }
    3062      106197 :         for (iQ = Q,i = 1; iQ <= bound; iQ += Q,i++)
    3063       86065 :           concat_join(&gel(z,iQ), gel(z2,i), join_z, &ID);
    3064             :       }
    3065             :     }
    3066       13216 :     if (gc_needed(av,1))
    3067             :     {
    3068           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"Ideallist");
    3069           0 :       z = gerepilecopy(av, z);
    3070             :     }
    3071             :   }
    3072        3192 :   return gerepilecopy(av0, z);
    3073             : }
    3074             : GEN
    3075         350 : ideallist0(GEN bnf,long bound, long flag) {
    3076         350 :   if (flag<0 || flag>15) pari_err_FLAG("ideallist");
    3077         350 :   return Ideallist(bnf,bound,flag);
    3078             : }
    3079             : GEN
    3080        2842 : ideallist(GEN bnf,long bound) { return Ideallist(bnf,bound,4); }
    3081             : 
    3082             : /* bid = for module m (without arch. part), arch = archimedean part.
    3083             :  * Output: bid for [m,arch] */
    3084             : static GEN
    3085          56 : join_bid_arch(GEN nf, GEN bid, GEN archp)
    3086             : {
    3087          56 :   pari_sp av = avma;
    3088             :   GEN G, U;
    3089          56 :   GEN sprk, cyc, y, u1 = NULL, x, sarch, gen;
    3090             : 
    3091          56 :   checkbid(bid);
    3092          56 :   G = bid_get_grp(bid);
    3093          56 :   x = bid_get_ideal(bid);
    3094          56 :   sarch = nfarchstar(nf, bid_get_ideal(bid), archp);
    3095          56 :   sprk = bid_get_sprk(bid);
    3096             : 
    3097          56 :   gen = (lg(G)>3)? gel(G,3): NULL;
    3098          56 :   cyc = diagonal_shallow(shallowconcat(gel(G,2), sarch_get_cyc(sarch)));
    3099          56 :   cyc = ZM_snf_group(cyc, &U, gen? &u1: NULL);
    3100          56 :   y = bid_grp(nf, u1, cyc, gen, x, sarch);
    3101          56 :   U = split_U(U, sprk);
    3102          56 :   y = mkvec5(mkvec2(x, archp), y, gel(bid,3), mkvec2(sprk, sarch), U);
    3103          56 :   return gerepilecopy(av,y);
    3104             : }
    3105             : static GEN
    3106          56 : join_arch(ideal_data *D, GEN x) {
    3107          56 :   return join_bid_arch(D->nf, x, D->archp);
    3108             : }
    3109             : static GEN
    3110          28 : join_archunit(ideal_data *D, GEN x) {
    3111          28 :   GEN bid = join_arch(D, gel(x,1)), u = gel(x,2), v = mkvec(D->emb);
    3112          28 :   if (lg(u) != 1) v = shallowconcat(u, v);
    3113          28 :   return mkvec2(bid, v);
    3114             : }
    3115             : 
    3116             : /* L from ideallist, add archimedean part */
    3117             : GEN
    3118          14 : ideallistarch(GEN bnf, GEN L, GEN arch)
    3119             : {
    3120             :   pari_sp av;
    3121          14 :   long i, j, l = lg(L), lz;
    3122             :   GEN v, z, V;
    3123             :   ideal_data ID;
    3124             :   GEN (*join_z)(ideal_data*, GEN);
    3125             : 
    3126          14 :   if (typ(L) != t_VEC) pari_err_TYPE("ideallistarch",L);
    3127          14 :   if (l == 1) return cgetg(1,t_VEC);
    3128          14 :   z = gel(L,1);
    3129          14 :   if (typ(z) != t_VEC) pari_err_TYPE("ideallistarch",z);
    3130          14 :   z = gel(z,1); /* either a bid or [bid,U] */
    3131          14 :   ID.nf = checknf(bnf);
    3132          14 :   ID.archp = vec01_to_indices(arch);
    3133          14 :   if (lg(z) == 3) { /* the latter: do units */
    3134           7 :     if (typ(z) != t_VEC) pari_err_TYPE("ideallistarch",z);
    3135           7 :     ID.emb = zm_to_ZM( rowpermute(nfsign_units(bnf,NULL,1), ID.archp) );
    3136           7 :     join_z = &join_archunit;
    3137             :   } else
    3138           7 :     join_z = &join_arch;
    3139          14 :   av = avma; V = cgetg(l, t_VEC);
    3140          70 :   for (i = 1; i < l; i++)
    3141             :   {
    3142          56 :     z = gel(L,i); lz = lg(z);
    3143          56 :     gel(V,i) = v = cgetg(lz,t_VEC);
    3144          56 :     for (j=1; j<lz; j++) gel(v,j) = join_z(&ID, gel(z,j));
    3145             :   }
    3146          14 :   return gerepilecopy(av,V);
    3147             : }

Generated by: LCOV version 1.13