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

Generated by: LCOV version 1.11