Code coverage tests

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

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

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

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

Generated by: LCOV version 1.11