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

Generated by: LCOV version 1.11