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.1 lcov report (development 25406-bf255ab81b) Lines: 1840 1967 93.5 %
Date: 2020-06-04 05:59:24 Functions: 206 217 94.9 %
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    14499810 : get_tab(GEN nf, long *N)
      30             : {
      31    14499810 :   GEN tab = (typ(nf) == t_MAT)? nf: gel(nf,9);
      32    14499810 :   *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   559265320 : _mulii(GEN x, GEN y) {
      38   913810786 :   return is_pm1(x)? (signe(x) < 0)? negi(y): y
      39   913810493 :                   : mulii(x, y);
      40             : }
      41             : 
      42             : GEN
      43       17864 : tablemul_ei_ej(GEN M, long i, long j)
      44             : {
      45             :   long N;
      46       17864 :   GEN tab = get_tab(M, &N);
      47       17864 :   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       10731 : tablemul_ei(GEN M, GEN x, long i)
      55             : {
      56             :   long j, k, N;
      57             :   GEN v, tab;
      58             : 
      59       10731 :   if (i==1) return gcopy(x);
      60       10731 :   tab = get_tab(M, &N);
      61       10731 :   if (typ(x) != t_COL) { v = zerocol(N); gel(v,i) = gcopy(x); return v; }
      62       10731 :   tab += (i-1)*N; v = cgetg(N+1,t_COL);
      63             :   /* wi . x = [ sum_j tab[k,j] x[j] ]_k */
      64       74725 :   for (k=1; k<=N; k++)
      65             :   {
      66       63994 :     pari_sp av = avma;
      67       63994 :     GEN s = gen_0;
      68      458388 :     for (j=1; j<=N; j++)
      69             :     {
      70      394394 :       GEN c = gcoeff(tab,k,j);
      71      394394 :       if (!gequal0(c)) s = gadd(s, gmul(c, gel(x,j)));
      72             :     }
      73       63994 :     gel(v,k) = gerepileupto(av,s);
      74             :   }
      75       10731 :   return v;
      76             : }
      77             : /* as tablemul_ei, assume x a ZV of correct length */
      78             : GEN
      79    11026152 : zk_ei_mul(GEN nf, GEN x, long i)
      80             : {
      81             :   long j, k, N;
      82             :   GEN v, tab;
      83             : 
      84    11026152 :   if (i==1) return ZC_copy(x);
      85    11026138 :   tab = get_tab(nf, &N); tab += (i-1)*N;
      86    11026132 :   v = cgetg(N+1,t_COL);
      87    74250886 :   for (k=1; k<=N; k++)
      88             :   {
      89    63224907 :     pari_sp av = avma;
      90    63224907 :     GEN s = gen_0;
      91   760337370 :     for (j=1; j<=N; j++)
      92             :     {
      93   697120835 :       GEN c = gcoeff(tab,k,j);
      94   697120835 :       if (signe(c)) s = addii(s, _mulii(c, gel(x,j)));
      95             :     }
      96    63216535 :     gel(v,k) = gerepileuptoint(av, s);
      97             :   }
      98    11025979 :   return v;
      99             : }
     100             : 
     101             : /* table of multiplication by wi in R[w1,..., wN] */
     102             : GEN
     103        2639 : ei_multable(GEN TAB, long i)
     104             : {
     105             :   long k,N;
     106        2639 :   GEN m, tab = get_tab(TAB, &N);
     107        2639 :   tab += (i-1)*N;
     108        2639 :   m = cgetg(N+1,t_MAT);
     109       24815 :   for (k=1; k<=N; k++) gel(m,k) = gel(tab,k);
     110        2639 :   return m;
     111             : }
     112             : 
     113             : GEN
     114     5827550 : zk_multable(GEN nf, GEN x)
     115             : {
     116     5827550 :   long i, l = lg(x);
     117     5827550 :   GEN mul = cgetg(l,t_MAT);
     118     5827501 :   gel(mul,1) = x; /* assume w_1 = 1 */
     119    16737824 :   for (i=2; i<l; i++) gel(mul,i) = zk_ei_mul(nf,x,i);
     120     5826717 :   return mul;
     121             : }
     122             : GEN
     123        2009 : multable(GEN M, GEN x)
     124             : {
     125             :   long i, N;
     126             :   GEN mul;
     127        2009 :   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     4280014 : zk_scalar_or_multable(GEN nf, GEN x)
     140             : {
     141     4280014 :   long tx = typ(x);
     142     4280014 :   if (tx == t_MAT || tx == t_INT) return x;
     143     4259929 :   x = nf_to_scalar_or_basis(nf, x);
     144     4259927 :   return (typ(x) == t_COL)? zk_multable(nf, x): x;
     145             : }
     146             : 
     147             : GEN
     148       15330 : nftrace(GEN nf, GEN x)
     149             : {
     150       15330 :   pari_sp av = avma;
     151       15330 :   nf = checknf(nf);
     152       15330 :   x = nf_to_scalar_or_basis(nf, x);
     153       15309 :   x = (typ(x) == t_COL)? RgV_dotproduct(x, gel(nf_get_Tr(nf),1))
     154       15330 :                        : gmulgs(x, nf_get_degree(nf));
     155       15330 :   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         616 :   x = (typ(x) == t_POLMOD)? rnfeltdown(rnf, gtrace(x))
     164         693 :                           : 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       42351 : nfnorm(GEN nf, GEN x)
     181             : {
     182       42351 :   pari_sp av = avma;
     183             :   GEN c, den;
     184             :   long n;
     185       42351 :   nf = checknf(nf);
     186       42351 :   n = nf_get_degree(nf);
     187       42351 :   if (typ(x) == t_MAT) return famat_norm(nf, x);
     188       42344 :   x = nf_to_scalar_or_basis(nf, x);
     189       42344 :   if (typ(x)!=t_COL)
     190       11690 :     return gerepileupto(av, gpowgs(x, n));
     191       30654 :   x = nf_to_scalar_or_alg(nf, Q_primitive_part(x, &c));
     192       30654 :   x = Q_remove_denom(x, &den);
     193       30654 :   x = ZX_resultant_all(nf_get_pol(nf), x, den, 0);
     194       30654 :   return gerepileupto(av, c ? gmul(x, gpowgs(c, n)): x);
     195             : }
     196             : 
     197             : static GEN
     198          70 : to_RgX(GEN P, long vx)
     199             : {
     200          70 :   return varn(P) == vx ? P: scalarpol_shallow(P, vx);
     201             : }
     202             : 
     203             : GEN
     204         231 : rnfeltnorm(GEN rnf, GEN x)
     205             : {
     206         231 :   pari_sp av = avma;
     207             :   GEN nf, pol;
     208         231 :   long v = rnf_get_varn(rnf);
     209         231 :   checkrnf(rnf);
     210         231 :   x = liftpol_shallow(rnfeltabstorel(rnf, x));
     211         140 :   nf = rnf_get_nf(rnf); pol = rnf_get_pol(rnf);
     212         280 :   x = (typ(x) == t_POL)
     213          70 :     ? rnfeltdown(rnf, nfX_resultant(nf,pol,to_RgX(x,v)))
     214         140 :     : gpowgs(x, rnf_get_degree(rnf));
     215         140 :   return gerepileupto(av, x);
     216             : }
     217             : 
     218             : /* x + y in nf */
     219             : GEN
     220    16724645 : nfadd(GEN nf, GEN x, GEN y)
     221             : {
     222    16724645 :   pari_sp av = avma;
     223             :   GEN z;
     224             : 
     225    16724645 :   nf = checknf(nf);
     226    16724645 :   x = nf_to_scalar_or_basis(nf, x);
     227    16724645 :   y = nf_to_scalar_or_basis(nf, y);
     228    16724645 :   if (typ(x) != t_COL)
     229    13397041 :   { z = (typ(y) == t_COL)? RgC_Rg_add(y, x): gadd(x,y); }
     230             :   else
     231     3327604 :   { z = (typ(y) == t_COL)? RgC_add(x, y): RgC_Rg_add(x, y); }
     232    16724645 :   return gerepileupto(av, z);
     233             : }
     234             : /* x - y in nf */
     235             : GEN
     236     1248142 : nfsub(GEN nf, GEN x, GEN y)
     237             : {
     238     1248142 :   pari_sp av = avma;
     239             :   GEN z;
     240             : 
     241     1248142 :   nf = checknf(nf);
     242     1248142 :   x = nf_to_scalar_or_basis(nf, x);
     243     1248142 :   y = nf_to_scalar_or_basis(nf, y);
     244     1248142 :   if (typ(x) != t_COL)
     245      893907 :   { z = (typ(y) == t_COL)? Rg_RgC_sub(x,y): gsub(x,y); }
     246             :   else
     247      354235 :   { z = (typ(y) == t_COL)? RgC_sub(x,y): RgC_Rg_sub(x,y); }
     248     1248142 :   return gerepileupto(av, z);
     249             : }
     250             : 
     251             : /* product of ZC x,y in nf; ( sum_i x_i sum_j y_j m^{i,j}_k )_k */
     252             : static GEN
     253     2141466 : nfmuli_ZC(GEN nf, GEN x, GEN y)
     254             : {
     255             :   long i, j, k, N;
     256     2141466 :   GEN TAB = get_tab(nf, &N), v = cgetg(N+1,t_COL);
     257             : 
     258    11826007 :   for (k = 1; k <= N; k++)
     259             :   {
     260     9684541 :     pari_sp av = avma;
     261     9684541 :     GEN s, TABi = TAB;
     262     9684541 :     if (k == 1)
     263     2141466 :       s = mulii(gel(x,1),gel(y,1));
     264             :     else
     265     7543075 :       s = addii(mulii(gel(x,1),gel(y,k)),
     266     7543075 :                 mulii(gel(x,k),gel(y,1)));
     267    89810619 :     for (i=2; i<=N; i++)
     268             :     {
     269    80126078 :       GEN t, xi = gel(x,i);
     270    80126078 :       TABi += N;
     271    80126078 :       if (!signe(xi)) continue;
     272             : 
     273    30845320 :       t = NULL;
     274   391829158 :       for (j=2; j<=N; j++)
     275             :       {
     276   360983838 :         GEN p1, c = gcoeff(TABi, k, j); /* m^{i,j}_k */
     277   360983838 :         if (!signe(c)) continue;
     278   117279180 :         p1 = _mulii(c, gel(y,j));
     279   117279180 :         t = t? addii(t, p1): p1;
     280             :       }
     281    30845320 :       if (t) s = addii(s, mulii(xi, t));
     282             :     }
     283     9684541 :     gel(v,k) = gerepileuptoint(av,s);
     284             :   }
     285     2141466 :   return v;
     286             : }
     287             : static int
     288    43137393 : is_famat(GEN x) { return typ(x) == t_MAT && lg(x) == 3; }
     289             : /* product of x and y in nf */
     290             : GEN
     291    22025055 : nfmul(GEN nf, GEN x, GEN y)
     292             : {
     293             :   GEN z;
     294    22025055 :   pari_sp av = avma;
     295             : 
     296    22025055 :   if (x == y) return nfsqr(nf,x);
     297             : 
     298    18845739 :   nf = checknf(nf);
     299    18845739 :   if (is_famat(x) || is_famat(y)) return famat_mul(x, y);
     300    18845648 :   x = nf_to_scalar_or_basis(nf, x);
     301    18845648 :   y = nf_to_scalar_or_basis(nf, y);
     302    18845648 :   if (typ(x) != t_COL)
     303             :   {
     304    14293289 :     if (isintzero(x)) return gen_0;
     305    10032921 :     z = (typ(y) == t_COL)? RgC_Rg_mul(y, x): gmul(x,y); }
     306             :   else
     307             :   {
     308     4552359 :     if (typ(y) != t_COL)
     309             :     {
     310     3181458 :       if (isintzero(y)) return gen_0;
     311      695877 :       z = RgC_Rg_mul(x, y);
     312             :     }
     313             :     else
     314             :     {
     315             :       GEN dx, dy;
     316     1370901 :       x = Q_remove_denom(x, &dx);
     317     1370901 :       y = Q_remove_denom(y, &dy);
     318     1370901 :       z = nfmuli_ZC(nf,x,y);
     319     1370901 :       dx = mul_denom(dx,dy);
     320     1370901 :       if (dx) z = ZC_Z_div(z, dx);
     321             :     }
     322             :   }
     323    12099699 :   return gerepileupto(av, z);
     324             : }
     325             : /* square of ZC x in nf */
     326             : static GEN
     327     1300982 : nfsqri_ZC(GEN nf, GEN x)
     328             : {
     329             :   long i, j, k, N;
     330     1300982 :   GEN TAB = get_tab(nf, &N), v = cgetg(N+1,t_COL);
     331             : 
     332    10045763 :   for (k = 1; k <= N; k++)
     333             :   {
     334     8744781 :     pari_sp av = avma;
     335     8744781 :     GEN s, TABi = TAB;
     336     8744781 :     if (k == 1)
     337     1300982 :       s = sqri(gel(x,1));
     338             :     else
     339     7443799 :       s = shifti(mulii(gel(x,1),gel(x,k)), 1);
     340   106978139 :     for (i=2; i<=N; i++)
     341             :     {
     342    98233358 :       GEN p1, c, t, xi = gel(x,i);
     343    98233358 :       TABi += N;
     344    98233358 :       if (!signe(xi)) continue;
     345             : 
     346    31435689 :       c = gcoeff(TABi, k, i);
     347    31435689 :       t = signe(c)? _mulii(c,xi): NULL;
     348   388177925 :       for (j=i+1; j<=N; j++)
     349             :       {
     350   356742236 :         c = gcoeff(TABi, k, j);
     351   356742236 :         if (!signe(c)) continue;
     352   156371500 :         p1 = _mulii(c, shifti(gel(x,j),1));
     353   156371500 :         t = t? addii(t, p1): p1;
     354             :       }
     355    31435689 :       if (t) s = addii(s, mulii(xi, t));
     356             :     }
     357     8744781 :     gel(v,k) = gerepileuptoint(av,s);
     358             :   }
     359     1300982 :   return v;
     360             : }
     361             : /* square of x in nf */
     362             : GEN
     363     5054589 : nfsqr(GEN nf, GEN x)
     364             : {
     365     5054589 :   pari_sp av = avma;
     366             :   GEN z;
     367             : 
     368     5054589 :   nf = checknf(nf);
     369     5054589 :   if (is_famat(x)) return famat_sqr(x);
     370     5054589 :   x = nf_to_scalar_or_basis(nf, x);
     371     5054589 :   if (typ(x) != t_COL) z = gsqr(x);
     372             :   else
     373             :   {
     374             :     GEN dx;
     375       96734 :     x = Q_remove_denom(x, &dx);
     376       96734 :     z = nfsqri_ZC(nf,x);
     377       96734 :     if (dx) z = RgC_Rg_div(z, sqri(dx));
     378             :   }
     379     5054589 :   return gerepileupto(av, z);
     380             : }
     381             : 
     382             : /* x a ZC, v a t_COL of ZC/Z */
     383             : GEN
     384      134361 : zkC_multable_mul(GEN v, GEN x)
     385             : {
     386      134361 :   long i, l = lg(v);
     387      134361 :   GEN y = cgetg(l, t_COL);
     388      469885 :   for (i = 1; i < l; i++)
     389             :   {
     390      335524 :     GEN c = gel(v,i);
     391      335524 :     if (typ(c)!=t_COL) {
     392           0 :       if (!isintzero(c)) c = ZC_Z_mul(gel(x,1), c);
     393             :     } else {
     394      335524 :       c = ZM_ZC_mul(x,c);
     395      335524 :       if (ZV_isscalar(c)) c = gel(c,1);
     396             :     }
     397      335524 :     gel(y,i) = c;
     398             :   }
     399      134361 :   return y;
     400             : }
     401             : 
     402             : GEN
     403       46515 : nfC_multable_mul(GEN v, GEN x)
     404             : {
     405       46515 :   long i, l = lg(v);
     406       46515 :   GEN y = cgetg(l, t_COL);
     407      323120 :   for (i = 1; i < l; i++)
     408             :   {
     409      276605 :     GEN c = gel(v,i);
     410      276605 :     if (typ(c)!=t_COL) {
     411      229950 :       if (!isintzero(c)) c = RgC_Rg_mul(gel(x,1), c);
     412             :     } else {
     413       46655 :       c = RgM_RgC_mul(x,c);
     414       46655 :       if (QV_isscalar(c)) c = gel(c,1);
     415             :     }
     416      276605 :     gel(y,i) = c;
     417             :   }
     418       46515 :   return y;
     419             : }
     420             : 
     421             : GEN
     422      167895 : nfC_nf_mul(GEN nf, GEN v, GEN x)
     423             : {
     424             :   long tx;
     425             :   GEN y;
     426             : 
     427      167895 :   x = nf_to_scalar_or_basis(nf, x);
     428      167895 :   tx = typ(x);
     429      167895 :   if (tx != t_COL)
     430             :   {
     431             :     long l, i;
     432      128324 :     if (tx == t_INT)
     433             :     {
     434      119945 :       long s = signe(x);
     435      119945 :       if (!s) return zerocol(lg(v)-1);
     436      113169 :       if (is_pm1(x)) return s > 0? leafcopy(v): RgC_neg(v);
     437             :     }
     438       42000 :     l = lg(v); y = cgetg(l, t_COL);
     439      307650 :     for (i=1; i < l; i++)
     440             :     {
     441      265650 :       GEN c = gel(v,i);
     442      265650 :       if (typ(c) != t_COL) c = gmul(c, x); else c = RgC_Rg_mul(c, x);
     443      265650 :       gel(y,i) = c;
     444             :     }
     445       42000 :     return y;
     446             :   }
     447             :   else
     448             :   {
     449             :     GEN dx;
     450       39571 :     x = zk_multable(nf, Q_remove_denom(x,&dx));
     451       39571 :     y = nfC_multable_mul(v, x);
     452       39571 :     return dx? RgC_Rg_div(y, dx): y;
     453             :   }
     454             : }
     455             : static GEN
     456        9191 : mulbytab(GEN M, GEN c)
     457        9191 : { return typ(c) == t_COL? RgM_RgC_mul(M,c): RgC_Rg_mul(gel(M,1), c); }
     458             : GEN
     459        2009 : tablemulvec(GEN M, GEN x, GEN v)
     460             : {
     461             :   long l, i;
     462             :   GEN y;
     463             : 
     464        2009 :   if (typ(x) == t_COL && RgV_isscalar(x))
     465             :   {
     466           0 :     x = gel(x,1);
     467           0 :     return typ(v) == t_POL? RgX_Rg_mul(v,x): RgV_Rg_mul(v,x);
     468             :   }
     469        2009 :   x = multable(M, x); /* multiplication table by x */
     470        2009 :   y = cgetg_copy(v, &l);
     471        2009 :   if (typ(v) == t_POL)
     472             :   {
     473        2009 :     y[1] = v[1];
     474       11200 :     for (i=2; i < l; i++) gel(y,i) = mulbytab(x, gel(v,i));
     475        2009 :     y = normalizepol(y);
     476             :   }
     477             :   else
     478             :   {
     479           0 :     for (i=1; i < l; i++) gel(y,i) = mulbytab(x, gel(v,i));
     480             :   }
     481        2009 :   return y;
     482             : }
     483             : 
     484             : GEN
     485      287451 : zkmultable_capZ(GEN mx) { return Q_denom(zkmultable_inv(mx)); }
     486             : GEN
     487      346935 : zkmultable_inv(GEN mx) { return ZM_gauss(mx, col_ei(lg(mx)-1,1)); }
     488             : /* nf a true nf, x a ZC */
     489             : GEN
     490       59484 : zk_inv(GEN nf, GEN x) { return zkmultable_inv(zk_multable(nf,x)); }
     491             : 
     492             : /* inverse of x in nf */
     493             : GEN
     494       68005 : nfinv(GEN nf, GEN x)
     495             : {
     496       68005 :   pari_sp av = avma;
     497             :   GEN z;
     498             : 
     499       68005 :   nf = checknf(nf);
     500       68005 :   if (is_famat(x)) return famat_inv(x);
     501       68005 :   x = nf_to_scalar_or_basis(nf, x);
     502       68005 :   if (typ(x) == t_COL)
     503             :   {
     504             :     GEN d;
     505       26810 :     x = Q_remove_denom(x, &d);
     506       26810 :     z = zk_inv(nf, x);
     507       26810 :     if (d) z = RgC_Rg_mul(z, d);
     508             :   }
     509             :   else
     510       41195 :     z = ginv(x);
     511       68005 :   return gerepileupto(av, z);
     512             : }
     513             : 
     514             : /* quotient of x and y in nf */
     515             : GEN
     516       31778 : nfdiv(GEN nf, GEN x, GEN y)
     517             : {
     518       31778 :   pari_sp av = avma;
     519             :   GEN z;
     520             : 
     521       31778 :   nf = checknf(nf);
     522       31778 :   if (is_famat(x) || is_famat(y)) return famat_div(x,y);
     523       31687 :   y = nf_to_scalar_or_basis(nf, y);
     524       31687 :   if (typ(y) != t_COL)
     525             :   {
     526       19775 :     x = nf_to_scalar_or_basis(nf, x);
     527       19775 :     z = (typ(x) == t_COL)? RgC_Rg_div(x, y): gdiv(x,y);
     528             :   }
     529             :   else
     530             :   {
     531             :     GEN d;
     532       11912 :     y = Q_remove_denom(y, &d);
     533       11912 :     z = nfmul(nf, x, zk_inv(nf,y));
     534       11912 :     if (d) z = RgC_Rg_mul(z, d);
     535             :   }
     536       31687 :   return gerepileupto(av, z);
     537             : }
     538             : 
     539             : /* product of INTEGERS (t_INT or ZC) x and y in nf */
     540             : GEN
     541      917978 : nfmuli(GEN nf, GEN x, GEN y)
     542             : {
     543      917978 :   if (typ(x) == t_INT) return (typ(y) == t_COL)? ZC_Z_mul(y, x): mulii(x,y);
     544      800580 :   if (typ(y) == t_INT) return ZC_Z_mul(x, y);
     545      770565 :   return nfmuli_ZC(nf, x, y);
     546             : }
     547             : GEN
     548     1204248 : nfsqri(GEN nf, GEN x)
     549     1204248 : { return (typ(x) == t_INT)? sqri(x): nfsqri_ZC(nf, x); }
     550             : 
     551             : /* both x and y are RgV */
     552             : GEN
     553           0 : tablemul(GEN TAB, GEN x, GEN y)
     554             : {
     555             :   long i, j, k, N;
     556             :   GEN s, v;
     557           0 :   if (typ(x) != t_COL) return gmul(x, y);
     558           0 :   if (typ(y) != t_COL) return gmul(y, x);
     559           0 :   N = lg(x)-1;
     560           0 :   v = cgetg(N+1,t_COL);
     561           0 :   for (k=1; k<=N; k++)
     562             :   {
     563           0 :     pari_sp av = avma;
     564           0 :     GEN TABi = TAB;
     565           0 :     if (k == 1)
     566           0 :       s = gmul(gel(x,1),gel(y,1));
     567             :     else
     568           0 :       s = gadd(gmul(gel(x,1),gel(y,k)),
     569           0 :                gmul(gel(x,k),gel(y,1)));
     570           0 :     for (i=2; i<=N; i++)
     571             :     {
     572           0 :       GEN t, xi = gel(x,i);
     573           0 :       TABi += N;
     574           0 :       if (gequal0(xi)) continue;
     575             : 
     576           0 :       t = NULL;
     577           0 :       for (j=2; j<=N; j++)
     578             :       {
     579           0 :         GEN p1, c = gcoeff(TABi, k, j); /* m^{i,j}_k */
     580           0 :         if (gequal0(c)) continue;
     581           0 :         p1 = gmul(c, gel(y,j));
     582           0 :         t = t? gadd(t, p1): p1;
     583             :       }
     584           0 :       if (t) s = gadd(s, gmul(xi, t));
     585             :     }
     586           0 :     gel(v,k) = gerepileupto(av,s);
     587             :   }
     588           0 :   return v;
     589             : }
     590             : GEN
     591       42091 : tablesqr(GEN TAB, GEN x)
     592             : {
     593             :   long i, j, k, N;
     594             :   GEN s, v;
     595             : 
     596       42091 :   if (typ(x) != t_COL) return gsqr(x);
     597       42091 :   N = lg(x)-1;
     598       42091 :   v = cgetg(N+1,t_COL);
     599             : 
     600      298795 :   for (k=1; k<=N; k++)
     601             :   {
     602      256704 :     pari_sp av = avma;
     603      256704 :     GEN TABi = TAB;
     604      256704 :     if (k == 1)
     605       42091 :       s = gsqr(gel(x,1));
     606             :     else
     607      214613 :       s = gmul2n(gmul(gel(x,1),gel(x,k)), 1);
     608     1611036 :     for (i=2; i<=N; i++)
     609             :     {
     610     1354332 :       GEN p1, c, t, xi = gel(x,i);
     611     1354332 :       TABi += N;
     612     1354332 :       if (gequal0(xi)) continue;
     613             : 
     614      372918 :       c = gcoeff(TABi, k, i);
     615      372918 :       t = !gequal0(c)? gmul(c,xi): NULL;
     616     1497363 :       for (j=i+1; j<=N; j++)
     617             :       {
     618     1124445 :         c = gcoeff(TABi, k, j);
     619     1124445 :         if (gequal0(c)) continue;
     620      586551 :         p1 = gmul(gmul2n(c,1), gel(x,j));
     621      586551 :         t = t? gadd(t, p1): p1;
     622             :       }
     623      372918 :       if (t) s = gadd(s, gmul(xi, t));
     624             :     }
     625      256704 :     gel(v,k) = gerepileupto(av,s);
     626             :   }
     627       42091 :   return v;
     628             : }
     629             : 
     630             : static GEN
     631      168269 : _mul(void *data, GEN x, GEN y) { return nfmuli((GEN)data,x,y); }
     632             : static GEN
     633      405395 : _sqr(void *data, GEN x) { return nfsqri((GEN)data,x); }
     634             : 
     635             : /* Compute z^n in nf, left-shift binary powering */
     636             : GEN
     637      259709 : nfpow(GEN nf, GEN z, GEN n)
     638             : {
     639      259709 :   pari_sp av = avma;
     640             :   long s;
     641             :   GEN x, cx;
     642             : 
     643      259709 :   if (typ(n)!=t_INT) pari_err_TYPE("nfpow",n);
     644      259709 :   nf = checknf(nf);
     645      259709 :   s = signe(n); if (!s) return gen_1;
     646      259709 :   if (is_famat(z)) return famat_pow(z, n);
     647      259695 :   x = nf_to_scalar_or_basis(nf, z);
     648      259695 :   if (typ(x) != t_COL) return powgi(x,n);
     649      255282 :   if (s < 0)
     650             :   { /* simplified nfinv */
     651             :     GEN d;
     652       14590 :     x = Q_remove_denom(x, &d);
     653       14590 :     x = zk_inv(nf, x);
     654       14590 :     x = primitive_part(x, &cx);
     655       14590 :     cx = mul_content(cx, d);
     656       14590 :     n = negi(n);
     657             :   }
     658             :   else
     659      240692 :     x = primitive_part(x, &cx);
     660      255282 :   x = gen_pow_i(x, n, (void*)nf, _sqr, _mul);
     661      255282 :   if (cx)
     662       54259 :     x = gerepileupto(av, gmul(x, powgi(cx, n)));
     663             :   else
     664      201023 :     x = gerepilecopy(av, x);
     665      255282 :   return x;
     666             : }
     667             : /* Compute z^n in nf, left-shift binary powering */
     668             : GEN
     669       59738 : nfpow_u(GEN nf, GEN z, ulong n)
     670             : {
     671       59738 :   pari_sp av = avma;
     672             :   GEN x, cx;
     673             : 
     674       59738 :   nf = checknf(nf);
     675       59738 :   if (!n) return gen_1;
     676       59738 :   x = nf_to_scalar_or_basis(nf, z);
     677       59738 :   if (typ(x) != t_COL) return gpowgs(x,n);
     678       31269 :   x = primitive_part(x, &cx);
     679       31269 :   x = gen_powu_i(x, n, (void*)nf, _sqr, _mul);
     680       31269 :   if (cx)
     681             :   {
     682        1113 :     x = gmul(x, powgi(cx, utoipos(n)));
     683        1113 :     return gerepileupto(av,x);
     684             :   }
     685       30156 :   return gerepilecopy(av, x);
     686             : }
     687             : 
     688             : static GEN
     689     2570687 : _nf_red(void *E, GEN x) { (void)E; return x; }
     690             : 
     691             : static GEN
     692    10367140 : _nf_add(void *E, GEN x, GEN y) { return nfadd((GEN)E,x,y); }
     693             : 
     694             : static GEN
     695      623763 : _nf_neg(void *E, GEN x) { (void)E; return gneg(x); }
     696             : 
     697             : static GEN
     698    12473391 : _nf_mul(void *E, GEN x, GEN y) { return nfmul((GEN)E,x,y); }
     699             : 
     700             : static GEN
     701       43218 : _nf_inv(void *E, GEN x) { return nfinv((GEN)E,x); }
     702             : 
     703             : static GEN
     704        8498 : _nf_s(void *E, long x) { (void)E; return stoi(x); }
     705             : 
     706             : static const struct bb_field nf_field={_nf_red,_nf_add,_nf_mul,_nf_neg,
     707             :                                         _nf_inv,&gequal0,_nf_s };
     708             : 
     709      186984 : const struct bb_field *get_nf_field(void **E, GEN nf)
     710      186984 : { *E = (void*)nf; return &nf_field; }
     711             : 
     712             : GEN
     713          14 : nfM_det(GEN nf, GEN M)
     714             : {
     715             :   void *E;
     716          14 :   const struct bb_field *S = get_nf_field(&E, nf);
     717          14 :   return gen_det(M, E, S);
     718             : }
     719             : GEN
     720        8484 : nfM_inv(GEN nf, GEN M)
     721             : {
     722             :   void *E;
     723        8484 :   const struct bb_field *S = get_nf_field(&E, nf);
     724        8484 :   return gen_Gauss(M, matid(lg(M)-1), E, S);
     725             : }
     726             : GEN
     727        8232 : nfM_mul(GEN nf, GEN A, GEN B)
     728             : {
     729             :   void *E;
     730        8232 :   const struct bb_field *S = get_nf_field(&E, nf);
     731        8232 :   return gen_matmul(A, B, E, S);
     732             : }
     733             : GEN
     734      170254 : nfM_nfC_mul(GEN nf, GEN A, GEN B)
     735             : {
     736             :   void *E;
     737      170254 :   const struct bb_field *S = get_nf_field(&E, nf);
     738      170254 :   return gen_matcolmul(A, B, E, S);
     739             : }
     740             : 
     741             : /* valuation of integral x (ZV), with resp. to prime ideal pr */
     742             : long
     743     9586398 : ZC_nfvalrem(GEN x, GEN pr, GEN *newx)
     744             : {
     745             :   long i, v, l;
     746     9586398 :   GEN r, y, p = pr_get_p(pr), mul = pr_get_tau(pr);
     747             : 
     748             :   /* p inert */
     749     9586402 :   if (typ(mul) == t_INT) return newx? ZV_pvalrem(x, p, newx):ZV_pval(x, p);
     750     9514847 :   y = cgetg_copy(x, &l); /* will hold the new x */
     751     9514852 :   x = leafcopy(x);
     752     9514856 :   for(v=0;; v++)
     753             :   {
     754    47778444 :     for (i=1; i<l; i++)
     755             :     { /* is (x.b)[i] divisible by p ? */
     756    43479068 :       gel(y,i) = dvmdii(ZMrow_ZC_mul(mul,x,i),p,&r);
     757    43479030 :       if (r != gen_0) { if (newx) *newx = x; return v; }
     758             :     }
     759     4299376 :     swap(x, y);
     760             :   }
     761             : }
     762             : long
     763     8898022 : ZC_nfval(GEN x, GEN P)
     764     8898022 : { return ZC_nfvalrem(x, P, NULL); }
     765             : 
     766             : /* v_P(x) != 0, x a ZV. Simpler version of ZC_nfvalrem */
     767             : int
     768      412269 : ZC_prdvd(GEN x, GEN P)
     769             : {
     770      412269 :   pari_sp av = avma;
     771             :   long i, l;
     772      412269 :   GEN p = pr_get_p(P), mul = pr_get_tau(P);
     773      412269 :   if (typ(mul) == t_INT) return ZV_Z_dvd(x, p);
     774      411926 :   l = lg(x);
     775     1644798 :   for (i=1; i<l; i++)
     776     1456505 :     if (!dvdii(ZMrow_ZC_mul(mul,x,i), p)) return gc_bool(av,0);
     777      188293 :   return gc_bool(av,1);
     778             : }
     779             : 
     780             : int
     781          28 : pr_equal(GEN P, GEN Q)
     782             : {
     783          28 :   GEN gQ, p = pr_get_p(P);
     784          28 :   long e = pr_get_e(P), f = pr_get_f(P), n;
     785          28 :   if (!equalii(p, pr_get_p(Q)) || e != pr_get_e(Q) || f != pr_get_f(Q))
     786          14 :     return 0;
     787          14 :   gQ = pr_get_gen(Q); n = lg(gQ)-1;
     788          14 :   if (2*e*f > n) return 1; /* room for only one such pr */
     789           7 :   return ZV_equal(pr_get_gen(P), gQ) || ZC_prdvd(gQ, P);
     790             : }
     791             : 
     792             : GEN
     793        1015 : famat_nfvalrem(GEN nf, GEN x, GEN pr, GEN *py)
     794             : {
     795        1015 :   pari_sp av = avma;
     796        1015 :   GEN P = gel(x,1), E = gel(x,2), V = gen_0, y = NULL;
     797        1015 :   long l = lg(P), simplify = 0, i;
     798        1015 :   if (py) { *py = gen_1; y = cgetg(l, t_COL); }
     799             : 
     800       21595 :   for (i = 1; i < l; i++)
     801             :   {
     802       20580 :     GEN e = gel(E,i);
     803             :     long v;
     804       20580 :     if (!signe(e))
     805             :     {
     806           7 :       if (py) gel(y,i) = gen_1;
     807           7 :       simplify = 1; continue;
     808             :     }
     809       20573 :     v = nfvalrem(nf, gel(P,i), pr, py? &gel(y,i): NULL);
     810       20573 :     if (v == LONG_MAX) { set_avma(av); if (py) *py = gen_0; return mkoo(); }
     811       20573 :     V = addmulii(V, stoi(v), e);
     812             :   }
     813        1015 :   if (!py) V = gerepileuptoint(av, V);
     814             :   else
     815             :   {
     816          35 :     y = mkmat2(y, gel(x,2));
     817          35 :     if (simplify) y = famat_remove_trivial(y);
     818          35 :     gerepileall(av, 2, &V, &y); *py = y;
     819             :   }
     820        1015 :   return V;
     821             : }
     822             : long
     823     1326913 : nfval(GEN nf, GEN x, GEN pr)
     824             : {
     825     1326913 :   pari_sp av = avma;
     826             :   long w, e;
     827             :   GEN cx, p;
     828             : 
     829     1326913 :   if (gequal0(x)) return LONG_MAX;
     830     1325332 :   nf = checknf(nf);
     831     1325331 :   checkprid(pr);
     832     1325333 :   p = pr_get_p(pr);
     833     1325329 :   e = pr_get_e(pr);
     834     1325331 :   x = nf_to_scalar_or_basis(nf, x);
     835     1325309 :   if (typ(x) != t_COL) return e*Q_pval(x,p);
     836      124289 :   x = Q_primitive_part(x, &cx);
     837      124284 :   w = ZC_nfval(x,pr);
     838      124267 :   if (cx) w += e*Q_pval(cx,p);
     839      124288 :   return gc_long(av,w);
     840             : }
     841             : 
     842             : /* want to write p^v = uniformizer^(e*v) * z^v, z coprime to pr */
     843             : /* z := tau^e / p^(e-1), algebraic integer coprime to pr; return z^v */
     844             : static GEN
     845       22064 : powp(GEN nf, GEN pr, long v)
     846             : {
     847             :   GEN b, z;
     848             :   long e;
     849       22064 :   if (!v) return gen_1;
     850       21938 :   b = pr_get_tau(pr);
     851       21938 :   if (typ(b) == t_INT) return gen_1;
     852        3234 :   e = pr_get_e(pr);
     853        3234 :   z = gel(b,1);
     854        3234 :   if (e != 1) z = gdiv(nfpow_u(nf, z, e), powiu(pr_get_p(pr),e-1));
     855        3234 :   if (v < 0) { v = -v; z = nfinv(nf, z); }
     856        3234 :   if (v != 1) z = nfpow_u(nf, z, v);
     857        3234 :   return z;
     858             : }
     859             : long
     860       84686 : nfvalrem(GEN nf, GEN x, GEN pr, GEN *py)
     861             : {
     862       84686 :   pari_sp av = avma;
     863             :   long w, e;
     864             :   GEN cx, p, t;
     865             : 
     866       84686 :   if (!py) return nfval(nf,x,pr);
     867       64057 :   if (gequal0(x)) { *py = gen_0; return LONG_MAX; }
     868       64001 :   nf = checknf(nf);
     869       64001 :   checkprid(pr);
     870       64001 :   p = pr_get_p(pr);
     871       64001 :   e = pr_get_e(pr);
     872       64001 :   x = nf_to_scalar_or_basis(nf, x);
     873       64001 :   if (typ(x) != t_COL) {
     874       52808 :     w = Q_pvalrem(x,p, py);
     875       52808 :     if (!w) { *py = gerepilecopy(av, x); return 0; }
     876       18963 :     *py = gerepileupto(av, gmul(powp(nf, pr, w), *py));
     877       18963 :     return e*w;
     878             :   }
     879       11193 :   x = Q_primitive_part(x, &cx);
     880       11193 :   w = ZC_nfvalrem(x,pr, py);
     881       11193 :   if (cx)
     882             :   {
     883        3101 :     long v = Q_pvalrem(cx,p, &t);
     884        3101 :     *py = nfmul(nf, *py, gmul(powp(nf,pr,v), t));
     885        3101 :     *py = gerepileupto(av, *py);
     886        3101 :     w += e*v;
     887             :   }
     888             :   else
     889        8092 :     *py = gerepilecopy(av, *py);
     890       11193 :   return w;
     891             : }
     892             : GEN
     893         154 : gpnfvalrem(GEN nf, GEN x, GEN pr, GEN *py)
     894             : {
     895             :   long v;
     896         154 :   if (is_famat(x)) return famat_nfvalrem(nf, x, pr, py);
     897         147 :   v = nfvalrem(nf,x,pr,py);
     898         147 :   return v == LONG_MAX? mkoo(): stoi(v);
     899             : }
     900             : 
     901             : /* true nf */
     902             : GEN
     903       98014 : coltoalg(GEN nf, GEN x)
     904             : {
     905       98014 :   return mkpolmod( nf_to_scalar_or_alg(nf, x), nf_get_pol(nf) );
     906             : }
     907             : 
     908             : GEN
     909      144340 : basistoalg(GEN nf, GEN x)
     910             : {
     911             :   GEN T;
     912             : 
     913      144340 :   nf = checknf(nf);
     914      144340 :   switch(typ(x))
     915             :   {
     916       91875 :     case t_COL: {
     917       91875 :       pari_sp av = avma;
     918       91875 :       return gerepilecopy(av, coltoalg(nf, x));
     919             :     }
     920       32039 :     case t_POLMOD:
     921       32039 :       T = nf_get_pol(nf);
     922       32039 :       if (!RgX_equal_var(T,gel(x,1)))
     923           0 :         pari_err_MODULUS("basistoalg", T,gel(x,1));
     924       32039 :       return gcopy(x);
     925        1848 :     case t_POL:
     926        1848 :       T = nf_get_pol(nf);
     927        1848 :       if (varn(T) != varn(x)) pari_err_VAR("basistoalg",x,T);
     928        1848 :       retmkpolmod(RgX_rem(x, T), ZX_copy(T));
     929       18578 :     case t_INT:
     930             :     case t_FRAC:
     931       18578 :       T = nf_get_pol(nf);
     932       18578 :       retmkpolmod(gcopy(x), ZX_copy(T));
     933           0 :     default:
     934           0 :       pari_err_TYPE("basistoalg",x);
     935             :       return NULL; /* LCOV_EXCL_LINE */
     936             :   }
     937             : }
     938             : 
     939             : /* true nf, x a t_POL */
     940             : static GEN
     941     1613270 : pol_to_scalar_or_basis(GEN nf, GEN x)
     942             : {
     943     1613270 :   GEN T = nf_get_pol(nf);
     944     1613264 :   long l = lg(x);
     945     1613264 :   if (varn(x) != varn(T)) pari_err_VAR("nf_to_scalar_or_basis", x,T);
     946     1613201 :   if (l >= lg(T)) { x = RgX_rem(x, T); l = lg(x); }
     947     1613201 :   if (l == 2) return gen_0;
     948      977748 :   if (l == 3)
     949             :   {
     950      215676 :     x = gel(x,2);
     951      215676 :     if (!is_rational_t(typ(x))) pari_err_TYPE("nf_to_scalar_or_basis",x);
     952      215669 :     return x;
     953             :   }
     954      762072 :   return poltobasis(nf,x);
     955             : }
     956             : /* Assume nf is a genuine nf. */
     957             : GEN
     958    91055866 : nf_to_scalar_or_basis(GEN nf, GEN x)
     959             : {
     960    91055866 :   switch(typ(x))
     961             :   {
     962    66402394 :     case t_INT: case t_FRAC:
     963    66402394 :       return x;
     964      182753 :     case t_POLMOD:
     965      182753 :       x = checknfelt_mod(nf,x,"nf_to_scalar_or_basis");
     966      182691 :       switch(typ(x))
     967             :       {
     968       36561 :         case t_INT: case t_FRAC: return x;
     969      146130 :         case t_POL: return pol_to_scalar_or_basis(nf,x);
     970             :       }
     971           0 :       break;
     972     1467147 :     case t_POL: return pol_to_scalar_or_basis(nf,x);
     973    23003595 :     case t_COL:
     974    23003595 :       if (lg(x)-1 != nf_get_degree(nf)) break;
     975    23003530 :       return QV_isscalar(x)? gel(x,1): x;
     976             :   }
     977          54 :   pari_err_TYPE("nf_to_scalar_or_basis",x);
     978             :   return NULL; /* LCOV_EXCL_LINE */
     979             : }
     980             : /* Let x be a polynomial with coefficients in Q or nf. Return the same
     981             :  * polynomial with coefficients expressed as vectors (on the integral basis).
     982             :  * No consistency checks, not memory-clean. */
     983             : GEN
     984        7903 : RgX_to_nfX(GEN nf, GEN x)
     985             : {
     986             :   long i, l;
     987        7903 :   GEN y = cgetg_copy(x, &l); y[1] = x[1];
     988       60151 :   for (i=2; i<l; i++) gel(y,i) = nf_to_scalar_or_basis(nf, gel(x,i));
     989        7903 :   return y;
     990             : }
     991             : 
     992             : /* Assume nf is a genuine nf. */
     993             : GEN
     994     1151922 : nf_to_scalar_or_alg(GEN nf, GEN x)
     995             : {
     996     1151922 :   switch(typ(x))
     997             :   {
     998       26678 :     case t_INT: case t_FRAC:
     999       26678 :       return x;
    1000           0 :     case t_POLMOD:
    1001           0 :       x = checknfelt_mod(nf,x,"nf_to_scalar_or_alg");
    1002           0 :       if (typ(x) != t_POL) return x;
    1003             :       /* fall through */
    1004             :     case t_POL:
    1005             :     {
    1006        2044 :       GEN T = nf_get_pol(nf);
    1007        2044 :       long l = lg(x);
    1008        2044 :       if (varn(x) != varn(T)) pari_err_VAR("nf_to_scalar_or_alg", x,T);
    1009        2044 :       if (l >= lg(T)) { x = RgX_rem(x, T); l = lg(x); }
    1010        2044 :       if (l == 2) return gen_0;
    1011        2044 :       if (l == 3) return gel(x,2);
    1012        2030 :       return x;
    1013             :     }
    1014     1123154 :     case t_COL:
    1015             :     {
    1016             :       GEN dx;
    1017     1123154 :       if (lg(x)-1 != nf_get_degree(nf)) break;
    1018     2210200 :       if (QV_isscalar(x)) return gel(x,1);
    1019     1087054 :       x = Q_remove_denom(x, &dx);
    1020     1087048 :       x = RgV_RgC_mul(nf_get_zkprimpart(nf), x);
    1021     1087047 :       dx = mul_denom(dx, nf_get_zkden(nf));
    1022     1087045 :       return gdiv(x,dx);
    1023             :     }
    1024             :   }
    1025          46 :   pari_err_TYPE("nf_to_scalar_or_alg",x);
    1026             :   return NULL; /* LCOV_EXCL_LINE */
    1027             : }
    1028             : 
    1029             : /* gmul(A, RgX_to_RgC(x)), A t_MAT of compatible dimensions */
    1030             : GEN
    1031        1337 : RgM_RgX_mul(GEN A, GEN x)
    1032             : {
    1033        1337 :   long i, l = lg(x)-1;
    1034             :   GEN z;
    1035        1337 :   if (l == 1) return zerocol(nbrows(A));
    1036        1337 :   z = gmul(gel(x,2), gel(A,1));
    1037        2541 :   for (i = 2; i < l; i++)
    1038        1204 :     if (!gequal0(gel(x,i+1))) z = gadd(z, gmul(gel(x,i+1), gel(A,i)));
    1039        1337 :   return z;
    1040             : }
    1041             : GEN
    1042     2995871 : ZM_ZX_mul(GEN A, GEN x)
    1043             : {
    1044     2995871 :   long i, l = lg(x)-1;
    1045             :   GEN z;
    1046     2995871 :   if (l == 1) return zerocol(nbrows(A));
    1047     2994985 :   z = ZC_Z_mul(gel(A,1), gel(x,2));
    1048    12046139 :   for (i = 2; i < l ; i++)
    1049     9051812 :     if (signe(gel(x,i+1))) z = ZC_add(z, ZC_Z_mul(gel(A,i), gel(x,i+1)));
    1050     2994327 :   return z;
    1051             : }
    1052             : /* x a t_POL, nf a genuine nf. No garbage collecting. No check.  */
    1053             : GEN
    1054     2766555 : poltobasis(GEN nf, GEN x)
    1055             : {
    1056     2766555 :   GEN d, T = nf_get_pol(nf);
    1057     2766528 :   if (varn(x) != varn(T)) pari_err_VAR( "poltobasis", x,T);
    1058     2766395 :   if (degpol(x) >= degpol(T)) x = RgX_rem(x,T);
    1059     2766372 :   x = Q_remove_denom(x, &d);
    1060     2766363 :   if (!RgX_is_ZX(x)) pari_err_TYPE("poltobasis",x);
    1061     2766329 :   x = ZM_ZX_mul(nf_get_invzk(nf), x);
    1062     2765898 :   if (d) x = RgC_Rg_div(x, d);
    1063     2765908 :   return x;
    1064             : }
    1065             : 
    1066             : GEN
    1067      300993 : algtobasis(GEN nf, GEN x)
    1068             : {
    1069             :   pari_sp av;
    1070             : 
    1071      300993 :   nf = checknf(nf);
    1072      300993 :   switch(typ(x))
    1073             :   {
    1074      107107 :     case t_POLMOD:
    1075      107107 :       if (!RgX_equal_var(nf_get_pol(nf),gel(x,1)))
    1076           7 :         pari_err_MODULUS("algtobasis", nf_get_pol(nf),gel(x,1));
    1077      107100 :       x = gel(x,2);
    1078      107100 :       switch(typ(x))
    1079             :       {
    1080        7497 :         case t_INT:
    1081        7497 :         case t_FRAC: return scalarcol(x, nf_get_degree(nf));
    1082       99603 :         case t_POL:
    1083       99603 :           av = avma;
    1084       99603 :           return gerepileupto(av,poltobasis(nf,x));
    1085             :       }
    1086           0 :       break;
    1087             : 
    1088       99876 :     case t_POL:
    1089       99876 :       av = avma;
    1090       99876 :       return gerepileupto(av,poltobasis(nf,x));
    1091             : 
    1092       22085 :     case t_COL:
    1093       22085 :       if (!RgV_is_QV(x)) pari_err_TYPE("nfalgtobasis",x);
    1094       22078 :       if (lg(x)-1 != nf_get_degree(nf)) pari_err_DIM("nfalgtobasis");
    1095       22078 :       return gcopy(x);
    1096             : 
    1097       71925 :     case t_INT:
    1098       71925 :     case t_FRAC: return scalarcol(x, nf_get_degree(nf));
    1099             :   }
    1100           0 :   pari_err_TYPE("algtobasis",x);
    1101             :   return NULL; /* LCOV_EXCL_LINE */
    1102             : }
    1103             : 
    1104             : GEN
    1105       44212 : rnfbasistoalg(GEN rnf,GEN x)
    1106             : {
    1107       44212 :   const char *f = "rnfbasistoalg";
    1108             :   long lx, i;
    1109       44212 :   pari_sp av = avma;
    1110             :   GEN z, nf, R, T;
    1111             : 
    1112       44212 :   checkrnf(rnf);
    1113       44212 :   nf = rnf_get_nf(rnf);
    1114       44212 :   T = nf_get_pol(nf);
    1115       44212 :   R = QXQX_to_mod_shallow(rnf_get_pol(rnf), T);
    1116       44212 :   switch(typ(x))
    1117             :   {
    1118         826 :     case t_COL:
    1119         826 :       z = cgetg_copy(x, &lx);
    1120        2478 :       for (i=1; i<lx; i++)
    1121             :       {
    1122        1701 :         GEN c = nf_to_scalar_or_alg(nf, gel(x,i));
    1123        1652 :         if (typ(c) == t_POL) c = mkpolmod(c,T);
    1124        1652 :         gel(z,i) = c;
    1125             :       }
    1126         777 :       z = RgV_RgC_mul(gel(rnf_get_zk(rnf),1), z);
    1127         714 :       return gerepileupto(av, gmodulo(z,R));
    1128             : 
    1129       29757 :     case t_POLMOD:
    1130       29757 :       x = polmod_nffix(f, rnf, x, 0);
    1131       29554 :       if (typ(x) != t_POL) break;
    1132       13545 :       retmkpolmod(RgX_copy(x), RgX_copy(R));
    1133        1099 :     case t_POL:
    1134        1099 :       if (varn(x) == varn(T)) { RgX_check_QX(x,f); x = gmodulo(x,T); break; }
    1135         875 :       if (varn(x) == varn(R))
    1136             :       {
    1137         826 :         x = RgX_nffix(f,nf_get_pol(nf),x,0);
    1138         826 :         return gmodulo(x, R);
    1139             :       }
    1140          49 :       pari_err_VAR(f, x,R);
    1141             :   }
    1142       28714 :   retmkpolmod(scalarpol(x, varn(R)), RgX_copy(R));
    1143             : }
    1144             : 
    1145             : GEN
    1146        1897 : matbasistoalg(GEN nf,GEN x)
    1147             : {
    1148             :   long i, j, li, lx;
    1149        1897 :   GEN z = cgetg_copy(x, &lx);
    1150             : 
    1151        1897 :   if (lx == 1) return z;
    1152        1890 :   switch(typ(x))
    1153             :   {
    1154          56 :     case t_VEC: case t_COL:
    1155         217 :       for (i=1; i<lx; i++) gel(z,i) = basistoalg(nf, gel(x,i));
    1156          56 :       return z;
    1157        1834 :     case t_MAT: break;
    1158           0 :     default: pari_err_TYPE("matbasistoalg",x);
    1159             :   }
    1160        1834 :   li = lgcols(x);
    1161        6895 :   for (j=1; j<lx; j++)
    1162             :   {
    1163        5061 :     GEN c = cgetg(li,t_COL), xj = gel(x,j);
    1164        5061 :     gel(z,j) = c;
    1165       24451 :     for (i=1; i<li; i++) gel(c,i) = basistoalg(nf, gel(xj,i));
    1166             :   }
    1167        1834 :   return z;
    1168             : }
    1169             : 
    1170             : GEN
    1171        4662 : matalgtobasis(GEN nf,GEN x)
    1172             : {
    1173             :   long i, j, li, lx;
    1174        4662 :   GEN z = cgetg_copy(x, &lx);
    1175             : 
    1176        4662 :   if (lx == 1) return z;
    1177        4298 :   switch(typ(x))
    1178             :   {
    1179        4291 :     case t_VEC: case t_COL:
    1180       13454 :       for (i=1; i<lx; i++) gel(z,i) = algtobasis(nf, gel(x,i));
    1181        4291 :       return z;
    1182           7 :     case t_MAT: break;
    1183           0 :     default: pari_err_TYPE("matalgtobasis",x);
    1184             :   }
    1185           7 :   li = lgcols(x);
    1186          14 :   for (j=1; j<lx; j++)
    1187             :   {
    1188           7 :     GEN c = cgetg(li,t_COL), xj = gel(x,j);
    1189           7 :     gel(z,j) = c;
    1190          21 :     for (i=1; i<li; i++) gel(c,i) = algtobasis(nf, gel(xj,i));
    1191             :   }
    1192           7 :   return z;
    1193             : }
    1194             : GEN
    1195        8708 : RgM_to_nfM(GEN nf,GEN x)
    1196             : {
    1197             :   long i, j, li, lx;
    1198        8708 :   GEN z = cgetg_copy(x, &lx);
    1199             : 
    1200        8708 :   if (lx == 1) return z;
    1201        8708 :   li = lgcols(x);
    1202       66962 :   for (j=1; j<lx; j++)
    1203             :   {
    1204       58254 :     GEN c = cgetg(li,t_COL), xj = gel(x,j);
    1205       58254 :     gel(z,j) = c;
    1206      391552 :     for (i=1; i<li; i++) gel(c,i) = nf_to_scalar_or_basis(nf, gel(xj,i));
    1207             :   }
    1208        8708 :   return z;
    1209             : }
    1210             : GEN
    1211       78372 : RgC_to_nfC(GEN nf, GEN x)
    1212      576436 : { pari_APPLY_type(t_COL, nf_to_scalar_or_basis(nf, gel(x,i))) }
    1213             : 
    1214             : /* x a t_POLMOD, supposedly in rnf = K[z]/(T), K = Q[y]/(Tnf) */
    1215             : GEN
    1216      134267 : polmod_nffix(const char *f, GEN rnf, GEN x, int lift)
    1217      134267 : { return polmod_nffix2(f, rnf_get_nfpol(rnf), rnf_get_pol(rnf), x,lift); }
    1218             : GEN
    1219      134358 : polmod_nffix2(const char *f, GEN T, GEN R, GEN x, int lift)
    1220             : {
    1221      134358 :   if (RgX_equal_var(gel(x,1), R))
    1222             :   {
    1223      124432 :     x = gel(x,2);
    1224      124432 :     if (typ(x) == t_POL && varn(x) == varn(R))
    1225             :     {
    1226       94808 :       x = RgX_nffix(f, T, x, lift);
    1227       94808 :       switch(lg(x))
    1228             :       {
    1229        5782 :         case 2: return gen_0;
    1230       11347 :         case 3: return gel(x,2);
    1231             :       }
    1232       77679 :       return x;
    1233             :     }
    1234             :   }
    1235       39550 :   return Rg_nffix(f, T, x, lift);
    1236             : }
    1237             : GEN
    1238        1204 : rnfalgtobasis(GEN rnf,GEN x)
    1239             : {
    1240        1204 :   const char *f = "rnfalgtobasis";
    1241        1204 :   pari_sp av = avma;
    1242             :   GEN T, R;
    1243             : 
    1244        1204 :   checkrnf(rnf);
    1245        1204 :   R = rnf_get_pol(rnf);
    1246        1204 :   T = rnf_get_nfpol(rnf);
    1247        1204 :   switch(typ(x))
    1248             :   {
    1249          49 :     case t_COL:
    1250          49 :       if (lg(x)-1 != rnf_get_degree(rnf)) pari_err_DIM(f);
    1251          28 :       x = RgV_nffix(f, T, x, 0);
    1252          21 :       return gerepilecopy(av, x);
    1253             : 
    1254        1071 :     case t_POLMOD:
    1255        1071 :       x = polmod_nffix(f, rnf, x, 0);
    1256        1036 :       if (typ(x) != t_POL) break;
    1257         714 :       return gerepileupto(av, RgM_RgX_mul(rnf_get_invzk(rnf), x));
    1258          56 :     case t_POL:
    1259          56 :       if (varn(x) == varn(T))
    1260             :       {
    1261          21 :         RgX_check_QX(x,f);
    1262          14 :         if (degpol(x) >= degpol(T)) x = RgX_rem(x,T);
    1263          14 :         x = mkpolmod(x,T); break;
    1264             :       }
    1265          35 :       x = RgX_nffix(f, T, x, 0);
    1266          28 :       if (degpol(x) >= degpol(R)) x = RgX_rem(x, R);
    1267          28 :       return gerepileupto(av, RgM_RgX_mul(rnf_get_invzk(rnf), x));
    1268             :   }
    1269         364 :   return gerepileupto(av, scalarcol(x, rnf_get_degree(rnf)));
    1270             : }
    1271             : 
    1272             : /* Given a and b in nf, gives an algebraic integer y in nf such that a-b.y
    1273             :  * is "small" */
    1274             : GEN
    1275         259 : nfdiveuc(GEN nf, GEN a, GEN b)
    1276             : {
    1277         259 :   pari_sp av = avma;
    1278         259 :   a = nfdiv(nf,a,b);
    1279         259 :   return gerepileupto(av, ground(a));
    1280             : }
    1281             : 
    1282             : /* Given a and b in nf, gives a "small" algebraic integer r in nf
    1283             :  * of the form a-b.y */
    1284             : GEN
    1285         259 : nfmod(GEN nf, GEN a, GEN b)
    1286             : {
    1287         259 :   pari_sp av = avma;
    1288         259 :   GEN p1 = gneg_i(nfmul(nf,b,ground(nfdiv(nf,a,b))));
    1289         259 :   return gerepileupto(av, nfadd(nf,a,p1));
    1290             : }
    1291             : 
    1292             : /* Given a and b in nf, gives a two-component vector [y,r] in nf such
    1293             :  * that r=a-b.y is "small". */
    1294             : GEN
    1295         259 : nfdivrem(GEN nf, GEN a, GEN b)
    1296             : {
    1297         259 :   pari_sp av = avma;
    1298         259 :   GEN p1,z, y = ground(nfdiv(nf,a,b));
    1299             : 
    1300         259 :   p1 = gneg_i(nfmul(nf,b,y));
    1301         259 :   z = cgetg(3,t_VEC);
    1302         259 :   gel(z,1) = gcopy(y);
    1303         259 :   gel(z,2) = nfadd(nf,a,p1); return gerepileupto(av, z);
    1304             : }
    1305             : 
    1306             : /*************************************************************************/
    1307             : /**                                                                     **/
    1308             : /**                   LOGARITHMIC EMBEDDINGS                            **/
    1309             : /**                                                                     **/
    1310             : /*************************************************************************/
    1311             : 
    1312             : static int
    1313      534306 : low_prec(GEN x)
    1314             : {
    1315      534306 :   switch(typ(x))
    1316             :   {
    1317           0 :     case t_INT: return !signe(x);
    1318      534306 :     case t_REAL: return !signe(x) || realprec(x) <= DEFAULTPREC;
    1319           0 :     default: return 0;
    1320             :   }
    1321             : }
    1322             : 
    1323             : static GEN
    1324       11346 : cxlog_1(GEN nf) { return zerocol(lg(nf_get_roots(nf))-1); }
    1325             : static GEN
    1326           0 : cxlog_m1(GEN nf, long prec)
    1327             : {
    1328           0 :   long i, l = lg(nf_get_roots(nf)), r1 = nf_get_r1(nf);
    1329           0 :   GEN v = cgetg(l, t_COL), p,  P;
    1330           0 :   p = mppi(prec); P = mkcomplex(gen_0, p);
    1331           0 :   for (i = 1; i <= r1; i++) gel(v,i) = P; /* IPi*/
    1332           0 :   if (i < l) P = gmul2n(P,1);
    1333           0 :   for (     ; i < l; i++) gel(v,i) = P; /* 2IPi */
    1334           0 :   return v;
    1335             : }
    1336             : static GEN
    1337        9849 : famat_cxlog(GEN nf, GEN fa, long prec)
    1338             : {
    1339        9849 :   GEN g, e, y = NULL;
    1340             :   long i, l;
    1341             : 
    1342        9849 :   if (typ(fa) != t_MAT) pari_err_TYPE("famat_cxlog",fa);
    1343        9849 :   if (lg(fa) == 1) return cxlog_1(nf);
    1344        9849 :   g = gel(fa,1);
    1345        9849 :   e = gel(fa,2); l = lg(e);
    1346       19172 :   for (i = 1; i < l; i++)
    1347             :   {
    1348        9323 :     GEN t, x = nf_to_scalar_or_basis(nf, gel(g,i));
    1349             :     /* multiplicative arch would be better (save logs), but exponents overflow
    1350             :      * [ could keep track of expo separately, but not worth it ] */
    1351        9323 :     t = nf_cxlog(nf,x,prec); if (!t) return NULL;
    1352        9323 :     if (gel(t,1) == gen_0) continue; /* positive rational */
    1353        5735 :     t = RgC_Rg_mul(t, gel(e,i));
    1354        5735 :     y = y? RgV_add(y,t): t;
    1355             :   }
    1356        9849 :   return y ? y: cxlog_1(nf);
    1357             : }
    1358             : /* Archimedean components: [e_i Log( sigma_i(X) )], where X = primpart(x),
    1359             :  * and e_i = 1 (resp 2.) for i <= R1 (resp. > R1) */
    1360             : GEN
    1361      229761 : nf_cxlog(GEN nf, GEN x, long prec)
    1362             : {
    1363             :   long i, l, r1;
    1364             :   GEN v;
    1365      229761 :   if (typ(x) == t_MAT) return famat_cxlog(nf,x,prec);
    1366      219912 :   x = nf_to_scalar_or_basis(nf,x);
    1367      219912 :   if (typ(x) != t_COL) return gsigne(x) > 0? cxlog_1(nf): cxlog_m1(nf, prec);
    1368      215680 :   x = RgM_RgC_mul(nf_get_M(nf), Q_primpart(x));
    1369      215680 :   l = lg(x); r1 = nf_get_r1(nf);
    1370      641740 :   for (i = 1; i <= r1; i++)
    1371      426060 :     if (low_prec(gel(x,i))) return NULL;
    1372      310808 :   for (     ; i <  l;  i++)
    1373       95128 :     if (low_prec(gnorm(gel(x,i)))) return NULL;
    1374      215680 :   v = cgetg(l,t_COL);
    1375      641740 :   for (i = 1; i <= r1; i++) gel(v,i) = glog(gel(x,i),prec);
    1376      310808 :   for (     ; i <  l;  i++) gel(v,i) = gmul2n(glog(gel(x,i),prec),1);
    1377      215680 :   return v;
    1378             : }
    1379             : GEN
    1380          91 : nfV_cxlog(GEN nf, GEN x, long prec)
    1381             : {
    1382             :   long i, l;
    1383          91 :   GEN v = cgetg_copy(x, &l);
    1384         161 :   for (i = 1; i < l; i++)
    1385          70 :     if (!(gel(v,i) = nf_cxlog(nf, gel(x,i), prec))) return NULL;
    1386          91 :   return v;
    1387             : }
    1388             : 
    1389             : static GEN
    1390        2114 : scalar_logembed(GEN nf, GEN u, GEN *emb)
    1391             : {
    1392             :   GEN v, logu;
    1393        2114 :   long i, s = signe(u), RU = lg(nf_get_roots(nf))-1, R1 = nf_get_r1(nf);
    1394             : 
    1395        2114 :   if (!s) pari_err_DOMAIN("nflogembed","argument","=",gen_0,u);
    1396        2114 :   v = cgetg(RU+1, t_COL); logu = logr_abs(u);
    1397        3885 :   for (i = 1; i <= R1; i++) gel(v,i) = logu;
    1398        2114 :   if (i <= RU)
    1399             :   {
    1400        1239 :     GEN logu2 = shiftr(logu,1);
    1401        3745 :     for (   ; i <= RU; i++) gel(v,i) = logu2;
    1402             :   }
    1403        2114 :   if (emb) *emb = const_col(RU, u);
    1404        2114 :   return v;
    1405             : }
    1406             : 
    1407             : static GEN
    1408        1302 : famat_logembed(GEN nf,GEN x,GEN *emb,long prec)
    1409             : {
    1410        1302 :   GEN A, M, T, a, t, g = gel(x,1), e = gel(x,2);
    1411        1302 :   long i, l = lg(e);
    1412             : 
    1413        1302 :   if (l == 1) return scalar_logembed(nf, real_1(prec), emb);
    1414        1302 :   A = NULL; T = emb? cgetg(l, t_COL): NULL;
    1415        1302 :   if (emb) *emb = M = mkmat2(T, e);
    1416        6524 :   for (i = 1; i < l; i++)
    1417             :   {
    1418        5222 :     a = nflogembed(nf, gel(g,i), &t, prec);
    1419        5222 :     if (!a) return NULL;
    1420        5222 :     a = RgC_Rg_mul(a, gel(e,i));
    1421        5222 :     A = A? RgC_add(A, a): a;
    1422        5222 :     if (emb) gel(T,i) = t;
    1423             :   }
    1424        1302 :   return A;
    1425             : }
    1426             : 
    1427             : /* Get archimedean components: [e_i log( | sigma_i(x) | )], with e_i = 1
    1428             :  * (resp 2.) for i <= R1 (resp. > R1) and set emb to the embeddings of x.
    1429             :  * Return NULL if precision problem */
    1430             : GEN
    1431        8141 : nflogembed(GEN nf, GEN x, GEN *emb, long prec)
    1432             : {
    1433             :   long i, l, r1;
    1434             :   GEN v, t;
    1435             : 
    1436        8141 :   if (typ(x) == t_MAT) return famat_logembed(nf,x,emb,prec);
    1437        6839 :   x = nf_to_scalar_or_basis(nf,x);
    1438        6839 :   if (typ(x) != t_COL) return scalar_logembed(nf, gtofp(x,prec), emb);
    1439        4725 :   x = RgM_RgC_mul(nf_get_M(nf), x);
    1440        4725 :   l = lg(x); r1 = nf_get_r1(nf); v = cgetg(l,t_COL);
    1441        9443 :   for (i = 1; i <= r1; i++)
    1442             :   {
    1443        4725 :     t = gabs(gel(x,i),prec); if (low_prec(t)) return NULL;
    1444        4718 :     gel(v,i) = glog(t,prec);
    1445             :   }
    1446       13104 :   for (   ; i < l; i++)
    1447             :   {
    1448        8393 :     t = gnorm(gel(x,i)); if (low_prec(t)) return NULL;
    1449        8386 :     gel(v,i) = glog(t,prec);
    1450             :   }
    1451        4711 :   if (emb) *emb = x;
    1452        4711 :   return v;
    1453             : }
    1454             : 
    1455             : /*************************************************************************/
    1456             : /**                                                                     **/
    1457             : /**                        REAL EMBEDDINGS                              **/
    1458             : /**                                                                     **/
    1459             : /*************************************************************************/
    1460             : static GEN
    1461       83587 : sarch_get_cyc(GEN sarch) { return gel(sarch,1); }
    1462             : static GEN
    1463      338777 : sarch_get_archp(GEN sarch) { return gel(sarch,2); }
    1464             : static GEN
    1465       66877 : sarch_get_MI(GEN sarch) { return gel(sarch,3); }
    1466             : static GEN
    1467       66877 : sarch_get_lambda(GEN sarch) { return gel(sarch,4); }
    1468             : static GEN
    1469       66877 : sarch_get_F(GEN sarch) { return gel(sarch,5); }
    1470             : 
    1471             : /* x not a scalar, true nf, return number of positive roots of char_x */
    1472             : static long
    1473        1903 : num_positive(GEN nf, GEN x)
    1474             : {
    1475        1903 :   GEN T = nf_get_pol(nf), B, charx;
    1476             :   long dnf, vnf, N;
    1477        1903 :   x = nf_to_scalar_or_alg(nf, x); /* not a scalar */
    1478        1903 :   charx = ZXQ_charpoly(x, T, 0);
    1479        1903 :   charx = ZX_radical(charx);
    1480        1903 :   N = degpol(T) / degpol(charx);
    1481             :   /* real places are unramified ? */
    1482        1903 :   if (N == 1 || ZX_sturm(charx) * N == nf_get_r1(nf))
    1483        1896 :     return ZX_sturmpart(charx, mkvec2(gen_0,mkoo())) * N;
    1484             :   /* painful case, multiply by random square until primitive */
    1485           7 :   dnf = nf_get_degree(nf);
    1486           7 :   vnf = varn(T);
    1487           7 :   B = int2n(10);
    1488             :   for(;;)
    1489           0 :   {
    1490           7 :     GEN y = RgXQ_sqr(random_FpX(dnf, vnf, B), T);
    1491           7 :     y = RgXQ_mul(x, y, T);
    1492           7 :     charx = ZXQ_charpoly(y, T, 0);
    1493           7 :     if (ZX_is_squarefree(charx))
    1494           7 :       return ZX_sturmpart(charx, mkvec2(gen_0,mkoo())) * N;
    1495             :   }
    1496             : }
    1497             : 
    1498             : /* x a QC: return sigma_k(x) where 1 <= k <= r1+r2; correct but inefficient
    1499             :  * if x in Q. M = nf_get_M(nf) */
    1500             : static GEN
    1501          91 : nfembed_i(GEN M, GEN x, long k)
    1502             : {
    1503          91 :   long i, l = lg(M);
    1504          91 :   GEN z = gel(x,1);
    1505         364 :   for (i = 2; i < l; i++) z = gadd(z, gmul(gcoeff(M,k,i), gel(x,i)));
    1506          91 :   return z;
    1507             : }
    1508             : GEN
    1509           0 : nfembed(GEN nf, GEN x, long k)
    1510             : {
    1511           0 :   pari_sp av = avma;
    1512           0 :   nf = checknf(nf);
    1513           0 :   x = nf_to_scalar_or_basis(nf,x);
    1514           0 :   if (typ(x) != t_COL) return gerepilecopy(av, x);
    1515           0 :   return gerepileupto(av, nfembed_i(nf_get_M(nf),x,k));
    1516             : }
    1517             : 
    1518             : /* x a ZC */
    1519             : static GEN
    1520      440819 : zk_embed(GEN M, GEN x, long k)
    1521             : {
    1522      440819 :   long i, l = lg(x);
    1523      440819 :   GEN z = gel(x,1); /* times M[k,1], which is 1 */
    1524     1621945 :   for (i = 2; i < l; i++) z = mpadd(z, mpmul(gcoeff(M,k,i), gel(x,i)));
    1525      440819 :   return z;
    1526             : }
    1527             : 
    1528             : /* Given floating point approximation z of sigma_k(x), decide its sign
    1529             :  * [0/+, 1/- and -1 for FAIL] */
    1530             : static long
    1531      429006 : eval_sign_embed(GEN z)
    1532             : { /* dubious, fail */
    1533      429006 :   if (typ(z) == t_REAL && realprec(z) <= LOWDEFAULTPREC) return -1;
    1534      427742 :   return (signe(z) < 1)? 1: 0;
    1535             : }
    1536             : /* return v such that (-1)^v = sign(sigma_k(x)), x primitive ZC */
    1537             : static long
    1538      351233 : eval_sign(GEN M, GEN x, long k)
    1539      351233 : { return eval_sign_embed( zk_embed(M, x, k) ); }
    1540             : 
    1541             : /* check that signs[i..#signs] == s; signs = NULL encodes "totally positive" */
    1542             : static int
    1543           0 : oksigns(long l, GEN signs, long i, long s)
    1544             : {
    1545           0 :   if (!signs) return s == 0;
    1546           0 :   for (; i < l; i++)
    1547           0 :     if (signs[i] != s) return 0;
    1548           0 :   return 1;
    1549             : }
    1550             : /* check that signs[i] = s and signs[i+1..#signs] = 1-s */
    1551             : static int
    1552           0 : oksigns2(long l, GEN signs, long i, long s)
    1553             : {
    1554           0 :   if (!signs) return s == 0 && i == l-1;
    1555           0 :   return signs[i] == s && oksigns(l, signs, i+1, 1-s);
    1556             : }
    1557             : 
    1558             : /* true nf, x a ZC (primitive for efficiency), embx its embeddings or NULL */
    1559             : static int
    1560       67711 : nfchecksigns_i(GEN nf, GEN x, GEN embx, GEN signs, GEN archp)
    1561             : {
    1562       67711 :   long l = lg(archp), i;
    1563       67711 :   GEN M = nf_get_M(nf), sarch = NULL;
    1564       67711 :   long np = -1;
    1565      102259 :   for (i = 1; i < l; i++)
    1566             :   {
    1567             :     long s;
    1568       78319 :     if (embx)
    1569       77773 :       s = eval_sign_embed(gel(embx,i));
    1570             :     else
    1571         546 :       s = eval_sign(M, x, archp[i]);
    1572             :     /* 0 / + or 1 / -; -1 for FAIL */
    1573       78319 :     if (s < 0) /* failure */
    1574             :     {
    1575           0 :       long ni, r1 = nf_get_r1(nf);
    1576             :       GEN xi;
    1577           0 :       if (np < 0)
    1578             :       {
    1579           0 :         np = num_positive(nf, x);
    1580           0 :         if (np == 0)  return oksigns(l, signs, i, 1);
    1581           0 :         if (np == r1) return oksigns(l, signs, i, 0);
    1582           0 :         sarch = nfarchstar(nf, NULL, identity_perm(r1));
    1583             :       }
    1584           0 :       xi = set_sign_mod_divisor(nf, vecsmall_ei(r1, archp[i]), gen_1, sarch);
    1585           0 :       xi = Q_primpart(xi);
    1586           0 :       ni = num_positive(nf, nfmuli(nf,x,xi));
    1587           0 :       if (ni == 0)  return oksigns2(l, signs, i, 0);
    1588           0 :       if (ni == r1) return oksigns2(l, signs, i, 1);
    1589           0 :       s = ni < np? 0: 1;
    1590             :     }
    1591       78319 :     if (s != (signs? signs[i]: 0)) return 0;
    1592             :   }
    1593       23940 :   return 1;
    1594             : }
    1595             : static void
    1596         406 : pl_convert(GEN pl, GEN *psigns, GEN *parchp)
    1597             : {
    1598         406 :   long i, j, l = lg(pl);
    1599         406 :   GEN signs = cgetg(l, t_VECSMALL);
    1600         406 :   GEN archp = cgetg(l, t_VECSMALL);
    1601        1372 :   for (i = j = 1; i < l; i++)
    1602             :   {
    1603         966 :     if (!pl[i]) continue;
    1604         714 :     archp[j] = i;
    1605         714 :     signs[j] = (pl[i] < 0)? 1: 0;
    1606         714 :     j++;
    1607             :   }
    1608         406 :   setlg(archp, j); *parchp = archp;
    1609         406 :   setlg(signs, j); *psigns = signs;
    1610         406 : }
    1611             : /* pl : requested signs for real embeddings, 0 = no sign constraint */
    1612             : int
    1613         931 : nfchecksigns(GEN nf, GEN x, GEN pl)
    1614             : {
    1615         931 :   pari_sp av = avma;
    1616             :   GEN signs, archp;
    1617         931 :   nf = checknf(nf);
    1618         931 :   x = nf_to_scalar_or_basis(nf,x);
    1619         931 :   if (typ(x) != t_COL)
    1620             :   {
    1621         525 :     long i, l = lg(pl), s = gsigne(x);
    1622        1064 :     for (i = 1; i < l; i++)
    1623         539 :       if (pl[i] && pl[i] != s) return gc_bool(av,0);
    1624         525 :     return gc_bool(av,1);
    1625             :   }
    1626         406 :   pl_convert(pl, &signs, &archp);
    1627         406 :   return gc_bool(av, nfchecksigns_i(nf, x, NULL, signs, archp));
    1628             : }
    1629             : 
    1630             : /* signs = NULL: totally positive, else sign[i] = 0 (+) or 1 (-) */
    1631             : static GEN
    1632       66877 : get_C(GEN lambda, long l, GEN signs)
    1633             : {
    1634             :   long i;
    1635             :   GEN C, mlambda;
    1636       66877 :   if (!signs) return const_vec(l-1, lambda);
    1637       28181 :   C = cgetg(l, t_COL); mlambda = gneg(lambda);
    1638       66341 :   for (i = 1; i < l; i++) gel(C,i) = signs[i]? mlambda: lambda;
    1639       28181 :   return C;
    1640             : }
    1641             : /* signs = NULL: totally positive at archp */
    1642             : static GEN
    1643      120161 : nfsetsigns(GEN nf, GEN signs, GEN x, GEN sarch)
    1644             : {
    1645      120161 :   long i, l = lg(sarch_get_archp(sarch));
    1646             :   GEN ex;
    1647             :   /* Is signature already correct ? */
    1648      120161 :   if (typ(x) != t_COL)
    1649             :   {
    1650       52856 :     long s = gsigne(x);
    1651       52856 :     if (!s) i = 1;
    1652       52842 :     else if (!signs)
    1653        4935 :       i = (s < 0)? 1: l;
    1654             :     else
    1655             :     {
    1656       47907 :       s = s < 0? 1: 0;
    1657       77655 :       for (i = 1; i < l; i++)
    1658       52833 :         if (signs[i] != s) break;
    1659             :     }
    1660       52856 :     ex = (i < l)? const_col(l-1, x): NULL;
    1661             :   }
    1662             :   else
    1663             :   {
    1664       67305 :     pari_sp av = avma;
    1665       67305 :     GEN cex, M = nf_get_M(nf), archp = sarch_get_archp(sarch);
    1666       67305 :     GEN xp = Q_primitive_part(x,&cex);
    1667       67305 :     ex = cgetg(l,t_COL);
    1668      156891 :     for (i = 1; i < l; i++) gel(ex,i) = zk_embed(M,xp,archp[i]);
    1669       67305 :     if (nfchecksigns_i(nf, xp, ex, signs, archp)) { ex = NULL; set_avma(av); }
    1670       43652 :     else if (cex) ex = RgC_Rg_mul(ex, cex); /* put back content */
    1671             :   }
    1672      120161 :   if (ex)
    1673             :   { /* If no, fix it */
    1674       66877 :     GEN MI = sarch_get_MI(sarch), F = sarch_get_F(sarch);
    1675       66877 :     GEN lambda = sarch_get_lambda(sarch);
    1676       66877 :     GEN t = RgC_sub(get_C(lambda, l, signs), ex);
    1677             :     long e;
    1678       66877 :     t = grndtoi(RgM_RgC_mul(MI,t), &e);
    1679       66877 :     if (lg(F) != 1) t = ZM_ZC_mul(F, t);
    1680       66877 :     x = typ(x) == t_COL? RgC_add(t, x): RgC_Rg_add(t, x);
    1681             :   }
    1682      120161 :   return x;
    1683             : }
    1684             : /* - sarch = nfarchstar(nf, F);
    1685             :  * - x encodes a vector of signs at arch.archp: either a t_VECSMALL
    1686             :  *   (vector of signs as {0,1}-vector), NULL (totally positive at archp),
    1687             :  *   or a non-zero number field element (replaced by its signature at archp);
    1688             :  * - y is a non-zero number field element
    1689             :  * Return z = y (mod F) with signs(y, archp) = signs(x) (a {0,1}-vector) */
    1690             : GEN
    1691      151311 : set_sign_mod_divisor(GEN nf, GEN x, GEN y, GEN sarch)
    1692             : {
    1693      151311 :   GEN archp = sarch_get_archp(sarch);
    1694      151311 :   if (lg(archp) == 1) return y;
    1695      118789 :   nf = checknf(nf);
    1696      118789 :   if (x && typ(x) != t_VECSMALL) x = nfsign_arch(nf, x, archp);
    1697      118789 :   y = nf_to_scalar_or_basis(nf,y);
    1698      118789 :   return nfsetsigns(nf, x, y, sarch);
    1699             : }
    1700             : 
    1701             : static GEN
    1702       23473 : setsigns_init(GEN nf, GEN archp, GEN F, GEN DATA)
    1703             : {
    1704       23473 :   GEN lambda, Mr = rowpermute(nf_get_M(nf), archp), MI = F? RgM_mul(Mr,F): Mr;
    1705       23473 :   lambda = gmul2n(matrixnorm(MI,DEFAULTPREC), -1);
    1706       23473 :   if (typ(lambda) != t_REAL) lambda = gmul(lambda, sstoQ(1001,1000));
    1707       23473 :   if (lg(archp) < lg(MI))
    1708             :   {
    1709       21175 :     GEN perm = gel(indexrank(MI), 2);
    1710       21175 :     if (!F) F = matid(nf_get_degree(nf));
    1711       21175 :     MI = vecpermute(MI, perm);
    1712       21175 :     F = vecpermute(F, perm);
    1713             :   }
    1714       23473 :   if (!F) F = cgetg(1,t_MAT);
    1715       23473 :   MI = RgM_inv(MI);
    1716       23473 :   return mkvec5(DATA, archp, MI, lambda, F);
    1717             : }
    1718             : /* F non-0 integral ideal in HNF (or NULL: Z_K), compute elements in 1+F
    1719             :  * whose sign matrix at archp is identity; archp in 'indices' format */
    1720             : GEN
    1721       46783 : nfarchstar(GEN nf, GEN F, GEN archp)
    1722             : {
    1723       46783 :   long nba = lg(archp) - 1;
    1724       46783 :   if (!nba) return mkvec2(cgetg(1,t_VEC), archp);
    1725       22108 :   if (F && equali1(gcoeff(F,1,1))) F = NULL;
    1726       22108 :   if (F) F = idealpseudored(F, nf_get_roundG(nf));
    1727       22108 :   return setsigns_init(nf, archp, F, const_vec(nba, gen_2));
    1728             : }
    1729             : 
    1730             : /*************************************************************************/
    1731             : /**                                                                     **/
    1732             : /**                         IDEALCHINESE                                **/
    1733             : /**                                                                     **/
    1734             : /*************************************************************************/
    1735             : static int
    1736        2989 : isprfact(GEN x)
    1737             : {
    1738             :   long i, l;
    1739             :   GEN L, E;
    1740        2989 :   if (typ(x) != t_MAT || lg(x) != 3) return 0;
    1741        2989 :   L = gel(x,1); l = lg(L);
    1742        2989 :   E = gel(x,2);
    1743        7154 :   for(i=1; i<l; i++)
    1744             :   {
    1745        4165 :     checkprid(gel(L,i));
    1746        4165 :     if (typ(gel(E,i)) != t_INT) return 0;
    1747             :   }
    1748        2989 :   return 1;
    1749             : }
    1750             : 
    1751             : /* initialize projectors mod pr[i]^e[i] for idealchinese */
    1752             : static GEN
    1753        2989 : pr_init(GEN nf, GEN fa, GEN w, GEN dw)
    1754             : {
    1755        2989 :   GEN U, E, F, L = gel(fa,1), E0 = gel(fa,2);
    1756        2989 :   long i, r = lg(L);
    1757             : 
    1758        2989 :   if (w && lg(w) != r) pari_err_TYPE("idealchinese", w);
    1759        2989 :   if (r == 1 && !dw) return cgetg(1,t_VEC);
    1760        2982 :   E = leafcopy(E0); /* do not destroy fa[2] */
    1761        7147 :   for (i = 1; i < r; i++)
    1762        4165 :     if (signe(gel(E,i)) < 0) gel(E,i) = gen_0;
    1763        2982 :   F = factorbackprime(nf, L, E);
    1764        2982 :   if (dw)
    1765             :   {
    1766         700 :     F = ZM_Z_mul(F, dw);
    1767        1603 :     for (i = 1; i < r; i++)
    1768             :     {
    1769         903 :       GEN pr = gel(L,i);
    1770         903 :       long e = itos(gel(E0,i)), v = idealval(nf, dw, pr);
    1771         903 :       if (e >= 0)
    1772         896 :         gel(E,i) = addiu(gel(E,i), v);
    1773           7 :       else if (v + e <= 0)
    1774           0 :         F = idealmulpowprime(nf, F, pr, stoi(-v)); /* coprime to pr */
    1775             :       else
    1776             :       {
    1777           7 :         F = idealmulpowprime(nf, F, pr, stoi(e));
    1778           7 :         gel(E,i) = stoi(v + e);
    1779             :       }
    1780             :     }
    1781             :   }
    1782        2982 :   U = cgetg(r, t_VEC);
    1783        7147 :   for (i = 1; i < r; i++)
    1784             :   {
    1785             :     GEN u;
    1786        4165 :     if (w && gequal0(gel(w,i))) u = gen_0; /* unused */
    1787             :     else
    1788             :     {
    1789        4088 :       GEN pr = gel(L,i), e = gel(E,i), t;
    1790        4088 :       t = idealdivpowprime(nf,F, pr, e);
    1791        4088 :       u = hnfmerge_get_1(t, idealpow(nf, pr, e));
    1792        4088 :       if (!u) pari_err_COPRIME("idealchinese", t,pr);
    1793             :     }
    1794        4165 :     gel(U,i) = u;
    1795             :   }
    1796        2982 :   F = idealpseudored(F, nf_get_roundG(nf));
    1797        2982 :   return mkvec2(F, U);
    1798             : }
    1799             : 
    1800             : static GEN
    1801        1785 : pl_normalize(GEN nf, GEN pl)
    1802             : {
    1803        1785 :   const char *fun = "idealchinese";
    1804        1785 :   if (lg(pl)-1 != nf_get_r1(nf)) pari_err_TYPE(fun,pl);
    1805        1785 :   switch(typ(pl))
    1806             :   {
    1807         714 :     case t_VEC: RgV_check_ZV(pl,fun); pl = ZV_to_zv(pl);
    1808             :       /* fall through */
    1809        1785 :     case t_VECSMALL: break;
    1810           0 :     default: pari_err_TYPE(fun,pl);
    1811             :   }
    1812        1785 :   return pl;
    1813             : }
    1814             : 
    1815             : static int
    1816        7091 : is_chineseinit(GEN x)
    1817             : {
    1818             :   GEN fa, pl;
    1819             :   long l;
    1820        7091 :   if (typ(x) != t_VEC || lg(x)!=3) return 0;
    1821        5411 :   fa = gel(x,1);
    1822        5411 :   pl = gel(x,2);
    1823        5411 :   if (typ(fa) != t_VEC || typ(pl) != t_VEC) return 0;
    1824        2639 :   l = lg(fa);
    1825        2639 :   if (l != 1)
    1826             :   {
    1827        2618 :     if (l != 3 || typ(gel(fa,1)) != t_MAT || typ(gel(fa,2)) != t_VEC)
    1828           7 :       return 0;
    1829             :   }
    1830        2632 :   l = lg(pl);
    1831        2632 :   if (l != 1)
    1832             :   {
    1833         511 :     if (l != 6 || typ(gel(pl,3)) != t_MAT || typ(gel(pl,1)) != t_VECSMALL
    1834         511 :                                           || typ(gel(pl,2)) != t_VECSMALL)
    1835           0 :       return 0;
    1836             :   }
    1837        2632 :   return 1;
    1838             : }
    1839             : 
    1840             : /* nf a true 'nf' */
    1841             : static GEN
    1842        3122 : chineseinit_i(GEN nf, GEN fa, GEN w, GEN dw)
    1843             : {
    1844        3122 :   const char *fun = "idealchineseinit";
    1845        3122 :   GEN archp = NULL, pl = NULL;
    1846        3122 :   switch(typ(fa))
    1847             :   {
    1848        1785 :     case t_VEC:
    1849        1785 :       if (is_chineseinit(fa))
    1850             :       {
    1851           0 :         if (dw) pari_err_DOMAIN(fun, "denom(y)", "!=", gen_1, w);
    1852           0 :         return fa;
    1853             :       }
    1854        1785 :       if (lg(fa) != 3) pari_err_TYPE(fun, fa);
    1855             :       /* of the form [x,s] */
    1856        1785 :       pl = pl_normalize(nf, gel(fa,2));
    1857        1785 :       fa = gel(fa,1);
    1858        1785 :       archp = vecsmall01_to_indices(pl);
    1859             :       /* keep pr_init, reset pl */
    1860        1785 :       if (is_chineseinit(fa)) { fa = gel(fa,1); break; }
    1861             :       /* fall through */
    1862             :     case t_MAT: /* factorization? */
    1863        2989 :       if (isprfact(fa)) { fa = pr_init(nf, fa, w, dw); break; }
    1864           0 :     default: pari_err_TYPE(fun,fa);
    1865             :   }
    1866             : 
    1867        3122 :   if (!pl) pl = cgetg(1,t_VEC);
    1868             :   else
    1869             :   {
    1870        1785 :     long r = lg(archp);
    1871        1785 :     if (r == 1) pl = cgetg(1, t_VEC);
    1872             :     else
    1873             :     {
    1874        1365 :       GEN F = (lg(fa) == 1)? NULL: gel(fa,1), signs = cgetg(r, t_VECSMALL);
    1875             :       long i;
    1876        3381 :       for (i = 1; i < r; i++) signs[i] = (pl[archp[i]] < 0)? 1: 0;
    1877        1365 :       pl = setsigns_init(nf, archp, F, signs);
    1878             :     }
    1879             :   }
    1880        3122 :   return mkvec2(fa, pl);
    1881             : }
    1882             : 
    1883             : /* Given a prime ideal factorization x, possibly with 0 or negative exponents,
    1884             :  * and a vector w of elements of nf, gives b such that
    1885             :  * v_p(b-w_p)>=v_p(x) for all prime ideals p in the ideal factorization
    1886             :  * and v_p(b)>=0 for all other p, using the standard proof given in GTM 138. */
    1887             : GEN
    1888        5621 : idealchinese(GEN nf, GEN x, GEN w)
    1889             : {
    1890        5621 :   const char *fun = "idealchinese";
    1891        5621 :   pari_sp av = avma;
    1892             :   GEN x1, x2, s, dw, F;
    1893             : 
    1894        5621 :   nf = checknf(nf);
    1895        5621 :   if (!w) return gerepilecopy(av, chineseinit_i(nf,x,NULL,NULL));
    1896             : 
    1897        3521 :   if (typ(w) != t_VEC) pari_err_TYPE(fun,w);
    1898        3521 :   w = Q_remove_denom(matalgtobasis(nf,w), &dw);
    1899        3521 :   if (!is_chineseinit(x)) x = chineseinit_i(nf,x,w,dw);
    1900             :   /* x is a 'chineseinit' */
    1901        3521 :   x1 = gel(x,1); s = NULL;
    1902        3521 :   x2 = gel(x,2);
    1903        3521 :   if (lg(x1) == 1) F = NULL;
    1904             :   else
    1905             :   {
    1906        3500 :     GEN  U = gel(x1,2);
    1907        3500 :     long i, r = lg(w);
    1908        3500 :     F = gel(x1,1);
    1909        9975 :     for (i=1; i<r; i++)
    1910        6475 :       if (!gequal0(gel(w,i)))
    1911             :       {
    1912        4109 :         GEN t = nfmuli(nf, gel(U,i), gel(w,i));
    1913        4109 :         s = s? ZC_add(s,t): t;
    1914             :       }
    1915        3500 :     if (s) s = ZC_reducemodmatrix(s, F);
    1916             :   }
    1917        3521 :   if (lg(x2) != 1) s = nfsetsigns(nf, gel(x2,1), s? s: gen_0, x2);
    1918        3521 :   if (!s) { s = zerocol(nf_get_degree(nf)); dw = NULL; }
    1919             : 
    1920        3521 :   if (dw) s = RgC_Rg_div(s,dw);
    1921        3521 :   return gerepileupto(av, s);
    1922             : }
    1923             : 
    1924             : /*************************************************************************/
    1925             : /**                                                                     **/
    1926             : /**                           (Z_K/I)^*                                 **/
    1927             : /**                                                                     **/
    1928             : /*************************************************************************/
    1929             : GEN
    1930        1785 : vecsmall01_to_indices(GEN v)
    1931             : {
    1932        1785 :   long i, k, l = lg(v);
    1933        1785 :   GEN p = new_chunk(l) + l;
    1934        4788 :   for (k=1, i=l-1; i; i--)
    1935        3003 :     if (v[i]) { *--p = i; k++; }
    1936        1785 :   *--p = evallg(k) | evaltyp(t_VECSMALL);
    1937        1785 :   set_avma((pari_sp)p); return p;
    1938             : }
    1939             : GEN
    1940      423577 : vec01_to_indices(GEN v)
    1941             : {
    1942             :   long i, k, l;
    1943             :   GEN p;
    1944             : 
    1945      423577 :   switch (typ(v))
    1946             :   {
    1947      400155 :    case t_VECSMALL: return v;
    1948       23422 :    case t_VEC: break;
    1949           0 :    default: pari_err_TYPE("vec01_to_indices",v);
    1950             :   }
    1951       23422 :   l = lg(v);
    1952       23422 :   p = new_chunk(l) + l;
    1953       69587 :   for (k=1, i=l-1; i; i--)
    1954       46165 :     if (signe(gel(v,i))) { *--p = i; k++; }
    1955       23422 :   *--p = evallg(k) | evaltyp(t_VECSMALL);
    1956       23422 :   set_avma((pari_sp)p); return p;
    1957             : }
    1958             : GEN
    1959        2863 : indices_to_vec01(GEN p, long r)
    1960             : {
    1961        2863 :   long i, l = lg(p);
    1962        2863 :   GEN v = zerovec(r);
    1963        3801 :   for (i = 1; i < l; i++) gel(v, p[i]) = gen_1;
    1964        2863 :   return v;
    1965             : }
    1966             : 
    1967             : /* return (column) vector of R1 signatures of x (0 or 1) */
    1968             : GEN
    1969      400155 : nfsign_arch(GEN nf, GEN x, GEN arch)
    1970             : {
    1971      400155 :   GEN sarch, M, V, archp = vec01_to_indices(arch);
    1972      400155 :   long i, s, np, n = lg(archp)-1;
    1973             :   pari_sp av;
    1974             : 
    1975      400155 :   if (!n) return cgetg(1,t_VECSMALL);
    1976      397922 :   nf = checknf(nf);
    1977      397922 :   if (typ(x) == t_MAT)
    1978             :   { /* factorisation */
    1979      184028 :     GEN g = gel(x,1), e = gel(x,2);
    1980      184028 :     long l = lg(g);
    1981      184028 :     V = zero_zv(n);
    1982      414704 :     for (i = 1; i < l; i++)
    1983      230676 :       if (mpodd(gel(e,i)))
    1984      206964 :         Flv_add_inplace(V, nfsign_arch(nf,gel(g,i),archp), 2);
    1985      184028 :     set_avma((pari_sp)V); return V;
    1986             :   }
    1987      213894 :   av = avma; V = cgetg(n+1,t_VECSMALL);
    1988      213894 :   x = nf_to_scalar_or_basis(nf, x);
    1989      213894 :   switch(typ(x))
    1990             :   {
    1991       29633 :     case t_INT:
    1992       29633 :       s = signe(x);
    1993       29633 :       if (!s) pari_err_DOMAIN("nfsign_arch","element","=",gen_0,x);
    1994       29633 :       set_avma(av); return const_vecsmall(n, (s < 0)? 1: 0);
    1995          84 :     case t_FRAC:
    1996          84 :       s = signe(gel(x,1));
    1997          84 :       set_avma(av); return const_vecsmall(n, (s < 0)? 1: 0);
    1998             :   }
    1999      184177 :   x = Q_primpart(x); M = nf_get_M(nf); sarch = NULL; np = -1;
    2000      533638 :   for (i = 1; i <= n; i++)
    2001             :   {
    2002      350687 :     long s = eval_sign(M, x, archp[i]);
    2003      350687 :     if (s < 0) /* failure */
    2004             :     {
    2005        1264 :       long ni, r1 = nf_get_r1(nf);
    2006             :       GEN xi;
    2007        1264 :       if (np < 0)
    2008             :       {
    2009        1246 :         np = num_positive(nf, x);
    2010        1246 :         if (np == 0) { set_avma(av); return const_vecsmall(n, 1); }
    2011        1094 :         if (np == r1){ set_avma(av); return const_vecsmall(n, 0); }
    2012         639 :         sarch = nfarchstar(nf, NULL, identity_perm(r1));
    2013             :       }
    2014         657 :       xi = set_sign_mod_divisor(nf, vecsmall_ei(r1, archp[i]), gen_1, sarch);
    2015         657 :       xi = Q_primpart(xi);
    2016         657 :       ni = num_positive(nf, nfmuli(nf,x,xi));
    2017         657 :       if (ni == 0) { set_avma(av); V = const_vecsmall(n, 1); V[i] = 0; return V; }
    2018         451 :       if (ni == r1){ set_avma(av); V = const_vecsmall(n, 0); V[i] = 1; return V; }
    2019          38 :       s = ni < np? 0: 1;
    2020             :     }
    2021      349461 :     V[i] = s;
    2022             :   }
    2023      182951 :   set_avma((pari_sp)V); return V;
    2024             : }
    2025             : static void
    2026        6384 : chk_ind(const char *s, long i, long r1)
    2027             : {
    2028        6384 :   if (i <= 0) pari_err_DOMAIN(s, "index", "<=", gen_0, stoi(i));
    2029        6370 :   if (i > r1) pari_err_DOMAIN(s, "index", ">", utoi(r1), utoi(i));
    2030        6335 : }
    2031             : static GEN
    2032        6307 : parse_embed(GEN ind, long r, const char *f)
    2033             : {
    2034             :   long l, i;
    2035        6307 :   if (!ind) return identity_perm(r);
    2036        4354 :   switch(typ(ind))
    2037             :   {
    2038         154 :     case t_INT: case t_VEC: case t_COL: ind = gtovecsmall(ind); break;
    2039        4200 :     case t_VECSMALL: break;
    2040           0 :     default: pari_err_TYPE(f, ind);
    2041             :   }
    2042        4354 :   l = lg(ind);
    2043       10689 :   for (i = 1; i < l; i++) chk_ind(f, ind[i], r);
    2044        4305 :   return ind;
    2045             : }
    2046             : GEN
    2047        4774 : nfeltsign(GEN nf, GEN x, GEN ind0)
    2048             : {
    2049        4774 :   pari_sp av = avma;
    2050             :   long i, l;
    2051             :   GEN v, ind;
    2052        4774 :   nf = checknf(nf);
    2053        4774 :   ind = parse_embed(ind0, nf_get_r1(nf), "nfeltsign");
    2054        4753 :   l = lg(ind);
    2055        4753 :   if (is_rational_t(typ(x)))
    2056             :   { /* nfsign_arch would test this, but avoid converting t_VECSMALL -> t_VEC */
    2057             :     GEN s;
    2058        2086 :     switch(gsigne(x))
    2059             :     {
    2060         525 :       case -1:s = gen_m1; break;
    2061        1554 :       case 1: s = gen_1; break;
    2062           7 :       default: s = gen_0; break;
    2063             :     }
    2064        2086 :     set_avma(av);
    2065        2086 :     return (ind0 && typ(ind0) == t_INT)? s: const_vec(l-1, s);
    2066             :   }
    2067        2667 :   v = nfsign_arch(nf, x, ind);
    2068        2667 :   if (ind0 && typ(ind0) == t_INT) { set_avma(av); return v[1]? gen_m1: gen_1; }
    2069        2653 :   settyp(v, t_VEC);
    2070        7238 :   for (i = 1; i < l; i++) gel(v,i) = v[i]? gen_m1: gen_1;
    2071        2653 :   return gerepileupto(av, v);
    2072             : }
    2073             : 
    2074             : GEN
    2075          63 : nfeltembed(GEN nf, GEN x, GEN ind0, long prec0)
    2076             : {
    2077          63 :   pari_sp av = avma;
    2078             :   long i, e, l, r1, r2, prec, prec1;
    2079             :   GEN v, ind, cx;
    2080          63 :   nf = checknf(nf); nf_get_sign(nf,&r1,&r2);
    2081          63 :   x = nf_to_scalar_or_basis(nf, x);
    2082          56 :   ind = parse_embed(ind0, r1+r2, "nfeltembed");
    2083          49 :   l = lg(ind);
    2084          49 :   if (typ(x) != t_COL)
    2085             :   {
    2086           0 :     if (!(ind0 && typ(ind0) == t_INT)) x = const_vec(l-1, x);
    2087           0 :     return gerepilecopy(av, x);
    2088             :   }
    2089          49 :   x = Q_primitive_part(x, &cx);
    2090          49 :   prec1 = prec0; e = gexpo(x);
    2091          49 :   if (e > 8) prec1 += nbits2extraprec(e);
    2092          49 :   prec = prec1;
    2093          49 :   if (nf_get_prec(nf) < prec) nf = nfnewprec_shallow(nf, prec);
    2094          49 :   v = cgetg(l, t_VEC);
    2095             :   for(;;)
    2096           7 :   {
    2097          56 :     GEN M = nf_get_M(nf);
    2098         140 :     for (i = 1; i < l; i++)
    2099             :     {
    2100          91 :       GEN t = nfembed_i(M, x, ind[i]);
    2101          91 :       long e = gexpo(t);
    2102          91 :       if (gequal0(t) || precision(t) < prec0
    2103          91 :                      || (e < 0 && prec < prec1 + nbits2extraprec(-e)) ) break;
    2104          84 :       if (cx) t = gmul(t, cx);
    2105          84 :       gel(v,i) = t;
    2106             :     }
    2107          56 :     if (i == l) break;
    2108           7 :     prec = precdbl(prec);
    2109           7 :     if (DEBUGLEVEL>1) pari_warn(warnprec,"eltnfembed", prec);
    2110           7 :     nf = nfnewprec_shallow(nf, prec);
    2111             :   }
    2112          49 :   if (ind0 && typ(ind0) == t_INT) v = gel(v,1);
    2113          49 :   return gerepilecopy(av, v);
    2114             : }
    2115             : 
    2116             : /* number of distinct roots of sigma(f) */
    2117             : GEN
    2118        1477 : nfpolsturm(GEN nf, GEN f, GEN ind0)
    2119             : {
    2120        1477 :   pari_sp av = avma;
    2121             :   long d, l, r1, single;
    2122             :   GEN ind, u, v, vr1, T, s, t;
    2123             : 
    2124        1477 :   nf = checknf(nf); T = nf_get_pol(nf); r1 = nf_get_r1(nf);
    2125        1477 :   ind = parse_embed(ind0, r1, "nfpolsturm");
    2126        1456 :   single = ind0 && typ(ind0) == t_INT;
    2127        1456 :   l = lg(ind);
    2128             : 
    2129        1456 :   if (gequal0(f)) pari_err_ROOTS0("nfpolsturm");
    2130        1449 :   if (typ(f) == t_POL && varn(f) != varn(T))
    2131             :   {
    2132        1428 :     f = RgX_nffix("nfpolsturm", T, f,1);
    2133        1428 :     if (lg(f) == 3) f = NULL;
    2134             :   }
    2135             :   else
    2136             :   {
    2137          21 :     (void)Rg_nffix("nfpolsturm", T, f, 0);
    2138          21 :     f = NULL;
    2139             :   }
    2140        1449 :   if (!f) { set_avma(av); return single? gen_0: zerovec(l-1); }
    2141        1428 :   d = degpol(f);
    2142        1428 :   if (d == 1) { set_avma(av); return single? gen_1: const_vec(l-1,gen_1); }
    2143             : 
    2144        1393 :   vr1 = const_vecsmall(l-1, 1);
    2145        1393 :   u = Q_primpart(f); s = ZV_to_zv(nfeltsign(nf, gel(u,d+2), ind));
    2146        1393 :   v = RgX_deriv(u); t = odd(d)? leafcopy(s): zv_neg(s);
    2147             :   for(;;)
    2148         182 :   {
    2149        1575 :     GEN r = RgX_neg( Q_primpart(RgX_pseudorem(u, v)) ), sr;
    2150        1575 :     long i, dr = degpol(r);
    2151        1575 :     if (dr < 0) break;
    2152        1575 :     sr = ZV_to_zv(nfeltsign(nf, gel(r,dr+2), ind));
    2153        3934 :     for (i = 1; i < l; i++)
    2154        2359 :       if (sr[i] != s[i]) { s[i] = sr[i], vr1[i]--; }
    2155        1575 :     if (odd(dr)) sr = zv_neg(sr);
    2156        3934 :     for (i = 1; i < l; i++)
    2157        2359 :       if (sr[i] != t[i]) { t[i] = sr[i], vr1[i]++; }
    2158        1575 :     if (!dr) break;
    2159         182 :     u = v; v = r;
    2160             :   }
    2161        1393 :   if (single) { set_avma(av); return stoi(vr1[1]); }
    2162        1386 :   return gerepileupto(av, zv_to_ZV(vr1));
    2163             : }
    2164             : 
    2165             : /* return the vector of signs of x; the matrix of such if x is a vector
    2166             :  * of nf elements */
    2167             : GEN
    2168        2128 : nfsign(GEN nf, GEN x)
    2169             : {
    2170             :   long i, l;
    2171             :   GEN archp, S;
    2172             : 
    2173        2128 :   nf = checknf(nf);
    2174        2128 :   archp = identity_perm( nf_get_r1(nf) );
    2175        2128 :   if (typ(x) != t_VEC) return nfsign_arch(nf, x, archp);
    2176         924 :   l = lg(x); S = cgetg(l, t_MAT);
    2177        4557 :   for (i=1; i<l; i++) gel(S,i) = nfsign_arch(nf, gel(x,i), archp);
    2178         924 :   return S;
    2179             : }
    2180             : 
    2181             : /* x integral elt, A integral ideal in HNF; reduce x mod A */
    2182             : static GEN
    2183     1254959 : zk_modHNF(GEN x, GEN A)
    2184     1254959 : { return (typ(x) == t_COL)?  ZC_hnfrem(x, A): modii(x, gcoeff(A,1,1)); }
    2185             : 
    2186             : /* given an element x in Z_K and an integral ideal y in HNF, coprime with x,
    2187             :    outputs an element inverse of x modulo y */
    2188             : GEN
    2189          63 : nfinvmodideal(GEN nf, GEN x, GEN y)
    2190             : {
    2191          63 :   pari_sp av = avma;
    2192          63 :   GEN a, yZ = gcoeff(y,1,1);
    2193             : 
    2194          63 :   if (equali1(yZ)) return gen_0;
    2195          63 :   x = nf_to_scalar_or_basis(nf, x);
    2196          63 :   if (typ(x) == t_INT) return gerepileupto(av, Fp_inv(x, yZ));
    2197             : 
    2198          35 :   a = hnfmerge_get_1(idealhnf_principal(nf,x), y);
    2199          35 :   if (!a) pari_err_INV("nfinvmodideal", x);
    2200          35 :   return gerepileupto(av, zk_modHNF(nfdiv(nf,a,x), y));
    2201             : }
    2202             : 
    2203             : static GEN
    2204      583147 : nfsqrmodideal(GEN nf, GEN x, GEN id)
    2205      583147 : { return zk_modHNF(nfsqri(nf,x), id); }
    2206             : static GEN
    2207     1620355 : nfmulmodideal(GEN nf, GEN x, GEN y, GEN id)
    2208     1620355 : { return x? zk_modHNF(nfmuli(nf,x,y), id): y; }
    2209             : /* assume x integral, k integer, A in HNF */
    2210             : GEN
    2211     1072151 : nfpowmodideal(GEN nf,GEN x,GEN k,GEN A)
    2212             : {
    2213     1072151 :   long s = signe(k);
    2214             :   pari_sp av;
    2215             :   GEN y;
    2216             : 
    2217     1072151 :   if (!s) return gen_1;
    2218     1072151 :   av = avma;
    2219     1072151 :   x = nf_to_scalar_or_basis(nf, x);
    2220     1072151 :   if (typ(x) != t_COL) return Fp_pow(x, k, gcoeff(A,1,1));
    2221      513159 :   if (s < 0) { x = nfinvmodideal(nf, x,A); k = negi(k); }
    2222      513159 :   for(y = NULL;;)
    2223             :   {
    2224     1096306 :     if (mpodd(k)) y = nfmulmodideal(nf,y,x,A);
    2225     1096306 :     k = shifti(k,-1); if (!signe(k)) break;
    2226      583147 :     x = nfsqrmodideal(nf,x,A);
    2227             :   }
    2228      513159 :   return gerepileupto(av, y);
    2229             : }
    2230             : 
    2231             : /* a * g^n mod id */
    2232             : static GEN
    2233      867134 : nfmulpowmodideal(GEN nf, GEN a, GEN g, GEN n, GEN id)
    2234             : {
    2235      867134 :   return nfmulmodideal(nf, a, nfpowmodideal(nf,g,n,id), id);
    2236             : }
    2237             : 
    2238             : /* assume (num(g[i]), id) = 1 for all i. Return prod g[i]^e[i] mod id.
    2239             :  * EX = multiple of exponent of (O_K/id)^* */
    2240             : GEN
    2241      458806 : famat_to_nf_modideal_coprime(GEN nf, GEN g, GEN e, GEN id, GEN EX)
    2242             : {
    2243      458806 :   GEN EXo2, plus = NULL, minus = NULL, idZ = gcoeff(id,1,1);
    2244      458806 :   long i, lx = lg(g);
    2245             : 
    2246      458806 :   if (equali1(idZ)) return gen_1; /* id = Z_K */
    2247      458351 :   EXo2 = (expi(EX) > 10)? shifti(EX,-1): NULL;
    2248     1528707 :   for (i = 1; i < lx; i++)
    2249             :   {
    2250     1070356 :     GEN h, n = centermodii(gel(e,i), EX, EXo2);
    2251     1070356 :     long sn = signe(n);
    2252     1070356 :     if (!sn) continue;
    2253             : 
    2254      806769 :     h = nf_to_scalar_or_basis(nf, gel(g,i));
    2255      806769 :     switch(typ(h))
    2256             :     {
    2257      411379 :       case t_INT: break;
    2258           0 :       case t_FRAC:
    2259           0 :         h = Fp_div(gel(h,1), gel(h,2), idZ); break;
    2260      395390 :       default:
    2261             :       {
    2262             :         GEN dh;
    2263      395390 :         h = Q_remove_denom(h, &dh);
    2264      395390 :         if (dh) h = FpC_Fp_mul(h, Fp_inv(dh,idZ), idZ);
    2265             :       }
    2266             :     }
    2267      806769 :     if (sn > 0)
    2268      805215 :       plus = nfmulpowmodideal(nf, plus, h, n, id);
    2269             :     else /* sn < 0 */
    2270        1554 :       minus = nfmulpowmodideal(nf, minus, h, negi(n), id);
    2271             :   }
    2272      458351 :   if (minus) plus = nfmulmodideal(nf, plus, nfinvmodideal(nf,minus,id), id);
    2273      458351 :   return plus? plus: gen_1;
    2274             : }
    2275             : 
    2276             : /* given 2 integral ideals x, y in HNF s.t x | y | x^2, compute (1+x)/(1+y) in
    2277             :  * the form [[cyc],[gen], U], where U := ux^-1 as a pair [ZM, denom(U)] */
    2278             : static GEN
    2279       22498 : zidealij(GEN x, GEN y)
    2280             : {
    2281       22498 :   GEN U, G, cyc, xp = gcoeff(x,1,1), xi = hnf_invscale(x, xp);
    2282             :   long j, N;
    2283             : 
    2284             :   /* x^(-1) y = relations between the 1 + x_i (HNF) */
    2285       22498 :   cyc = ZM_snf_group(ZM_Z_divexact(ZM_mul(xi, y), xp), &U, &G);
    2286       22498 :   N = lg(cyc); G = ZM_mul(x,G); settyp(G, t_VEC); /* new generators */
    2287       53977 :   for (j=1; j<N; j++)
    2288             :   {
    2289       31479 :     GEN c = gel(G,j);
    2290       31479 :     gel(c,1) = addiu(gel(c,1), 1); /* 1 + g_j */
    2291       31479 :     if (ZV_isscalar(c)) gel(G,j) = gel(c,1);
    2292             :   }
    2293       22498 :   return mkvec4(cyc, G, ZM_mul(U,xi), xp);
    2294             : }
    2295             : 
    2296             : /* lg(x) > 1, x + 1; shallow */
    2297             : static GEN
    2298       12922 : ZC_add1(GEN x)
    2299             : {
    2300       12922 :   long i, l = lg(x);
    2301       12922 :   GEN y = cgetg(l, t_COL);
    2302       32137 :   for (i = 2; i < l; i++) gel(y,i) = gel(x,i);
    2303       12922 :   gel(y,1) = addiu(gel(x,1), 1); return y;
    2304             : }
    2305             : /* lg(x) > 1, x - 1; shallow */
    2306             : static GEN
    2307        6342 : ZC_sub1(GEN x)
    2308             : {
    2309        6342 :   long i, l = lg(x);
    2310        6342 :   GEN y = cgetg(l, t_COL);
    2311       17052 :   for (i = 2; i < l; i++) gel(y,i) = gel(x,i);
    2312        6342 :   gel(y,1) = subiu(gel(x,1), 1); return y;
    2313             : }
    2314             : 
    2315             : /* x,y are t_INT or ZC */
    2316             : static GEN
    2317           0 : zkadd(GEN x, GEN y)
    2318             : {
    2319           0 :   long tx = typ(x);
    2320           0 :   if (tx == typ(y))
    2321           0 :     return tx == t_INT? addii(x,y): ZC_add(x,y);
    2322             :   else
    2323           0 :     return tx == t_INT? ZC_Z_add(y,x): ZC_Z_add(x,y);
    2324             : }
    2325             : /* x a t_INT or ZC, x+1; shallow */
    2326             : static GEN
    2327       27279 : zkadd1(GEN x)
    2328             : {
    2329       27279 :   long tx = typ(x);
    2330       27279 :   return tx == t_INT? addiu(x,1): ZC_add1(x);
    2331             : }
    2332             : /* x a t_INT or ZC, x-1; shallow */
    2333             : static GEN
    2334       27279 : zksub1(GEN x)
    2335             : {
    2336       27279 :   long tx = typ(x);
    2337       27279 :   return tx == t_INT? subiu(x,1): ZC_sub1(x);
    2338             : }
    2339             : /* x,y are t_INT or ZC; x - y */
    2340             : static GEN
    2341           0 : zksub(GEN x, GEN y)
    2342             : {
    2343           0 :   long tx = typ(x), ty = typ(y);
    2344           0 :   if (tx == ty)
    2345           0 :     return tx == t_INT? subii(x,y): ZC_sub(x,y);
    2346             :   else
    2347           0 :     return tx == t_INT? Z_ZC_sub(x,y): ZC_Z_sub(x,y);
    2348             : }
    2349             : /* x is t_INT or ZM (mult. map), y is t_INT or ZC; x * y */
    2350             : static GEN
    2351       27279 : zkmul(GEN x, GEN y)
    2352             : {
    2353       27279 :   long tx = typ(x), ty = typ(y);
    2354       27279 :   if (ty == t_INT)
    2355       20937 :     return tx == t_INT? mulii(x,y): ZC_Z_mul(gel(x,1),y);
    2356             :   else
    2357        6342 :     return tx == t_INT? ZC_Z_mul(y,x): ZM_ZC_mul(x,y);
    2358             : }
    2359             : 
    2360             : /* (U,V) = 1 coprime ideals. Want z = x mod U, = y mod V; namely
    2361             :  * z =vx + uy = v(x-y) + y, where u + v = 1, u in U, v in V.
    2362             :  * zkc = [v, UV], v a t_INT or ZM (mult. by v map), UV a ZM (ideal in HNF);
    2363             :  * shallow */
    2364             : GEN
    2365           0 : zkchinese(GEN zkc, GEN x, GEN y)
    2366             : {
    2367           0 :   GEN v = gel(zkc,1), UV = gel(zkc,2), z = zkadd(zkmul(v, zksub(x,y)), y);
    2368           0 :   return zk_modHNF(z, UV);
    2369             : }
    2370             : /* special case z = x mod U, = 1 mod V; shallow */
    2371             : GEN
    2372       27279 : zkchinese1(GEN zkc, GEN x)
    2373             : {
    2374       27279 :   GEN v = gel(zkc,1), UV = gel(zkc,2), z = zkadd1(zkmul(v, zksub1(x)));
    2375       27279 :   return (typ(z) == t_INT)? z: ZC_hnfrem(z, UV);
    2376             : }
    2377             : static GEN
    2378       25172 : zkVchinese1(GEN zkc, GEN v)
    2379             : {
    2380             :   long i, ly;
    2381       25172 :   GEN y = cgetg_copy(v, &ly);
    2382       52451 :   for (i=1; i<ly; i++) gel(y,i) = zkchinese1(zkc, gel(v,i));
    2383       25172 :   return y;
    2384             : }
    2385             : 
    2386             : /* prepare to solve z = x (mod A), z = y mod (B) [zkchinese or zkchinese1] */
    2387             : GEN
    2388       24913 : zkchineseinit(GEN nf, GEN A, GEN B, GEN AB)
    2389             : {
    2390             :   GEN v;
    2391             :   long e;
    2392       24913 :   nf = checknf(nf);
    2393       24913 :   v = idealaddtoone_raw(nf, A, B);
    2394       24913 :   if ((e = gexpo(v)) > 5)
    2395             :   {
    2396        1421 :     GEN b = (typ(v) == t_COL)? v: scalarcol_shallow(v, nf_get_degree(nf));
    2397        1421 :     b= ZC_reducemodlll(b, AB);
    2398        1421 :     if (gexpo(b) < e) v = b;
    2399             :   }
    2400       24913 :   return mkvec2(zk_scalar_or_multable(nf,v), AB);
    2401             : }
    2402             : /* prepare to solve z = x (mod A), z = 1 mod (B)
    2403             :  * and then         z = 1 (mod A), z = y mod (B) [zkchinese1 twice] */
    2404             : static GEN
    2405         259 : zkchinese1init2(GEN nf, GEN A, GEN B, GEN AB)
    2406             : {
    2407         259 :   GEN zkc = zkchineseinit(nf, A, B, AB);
    2408         259 :   GEN mv = gel(zkc,1), mu;
    2409         259 :   if (typ(mv) == t_INT) return mkvec2(zkc, mkvec2(subui(1,mv),AB));
    2410          35 :   mu = RgM_Rg_add_shallow(ZM_neg(mv), gen_1);
    2411          35 :   return mkvec2(mkvec2(mv,AB), mkvec2(mu,AB));
    2412             : }
    2413             : 
    2414             : static GEN
    2415      327614 : apply_U(GEN L, GEN a)
    2416             : {
    2417      327614 :   GEN e, U = gel(L,3), dU = gel(L,4);
    2418      327614 :   if (typ(a) == t_INT)
    2419      138962 :     e = ZC_Z_mul(gel(U,1), subiu(a, 1));
    2420             :   else
    2421             :   { /* t_COL */
    2422      188652 :     GEN t = shallowcopy(a);
    2423      188652 :     gel(t,1) = subiu(gel(t,1), 1); /* t = a - 1 */
    2424      188652 :     e = ZM_ZC_mul(U, t);
    2425             :   }
    2426      327614 :   return gdiv(e, dU);
    2427             : }
    2428             : 
    2429             : /* true nf; vectors of [[cyc],[g],U.X^-1]. Assume k > 1. */
    2430             : static GEN
    2431       15449 : principal_units(GEN nf, GEN pr, long k, GEN prk)
    2432             : {
    2433             :   GEN list, prb;
    2434       15449 :   ulong mask = quadratic_prec_mask(k);
    2435       15449 :   long a = 1;
    2436             : 
    2437       15449 :   prb = pr_hnf(nf,pr);
    2438       15449 :   list = vectrunc_init(k);
    2439       37947 :   while (mask > 1)
    2440             :   {
    2441       22498 :     GEN pra = prb;
    2442       22498 :     long b = a << 1;
    2443             : 
    2444       22498 :     if (mask & 1) b--;
    2445       22498 :     mask >>= 1;
    2446             :     /* compute 1 + pr^a / 1 + pr^b, 2a <= b */
    2447       22498 :     prb = (b >= k)? prk: idealpows(nf,pr,b);
    2448       22498 :     vectrunc_append(list, zidealij(pra, prb));
    2449       22498 :     a = b;
    2450             :   }
    2451       15449 :   return list;
    2452             : }
    2453             : /* a = 1 mod (pr) return log(a) on local-gens of 1+pr/1+pr^k */
    2454             : static GEN
    2455      204575 : log_prk1(GEN nf, GEN a, long nh, GEN L2, GEN prk)
    2456             : {
    2457      204575 :   GEN y = cgetg(nh+1, t_COL);
    2458      204575 :   long j, iy, c = lg(L2)-1;
    2459      532182 :   for (j = iy = 1; j <= c; j++)
    2460             :   {
    2461      327614 :     GEN L = gel(L2,j), cyc = gel(L,1), gen = gel(L,2), E = apply_U(L,a);
    2462      327614 :     long i, nc = lg(cyc)-1;
    2463      327614 :     int last = (j == c);
    2464     1012284 :     for (i = 1; i <= nc; i++, iy++)
    2465             :     {
    2466      684677 :       GEN t, e = gel(E,i);
    2467      684677 :       if (typ(e) != t_INT) pari_err_COPRIME("zlog_prk1", a, prk);
    2468      684670 :       t = Fp_neg(e, gel(cyc,i));
    2469      684670 :       gel(y,iy) = negi(t);
    2470      684670 :       if (!last && signe(t)) a = nfmulpowmodideal(nf, a, gel(gen,i), t, prk);
    2471             :     }
    2472             :   }
    2473      204568 :   return y;
    2474             : }
    2475             : /* true nf */
    2476             : static GEN
    2477        5873 : principal_units_relations(GEN nf, GEN L2, GEN prk, long nh)
    2478             : {
    2479        5873 :   GEN h = cgetg(nh+1,t_MAT);
    2480        5873 :   long ih, j, c = lg(L2)-1;
    2481       18795 :   for (j = ih = 1; j <= c; j++)
    2482             :   {
    2483       12922 :     GEN L = gel(L2,j), F = gel(L,1), G = gel(L,2);
    2484       12922 :     long k, lG = lg(G);
    2485       32760 :     for (k = 1; k < lG; k++,ih++)
    2486             :     { /* log(g^f) mod pr^e */
    2487       19838 :       GEN a = nfpowmodideal(nf,gel(G,k),gel(F,k),prk);
    2488       19838 :       gel(h,ih) = ZC_neg(log_prk1(nf, a, nh, L2, prk));
    2489       19838 :       gcoeff(h,ih,ih) = gel(F,k);
    2490             :     }
    2491             :   }
    2492        5873 :   return h;
    2493             : }
    2494             : /* true nf; k > 1; multiplicative group (1 + pr) / (1 + pr^k) */
    2495             : static GEN
    2496       15449 : idealprincipalunits_i(GEN nf, GEN pr, long k, GEN *pU)
    2497             : {
    2498       15449 :   GEN cyc, gen, L2, prk = idealpows(nf, pr, k);
    2499             : 
    2500       15449 :   L2 = principal_units(nf, pr, k, prk);
    2501       15449 :   if (k == 2)
    2502             :   {
    2503        9576 :     GEN L = gel(L2,1);
    2504        9576 :     cyc = gel(L,1);
    2505        9576 :     gen = gel(L,2);
    2506        9576 :     if (pU) *pU = matid(lg(gen)-1);
    2507             :   }
    2508             :   else
    2509             :   {
    2510        5873 :     long c = lg(L2), j;
    2511        5873 :     GEN EX, h, Ui, vg = cgetg(c, t_VEC);
    2512       18795 :     for (j = 1; j < c; j++) gel(vg, j) = gmael(L2,j,2);
    2513        5873 :     vg = shallowconcat1(vg);
    2514        5873 :     h = principal_units_relations(nf, L2, prk, lg(vg)-1);
    2515        5873 :     h = ZM_hnfall_i(h, NULL, 0);
    2516        5873 :     cyc = ZM_snf_group(h, pU, &Ui);
    2517        5873 :     c = lg(Ui); gen = cgetg(c, t_VEC); EX = cyc_get_expo(cyc);
    2518       20860 :     for (j = 1; j < c; j++)
    2519       14987 :       gel(gen,j) = famat_to_nf_modideal_coprime(nf, vg, gel(Ui,j), prk, EX);
    2520             :   }
    2521       15449 :   return mkvec4(cyc, gen, prk, L2);
    2522             : }
    2523             : GEN
    2524         112 : idealprincipalunits(GEN nf, GEN pr, long k)
    2525             : {
    2526             :   pari_sp av;
    2527             :   GEN v;
    2528         112 :   nf = checknf(nf);
    2529         112 :   if (k == 1) { checkprid(pr); retmkvec3(gen_1,cgetg(1,t_VEC),cgetg(1,t_VEC)); }
    2530         105 :   av = avma; v = idealprincipalunits_i(nf, pr, k, NULL);
    2531         105 :   return gerepilecopy(av, mkvec3(powiu(pr_norm(pr), k-1), gel(v,1), gel(v,2)));
    2532             : }
    2533             : 
    2534             : /* true nf; given an ideal pr^k dividing an integral ideal x (in HNF form)
    2535             :  * compute an 'sprk', the structure of G = (Z_K/pr^k)^* [ x = NULL for x=pr^k ]
    2536             :  * Return a vector with at least 4 components [cyc],[gen],[HNF pr^k,pr,k],ff,
    2537             :  * where
    2538             :  * cyc : type of G as abelian group (SNF)
    2539             :  * gen : generators of G, coprime to x
    2540             :  * pr^k: in HNF
    2541             :  * ff  : data for log_g in (Z_K/pr)^*
    2542             :  * Two extra components are present iff k > 1: L2, U
    2543             :  * L2  : list of data structures to compute local DL in (Z_K/pr)^*,
    2544             :  *       and 1 + pr^a/ 1 + pr^b for various a < b <= min(2a, k)
    2545             :  * U   : base change matrices to convert a vector of local DL to DL wrt gen */
    2546             : static GEN
    2547       47670 : sprkinit(GEN nf, GEN pr, long k, GEN x)
    2548             : {
    2549             :   GEN T, p, modpr, cyc, gen, g, g0, ord0, A, prk, U, L2;
    2550       47670 :   long f = pr_get_f(pr);
    2551             : 
    2552       47670 :   if(DEBUGLEVEL>3) err_printf("treating pr^%ld, pr = %Ps\n",k,pr);
    2553       47670 :   modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    2554             :   /* (Z_K / pr)^* */
    2555       47670 :   if (f == 1)
    2556             :   {
    2557       40649 :     g0 = g = pgener_Fp(p);
    2558       40649 :     ord0 = get_arith_ZZM(subiu(p,1));
    2559             :   }
    2560             :   else
    2561             :   {
    2562        7021 :     g0 = g = gener_FpXQ(T,p, &ord0);
    2563        7021 :     g = Fq_to_nf(g, modpr);
    2564        7021 :     if (typ(g) == t_POL) g = poltobasis(nf, g);
    2565             :   }
    2566       47670 :   A = gel(ord0, 1); /* Norm(pr)-1 */
    2567       47670 :   if (k == 1)
    2568             :   {
    2569       32326 :     cyc = mkvec(A);
    2570       32326 :     gen = mkvec(g);
    2571       32326 :     prk = pr_hnf(nf,pr);
    2572       32326 :     L2 = U = NULL;
    2573             :   }
    2574             :   else
    2575             :   { /* local-gens of (1 + pr)/(1 + pr^k) = SNF-gens * U */
    2576             :     GEN AB, B, u, v, w;
    2577             :     long j, l;
    2578       15344 :     w = idealprincipalunits_i(nf, pr, k, &U);
    2579             :     /* incorporate (Z_K/pr)^*, order A coprime to B = expo(1+pr/1+pr^k)*/
    2580       15344 :     cyc = leafcopy(gel(w,1)); B = cyc_get_expo(cyc); AB = mulii(A,B);
    2581       15344 :     gen = leafcopy(gel(w,2));
    2582       15344 :     prk = gel(w,3);
    2583       15344 :     g = nfpowmodideal(nf, g, B, prk);
    2584       15344 :     g0 = Fq_pow(g0, modii(B,A), T, p); /* update primitive root */
    2585       15344 :     L2 = mkvec3(A, g, gel(w,4));
    2586       15344 :     gel(cyc,1) = AB;
    2587       15344 :     gel(gen,1) = nfmulmodideal(nf, gel(gen,1), g, prk);
    2588       15344 :     u = mulii(Fp_inv(A,B), A);
    2589       15344 :     v = subui(1, u); l = lg(U);
    2590       46445 :     for (j = 1; j < l; j++) gcoeff(U,1,j) = Fp_mul(u, gcoeff(U,1,j), AB);
    2591       15344 :     U = mkvec2(Rg_col_ei(v, lg(gen)-1, 1), U);
    2592             :   }
    2593             :   /* local-gens of (Z_K/pr^k)^* = SNF-gens * U */
    2594       47670 :   if (x)
    2595             :   {
    2596       24654 :     GEN uv = zkchineseinit(nf, idealmulpowprime(nf,x,pr,utoineg(k)), prk, x);
    2597       24654 :     gen = zkVchinese1(uv, gen);
    2598             :   }
    2599       47670 :   return mkvecn(U? 6: 4, cyc, gen, prk, mkvec3(modpr,g0,ord0), L2, U);
    2600             : }
    2601             : static GEN
    2602      659895 : sprk_get_cyc(GEN s) { return gel(s,1); }
    2603             : static GEN
    2604      343306 : sprk_get_expo(GEN s) { return cyc_get_expo(sprk_get_cyc(s)); }
    2605             : static GEN
    2606       44849 : sprk_get_gen(GEN s) { return gel(s,2); }
    2607             : static GEN
    2608      817785 : sprk_get_prk(GEN s) { return gel(s,3); }
    2609             : static GEN
    2610      424611 : sprk_get_ff(GEN s) { return gel(s,4); }
    2611             : static GEN
    2612      377739 : sprk_get_pr(GEN s) { GEN ff = gel(s,4); return modpr_get_pr(gel(ff,1)); }
    2613             : /* L2 to 1 + pr / 1 + pr^k */
    2614             : static GEN
    2615      202853 : sprk_get_L2(GEN s) { return gmael(s,5,3); }
    2616             : /* lift to nf of primitive root of k(pr) */
    2617             : static GEN
    2618       10892 : sprk_get_gnf(GEN s) { return gmael(s,5,2); }
    2619             : static void
    2620      185619 : sprk_get_U2(GEN s, GEN *U1, GEN *U2)
    2621      185619 : { GEN v = gel(s,6); *U1 = gel(v,1); *U2 = gel(v,2); }
    2622             : static int
    2623      424611 : sprk_is_prime(GEN s) { return lg(s) == 5; }
    2624             : 
    2625             : static GEN
    2626      343243 : famat_zlog_pr(GEN nf, GEN g, GEN e, GEN sprk, GEN mod)
    2627             : {
    2628      343243 :   GEN x, expo = sprk_get_expo(sprk);
    2629      343243 :   if (mod) expo = gcdii(expo,mod);
    2630      343243 :   x = famat_makecoprime(nf, g, e, sprk_get_pr(sprk), sprk_get_prk(sprk), expo);
    2631      343243 :   return log_prk(nf, x, sprk, mod);
    2632             : }
    2633             : /* famat_zlog_pr assuming (g,sprk.pr) = 1 */
    2634             : static GEN
    2635          63 : famat_zlog_pr_coprime(GEN nf, GEN g, GEN e, GEN sprk, GEN MOD)
    2636             : {
    2637          63 :   GEN x = famat_to_nf_modideal_coprime(nf, g, e, sprk_get_prk(sprk),
    2638             :                                        sprk_get_expo(sprk));
    2639          63 :   return log_prk(nf, x, sprk, MOD);
    2640             : }
    2641             : 
    2642             : /* o t_INT, O = [ord,fa] format for multiple of o (for Fq_log);
    2643             :  * return o in [ord,fa] format */
    2644             : static GEN
    2645      122263 : order_update(GEN o, GEN O)
    2646             : {
    2647      122263 :   GEN p = gmael(O,2,1), z = o, P, E;
    2648      122263 :   long i, j, l = lg(p);
    2649      122263 :   P = cgetg(l, t_COL);
    2650      122263 :   E = cgetg(l, t_COL);
    2651      179867 :   for (i = j = 1; i < l; i++)
    2652             :   {
    2653      179867 :     long v = Z_pvalrem(z, gel(p,i), &z);
    2654      179867 :     if (v)
    2655             :     {
    2656      158951 :       gel(P,j) = gel(p,i);
    2657      158951 :       gel(E,j) = utoipos(v); j++;
    2658      158951 :       if (is_pm1(z)) break;
    2659             :     }
    2660             :   }
    2661      122263 :   setlg(P, j);
    2662      122263 :   setlg(E, j); return mkvec2(o, mkmat2(P,E));
    2663             : }
    2664             : 
    2665             : /* a in Z_K (t_COL or t_INT), pr prime ideal, sprk = sprkinit(nf,pr,k,x),
    2666             :  * mod positive t_INT or NULL (meaning mod=0).
    2667             :  * return log(a) modulo mod on SNF-generators of (Z_K/pr^k)^* */
    2668             : GEN
    2669      430204 : log_prk(GEN nf, GEN a, GEN sprk, GEN mod)
    2670             : {
    2671             :   GEN e, prk, g, U1, U2, y, ff, O, o, oN, gN,  N, T, p, modpr, pr, cyc;
    2672             : 
    2673      430204 :   if (typ(a) == t_MAT) return famat_zlog_pr(nf, gel(a,1), gel(a,2), sprk, mod);
    2674      424611 :   N = NULL;
    2675      424611 :   ff = sprk_get_ff(sprk);
    2676      424611 :   pr = gel(ff,1); /* modpr */
    2677      424611 :   g = gN = gel(ff,2);
    2678      424611 :   O = gel(ff,3); /* order of g = |Fq^*|, in [ord, fa] format */
    2679      424611 :   o = oN = gel(O,1); /* order as a t_INT */
    2680      424611 :   prk = sprk_get_prk(sprk);
    2681      424611 :   modpr = nf_to_Fq_init(nf, &pr, &T, &p);
    2682      424611 :   if (mod)
    2683             :   {
    2684      333737 :     GEN d = gcdii(o,mod);
    2685      333737 :     if (!equalii(o, d))
    2686             :     {
    2687      164165 :       N = diviiexact(o,d); /* > 1, coprime to p */
    2688      164165 :       a = nfpowmodideal(nf, a, N, prk);
    2689      164165 :       oN = d; /* order of g^N mod pr */
    2690             :     }
    2691             :   }
    2692      424611 :   if (equali1(oN))
    2693      118279 :     e = gen_0;
    2694             :   else
    2695             :   {
    2696      306332 :     if (N) { O = order_update(oN, O); gN = Fq_pow(g, N, T, p); }
    2697      306332 :     e = Fq_log(nf_to_Fq(nf,a,modpr), gN, O, T, p);
    2698             :   }
    2699             :   /* 0 <= e < oN is correct modulo oN */
    2700      424611 :   if (sprk_is_prime(sprk)) return mkcol(e); /* k = 1 */
    2701             : 
    2702      135793 :   sprk_get_U2(sprk, &U1,&U2);
    2703      135793 :   cyc = sprk_get_cyc(sprk);
    2704      135793 :   if (mod)
    2705             :   {
    2706      113295 :     cyc = ZV_gcdmod(cyc, mod);
    2707      113295 :     if (signe(remii(mod,p))) return vecmodii(ZC_Z_mul(U1,e), cyc);
    2708             :   }
    2709      134911 :   if (signe(e))
    2710             :   {
    2711       10892 :     GEN E = N? mulii(e, N): e;
    2712       10892 :     a = nfmulpowmodideal(nf, a, sprk_get_gnf(sprk), Fp_neg(E, o), prk);
    2713             :   }
    2714             :   /* a = 1 mod pr */
    2715      134911 :   y = log_prk1(nf, a, lg(U2)-1, sprk_get_L2(sprk), prk);
    2716      134904 :   if (N)
    2717             :   { /* from DL(a^N) to DL(a) */
    2718       42077 :     GEN E = gel(sprk_get_cyc(sprk), 1), q = powiu(p, Z_pval(E, p));
    2719       42077 :     y = ZC_Z_mul(y, Fp_inv(N, q));
    2720             :   }
    2721      134904 :   y = ZC_lincomb(gen_1, e, ZM_ZC_mul(U2,y), U1);
    2722      134904 :   return vecmodii(y, cyc);
    2723             : }
    2724             : GEN
    2725        2821 : log_prk_init(GEN nf, GEN pr, long k)
    2726        2821 : { return sprkinit(checknf(nf),pr,k,NULL);}
    2727             : GEN
    2728         497 : veclog_prk(GEN nf, GEN v, GEN sprk)
    2729             : {
    2730         497 :   long l = lg(v), i;
    2731         497 :   GEN w = cgetg(l, t_MAT);
    2732        1232 :   for (i = 1; i < l; i++) gel(w,i) = log_prk(nf, gel(v,i), sprk, NULL);
    2733         497 :   return w;
    2734             : }
    2735             : 
    2736             : static GEN
    2737      287446 : famat_zlog(GEN nf, GEN fa, GEN sgn, zlog_S *S)
    2738             : {
    2739      287446 :   long i, l0, l = lg(S->U);
    2740      287446 :   GEN g = gel(fa,1), e = gel(fa,2), y = cgetg(l, t_COL);
    2741      287446 :   l0 = lg(S->sprk); /* = l (trivial arch. part), or l-1 */
    2742      625096 :   for (i=1; i < l0; i++) gel(y,i) = famat_zlog_pr(nf, g, e, gel(S->sprk,i), S->mod);
    2743      287446 :   if (l0 != l)
    2744             :   {
    2745      171176 :     if (!sgn) sgn = nfsign_arch(nf, fa, S->archp);
    2746      171176 :     gel(y,l0) = Flc_to_ZC(sgn);
    2747             :   }
    2748      287446 :   return y;
    2749             : }
    2750             : 
    2751             : /* assume that cyclic factors are normalized, in particular != [1] */
    2752             : static GEN
    2753       44492 : split_U(GEN U, GEN Sprk)
    2754             : {
    2755       44492 :   long t = 0, k, n, l = lg(Sprk);
    2756       44492 :   GEN vU = cgetg(l+1, t_VEC);
    2757       88536 :   for (k = 1; k < l; k++)
    2758             :   {
    2759       44044 :     n = lg(sprk_get_cyc(gel(Sprk,k))) - 1; /* > 0 */
    2760       44044 :     gel(vU,k) = vecslice(U, t+1, t+n);
    2761       44044 :     t += n;
    2762             :   }
    2763             :   /* t+1 .. lg(U)-1 */
    2764       44492 :   n = lg(U) - t - 1; /* can be 0 */
    2765       44492 :   if (!n) setlg(vU,l); else gel(vU,l) = vecslice(U, t+1, t+n);
    2766       44492 :   return vU;
    2767             : }
    2768             : 
    2769             : static void
    2770      431450 : init_zlog_mod(zlog_S *S, GEN bid, GEN mod)
    2771             : {
    2772      431450 :   GEN fa2 = bid_get_fact2(bid);
    2773      431450 :   S->U = bid_get_U(bid);
    2774      431450 :   S->hU = lg(bid_get_cyc(bid))-1;
    2775      431450 :   S->archp = bid_get_archp(bid);
    2776      431450 :   S->sprk = bid_get_sprk(bid);
    2777      431450 :   S->bid = bid;
    2778      431450 :   S->mod = mod;
    2779      431450 :   S->P = gel(fa2,1);
    2780      431450 :   S->k = gel(fa2,2);
    2781      431450 :   S->no2 = lg(S->P) == lg(gel(bid_get_fact(bid),1));
    2782      431450 : }
    2783             : void
    2784       99596 : init_zlog(zlog_S *S, GEN bid)
    2785             : {
    2786       99596 :   return init_zlog_mod(S, bid, NULL);
    2787             : }
    2788             : 
    2789             : /* a a t_FRAC/t_INT, reduce mod bid */
    2790             : static GEN
    2791          14 : Q_mod_bid(GEN bid, GEN a)
    2792             : {
    2793          14 :   GEN xZ = gcoeff(bid_get_ideal(bid),1,1);
    2794          14 :   GEN b = Rg_to_Fp(a, xZ);
    2795          14 :   if (gsigne(a) < 0) b = subii(b, xZ);
    2796          14 :   return signe(b)? b: xZ;
    2797             : }
    2798             : /* Return decomposition of a on the CRT generators blocks attached to the
    2799             :  * S->sprk and sarch; sgn = sign(a, S->arch), NULL if unknown */
    2800             : static GEN
    2801       66241 : zlog(GEN nf, GEN a, GEN sgn, zlog_S *S)
    2802             : {
    2803             :   long k, l;
    2804             :   GEN y;
    2805       66241 :   a = nf_to_scalar_or_basis(nf, a);
    2806       66241 :   switch(typ(a))
    2807             :   {
    2808       35105 :     case t_INT: break;
    2809          14 :     case t_FRAC: a = Q_mod_bid(S->bid, a); break;
    2810       31122 :     default: /* case t_COL: */
    2811             :     {
    2812             :       GEN den;
    2813       31122 :       check_nfelt(a, &den);
    2814       31122 :       if (den)
    2815             :       {
    2816          98 :         a = Q_muli_to_int(a, den);
    2817          98 :         a = mkmat2(mkcol2(a, den), mkcol2(gen_1, gen_m1));
    2818          98 :         return famat_zlog(nf, a, sgn, S);
    2819             :       }
    2820             :     }
    2821             :   }
    2822       66143 :   if (sgn)
    2823       60718 :     sgn = (lg(sgn) == 1)? NULL: leafcopy(sgn);
    2824             :   else
    2825        5425 :     sgn = (lg(S->archp) == 1)? NULL: nfsign_arch(nf, a, S->archp);
    2826       66143 :   l = lg(S->sprk);
    2827       66143 :   y = cgetg(sgn? l+1: l, t_COL);
    2828      143885 :   for (k = 1; k < l; k++)
    2829             :   {
    2830       77749 :     GEN sprk = gel(S->sprk,k);
    2831       77749 :     gel(y,k) = log_prk(nf, a, sprk, S->mod);
    2832             :   }
    2833       66136 :   if (sgn) gel(y,l) = Flc_to_ZC(sgn);
    2834       66136 :   return y;
    2835             : }
    2836             : 
    2837             : /* true nf */
    2838             : GEN
    2839       16590 : pr_basis_perm(GEN nf, GEN pr)
    2840             : {
    2841       16590 :   long f = pr_get_f(pr);
    2842             :   GEN perm;
    2843       16590 :   if (f == nf_get_degree(nf)) return identity_perm(f);
    2844       14966 :   perm = cgetg(f+1, t_VECSMALL);
    2845       14966 :   perm[1] = 1;
    2846       14966 :   if (f > 1)
    2847             :   {
    2848        2646 :     GEN H = pr_hnf(nf,pr);
    2849             :     long i, k;
    2850       10304 :     for (i = k = 2; k <= f; i++)
    2851        7658 :       if (!equali1(gcoeff(H,i,i))) perm[k++] = i;
    2852             :   }
    2853       14966 :   return perm;
    2854             : }
    2855             : 
    2856             : /* \sum U[i]*y[i], U[i] ZM, y[i] ZC. We allow lg(y) > lg(U). */
    2857             : static GEN
    2858      353603 : ZMV_ZCV_mul(GEN U, GEN y)
    2859             : {
    2860      353603 :   long i, l = lg(U);
    2861      353603 :   GEN z = NULL;
    2862      353603 :   if (l == 1) return cgetg(1,t_COL);
    2863      985608 :   for (i = 1; i < l; i++)
    2864             :   {
    2865      632005 :     GEN u = ZM_ZC_mul(gel(U,i), gel(y,i));
    2866      632005 :     z = z? ZC_add(z, u): u;
    2867             :   }
    2868      353603 :   return z;
    2869             : }
    2870             : /* A * (U[1], ..., U[d] */
    2871             : static GEN
    2872         518 : ZM_ZMV_mul(GEN A, GEN U)
    2873             : {
    2874             :   long i, l;
    2875         518 :   GEN V = cgetg_copy(U,&l);
    2876        1057 :   for (i = 1; i < l; i++) gel(V,i) = ZM_mul(A,gel(U,i));
    2877         518 :   return V;
    2878             : }
    2879             : 
    2880             : /* a = 1 mod pr, sprk mod pr^e, e >= 1 */
    2881             : static GEN
    2882       49826 : sprk_log_prk1_2(GEN nf, GEN a, GEN sprk)
    2883             : {
    2884       49826 :   GEN U1, U2, y, L2 = sprk_get_L2(sprk);
    2885       49826 :   sprk_get_U2(sprk, &U1,&U2);
    2886       49826 :   y = ZM_ZC_mul(U2, log_prk1(nf, a, lg(U2)-1, L2, sprk_get_prk(sprk)));
    2887       49826 :   return vecmodii(y, sprk_get_cyc(sprk));
    2888             : }
    2889             : /* assume e >= 2 */
    2890             : static GEN
    2891       34454 : sprk_log_gen_pr2(GEN nf, GEN sprk, long e)
    2892             : {
    2893       34454 :   GEN M, G, pr = sprk_get_pr(sprk);
    2894             :   long i, l;
    2895       34454 :   if (e == 2)
    2896             :   {
    2897       18116 :     GEN L2 = sprk_get_L2(sprk), L = gel(L2,1);
    2898       18116 :     G = gel(L,2); l = lg(G);
    2899             :   }
    2900             :   else
    2901             :   {
    2902       16338 :     GEN perm = pr_basis_perm(nf,pr), PI = nfpow_u(nf, pr_get_gen(pr), e-1);
    2903       16338 :     l = lg(perm);
    2904       16338 :     G = cgetg(l, t_VEC);
    2905       16338 :     if (typ(PI) == t_INT)
    2906             :     { /* zk_ei_mul doesn't allow t_INT */
    2907        1617 :       long N = nf_get_degree(nf);
    2908        1617 :       gel(G,1) = addiu(PI,1);
    2909        2135 :       for (i = 2; i < l; i++)
    2910             :       {
    2911         518 :         GEN z = col_ei(N, 1);
    2912         518 :         gel(G,i) = z; gel(z, perm[i]) = PI;
    2913             :       }
    2914             :     }
    2915             :     else
    2916             :     {
    2917       14721 :       gel(G,1) = nfadd(nf, gen_1, PI);
    2918       21182 :       for (i = 2; i < l; i++)
    2919        6461 :         gel(G,i) = nfadd(nf, gen_1, zk_ei_mul(nf, PI, perm[i]));
    2920             :     }
    2921             :   }
    2922       34454 :   M = cgetg(l, t_MAT);
    2923       78379 :   for (i = 1; i < l; i++) gel(M,i) = sprk_log_prk1_2(nf, gel(G,i), sprk);
    2924       34454 :   return M;
    2925             : }
    2926             : /* Log on bid.gen of generators of P_{1,I pr^{e-1}} / P_{1,I pr^e} (I,pr) = 1,
    2927             :  * defined implicitly via CRT. 'ind' is the index of pr in modulus
    2928             :  * factorization */
    2929             : GEN
    2930       94941 : log_gen_pr(zlog_S *S, long ind, GEN nf, long e)
    2931             : {
    2932       94941 :   GEN Uind = gel(S->U, ind);
    2933       94941 :   if (e == 1) retmkmat( gel(Uind,1) );
    2934       32417 :   return ZM_mul(Uind, sprk_log_gen_pr2(nf, gel(S->sprk,ind), e));
    2935             : }
    2936             : GEN
    2937        2037 : sprk_log_gen_pr(GEN nf, GEN sprk, long e)
    2938             : {
    2939        2037 :   if (e == 1)
    2940             :   {
    2941           0 :     long n = lg(sprk_get_cyc(sprk))-1;
    2942           0 :     retmkmat(col_ei(n, 1));
    2943             :   }
    2944        2037 :   return sprk_log_gen_pr2(nf, sprk, e);
    2945             : }
    2946             : /* a = 1 mod pr */
    2947             : GEN
    2948        5901 : sprk_log_prk1(GEN nf, GEN a, GEN sprk)
    2949             : {
    2950        5901 :   if (lg(sprk) == 5) return mkcol(gen_0); /* mod pr */
    2951        5901 :   return sprk_log_prk1_2(nf, a, sprk);
    2952             : }
    2953             : /* Log on bid.gen of generator of P_{1,f} / P_{1,f v[index]}
    2954             :  * v = vector of r1 real places */
    2955             : GEN
    2956       16464 : log_gen_arch(zlog_S *S, long index)
    2957             : {
    2958       16464 :   GEN U = gel(S->U, lg(S->U)-1);
    2959       16464 :   return gel(U, index);
    2960             : }
    2961             : 
    2962             : /* compute bid.clgp: [h,cyc] or [h,cyc,gen] */
    2963             : static GEN
    2964       45563 : bid_grp(GEN nf, GEN U, GEN cyc, GEN g, GEN F, GEN sarch)
    2965             : {
    2966       45563 :   GEN G, h = ZV_prod(cyc);
    2967             :   long c;
    2968       45563 :   if (!U) return mkvec2(h,cyc);
    2969       45297 :   c = lg(U);
    2970       45297 :   G = cgetg(c,t_VEC);
    2971       45297 :   if (c > 1)
    2972             :   {
    2973       38283 :     GEN U0, Uoo, EX = cyc_get_expo(cyc); /* exponent of bid */
    2974       38283 :     long i, hU = nbrows(U), nba = lg(sarch_get_cyc(sarch))-1; /* #f_oo */
    2975       38283 :     if (!nba) { U0 = U; Uoo = NULL; }
    2976       20594 :     else if (nba == hU) { U0 = NULL; Uoo = U; }
    2977             :     else
    2978             :     {
    2979       16807 :       U0 = rowslice(U, 1, hU-nba);
    2980       16807 :       Uoo = rowslice(U, hU-nba+1, hU);
    2981             :     }
    2982      110684 :     for (i = 1; i < c; i++)
    2983             :     {
    2984       72401 :       GEN t = gen_1;
    2985       72401 :       if (U0) t = famat_to_nf_modideal_coprime(nf, g, gel(U0,i), F, EX);
    2986       72401 :       if (Uoo) t = set_sign_mod_divisor(nf, ZV_to_Flv(gel(Uoo,i),2), t, sarch);
    2987       72401 :       gel(G,i) = t;
    2988             :     }
    2989             :   }
    2990       45297 :   return mkvec3(h, cyc, G);
    2991             : }
    2992             : 
    2993             : /* remove prime ideals of norm 2 with exponent 1 from factorization */
    2994             : static GEN
    2995       45262 : famat_strip2(GEN fa)
    2996             : {
    2997       45262 :   GEN P = gel(fa,1), E = gel(fa,2), Q, F;
    2998       45262 :   long l = lg(P), i, j;
    2999       45262 :   Q = cgetg(l, t_COL);
    3000       45262 :   F = cgetg(l, t_COL);
    3001       98147 :   for (i = j = 1; i < l; i++)
    3002             :   {
    3003       52885 :     GEN pr = gel(P,i), e = gel(E,i);
    3004       52885 :     if (!absequaliu(pr_get_p(pr), 2) || itou(e) != 1 || pr_get_f(pr) != 1)
    3005             :     {
    3006       44849 :       gel(Q,j) = pr;
    3007       44849 :       gel(F,j) = e; j++;
    3008             :     }
    3009             :   }
    3010       45262 :   setlg(Q,j);
    3011       45262 :   setlg(F,j); return mkmat2(Q,F);
    3012             : }
    3013             : 
    3014             : /* Compute [[ideal,arch], [h,[cyc],[gen]], idealfact, [liste], U]
    3015             :    flag may include nf_GEN | nf_INIT */
    3016             : static GEN
    3017       45283 : Idealstar_i(GEN nf, GEN ideal, long flag)
    3018             : {
    3019             :   long i, k, nbp, R1;
    3020       45283 :   GEN y, cyc, U, u1 = NULL, fa, fa2, sprk, x, arch, archp, E, P, sarch, gen;
    3021             : 
    3022       45283 :   nf = checknf(nf);
    3023       45283 :   R1 = nf_get_r1(nf);
    3024       45283 :   if (typ(ideal) == t_VEC && lg(ideal) == 3)
    3025             :   {
    3026       22078 :     arch = gel(ideal,2);
    3027       22078 :     ideal= gel(ideal,1);
    3028       44149 :     switch(typ(arch))
    3029             :     {
    3030       21630 :       case t_VEC:
    3031       21630 :         if (lg(arch) != R1+1)
    3032           0 :           pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
    3033       21630 :         archp = vec01_to_indices(arch);
    3034       21630 :         break;
    3035         448 :       case t_VECSMALL:
    3036         448 :         archp = arch;
    3037         448 :         k = lg(archp)-1;
    3038         448 :         if (k && archp[k] > R1)
    3039           7 :           pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
    3040         441 :         arch = indices_to_vec01(archp, R1);
    3041         441 :         break;
    3042           0 :       default:
    3043           0 :         pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
    3044             :         return NULL;/*LCOV_EXCL_LINE*/
    3045             :     }
    3046             :   }
    3047             :   else
    3048             :   {
    3049       23205 :     arch = zerovec(R1);
    3050       23205 :     archp = cgetg(1, t_VECSMALL);
    3051             :   }
    3052       45276 :   if (is_nf_factor(ideal))
    3053             :   {
    3054        2751 :     fa = ideal;
    3055        2751 :     x = factorbackprime(nf, gel(fa,1), gel(fa,2));
    3056             :   }
    3057             :   else
    3058             :   {
    3059       42525 :     fa = idealfactor(nf, ideal);
    3060       42518 :     x = ideal;
    3061             :   }
    3062       45269 :   if (typ(x) != t_MAT) x = idealhnf_shallow(nf, x);
    3063       45269 :   if (lg(x) == 1) pari_err_DOMAIN("Idealstar", "ideal","=",gen_0,x);
    3064       45269 :   if (typ(gcoeff(x,1,1)) != t_INT)
    3065           7 :     pari_err_DOMAIN("Idealstar","denominator(ideal)", "!=",gen_1,x);
    3066       45262 :   sarch = nfarchstar(nf, x, archp);
    3067       45262 :   fa2 = famat_strip2(fa);
    3068       45262 :   P = gel(fa2,1);
    3069       45262 :   E = gel(fa2,2);
    3070       45262 :   nbp = lg(P)-1;
    3071       45262 :   sprk = cgetg(nbp+1,t_VEC);
    3072       45262 :   if (nbp)
    3073             :   {
    3074       34433 :     GEN t = (lg(gel(fa,1))==2)? NULL: x; /* beware fa != fa2 */
    3075       34433 :     cyc = cgetg(nbp+2,t_VEC);
    3076       34433 :     gen = cgetg(nbp+1,t_VEC);
    3077       79282 :     for (i = 1; i <= nbp; i++)
    3078             :     {
    3079       44849 :       GEN L = sprkinit(nf, gel(P,i), itou(gel(E,i)), t);
    3080       44849 :       gel(sprk,i) = L;
    3081       44849 :       gel(cyc,i) = sprk_get_cyc(L);
    3082             :       /* true gens are congruent to those mod x AND positive at archp */
    3083       44849 :       gel(gen,i) = sprk_get_gen(L);
    3084             :     }
    3085       34433 :     gel(cyc,i) = sarch_get_cyc(sarch);
    3086       34433 :     cyc = shallowconcat1(cyc);
    3087       34433 :     gen = shallowconcat1(gen);
    3088       34433 :     cyc = ZV_snf_group(cyc, &U, (flag & nf_GEN)? &u1: NULL);
    3089             :   }
    3090             :   else
    3091             :   {
    3092       10829 :     cyc = sarch_get_cyc(sarch);
    3093       10829 :     gen = cgetg(1,t_VEC);
    3094       10829 :     U = matid(lg(cyc)-1);
    3095       10829 :     if (flag & nf_GEN) u1 = U;
    3096             :   }
    3097       45262 :   y = bid_grp(nf, u1, cyc, gen, x, sarch);
    3098       45262 :   if (!(flag & nf_INIT)) return y;
    3099       44450 :   U = split_U(U, sprk);
    3100       44450 :   return mkvec5(mkvec2(x, arch), y, mkvec2(fa,fa2), mkvec2(sprk, sarch), U);
    3101             : }
    3102             : GEN
    3103       45017 : Idealstar(GEN nf, GEN ideal, long flag)
    3104             : {
    3105       45017 :   pari_sp av = avma;
    3106       45017 :   if (!nf) nf = nfinit(pol_x(0), DEFAULTPREC);
    3107       45017 :   return gerepilecopy(av, Idealstar_i(nf, ideal, flag));
    3108             : }
    3109             : GEN
    3110         266 : Idealstarprk(GEN nf, GEN pr, long k, long flag)
    3111             : {
    3112         266 :   pari_sp av = avma;
    3113         266 :   GEN z = Idealstar_i(nf, mkmat2(mkcol(pr),mkcols(k)), flag);
    3114         266 :   return gerepilecopy(av, z);
    3115             : }
    3116             : 
    3117             : /* FIXME: obsolete */
    3118             : GEN
    3119           0 : zidealstarinitgen(GEN nf, GEN ideal)
    3120           0 : { return Idealstar(nf,ideal, nf_INIT|nf_GEN); }
    3121             : GEN
    3122           0 : zidealstarinit(GEN nf, GEN ideal)
    3123           0 : { return Idealstar(nf,ideal, nf_INIT); }
    3124             : GEN
    3125           0 : zidealstar(GEN nf, GEN ideal)
    3126           0 : { return Idealstar(nf,ideal, nf_GEN); }
    3127             : 
    3128             : GEN
    3129          70 : idealstar0(GEN nf, GEN ideal,long flag)
    3130             : {
    3131          70 :   switch(flag)
    3132             :   {
    3133           0 :     case 0: return Idealstar(nf,ideal, nf_GEN);
    3134          56 :     case 1: return Idealstar(nf,ideal, nf_INIT);
    3135          14 :     case 2: return Idealstar(nf,ideal, nf_INIT|nf_GEN);
    3136           0 :     default: pari_err_FLAG("idealstar");
    3137             :   }
    3138             :   return NULL; /* LCOV_EXCL_LINE */
    3139             : }
    3140             : 
    3141             : void
    3142       31122 : check_nfelt(GEN x, GEN *den)
    3143             : {
    3144       31122 :   long l = lg(x), i;
    3145       31122 :   GEN t, d = NULL;
    3146       31122 :   if (typ(x) != t_COL) pari_err_TYPE("check_nfelt", x);
    3147      112476 :   for (i=1; i<l; i++)
    3148             :   {
    3149       81354 :     t = gel(x,i);
    3150       81354 :     switch (typ(t))
    3151             :     {
    3152       81151 :       case t_INT: break;
    3153         203 :       case t_FRAC:
    3154         203 :         if (!d) d = gel(t,2); else d = lcmii(d, gel(t,2));
    3155         203 :         break;
    3156           0 :       default: pari_err_TYPE("check_nfelt", x);
    3157             :     }
    3158             :   }
    3159       31122 :   *den = d;
    3160       31122 : }
    3161             : 
    3162             : GEN
    3163     1700913 : vecmodii(GEN x, GEN y)
    3164     4790621 : { pari_APPLY_same(modii(gel(x,i), gel(y,i))) }
    3165             : GEN
    3166      142731 : ZV_gcdmod(GEN x, GEN mod)
    3167      421754 : { pari_APPLY_same(gcdii(gel(x,i), mod)); }
    3168             : 
    3169             : GEN
    3170       27230 : vecmoduu(GEN x, GEN y)
    3171       88158 : { pari_APPLY_ulong(uel(x,i) % uel(y,i)) }
    3172             : 
    3173             : /* assume a true bnf and bid */
    3174             : GEN
    3175       37233 : ideallog_units0(GEN bnf, GEN bid, GEN MOD)
    3176             : {
    3177       37233 :   GEN nf = bnf_get_nf(bnf), D, y, C, cyc;
    3178       37233 :   long j, lU = lg(bnf_get_logfu(bnf)); /* r1+r2 */
    3179             :   zlog_S S;
    3180       37233 :   init_zlog_mod(&S, bid, MOD);
    3181       37233 :   if (!S.hU) return zeromat(0,lU);
    3182       37233 :   cyc = bid_get_cyc(bid);
    3183       37233 :   if (MOD) cyc = ZV_gcdmod(cyc, MOD);
    3184       37233 :   D = nfsign_fu(bnf, bid_get_archp(bid));
    3185       37233 :   y = cgetg(lU, t_MAT);
    3186       37233 :   if ((C = bnf_build_cheapfu(bnf)))
    3187       60697 :   { for (j = 1; j < lU; j++) gel(y,j) = zlog(nf, gel(C,j), gel(D,j), &S); }
    3188             :   else
    3189             :   {
    3190          21 :     long i, l = lg(S.U), l0 = lg(S.sprk);
    3191             :     GEN X, U;
    3192          21 :     if (!(C = bnf_compactfu_mat(bnf))) bnf_build_units(bnf); /* error */
    3193          21 :     X = gel(C,1); U = gel(C,2);
    3194          42 :     for (j = 1; j < lU; j++) gel(y,j) = cgetg(l, t_COL);
    3195          42 :     for (i = 1; i < l0; i++)
    3196             :     {
    3197          21 :       GEN sprk = gel(S.sprk, i);
    3198          21 :       GEN Xi = sunits_makecoprime(X, sprk_get_pr(sprk), sprk_get_prk(sprk));
    3199          42 :       for (j = 1; j < lU; j++)
    3200          21 :         gcoeff(y,i,j) = famat_zlog_pr_coprime(nf, Xi, gel(U,j), sprk, MOD);
    3201             :     }
    3202          21 :     if (l0 != l)
    3203          14 :       for (j = 1; j < lU; j++) gcoeff(y,l0,j) = Flc_to_ZC(gel(D,j));
    3204             :   }
    3205       37233 :   y = vec_prepend(y, zlog(nf, bnf_get_tuU(bnf), nfsign_tu(bnf, S.archp), &S));
    3206       97972 :   for (j = 1; j <= lU; j++)
    3207       60739 :     gel(y,j) = vecmodii(ZMV_ZCV_mul(S.U, gel(y,j)), cyc);
    3208       37233 :   return y;
    3209             : }
    3210             : GEN
    3211          84 : ideallog_units(GEN bnf, GEN bid)
    3212          84 : { return ideallog_units0(bnf, bid, NULL); }
    3213             : GEN
    3214         518 : log_prk_units(GEN nf, GEN D, GEN sprk)
    3215             : {
    3216         518 :   GEN L, Ltu = log_prk(nf, gel(D,1), sprk, NULL);
    3217         518 :   D = gel(D,2);
    3218         518 :   if (lg(D) != 3 || typ(gel(D,2)) != t_MAT) L = veclog_prk(nf, D, sprk);
    3219             :   else
    3220             :   {
    3221          21 :     GEN X = gel(D,1), U = gel(D,2);
    3222          21 :     long j, lU = lg(U);
    3223          21 :     X = sunits_makecoprime(X, sprk_get_pr(sprk), sprk_get_prk(sprk));
    3224          21 :     L = cgetg(lU, t_MAT);
    3225          63 :     for (j = 1; j < lU; j++)
    3226          42 :       gel(L,j) = famat_zlog_pr_coprime(nf, X, gel(U,j), sprk, NULL);
    3227             :   }
    3228         518 :   return vec_prepend(L, Ltu);
    3229             : }
    3230             : 
    3231             : static GEN
    3232      294621 : ideallog_i(GEN nf, GEN x, zlog_S *S)
    3233             : {
    3234      294621 :   pari_sp av = avma;
    3235             :   GEN y;
    3236      294621 :   if (!S->hU) return cgetg(1, t_COL);
    3237      292871 :   if (typ(x) == t_MAT)
    3238      287348 :     y = famat_zlog(nf, x, NULL, S);
    3239             :   else
    3240        5523 :     y = zlog(nf, x, NULL, S);
    3241      292864 :   y = ZMV_ZCV_mul(S->U, y);
    3242      292864 :   return gerepileupto(av, vecmodii(y, bid_get_cyc(S->bid)));
    3243             : }
    3244             : GEN
    3245      301299 : ideallogmod(GEN nf, GEN x, GEN bid, GEN mod)
    3246             : {
    3247             :   zlog_S S;
    3248      301299 :   if (!nf)
    3249             :   {
    3250        6671 :     if (mod) pari_err_IMPL("Zideallogmod");
    3251        6671 :     return Zideallog(bid, x);
    3252             :   }
    3253      294628 :   checkbid(bid); init_zlog_mod(&S, bid, mod);
    3254      294621 :   return ideallog_i(checknf(nf), x, &S);
    3255             : }
    3256             : GEN
    3257       13230 : ideallog(GEN nf, GEN x, GEN bid)
    3258             : {
    3259       13230 :   return ideallogmod(nf, x, bid, NULL);
    3260             : }
    3261             : 
    3262             : /*************************************************************************/
    3263             : /**                                                                     **/
    3264             : /**               JOIN BID STRUCTURES, IDEAL LISTS                      **/
    3265             : /**                                                                     **/
    3266             : /*************************************************************************/
    3267             : /* bid1, bid2: for coprime modules m1 and m2 (without arch. part).
    3268             :  * Output: bid for m1 m2 */
    3269             : static GEN
    3270         462 : join_bid(GEN nf, GEN bid1, GEN bid2)
    3271             : {
    3272         462 :   pari_sp av = avma;
    3273             :   long nbgen, l1,l2;
    3274             :   GEN I1,I2, G1,G2, sprk1,sprk2, cyc1,cyc2, sarch;
    3275         462 :   GEN sprk, fa,fa2, U, cyc, y, u1 = NULL, x, gen;
    3276             : 
    3277         462 :   I1 = bid_get_ideal(bid1);
    3278         462 :   I2 = bid_get_ideal(bid2);
    3279         462 :   if (gequal1(gcoeff(I1,1,1))) return bid2; /* frequent trivial case */
    3280         259 :   G1 = bid_get_grp(bid1);
    3281         259 :   G2 = bid_get_grp(bid2);
    3282         259 :   x = idealmul(nf, I1,I2);
    3283         259 :   fa = famat_mul_shallow(bid_get_fact(bid1), bid_get_fact(bid2));
    3284         259 :   fa2= famat_mul_shallow(bid_get_fact2(bid1), bid_get_fact2(bid2));
    3285         259 :   sprk1 = bid_get_sprk(bid1);
    3286         259 :   sprk2 = bid_get_sprk(bid2);
    3287         259 :   sprk = shallowconcat(sprk1, sprk2);
    3288             : 
    3289         259 :   cyc1 = abgrp_get_cyc(G1); l1 = lg(cyc1);
    3290         259 :   cyc2 = abgrp_get_cyc(G2); l2 = lg(cyc2);
    3291         259 :   gen = (lg(G1)>3 && lg(G2)>3)? gen_1: NULL;
    3292         259 :   nbgen = l1+l2-2;
    3293         259 :   cyc = ZV_snf_group(shallowconcat(cyc1,cyc2), &U, gen? &u1: NULL);
    3294         259 :   if (nbgen)
    3295             :   {
    3296         259 :     GEN U1 = bid_get_U(bid1), U2 = bid_get_U(bid2);
    3297           0 :     U1 = l1==1? const_vec(lg(sprk1), cgetg(1,t_MAT))
    3298         259 :               : ZM_ZMV_mul(vecslice(U, 1, l1-1),   U1);
    3299           0 :     U2 = l2==1? const_vec(lg(sprk2), cgetg(1,t_MAT))
    3300         259 :               : ZM_ZMV_mul(vecslice(U, l1, nbgen), U2);
    3301         259 :     U = shallowconcat(U1, U2);
    3302             :   }
    3303             :   else
    3304           0 :     U = const_vec(lg(sprk), cgetg(1,t_MAT));
    3305             : 
    3306         259 :   if (gen)
    3307             :   {
    3308         259 :     GEN uv = zkchinese1init2(nf, I2, I1, x);
    3309         518 :     gen = shallowconcat(zkVchinese1(gel(uv,1), abgrp_get_gen(G1)),
    3310         259 :                         zkVchinese1(gel(uv,2), abgrp_get_gen(G2)));
    3311             :   }
    3312         259 :   sarch = bid_get_sarch(bid1); /* trivial */
    3313         259 :   y = bid_grp(nf, u1, cyc, gen, x, sarch);
    3314         259 :   x = mkvec2(x, bid_get_arch(bid1));
    3315         259 :   y = mkvec5(x, y, mkvec2(fa, fa2), mkvec2(sprk, sarch), U);
    3316         259 :   return gerepilecopy(av,y);
    3317             : }
    3318             : 
    3319             : typedef struct _ideal_data {
    3320             :   GEN nf, emb, L, pr, prL, sgnU, archp;
    3321             : } ideal_data;
    3322             : 
    3323             : /* z <- ( z | f(v[i])_{i=1..#v} ) */
    3324             : static void
    3325      119245 : concat_join(GEN *pz, GEN v, GEN (*f)(ideal_data*,GEN), ideal_data *data)
    3326             : {
    3327      119245 :   long i, nz, lv = lg(v);
    3328             :   GEN z, Z;
    3329      119245 :   if (lv == 1) return;
    3330       52535 :   z = *pz; nz = lg(z)-1;
    3331       52535 :   *pz = Z = cgetg(lv + nz, typ(z));
    3332       86863 :   for (i = 1; i <=nz; i++) gel(Z,i) = gel(z,i);
    3333       52535 :   Z += nz;
    3334      113036 :   for (i = 1; i < lv; i++) gel(Z,i) = f(data, gel(v,i));
    3335             : }
    3336             : static GEN
    3337         462 : join_idealinit(ideal_data *D, GEN x)
    3338         462 : { return join_bid(D->nf, x, D->prL); }
    3339             : static GEN
    3340       60039 : join_ideal(ideal_data *D, GEN x)
    3341       60039 : { return idealmulpowprime(D->nf, x, D->pr, D->L); }
    3342             : static GEN
    3343         441 : join_unit(ideal_data *D, GEN x)
    3344             : {
    3345         441 :   GEN bid = join_idealinit(D, gel(x,1)), u = gel(x,2), v = mkvec(D->emb);
    3346         441 :   if (lg(u) != 1) v = shallowconcat(u, v);
    3347         441 :   return mkvec2(bid, v);
    3348             : }
    3349             : 
    3350             : GEN
    3351          42 : log_prk_units_init(GEN bnf)
    3352             : {
    3353          42 :   GEN U = bnf_has_fu(bnf);
    3354          42 :   if (U) U = matalgtobasis(bnf_get_nf(bnf), U);
    3355          14 :   else if (!(U = bnf_compactfu_mat(bnf))) (void)bnf_build_units(bnf);
    3356          42 :   return mkvec2(bnf_get_tuU(bnf), U);
    3357             : }
    3358             : /*  flag & nf_GEN : generators, otherwise no
    3359             :  *  flag &2 : units, otherwise no
    3360             :  *  flag &4 : ideals in HNF, otherwise bid
    3361             :  *  flag &8 : omit ideals which cannot be conductors (pr^1 with Npr=2) */
    3362             : static GEN
    3363        6034 : Ideallist(GEN bnf, ulong bound, long flag)
    3364             : {
    3365        6034 :   const long cond = flag & 8;
    3366        6034 :   const long do_units = flag & 2, big_id = !(flag & 4);
    3367        6034 :   const long istar_flag = (flag & nf_GEN) | nf_INIT;
    3368        6034 :   pari_sp av, av0 = avma;
    3369             :   long i, j;
    3370        6034 :   GEN nf, z, p, fa, id, BOUND, U, empty = cgetg(1,t_VEC);
    3371             :   forprime_t S;
    3372             :   ideal_data ID;
    3373        6034 :   GEN (*join_z)(ideal_data*, GEN) =
    3374             :     do_units? &join_unit
    3375        6034 :               : (big_id? &join_idealinit: &join_ideal);
    3376             : 
    3377        6034 :   nf = checknf(bnf);
    3378        6034 :   if ((long)bound <= 0) return empty;
    3379        6034 :   id = matid(nf_get_degree(nf));
    3380        6034 :   if (big_id) id = Idealstar(nf,id, istar_flag);
    3381             : 
    3382             :   /* z[i] will contain all "objects" of norm i. Depending on flag, this means
    3383             :    * an ideal, a bid, or a couple [bid, log(units)]. Such objects are stored
    3384             :    * in vectors, computed one primary component at a time; join_z
    3385             :    * reconstructs the global object */
    3386        6034 :   BOUND = utoipos(bound);
    3387        6034 :   z = const_vec(bound, empty);
    3388        6034 :   U = do_units? log_prk_units_init(bnf): NULL;
    3389        6034 :   gel(z,1) = mkvec(U? mkvec2(id, cgetg(1,t_VEC)): id);
    3390        6034 :   ID.nf = nf;
    3391             : 
    3392        6034 :   p = cgetipos(3);
    3393        6034 :   u_forprime_init(&S, 2, bound);
    3394        6034 :   av = avma;
    3395       27440 :   while ((p[2] = u_forprime_next(&S)))
    3396             :   {
    3397       21406 :     if (DEBUGLEVEL>1) err_printf("%ld ",p[2]);
    3398       21406 :     fa = idealprimedec_limit_norm(nf, p, BOUND);
    3399       43638 :     for (j = 1; j < lg(fa); j++)
    3400             :     {
    3401       22232 :       GEN pr = gel(fa,j), z2 = leafcopy(z);
    3402       22232 :       ulong Q, q = upr_norm(pr);
    3403             :       long l;
    3404       22232 :       ID.pr = ID.prL = pr;
    3405       22232 :       if (cond && q == 2) { l = 2; Q = 4; } else { l = 1; Q = q; }
    3406       53781 :       for (; Q <= bound; l++, Q *= q) /* add pr^l */
    3407             :       {
    3408             :         ulong iQ;
    3409       31549 :         ID.L = utoipos(l);
    3410       31549 :         if (big_id) {
    3411         203 :           ID.prL = Idealstarprk(nf, pr, l, istar_flag);
    3412         203 :           if (U)
    3413         182 :             ID.emb = Q == 2? cgetg(1,t_VEC)
    3414         182 :                            : log_prk_units(nf, U, gel(bid_get_sprk(ID.prL),1));
    3415             :         }
    3416      150794 :         for (iQ = Q,i = 1; iQ <= bound; iQ += Q,i++)
    3417      119245 :           concat_join(&gel(z,iQ), gel(z2,i), join_z, &ID);
    3418             :       }
    3419             :     }
    3420       21406 :     if (gc_needed(av,1))
    3421             :     {
    3422           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"Ideallist");
    3423           0 :       z = gerepilecopy(av, z);
    3424             :     }
    3425             :   }
    3426        6034 :   return gerepilecopy(av0, z);
    3427             : }
    3428             : GEN
    3429         350 : ideallist0(GEN bnf,long bound, long flag) {
    3430         350 :   if (flag<0 || flag>15) pari_err_FLAG("ideallist");
    3431         350 :   return Ideallist(bnf,bound,flag);
    3432             : }
    3433             : GEN
    3434        5684 : ideallist(GEN bnf,long bound) { return Ideallist(bnf,bound,4); }
    3435             : 
    3436             : /* bid = for module m (without arch. part), arch = archimedean part.
    3437             :  * Output: bid for [m,arch] */
    3438             : static GEN
    3439          42 : join_bid_arch(GEN nf, GEN bid, GEN archp)
    3440             : {
    3441          42 :   pari_sp av = avma;
    3442             :   GEN G, U;
    3443          42 :   GEN sprk, cyc, y, u1 = NULL, x, sarch, gen;
    3444             : 
    3445          42 :   checkbid(bid);
    3446          42 :   G = bid_get_grp(bid);
    3447          42 :   x = bid_get_ideal(bid);
    3448          42 :   sarch = nfarchstar(nf, bid_get_ideal(bid), archp);
    3449          42 :   sprk = bid_get_sprk(bid);
    3450             : 
    3451          42 :   gen = (lg(G)>3)? gel(G,3): NULL;
    3452          42 :   cyc = diagonal_shallow(shallowconcat(gel(G,2), sarch_get_cyc(sarch)));
    3453          42 :   cyc = ZM_snf_group(cyc, &U, gen? &u1: NULL);
    3454          42 :   y = bid_grp(nf, u1, cyc, gen, x, sarch);
    3455          42 :   U = split_U(U, sprk);
    3456          42 :   y = mkvec5(mkvec2(x, archp), y, gel(bid,3), mkvec2(sprk, sarch), U);
    3457          42 :   return gerepilecopy(av,y);
    3458             : }
    3459             : static GEN
    3460          42 : join_arch(ideal_data *D, GEN x) {
    3461          42 :   return join_bid_arch(D->nf, x, D->archp);
    3462             : }
    3463             : static GEN
    3464          14 : join_archunit(ideal_data *D, GEN x) {
    3465          14 :   GEN bid = join_arch(D, gel(x,1)), u = gel(x,2), v = mkvec(D->emb);
    3466          14 :   if (lg(u) != 1) v = shallowconcat(u, v);
    3467          14 :   return mkvec2(bid, v);
    3468             : }
    3469             : 
    3470             : /* L from ideallist, add archimedean part */
    3471             : GEN
    3472          14 : ideallistarch(GEN bnf, GEN L, GEN arch)
    3473             : {
    3474             :   pari_sp av;
    3475          14 :   long i, j, l = lg(L), lz;
    3476             :   GEN v, z, V;
    3477             :   ideal_data ID;
    3478             :   GEN (*join_z)(ideal_data*, GEN);
    3479             : 
    3480          14 :   if (typ(L) != t_VEC) pari_err_TYPE("ideallistarch",L);
    3481          14 :   if (l == 1) return cgetg(1,t_VEC);
    3482          14 :   z = gel(L,1);
    3483          14 :   if (typ(z) != t_VEC) pari_err_TYPE("ideallistarch",z);
    3484          14 :   z = gel(z,1); /* either a bid or [bid,U] */
    3485          14 :   ID.nf = checknf(bnf);
    3486          14 :   ID.archp = vec01_to_indices(arch);
    3487          14 :   if (lg(z) == 3) { /* the latter: do units */
    3488           7 :     if (typ(z) != t_VEC) pari_err_TYPE("ideallistarch",z);
    3489           7 :     ID.emb = zm_to_ZM( rowpermute(nfsign_units(bnf,NULL,1), ID.archp) );
    3490           7 :     join_z = &join_archunit;
    3491             :   } else
    3492           7 :     join_z = &join_arch;
    3493          14 :   av = avma; V = cgetg(l, t_VEC);
    3494          63 :   for (i = 1; i < l; i++)
    3495             :   {
    3496          49 :     z = gel(L,i); lz = lg(z);
    3497          49 :     gel(V,i) = v = cgetg(lz,t_VEC);
    3498          91 :     for (j=1; j<lz; j++) gel(v,j) = join_z(&ID, gel(z,j));
    3499             :   }
    3500          14 :   return gerepilecopy(av,V);
    3501             : }

Generated by: LCOV version 1.13