Code coverage tests

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

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

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

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

Generated by: LCOV version 1.13