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 - base4.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.8.0 lcov report (development 19361-ac4f238) Lines: 1406 1549 90.8 %
Date: 2016-08-28 06:11:39 Functions: 127 138 92.0 %
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             : /*                           (continued)                           */
      18             : /*                                                                 */
      19             : /*******************************************************************/
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : 
      23             : /*******************************************************************/
      24             : /*                                                                 */
      25             : /*                     IDEAL OPERATIONS                            */
      26             : /*                                                                 */
      27             : /*******************************************************************/
      28             : 
      29             : /* A valid ideal is either principal (valid nf_element), or prime, or a matrix
      30             :  * on the integer basis in HNF.
      31             :  * A prime ideal is of the form [p,a,e,f,b], where the ideal is p.Z_K+a.Z_K,
      32             :  * p is a rational prime, a belongs to Z_K, e=e(P/p), f=f(P/p), and b
      33             :  * is Lenstra's constant, such that p.P^(-1)= p Z_K + b Z_K.
      34             :  *
      35             :  * An extended ideal is a couple [I,F] where I is a valid ideal and F is
      36             :  * either an algebraic number, or a factorization matrix attached to an
      37             :  * algebraic number. All routines work with either extended ideals or ideals
      38             :  * (an omitted F is assumed to be [;] <-> 1).
      39             :  * All ideals are output in HNF form. */
      40             : 
      41             : /* types and conversions */
      42             : 
      43             : long
      44     3081737 : idealtyp(GEN *ideal, GEN *arch)
      45             : {
      46     3081737 :   GEN x = *ideal;
      47     3081737 :   long t,lx,tx = typ(x);
      48             : 
      49     3081737 :   if (tx==t_VEC && lg(x)==3)
      50      328280 :   { *arch = gel(x,2); x = gel(x,1); tx = typ(x); }
      51             :   else
      52     2753457 :     *arch = NULL;
      53     3081737 :   switch(tx)
      54             :   {
      55     1354949 :     case t_MAT: lx = lg(x);
      56     1354949 :       if (lx == 1) { t = id_PRINCIPAL; x = gen_0; break; }
      57     1354872 :       if (lx != lgcols(x)) pari_err_TYPE("idealtyp [non-square t_MAT]",x);
      58     1354865 :       t = id_MAT;
      59     1354865 :       break;
      60             : 
      61     1491438 :     case t_VEC: if (lg(x)!=6) pari_err_TYPE("idealtyp",x);
      62     1491424 :       t = id_PRIME; break;
      63             : 
      64             :     case t_POL: case t_POLMOD: case t_COL:
      65             :     case t_INT: case t_FRAC:
      66      235350 :       t = id_PRINCIPAL; break;
      67             :     default:
      68           0 :       pari_err_TYPE("idealtyp",x);
      69           0 :       return 0; /*not reached*/
      70             :   }
      71     3081716 :   *ideal = x; return t;
      72             : }
      73             : 
      74             : /* nf a true nf; v = [a,x,...], a in Z. Return (a,x) */
      75             : GEN
      76     1894119 : idealhnf_two(GEN nf, GEN v)
      77             : {
      78     1894119 :   GEN p = gel(v,1), pi = gel(v,2), m = zk_scalar_or_multable(nf, pi);
      79     1894119 :   if (typ(m) == t_INT) return scalarmat(gcdii(m,p), nf_get_degree(nf));
      80     1512205 :   return ZM_hnfmodid(m, p);
      81             : }
      82             : 
      83             : static GEN
      84       35495 : ZM_Q_mul(GEN x, GEN y)
      85       35495 : { return typ(y) == t_INT? ZM_Z_mul(x,y): RgM_Rg_mul(x,y); }
      86             : 
      87             : 
      88             : GEN
      89      183636 : idealhnf_principal(GEN nf, GEN x)
      90             : {
      91             :   GEN cx;
      92      183636 :   x = nf_to_scalar_or_basis(nf, x);
      93      183636 :   switch(typ(x))
      94             :   {
      95      108377 :     case t_COL: break;
      96       66180 :     case t_INT:  if (!signe(x)) return cgetg(1,t_MAT);
      97       66075 :       return scalarmat(absi(x), nf_get_degree(nf));
      98             :     case t_FRAC:
      99        9079 :       return scalarmat(Q_abs_shallow(x), nf_get_degree(nf));
     100           0 :     default: pari_err_TYPE("idealhnf",x);
     101             :   }
     102      108377 :   x = Q_primitive_part(x, &cx);
     103      108377 :   RgV_check_ZV(x, "idealhnf");
     104      108370 :   x = zk_multable(nf, x);
     105      108370 :   x = ZM_hnfmodid(x, zkmultable_capZ(x));
     106      108370 :   return cx? ZM_Q_mul(x,cx): x;
     107             : }
     108             : 
     109             : /* x integral ideal in t_MAT form, nx columns */
     110             : static GEN
     111           7 : vec_mulid(GEN nf, GEN x, long nx, long N)
     112             : {
     113           7 :   GEN m = cgetg(nx*N + 1, t_MAT);
     114             :   long i, j, k;
     115          21 :   for (i=k=1; i<=nx; i++)
     116          14 :     for (j=1; j<=N; j++) gel(m, k++) = zk_ei_mul(nf, gel(x,i),j);
     117           7 :   return m;
     118             : }
     119             : GEN
     120      449257 : idealhnf_shallow(GEN nf, GEN x)
     121             : {
     122      449257 :   long tx = typ(x), lx = lg(x), N;
     123             : 
     124             :   /* cannot use idealtyp because here we allow non-square matrices */
     125      449257 :   if (tx == t_VEC && lx == 3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
     126      449257 :   if (tx == t_VEC && lx == 6) return idealhnf_two(nf,x); /* PRIME */
     127      146557 :   switch(tx)
     128             :   {
     129             :     case t_MAT:
     130             :     {
     131             :       GEN cx;
     132        9737 :       long nx = lx-1;
     133        9737 :       N = nf_get_degree(nf);
     134        9737 :       if (nx == 0) return cgetg(1, t_MAT);
     135        9716 :       if (nbrows(x) != N) pari_err_TYPE("idealhnf [wrong dimension]",x);
     136        9709 :       if (nx == 1) return idealhnf_principal(nf, gel(x,1));
     137             : 
     138        8253 :       if (nx == N && RgM_is_ZM(x) && ZM_ishnf(x)) return x;
     139         980 :       x = Q_primitive_part(x, &cx);
     140         980 :       if (nx < N) x = vec_mulid(nf, x, nx, N);
     141         980 :       x = ZM_hnfmod(x, ZM_detmult(x));
     142         980 :       return cx? ZM_Q_mul(x,cx): x;
     143             :     }
     144             :     case t_QFI:
     145             :     case t_QFR:
     146             :     {
     147          14 :       pari_sp av = avma;
     148          14 :       GEN u, D = nf_get_disc(nf), T = nf_get_pol(nf), f = nf_get_index(nf);
     149          14 :       GEN A = gel(x,1), B = gel(x,2);
     150          14 :       N = nf_get_degree(nf);
     151          14 :       if (N != 2)
     152           0 :         pari_err_TYPE("idealhnf [Qfb for non-quadratic fields]", x);
     153          14 :       if (!equalii(qfb_disc(x), D))
     154           7 :         pari_err_DOMAIN("idealhnf [Qfb]", "disc(q)", "!=", D, x);
     155             :       /* x -> A Z + (-B + sqrt(D)) / 2 Z
     156             :          K = Q[t]/T(t), t^2 + ut + v = 0,  u^2 - 4v = Df^2
     157             :          => t = (-u + sqrt(D) f)/2
     158             :          => sqrt(D)/2 = (t + u/2)/f */
     159           7 :       u = gel(T,3);
     160           7 :       B = deg1pol_shallow(ginv(f),
     161             :                           gsub(gdiv(u, shifti(f,1)), gdiv(B,gen_2)),
     162           7 :                           varn(T));
     163           7 :       return gerepileupto(av, idealhnf_two(nf, mkvec2(A,B)));
     164             :     }
     165      136806 :     default: return idealhnf_principal(nf, x); /* PRINCIPAL */
     166             :   }
     167             : }
     168             : GEN
     169        2240 : idealhnf(GEN nf, GEN x)
     170             : {
     171        2240 :   pari_sp av = avma;
     172        2240 :   GEN y = idealhnf_shallow(checknf(nf), x);
     173        2226 :   return (avma == av)? gcopy(y): gerepileupto(av, y);
     174             : }
     175             : 
     176             : /* GP functions */
     177             : 
     178             : GEN
     179          63 : idealtwoelt0(GEN nf, GEN x, GEN a)
     180             : {
     181          63 :   if (!a) return idealtwoelt(nf,x);
     182          42 :   return idealtwoelt2(nf,x,a);
     183             : }
     184             : 
     185             : GEN
     186          42 : idealpow0(GEN nf, GEN x, GEN n, long flag)
     187             : {
     188          42 :   if (flag) return idealpowred(nf,x,n);
     189          35 :   return idealpow(nf,x,n);
     190             : }
     191             : 
     192             : GEN
     193          28 : idealmul0(GEN nf, GEN x, GEN y, long flag)
     194             : {
     195          28 :   if (flag) return idealmulred(nf,x,y);
     196          21 :   return idealmul(nf,x,y);
     197             : }
     198             : 
     199             : GEN
     200          42 : idealdiv0(GEN nf, GEN x, GEN y, long flag)
     201             : {
     202          42 :   switch(flag)
     203             :   {
     204          21 :     case 0: return idealdiv(nf,x,y);
     205          21 :     case 1: return idealdivexact(nf,x,y);
     206           0 :     default: pari_err_FLAG("idealdiv");
     207             :   }
     208           0 :   return NULL; /* not reached */
     209             : }
     210             : 
     211             : GEN
     212          70 : idealaddtoone0(GEN nf, GEN arg1, GEN arg2)
     213             : {
     214          70 :   if (!arg2) return idealaddmultoone(nf,arg1);
     215          35 :   return idealaddtoone(nf,arg1,arg2);
     216             : }
     217             : 
     218             : /* b not a scalar */
     219             : static GEN
     220          28 : hnf_Z_ZC(GEN nf, GEN a, GEN b) { return hnfmodid(zk_multable(nf,b), a); }
     221             : /* b not a scalar */
     222             : static GEN
     223          21 : hnf_Z_QC(GEN nf, GEN a, GEN b)
     224             : {
     225             :   GEN db;
     226          21 :   b = Q_remove_denom(b, &db);
     227          21 :   if (db) a = mulii(a, db);
     228          21 :   b = hnf_Z_ZC(nf,a,b);
     229          21 :   return db? RgM_Rg_div(b, db): b;
     230             : }
     231             : /* b not a scalar (not point in trying to optimize for this case) */
     232             : static GEN
     233          28 : hnf_Q_QC(GEN nf, GEN a, GEN b)
     234             : {
     235             :   GEN da, db;
     236          28 :   if (typ(a) == t_INT) return hnf_Z_QC(nf, a, b);
     237           7 :   da = gel(a,2);
     238           7 :   a = gel(a,1);
     239           7 :   b = Q_remove_denom(b, &db);
     240             :   /* write da = d*A, db = d*B, gcd(A,B) = 1
     241             :    * gcd(a/(d A), b/(d B)) = gcd(a B, A b) / A B d = gcd(a B, b) / A B d */
     242           7 :   if (db)
     243             :   {
     244           7 :     GEN d = gcdii(da,db);
     245           7 :     if (!is_pm1(d)) db = diviiexact(db,d); /* B */
     246           7 :     if (!is_pm1(db))
     247             :     {
     248           7 :       a = mulii(a, db); /* a B */
     249           7 :       da = mulii(da, db); /* A B d = lcm(denom(a),denom(b)) */
     250             :     }
     251             :   }
     252           7 :   return RgM_Rg_div(hnf_Z_ZC(nf,a,b), da);
     253             : }
     254             : static GEN
     255           7 : hnf_QC_QC(GEN nf, GEN a, GEN b)
     256             : {
     257             :   GEN da, db, d, x;
     258           7 :   a = Q_remove_denom(a, &da);
     259           7 :   b = Q_remove_denom(b, &db);
     260           7 :   if (da) b = ZC_Z_mul(b, da);
     261           7 :   if (db) a = ZC_Z_mul(a, db);
     262           7 :   d = mul_denom(da, db);
     263           7 :   a = zk_multable(nf,a); da = zkmultable_capZ(a);
     264           7 :   b = zk_multable(nf,b); db = zkmultable_capZ(b);
     265           7 :   x = ZM_hnfmodid(shallowconcat(a,b), gcdii(da,db));
     266           7 :   return d? RgM_Rg_div(x, d): x;
     267             : }
     268             : static GEN
     269          21 : hnf_Q_Q(GEN nf, GEN a, GEN b) {return scalarmat(Q_gcd(a,b), nf_get_degree(nf));}
     270             : GEN
     271         119 : idealhnf0(GEN nf, GEN a, GEN b)
     272             : {
     273             :   long ta, tb;
     274             :   pari_sp av;
     275             :   GEN x;
     276         119 :   if (!b) return idealhnf(nf,a);
     277             : 
     278             :   /* HNF of aZ_K+bZ_K */
     279          56 :   av = avma; nf = checknf(nf);
     280          56 :   a = nf_to_scalar_or_basis(nf,a); ta = typ(a);
     281          56 :   b = nf_to_scalar_or_basis(nf,b); tb = typ(b);
     282          56 :   if (ta == t_COL)
     283          14 :     x = (tb==t_COL)? hnf_QC_QC(nf, a,b): hnf_Q_QC(nf, b,a);
     284             :   else
     285          42 :     x = (tb==t_COL)? hnf_Q_QC(nf, a,b): hnf_Q_Q(nf, a,b);
     286          56 :   return gerepileupto(av, x);
     287             : }
     288             : 
     289             : /*******************************************************************/
     290             : /*                                                                 */
     291             : /*                       TWO-ELEMENT FORM                          */
     292             : /*                                                                 */
     293             : /*******************************************************************/
     294             : static GEN idealapprfact_i(GEN nf, GEN x, int nored);
     295             : 
     296             : static int
     297      277737 : ok_elt(GEN x, GEN xZ, GEN y)
     298             : {
     299      277737 :   pari_sp av = avma;
     300      277737 :   int r = ZM_equal(x, ZM_hnfmodid(y, xZ));
     301      277737 :   avma = av; return r;
     302             : }
     303             : 
     304             : static GEN
     305       60368 : addmul_col(GEN a, long s, GEN b)
     306             : {
     307             :   long i,l;
     308       60368 :   if (!s) return a? leafcopy(a): a;
     309       60222 :   if (!a) return gmulsg(s,b);
     310       57194 :   l = lg(a);
     311      329061 :   for (i=1; i<l; i++)
     312      271867 :     if (signe(gel(b,i))) gel(a,i) = addii(gel(a,i), mulsi(s, gel(b,i)));
     313       57194 :   return a;
     314             : }
     315             : 
     316             : /* a <-- a + s * b, all coeffs integers */
     317             : static GEN
     318       25091 : addmul_mat(GEN a, long s, GEN b)
     319             : {
     320             :   long j,l;
     321             :   /* copy otherwise next call corrupts a */
     322       25091 :   if (!s) return a? RgM_shallowcopy(a): a;
     323       23463 :   if (!a) return gmulsg(s,b);
     324       12905 :   l = lg(a);
     325       65273 :   for (j=1; j<l; j++)
     326       52368 :     (void)addmul_col(gel(a,j), s, gel(b,j));
     327       12905 :   return a;
     328             : }
     329             : 
     330             : static GEN
     331      211758 : get_random_a(GEN nf, GEN x, GEN xZ)
     332             : {
     333             :   pari_sp av1;
     334      211758 :   long i, lm, l = lg(x);
     335             :   GEN a, z, beta, mul;
     336             : 
     337      211758 :   beta= cgetg(l, t_VEC);
     338      211758 :   mul = cgetg(l, t_VEC); lm = 1; /* = lg(mul) */
     339             :   /* look for a in x such that a O/xZ = x O/xZ */
     340      387258 :   for (i = 2; i < l; i++)
     341             :   {
     342      384230 :     GEN t, y, xi = gel(x,i);
     343      384230 :     av1 = avma;
     344      384230 :     y = zk_scalar_or_multable(nf, xi); /* ZM, cannot be a scalar */
     345      384223 :     t = FpM_red(y, xZ);
     346      384223 :     if (gequal0(t)) { avma = av1; continue; }
     347      267179 :     if (ok_elt(x,xZ, t)) return xi;
     348       58456 :     gel(beta,lm) = xi;
     349             :     /* mul[i] = { canonical generators for x[i] O/xZ as Z-module } */
     350       58456 :     gel(mul,lm) = t; lm++;
     351             :   }
     352        3028 :   setlg(mul, lm);
     353        3028 :   setlg(beta,lm);
     354        3028 :   z = cgetg(lm, t_VECSMALL);
     355       10586 :   for(av1=avma;;avma=av1)
     356             :   {
     357       35677 :     for (a=NULL,i=1; i<lm; i++)
     358             :     {
     359       25091 :       long t = random_bits(4) - 7; /* in [-7,8] */
     360       25091 :       z[i] = t;
     361       25091 :       a = addmul_mat(a, t, gel(mul,i));
     362             :     }
     363             :     /* a = matrix (NOT HNF) of ideal generated by beta.z in O/xZ */
     364       10586 :     if (a && ok_elt(x,xZ, a)) break;
     365        7558 :   }
     366       11028 :   for (a=NULL,i=1; i<lm; i++)
     367        8000 :     a = addmul_col(a, z[i], gel(beta,i));
     368        3028 :   return a;
     369             : }
     370             : 
     371             : /* if x square matrix, assume it is HNF */
     372             : static GEN
     373      481061 : mat_ideal_two_elt(GEN nf, GEN x)
     374             : {
     375             :   GEN y, a, cx, xZ;
     376      481061 :   long N = nf_get_degree(nf);
     377             :   pari_sp av, tetpil;
     378             : 
     379      481061 :   if (N == 2) return mkvec2copy(gcoeff(x,1,1), gel(x,2));
     380             : 
     381      224185 :   y = cgetg(3,t_VEC); av = avma;
     382      224185 :   cx = Q_content(x);
     383      224185 :   xZ = gcoeff(x,1,1);
     384      224185 :   if (gequal(xZ, cx)) /* x = (cx) */
     385             :   {
     386        3101 :     gel(y,1) = cx;
     387        3101 :     gel(y,2) = scalarcol_shallow(gen_0, N); return y;
     388             :   }
     389      221084 :   if (equali1(cx)) cx = NULL;
     390             :   else
     391             :   {
     392         371 :     x = Q_div_to_int(x, cx);
     393         371 :     xZ = gcoeff(x,1,1);
     394             :   }
     395      221084 :   if (N < 6)
     396      207142 :     a = get_random_a(nf, x, xZ);
     397             :   else
     398             :   {
     399       13942 :     const long FB[] = { _evallg(15+1) | evaltyp(t_VECSMALL),
     400             :       2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47
     401             :     };
     402       13942 :     GEN P, E, a1 = Z_smoothen(xZ, (GEN)FB, &P, &E);
     403       13942 :     if (!a1) /* factors completely */
     404        9326 :       a = idealapprfact_i(nf, idealfactor(nf,x), 1);
     405        4616 :     else if (lg(P) == 1) /* no small factors */
     406        3061 :       a = get_random_a(nf, x, xZ);
     407             :     else /* general case */
     408             :     {
     409             :       GEN A0, A1, a0, u0, u1, v0, v1, pi0, pi1, t, u;
     410        1555 :       a0 = diviiexact(xZ, a1);
     411        1555 :       A0 = ZM_hnfmodid(x, a0); /* smooth part of x */
     412        1555 :       A1 = ZM_hnfmodid(x, a1); /* cofactor */
     413        1555 :       pi0 = idealapprfact_i(nf, idealfactor(nf,A0), 1);
     414        1555 :       pi1 = get_random_a(nf, A1, a1);
     415        1555 :       (void)bezout(a0, a1, &v0,&v1);
     416        1555 :       u0 = mulii(a0, v0);
     417        1555 :       u1 = mulii(a1, v1);
     418        1555 :       t = ZC_Z_mul(pi0, u1); gel(t,1) = addii(gel(t,1), u0);
     419        1555 :       u = ZC_Z_mul(pi1, u0); gel(u,1) = addii(gel(u,1), u1);
     420        1555 :       a = nfmuli(nf, centermod(u, xZ), centermod(t, xZ));
     421             :     }
     422             :   }
     423      221077 :   if (cx)
     424             :   {
     425         371 :     a = centermod(a, xZ);
     426         371 :     tetpil = avma;
     427         371 :     if (typ(cx) == t_INT)
     428             :     {
     429         329 :       gel(y,1) = mulii(xZ, cx);
     430         329 :       gel(y,2) = ZC_Z_mul(a, cx);
     431             :     }
     432             :     else
     433             :     {
     434          42 :       gel(y,1) = gmul(xZ, cx);
     435          42 :       gel(y,2) = RgC_Rg_mul(a, cx);
     436             :     }
     437             :   }
     438             :   else
     439             :   {
     440      220706 :     tetpil = avma;
     441      220706 :     gel(y,1) = icopy(xZ);
     442      220706 :     gel(y,2) = centermod(a, xZ);
     443             :   }
     444      221077 :   gerepilecoeffssp(av,tetpil,y+1,2); return y;
     445             : }
     446             : 
     447             : /* Given an ideal x, returns [a,alpha] such that a is in Q,
     448             :  * x = a Z_K + alpha Z_K, alpha in K^*
     449             :  * a = 0 or alpha = 0 are possible, but do not try to determine whether
     450             :  * x is principal. */
     451             : GEN
     452       20653 : idealtwoelt(GEN nf, GEN x)
     453             : {
     454             :   pari_sp av;
     455             :   GEN z;
     456       20653 :   long tx = idealtyp(&x,&z);
     457       20646 :   nf = checknf(nf);
     458       20646 :   if (tx == id_MAT) return mat_ideal_two_elt(nf,x);
     459        1288 :   if (tx == id_PRIME) return mkvec2copy(gel(x,1), gel(x,2));
     460             :   /* id_PRINCIPAL */
     461         511 :   av = avma; x = nf_to_scalar_or_basis(nf, x);
     462         833 :   return gerepilecopy(av, typ(x)==t_COL? mkvec2(gen_0,x):
     463         406 :                                          mkvec2(Q_abs_shallow(x),gen_0));
     464             : }
     465             : 
     466             : /*******************************************************************/
     467             : /*                                                                 */
     468             : /*                         FACTORIZATION                           */
     469             : /*                                                                 */
     470             : /*******************************************************************/
     471             : /* x integral ideal in HNF, return v_p(Nx), *vz = v_p(x \cap Z)
     472             :  * Use x[1,1] = x \cap Z */
     473             : long
     474      356662 : val_norm(GEN x, GEN p, long *vz)
     475             : {
     476      356662 :   long i,l = lg(x), v;
     477      356662 :   *vz = v = Z_pval(gcoeff(x,1,1), p);
     478      356662 :   if (!v) return 0;
     479      141285 :   for (i=2; i<l; i++) v += Z_pval(gcoeff(x,i,i), p);
     480      141285 :   return v;
     481             : }
     482             : 
     483             : /* return factorization of Nx, x integral in HNF */
     484             : GEN
     485       22123 : factor_norm(GEN x)
     486             : {
     487       22123 :   GEN r = gcoeff(x,1,1), f, p, e;
     488             :   long i, k, l;
     489       22123 :   if (typ(r)!=t_INT) pari_err_TYPE("idealfactor",r);
     490       22123 :   f = Z_factor(r); p = gel(f,1); e = gel(f,2); l = lg(p);
     491       22123 :   for (i=1; i<l; i++) e[i] = val_norm(x,gel(p,i), &k);
     492       22123 :   settyp(e, t_VECSMALL); return f;
     493             : }
     494             : 
     495             : /* X integral ideal */
     496             : static GEN
     497       22123 : idealfactor_HNF(GEN nf, GEN x)
     498             : {
     499       22123 :   const long N = lg(x)-1;
     500             :   long i, j, k, lf, lc, v, vc;
     501             :   GEN f, f1, f2, c1, c2, y1, y2, p1, cx, P;
     502             : 
     503       22123 :   x = Q_primitive_part(x, &cx);
     504       22123 :   if (!cx)
     505             :   {
     506       17986 :     c1 = c2 = NULL; /* gcc -Wall */
     507       17986 :     lc = 1;
     508             :   }
     509             :   else
     510             :   {
     511        4137 :     f = Z_factor(cx);
     512        4137 :     c1 = gel(f,1);
     513        4137 :     c2 = gel(f,2); lc = lg(c1);
     514             :   }
     515       22123 :   f = factor_norm(x);
     516       22123 :   f1 = gel(f,1);
     517       22123 :   f2 = gel(f,2); lf = lg(f1);
     518       22123 :   y1 = cgetg((lf+lc-2)*N+1, t_COL);
     519       22123 :   y2 = cgetg((lf+lc-2)*N+1, t_VECSMALL);
     520       22123 :   k = 1;
     521       43448 :   for (i=1; i<lf; i++)
     522             :   {
     523       21325 :     long l = f2[i]; /* = v_p(Nx) */
     524       21325 :     p1 = idealprimedec(nf,gel(f1,i));
     525       21325 :     vc = cx? Z_pval(cx,gel(f1,i)): 0;
     526       40818 :     for (j=1; j<lg(p1); j++)
     527             :     {
     528       40811 :       P = gel(p1,j);
     529       40811 :       v = idealval(nf,x,P);
     530       40811 :       l -= v*pr_get_f(P);
     531       40811 :       v += vc * pr_get_e(P); if (!v) continue;
     532       28746 :       gel(y1,k) = P;
     533       28746 :       y2[k] = v; k++;
     534       28746 :       if (l == 0) break; /* now only the content contributes */
     535             :     }
     536       21325 :     if (vc == 0) continue;
     537         748 :     for (j++; j<lg(p1); j++)
     538             :     {
     539          83 :       P = gel(p1,j);
     540          83 :       gel(y1,k) = P;
     541          83 :       y2[k++] = vc * pr_get_e(P);
     542             :     }
     543             :   }
     544       26995 :   for (i=1; i<lc; i++)
     545             :   {
     546             :     /* p | Nx already treated */
     547        4872 :     if (dvdii(gcoeff(x,1,1),gel(c1,i))) continue;
     548        4207 :     p1 = idealprimedec(nf,gel(c1,i));
     549        4207 :     vc = itos(gel(c2,i));
     550        9478 :     for (j=1; j<lg(p1); j++)
     551             :     {
     552        5271 :       P = gel(p1,j);
     553        5271 :       gel(y1,k) = P;
     554        5271 :       y2[k++] = vc * pr_get_e(P);
     555             :     }
     556             :   }
     557       22123 :   setlg(y1, k);
     558       22123 :   setlg(y2, k);
     559       22123 :   return mkmat2(y1, zc_to_ZC(y2));
     560             : }
     561             : 
     562             : GEN
     563       24643 : idealfactor(GEN nf, GEN x)
     564             : {
     565       24643 :   pari_sp av = avma;
     566             :   long tx;
     567             :   GEN fa, f, y;
     568             : 
     569       24643 :   nf = checknf(nf);
     570       24643 :   tx = idealtyp(&x,&y);
     571       24643 :   if (tx == id_PRIME)
     572             :   {
     573         182 :     y = cgetg(3,t_MAT);
     574         182 :     gel(y,1) = mkcolcopy(x);
     575         182 :     gel(y,2) = mkcol(gen_1); return y;
     576             :   }
     577       24461 :   if (tx == id_PRINCIPAL)
     578             :   {
     579        3836 :     y = nf_to_scalar_or_basis(nf, x);
     580        3836 :     if (typ(y) != t_COL)
     581             :     {
     582             :       GEN c1, c2;
     583             :       long lfa, i,j;
     584        2345 :       if (isintzero(y)) pari_err_DOMAIN("idealfactor", "ideal", "=",gen_0,x);
     585        2331 :       f = factor(Q_abs_shallow(y));
     586        2331 :       c1 = gel(f,1); lfa = lg(c1);
     587        2331 :       if (lfa == 1) { avma = av; return trivial_fact(); }
     588        1659 :       c2 = gel(f,2);
     589        1659 :       settyp(c1, t_VEC); /* for shallowconcat */
     590        1659 :       settyp(c2, t_VEC); /* for shallowconcat */
     591        4032 :       for (i = 1; i < lfa; i++)
     592             :       {
     593        2373 :         GEN P = idealprimedec(nf, gel(c1,i)), E = gel(c2,i), z;
     594        2373 :         long lP = lg(P);
     595        2373 :         z = cgetg(lP, t_COL);
     596        2373 :         for (j = 1; j < lP; j++) gel(z,j) = mului(pr_get_e(gel(P,j)), E);
     597        2373 :         gel(c1,i) = P;
     598        2373 :         gel(c2,i) = z;
     599             :       }
     600        1659 :       c1 = shallowconcat1(c1); settyp(c1, t_COL);
     601        1659 :       c2 = shallowconcat1(c2);
     602        1659 :       gel(f,1) = c1;
     603        1659 :       gel(f,2) = c2; return gerepilecopy(av, f);
     604             :     }
     605             :   }
     606       22116 :   y = idealnumden(nf, x);
     607       22116 :   if (isintzero(gel(y,1))) pari_err_DOMAIN("idealfactor", "ideal", "=",gen_0,x);
     608       22116 :   fa = idealfactor_HNF(nf, gel(y,1));
     609       22116 :   if (!isint1(gel(y,2)))
     610             :   {
     611           7 :     GEN fa2 = idealfactor_HNF(nf, gel(y,2));
     612           7 :     fa2 = famat_inv_shallow(fa2);
     613           7 :     fa = famat_mul_shallow(fa, fa2);
     614             :   }
     615       22116 :   fa = gerepilecopy(av, fa);
     616       22116 :   return sort_factor(fa, (void*)&cmp_prime_ideal, &cmp_nodata);
     617             : }
     618             : 
     619             : /* P prime ideal in idealprimedec format. Return valuation(ix) at P */
     620             : long
     621      337038 : idealval(GEN nf, GEN ix, GEN P)
     622             : {
     623      337038 :   pari_sp av = avma, av1;
     624      337038 :   long N, vmax, vd, v, e, f, i, j, k, tx = typ(ix);
     625             :   GEN mul, B, a, x, y, r, p, pk, cx, vals;
     626             : 
     627      337038 :   if (is_extscalar_t(tx) || tx==t_COL) return nfval(nf,ix,P);
     628      336772 :   tx = idealtyp(&ix,&a);
     629      336772 :   if (tx == id_PRINCIPAL) return nfval(nf,ix,P);
     630      336765 :   checkprid(P);
     631      336765 :   if (tx == id_PRIME) return pr_equal(nf, P, ix)? 1: 0;
     632             :   /* id_MAT */
     633      336737 :   nf = checknf(nf);
     634      336737 :   N = nf_get_degree(nf);
     635      336737 :   ix = Q_primitive_part(ix, &cx);
     636      336737 :   p = pr_get_p(P);
     637      336737 :   f = pr_get_f(P);
     638      336737 :   if (f == N) { v = cx? Q_pval(cx,p): 0; avma = av; return v; }
     639      335337 :   i = val_norm(ix,p, &k);
     640      335337 :   if (!i) { v = cx? pr_get_e(P) * Q_pval(cx,p): 0; avma = av; return v; }
     641             : 
     642      119960 :   e = pr_get_e(P);
     643      119960 :   vd = cx? e * Q_pval(cx,p): 0;
     644             :   /* 0 <= ceil[v_P(ix) / e] <= v_p(ix \cap Z) --> v_P <= e * v_p */
     645      119960 :   j = k * e;
     646             :   /* 0 <= v_P(ix) <= floor[v_p(Nix) / f] */
     647      119960 :   i = i / f;
     648      119960 :   vmax = minss(i,j); /* v_P(ix) <= vmax */
     649             : 
     650      119960 :   mul = pr_get_tau(P);
     651             :   /* occurs when reading from file a prid in old format */
     652      119960 :   if (typ(mul) != t_MAT) mul = zk_scalar_or_multable(nf,mul);
     653      119960 :   B = cgetg(N+1,t_MAT);
     654      119960 :   pk = powiu(p, (ulong)ceil((double)vmax / e));
     655             :   /* B[1] not needed: v_pr(ix[1]) = v_pr(ix \cap Z) is known already */
     656      119960 :   gel(B,1) = gen_0; /* dummy */
     657      500480 :   for (j=2; j<=N; j++)
     658             :   {
     659      421119 :     x = gel(ix,j);
     660      421119 :     y = cgetg(N+1, t_COL); gel(B,j) = y;
     661     3909992 :     for (i=1; i<=N; i++)
     662             :     { /* compute a = (x.t0)_i, ix in HNF ==> x[j+1..N] = 0 */
     663     3529472 :       a = mulii(gel(x,1), gcoeff(mul,i,1));
     664     3529472 :       for (k=2; k<=j; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     665             :       /* p | a ? */
     666     3529472 :       gel(y,i) = dvmdii(a,p,&r);
     667     3529472 :       if (signe(r)) { avma = av; return vd; }
     668             :     }
     669             :   }
     670       79361 :   vals = cgetg(N+1, t_VECSMALL);
     671             :   /* vals[1] not needed */
     672      397759 :   for (j = 2; j <= N; j++)
     673             :   {
     674      318398 :     gel(B,j) = Q_primitive_part(gel(B,j), &cx);
     675      318398 :     vals[j] = cx? 1 + e * Q_pval(cx, p): 1;
     676             :   }
     677       79361 :   av1 = avma;
     678       79361 :   y = cgetg(N+1,t_COL);
     679             :   /* can compute mod p^ceil((vmax-v)/e) */
     680      129964 :   for (v = 1; v < vmax; v++)
     681             :   { /* we know v_pr(Bj) >= v for all j */
     682       53416 :     if (e == 1 || (vmax - v) % e == 0) pk = diviiexact(pk, p);
     683      413048 :     for (j = 2; j <= N; j++)
     684             :     {
     685      362445 :       x = gel(B,j); if (v < vals[j]) continue;
     686     3685724 :       for (i=1; i<=N; i++)
     687             :       {
     688     3424522 :         pari_sp av2 = avma;
     689     3424522 :         a = mulii(gel(x,1), gcoeff(mul,i,1));
     690     3424522 :         for (k=2; k<=N; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     691             :         /* a = (x.t_0)_i; p | a ? */
     692     3424522 :         a = dvmdii(a,p,&r);
     693     3424522 :         if (signe(r)) { avma = av; return v + vd; }
     694     3421709 :         if (lgefint(a) > lgefint(pk)) a = remii(a, pk);
     695     3421709 :         gel(y,i) = gerepileuptoint(av2, a);
     696             :       }
     697      261202 :       gel(B,j) = y; y = x;
     698      261202 :       if (gc_needed(av1,3))
     699             :       {
     700           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"idealval");
     701           0 :         gerepileall(av1,3, &y,&B,&pk);
     702             :       }
     703             :     }
     704             :   }
     705       76548 :   avma = av; return v + vd;
     706             : }
     707             : GEN
     708          42 : gpidealval(GEN nf, GEN ix, GEN P)
     709             : {
     710          42 :   long v = idealval(nf,ix,P);
     711          42 :   return v == LONG_MAX? mkoo(): stoi(v);
     712             : }
     713             : 
     714             : /* gcd and generalized Bezout */
     715             : 
     716             : GEN
     717       29379 : idealadd(GEN nf, GEN x, GEN y)
     718             : {
     719       29379 :   pari_sp av = avma;
     720             :   long tx, ty;
     721             :   GEN z, a, dx, dy, dz;
     722             : 
     723       29379 :   tx = idealtyp(&x,&z);
     724       29379 :   ty = idealtyp(&y,&z); nf = checknf(nf);
     725       29379 :   if (tx != id_MAT) x = idealhnf_shallow(nf,x);
     726       29379 :   if (ty != id_MAT) y = idealhnf_shallow(nf,y);
     727       29379 :   if (lg(x) == 1) return gerepilecopy(av,y);
     728       29379 :   if (lg(y) == 1) return gerepilecopy(av,x); /* check for 0 ideal */
     729       29379 :   dx = Q_denom(x);
     730       29379 :   dy = Q_denom(y); dz = lcmii(dx,dy);
     731       29379 :   if (is_pm1(dz)) dz = NULL; else {
     732        5719 :     x = Q_muli_to_int(x, dz);
     733        5719 :     y = Q_muli_to_int(y, dz);
     734             :   }
     735       29379 :   a = gcdii(gcoeff(x,1,1), gcoeff(y,1,1));
     736       29379 :   if (is_pm1(a))
     737             :   {
     738       13580 :     long N = lg(x)-1;
     739       13580 :     if (!dz) { avma = av; return matid(N); }
     740         833 :     return gerepileupto(av, scalarmat(ginv(dz), N));
     741             :   }
     742       15799 :   z = ZM_hnfmodid(shallowconcat(x,y), a);
     743       15799 :   if (dz) z = RgM_Rg_div(z,dz);
     744       15799 :   return gerepileupto(av,z);
     745             : }
     746             : 
     747             : static GEN
     748          28 : trivial_merge(GEN x)
     749             : {
     750          28 :   long lx = lg(x);
     751             :   GEN a;
     752          28 :   if (lx == 1) return NULL;
     753          21 :   a = gcoeff(x,1,1);
     754          21 :   if (!is_pm1(a)) return NULL;
     755          14 :   return scalarcol_shallow(gen_1, lx-1);
     756             : }
     757             : GEN
     758      316600 : idealaddtoone_i(GEN nf, GEN x, GEN y)
     759             : {
     760             :   GEN a;
     761      316600 :   long tx = idealtyp(&x, &a/*junk*/);
     762      316600 :   long ty = idealtyp(&y, &a/*junk*/);
     763      316600 :   if (tx != id_MAT) x = idealhnf_shallow(nf, x);
     764      316600 :   if (ty != id_MAT) y = idealhnf_shallow(nf, y);
     765      316600 :   if (lg(x) == 1)
     766          14 :     a = trivial_merge(y);
     767      316586 :   else if (lg(y) == 1)
     768          14 :     a = trivial_merge(x);
     769             :   else {
     770      316572 :     a = hnfmerge_get_1(x, y);
     771      316572 :     if (a) a = ZC_reducemodlll(a, idealmul_HNF(nf,x,y));
     772             :   }
     773      316600 :   if (!a) pari_err_COPRIME("idealaddtoone",x,y);
     774      316586 :   return a;
     775             : }
     776             : 
     777             : GEN
     778        2807 : idealaddtoone(GEN nf, GEN x, GEN y)
     779             : {
     780        2807 :   GEN z = cgetg(3,t_VEC), a;
     781        2807 :   pari_sp av = avma;
     782        2807 :   nf = checknf(nf);
     783        2807 :   a = gerepileupto(av, idealaddtoone_i(nf,x,y));
     784        2793 :   gel(z,1) = a;
     785        2793 :   gel(z,2) = Z_ZC_sub(gen_1,a); return z;
     786             : }
     787             : 
     788             : /* assume elements of list are integral ideals */
     789             : GEN
     790          35 : idealaddmultoone(GEN nf, GEN list)
     791             : {
     792          35 :   pari_sp av = avma;
     793          35 :   long N, i, l, nz, tx = typ(list);
     794             :   GEN H, U, perm, L;
     795             : 
     796          35 :   nf = checknf(nf); N = nf_get_degree(nf);
     797          35 :   if (!is_vec_t(tx)) pari_err_TYPE("idealaddmultoone",list);
     798          35 :   l = lg(list);
     799          35 :   L = cgetg(l, t_VEC);
     800          35 :   if (l == 1)
     801           0 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
     802          35 :   nz = 0; /* number of non-zero ideals in L */
     803          98 :   for (i=1; i<l; i++)
     804             :   {
     805          70 :     GEN I = gel(list,i);
     806          70 :     if (typ(I) != t_MAT) I = idealhnf_shallow(nf,I);
     807          70 :     if (lg(I) != 1)
     808             :     {
     809          42 :       nz++; RgM_check_ZM(I,"idealaddmultoone");
     810          35 :       if (lgcols(I) != N+1) pari_err_TYPE("idealaddmultoone [not an ideal]", I);
     811             :     }
     812          63 :     gel(L,i) = I;
     813             :   }
     814          28 :   H = ZM_hnfperm(shallowconcat1(L), &U, &perm);
     815          28 :   if (lg(H) == 1 || !equali1(gcoeff(H,1,1)))
     816           7 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
     817          49 :   for (i=1; i<=N; i++)
     818          49 :     if (perm[i] == 1) break;
     819          21 :   U = gel(U,(nz-1)*N + i); /* (L[1]|...|L[nz]) U = 1 */
     820          21 :   nz = 0;
     821          63 :   for (i=1; i<l; i++)
     822             :   {
     823          42 :     GEN c = gel(L,i);
     824          42 :     if (lg(c) == 1)
     825          14 :       c = zerocol(N);
     826             :     else {
     827          28 :       c = ZM_ZC_mul(c, vecslice(U, nz*N + 1, (nz+1)*N));
     828          28 :       nz++;
     829             :     }
     830          42 :     gel(L,i) = c;
     831             :   }
     832          21 :   return gerepilecopy(av, L);
     833             : }
     834             : 
     835             : /* multiplication */
     836             : 
     837             : /* x integral ideal (without archimedean component) in HNF form
     838             :  * y = [a,alpha] corresponds to the integral ideal aZ_K+alpha Z_K, a in Z,
     839             :  * alpha a ZV or a ZM (multiplication table). Multiply them */
     840             : static GEN
     841      949060 : idealmul_HNF_two(GEN nf, GEN x, GEN y)
     842             : {
     843      949060 :   GEN m, a = gel(y,1), alpha = gel(y,2);
     844             :   long i, N;
     845             : 
     846      949060 :   if (typ(alpha) != t_MAT)
     847             :   {
     848      780551 :     alpha = zk_scalar_or_multable(nf, alpha);
     849      780551 :     if (typ(alpha) == t_INT) /* e.g. y inert ? 0 should not (but may) occur */
     850        4165 :       return signe(a)? ZM_Z_mul(x, gcdii(a, alpha)): cgetg(1,t_MAT);
     851             :   }
     852      944895 :   N = lg(x)-1; m = cgetg((N<<1)+1,t_MAT);
     853      944895 :   for (i=1; i<=N; i++) gel(m,i)   = ZM_ZC_mul(alpha,gel(x,i));
     854      944895 :   for (i=1; i<=N; i++) gel(m,i+N) = ZC_Z_mul(gel(x,i), a);
     855      944895 :   return ZM_hnfmodid(m, mulii(a, gcoeff(x,1,1)));
     856             : }
     857             : 
     858             : /* Assume ix and iy are integral in HNF form [NOT extended]. Not memory clean.
     859             :  * HACK: ideal in iy can be of the form [a,b], a in Z, b in Z_K */
     860             : GEN
     861      613849 : idealmul_HNF(GEN nf, GEN x, GEN y)
     862             : {
     863             :   GEN z;
     864      613849 :   if (typ(y) == t_VEC)
     865      181637 :     z = idealmul_HNF_two(nf,x,y);
     866             :   else
     867             :   { /* reduce one ideal to two-elt form. The smallest */
     868      432212 :     GEN xZ = gcoeff(x,1,1), yZ = gcoeff(y,1,1);
     869      432212 :     if (cmpii(xZ, yZ) < 0)
     870             :     {
     871       32172 :       if (is_pm1(xZ)) return gcopy(y);
     872       24667 :       z = idealmul_HNF_two(nf, y, mat_ideal_two_elt(nf,x));
     873             :     }
     874             :     else
     875             :     {
     876      400040 :       if (is_pm1(yZ)) return gcopy(x);
     877      383415 :       z = idealmul_HNF_two(nf, x, mat_ideal_two_elt(nf,y));
     878             :     }
     879             :   }
     880      589719 :   return z;
     881             : }
     882             : 
     883             : /* operations on elements in factored form */
     884             : 
     885             : GEN
     886        3073 : famat_mul_shallow(GEN f, GEN g)
     887             : {
     888        3073 :   if (lg(f) == 1) return g;
     889        3073 :   if (lg(g) == 1) return f;
     890        6146 :   return mkmat2(shallowconcat(gel(f,1), gel(g,1)),
     891        6146 :                 shallowconcat(gel(f,2), gel(g,2)));
     892             : }
     893             : 
     894             : GEN
     895         910 : to_famat(GEN x, GEN y) {
     896         910 :   GEN fa = cgetg(3, t_MAT);
     897         910 :   gel(fa,1) = mkcol(gcopy(x));
     898         910 :   gel(fa,2) = mkcol(gcopy(y)); return fa;
     899             : }
     900             : GEN
     901      720476 : to_famat_shallow(GEN x, GEN y) {
     902      720476 :   GEN fa = cgetg(3, t_MAT);
     903      720476 :   gel(fa,1) = mkcol(x);
     904      720476 :   gel(fa,2) = mkcol(y); return fa;
     905             : }
     906             : 
     907             : static GEN
     908      101481 : append(GEN v, GEN x)
     909             : {
     910      101481 :   long i, l = lg(v);
     911      101481 :   GEN w = cgetg(l+1, typ(v));
     912      101481 :   for (i=1; i<l; i++) gel(w,i) = gcopy(gel(v,i));
     913      101481 :   gel(w,i) = gcopy(x); return w;
     914             : }
     915             : 
     916             : /* add x^1 to famat f */
     917             : static GEN
     918      126495 : famat_add(GEN f, GEN x)
     919             : {
     920      126495 :   GEN h = cgetg(3,t_MAT);
     921      126495 :   if (lg(f) == 1)
     922             :   {
     923       25014 :     gel(h,1) = mkcolcopy(x);
     924       25014 :     gel(h,2) = mkcol(gen_1);
     925             :   }
     926             :   else
     927             :   {
     928      101481 :     gel(h,1) = append(gel(f,1), x); /* x may be a t_COL */
     929      101481 :     gel(h,2) = gconcat(gel(f,2), gen_1);
     930             :   }
     931      126495 :   return h;
     932             : }
     933             : 
     934             : GEN
     935      180693 : famat_mul(GEN f, GEN g)
     936             : {
     937             :   GEN h;
     938      180693 :   if (typ(g) != t_MAT) {
     939      126467 :     if (typ(f) == t_MAT) return famat_add(f, g);
     940           0 :     h = cgetg(3, t_MAT);
     941           0 :     gel(h,1) = mkcol2(gcopy(f), gcopy(g));
     942           0 :     gel(h,2) = mkcol2(gen_1, gen_1);
     943             :   }
     944       54226 :   if (typ(f) != t_MAT) return famat_add(g, f);
     945       54198 :   if (lg(f) == 1) return gcopy(g);
     946       19184 :   if (lg(g) == 1) return gcopy(f);
     947       15305 :   h = cgetg(3,t_MAT);
     948       15305 :   gel(h,1) = gconcat(gel(f,1), gel(g,1));
     949       15305 :   gel(h,2) = gconcat(gel(f,2), gel(g,2));
     950       15305 :   return h;
     951             : }
     952             : 
     953             : GEN
     954       54953 : famat_sqr(GEN f)
     955             : {
     956             :   GEN h;
     957       54953 :   if (lg(f) == 1) return cgetg(1,t_MAT);
     958       26168 :   if (typ(f) != t_MAT) return to_famat(f,gen_2);
     959       26168 :   h = cgetg(3,t_MAT);
     960       26168 :   gel(h,1) = gcopy(gel(f,1));
     961       26168 :   gel(h,2) = gmul2n(gel(f,2),1);
     962       26168 :   return h;
     963             : }
     964             : GEN
     965           7 : famat_inv_shallow(GEN f)
     966             : {
     967             :   GEN h;
     968           7 :   if (lg(f) == 1) return cgetg(1,t_MAT);
     969           7 :   if (typ(f) != t_MAT) return to_famat_shallow(f,gen_m1);
     970           7 :   h = cgetg(3,t_MAT);
     971           7 :   gel(h,1) = gel(f,1);
     972           7 :   gel(h,2) = ZC_neg(gel(f,2));
     973           7 :   return h;
     974             : }
     975             : GEN
     976        4442 : famat_inv(GEN f)
     977             : {
     978             :   GEN h;
     979        4442 :   if (lg(f) == 1) return cgetg(1,t_MAT);
     980        2379 :   if (typ(f) != t_MAT) return to_famat(f,gen_m1);
     981        2379 :   h = cgetg(3,t_MAT);
     982        2379 :   gel(h,1) = gcopy(gel(f,1));
     983        2379 :   gel(h,2) = ZC_neg(gel(f,2));
     984        2379 :   return h;
     985             : }
     986             : GEN
     987        3026 : famat_pow(GEN f, GEN n)
     988             : {
     989             :   GEN h;
     990        3026 :   if (lg(f) == 1) return cgetg(1,t_MAT);
     991        2737 :   if (typ(f) != t_MAT) return to_famat(f,n);
     992        1827 :   h = cgetg(3,t_MAT);
     993        1827 :   gel(h,1) = gcopy(gel(f,1));
     994        1827 :   gel(h,2) = ZC_Z_mul(gel(f,2),n);
     995        1827 :   return h;
     996             : }
     997             : 
     998             : GEN
     999           0 : famat_Z_gcd(GEN M, GEN n)
    1000             : {
    1001           0 :   pari_sp av=avma;
    1002           0 :   long i, j, l=lgcols(M);
    1003           0 :   GEN F=cgetg(3,t_MAT);
    1004           0 :   gel(F,1)=cgetg(l,t_COL);
    1005           0 :   gel(F,2)=cgetg(l,t_COL);
    1006           0 :   for (i=1, j=1; i<l; i++)
    1007             :   {
    1008           0 :     GEN p = gcoeff(M,i,1);
    1009           0 :     GEN e = gminsg(Z_pval(n,p),gcoeff(M,i,2));
    1010           0 :     if (signe(e))
    1011             :     {
    1012           0 :       gcoeff(F,j,1)=p;
    1013           0 :       gcoeff(F,j,2)=e;
    1014           0 :       j++;
    1015             :     }
    1016             :   }
    1017           0 :   setlg(gel(F,1),j); setlg(gel(F,2),j);
    1018           0 :   return gerepilecopy(av,F);
    1019             : }
    1020             : 
    1021             : /* x assumed to be a t_MATs (factorization matrix), or compatible with
    1022             :  * the element_* functions. */
    1023             : static GEN
    1024       65502 : ext_sqr(GEN nf, GEN x) {
    1025       65502 :   if (typ(x) == t_MAT) return famat_sqr(x);
    1026       10549 :   return nfsqr(nf, x);
    1027             : }
    1028             : static GEN
    1029      176168 : ext_mul(GEN nf, GEN x, GEN y) {
    1030      176168 :   if (typ(x) == t_MAT) return (x == y)? famat_sqr(x): famat_mul(x,y);
    1031       52941 :   return nfmul(nf, x, y);
    1032             : }
    1033             : static GEN
    1034        4302 : ext_inv(GEN nf, GEN x) {
    1035        4302 :   if (typ(x) == t_MAT) return famat_inv(x);
    1036           0 :   return nfinv(nf, x);
    1037             : }
    1038             : static GEN
    1039         289 : ext_pow(GEN nf, GEN x, GEN n) {
    1040         289 :   if (typ(x) == t_MAT) return famat_pow(x,n);
    1041           0 :   return nfpow(nf, x, n);
    1042             : }
    1043             : 
    1044             : /* x, y 2 extended ideals whose first component is an integral HNF */
    1045             : GEN
    1046       18869 : extideal_HNF_mul(GEN nf, GEN x, GEN y)
    1047             : {
    1048       37738 :   return mkvec2(idealmul_HNF(nf, gel(x,1), gel(y,1)),
    1049       37738 :                 ext_mul(nf, gel(x,2), gel(y,2)));
    1050             : }
    1051             : 
    1052             : GEN
    1053           0 : famat_to_nf(GEN nf, GEN f)
    1054             : {
    1055             :   GEN t, x, e;
    1056             :   long i;
    1057           0 :   if (lg(f) == 1) return gen_1;
    1058             : 
    1059           0 :   x = gel(f,1);
    1060           0 :   e = gel(f,2);
    1061           0 :   t = nfpow(nf, gel(x,1), gel(e,1));
    1062           0 :   for (i=lg(x)-1; i>1; i--)
    1063           0 :     t = nfmul(nf, t, nfpow(nf, gel(x,i), gel(e,i)));
    1064           0 :   return t;
    1065             : }
    1066             : 
    1067             : /* "compare" two nf elt. Goal is to quickly sort for uniqueness of
    1068             :  * representation, not uniqueness of represented element ! */
    1069             : static int
    1070       20602 : elt_cmp(GEN x, GEN y)
    1071             : {
    1072       20602 :   long tx = typ(x), ty = typ(y);
    1073       20602 :   if (ty == tx)
    1074       20112 :     return (tx == t_POL || tx == t_POLMOD)? cmp_RgX(x,y): lexcmp(x,y);
    1075         490 :   return tx - ty;
    1076             : }
    1077             : static int
    1078        6027 : elt_egal(GEN x, GEN y)
    1079             : {
    1080        6027 :   if (typ(x) == typ(y)) return gequal(x,y);
    1081         252 :   return 0;
    1082             : }
    1083             : 
    1084             : GEN
    1085        7903 : famat_reduce(GEN fa)
    1086             : {
    1087             :   GEN E, G, L, g, e;
    1088             :   long i, k, l;
    1089             : 
    1090        7903 :   if (lg(fa) == 1) return fa;
    1091        5187 :   g = gel(fa,1); l = lg(g);
    1092        5187 :   e = gel(fa,2);
    1093        5187 :   L = gen_indexsort(g, (void*)&elt_cmp, &cmp_nodata);
    1094        5187 :   G = cgetg(l, t_COL);
    1095        5187 :   E = cgetg(l, t_COL);
    1096             :   /* merge */
    1097       16401 :   for (k=i=1; i<l; i++,k++)
    1098             :   {
    1099       11214 :     gel(G,k) = gel(g,L[i]);
    1100       11214 :     gel(E,k) = gel(e,L[i]);
    1101       11214 :     if (k > 1 && elt_egal(gel(G,k), gel(G,k-1)))
    1102             :     {
    1103         777 :       gel(E,k-1) = addii(gel(E,k), gel(E,k-1));
    1104         777 :       k--;
    1105             :     }
    1106             :   }
    1107             :   /* kill 0 exponents */
    1108        5187 :   l = k;
    1109       15624 :   for (k=i=1; i<l; i++)
    1110       10437 :     if (!gequal0(gel(E,i)))
    1111             :     {
    1112        9849 :       gel(G,k) = gel(G,i);
    1113        9849 :       gel(E,k) = gel(E,i); k++;
    1114             :     }
    1115        5187 :   setlg(G, k);
    1116        5187 :   setlg(E, k); return mkmat2(G,E);
    1117             : }
    1118             : 
    1119             : GEN
    1120        8806 : famatsmall_reduce(GEN fa)
    1121             : {
    1122             :   GEN E, G, L, g, e;
    1123             :   long i, k, l;
    1124        8806 :   if (lg(fa) == 1) return fa;
    1125        8806 :   g = gel(fa,1); l = lg(g);
    1126        8806 :   e = gel(fa,2);
    1127        8806 :   L = vecsmall_indexsort(g);
    1128        8806 :   G = cgetg(l, t_VECSMALL);
    1129        8806 :   E = cgetg(l, t_VECSMALL);
    1130             :   /* merge */
    1131       76391 :   for (k=i=1; i<l; i++,k++)
    1132             :   {
    1133       67585 :     G[k] = g[L[i]];
    1134       67585 :     E[k] = e[L[i]];
    1135       67585 :     if (k > 1 && G[k] == G[k-1])
    1136             :     {
    1137        3136 :       E[k-1] += E[k];
    1138        3136 :       k--;
    1139             :     }
    1140             :   }
    1141             :   /* kill 0 exponents */
    1142        8806 :   l = k;
    1143       73255 :   for (k=i=1; i<l; i++)
    1144       64449 :     if (E[i])
    1145             :     {
    1146       63007 :       G[k] = G[i];
    1147       63007 :       E[k] = E[i]; k++;
    1148             :     }
    1149        8806 :   setlg(G, k);
    1150        8806 :   setlg(E, k); return mkmat2(G,E);
    1151             : }
    1152             : 
    1153             : GEN
    1154       60844 : ZM_famat_limit(GEN fa, GEN limit)
    1155             : {
    1156             :   pari_sp av;
    1157             :   GEN E, G, g, e, r;
    1158             :   long i, k, l, n, lG;
    1159             : 
    1160       60844 :   if (lg(fa) == 1) return fa;
    1161       60844 :   g = gel(fa,1); l = lg(g);
    1162       60844 :   e = gel(fa,2);
    1163      148834 :   for(n=0, i=1; i<l; i++)
    1164       87990 :     if (cmpii(gel(g,i),limit)<=0) n++;
    1165       60844 :   lG = n<l-1 ? n+2 : n+1;
    1166       60844 :   G = cgetg(lG, t_COL);
    1167       60844 :   E = cgetg(lG, t_COL);
    1168       60844 :   av = avma;
    1169      148834 :   for (i=1, k=1, r = gen_1; i<l; i++)
    1170             :   {
    1171       87990 :     if (cmpii(gel(g,i),limit)<=0)
    1172             :     {
    1173       87913 :       gel(G,k) = gel(g,i);
    1174       87913 :       gel(E,k) = gel(e,i);
    1175       87913 :       k++;
    1176          77 :     } else r = mulii(r, powii(gel(g,i), gel(e,i)));
    1177             :   }
    1178       60844 :   if (k<i)
    1179             :   {
    1180          77 :     gel(G, k) = gerepileuptoint(av, r);
    1181          77 :     gel(E, k) = gen_1;
    1182             :   }
    1183       60844 :   return mkmat2(G,E);
    1184             : }
    1185             : 
    1186             : /* assume pr has degree 1 and coprime to Q_denom(x) */
    1187             : static GEN
    1188        5117 : to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1189             : {
    1190        5117 :   GEN d, r, p = modpr_get_p(modpr);
    1191        5117 :   x = nf_to_scalar_or_basis(nf,x);
    1192        5117 :   if (typ(x) != t_COL) return Rg_to_Fp(x,p);
    1193        4753 :   x = Q_remove_denom(x, &d);
    1194        4753 :   r = zk_to_Fq(x, modpr);
    1195        4753 :   if (d) r = Fp_div(r, d, p);
    1196        4753 :   return r;
    1197             : }
    1198             : 
    1199             : /* pr coprime to all denominators occurring in x */
    1200             : static GEN
    1201         789 : famat_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1202             : {
    1203         789 :   GEN p = modpr_get_p(modpr);
    1204         789 :   GEN t = NULL, g = gel(x,1), e = gel(x,2), q = subiu(p,1);
    1205         789 :   long i, l = lg(g);
    1206        2433 :   for (i = 1; i < l; i++)
    1207             :   {
    1208        1644 :     GEN n = modii(gel(e,i), q);
    1209        1644 :     if (signe(n))
    1210             :     {
    1211        1644 :       GEN h = to_Fp_coprime(nf, gel(g,i), modpr);
    1212        1644 :       h = Fp_pow(h, n, p);
    1213        1644 :       t = t? Fp_mul(t, h, p): h;
    1214             :     }
    1215             :   }
    1216         789 :   return t? modii(t, p): gen_1;
    1217             : }
    1218             : 
    1219             : /* cf famat_to_nf_modideal_coprime, modpr attached to prime of degree 1 */
    1220             : GEN
    1221        4262 : nf_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1222             : {
    1223        8524 :   return typ(x)==t_MAT? famat_to_Fp_coprime(nf, x, modpr)
    1224        4262 :                       : to_Fp_coprime(nf, x, modpr);
    1225             : }
    1226             : 
    1227             : /* Compute A = prod g[i]^e[i] mod pr^k, assuming (A, pr) = 1.
    1228             :  * Method: modify each g[i] so that it becomes coprime to pr :
    1229             :  *  x / (p^k u) --> x * (b/p)^v_pr(x) / z^k u, where z = b^e/p^(e-1)
    1230             :  * b/p = pr^(-1) times something prime to p; both numerator and denominator
    1231             :  * are integral and coprime to pr.  Globally, we multiply by (b/p)^v_pr(A) = 1.
    1232             :  *
    1233             :  * EX = multiple of exponent of (O_K / pr^k)^* used to reduce the product in
    1234             :  * case the e[i] are large */
    1235             : GEN
    1236       95865 : famat_makecoprime(GEN nf, GEN g, GEN e, GEN pr, GEN prk, GEN EX)
    1237             : {
    1238       95865 :   long i, l = lg(g);
    1239       95865 :   GEN prkZ, u, vden = gen_0, p = pr_get_p(pr);
    1240       95865 :   pari_sp av = avma;
    1241       95865 :   GEN newg = cgetg(l+1, t_VEC); /* room for z */
    1242             : 
    1243       95865 :   prkZ = gcoeff(prk, 1,1);
    1244      335027 :   for (i=1; i < l; i++)
    1245             :   {
    1246      239162 :     GEN dx, x = nf_to_scalar_or_basis(nf, gel(g,i));
    1247      239162 :     long vdx = 0;
    1248      239162 :     x = Q_remove_denom(x, &dx);
    1249      239162 :     if (dx)
    1250             :     {
    1251      148766 :       vdx = Z_pvalrem(dx, p, &u);
    1252      148766 :       if (!is_pm1(u))
    1253             :       { /* could avoid the inversion, but prkZ is small--> cheap */
    1254       50652 :         u = Fp_inv(u, prkZ);
    1255       50652 :         x = typ(x) == t_INT? mulii(x,u): ZC_Z_mul(x, u);
    1256             :       }
    1257      148766 :       if (vdx) vden = addii(vden, mului(vdx, gel(e,i)));
    1258             :     }
    1259      239162 :     if (typ(x) == t_INT) {
    1260       39899 :       if (!vdx) vden = subii(vden, mului(Z_pvalrem(x, p, &x), gel(e,i)));
    1261             :     } else {
    1262      199263 :       (void)ZC_nfvalrem(nf, x, pr, &x);
    1263      199263 :       x =  ZC_hnfrem(x, prk);
    1264             :     }
    1265      239162 :     gel(newg,i) = x;
    1266      239162 :     if (gc_needed(av, 2))
    1267             :     {
    1268           0 :       GEN dummy = cgetg(1,t_VEC);
    1269             :       long j;
    1270           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"famat_makecoprime");
    1271           0 :       for (j = i+1; j <= l; j++) gel(newg,j) = dummy;
    1272           0 :       gerepileall(av,2, &newg, &vden);
    1273             :     }
    1274             :   }
    1275       95865 :   if (vden == gen_0) setlg(newg, l);
    1276             :   else
    1277             :   {
    1278       10563 :     GEN t = special_anti_uniformizer(nf, pr);
    1279       10563 :     if (typ(t) == t_INT) setlg(newg, l); /* = 1 */
    1280             :     else {
    1281       10563 :       if (typ(t) == t_MAT) t = gel(t,1); /* multiplication table */
    1282       10563 :       gel(newg,i) = FpC_red(t, prkZ);
    1283       10563 :       e = shallowconcat(e, negi(vden));
    1284             :     }
    1285             :   }
    1286       95865 :   return famat_to_nf_modideal_coprime(nf, newg, e, prk, EX);
    1287             : }
    1288             : 
    1289             : /* prod g[i]^e[i] mod bid, assume (g[i], id) = 1 */
    1290             : GEN
    1291       15015 : famat_to_nf_moddivisor(GEN nf, GEN g, GEN e, GEN bid)
    1292             : {
    1293             :   GEN t,sarch,module,cyc,fa2;
    1294             :   long lc;
    1295       15015 :   if (lg(g) == 1) return scalarcol_shallow(gen_1, nf_get_degree(nf)); /* 1 */
    1296       15015 :   module = bid_get_mod(bid);
    1297       15015 :   cyc = bid_get_cyc(bid); lc = lg(cyc);
    1298       15015 :   fa2 = gel(bid,4); sarch = gel(fa2,lg(fa2)-1);
    1299       15015 :   t = NULL;
    1300       15015 :   if (lc != 1)
    1301             :   {
    1302       15015 :     GEN EX = gel(cyc,1); /* group exponent */
    1303       15015 :     GEN id = gel(module,1);
    1304       15015 :     t = famat_to_nf_modideal_coprime(nf, g, e, id, EX);
    1305             :   }
    1306       15015 :   if (!t) t = gen_1;
    1307       15015 :   return set_sign_mod_divisor(nf, mkmat2(g,e), t, module, sarch);
    1308             : }
    1309             : 
    1310             : GEN
    1311      178024 : vecmul(GEN x, GEN y)
    1312             : {
    1313      178024 :   long i,lx, tx = typ(x);
    1314             :   GEN z;
    1315      178024 :   if (is_scalar_t(tx)) return gmul(x,y);
    1316       15358 :   z = cgetg_copy(x, &lx);
    1317       15358 :   for (i=1; i<lx; i++) gel(z,i) = vecmul(gel(x,i), gel(y,i));
    1318       15358 :   return z;
    1319             : }
    1320             : 
    1321             : GEN
    1322           0 : vecinv(GEN x)
    1323             : {
    1324           0 :   long i,lx, tx = typ(x);
    1325             :   GEN z;
    1326           0 :   if (is_scalar_t(tx)) return ginv(x);
    1327           0 :   z = cgetg_copy(x, &lx);
    1328           0 :   for (i=1; i<lx; i++) gel(z,i) = vecinv(gel(x,i));
    1329           0 :   return z;
    1330             : }
    1331             : 
    1332             : GEN
    1333       15729 : vecpow(GEN x, GEN n)
    1334             : {
    1335       15729 :   long i,lx, tx = typ(x);
    1336             :   GEN z;
    1337       15729 :   if (is_scalar_t(tx)) return powgi(x,n);
    1338        4270 :   z = cgetg_copy(x, &lx);
    1339        4270 :   for (i=1; i<lx; i++) gel(z,i) = vecpow(gel(x,i), n);
    1340        4270 :   return z;
    1341             : }
    1342             : 
    1343             : GEN
    1344         903 : vecdiv(GEN x, GEN y)
    1345             : {
    1346         903 :   long i,lx, tx = typ(x);
    1347             :   GEN z;
    1348         903 :   if (is_scalar_t(tx)) return gdiv(x,y);
    1349         301 :   z = cgetg_copy(x, &lx);
    1350         301 :   for (i=1; i<lx; i++) gel(z,i) = vecdiv(gel(x,i), gel(y,i));
    1351         301 :   return z;
    1352             : }
    1353             : 
    1354             : /* A ideal as a square t_MAT */
    1355             : static GEN
    1356      136223 : idealmulelt(GEN nf, GEN x, GEN A)
    1357             : {
    1358             :   long i, lx;
    1359             :   GEN dx, dA, D;
    1360      136223 :   if (lg(A) == 1) return cgetg(1, t_MAT);
    1361      136223 :   x = nf_to_scalar_or_basis(nf,x);
    1362      136223 :   if (typ(x) != t_COL)
    1363       23037 :     return isintzero(x)? cgetg(1,t_MAT): RgM_Rg_mul(A, Q_abs_shallow(x));
    1364      113186 :   x = Q_remove_denom(x, &dx);
    1365      113186 :   A = Q_remove_denom(A, &dA);
    1366      113186 :   x = zk_multable(nf, x);
    1367      113186 :   D = mulii(zkmultable_capZ(x), gcoeff(A,1,1));
    1368      113186 :   x = zkC_multable_mul(A, x);
    1369      113186 :   settyp(x, t_MAT); lx = lg(x);
    1370             :   /* x may contain scalars (at most 1 since the ideal is non-0)*/
    1371      405743 :   for (i=1; i<lx; i++)
    1372      294811 :     if (typ(gel(x,i)) == t_INT)
    1373             :     {
    1374        2254 :       if (i > 1) swap(gel(x,1), gel(x,i)); /* help HNF */
    1375        2254 :       gel(x,1) = scalarcol_shallow(gel(x,1), lx-1);
    1376        2254 :       break;
    1377             :     }
    1378      113186 :   x = ZM_hnfmodid(x, D);
    1379      113186 :   dx = mul_denom(dx,dA);
    1380      113186 :   return dx? gdiv(x,dx): x;
    1381             : }
    1382             : 
    1383             : /* nf a true nf, tx <= ty */
    1384             : static GEN
    1385      595905 : idealmul_aux(GEN nf, GEN x, GEN y, long tx, long ty)
    1386             : {
    1387             :   GEN z, cx, cy;
    1388      595905 :   switch(tx)
    1389             :   {
    1390             :     case id_PRINCIPAL:
    1391      162599 :       switch(ty)
    1392             :       {
    1393             :         case id_PRINCIPAL:
    1394       26257 :           return idealhnf_principal(nf, nfmul(nf,x,y));
    1395             :         case id_PRIME:
    1396             :         {
    1397         119 :           GEN p = gel(y,1), pi = gel(y,2), cx;
    1398         119 :           if (pr_is_inert(y)) return RgM_Rg_mul(idealhnf_principal(nf,x),p);
    1399             : 
    1400          35 :           x = nf_to_scalar_or_basis(nf, x);
    1401          35 :           switch(typ(x))
    1402             :           {
    1403             :             case t_INT:
    1404          21 :               if (!signe(x)) return cgetg(1,t_MAT);
    1405          21 :               return ZM_Z_mul(idealhnf_two(nf,y), absi(x));
    1406             :             case t_FRAC:
    1407           7 :               return RgM_Rg_mul(idealhnf_two(nf,y), Q_abs_shallow(x));
    1408             :           }
    1409             :           /* t_COL */
    1410           7 :           x = Q_primitive_part(x, &cx);
    1411           7 :           x = zk_multable(nf, x);
    1412           7 :           z = shallowconcat(ZM_Z_mul(x,p), ZM_ZC_mul(x,pi));
    1413           7 :           z = ZM_hnfmodid(z, mulii(p, zkmultable_capZ(x)));
    1414           7 :           return cx? ZM_Q_mul(z, cx): z;
    1415             :         }
    1416             :         default: /* id_MAT */
    1417      136223 :           return idealmulelt(nf, x,y);
    1418             :       }
    1419             :     case id_PRIME:
    1420      337025 :       if (ty==id_PRIME)
    1421      311651 :       { y = idealhnf_two(nf,y); cy = NULL; }
    1422             :       else
    1423       25374 :         y = Q_primitive_part(y, &cy);
    1424      337025 :       y = idealmul_HNF_two(nf,y,x);
    1425      337025 :       return cy? RgM_Rg_mul(y,cy): y;
    1426             : 
    1427             :     default: /* id_MAT */
    1428       96281 :       x = Q_primitive_part(x, &cx);
    1429       96281 :       y = Q_primitive_part(y, &cy); cx = mul_content(cx,cy);
    1430       96281 :       y = idealmul_HNF(nf,x,y);
    1431       96281 :       return cx? ZM_Q_mul(y,cx): y;
    1432             :   }
    1433             : }
    1434             : 
    1435             : /* output the ideal product ix.iy */
    1436             : GEN
    1437      530389 : idealmul(GEN nf, GEN x, GEN y)
    1438             : {
    1439             :   pari_sp av;
    1440             :   GEN res, ax, ay, z;
    1441      530389 :   long tx = idealtyp(&x,&ax);
    1442      530389 :   long ty = idealtyp(&y,&ay), f;
    1443      530389 :   if (tx>ty) { swap(ax,ay); swap(x,y); lswap(tx,ty); }
    1444      530389 :   f = (ax||ay); res = f? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1445      530389 :   av = avma;
    1446      530389 :   z = gerepileupto(av, idealmul_aux(checknf(nf), x,y, tx,ty));
    1447      530382 :   if (!f) return z;
    1448       44972 :   if (ax && ay)
    1449       43288 :     ax = ext_mul(nf, ax, ay);
    1450             :   else
    1451        1684 :     ax = gcopy(ax? ax: ay);
    1452       44972 :   gel(res,1) = z; gel(res,2) = ax; return res;
    1453             : }
    1454             : GEN
    1455       65516 : idealsqr(GEN nf, GEN x)
    1456             : {
    1457             :   pari_sp av;
    1458             :   GEN res, ax, z;
    1459       65516 :   long tx = idealtyp(&x,&ax);
    1460       65516 :   res = ax? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1461       65516 :   av = avma;
    1462       65516 :   z = gerepileupto(av, idealmul_aux(checknf(nf), x,x, tx,tx));
    1463       65516 :   if (!ax) return z;
    1464       65502 :   gel(res,1) = z;
    1465       65502 :   gel(res,2) = ext_sqr(nf, ax); return res;
    1466             : }
    1467             : 
    1468             : /* norm of an ideal */
    1469             : GEN
    1470        8085 : idealnorm(GEN nf, GEN x)
    1471             : {
    1472             :   pari_sp av;
    1473             :   GEN y, T;
    1474             :   long tx;
    1475             : 
    1476        8085 :   switch(idealtyp(&x,&y))
    1477             :   {
    1478         182 :     case id_PRIME: return pr_norm(x);
    1479        6405 :     case id_MAT: return RgM_det_triangular(x);
    1480             :   }
    1481             :   /* id_PRINCIPAL */
    1482        1498 :   nf = checknf(nf); T = nf_get_pol(nf); av = avma;
    1483        1498 :   x = nf_to_scalar_or_alg(nf, x);
    1484        1498 :   x = (typ(x) == t_POL)? RgXQ_norm(x, T): gpowgs(x, degpol(T));
    1485        1498 :   tx = typ(x);
    1486        1498 :   if (tx == t_INT) return gerepileuptoint(av, absi(x));
    1487         266 :   if (tx != t_FRAC) pari_err_TYPE("idealnorm",x);
    1488         266 :   return gerepileupto(av, Q_abs(x));
    1489             : }
    1490             : 
    1491             : /* I^(-1) = { x \in K, Tr(x D^(-1) I) \in Z }, D different of K/Q
    1492             :  *
    1493             :  * nf[5][6] = pp( D^(-1) ) = pp( HNF( T^(-1) ) ), T = (Tr(wi wj))
    1494             :  * nf[5][7] = same in 2-elt form.
    1495             :  * Assume I integral. Return the integral ideal (I\cap Z) I^(-1) */
    1496             : GEN
    1497      156824 : idealinv_HNF_Z(GEN nf, GEN I)
    1498             : {
    1499      156824 :   GEN J, dual, IZ = gcoeff(I,1,1); /* I \cap Z */
    1500      156824 :   if (isint1(IZ)) return matid(lg(I)-1);
    1501      152539 :   J = idealmul_HNF(nf,I, gmael(nf,5,7));
    1502             :  /* I in HNF, hence easily inverted; multiply by IZ to get integer coeffs
    1503             :   * missing content cancels while solving the linear equation */
    1504      152539 :   dual = shallowtrans( hnf_divscale(J, gmael(nf,5,6), IZ) );
    1505      152539 :   return ZM_hnfmodid(dual, IZ);
    1506             : }
    1507             : /* I HNF with rational coefficients (denominator d). */
    1508             : GEN
    1509       32676 : idealinv_HNF(GEN nf, GEN I)
    1510             : {
    1511       32676 :   GEN J, IQ = gcoeff(I,1,1); /* I \cap Q; d IQ = dI \cap Z */
    1512       32676 :   J = idealinv_HNF_Z(nf, Q_remove_denom(I, NULL)); /* = (dI)^(-1) * (d IQ) */
    1513       32676 :   return equali1(IQ)? J: RgM_Rg_div(J, IQ);
    1514             : }
    1515             : 
    1516             : /* return p * P^(-1)  [integral] */
    1517             : GEN
    1518       40287 : pidealprimeinv(GEN nf, GEN x)
    1519             : {
    1520       40287 :   if (pr_is_inert(x)) return matid(lg(gel(x,2)) - 1);
    1521       39776 :   return idealhnf_two(nf, mkvec2(gel(x,1), gel(x,5)));
    1522             : }
    1523             : 
    1524             : GEN
    1525       48717 : idealinv(GEN nf, GEN x)
    1526             : {
    1527             :   GEN res, ax;
    1528             :   pari_sp av;
    1529       48717 :   long tx = idealtyp(&x,&ax), N;
    1530             : 
    1531       48717 :   res = ax? cgetg(3,t_VEC): NULL;
    1532       48717 :   nf = checknf(nf); av = avma;
    1533       48717 :   N = nf_get_degree(nf);
    1534       48717 :   switch (tx)
    1535             :   {
    1536             :     case id_MAT:
    1537       28770 :       if (lg(x)-1 != N) pari_err_DIM("idealinv");
    1538       28770 :       x = idealinv_HNF(nf,x); break;
    1539             :     case id_PRINCIPAL:
    1540       18760 :       x = nf_to_scalar_or_basis(nf, x);
    1541       18760 :       if (typ(x) != t_COL)
    1542       18718 :         x = idealhnf_principal(nf,ginv(x));
    1543             :       else
    1544             :       { /* nfinv + idealhnf where we already know (x) \cap Z */
    1545             :         GEN c, d;
    1546          42 :         x = Q_remove_denom(x, &c);
    1547          42 :         x = zk_inv(nf, x);
    1548          42 :         x = Q_remove_denom(x, &d); /* true inverse is c/d * x */
    1549          42 :         if (!d) /* x and x^(-1) integral => x a unit */
    1550           7 :           x = scalarmat_shallow(c? c: gen_1, N);
    1551             :         else
    1552             :         {
    1553          35 :           c = c? gdiv(c,d): ginv(d);
    1554          35 :           x = zk_multable(nf, x);
    1555          35 :           x = ZM_Q_mul(ZM_hnfmodid(x,d), c);
    1556             :         }
    1557             :       }
    1558       18760 :       break;
    1559             :     case id_PRIME:
    1560        1187 :       x = RgM_Rg_div(pidealprimeinv(nf,x), pr_get_p(x));
    1561             :   }
    1562       48717 :   x = gerepileupto(av,x); if (!ax) return x;
    1563        4302 :   gel(res,1) = x;
    1564        4302 :   gel(res,2) = ext_inv(nf, ax); return res;
    1565             : }
    1566             : 
    1567             : /* write x = A/B, A,B coprime integral ideals */
    1568             : GEN
    1569       22249 : idealnumden(GEN nf, GEN x)
    1570             : {
    1571       22249 :   pari_sp av = avma;
    1572             :   GEN ax, c, d, A, B, J;
    1573       22249 :   long tx = idealtyp(&x,&ax);
    1574       22249 :   nf = checknf(nf);
    1575       22249 :   switch (tx)
    1576             :   {
    1577             :     case id_PRIME:
    1578           7 :       retmkvec2(idealhnf(nf, x), gen_1);
    1579             :     case id_PRINCIPAL:
    1580        1610 :       x = nf_to_scalar_or_basis(nf, x);
    1581        1610 :       switch(typ(x))
    1582             :       {
    1583             :         case t_INT:
    1584          56 :           return gerepilecopy(av, mkvec2(absi(x),gen_1));
    1585             :         case t_FRAC:
    1586          14 :           return gerepilecopy(av, mkvec2(absi(gel(x,1)), gel(x,2)));
    1587             :       }
    1588             :       /* t_COL */
    1589        1540 :       x = Q_remove_denom(x, &d);
    1590        1540 :       if (!d) return gerepilecopy(av, mkvec2(idealhnf(nf, x), gen_1));
    1591          14 :       x = idealhnf(nf, x);
    1592          14 :       break;
    1593             :     case id_MAT: {
    1594       20632 :       long n = lg(x)-1;
    1595       20632 :       if (n == 0) return mkvec2(gen_0, gen_1);
    1596       20632 :       if (n != nf_get_degree(nf)) pari_err_DIM("idealnumden");
    1597       20632 :       x = Q_remove_denom(x, &d);
    1598       20632 :       if (!d) return gerepilecopy(av, mkvec2(x, gen_1));
    1599          14 :       break;
    1600             :     }
    1601             :   }
    1602          28 :   J = hnfmodid(x, d); /* = d/B */
    1603          28 :   c = gcoeff(J,1,1); /* (d/B) \cap Z, divides d */
    1604          28 :   B = idealinv_HNF_Z(nf, J); /* (d/B \cap Z) B/d */
    1605          28 :   c = diviiexact(d, c);
    1606          28 :   if (!is_pm1(c)) B = ZM_Z_mul(B, c); /* = B ! */
    1607          28 :   A = idealmul(nf, x, B); /* d * (original x) * B = d A */
    1608          28 :   if (!is_pm1(d)) A = ZM_Z_divexact(A, d); /* = A ! */
    1609          28 :   if (is_pm1(gcoeff(B,1,1))) B = gen_1;
    1610          28 :   return gerepilecopy(av, mkvec2(A, B));
    1611             : }
    1612             : 
    1613             : /* Return x, integral in 2-elt form, such that pr^n = c * x. Assume n != 0.
    1614             :  * nf = true nf */
    1615             : static GEN
    1616      232923 : idealpowprime(GEN nf, GEN pr, GEN n, GEN *pc)
    1617             : {
    1618      232923 :   GEN p = pr_get_p(pr), q, gen;
    1619             : 
    1620      232923 :   *pc = NULL;
    1621      232923 :   if (is_pm1(n)) /* n = 1 special cased for efficiency */
    1622             :   {
    1623      112368 :     q = p;
    1624      112368 :     if (typ(pr_get_tau(pr)) == t_INT) /* inert */
    1625             :     {
    1626           0 :       *pc = (signe(n) >= 0)? p: ginv(p);
    1627           0 :       return mkvec2(gen_1,gen_0);
    1628             :     }
    1629      112368 :     if (signe(n) >= 0) gen = pr_get_gen(pr);
    1630             :     else
    1631             :     {
    1632        2310 :       gen = pr_get_tau(pr); /* possibly t_MAT */
    1633        2310 :       *pc = ginv(p);
    1634             :     }
    1635             :   }
    1636             :   else
    1637             :   {
    1638      120555 :     long e = pr_get_e(pr), f = pr_get_f(pr);
    1639      120555 :     GEN r, m = truedvmdis(n, e, &r);
    1640      120555 :     if (e * f == nf_get_degree(nf))
    1641             :     { /* pr^e = (p) */
    1642       85617 :       *pc = powii(p,m);
    1643       85617 :       if (!signe(r)) return mkvec2(gen_1,gen_0);
    1644       16492 :       q = p;
    1645       16492 :       gen = nfpow(nf, pr_get_gen(pr), r);
    1646             :     }
    1647             :     else
    1648             :     {
    1649       34938 :       m = absi(m);
    1650       34938 :       if (signe(r)) m = addiu(m,1);
    1651       34938 :       q = powii(p,m); /* m = ceil(|n|/e) */
    1652       34938 :       if (signe(n) >= 0) gen = nfpow(nf, pr_get_gen(pr), n);
    1653             :       else
    1654             :       {
    1655         252 :         gen = pr_get_tau(pr);
    1656         252 :         if (typ(gen) == t_MAT) gen = gel(gen,1);
    1657         252 :         n = negi(n);
    1658         252 :         gen = ZC_Z_divexact(nfpow(nf, gen, n), powii(p, subii(n,m)));
    1659         252 :         *pc = ginv(q);
    1660             :       }
    1661             :     }
    1662       51430 :     gen = FpC_red(gen, q);
    1663             :   }
    1664      163798 :   return mkvec2(q, gen);
    1665             : }
    1666             : 
    1667             : /* x * pr^n. Assume x in HNF or scalar (possibly non-integral) */
    1668             : GEN
    1669       37807 : idealmulpowprime(GEN nf, GEN x, GEN pr, GEN n)
    1670             : {
    1671             :   GEN c, cx, y;
    1672             :   long N;
    1673             : 
    1674       37807 :   nf = checknf(nf);
    1675       37807 :   N = nf_get_degree(nf);
    1676       37807 :   if (!signe(n)) return typ(x) == t_MAT? x: scalarmat_shallow(x, N);
    1677             : 
    1678             :   /* inert, special cased for efficiency */
    1679       37695 :   if (pr_is_inert(pr))
    1680             :   {
    1681        3073 :     GEN q = powii(pr_get_p(pr), n);
    1682        3073 :     return typ(x) == t_MAT? RgM_Rg_mul(x,q): scalarmat_shallow(gmul(x,q), N);
    1683             :   }
    1684             : 
    1685       34622 :   y = idealpowprime(nf, pr, n, &c);
    1686       34622 :   if (typ(x) == t_MAT)
    1687       34132 :   { x = Q_primitive_part(x, &cx); if (is_pm1(gcoeff(x,1,1))) x = NULL; }
    1688             :   else
    1689         490 :   { cx = x; x = NULL; }
    1690       34622 :   cx = mul_content(c,cx);
    1691       34622 :   if (x)
    1692       22295 :     x = idealmul_HNF_two(nf,x,y);
    1693             :   else
    1694       12327 :     x = idealhnf_two(nf,y);
    1695       34622 :   if (cx) x = RgM_Rg_mul(x,cx);
    1696       34622 :   return x;
    1697             : }
    1698             : GEN
    1699        4109 : idealdivpowprime(GEN nf, GEN x, GEN pr, GEN n)
    1700             : {
    1701        4109 :   return idealmulpowprime(nf,x,pr, negi(n));
    1702             : }
    1703             : 
    1704             : /* nf = true nf */
    1705             : static GEN
    1706      398172 : idealpow_aux(GEN nf, GEN x, long tx, GEN n)
    1707             : {
    1708      398172 :   GEN T = nf_get_pol(nf), m, cx, n1, a, alpha;
    1709      398172 :   long N = degpol(T), s = signe(n);
    1710      398172 :   if (!s) return matid(N);
    1711      395251 :   switch(tx)
    1712             :   {
    1713             :     case id_PRINCIPAL:
    1714          70 :       x = nf_to_scalar_or_alg(nf, x);
    1715          70 :       x = (typ(x) == t_POL)? RgXQ_pow(x,n,T): powgi(x,n);
    1716          70 :       return idealhnf_principal(nf,x);
    1717             :     case id_PRIME: {
    1718      300712 :       if (pr_is_inert(x)) return scalarmat(powii(gel(x,1), n), N);
    1719      198301 :       x = idealpowprime(nf, x, n, &cx);
    1720      198301 :       x = idealhnf_two(nf,x);
    1721      198301 :       return cx? RgM_Rg_mul(x, cx): x;
    1722             :     }
    1723             :     default:
    1724       94469 :       if (is_pm1(n)) return (s < 0)? idealinv(nf, x): gcopy(x);
    1725       53621 :       n1 = (s < 0)? negi(n): n;
    1726             : 
    1727       53621 :       x = Q_primitive_part(x, &cx);
    1728       53621 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    1729       53621 :       alpha = nfpow(nf,alpha,n1);
    1730       53621 :       m = zk_scalar_or_multable(nf, alpha);
    1731       53621 :       if (typ(m) == t_INT) {
    1732         252 :         x = gcdii(m, powii(a,n1));
    1733         252 :         if (s<0) x = ginv(x);
    1734         252 :         if (cx) x = gmul(x, powgi(cx,n));
    1735         252 :         x = scalarmat(x, N);
    1736             :       }
    1737             :       else {
    1738       53369 :         x = ZM_hnfmodid(m, powii(a,n1));
    1739       53369 :         if (cx) cx = powgi(cx,n);
    1740       53369 :         if (s<0) {
    1741           7 :           GEN xZ = gcoeff(x,1,1);
    1742           7 :           cx = cx ? gdiv(cx, xZ): ginv(xZ);
    1743           7 :           x = idealinv_HNF_Z(nf,x);
    1744             :         }
    1745       53369 :         if (cx) x = RgM_Rg_mul(x, cx);
    1746             :       }
    1747       53621 :       return x;
    1748             :   }
    1749             : }
    1750             : 
    1751             : /* raise the ideal x to the power n (in Z) */
    1752             : GEN
    1753      398172 : idealpow(GEN nf, GEN x, GEN n)
    1754             : {
    1755             :   pari_sp av;
    1756             :   long tx;
    1757             :   GEN res, ax;
    1758             : 
    1759      398172 :   if (typ(n) != t_INT) pari_err_TYPE("idealpow",n);
    1760      398172 :   tx = idealtyp(&x,&ax);
    1761      398172 :   res = ax? cgetg(3,t_VEC): NULL;
    1762      398172 :   av = avma;
    1763      398172 :   x = gerepileupto(av, idealpow_aux(checknf(nf), x, tx, n));
    1764      398172 :   if (!ax) return x;
    1765         289 :   ax = ext_pow(nf, ax, n);
    1766         289 :   gel(res,1) = x;
    1767         289 :   gel(res,2) = ax;
    1768         289 :   return res;
    1769             : }
    1770             : 
    1771             : /* Return ideal^e in number field nf. e is a C integer. */
    1772             : GEN
    1773        8050 : idealpows(GEN nf, GEN ideal, long e)
    1774             : {
    1775        8050 :   long court[] = {evaltyp(t_INT) | _evallg(3),0,0};
    1776        8050 :   affsi(e,court); return idealpow(nf,ideal,court);
    1777             : }
    1778             : 
    1779             : static GEN
    1780       44986 : _idealmulred(GEN nf, GEN x, GEN y)
    1781       44986 : { return idealred(nf,idealmul(nf,x,y)); }
    1782             : static GEN
    1783       65516 : _idealsqrred(GEN nf, GEN x)
    1784       65516 : { return idealred(nf,idealsqr(nf,x)); }
    1785             : static GEN
    1786       28584 : _mul(void *data, GEN x, GEN y) { return _idealmulred((GEN)data,x,y); }
    1787             : static GEN
    1788       65516 : _sqr(void *data, GEN x) { return _idealsqrred((GEN)data, x); }
    1789             : 
    1790             : /* compute x^n (x ideal, n integer), reducing along the way */
    1791             : GEN
    1792       71566 : idealpowred(GEN nf, GEN x, GEN n)
    1793             : {
    1794       71566 :   pari_sp av = avma;
    1795             :   long s;
    1796             :   GEN y;
    1797             : 
    1798       71566 :   if (typ(n) != t_INT) pari_err_TYPE("idealpowred",n);
    1799       71566 :   s = signe(n); if (s == 0) return idealpow(nf,x,n);
    1800       71277 :   y = gen_pow(x, n, (void*)nf, &_sqr, &_mul);
    1801             : 
    1802       71277 :   if (s < 0) y = idealinv(nf,y);
    1803       71277 :   if (s < 0 || is_pm1(n)) y = idealred(nf,y);
    1804       71277 :   return gerepileupto(av,y);
    1805             : }
    1806             : 
    1807             : GEN
    1808       16402 : idealmulred(GEN nf, GEN x, GEN y)
    1809             : {
    1810       16402 :   pari_sp av = avma;
    1811       16402 :   return gerepileupto(av, _idealmulred(nf,x,y));
    1812             : }
    1813             : 
    1814             : long
    1815          91 : isideal(GEN nf,GEN x)
    1816             : {
    1817          91 :   long N, i, j, lx, tx = typ(x);
    1818             :   pari_sp av;
    1819             :   GEN T, xZ;
    1820             : 
    1821          91 :   nf = checknf(nf); T = nf_get_pol(nf); lx = lg(x);
    1822          91 :   if (tx==t_VEC && lx==3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
    1823          91 :   switch(tx)
    1824             :   {
    1825          14 :     case t_INT: case t_FRAC: return 1;
    1826           7 :     case t_POL: return varn(x) == varn(T);
    1827           7 :     case t_POLMOD: return RgX_equal_var(T, gel(x,1));
    1828          14 :     case t_VEC: return get_prid(x)? 1 : 0;
    1829          42 :     case t_MAT: break;
    1830           7 :     default: return 0;
    1831             :   }
    1832          42 :   N = degpol(T);
    1833          42 :   if (lx-1 != N) return (lx == 1);
    1834          28 :   if (nbrows(x) != N) return 0;
    1835             : 
    1836          28 :   av = avma; x = Q_primpart(x);
    1837          28 :   if (!ZM_ishnf(x)) return 0;
    1838          14 :   xZ = gcoeff(x,1,1);
    1839          21 :   for (j=2; j<=N; j++)
    1840          14 :     if (!dvdii(xZ, gcoeff(x,j,j))) { avma = av; return 0; }
    1841          14 :   for (i=2; i<=N; i++)
    1842          14 :     for (j=2; j<=N; j++)
    1843           7 :       if (! hnf_invimage(x, zk_ei_mul(nf,gel(x,i),j))) { avma = av; return 0; }
    1844           7 :   avma=av; return 1;
    1845             : }
    1846             : 
    1847             : GEN
    1848       15211 : idealdiv(GEN nf, GEN x, GEN y)
    1849             : {
    1850       15211 :   pari_sp av = avma, tetpil;
    1851       15211 :   GEN z = idealinv(nf,y);
    1852       15211 :   tetpil = avma; return gerepile(av,tetpil, idealmul(nf,x,z));
    1853             : }
    1854             : 
    1855             : /* This routine computes the quotient x/y of two ideals in the number field nf.
    1856             :  * It assumes that the quotient is an integral ideal.  The idea is to find an
    1857             :  * ideal z dividing y such that gcd(Nx/Nz, Nz) = 1.  Then
    1858             :  *
    1859             :  *   x + (Nx/Nz)    x
    1860             :  *   ----------- = ---
    1861             :  *   y + (Ny/Nz)    y
    1862             :  *
    1863             :  * Proof: we can assume x and y are integral. Let p be any prime ideal
    1864             :  *
    1865             :  * If p | Nz, then it divides neither Nx/Nz nor Ny/Nz (since Nx/Nz is the
    1866             :  * product of the integers N(x/y) and N(y/z)).  Both the numerator and the
    1867             :  * denominator on the left will be coprime to p.  So will x/y, since x/y is
    1868             :  * assumed integral and its norm N(x/y) is coprime to p.
    1869             :  *
    1870             :  * If instead p does not divide Nz, then v_p (Nx/Nz) = v_p (Nx) >= v_p(x).
    1871             :  * Hence v_p (x + Nx/Nz) = v_p(x).  Likewise for the denominators.  QED.
    1872             :  *
    1873             :  *                Peter Montgomery.  July, 1994. */
    1874             : static void
    1875           7 : err_divexact(GEN x, GEN y)
    1876           7 : { pari_err_DOMAIN("idealdivexact","denominator(x/y)", "!=",
    1877           0 :                   gen_1,mkvec2(x,y)); }
    1878             : GEN
    1879        2100 : idealdivexact(GEN nf, GEN x0, GEN y0)
    1880             : {
    1881        2100 :   pari_sp av = avma;
    1882             :   GEN x, y, yZ, Nx, Ny, Nz, cy, q, r;
    1883             : 
    1884        2100 :   nf = checknf(nf);
    1885        2100 :   x = idealhnf_shallow(nf, x0);
    1886        2100 :   y = idealhnf_shallow(nf, y0);
    1887        2100 :   if (lg(y) == 1) pari_err_INV("idealdivexact", y0);
    1888        2093 :   if (lg(x) == 1) { avma = av; return cgetg(1, t_MAT); } /* numerator is zero */
    1889        2093 :   y = Q_primitive_part(y, &cy);
    1890        2093 :   if (cy) x = RgM_Rg_div(x,cy);
    1891        2093 :   Nx = idealnorm(nf,x);
    1892        2093 :   Ny = idealnorm(nf,y);
    1893        2093 :   if (typ(Nx) != t_INT) err_divexact(x,y);
    1894        2086 :   q = dvmdii(Nx,Ny, &r);
    1895        2086 :   if (signe(r)) err_divexact(x,y);
    1896        2086 :   if (is_pm1(q)) { avma = av; return matid(nf_get_degree(nf)); }
    1897             :   /* Find a norm Nz | Ny such that gcd(Nx/Nz, Nz) = 1 */
    1898        1883 :   for (Nz = Ny;;) /* q = Nx/Nz */
    1899             :   {
    1900        2569 :     GEN p1 = gcdii(Nz, q);
    1901        2569 :     if (is_pm1(p1)) break;
    1902         686 :     Nz = diviiexact(Nz,p1);
    1903         686 :     q = mulii(q,p1);
    1904         686 :   }
    1905             :   /* Replace x/y  by  x+(Nx/Nz) / y+(Ny/Nz) */
    1906        1883 :   x = ZM_hnfmodid(x, q);
    1907             :   /* y reduced to unit ideal ? */
    1908        1883 :   if (Nz == Ny) return gerepileupto(av, x);
    1909             : 
    1910         490 :   y = ZM_hnfmodid(y, diviiexact(Ny,Nz));
    1911         490 :   yZ = gcoeff(y,1,1);
    1912         490 :   y = idealmul_HNF(nf,x, idealinv_HNF_Z(nf,y));
    1913         490 :   return gerepileupto(av, RgM_Rg_div(y, yZ));
    1914             : }
    1915             : 
    1916             : GEN
    1917          21 : idealintersect(GEN nf, GEN x, GEN y)
    1918             : {
    1919          21 :   pari_sp av = avma;
    1920             :   long lz, lx, i;
    1921             :   GEN z, dx, dy, xZ, yZ;;
    1922             : 
    1923          21 :   nf = checknf(nf);
    1924          21 :   x = idealhnf_shallow(nf,x);
    1925          21 :   y = idealhnf_shallow(nf,y);
    1926          21 :   if (lg(x) == 1 || lg(y) == 1) { avma = av; return cgetg(1,t_MAT); }
    1927          14 :   x = Q_remove_denom(x, &dx);
    1928          14 :   y = Q_remove_denom(y, &dy);
    1929          14 :   if (dx) y = ZM_Z_mul(y, dx);
    1930          14 :   if (dy) x = ZM_Z_mul(x, dy);
    1931          14 :   xZ = gcoeff(x,1,1);
    1932          14 :   yZ = gcoeff(y,1,1);
    1933          14 :   dx = mul_denom(dx,dy);
    1934          14 :   z = ZM_lll(shallowconcat(x,y), 0.99, LLL_KER); lz = lg(z);
    1935          14 :   lx = lg(x);
    1936          14 :   for (i=1; i<lz; i++) setlg(z[i], lx);
    1937          14 :   z = ZM_hnfmodid(ZM_mul(x,z), lcmii(xZ, yZ));
    1938          14 :   if (dx) z = RgM_Rg_div(z,dx);
    1939          14 :   return gerepileupto(av,z);
    1940             : }
    1941             : 
    1942             : /*******************************************************************/
    1943             : /*                                                                 */
    1944             : /*                      T2-IDEAL REDUCTION                         */
    1945             : /*                                                                 */
    1946             : /*******************************************************************/
    1947             : 
    1948             : static GEN
    1949          21 : chk_vdir(GEN nf, GEN vdir)
    1950             : {
    1951          21 :   long i, l = lg(vdir);
    1952             :   GEN v;
    1953          21 :   if (l != lg(nf_get_roots(nf))) pari_err_DIM("idealred");
    1954          14 :   switch(typ(vdir))
    1955             :   {
    1956           0 :     case t_VECSMALL: return vdir;
    1957          14 :     case t_VEC: break;
    1958           0 :     default: pari_err_TYPE("idealred",vdir);
    1959             :   }
    1960          14 :   v = cgetg(l, t_VECSMALL);
    1961          14 :   for (i = 1; i < l; i++) v[i] = itos(gceil(gel(vdir,i)));
    1962          14 :   return v;
    1963             : }
    1964             : 
    1965             : static void
    1966       26869 : twistG(GEN G, long r1, long i, long v)
    1967             : {
    1968       26869 :   long j, lG = lg(G);
    1969       26869 :   if (i <= r1) {
    1970       24534 :     for (j=1; j<lG; j++) gcoeff(G,i,j) = gmul2n(gcoeff(G,i,j), v);
    1971             :   } else {
    1972        2335 :     long k = (i<<1) - r1;
    1973       12741 :     for (j=1; j<lG; j++)
    1974             :     {
    1975       10406 :       gcoeff(G,k-1,j) = gmul2n(gcoeff(G,k-1,j), v);
    1976       10406 :       gcoeff(G,k  ,j) = gmul2n(gcoeff(G,k  ,j), v);
    1977             :     }
    1978             :   }
    1979       26869 : }
    1980             : 
    1981             : GEN
    1982      169766 : nf_get_Gtwist(GEN nf, GEN vdir)
    1983             : {
    1984             :   long i, l, v, r1;
    1985             :   GEN G;
    1986             : 
    1987      169766 :   if (!vdir) return nf_get_roundG(nf);
    1988        2849 :   if (typ(vdir) == t_MAT)
    1989             :   {
    1990        2828 :     long N = nf_get_degree(nf);
    1991        2828 :     if (lg(vdir) != N+1 || lgcols(vdir) != N+1) pari_err_DIM("idealred");
    1992        2828 :     return vdir;
    1993             :   }
    1994          21 :   vdir = chk_vdir(nf, vdir);
    1995          14 :   G = RgM_shallowcopy(nf_get_G(nf));
    1996          14 :   r1 = nf_get_r1(nf);
    1997          14 :   l = lg(vdir);
    1998          56 :   for (i=1; i<l; i++)
    1999             :   {
    2000          42 :     v = vdir[i]; if (!v) continue;
    2001          42 :     twistG(G, r1, i, v);
    2002             :   }
    2003          14 :   return RM_round_maxrank(G);
    2004             : }
    2005             : GEN
    2006       26827 : nf_get_Gtwist1(GEN nf, long i)
    2007             : {
    2008       26827 :   GEN G = RgM_shallowcopy( nf_get_G(nf) );
    2009       26827 :   long r1 = nf_get_r1(nf);
    2010       26827 :   twistG(G, r1, i, 10);
    2011       26827 :   return RM_round_maxrank(G);
    2012             : }
    2013             : 
    2014             : GEN
    2015       31951 : RM_round_maxrank(GEN G0)
    2016             : {
    2017       31951 :   long e, r = lg(G0)-1;
    2018       31951 :   pari_sp av = avma;
    2019       31951 :   GEN G = G0;
    2020       31951 :   for (e = 4; ; e <<= 1)
    2021             :   {
    2022       31951 :     GEN H = ground(G);
    2023       63902 :     if (ZM_rank(H) == r) return H; /* maximal rank ? */
    2024           0 :     avma = av;
    2025           0 :     G = gmul2n(G0, e);
    2026           0 :   }
    2027             : }
    2028             : 
    2029             : GEN
    2030      169759 : idealred0(GEN nf, GEN I, GEN vdir)
    2031             : {
    2032      169759 :   pari_sp av = avma;
    2033      169759 :   GEN G, aI, IZ, J, y, yZ, my, c1 = NULL;
    2034             :   long N;
    2035             : 
    2036      169759 :   nf = checknf(nf);
    2037      169759 :   N = nf_get_degree(nf);
    2038             :   /* put first for sanity checks, unused when I obviously principal */
    2039      169759 :   G = nf_get_Gtwist(nf, vdir);
    2040      169752 :   switch (idealtyp(&I,&aI))
    2041             :   {
    2042             :     case id_PRIME:
    2043       38883 :       if (pr_is_inert(I)) {
    2044         581 :         if (!aI) { avma = av; return matid(N); }
    2045         581 :         c1 = gel(I,1); I = matid(N);
    2046         581 :         goto END;
    2047             :       }
    2048       38302 :       IZ = pr_get_p(I);
    2049       38302 :       J = pidealprimeinv(nf,I);
    2050       38302 :       I = idealhnf_two(nf,I);
    2051       38302 :       break;
    2052             :     case id_MAT:
    2053      130855 :       I = Q_primitive_part(I, &c1);
    2054      130855 :       IZ = gcoeff(I,1,1);
    2055      130855 :       if (is_pm1(IZ))
    2056             :       {
    2057        7232 :         if (!aI) { avma = av; return matid(N); }
    2058        7232 :         goto END;
    2059             :       }
    2060      123623 :       J = idealinv_HNF_Z(nf, I);
    2061      123623 :       break;
    2062             :     default: /* id_PRINCIPAL, silly case */
    2063          14 :       if (gequal0(I)) I = cgetg(1,t_MAT); else { c1 = I; I = matid(N); }
    2064          14 :       if (!aI) return I;
    2065           7 :       goto END;
    2066             :   }
    2067             :   /* now I integral, HNF; and J = (I\cap Z) I^(-1), integral */
    2068      161925 :   y = idealpseudomin(J, G); /* small elt in (I\cap Z)I^(-1), integral */
    2069      161925 :   if (ZV_isscalar(y))
    2070             :   { /* already reduced */
    2071       62011 :     if (!aI) return gerepilecopy(av, I);
    2072       61941 :     goto END;
    2073             :   }
    2074             : 
    2075       99914 :   my = zk_multable(nf, y);
    2076       99914 :   I = ZM_Z_divexact(ZM_mul(my, I), IZ); /* y I / (I\cap Z), integral */
    2077       99914 :   c1 = mul_content(c1, IZ);
    2078       99914 :   my = ZM_gauss(my, col_ei(N,1)); /* y^-1 */
    2079       99914 :   yZ = Q_denom(my); /* (y) \cap Z */
    2080       99914 :   I = hnfmodid(I, yZ);
    2081       99914 :   if (!aI) return gerepileupto(av, I);
    2082       99893 :   c1 = RgC_Rg_mul(my, c1);
    2083             : END:
    2084      169654 :   if (c1) aI = ext_mul(nf, aI,c1);
    2085      169654 :   return gerepilecopy(av, mkvec2(I, aI));
    2086             : }
    2087             : 
    2088             : GEN
    2089           7 : idealmin(GEN nf, GEN x, GEN vdir)
    2090             : {
    2091           7 :   pari_sp av = avma;
    2092             :   GEN y, dx;
    2093           7 :   nf = checknf(nf);
    2094           7 :   switch( idealtyp(&x,&y) )
    2095             :   {
    2096           0 :     case id_PRINCIPAL: return gcopy(x);
    2097           0 :     case id_PRIME: x = idealhnf_two(nf,x); break;
    2098           7 :     case id_MAT: if (lg(x) == 1) return gen_0;
    2099             :   }
    2100           7 :   x = Q_remove_denom(x, &dx);
    2101           7 :   y = idealpseudomin(x, nf_get_Gtwist(nf,vdir));
    2102           7 :   if (dx) y = RgC_Rg_div(y, dx);
    2103           7 :   return gerepileupto(av, y);
    2104             : }
    2105             : 
    2106             : /*******************************************************************/
    2107             : /*                                                                 */
    2108             : /*                   APPROXIMATION THEOREM                         */
    2109             : /*                                                                 */
    2110             : /*******************************************************************/
    2111             : /* a = ppi(a,b) ppo(a,b), where ppi regroups primes common to a and b
    2112             :  * and ppo(a,b) = coprime_part(a,b) */
    2113             : /* return gcd(a,b),ppi(a,b),ppo(a,b) */
    2114             : GEN
    2115      451654 : Z_ppio(GEN a, GEN b)
    2116             : {
    2117      451654 :   GEN x, y, d = gcdii(a,b);
    2118      451654 :   if (is_pm1(d)) return mkvec3(gen_1, gen_1, a);
    2119      343504 :   x = d; y = diviiexact(a,d);
    2120             :   for(;;)
    2121             :   {
    2122      405951 :     GEN g = gcdii(x,y);
    2123      405951 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2124       62447 :     x = mulii(x,g); y = diviiexact(y,g);
    2125       62447 :   }
    2126             : }
    2127             : /* a = ppg(a,b)pple(a,b), where ppg regroups primes such that v(a) > v(b)
    2128             :  * and pple all others */
    2129             : /* return gcd(a,b),ppg(a,b),pple(a,b) */
    2130             : GEN
    2131           0 : Z_ppgle(GEN a, GEN b)
    2132             : {
    2133           0 :   GEN x, y, g, d = gcdii(a,b);
    2134           0 :   if (equalii(a, d)) return mkvec3(a, gen_1, a);
    2135           0 :   x = diviiexact(a,d); y = d;
    2136             :   for(;;)
    2137             :   {
    2138           0 :     g = gcdii(x,y);
    2139           0 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2140           0 :     x = mulii(x,g); y = diviiexact(y,g);
    2141           0 :   }
    2142             : }
    2143             : static void
    2144           0 : Z_dcba_rec(GEN L, GEN a, GEN b)
    2145             : {
    2146             :   GEN x, r, v, g, h, c, c0;
    2147             :   long n;
    2148           0 :   if (is_pm1(b)) {
    2149           0 :     if (!is_pm1(a)) vectrunc_append(L, a);
    2150           0 :     return;
    2151             :   }
    2152           0 :   v = Z_ppio(a,b);
    2153           0 :   a = gel(v,2);
    2154           0 :   r = gel(v,3);
    2155           0 :   if (!is_pm1(r)) vectrunc_append(L, r);
    2156           0 :   v = Z_ppgle(a,b);
    2157           0 :   g = gel(v,1);
    2158           0 :   h = gel(v,2);
    2159           0 :   x = c0 = gel(v,3);
    2160           0 :   for (n = 1; !is_pm1(h); n++)
    2161             :   {
    2162             :     GEN d, y;
    2163             :     long i;
    2164           0 :     v = Z_ppgle(h,sqri(g));
    2165           0 :     g = gel(v,1);
    2166           0 :     h = gel(v,2);
    2167           0 :     c = gel(v,3); if (is_pm1(c)) continue;
    2168           0 :     d = gcdii(c,b);
    2169           0 :     x = mulii(x,d);
    2170           0 :     y = d; for (i=1; i < n; i++) y = sqri(y);
    2171           0 :     Z_dcba_rec(L, diviiexact(c,y), d);
    2172             :   }
    2173           0 :   Z_dcba_rec(L,diviiexact(b,x), c0);
    2174             : }
    2175             : static GEN
    2176     3058286 : Z_cba_rec(GEN L, GEN a, GEN b)
    2177             : {
    2178             :   GEN g;
    2179     3058286 :   if (lg(L) > 10)
    2180             :   { /* a few naive steps before switching to dcba */
    2181           0 :     Z_dcba_rec(L, a, b);
    2182           0 :     return gel(L, lg(L)-1);
    2183             :   }
    2184     3058286 :   if (is_pm1(a)) return b;
    2185     1817060 :   g = gcdii(a,b);
    2186     1817060 :   if (is_pm1(g)) { vectrunc_append(L, a); return b; }
    2187     1357391 :   a = diviiexact(a,g);
    2188     1357391 :   b = diviiexact(b,g);
    2189     1357391 :   return Z_cba_rec(L, Z_cba_rec(L, a, g), b);
    2190             : }
    2191             : GEN
    2192      343504 : Z_cba(GEN a, GEN b)
    2193             : {
    2194      343504 :   GEN L = vectrunc_init(expi(a) + expi(b) + 2);
    2195      343504 :   GEN t = Z_cba_rec(L, a, b);
    2196      343504 :   if (!is_pm1(t)) vectrunc_append(L, t);
    2197      343504 :   return L;
    2198             : }
    2199             : 
    2200             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2201             : GEN
    2202     1104115 : coprime_part(GEN x, GEN f)
    2203             : {
    2204             :   for (;;)
    2205             :   {
    2206     1104115 :     f = gcdii(x, f); if (is_pm1(f)) break;
    2207      751708 :     x = diviiexact(x, f);
    2208      751708 :   }
    2209      352407 :   return x;
    2210             : }
    2211             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2212             : ulong
    2213      273245 : ucoprime_part(ulong x, ulong f)
    2214             : {
    2215             :   for (;;)
    2216             :   {
    2217      273245 :     f = ugcd(x, f); if (f == 1) break;
    2218      111398 :     x /= f;
    2219      111398 :   }
    2220      161847 :   return x;
    2221             : }
    2222             : 
    2223             : /* x t_INT, f ideal. Write x = x1 x2, sqf(x1) | f, (x2,f) = 1. Return x2 */
    2224             : static GEN
    2225          14 : nf_coprime_part(GEN nf, GEN x, GEN listpr)
    2226             : {
    2227          14 :   long v, j, lp = lg(listpr), N = nf_get_degree(nf);
    2228             :   GEN x1, x2, ex;
    2229             : 
    2230             : #if 0 /*1) via many gcds. Expensive ! */
    2231             :   GEN f = idealprodprime(nf, listpr);
    2232             :   f = ZM_hnfmodid(f, x); /* first gcd is less expensive since x in Z */
    2233             :   x = scalarmat(x, N);
    2234             :   for (;;)
    2235             :   {
    2236             :     if (gequal1(gcoeff(f,1,1))) break;
    2237             :     x = idealdivexact(nf, x, f);
    2238             :     f = ZM_hnfmodid(shallowconcat(f,x), gcoeff(x,1,1)); /* gcd(f,x) */
    2239             :   }
    2240             :   x2 = x;
    2241             : #else /*2) from prime decomposition */
    2242          14 :   x1 = NULL;
    2243          35 :   for (j=1; j<lp; j++)
    2244             :   {
    2245          21 :     GEN pr = gel(listpr,j);
    2246          21 :     v = Z_pval(x, pr_get_p(pr)); if (!v) continue;
    2247             : 
    2248          14 :     ex = muluu(v, pr_get_e(pr)); /* = v_pr(x) > 0 */
    2249          14 :     x1 = x1? idealmulpowprime(nf, x1, pr, ex)
    2250          14 :            : idealpow(nf, pr, ex);
    2251             :   }
    2252          14 :   x = scalarmat(x, N);
    2253          14 :   x2 = x1? idealdivexact(nf, x, x1): x;
    2254             : #endif
    2255          14 :   return x2;
    2256             : }
    2257             : 
    2258             : /* L0 in K^*, assume (L0,f) = 1. Return L integral, L0 = L mod f  */
    2259             : GEN
    2260        3381 : make_integral(GEN nf, GEN L0, GEN f, GEN listpr)
    2261             : {
    2262             :   GEN fZ, t, L, D2, d1, d2, d;
    2263             : 
    2264        3381 :   L = Q_remove_denom(L0, &d);
    2265        3381 :   if (!d) return L0;
    2266             : 
    2267             :   /* L0 = L / d, L integral */
    2268        1841 :   fZ = gcoeff(f,1,1);
    2269        1841 :   if (typ(L) == t_INT) return Fp_mul(L, Fp_inv(d, fZ), fZ);
    2270             :   /* Kill denom part coprime to fZ */
    2271        1617 :   d2 = coprime_part(d, fZ);
    2272        1617 :   t = Fp_inv(d2, fZ); if (!is_pm1(t)) L = ZC_Z_mul(L,t);
    2273        1617 :   if (equalii(d, d2)) return L;
    2274             : 
    2275          14 :   d1 = diviiexact(d, d2);
    2276             :   /* L0 = (L / d1) mod f. d1 not coprime to f
    2277             :    * write (d1) = D1 D2, D2 minimal, (D2,f) = 1. */
    2278          14 :   D2 = nf_coprime_part(nf, d1, listpr);
    2279          14 :   t = idealaddtoone_i(nf, D2, f); /* in D2, 1 mod f */
    2280          14 :   L = nfmuli(nf,t,L);
    2281             : 
    2282             :   /* if (L0, f) = 1, then L in D1 ==> in D1 D2 = (d1) */
    2283          14 :   return Q_div_to_int(L, d1); /* exact division */
    2284             : }
    2285             : 
    2286             : /* assume L is a list of prime ideals. Return the product */
    2287             : GEN
    2288         126 : idealprodprime(GEN nf, GEN L)
    2289             : {
    2290         126 :   long l = lg(L), i;
    2291             :   GEN z;
    2292         126 :   if (l == 1) return matid(nf_get_degree(nf));
    2293         126 :   z = idealhnf_two(nf, gel(L,1));
    2294         126 :   for (i=2; i<l; i++) z = idealmul_HNF_two(nf,z, gel(L,i));
    2295         126 :   return z;
    2296             : }
    2297             : 
    2298             : GEN
    2299         763 : idealprod(GEN nf, GEN I)
    2300             : {
    2301         763 :   long i, l = lg(I);
    2302             :   GEN z;
    2303         763 :   if (l == 1) return matid(nf_get_degree(nf));
    2304         756 :   z = gel(I,1);
    2305         756 :   for (i=2; i<l; i++) z = idealmul(nf, z, gel(I,i));
    2306         756 :   if (typ(z) != t_MAT) z = idealhnf(nf, z);
    2307         756 :   return z;
    2308             : }
    2309             : 
    2310             : /* assume L is a list of prime ideals. Return prod L[i]^e[i] */
    2311             : GEN
    2312        6013 : factorbackprime(GEN nf, GEN L, GEN e)
    2313             : {
    2314        6013 :   long l = lg(L), i;
    2315             :   GEN z;
    2316             : 
    2317        6013 :   if (l == 1) return matid(nf_get_degree(nf));
    2318        6013 :   z = idealpow(nf, gel(L,1), gel(e,1));
    2319       11774 :   for (i=2; i<l; i++)
    2320        5761 :     if (signe(gel(e,i))) z = idealmulpowprime(nf,z, gel(L,i),gel(e,i));
    2321        6013 :   return z;
    2322             : }
    2323             : 
    2324             : /* F in Z squarefree, multiple of p. Return F-uniformizer for pr/p */
    2325             : GEN
    2326       24636 : unif_mod_fZ(GEN pr, GEN F)
    2327             : {
    2328       24636 :   GEN p = pr_get_p(pr), t = pr_get_gen(pr);
    2329       24636 :   if (!equalii(F, p))
    2330             :   {
    2331        8029 :     GEN u, v, q, a = diviiexact(F,p);
    2332        8029 :     q = (pr_get_e(pr) == 1)? sqri(p): p;
    2333        8029 :     if (!gequal1(bezout(q, a, &u,&v))) pari_err_BUG("unif_mod_fZ");
    2334        8029 :     u = mulii(u,q);
    2335        8029 :     v = mulii(v,a);
    2336        8029 :     t = ZC_Z_mul(t, v);
    2337        8029 :     gel(t,1) = addii(gel(t,1), u); /* return u + vt */
    2338             :   }
    2339       24636 :   return t;
    2340             : }
    2341             : /* L = list of prime ideals, return lcm_i (L[i] \cap \ZM) */
    2342             : GEN
    2343       22340 : init_unif_mod_fZ(GEN L)
    2344             : {
    2345       22340 :   long i, r = lg(L);
    2346       22340 :   GEN pr, p, F = gen_1;
    2347       55915 :   for (i = 1; i < r; i++)
    2348             :   {
    2349       33575 :     pr = gel(L,i); p = pr_get_p(pr);
    2350       33575 :     if (!dvdii(F, p)) F = mulii(F,p);
    2351             :   }
    2352       22340 :   return F;
    2353             : }
    2354             : 
    2355             : void
    2356           0 : check_listpr(GEN x)
    2357             : {
    2358           0 :   long l = lg(x), i;
    2359           0 :   for (i=1; i<l; i++) checkprid(gel(x,i));
    2360           0 : }
    2361             : 
    2362             : /* Given a prime ideal factorization with possibly zero or negative
    2363             :  * exponents, gives b such that v_p(b) = v_p(x) for all prime ideals pr | x
    2364             :  * and v_pr(b)> = 0 for all other pr.
    2365             :  * For optimal performance, all [anti-]uniformizers should be precomputed,
    2366             :  * but no support for this yet.
    2367             :  *
    2368             :  * If nored, do not reduce result.
    2369             :  * No garbage collecting */
    2370             : static GEN
    2371       17167 : idealapprfact_i(GEN nf, GEN x, int nored)
    2372             : {
    2373             :   GEN z, d, L, e, e2, F;
    2374             :   long i, r;
    2375             :   int flagden;
    2376             : 
    2377       17167 :   nf = checknf(nf);
    2378       17167 :   L = gel(x,1);
    2379       17167 :   e = gel(x,2);
    2380       17167 :   F = init_unif_mod_fZ(L);
    2381       17167 :   flagden = 0;
    2382       17167 :   z = NULL; r = lg(e);
    2383       45142 :   for (i = 1; i < r; i++)
    2384             :   {
    2385       27975 :     long s = signe(gel(e,i));
    2386             :     GEN pi, q;
    2387       27975 :     if (!s) continue;
    2388       23873 :     if (s < 0) flagden = 1;
    2389       23873 :     pi = unif_mod_fZ(gel(L,i), F);
    2390       23873 :     q = nfpow(nf, pi, gel(e,i));
    2391       23873 :     z = z? nfmul(nf, z, q): q;
    2392             :   }
    2393       17167 :   if (!z) return scalarcol_shallow(gen_1, nf_get_degree(nf));
    2394       13492 :   if (nored)
    2395             :   {
    2396       10895 :     if (flagden) pari_err_IMPL("nored + denominator in idealapprfact");
    2397       10895 :     return z;
    2398             :   }
    2399        2597 :   e2 = cgetg(r, t_VEC);
    2400        2597 :   for (i=1; i<r; i++) gel(e2,i) = addis(gel(e,i), 1);
    2401        2597 :   x = factorbackprime(nf, L,e2);
    2402        2597 :   if (flagden) /* denominator */
    2403             :   {
    2404        1043 :     z = Q_remove_denom(z, &d);
    2405        1043 :     d = diviiexact(d, coprime_part(d, F));
    2406        1043 :     x = RgM_Rg_mul(x, d);
    2407             :   }
    2408             :   else
    2409        1554 :     d = NULL;
    2410        2597 :   z = ZC_reducemodlll(z, x);
    2411        2597 :   return d? RgC_Rg_div(z,d): z;
    2412             : }
    2413             : 
    2414             : GEN
    2415         763 : idealapprfact(GEN nf, GEN x) {
    2416         763 :   pari_sp av = avma;
    2417         763 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    2418             : }
    2419             : GEN
    2420         791 : idealappr(GEN nf, GEN x) {
    2421         791 :   pari_sp av = avma;
    2422         791 :   if (!is_nf_extfactor(x)) x = idealfactor(nf, x);
    2423         791 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    2424             : }
    2425             : 
    2426             : /* OBSOLETE */
    2427             : GEN
    2428           0 : idealappr0(GEN nf, GEN x, long fl) { (void)fl; return idealappr(nf, x); }
    2429             : 
    2430             : /* merge a^e b^f. Assume a and b sorted. Keep 0 exponents and *append* new
    2431             :  * entries from b [ result not sorted ] */
    2432             : static void
    2433          21 : merge_fact(GEN *pa, GEN *pe, GEN b, GEN f)
    2434             : {
    2435          21 :   GEN A, E, a = *pa, e = *pe;
    2436          21 :   long k, i, la = lg(a), lb = lg(b), l = la+lb-1;
    2437             : 
    2438          21 :   *pa = A = cgetg(l, t_COL);
    2439          21 :   *pe = E = cgetg(l, t_COL);
    2440          21 :   k = 1;
    2441         105 :   for (i=1; i<la; i++)
    2442             :   {
    2443          84 :     gel(A,i) = gel(a,i);
    2444          84 :     gel(E,i) = gel(e,i);
    2445          84 :     if (k < lb && gequal(gel(A,i), gel(b,k)))
    2446             :     {
    2447          28 :       gel(E,i) = addii(gel(E,i), gel(f,k));
    2448          28 :       k++;
    2449             :     }
    2450             :   }
    2451          28 :   for (; k < lb; i++,k++)
    2452             :   {
    2453           7 :     gel(A,i) = gel(b,k);
    2454           7 :     gel(E,i) = gel(f,k);
    2455             :   }
    2456          21 :   setlg(A, i);
    2457          21 :   setlg(E, i);
    2458          21 : }
    2459             : 
    2460             : static int
    2461        1316 : isprfact(GEN x)
    2462             : {
    2463             :   long i, l;
    2464             :   GEN L, E;
    2465        1316 :   if (typ(x) != t_MAT || lg(x) != 3) return 0;
    2466        1316 :   L = gel(x,1); l = lg(L);
    2467        1316 :   E = gel(x,2);
    2468        3234 :   for(i=1; i<l; i++)
    2469             :   {
    2470        1918 :     checkprid(gel(L,i));
    2471        1918 :     if (typ(gel(E,i)) != t_INT) return 0;
    2472             :   }
    2473        1316 :   return 1;
    2474             : }
    2475             : 
    2476             : /* initialize projectors mod pr[i]^e[i] for idealchinese */
    2477             : static GEN
    2478        1316 : pr_init(GEN nf, GEN fa, GEN w, GEN dw)
    2479             : {
    2480        1316 :   GEN L = gel(fa,1), E = gel(fa,2);
    2481        1316 :   long r = lg(L);
    2482             : 
    2483        1316 :   if (w && lg(w) != r) pari_err_TYPE("idealchinese", w);
    2484        1316 :   if (r > 1)
    2485             :   {
    2486             :     GEN U, F;
    2487             :     long i;
    2488        1316 :     if (dw)
    2489             :     {
    2490          21 :       GEN p = gen_indexsort(L, (void*)&cmp_prime_ideal, cmp_nodata);
    2491          21 :       GEN fw = idealfactor(nf, dw); /* sorted */
    2492          21 :       L = vecpermute(L, p);
    2493          21 :       E = vecpermute(E, p);
    2494          21 :       w = vecpermute(w, p);
    2495          21 :       merge_fact(&L, &E, gel(fw,1), gel(fw,2));
    2496             :       /* L and E lenghtened, with factors of dw coming last */
    2497             :     }
    2498             :     else
    2499        1295 :       E = leafcopy(E); /* do not destroy fa[2] */
    2500             : 
    2501        3234 :     for (i=1; i<r; i++)
    2502        1918 :       if (signe(gel(E,i)) < 0) gel(E,i) = gen_0;
    2503        1316 :     F = factorbackprime(nf, L, E);
    2504        1316 :     U = cgetg(r, t_VEC);
    2505        3234 :     for (i = 1; i < r; i++)
    2506             :     {
    2507             :       GEN u;
    2508        1918 :       if (w && gequal0(gel(w,i))) u = gen_0; /* unused */
    2509             :       else
    2510             :       {
    2511        1862 :         GEN pr = gel(L,i), e = gel(E,i), t;
    2512        1862 :         t = idealdivpowprime(nf,F, pr, e);
    2513        1862 :         u = hnfmerge_get_1(t, idealpow(nf, pr, e));
    2514        1862 :         if (!u) pari_err_COPRIME("idealchinese", t,pr);
    2515             :       }
    2516        1918 :       gel(U,i) = u;
    2517             :     }
    2518        1316 :     F = idealpseudored(F, nf_get_roundG(nf));
    2519        1316 :     fa = mkvec2(F, U);
    2520             :   }
    2521             :   else
    2522           0 :     fa = cgetg(1,t_VEC);
    2523        1316 :   return fa;
    2524             : }
    2525             : 
    2526             : static GEN
    2527         595 : pl_normalize(GEN nf, GEN pl)
    2528             : {
    2529         595 :   const char *fun = "idealchinese";
    2530         595 :   if (lg(pl)-1 != nf_get_r1(nf)) pari_err_TYPE(fun,pl);
    2531         595 :   switch(typ(pl))
    2532             :   {
    2533             :     case t_VEC:
    2534          14 :       RgV_check_ZV(pl,fun);
    2535          14 :       pl = ZV_to_zv(pl);
    2536             :       /* fall through */
    2537         595 :     case t_VECSMALL: break;
    2538           0 :     default: pari_err_TYPE(fun,pl);
    2539             :   }
    2540         595 :   return pl;
    2541             : }
    2542             : 
    2543             : static int
    2544        2716 : is_chineseinit(GEN x)
    2545             : {
    2546             :   GEN fa, pl;
    2547             :   long l;
    2548        2716 :   if (typ(x) != t_VEC || lg(x)!=3) return 0;
    2549        2156 :   fa = gel(x,1);
    2550        2156 :   pl = gel(x,2);
    2551        2156 :   if (typ(fa) != t_VEC || typ(pl) != t_VEC) return 0;
    2552        1442 :   l = lg(fa);
    2553        1442 :   if (l != 1)
    2554             :   {
    2555        1442 :     if (l != 3 || typ(gel(fa,1)) != t_MAT || typ(gel(fa,2)) != t_VEC)
    2556           7 :       return 0;
    2557             :   }
    2558        1435 :   l = lg(pl);
    2559        1435 :   if (l != 1)
    2560             :   {
    2561         476 :     if (l != 5 || typ(gel(pl,1)) != t_MAT || typ(gel(pl,2)) != t_MAT
    2562         476 :                || typ(gel(pl,3)) != t_COL || typ(gel(pl,4)) != t_VECSMALL)
    2563           0 :       return 0;
    2564             :   }
    2565        1435 :   return 1;
    2566             : }
    2567             : 
    2568             : /* nf a true 'nf' */
    2569             : static GEN
    2570        1379 : chineseinit_i(GEN nf, GEN fa, GEN w, GEN dw)
    2571             : {
    2572        1379 :   const char *fun = "idealchineseinit";
    2573        1379 :   GEN nz = NULL, pl = NULL;
    2574        1379 :   switch(typ(fa))
    2575             :   {
    2576             :     case t_VEC:
    2577         595 :       if (is_chineseinit(fa))
    2578             :       {
    2579           0 :         if (dw) pari_err_DOMAIN(fun, "denom(y)", "!=", gen_1, w);
    2580           0 :         return fa;
    2581             :       }
    2582         595 :       if (lg(fa) != 3) pari_err_TYPE(fun, fa);
    2583             :       /* of the form [x,s] */
    2584         595 :       pl = pl_normalize(nf, gel(fa,2));
    2585         595 :       fa = gel(fa,1);
    2586         595 :       nz = vecsmall01_to_indices(pl);
    2587         595 :       if (is_chineseinit(fa)) { fa = gel(fa,1); break; /* keep fa, reset pl */ }
    2588             :       /* fall through */
    2589             :     case t_MAT: /* factorization? */
    2590        1316 :       if (isprfact(fa)) { fa = pr_init(nf, fa, w, dw); break; }
    2591           0 :     default: pari_err_TYPE(fun,fa);
    2592             :   }
    2593             : 
    2594        1379 :   if (pl)
    2595             :   {
    2596             :     GEN C, Mr, MI, lambda, mlambda;
    2597         595 :     GEN F = (lg(fa) == 1)? NULL: gel(fa,1);
    2598             :     long i, r;
    2599         595 :     Mr = rowpermute(nf_get_M(nf), nz);
    2600         595 :     MI = F? RgM_mul(Mr, F): Mr;
    2601         595 :     lambda = gmul2n(matrixnorm(MI,DEFAULTPREC), -1);
    2602         595 :     mlambda = gneg(lambda);
    2603         595 :     r = lg(nz);
    2604         595 :     C = cgetg(r, t_COL);
    2605         595 :     for (i = 1; i < r; i++) gel(C,i) = pl[nz[i]] < 0? mlambda: lambda;
    2606         595 :     pl = mkvec4(MI, Mr, C, pl);
    2607             :   }
    2608             :   else
    2609         784 :     pl = cgetg(1,t_VEC);
    2610        1379 :   return mkvec2(fa, pl);
    2611             : }
    2612             : 
    2613             : /* Given a prime ideal factorization x, possibly with 0 or negative exponents,
    2614             :  * and a vector w of elements of nf, gives b such that
    2615             :  * v_p(b-w_p)>=v_p(x) for all prime ideals p in the ideal factorization
    2616             :  * and v_p(b)>=0 for all other p, using the standard proof given in GTM 138. */
    2617             : GEN
    2618        2751 : idealchinese(GEN nf, GEN x, GEN w)
    2619             : {
    2620        2751 :   const char *fun = "idealchinese";
    2621        2751 :   pari_sp av = avma;
    2622             :   GEN x1, x2, s, dw;
    2623             : 
    2624        2751 :   nf = checknf(nf);
    2625        2751 :   if (!w) return gerepilecopy(av, chineseinit_i(nf,x,NULL,NULL));
    2626             : 
    2627        1526 :   if (typ(w) != t_VEC) pari_err_TYPE(fun,w);
    2628        1526 :   w = Q_remove_denom(matalgtobasis(nf,w), &dw);
    2629        1526 :   if (!is_chineseinit(x)) x = chineseinit_i(nf,x,w,dw);
    2630             :   /* x is a 'chineseinit' */
    2631        1526 :   x1 = gel(x,1); s = NULL;
    2632        1526 :   if (lg(x1) != 1)
    2633             :   {
    2634        1526 :     GEN F = gel(x1,1), U = gel(x1,2);
    2635        1526 :     long i, r = lg(w);
    2636        3892 :     for (i=1; i<r; i++)
    2637        2366 :       if (!gequal0(gel(w,i)))
    2638             :       {
    2639        1862 :         GEN t = nfmuli(nf, gel(U,i), gel(w,i));
    2640        1862 :         s = s? ZC_add(s,t): t;
    2641             :       }
    2642        1526 :     if (s) s = ZC_reducemodmatrix(s, F);
    2643             :   }
    2644        1526 :   if (!s) { s = zerocol(nf_get_degree(nf)); dw = NULL; }
    2645             : 
    2646        1526 :   x2 = gel(x,2);
    2647        1526 :   if (lg(x2) != 1)
    2648             :   {
    2649         602 :     GEN pl = gel(x2,4);
    2650         602 :     if (!nfchecksigns(nf, s, pl))
    2651             :     {
    2652         273 :       GEN MI = gel(x2,1), Mr = gel(x2,2), C = gel(x2,3);
    2653         273 :       GEN t = RgC_sub(C, RgM_RgC_mul(Mr,s));
    2654             :       long e;
    2655         273 :       t = grndtoi(RgM_RgC_invimage(MI,t), &e);
    2656         273 :       if (lg(x1) != 1) { GEN F = gel(x1,1); t = ZM_ZC_mul(F, t); }
    2657         273 :       s = ZC_add(s, t);
    2658             :     }
    2659             :   }
    2660        1526 :   if (dw) s = RgC_Rg_div(s,dw);
    2661        1526 :   return gerepileupto(av, s);
    2662             : }
    2663             : 
    2664             : static GEN
    2665          21 : mat_ideal_two_elt2(GEN nf, GEN x, GEN a)
    2666             : {
    2667          21 :   GEN L, e, fact = idealfactor(nf,a);
    2668             :   long i, r;
    2669          21 :   L = gel(fact,1);
    2670          21 :   e = gel(fact,2); r = lg(e);
    2671          21 :   for (i=1; i<r; i++) gel(e,i) = stoi( idealval(nf,x,gel(L,i)) );
    2672          21 :   return idealapprfact_i(nf,fact,1);
    2673             : }
    2674             : 
    2675             : static void
    2676          14 : not_in_ideal(GEN a) {
    2677          14 :   pari_err_DOMAIN("idealtwoelt2","element mod ideal", "!=", gen_0, a);
    2678           0 : }
    2679             : 
    2680             : /* Given an integral ideal x and a in x, gives a b such that
    2681             :  * x = aZ_K + bZ_K using the approximation theorem */
    2682             : GEN
    2683          42 : idealtwoelt2(GEN nf, GEN x, GEN a)
    2684             : {
    2685          42 :   pari_sp av = avma;
    2686             :   GEN cx, b, mod;
    2687             : 
    2688          42 :   nf = checknf(nf);
    2689          42 :   a = nf_to_scalar_or_basis(nf, a);
    2690          42 :   x = idealhnf_shallow(nf,x);
    2691          42 :   if (lg(x) == 1)
    2692             :   {
    2693          14 :     if (!isintzero(a)) not_in_ideal(a);
    2694           7 :     avma = av; return zerocol(nf_get_degree(nf));
    2695             :   }
    2696          28 :   x = Q_primitive_part(x, &cx);
    2697          28 :   if (cx) a = gdiv(a, cx);
    2698          28 :   if (typ(a) != t_COL)
    2699             :   { /* rational number */
    2700          21 :     if (typ(a) != t_INT || !dvdii(a, gcoeff(x,1,1))) not_in_ideal(a);
    2701          14 :     mod = NULL;
    2702             :   }
    2703             :   else
    2704             :   {
    2705           7 :     if (!hnf_invimage(x, a)) not_in_ideal(a);
    2706           7 :     mod = idealhnf_principal(nf, a);
    2707             :   }
    2708          21 :   b = mat_ideal_two_elt2(nf, x, a);
    2709          21 :   b = mod? ZC_hnfrem(b, mod): centermod(b, a);
    2710          21 :   b = cx? RgC_Rg_mul(b,cx): gcopy(b);
    2711          21 :   return gerepileupto(av, b);
    2712             : }
    2713             : 
    2714             : /* Given 2 integral ideals x and y in nf, returns a beta in nf such that
    2715             :  * beta * x is an integral ideal coprime to y */
    2716             : GEN
    2717        4711 : idealcoprimefact(GEN nf, GEN x, GEN fy)
    2718             : {
    2719        4711 :   GEN L = gel(fy,1), e;
    2720        4711 :   long i, r = lg(L);
    2721             : 
    2722        4711 :   e = cgetg(r, t_COL);
    2723        4711 :   for (i=1; i<r; i++) gel(e,i) = stoi( -idealval(nf,x,gel(L,i)) );
    2724        4711 :   return idealapprfact_i(nf, mkmat2(L,e), 0);
    2725             : }
    2726             : GEN
    2727          63 : idealcoprime(GEN nf, GEN x, GEN y)
    2728             : {
    2729          63 :   pari_sp av = avma;
    2730          63 :   return gerepileupto(av, idealcoprimefact(nf, x, idealfactor(nf,y)));
    2731             : }
    2732             : 
    2733             : GEN
    2734           7 : nfmulmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    2735             : {
    2736           7 :   pari_sp av = avma;
    2737           7 :   GEN z, p, pr = modpr, T;
    2738             : 
    2739           7 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    2740           0 :   x = nf_to_Fq(nf,x,modpr);
    2741           0 :   y = nf_to_Fq(nf,y,modpr);
    2742           0 :   z = Fq_mul(x,y,T,p);
    2743           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    2744             : }
    2745             : 
    2746             : GEN
    2747           0 : nfdivmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    2748             : {
    2749           0 :   pari_sp av = avma;
    2750           0 :   nf = checknf(nf);
    2751           0 :   return gerepileupto(av, nfreducemodpr(nf, nfdiv(nf,x,y), modpr));
    2752             : }
    2753             : 
    2754             : GEN
    2755           0 : nfpowmodpr(GEN nf, GEN x, GEN k, GEN modpr)
    2756             : {
    2757           0 :   pari_sp av=avma;
    2758           0 :   GEN z, T, p, pr = modpr;
    2759             : 
    2760           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    2761           0 :   z = nf_to_Fq(nf,x,modpr);
    2762           0 :   z = Fq_pow(z,k,T,p);
    2763           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    2764             : }
    2765             : 
    2766             : GEN
    2767           0 : nfkermodpr(GEN nf, GEN x, GEN modpr)
    2768             : {
    2769           0 :   pari_sp av = avma;
    2770           0 :   GEN T, p, pr = modpr;
    2771             : 
    2772           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    2773           0 :   if (typ(x)!=t_MAT) pari_err_TYPE("nfkermodpr",x);
    2774           0 :   x = nfM_to_FqM(x, nf, modpr);
    2775           0 :   return gerepilecopy(av, FqM_to_nfM(FqM_ker(x,T,p), modpr));
    2776             : }
    2777             : 
    2778             : GEN
    2779           0 : nfsolvemodpr(GEN nf, GEN a, GEN b, GEN pr)
    2780             : {
    2781           0 :   const char *f = "nfsolvemodpr";
    2782           0 :   pari_sp av = avma;
    2783             :   GEN T, p, modpr;
    2784             : 
    2785           0 :   nf = checknf(nf);
    2786           0 :   modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    2787           0 :   if (typ(a)!=t_MAT) pari_err_TYPE(f,a);
    2788           0 :   a = nfM_to_FqM(a, nf, modpr);
    2789           0 :   switch(typ(b))
    2790             :   {
    2791             :     case t_MAT:
    2792           0 :       b = nfM_to_FqM(b, nf, modpr);
    2793           0 :       b = FqM_gauss(a,b,T,p);
    2794           0 :       if (!b) pari_err_INV(f,a);
    2795           0 :       a = FqM_to_nfM(b, modpr);
    2796           0 :       break;
    2797             :     case t_COL:
    2798           0 :       b = nfV_to_FqV(b, nf, modpr);
    2799           0 :       b = FqM_FqC_gauss(a,b,T,p);
    2800           0 :       if (!b) pari_err_INV(f,a);
    2801           0 :       a = FqV_to_nfV(b, modpr);
    2802           0 :       break;
    2803           0 :     default: pari_err_TYPE(f,b);
    2804             :   }
    2805           0 :   return gerepilecopy(av, a);
    2806             : }

Generated by: LCOV version 1.11