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.10.0 lcov report (development 20443-183d202) Lines: 1314 1447 90.8 %
Date: 2017-03-27 05:17:48 Functions: 131 142 92.3 %
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     1742955 : idealtyp(GEN *ideal, GEN *arch)
      45             : {
      46     1742955 :   GEN x = *ideal;
      47     1742955 :   long t,lx,tx = typ(x);
      48             : 
      49     1742955 :   if (tx==t_VEC && lg(x)==3)
      50      216780 :   { *arch = gel(x,2); x = gel(x,1); tx = typ(x); }
      51             :   else
      52     1526175 :     *arch = NULL;
      53     1742955 :   switch(tx)
      54             :   {
      55      808015 :     case t_MAT: lx = lg(x);
      56      808015 :       if (lx == 1) { t = id_PRINCIPAL; x = gen_0; break; }
      57      807960 :       if (lx != lgcols(x)) pari_err_TYPE("idealtyp [non-square t_MAT]",x);
      58      807955 :       t = id_MAT;
      59      807955 :       break;
      60             : 
      61      783035 :     case t_VEC: if (lg(x)!=6) pari_err_TYPE("idealtyp",x);
      62      783025 :       t = id_PRIME; break;
      63             : 
      64             :     case t_POL: case t_POLMOD: case t_COL:
      65             :     case t_INT: case t_FRAC:
      66      151905 :       t = id_PRINCIPAL; break;
      67             :     default:
      68           0 :       pari_err_TYPE("idealtyp",x);
      69             :       return 0; /*LCOV_EXCL_LINE*/
      70             :   }
      71     1742940 :   *ideal = x; return t;
      72             : }
      73             : 
      74             : /* true nf; v = [a,x,...], a in Z. Return (a,x) */
      75             : GEN
      76       56615 : idealhnf_two(GEN nf, GEN v)
      77             : {
      78       56615 :   GEN p = gel(v,1), pi = gel(v,2), m = zk_scalar_or_multable(nf, pi);
      79       56615 :   if (typ(m) == t_INT) return scalarmat(gcdii(m,p), nf_get_degree(nf));
      80       48830 :   return ZM_hnfmodid(m, p);
      81             : }
      82             : /* true nf */
      83             : GEN
      84      808861 : pr_hnf(GEN nf, GEN pr)
      85             : {
      86      808861 :   GEN p = pr_get_p(pr), m;
      87      808861 :   if (pr_is_inert(pr)) return scalarmat(p, nf_get_degree(nf));
      88      628356 :   m = zk_scalar_or_multable(nf, pr_get_gen(pr));
      89      628356 :   return ZM_hnfmodprime(m, p);
      90             : }
      91             : 
      92             : static GEN
      93       27570 : ZM_Q_mul(GEN x, GEN y)
      94       27570 : { return typ(y) == t_INT? ZM_Z_mul(x,y): RgM_Rg_mul(x,y); }
      95             : 
      96             : 
      97             : GEN
      98      127745 : idealhnf_principal(GEN nf, GEN x)
      99             : {
     100             :   GEN cx;
     101      127745 :   x = nf_to_scalar_or_basis(nf, x);
     102      127745 :   switch(typ(x))
     103             :   {
     104       88760 :     case t_COL: break;
     105       32930 :     case t_INT:  if (!signe(x)) return cgetg(1,t_MAT);
     106       32860 :       return scalarmat(absi(x), nf_get_degree(nf));
     107             :     case t_FRAC:
     108        6055 :       return scalarmat(Q_abs_shallow(x), nf_get_degree(nf));
     109           0 :     default: pari_err_TYPE("idealhnf",x);
     110             :   }
     111       88760 :   x = Q_primitive_part(x, &cx);
     112       88760 :   RgV_check_ZV(x, "idealhnf");
     113       88760 :   x = zk_multable(nf, x);
     114       88760 :   x = ZM_hnfmodid(x, zkmultable_capZ(x));
     115       88760 :   return cx? ZM_Q_mul(x,cx): x;
     116             : }
     117             : 
     118             : /* x integral ideal in t_MAT form, nx columns */
     119             : static GEN
     120           5 : vec_mulid(GEN nf, GEN x, long nx, long N)
     121             : {
     122           5 :   GEN m = cgetg(nx*N + 1, t_MAT);
     123             :   long i, j, k;
     124          15 :   for (i=k=1; i<=nx; i++)
     125          10 :     for (j=1; j<=N; j++) gel(m, k++) = zk_ei_mul(nf, gel(x,i),j);
     126           5 :   return m;
     127             : }
     128             : /* true nf */
     129             : GEN
     130      206275 : idealhnf_shallow(GEN nf, GEN x)
     131             : {
     132      206275 :   long tx = typ(x), lx = lg(x), N;
     133             : 
     134             :   /* cannot use idealtyp because here we allow non-square matrices */
     135      206275 :   if (tx == t_VEC && lx == 3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
     136      206275 :   if (tx == t_VEC && lx == 6) return pr_hnf(nf,x); /* PRIME */
     137      106695 :   switch(tx)
     138             :   {
     139             :     case t_MAT:
     140             :     {
     141             :       GEN cx;
     142       10340 :       long nx = lx-1;
     143       10340 :       N = nf_get_degree(nf);
     144       10340 :       if (nx == 0) return cgetg(1, t_MAT);
     145       10325 :       if (nbrows(x) != N) pari_err_TYPE("idealhnf [wrong dimension]",x);
     146       10320 :       if (nx == 1) return idealhnf_principal(nf, gel(x,1));
     147             : 
     148        9460 :       if (nx == N && RgM_is_ZM(x) && ZM_ishnf(x)) return x;
     149        1705 :       x = Q_primitive_part(x, &cx);
     150        1705 :       if (nx < N) x = vec_mulid(nf, x, nx, N);
     151        1705 :       x = ZM_hnfmod(x, ZM_detmult(x));
     152        1705 :       return cx? ZM_Q_mul(x,cx): x;
     153             :     }
     154             :     case t_QFI:
     155             :     case t_QFR:
     156             :     {
     157          10 :       pari_sp av = avma;
     158          10 :       GEN u, D = nf_get_disc(nf), T = nf_get_pol(nf), f = nf_get_index(nf);
     159          10 :       GEN A = gel(x,1), B = gel(x,2);
     160          10 :       N = nf_get_degree(nf);
     161          10 :       if (N != 2)
     162           0 :         pari_err_TYPE("idealhnf [Qfb for non-quadratic fields]", x);
     163          10 :       if (!equalii(qfb_disc(x), D))
     164           5 :         pari_err_DOMAIN("idealhnf [Qfb]", "disc(q)", "!=", D, x);
     165             :       /* x -> A Z + (-B + sqrt(D)) / 2 Z
     166             :          K = Q[t]/T(t), t^2 + ut + v = 0,  u^2 - 4v = Df^2
     167             :          => t = (-u + sqrt(D) f)/2
     168             :          => sqrt(D)/2 = (t + u/2)/f */
     169           5 :       u = gel(T,3);
     170           5 :       B = deg1pol_shallow(ginv(f),
     171             :                           gsub(gdiv(u, shifti(f,1)), gdiv(B,gen_2)),
     172           5 :                           varn(T));
     173           5 :       return gerepileupto(av, idealhnf_two(nf, mkvec2(A,B)));
     174             :     }
     175       96345 :     default: return idealhnf_principal(nf, x); /* PRINCIPAL */
     176             :   }
     177             : }
     178             : GEN
     179        1850 : idealhnf(GEN nf, GEN x)
     180             : {
     181        1850 :   pari_sp av = avma;
     182        1850 :   GEN y = idealhnf_shallow(checknf(nf), x);
     183        1840 :   return (avma == av)? gcopy(y): gerepileupto(av, y);
     184             : }
     185             : 
     186             : /* GP functions */
     187             : 
     188             : GEN
     189          45 : idealtwoelt0(GEN nf, GEN x, GEN a)
     190             : {
     191          45 :   if (!a) return idealtwoelt(nf,x);
     192          30 :   return idealtwoelt2(nf,x,a);
     193             : }
     194             : 
     195             : GEN
     196          30 : idealpow0(GEN nf, GEN x, GEN n, long flag)
     197             : {
     198          30 :   if (flag) return idealpowred(nf,x,n);
     199          25 :   return idealpow(nf,x,n);
     200             : }
     201             : 
     202             : GEN
     203          40 : idealmul0(GEN nf, GEN x, GEN y, long flag)
     204             : {
     205          40 :   if (flag) return idealmulred(nf,x,y);
     206          35 :   return idealmul(nf,x,y);
     207             : }
     208             : 
     209             : GEN
     210          30 : idealdiv0(GEN nf, GEN x, GEN y, long flag)
     211             : {
     212          30 :   switch(flag)
     213             :   {
     214          15 :     case 0: return idealdiv(nf,x,y);
     215          15 :     case 1: return idealdivexact(nf,x,y);
     216           0 :     default: pari_err_FLAG("idealdiv");
     217             :   }
     218             :   return NULL; /* LCOV_EXCL_LINE */
     219             : }
     220             : 
     221             : GEN
     222          50 : idealaddtoone0(GEN nf, GEN arg1, GEN arg2)
     223             : {
     224          50 :   if (!arg2) return idealaddmultoone(nf,arg1);
     225          25 :   return idealaddtoone(nf,arg1,arg2);
     226             : }
     227             : 
     228             : /* b not a scalar */
     229             : static GEN
     230          20 : hnf_Z_ZC(GEN nf, GEN a, GEN b) { return hnfmodid(zk_multable(nf,b), a); }
     231             : /* b not a scalar */
     232             : static GEN
     233          15 : hnf_Z_QC(GEN nf, GEN a, GEN b)
     234             : {
     235             :   GEN db;
     236          15 :   b = Q_remove_denom(b, &db);
     237          15 :   if (db) a = mulii(a, db);
     238          15 :   b = hnf_Z_ZC(nf,a,b);
     239          15 :   return db? RgM_Rg_div(b, db): b;
     240             : }
     241             : /* b not a scalar (not point in trying to optimize for this case) */
     242             : static GEN
     243          20 : hnf_Q_QC(GEN nf, GEN a, GEN b)
     244             : {
     245             :   GEN da, db;
     246          20 :   if (typ(a) == t_INT) return hnf_Z_QC(nf, a, b);
     247           5 :   da = gel(a,2);
     248           5 :   a = gel(a,1);
     249           5 :   b = Q_remove_denom(b, &db);
     250             :   /* write da = d*A, db = d*B, gcd(A,B) = 1
     251             :    * gcd(a/(d A), b/(d B)) = gcd(a B, A b) / A B d = gcd(a B, b) / A B d */
     252           5 :   if (db)
     253             :   {
     254           5 :     GEN d = gcdii(da,db);
     255           5 :     if (!is_pm1(d)) db = diviiexact(db,d); /* B */
     256           5 :     if (!is_pm1(db))
     257             :     {
     258           5 :       a = mulii(a, db); /* a B */
     259           5 :       da = mulii(da, db); /* A B d = lcm(denom(a),denom(b)) */
     260             :     }
     261             :   }
     262           5 :   return RgM_Rg_div(hnf_Z_ZC(nf,a,b), da);
     263             : }
     264             : static GEN
     265           5 : hnf_QC_QC(GEN nf, GEN a, GEN b)
     266             : {
     267             :   GEN da, db, d, x;
     268           5 :   a = Q_remove_denom(a, &da);
     269           5 :   b = Q_remove_denom(b, &db);
     270           5 :   if (da) b = ZC_Z_mul(b, da);
     271           5 :   if (db) a = ZC_Z_mul(a, db);
     272           5 :   d = mul_denom(da, db);
     273           5 :   a = zk_multable(nf,a); da = zkmultable_capZ(a);
     274           5 :   b = zk_multable(nf,b); db = zkmultable_capZ(b);
     275           5 :   x = ZM_hnfmodid(shallowconcat(a,b), gcdii(da,db));
     276           5 :   return d? RgM_Rg_div(x, d): x;
     277             : }
     278             : static GEN
     279          15 : hnf_Q_Q(GEN nf, GEN a, GEN b) {return scalarmat(Q_gcd(a,b), nf_get_degree(nf));}
     280             : GEN
     281          85 : idealhnf0(GEN nf, GEN a, GEN b)
     282             : {
     283             :   long ta, tb;
     284             :   pari_sp av;
     285             :   GEN x;
     286          85 :   if (!b) return idealhnf(nf,a);
     287             : 
     288             :   /* HNF of aZ_K+bZ_K */
     289          40 :   av = avma; nf = checknf(nf);
     290          40 :   a = nf_to_scalar_or_basis(nf,a); ta = typ(a);
     291          40 :   b = nf_to_scalar_or_basis(nf,b); tb = typ(b);
     292          40 :   if (ta == t_COL)
     293          10 :     x = (tb==t_COL)? hnf_QC_QC(nf, a,b): hnf_Q_QC(nf, b,a);
     294             :   else
     295          30 :     x = (tb==t_COL)? hnf_Q_QC(nf, a,b): hnf_Q_Q(nf, a,b);
     296          40 :   return gerepileupto(av, x);
     297             : }
     298             : 
     299             : /*******************************************************************/
     300             : /*                                                                 */
     301             : /*                       TWO-ELEMENT FORM                          */
     302             : /*                                                                 */
     303             : /*******************************************************************/
     304             : static GEN idealapprfact_i(GEN nf, GEN x, int nored);
     305             : 
     306             : static int
     307      107500 : ok_elt(GEN x, GEN xZ, GEN y)
     308             : {
     309      107500 :   pari_sp av = avma;
     310      107500 :   int r = ZM_equal(x, ZM_hnfmodid(y, xZ));
     311      107500 :   avma = av; return r;
     312             : }
     313             : 
     314             : static GEN
     315       37710 : addmul_col(GEN a, long s, GEN b)
     316             : {
     317             :   long i,l;
     318       37710 :   if (!s) return a? leafcopy(a): a;
     319       37595 :   if (!a) return gmulsg(s,b);
     320       35385 :   l = lg(a);
     321      189250 :   for (i=1; i<l; i++)
     322      153865 :     if (signe(gel(b,i))) gel(a,i) = addii(gel(a,i), mulsi(s, gel(b,i)));
     323       35385 :   return a;
     324             : }
     325             : 
     326             : /* a <-- a + s * b, all coeffs integers */
     327             : static GEN
     328       16455 : addmul_mat(GEN a, long s, GEN b)
     329             : {
     330             :   long j,l;
     331             :   /* copy otherwise next call corrupts a */
     332       16455 :   if (!s) return a? RgM_shallowcopy(a): a;
     333       15370 :   if (!a) return gmulsg(s,b);
     334        8350 :   l = lg(a);
     335       40430 :   for (j=1; j<l; j++)
     336       32080 :     (void)addmul_col(gel(a,j), s, gel(b,j));
     337        8350 :   return a;
     338             : }
     339             : 
     340             : static GEN
     341       55485 : get_random_a(GEN nf, GEN x, GEN xZ)
     342             : {
     343             :   pari_sp av;
     344       55485 :   long i, lm, l = lg(x);
     345             :   GEN a, z, beta, mul;
     346             : 
     347       55485 :   beta= cgetg(l, t_VEC);
     348       55485 :   mul = cgetg(l, t_VEC); lm = 1; /* = lg(mul) */
     349             :   /* look for a in x such that a O/xZ = x O/xZ */
     350      109250 :   for (i = 2; i < l; i++)
     351             :   {
     352      107040 :     GEN xi = gel(x,i);
     353      107040 :     GEN t = FpM_red(zk_multable(nf,xi), xZ); /* ZM, cannot be a scalar */
     354      107040 :     if (gequal0(t)) continue;
     355      100480 :     if (ok_elt(x,xZ, t)) return xi;
     356       47205 :     gel(beta,lm) = xi;
     357             :     /* mul[i] = { canonical generators for x[i] O/xZ as Z-module } */
     358       47205 :     gel(mul,lm) = t; lm++;
     359             :   }
     360        2210 :   setlg(mul, lm);
     361        2210 :   setlg(beta,lm);
     362        2210 :   z = cgetg(lm, t_VECSMALL);
     363        7055 :   for(av = avma;; avma = av)
     364             :   {
     365       23510 :     for (a=NULL,i=1; i<lm; i++)
     366             :     {
     367       16455 :       long t = random_bits(4) - 7; /* in [-7,8] */
     368       16455 :       z[i] = t;
     369       16455 :       a = addmul_mat(a, t, gel(mul,i));
     370             :     }
     371             :     /* a = matrix (NOT HNF) of ideal generated by beta.z in O/xZ */
     372        7055 :     if (a && ok_elt(x,xZ, a)) break;
     373        4845 :   }
     374        7840 :   for (a=NULL,i=1; i<lm; i++)
     375        5630 :     a = addmul_col(a, z[i], gel(beta,i));
     376        2210 :   return a;
     377             : }
     378             : 
     379             : /* x square matrix, assume it is HNF */
     380             : static GEN
     381      119655 : mat_ideal_two_elt(GEN nf, GEN x)
     382             : {
     383             :   GEN y, a, cx, xZ;
     384      119655 :   long N = nf_get_degree(nf);
     385             :   pari_sp av, tetpil;
     386             : 
     387      119655 :   if (lg(x)-1 != N) pari_err_DIM("idealtwoelt");
     388      119645 :   if (N == 2) return mkvec2copy(gcoeff(x,1,1), gel(x,2));
     389             : 
     390       60875 :   y = cgetg(3,t_VEC); av = avma;
     391       60875 :   cx = Q_content(x);
     392       60875 :   xZ = gcoeff(x,1,1);
     393       60875 :   if (gequal(xZ, cx)) /* x = (cx) */
     394             :   {
     395        1885 :     gel(y,1) = cx;
     396        1885 :     gel(y,2) = gen_0; return y;
     397             :   }
     398       58990 :   if (equali1(cx)) cx = NULL;
     399             :   else
     400             :   {
     401         285 :     x = Q_div_to_int(x, cx);
     402         285 :     xZ = gcoeff(x,1,1);
     403             :   }
     404       58990 :   if (N < 6)
     405       52720 :     a = get_random_a(nf, x, xZ);
     406             :   else
     407             :   {
     408        6270 :     const long FB[] = { _evallg(15+1) | evaltyp(t_VECSMALL),
     409             :       2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47
     410             :     };
     411        6270 :     GEN P, E, a1 = Z_smoothen(xZ, (GEN)FB, &P, &E);
     412        6270 :     if (!a1) /* factors completely */
     413        3505 :       a = idealapprfact_i(nf, idealfactor(nf,x), 1);
     414        2765 :     else if (lg(P) == 1) /* no small factors */
     415        1945 :       a = get_random_a(nf, x, xZ);
     416             :     else /* general case */
     417             :     {
     418             :       GEN A0, A1, a0, u0, u1, v0, v1, pi0, pi1, t, u;
     419         820 :       a0 = diviiexact(xZ, a1);
     420         820 :       A0 = ZM_hnfmodid(x, a0); /* smooth part of x */
     421         820 :       A1 = ZM_hnfmodid(x, a1); /* cofactor */
     422         820 :       pi0 = idealapprfact_i(nf, idealfactor(nf,A0), 1);
     423         820 :       pi1 = get_random_a(nf, A1, a1);
     424         820 :       (void)bezout(a0, a1, &v0,&v1);
     425         820 :       u0 = mulii(a0, v0);
     426         820 :       u1 = mulii(a1, v1);
     427         820 :       if (typ(pi0) != t_COL) t = addmulii(u0, pi0, u1);
     428             :       else
     429         820 :       { t = ZC_Z_mul(pi0, u1); gel(t,1) = addii(gel(t,1), u0); }
     430         820 :       u = ZC_Z_mul(pi1, u0); gel(u,1) = addii(gel(u,1), u1);
     431         820 :       a = nfmuli(nf, centermod(u, xZ), centermod(t, xZ));
     432             :     }
     433             :   }
     434       58990 :   if (cx)
     435             :   {
     436         285 :     a = centermod(a, xZ);
     437         285 :     tetpil = avma;
     438         285 :     if (typ(cx) == t_INT)
     439             :     {
     440         255 :       gel(y,1) = mulii(xZ, cx);
     441         255 :       gel(y,2) = ZC_Z_mul(a, cx);
     442             :     }
     443             :     else
     444             :     {
     445          30 :       gel(y,1) = gmul(xZ, cx);
     446          30 :       gel(y,2) = RgC_Rg_mul(a, cx);
     447             :     }
     448             :   }
     449             :   else
     450             :   {
     451       58705 :     tetpil = avma;
     452       58705 :     gel(y,1) = icopy(xZ);
     453       58705 :     gel(y,2) = centermod(a, xZ);
     454             :   }
     455       58990 :   gerepilecoeffssp(av,tetpil,y+1,2); return y;
     456             : }
     457             : 
     458             : /* Given an ideal x, returns [a,alpha] such that a is in Q,
     459             :  * x = a Z_K + alpha Z_K, alpha in K^*
     460             :  * a = 0 or alpha = 0 are possible, but do not try to determine whether
     461             :  * x is principal. */
     462             : GEN
     463       17260 : idealtwoelt(GEN nf, GEN x)
     464             : {
     465             :   pari_sp av;
     466             :   GEN z;
     467       17260 :   long tx = idealtyp(&x,&z);
     468       17255 :   nf = checknf(nf);
     469       17255 :   if (tx == id_MAT) return mat_ideal_two_elt(nf,x);
     470         875 :   if (tx == id_PRIME) return mkvec2copy(gel(x,1), gel(x,2));
     471             :   /* id_PRINCIPAL */
     472         325 :   av = avma; x = nf_to_scalar_or_basis(nf, x);
     473         510 :   return gerepilecopy(av, typ(x)==t_COL? mkvec2(gen_0,x):
     474         250 :                                          mkvec2(Q_abs_shallow(x),gen_0));
     475             : }
     476             : 
     477             : /*******************************************************************/
     478             : /*                                                                 */
     479             : /*                         FACTORIZATION                           */
     480             : /*                                                                 */
     481             : /*******************************************************************/
     482             : /* x integral ideal in HNF, Zval = v_p(x \cap Z) > 0; return v_p(Nx) */
     483             : static long
     484       76155 : idealHNF_norm_pval(GEN x, GEN p, long Zval)
     485             : {
     486       76155 :   long i, v = Zval, l = lg(x);
     487       76155 :   for (i = 2; i < l; i++) v += Z_pval(gcoeff(x,i,i), p);
     488       76155 :   return v;
     489             : }
     490             : 
     491             : /* return P, primes dividing Nx and xZ = x\cap Z, set v_p(Nx), v_p(xZ);
     492             :  * x integral in HNF */
     493             : GEN
     494       10110 : idealHNF_Z_factor(GEN x, GEN *pvN, GEN *pvZ)
     495             : {
     496       10110 :   GEN xZ = gcoeff(x,1,1), f, P, E, vN, vZ;
     497             :   long i, l;
     498       10110 :   if (typ(xZ) != t_INT) pari_err_TYPE("idealfactor",x);
     499       10110 :   f = Z_factor(xZ);
     500       10110 :   P = gel(f,1); l = lg(P);
     501       10110 :   E = gel(f,2);
     502       10110 :   *pvN = vN = cgetg(l, t_VECSMALL);
     503       10110 :   *pvZ = vZ = cgetg(l, t_VECSMALL);
     504       18660 :   for (i = 1; i < l; i++)
     505             :   {
     506        8550 :     vZ[i] = itou(gel(E,i));
     507        8550 :     vN[i] = idealHNF_norm_pval(x,gel(P,i), vZ[i]);
     508             :   }
     509       10110 :   return P;
     510             : }
     511             : 
     512             : /* v_P(A)*f(P) <= Nval [e.g. Nval = v_p(Norm A)], Zval = v_p(A \cap Z).
     513             :  * Return v_P(A) */
     514             : static long
     515       85180 : idealHNF_val(GEN A, GEN P, long Nval, long Zval)
     516             : {
     517       85180 :   long f = pr_get_f(P), vmax, v, e, i, j, k, l;
     518             :   GEN mul, B, a, y, r, p, pk, cx, vals;
     519             :   pari_sp av;
     520             : 
     521       85180 :   if (Nval < f) return 0;
     522       85135 :   p = pr_get_p(P);
     523       85135 :   e = pr_get_e(P);
     524             :   /* v_P(A) <= max [ e * v_p(A \cap Z), floor[v_p(Nix) / f ] */
     525       85135 :   vmax = minss(Zval * e, Nval / f);
     526       85135 :   mul = pr_get_tau(P);
     527       85135 :   l = lg(mul);
     528       85135 :   B = cgetg(l,t_MAT);
     529             :   /* B[1] not needed: v_pr(A[1]) = v_pr(A \cap Z) is known already */
     530       85135 :   gel(B,1) = gen_0; /* dummy */
     531      333600 :   for (j = 2; j < l; j++)
     532             :   {
     533      280020 :     GEN x = gel(A,j);
     534      280020 :     gel(B,j) = y = cgetg(l, t_COL);
     535     2527810 :     for (i = 1; i < l; i++)
     536             :     { /* compute a = (x.t0)_i, A in HNF ==> x[j+1..l-1] = 0 */
     537     2279345 :       a = mulii(gel(x,1), gcoeff(mul,i,1));
     538     2279345 :       for (k = 2; k <= j; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     539             :       /* p | a ? */
     540     2279345 :       gel(y,i) = dvmdii(a,p,&r); if (signe(r)) return 0;
     541             :     }
     542             :   }
     543       53580 :   vals = cgetg(l, t_VECSMALL);
     544             :   /* vals[1] not needed */
     545      257665 :   for (j = 2; j < l; j++)
     546             :   {
     547      204085 :     gel(B,j) = Q_primitive_part(gel(B,j), &cx);
     548      204085 :     vals[j] = cx? 1 + e * Q_pval(cx, p): 1;
     549             :   }
     550       53580 :   pk = powiu(p, ceildivuu(vmax, e));
     551       53580 :   av = avma; y = cgetg(l,t_COL);
     552             :   /* can compute mod p^ceil((vmax-v)/e) */
     553       88480 :   for (v = 1; v < vmax; v++)
     554             :   { /* we know v_pr(Bj) >= v for all j */
     555       36535 :     if (e == 1 || (vmax - v) % e == 0) pk = diviiexact(pk, p);
     556      291780 :     for (j = 2; j < l; j++)
     557             :     {
     558      256880 :       GEN x = gel(B,j); if (v < vals[j]) continue;
     559     2559830 :       for (i = 1; i < l; i++)
     560             :       {
     561     2375415 :         pari_sp av2 = avma;
     562     2375415 :         a = mulii(gel(x,1), gcoeff(mul,i,1));
     563     2375415 :         for (k = 2; k < l; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     564             :         /* a = (x.t_0)_i; p | a ? */
     565     2375415 :         a = dvmdii(a,p,&r); if (signe(r)) return v;
     566     2373780 :         if (lgefint(a) > lgefint(pk)) a = remii(a, pk);
     567     2373780 :         gel(y,i) = gerepileuptoint(av2, a);
     568             :       }
     569      184415 :       gel(B,j) = y; y = x;
     570      184415 :       if (gc_needed(av,3))
     571             :       {
     572           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"idealval");
     573           0 :         gerepileall(av,3, &y,&B,&pk);
     574             :       }
     575             :     }
     576             :   }
     577       51945 :   return v;
     578             : }
     579             : /* true nf, x integral ideal */
     580             : static GEN
     581       10110 : idealHNF_factor(GEN nf, GEN x)
     582             : {
     583       10110 :   const long N = lg(x)-1;
     584             :   long i, j, k, l, v;
     585             :   GEN vp, vN, vZ, vP, vE, cx;
     586             : 
     587       10110 :   x = Q_primitive_part(x, &cx);
     588       10110 :   vp = idealHNF_Z_factor(x, &vN,&vZ);
     589       10110 :   l = lg(vp);
     590       10110 :   i = cx? expi(cx)+1: 1;
     591       10110 :   vP = cgetg((l+i-2)*N+1, t_COL);
     592       10110 :   vE = cgetg((l+i-2)*N+1, t_COL);
     593       18660 :   for (i = k = 1; i < l; i++)
     594             :   {
     595        8550 :     GEN L, p = gel(vp,i);
     596        8550 :     long Nval = vN[i], Zval = vZ[i], vc = cx? Z_pvalrem(cx,p,&cx): 0;
     597        8550 :     if (vc)
     598             :     {
     599         275 :       L = idealprimedec(nf,p);
     600         275 :       if (is_pm1(cx)) cx = NULL;
     601             :     }
     602             :     else
     603        8275 :       L = idealprimedec_limit_f(nf,p,Nval);
     604       17580 :     for (j = 1; j < lg(L); j++)
     605             :     {
     606       17575 :       GEN P = gel(L,j);
     607       17575 :       pari_sp av = avma;
     608       17575 :       v = idealHNF_val(x, P, Nval, Zval);
     609       17575 :       avma = av;
     610       17575 :       Nval -= v*pr_get_f(P);
     611       17575 :       v += vc * pr_get_e(P); if (!v) continue;
     612       10820 :       gel(vP,k) = P;
     613       10820 :       gel(vE,k) = utoipos(v); k++;
     614       10820 :       if (!Nval) break; /* now only the content contributes */
     615             :     }
     616        8565 :     if (vc) for (j++; j<lg(L); j++)
     617             :     {
     618          15 :       GEN P = gel(L,j);
     619          15 :       gel(vP,k) = P;
     620          15 :       gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     621             :     }
     622             :   }
     623       10110 :   if (cx)
     624             :   {
     625        2005 :     GEN f = Z_factor(cx), cP = gel(f,1), cE = gel(f,2);
     626        2005 :     long lc = lg(cP);
     627        4215 :     for (i=1; i<lc; i++)
     628             :     {
     629        2210 :       GEN p = gel(cP,i), L = idealprimedec(nf,p);
     630        2210 :       long vc = itos(gel(cE,i));
     631        4970 :       for (j=1; j<lg(L); j++)
     632             :       {
     633        2760 :         GEN P = gel(L,j);
     634        2760 :         gel(vP,k) = P;
     635        2760 :         gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     636             :       }
     637             :     }
     638             :   }
     639       10110 :   setlg(vP, k);
     640       10110 :   setlg(vE, k); return mkmat2(vP, vE);
     641             : }
     642             : /* c * vector(#L,i,L[i].e), assume results fit in ulong */
     643             : static GEN
     644        2120 : prV_e_muls(GEN L, long c)
     645             : {
     646        2120 :   long j, l = lg(L);
     647        2120 :   GEN z = cgetg(l, t_COL);
     648        2120 :   for (j = 1; j < l; j++) gel(z,j) = stoi(c * pr_get_e(gel(L,j)));
     649        2120 :   return z;
     650             : }
     651             : /* true nf, y in Q */
     652             : static GEN
     653        2120 : Q_nffactor(GEN nf, GEN y)
     654             : {
     655             :   GEN f, P, E;
     656             :   long lfa, i;
     657        2120 :   if (typ(y) == t_INT)
     658             :   {
     659        2110 :     if (!signe(y)) pari_err_DOMAIN("idealfactor", "ideal", "=",gen_0,y);
     660        2095 :     if (is_pm1(y)) return trivial_fact();
     661             :   }
     662        1590 :   f = factor(Q_abs_shallow(y));
     663        1590 :   P = gel(f,1); lfa = lg(P);
     664        1590 :   E = gel(f,2);
     665        3710 :   for (i = 1; i < lfa; i++)
     666             :   {
     667        2120 :     gel(P,i) = idealprimedec(nf, gel(P,i));
     668        2120 :     gel(E,i) = prV_e_muls(gel(P,i), itos(gel(E,i)));
     669             :   }
     670        1590 :   settyp(P,t_VEC); P = shallowconcat1(P);
     671        1590 :   settyp(E,t_VEC); E = shallowconcat1(E);
     672        1590 :   gel(f,1) = P; settyp(P, t_COL);
     673        1590 :   gel(f,2) = E; return f;
     674             : }
     675             : 
     676             : GEN
     677       12250 : idealfactor(GEN nf, GEN x)
     678             : {
     679       12250 :   pari_sp av = avma;
     680             :   GEN fa, y;
     681       12250 :   long tx = idealtyp(&x,&y);
     682             : 
     683       12250 :   nf = checknf(nf);
     684       12250 :   if (tx == id_PRIME) retmkmat2(mkcolcopy(x), mkcol(gen_1));
     685       12225 :   if (tx == id_PRINCIPAL)
     686             :   {
     687        3180 :     y = nf_to_scalar_or_basis(nf, x);
     688        3180 :     if (typ(y) != t_COL) return gerepilecopy(av, Q_nffactor(nf, y));
     689             :   }
     690       10105 :   y = idealnumden(nf, x);
     691       10105 :   fa = idealHNF_factor(nf, gel(y,1));
     692       10105 :   if (!isint1(gel(y,2)))
     693             :   {
     694           5 :     GEN F = idealHNF_factor(nf, gel(y,2));
     695           5 :     fa = famat_mul_shallow(fa, famat_inv_shallow(F));
     696             :   }
     697       10105 :   fa = gerepilecopy(av, fa);
     698       10105 :   return sort_factor(fa, (void*)&cmp_prime_ideal, &cmp_nodata);
     699             : }
     700             : 
     701             : /* P prime ideal in idealprimedec format. Return valuation(A) at P */
     702             : long
     703      227840 : idealval(GEN nf, GEN A, GEN P)
     704             : {
     705      227840 :   pari_sp av = avma;
     706             :   GEN a, p, cA;
     707      227840 :   long vcA, v, Zval, tx = idealtyp(&A,&a);
     708             : 
     709      227840 :   if (tx == id_PRINCIPAL) return nfval(nf,A,P);
     710      226980 :   checkprid(P);
     711      226980 :   if (tx == id_PRIME) return pr_equal(P, A)? 1: 0;
     712             :   /* id_MAT */
     713      226960 :   nf = checknf(nf);
     714      226960 :   A = Q_primitive_part(A, &cA);
     715      226960 :   p = pr_get_p(P);
     716      226960 :   vcA = cA? Q_pval(cA,p): 0;
     717      226960 :   if (pr_is_inert(P)) { avma = av; return vcA; }
     718      225895 :   Zval = Z_pval(gcoeff(A,1,1), p);
     719      225895 :   if (!Zval) v = 0;
     720             :   else
     721             :   {
     722       67605 :     long Nval = idealHNF_norm_pval(A, p, Zval);
     723       67605 :     v = idealHNF_val(A, P, Nval, Zval);
     724             :   }
     725      225895 :   avma = av; return vcA? v + vcA*pr_get_e(P): v;
     726             : }
     727             : GEN
     728        4695 : gpidealval(GEN nf, GEN ix, GEN P)
     729             : {
     730        4695 :   long v = idealval(nf,ix,P);
     731        4695 :   return v == LONG_MAX? mkoo(): stoi(v);
     732             : }
     733             : 
     734             : /* gcd and generalized Bezout */
     735             : 
     736             : GEN
     737       20160 : idealadd(GEN nf, GEN x, GEN y)
     738             : {
     739       20160 :   pari_sp av = avma;
     740             :   long tx, ty;
     741             :   GEN z, a, dx, dy, dz;
     742             : 
     743       20160 :   tx = idealtyp(&x,&z);
     744       20160 :   ty = idealtyp(&y,&z); nf = checknf(nf);
     745       20160 :   if (tx != id_MAT) x = idealhnf_shallow(nf,x);
     746       20160 :   if (ty != id_MAT) y = idealhnf_shallow(nf,y);
     747       20160 :   if (lg(x) == 1) return gerepilecopy(av,y);
     748       20160 :   if (lg(y) == 1) return gerepilecopy(av,x); /* check for 0 ideal */
     749       20160 :   dx = Q_denom(x);
     750       20160 :   dy = Q_denom(y); dz = lcmii(dx,dy);
     751       20160 :   if (is_pm1(dz)) dz = NULL; else {
     752        2800 :     x = Q_muli_to_int(x, dz);
     753        2800 :     y = Q_muli_to_int(y, dz);
     754             :   }
     755       20160 :   a = gcdii(gcoeff(x,1,1), gcoeff(y,1,1));
     756       20160 :   if (is_pm1(a))
     757             :   {
     758        7980 :     long N = lg(x)-1;
     759        7980 :     if (!dz) { avma = av; return matid(N); }
     760         495 :     return gerepileupto(av, scalarmat(ginv(dz), N));
     761             :   }
     762       12180 :   z = ZM_hnfmodid(shallowconcat(x,y), a);
     763       12180 :   if (dz) z = RgM_Rg_div(z,dz);
     764       12180 :   return gerepileupto(av,z);
     765             : }
     766             : 
     767             : static GEN
     768          20 : trivial_merge(GEN x)
     769          20 : { return (lg(x) == 1 || !is_pm1(gcoeff(x,1,1)))? NULL: gen_1; }
     770             : /* true nf */
     771             : static GEN
     772      103100 : _idealaddtoone(GEN nf, GEN x, GEN y, long red)
     773             : {
     774             :   GEN a;
     775      103100 :   long tx = idealtyp(&x, &a/*junk*/);
     776      103100 :   long ty = idealtyp(&y, &a/*junk*/);
     777             :   long ea;
     778      103100 :   if (tx != id_MAT) x = idealhnf_shallow(nf, x);
     779      103100 :   if (ty != id_MAT) y = idealhnf_shallow(nf, y);
     780      103100 :   if (lg(x) == 1)
     781          10 :     a = trivial_merge(y);
     782      103090 :   else if (lg(y) == 1)
     783          10 :     a = trivial_merge(x);
     784             :   else
     785      103080 :     a = hnfmerge_get_1(x, y);
     786      103100 :   if (!a) pari_err_COPRIME("idealaddtoone",x,y);
     787      103090 :   if (red && (ea = gexpo(a)) > 10)
     788             :   {
     789        4005 :     GEN b = (typ(a) == t_COL)? a: scalarcol_shallow(a, nf_get_degree(nf));
     790        4005 :     b = ZC_reducemodlll(b, idealHNF_mul(nf,x,y));
     791        4005 :     if (gexpo(b) < ea) a = b;
     792             :   }
     793      103090 :   return a;
     794             : }
     795             : /* true nf */
     796             : GEN
     797        5355 : idealaddtoone_i(GEN nf, GEN x, GEN y)
     798        5355 : { return _idealaddtoone(nf, x, y, 1); }
     799             : /* true nf */
     800             : GEN
     801       97745 : idealaddtoone_raw(GEN nf, GEN x, GEN y)
     802       97745 : { return _idealaddtoone(nf, x, y, 0); }
     803             : 
     804             : GEN
     805          70 : idealaddtoone(GEN nf, GEN x, GEN y)
     806             : {
     807          70 :   GEN z = cgetg(3,t_VEC), a;
     808          70 :   pari_sp av = avma;
     809          70 :   nf = checknf(nf);
     810          70 :   a = gerepileupto(av, idealaddtoone_i(nf,x,y));
     811          60 :   gel(z,1) = a;
     812          60 :   gel(z,2) = typ(a) == t_COL? Z_ZC_sub(gen_1,a): subui(1,a);
     813          60 :   return z;
     814             : }
     815             : 
     816             : /* assume elements of list are integral ideals */
     817             : GEN
     818          25 : idealaddmultoone(GEN nf, GEN list)
     819             : {
     820          25 :   pari_sp av = avma;
     821          25 :   long N, i, l, nz, tx = typ(list);
     822             :   GEN H, U, perm, L;
     823             : 
     824          25 :   nf = checknf(nf); N = nf_get_degree(nf);
     825          25 :   if (!is_vec_t(tx)) pari_err_TYPE("idealaddmultoone",list);
     826          25 :   l = lg(list);
     827          25 :   L = cgetg(l, t_VEC);
     828          25 :   if (l == 1)
     829           0 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
     830          25 :   nz = 0; /* number of non-zero ideals in L */
     831          70 :   for (i=1; i<l; i++)
     832             :   {
     833          50 :     GEN I = gel(list,i);
     834          50 :     if (typ(I) != t_MAT) I = idealhnf_shallow(nf,I);
     835          50 :     if (lg(I) != 1)
     836             :     {
     837          30 :       nz++; RgM_check_ZM(I,"idealaddmultoone");
     838          25 :       if (lgcols(I) != N+1) pari_err_TYPE("idealaddmultoone [not an ideal]", I);
     839             :     }
     840          45 :     gel(L,i) = I;
     841             :   }
     842          20 :   H = ZM_hnfperm(shallowconcat1(L), &U, &perm);
     843          20 :   if (lg(H) == 1 || !equali1(gcoeff(H,1,1)))
     844           5 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
     845          35 :   for (i=1; i<=N; i++)
     846          35 :     if (perm[i] == 1) break;
     847          15 :   U = gel(U,(nz-1)*N + i); /* (L[1]|...|L[nz]) U = 1 */
     848          15 :   nz = 0;
     849          45 :   for (i=1; i<l; i++)
     850             :   {
     851          30 :     GEN c = gel(L,i);
     852          30 :     if (lg(c) == 1)
     853          10 :       c = gen_0;
     854             :     else {
     855          20 :       c = ZM_ZC_mul(c, vecslice(U, nz*N + 1, (nz+1)*N));
     856          20 :       nz++;
     857             :     }
     858          30 :     gel(L,i) = c;
     859             :   }
     860          15 :   return gerepilecopy(av, L);
     861             : }
     862             : 
     863             : /* multiplication */
     864             : 
     865             : /* x integral ideal (without archimedean component) in HNF form
     866             :  * y = [a,alpha] corresponds to the integral ideal aZ_K+alpha Z_K, a in Z,
     867             :  * alpha a ZV or a ZM (multiplication table). Multiply them */
     868             : static GEN
     869      409910 : idealHNF_mul_two(GEN nf, GEN x, GEN y)
     870             : {
     871      409910 :   GEN m, a = gel(y,1), alpha = gel(y,2);
     872             :   long i, N;
     873             : 
     874      409910 :   if (typ(alpha) != t_MAT)
     875             :   {
     876      289765 :     alpha = zk_scalar_or_multable(nf, alpha);
     877      289765 :     if (typ(alpha) == t_INT) /* e.g. y inert ? 0 should not (but may) occur */
     878        1690 :       return signe(a)? ZM_Z_mul(x, gcdii(a, alpha)): cgetg(1,t_MAT);
     879             :   }
     880      408220 :   N = lg(x)-1; m = cgetg((N<<1)+1,t_MAT);
     881      408220 :   for (i=1; i<=N; i++) gel(m,i)   = ZM_ZC_mul(alpha,gel(x,i));
     882      408220 :   for (i=1; i<=N; i++) gel(m,i+N) = ZC_Z_mul(gel(x,i), a);
     883      408220 :   return ZM_hnfmodid(m, mulii(a, gcoeff(x,1,1)));
     884             : }
     885             : 
     886             : /* Assume ix and iy are integral in HNF form [NOT extended]. Not memory clean.
     887             :  * HACK: ideal in iy can be of the form [a,b], a in Z, b in Z_K */
     888             : GEN
     889      183525 : idealHNF_mul(GEN nf, GEN x, GEN y)
     890             : {
     891             :   GEN z;
     892      183525 :   if (typ(y) == t_VEC)
     893      129470 :     z = idealHNF_mul_two(nf,x,y);
     894             :   else
     895             :   { /* reduce one ideal to two-elt form. The smallest */
     896       54055 :     GEN xZ = gcoeff(x,1,1), yZ = gcoeff(y,1,1);
     897       54055 :     if (cmpii(xZ, yZ) < 0)
     898             :     {
     899       19595 :       if (is_pm1(xZ)) return gcopy(y);
     900       14490 :       z = idealHNF_mul_two(nf, y, mat_ideal_two_elt(nf,x));
     901             :     }
     902             :     else
     903             :     {
     904       34460 :       if (is_pm1(yZ)) return gcopy(x);
     905       23990 :       z = idealHNF_mul_two(nf, x, mat_ideal_two_elt(nf,y));
     906             :     }
     907             :   }
     908      167950 :   return z;
     909             : }
     910             : 
     911             : /* operations on elements in factored form */
     912             : 
     913             : GEN
     914       63060 : famat_mul_shallow(GEN f, GEN g)
     915             : {
     916       63060 :   if (typ(f) != t_MAT) f = to_famat_shallow(f,gen_1);
     917       63060 :   if (typ(g) != t_MAT) g = to_famat_shallow(g,gen_1);
     918       63060 :   if (lg(f) == 1) return g;
     919       49600 :   if (lg(g) == 1) return f;
     920       97250 :   return mkmat2(shallowconcat(gel(f,1), gel(g,1)),
     921       97250 :                 shallowconcat(gel(f,2), gel(g,2)));
     922             : }
     923             : GEN
     924       42150 : famat_mulpow_shallow(GEN f, GEN g, GEN e)
     925             : {
     926       42150 :   if (!signe(e)) return f;
     927       40865 :   return famat_mul_shallow(f, famat_pow_shallow(g, e));
     928             : }
     929             : 
     930             : GEN
     931           0 : to_famat(GEN x, GEN y) { retmkmat2(mkcolcopy(x), mkcolcopy(y)); }
     932             : GEN
     933      562390 : to_famat_shallow(GEN x, GEN y) { return mkmat2(mkcol(x), mkcol(y)); }
     934             : 
     935             : /* concat the single elt x; not gconcat since x may be a t_COL */
     936             : static GEN
     937       42015 : append(GEN v, GEN x)
     938             : {
     939       42015 :   long i, l = lg(v);
     940       42015 :   GEN w = cgetg(l+1, typ(v));
     941       42015 :   for (i=1; i<l; i++) gel(w,i) = gcopy(gel(v,i));
     942       42015 :   gel(w,i) = gcopy(x); return w;
     943             : }
     944             : /* add x^1 to famat f */
     945             : static GEN
     946       57660 : famat_add(GEN f, GEN x)
     947             : {
     948       57660 :   GEN h = cgetg(3,t_MAT);
     949       57660 :   if (lg(f) == 1)
     950             :   {
     951       15645 :     gel(h,1) = mkcolcopy(x);
     952       15645 :     gel(h,2) = mkcol(gen_1);
     953             :   }
     954             :   else
     955             :   {
     956       42015 :     gel(h,1) = append(gel(f,1), x);
     957       42015 :     gel(h,2) = gconcat(gel(f,2), gen_1);
     958             :   }
     959       57660 :   return h;
     960             : }
     961             : 
     962             : GEN
     963       74700 : famat_mul(GEN f, GEN g)
     964             : {
     965             :   GEN h;
     966       74700 :   if (typ(g) != t_MAT) {
     967       57660 :     if (typ(f) == t_MAT) return famat_add(f, g);
     968           0 :     h = cgetg(3, t_MAT);
     969           0 :     gel(h,1) = mkcol2(gcopy(f), gcopy(g));
     970           0 :     gel(h,2) = mkcol2(gen_1, gen_1);
     971             :   }
     972       17040 :   if (typ(f) != t_MAT) return famat_add(g, f);
     973       17040 :   if (lg(f) == 1) return gcopy(g);
     974        2945 :   if (lg(g) == 1) return gcopy(f);
     975        1315 :   h = cgetg(3,t_MAT);
     976        1315 :   gel(h,1) = gconcat(gel(f,1), gel(g,1));
     977        1315 :   gel(h,2) = gconcat(gel(f,2), gel(g,2));
     978        1315 :   return h;
     979             : }
     980             : 
     981             : GEN
     982       35960 : famat_sqr(GEN f)
     983             : {
     984             :   GEN h;
     985       35960 :   if (lg(f) == 1) return cgetg(1,t_MAT);
     986       18030 :   if (typ(f) != t_MAT) return to_famat(f,gen_2);
     987       18030 :   h = cgetg(3,t_MAT);
     988       18030 :   gel(h,1) = gcopy(gel(f,1));
     989       18030 :   gel(h,2) = gmul2n(gel(f,2),1);
     990       18030 :   return h;
     991             : }
     992             : 
     993             : GEN
     994       17935 : famat_inv_shallow(GEN f)
     995             : {
     996       17935 :   if (lg(f) == 1) return f;
     997       17935 :   if (typ(f) != t_MAT) return to_famat_shallow(f,gen_m1);
     998          10 :   return mkmat2(gel(f,1), ZC_neg(gel(f,2)));
     999             : }
    1000             : GEN
    1001        3085 : famat_inv(GEN f)
    1002             : {
    1003        3085 :   if (lg(f) == 1) return cgetg(1,t_MAT);
    1004        1710 :   if (typ(f) != t_MAT) return to_famat(f,gen_m1);
    1005        1710 :   retmkmat2(gcopy(gel(f,1)), ZC_neg(gel(f,2)));
    1006             : }
    1007             : GEN
    1008         290 : famat_pow(GEN f, GEN n)
    1009             : {
    1010         290 :   if (lg(f) == 1) return cgetg(1,t_MAT);
    1011           0 :   if (typ(f) != t_MAT) return to_famat(f,n);
    1012           0 :   retmkmat2(gcopy(gel(f,1)), ZC_Z_mul(gel(f,2),n));
    1013             : }
    1014             : GEN
    1015       40865 : famat_pow_shallow(GEN f, GEN n)
    1016             : {
    1017       40865 :   if (is_pm1(n)) return signe(n) > 0? f: famat_inv_shallow(f);
    1018       20750 :   if (lg(f) == 1) return f;
    1019       20750 :   if (typ(f) != t_MAT) return to_famat_shallow(f,n);
    1020         885 :   return mkmat2(gel(f,1), ZC_Z_mul(gel(f,2),n));
    1021             : }
    1022             : 
    1023             : GEN
    1024           0 : famat_Z_gcd(GEN M, GEN n)
    1025             : {
    1026           0 :   pari_sp av=avma;
    1027           0 :   long i, j, l=lgcols(M);
    1028           0 :   GEN F=cgetg(3,t_MAT);
    1029           0 :   gel(F,1)=cgetg(l,t_COL);
    1030           0 :   gel(F,2)=cgetg(l,t_COL);
    1031           0 :   for (i=1, j=1; i<l; i++)
    1032             :   {
    1033           0 :     GEN p = gcoeff(M,i,1);
    1034           0 :     GEN e = gminsg(Z_pval(n,p),gcoeff(M,i,2));
    1035           0 :     if (signe(e))
    1036             :     {
    1037           0 :       gcoeff(F,j,1)=p;
    1038           0 :       gcoeff(F,j,2)=e;
    1039           0 :       j++;
    1040             :     }
    1041             :   }
    1042           0 :   setlg(gel(F,1),j); setlg(gel(F,2),j);
    1043           0 :   return gerepilecopy(av,F);
    1044             : }
    1045             : 
    1046             : /* x assumed to be a t_MATs (factorization matrix), or compatible with
    1047             :  * the element_* functions. */
    1048             : static GEN
    1049       43510 : ext_sqr(GEN nf, GEN x)
    1050       43510 : { return (typ(x)==t_MAT)? famat_sqr(x): nfsqr(nf, x); }
    1051             : static GEN
    1052       99490 : ext_mul(GEN nf, GEN x, GEN y)
    1053       99490 : { return (typ(x)==t_MAT)? famat_mul(x,y): nfmul(nf, x, y); }
    1054             : static GEN
    1055        2985 : ext_inv(GEN nf, GEN x)
    1056        2985 : { return (typ(x)==t_MAT)? famat_inv(x): nfinv(nf, x); }
    1057             : static GEN
    1058         290 : ext_pow(GEN nf, GEN x, GEN n)
    1059         290 : { return (typ(x)==t_MAT)? famat_pow(x,n): nfpow(nf, x, n); }
    1060             : 
    1061             : GEN
    1062           0 : famat_to_nf(GEN nf, GEN f)
    1063             : {
    1064             :   GEN t, x, e;
    1065             :   long i;
    1066           0 :   if (lg(f) == 1) return gen_1;
    1067             : 
    1068           0 :   x = gel(f,1);
    1069           0 :   e = gel(f,2);
    1070           0 :   t = nfpow(nf, gel(x,1), gel(e,1));
    1071           0 :   for (i=lg(x)-1; i>1; i--)
    1072           0 :     t = nfmul(nf, t, nfpow(nf, gel(x,i), gel(e,i)));
    1073           0 :   return t;
    1074             : }
    1075             : 
    1076             : GEN
    1077        1890 : famat_reduce(GEN fa)
    1078             : {
    1079             :   GEN E, G, L, g, e;
    1080             :   long i, k, l;
    1081             : 
    1082        1890 :   if (lg(fa) == 1) return fa;
    1083        1850 :   g = gel(fa,1); l = lg(g);
    1084        1850 :   e = gel(fa,2);
    1085        1850 :   L = gen_indexsort(g, (void*)&cmp_universal, &cmp_nodata);
    1086        1850 :   G = cgetg(l, t_COL);
    1087        1850 :   E = cgetg(l, t_COL);
    1088             :   /* merge */
    1089        7850 :   for (k=i=1; i<l; i++,k++)
    1090             :   {
    1091        6000 :     gel(G,k) = gel(g,L[i]);
    1092        6000 :     gel(E,k) = gel(e,L[i]);
    1093        6000 :     if (k > 1 && gidentical(gel(G,k), gel(G,k-1)))
    1094             :     {
    1095         530 :       gel(E,k-1) = addii(gel(E,k), gel(E,k-1));
    1096         530 :       k--;
    1097             :     }
    1098             :   }
    1099             :   /* kill 0 exponents */
    1100        1850 :   l = k;
    1101        7320 :   for (k=i=1; i<l; i++)
    1102        5470 :     if (!gequal0(gel(E,i)))
    1103             :     {
    1104        5040 :       gel(G,k) = gel(G,i);
    1105        5040 :       gel(E,k) = gel(E,i); k++;
    1106             :     }
    1107        1850 :   setlg(G, k);
    1108        1850 :   setlg(E, k); return mkmat2(G,E);
    1109             : }
    1110             : 
    1111             : GEN
    1112        8940 : famatsmall_reduce(GEN fa)
    1113             : {
    1114             :   GEN E, G, L, g, e;
    1115             :   long i, k, l;
    1116        8940 :   if (lg(fa) == 1) return fa;
    1117        8940 :   g = gel(fa,1); l = lg(g);
    1118        8940 :   e = gel(fa,2);
    1119        8940 :   L = vecsmall_indexsort(g);
    1120        8940 :   G = cgetg(l, t_VECSMALL);
    1121        8940 :   E = cgetg(l, t_VECSMALL);
    1122             :   /* merge */
    1123       79845 :   for (k=i=1; i<l; i++,k++)
    1124             :   {
    1125       70905 :     G[k] = g[L[i]];
    1126       70905 :     E[k] = e[L[i]];
    1127       70905 :     if (k > 1 && G[k] == G[k-1])
    1128             :     {
    1129        4130 :       E[k-1] += E[k];
    1130        4130 :       k--;
    1131             :     }
    1132             :   }
    1133             :   /* kill 0 exponents */
    1134        8940 :   l = k;
    1135       75715 :   for (k=i=1; i<l; i++)
    1136       66775 :     if (E[i])
    1137             :     {
    1138       64740 :       G[k] = G[i];
    1139       64740 :       E[k] = E[i]; k++;
    1140             :     }
    1141        8940 :   setlg(G, k);
    1142        8940 :   setlg(E, k); return mkmat2(G,E);
    1143             : }
    1144             : 
    1145             : GEN
    1146       34455 : ZM_famat_limit(GEN fa, GEN limit)
    1147             : {
    1148             :   pari_sp av;
    1149             :   GEN E, G, g, e, r;
    1150             :   long i, k, l, n, lG;
    1151             : 
    1152       34455 :   if (lg(fa) == 1) return fa;
    1153       34455 :   g = gel(fa,1); l = lg(g);
    1154       34455 :   e = gel(fa,2);
    1155       77690 :   for(n=0, i=1; i<l; i++)
    1156       43235 :     if (cmpii(gel(g,i),limit)<=0) n++;
    1157       34455 :   lG = n<l-1 ? n+2 : n+1;
    1158       34455 :   G = cgetg(lG, t_COL);
    1159       34455 :   E = cgetg(lG, t_COL);
    1160       34455 :   av = avma;
    1161       77690 :   for (i=1, k=1, r = gen_1; i<l; i++)
    1162             :   {
    1163       43235 :     if (cmpii(gel(g,i),limit)<=0)
    1164             :     {
    1165       43175 :       gel(G,k) = gel(g,i);
    1166       43175 :       gel(E,k) = gel(e,i);
    1167       43175 :       k++;
    1168          60 :     } else r = mulii(r, powii(gel(g,i), gel(e,i)));
    1169             :   }
    1170       34455 :   if (k<i)
    1171             :   {
    1172          60 :     gel(G, k) = gerepileuptoint(av, r);
    1173          60 :     gel(E, k) = gen_1;
    1174             :   }
    1175       34455 :   return mkmat2(G,E);
    1176             : }
    1177             : 
    1178             : /* assume pr has degree 1 and coprime to Q_denom(x) */
    1179             : static GEN
    1180        3644 : to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1181             : {
    1182        3644 :   GEN d, r, p = modpr_get_p(modpr);
    1183        3644 :   x = nf_to_scalar_or_basis(nf,x);
    1184        3644 :   if (typ(x) != t_COL) return Rg_to_Fp(x,p);
    1185        3384 :   x = Q_remove_denom(x, &d);
    1186        3384 :   r = zk_to_Fq(x, modpr);
    1187        3384 :   if (d) r = Fp_div(r, d, p);
    1188        3384 :   return r;
    1189             : }
    1190             : 
    1191             : /* pr coprime to all denominators occurring in x */
    1192             : static GEN
    1193         564 : famat_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1194             : {
    1195         564 :   GEN p = modpr_get_p(modpr);
    1196         564 :   GEN t = NULL, g = gel(x,1), e = gel(x,2), q = subiu(p,1);
    1197         564 :   long i, l = lg(g);
    1198        1738 :   for (i = 1; i < l; i++)
    1199             :   {
    1200        1174 :     GEN n = modii(gel(e,i), q);
    1201        1174 :     if (signe(n))
    1202             :     {
    1203        1174 :       GEN h = to_Fp_coprime(nf, gel(g,i), modpr);
    1204        1174 :       h = Fp_pow(h, n, p);
    1205        1174 :       t = t? Fp_mul(t, h, p): h;
    1206             :     }
    1207             :   }
    1208         564 :   return t? modii(t, p): gen_1;
    1209             : }
    1210             : 
    1211             : /* cf famat_to_nf_modideal_coprime, modpr attached to prime of degree 1 */
    1212             : GEN
    1213        3034 : nf_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1214             : {
    1215        6068 :   return typ(x)==t_MAT? famat_to_Fp_coprime(nf, x, modpr)
    1216        3034 :                       : to_Fp_coprime(nf, x, modpr);
    1217             : }
    1218             : 
    1219             : static long
    1220       83535 : zk_pvalrem(GEN x, GEN p, GEN *py)
    1221       83535 : { return (typ(x) == t_INT)? Z_pvalrem(x, p, py): ZV_pvalrem(x, p, py); }
    1222             : /* x a QC or Q. Return a ZC or Z, whose content is coprime to Z. Set v, dx
    1223             :  * such that x = p^v (newx / dx); dx = NULL if 1 */
    1224             : static GEN
    1225      170820 : nf_remove_denom_p(GEN nf, GEN x, GEN p, GEN *pdx, long *pv)
    1226             : {
    1227             :   long vcx;
    1228             :   GEN dx;
    1229      170820 :   x = nf_to_scalar_or_basis(nf, x);
    1230      170820 :   x = Q_remove_denom(x, &dx);
    1231      170820 :   if (dx)
    1232             :   {
    1233      116585 :     vcx = - Z_pvalrem(dx, p, &dx);
    1234      116585 :     if (!vcx) vcx = zk_pvalrem(x, p, &x);
    1235      116585 :     if (isint1(dx)) dx = NULL;
    1236             :   }
    1237             :   else
    1238             :   {
    1239       54235 :     vcx = zk_pvalrem(x, p, &x);
    1240       54235 :     dx = NULL;
    1241             :   }
    1242      170820 :   *pv = vcx;
    1243      170820 :   *pdx = dx; return x;
    1244             : }
    1245             : /* x = b^e/p^(e-1) in Z_K; x = 0 mod p/pr^e, (x,pr) = 1. Return NULL
    1246             :  * if p inert (instead of 1) */
    1247             : static GEN
    1248       41625 : p_makecoprime(GEN pr)
    1249             : {
    1250       41625 :   GEN B = pr_get_tau(pr), b;
    1251             :   long i, e;
    1252             : 
    1253       41625 :   if (typ(B) == t_INT) return NULL;
    1254       41525 :   b = gel(B,1); /* B = multiplication table by b */
    1255       41525 :   e = pr_get_e(pr);
    1256       41525 :   if (e == 1) return b;
    1257             :   /* one could also divide (exactly) by p in each iteration */
    1258       11470 :   for (i = 1; i < e; i++) b = ZM_ZC_mul(B, b);
    1259       11470 :   return ZC_Z_divexact(b, powiu(pr_get_p(pr), e-1));
    1260             : }
    1261             : 
    1262             : /* Compute A = prod g[i]^e[i] mod pr^k, assuming (A, pr) = 1.
    1263             :  * Method: modify each g[i] so that it becomes coprime to pr,
    1264             :  * g[i] *= (b/p)^v_pr(g[i]), where b/p = pr^(-1) times something integral
    1265             :  * and prime to p; globally, we multiply by (b/p)^v_pr(A) = 1.
    1266             :  * Optimizations:
    1267             :  * 1) remove all powers of p from contents, and consider extra generator p^vp;
    1268             :  * modified as p * (b/p)^e = b^e / p^(e-1)
    1269             :  * 2) remove denominators, coprime to p, by multiplying by inverse mod prk\cap Z
    1270             :  *
    1271             :  * EX = multiple of exponent of (O_K / pr^k)^* used to reduce the product in
    1272             :  * case the e[i] are large */
    1273             : GEN
    1274       70385 : famat_makecoprime(GEN nf, GEN g, GEN e, GEN pr, GEN prk, GEN EX)
    1275             : {
    1276       70385 :   GEN G, E, t, vp = NULL, p = pr_get_p(pr), prkZ = gcoeff(prk, 1,1);
    1277       70385 :   long i, l = lg(g);
    1278             : 
    1279       70385 :   G = cgetg(l+1, t_VEC);
    1280       70385 :   E = cgetg(l+1, t_VEC); /* l+1: room for "modified p" */
    1281      241205 :   for (i=1; i < l; i++)
    1282             :   {
    1283             :     long vcx;
    1284      170820 :     GEN dx, x = nf_remove_denom_p(nf, gel(g,i), p, &dx, &vcx);
    1285      170820 :     if (vcx) /* = v_p(content(g[i])) */
    1286             :     {
    1287       87850 :       GEN a = mulsi(vcx, gel(e,i));
    1288       87850 :       vp = vp? addii(vp, a): a;
    1289             :     }
    1290             :     /* x integral, content coprime to p; dx coprime to p */
    1291      170820 :     if (typ(x) == t_INT)
    1292             :     { /* x coprime to p, hence to pr */
    1293       21935 :       x = modii(x, prkZ);
    1294       21935 :       if (dx) x = Fp_div(x, dx, prkZ);
    1295             :     }
    1296             :     else
    1297             :     {
    1298      148885 :       (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
    1299      148885 :       x = ZC_hnfrem(FpC_red(x,prkZ), prk);
    1300      148885 :       if (dx) x = FpC_Fp_mul(x, Fp_inv(dx,prkZ), prkZ);
    1301             :     }
    1302      170820 :     gel(G,i) = x;
    1303      170820 :     gel(E,i) = gel(e,i);
    1304             :   }
    1305             : 
    1306       70385 :   t = vp? p_makecoprime(pr): NULL;
    1307       70385 :   if (!t)
    1308             :   { /* no need for extra generator */
    1309       28860 :     setlg(G,l);
    1310       28860 :     setlg(E,l);
    1311             :   }
    1312             :   else
    1313             :   {
    1314       41525 :     gel(G,i) = FpC_red(t, prkZ);
    1315       41525 :     gel(E,i) = vp;
    1316             :   }
    1317       70385 :   return famat_to_nf_modideal_coprime(nf, G, E, prk, EX);
    1318             : }
    1319             : 
    1320             : /* prod g[i]^e[i] mod bid, assume (g[i], id) = 1 */
    1321             : GEN
    1322         770 : famat_to_nf_moddivisor(GEN nf, GEN g, GEN e, GEN bid)
    1323             : {
    1324             :   GEN t, cyc;
    1325         770 :   if (lg(g) == 1) return gen_1;
    1326         770 :   cyc = bid_get_cyc(bid);
    1327         770 :   if (lg(cyc) == 1)
    1328           0 :     t = gen_1;
    1329             :   else
    1330         770 :     t = famat_to_nf_modideal_coprime(nf, g, e, bid_get_ideal(bid), gel(cyc,1));
    1331         770 :   return set_sign_mod_divisor(nf, mkmat2(g,e), t, bid_get_sarch(bid));
    1332             : }
    1333             : 
    1334             : GEN
    1335      127160 : vecmul(GEN x, GEN y)
    1336             : {
    1337      127160 :   long i,lx, tx = typ(x);
    1338             :   GEN z;
    1339      127160 :   if (is_scalar_t(tx)) return gmul(x,y);
    1340       10970 :   z = cgetg_copy(x, &lx);
    1341       10970 :   for (i=1; i<lx; i++) gel(z,i) = vecmul(gel(x,i), gel(y,i));
    1342       10970 :   return z;
    1343             : }
    1344             : 
    1345             : GEN
    1346           0 : vecinv(GEN x)
    1347             : {
    1348           0 :   long i,lx, tx = typ(x);
    1349             :   GEN z;
    1350           0 :   if (is_scalar_t(tx)) return ginv(x);
    1351           0 :   z = cgetg_copy(x, &lx);
    1352           0 :   for (i=1; i<lx; i++) gel(z,i) = vecinv(gel(x,i));
    1353           0 :   return z;
    1354             : }
    1355             : 
    1356             : GEN
    1357       11235 : vecpow(GEN x, GEN n)
    1358             : {
    1359       11235 :   long i,lx, tx = typ(x);
    1360             :   GEN z;
    1361       11235 :   if (is_scalar_t(tx)) return powgi(x,n);
    1362        3050 :   z = cgetg_copy(x, &lx);
    1363        3050 :   for (i=1; i<lx; i++) gel(z,i) = vecpow(gel(x,i), n);
    1364        3050 :   return z;
    1365             : }
    1366             : 
    1367             : GEN
    1368         645 : vecdiv(GEN x, GEN y)
    1369             : {
    1370         645 :   long i,lx, tx = typ(x);
    1371             :   GEN z;
    1372         645 :   if (is_scalar_t(tx)) return gdiv(x,y);
    1373         215 :   z = cgetg_copy(x, &lx);
    1374         215 :   for (i=1; i<lx; i++) gel(z,i) = vecdiv(gel(x,i), gel(y,i));
    1375         215 :   return z;
    1376             : }
    1377             : 
    1378             : /* A ideal as a square t_MAT */
    1379             : static GEN
    1380       91890 : idealmulelt(GEN nf, GEN x, GEN A)
    1381             : {
    1382             :   long i, lx;
    1383             :   GEN dx, dA, D;
    1384       91890 :   if (lg(A) == 1) return cgetg(1, t_MAT);
    1385       91890 :   x = nf_to_scalar_or_basis(nf,x);
    1386       91890 :   if (typ(x) != t_COL)
    1387       16375 :     return isintzero(x)? cgetg(1,t_MAT): RgM_Rg_mul(A, Q_abs_shallow(x));
    1388       75515 :   x = Q_remove_denom(x, &dx);
    1389       75515 :   A = Q_remove_denom(A, &dA);
    1390       75515 :   x = zk_multable(nf, x);
    1391       75515 :   D = mulii(zkmultable_capZ(x), gcoeff(A,1,1));
    1392       75515 :   x = zkC_multable_mul(A, x);
    1393       75515 :   settyp(x, t_MAT); lx = lg(x);
    1394             :   /* x may contain scalars (at most 1 since the ideal is non-0)*/
    1395      272335 :   for (i=1; i<lx; i++)
    1396      198255 :     if (typ(gel(x,i)) == t_INT)
    1397             :     {
    1398        1435 :       if (i > 1) swap(gel(x,1), gel(x,i)); /* help HNF */
    1399        1435 :       gel(x,1) = scalarcol_shallow(gel(x,1), lx-1);
    1400        1435 :       break;
    1401             :     }
    1402       75515 :   x = ZM_hnfmodid(x, D);
    1403       75515 :   dx = mul_denom(dx,dA);
    1404       75515 :   return dx? gdiv(x,dx): x;
    1405             : }
    1406             : 
    1407             : /* nf a true nf, tx <= ty */
    1408             : static GEN
    1409      375550 : idealmul_aux(GEN nf, GEN x, GEN y, long tx, long ty)
    1410             : {
    1411             :   GEN z, cx, cy;
    1412      375550 :   switch(tx)
    1413             :   {
    1414             :     case id_PRINCIPAL:
    1415      109710 :       switch(ty)
    1416             :       {
    1417             :         case id_PRINCIPAL:
    1418       17730 :           return idealhnf_principal(nf, nfmul(nf,x,y));
    1419             :         case id_PRIME:
    1420             :         {
    1421          90 :           GEN p = pr_get_p(y), pi = pr_get_gen(y), cx;
    1422          90 :           if (pr_is_inert(y)) return RgM_Rg_mul(idealhnf_principal(nf,x),p);
    1423             : 
    1424          30 :           x = nf_to_scalar_or_basis(nf, x);
    1425          30 :           switch(typ(x))
    1426             :           {
    1427             :             case t_INT:
    1428          20 :               if (!signe(x)) return cgetg(1,t_MAT);
    1429          20 :               return ZM_Z_mul(pr_hnf(nf,y), absi_shallow(x));
    1430             :             case t_FRAC:
    1431           5 :               return RgM_Rg_mul(pr_hnf(nf,y), Q_abs_shallow(x));
    1432             :           }
    1433             :           /* t_COL */
    1434           5 :           x = Q_primitive_part(x, &cx);
    1435           5 :           x = zk_multable(nf, x);
    1436           5 :           z = shallowconcat(ZM_Z_mul(x,p), ZM_ZC_mul(x,pi));
    1437           5 :           z = ZM_hnfmodid(z, mulii(p, zkmultable_capZ(x)));
    1438           5 :           return cx? ZM_Q_mul(z, cx): z;
    1439             :         }
    1440             :         default: /* id_MAT */
    1441       91890 :           return idealmulelt(nf, x,y);
    1442             :       }
    1443             :     case id_PRIME:
    1444      227600 :       if (ty==id_PRIME)
    1445      211995 :       { y = pr_hnf(nf,y); cy = NULL; }
    1446             :       else
    1447       15605 :         y = Q_primitive_part(y, &cy);
    1448      227600 :       y = idealHNF_mul_two(nf,y,x);
    1449      227600 :       return cy? RgM_Rg_mul(y,cy): y;
    1450             : 
    1451             :     default: /* id_MAT */
    1452             :     {
    1453       38240 :       long N = nf_get_degree(nf);
    1454       38240 :       if (lg(x)-1 != N || lg(y)-1 != N) pari_err_DIM("idealmul");
    1455       38230 :       x = Q_primitive_part(x, &cx);
    1456       38230 :       y = Q_primitive_part(y, &cy); cx = mul_content(cx,cy);
    1457       38230 :       y = idealHNF_mul(nf,x,y);
    1458       38230 :       return cx? ZM_Q_mul(y,cx): y;
    1459             :     }
    1460             :   }
    1461             : }
    1462             : 
    1463             : /* output the ideal product ix.iy */
    1464             : GEN
    1465      375550 : idealmul(GEN nf, GEN x, GEN y)
    1466             : {
    1467             :   pari_sp av;
    1468             :   GEN res, ax, ay, z;
    1469      375550 :   long tx = idealtyp(&x,&ax);
    1470      375550 :   long ty = idealtyp(&y,&ay), f;
    1471      375550 :   if (tx>ty) { swap(ax,ay); swap(x,y); lswap(tx,ty); }
    1472      375550 :   f = (ax||ay); res = f? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1473      375550 :   av = avma;
    1474      375550 :   z = gerepileupto(av, idealmul_aux(checknf(nf), x,y, tx,ty));
    1475      375540 :   if (!f) return z;
    1476       30295 :   if (ax && ay)
    1477       29110 :     ax = ext_mul(nf, ax, ay);
    1478             :   else
    1479        1185 :     ax = gcopy(ax? ax: ay);
    1480       30295 :   gel(res,1) = z; gel(res,2) = ax; return res;
    1481             : }
    1482             : 
    1483             : /* Return x, integral in 2-elt form, such that pr^2 = c * x. cf idealpowprime
    1484             :  * nf = true nf */
    1485             : static GEN
    1486       25590 : idealsqrprime(GEN nf, GEN pr, GEN *pc)
    1487             : {
    1488       25590 :   GEN p = pr_get_p(pr), q, gen;
    1489       25590 :   long e = pr_get_e(pr), f = pr_get_f(pr);
    1490             : 
    1491       25590 :   q = (e == 1)? sqri(p): p;
    1492       25590 :   if (e <= 2 && e * f == nf_get_degree(nf))
    1493             :   { /* pr^e = (p) */
    1494        5140 :     *pc = q;
    1495        5140 :     return mkvec2(gen_1,gen_0);
    1496             :   }
    1497       20450 :   gen = nfsqr(nf, pr_get_gen(pr));
    1498       20450 :   gen = FpC_red(gen, q);
    1499       20450 :   *pc = NULL;
    1500       20450 :   return mkvec2(q, gen);
    1501             : }
    1502             : /* cf idealpow_aux */
    1503             : static GEN
    1504       43820 : idealsqr_aux(GEN nf, GEN x, long tx)
    1505             : {
    1506       43820 :   GEN T = nf_get_pol(nf), m, cx, a, alpha;
    1507       43820 :   long N = degpol(T);
    1508       43820 :   switch(tx)
    1509             :   {
    1510             :     case id_PRINCIPAL:
    1511          50 :       return idealhnf_principal(nf, nfsqr(nf,x));
    1512             :     case id_PRIME:
    1513       16700 :       if (pr_is_inert(x)) return scalarmat(sqri(gel(x,1)), N);
    1514       16580 :       x = idealsqrprime(nf, x, &cx);
    1515       16580 :       x = idealhnf_two(nf,x);
    1516       16580 :       return cx? ZM_Z_mul(x, cx): x;
    1517             :     default:
    1518       27070 :       x = Q_primitive_part(x, &cx);
    1519       27070 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    1520       27070 :       alpha = nfsqr(nf,alpha);
    1521       27070 :       m = zk_scalar_or_multable(nf, alpha);
    1522       27070 :       if (typ(m) == t_INT) {
    1523        1010 :         x = gcdii(sqri(a), m);
    1524        1010 :         if (cx) x = gmul(x, gsqr(cx));
    1525        1010 :         x = scalarmat(x, N);
    1526             :       }
    1527             :       else
    1528             :       {
    1529       26060 :         x = ZM_hnfmodid(m, gcdii(sqri(a), zkmultable_capZ(m)));
    1530       26060 :         if (cx) cx = gsqr(cx);
    1531       26060 :         if (cx) x = RgM_Rg_mul(x, cx);
    1532             :       }
    1533       27070 :       return x;
    1534             :   }
    1535             : }
    1536             : GEN
    1537       43820 : idealsqr(GEN nf, GEN x)
    1538             : {
    1539             :   pari_sp av;
    1540             :   GEN res, ax, z;
    1541       43820 :   long tx = idealtyp(&x,&ax);
    1542       43820 :   res = ax? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1543       43820 :   av = avma;
    1544       43820 :   z = gerepileupto(av, idealsqr_aux(checknf(nf), x, tx));
    1545       43820 :   if (!ax) return z;
    1546       43510 :   gel(res,1) = z;
    1547       43510 :   gel(res,2) = ext_sqr(nf, ax); return res;
    1548             : }
    1549             : 
    1550             : /* norm of an ideal */
    1551             : GEN
    1552        3965 : idealnorm(GEN nf, GEN x)
    1553             : {
    1554             :   pari_sp av;
    1555             :   GEN y, T;
    1556             :   long tx;
    1557             : 
    1558        3965 :   switch(idealtyp(&x,&y))
    1559             :   {
    1560         125 :     case id_PRIME: return pr_norm(x);
    1561        2585 :     case id_MAT: return RgM_det_triangular(x);
    1562             :   }
    1563             :   /* id_PRINCIPAL */
    1564        1255 :   nf = checknf(nf); T = nf_get_pol(nf); av = avma;
    1565        1255 :   x = nf_to_scalar_or_alg(nf, x);
    1566        1255 :   x = (typ(x) == t_POL)? RgXQ_norm(x, T): gpowgs(x, degpol(T));
    1567        1255 :   tx = typ(x);
    1568        1255 :   if (tx == t_INT) return gerepileuptoint(av, absi(x));
    1569         250 :   if (tx != t_FRAC) pari_err_TYPE("idealnorm",x);
    1570         250 :   return gerepileupto(av, Q_abs(x));
    1571             : }
    1572             : 
    1573             : /* I^(-1) = { x \in K, Tr(x D^(-1) I) \in Z }, D different of K/Q
    1574             :  *
    1575             :  * nf[5][6] = pp( D^(-1) ) = pp( HNF( T^(-1) ) ), T = (Tr(wi wj))
    1576             :  * nf[5][7] = same in 2-elt form.
    1577             :  * Assume I integral. Return the integral ideal (I\cap Z) I^(-1) */
    1578             : GEN
    1579      108455 : idealHNF_inv_Z(GEN nf, GEN I)
    1580             : {
    1581      108455 :   GEN J, dual, IZ = gcoeff(I,1,1); /* I \cap Z */
    1582      108455 :   if (isint1(IZ)) return matid(lg(I)-1);
    1583      106810 :   J = idealHNF_mul(nf,I, gmael(nf,5,7));
    1584             :  /* I in HNF, hence easily inverted; multiply by IZ to get integer coeffs
    1585             :   * missing content cancels while solving the linear equation */
    1586      106810 :   dual = shallowtrans( hnf_divscale(J, gmael(nf,5,6), IZ) );
    1587      106810 :   return ZM_hnfmodid(dual, IZ);
    1588             : }
    1589             : /* I HNF with rational coefficients (denominator d). */
    1590             : GEN
    1591       22150 : idealHNF_inv(GEN nf, GEN I)
    1592             : {
    1593       22150 :   GEN J, IQ = gcoeff(I,1,1); /* I \cap Q; d IQ = dI \cap Z */
    1594       22150 :   J = idealHNF_inv_Z(nf, Q_remove_denom(I, NULL)); /* = (dI)^(-1) * (d IQ) */
    1595       22150 :   return equali1(IQ)? J: RgM_Rg_div(J, IQ);
    1596             : }
    1597             : 
    1598             : /* return p * P^(-1)  [integral] */
    1599             : GEN
    1600       14770 : pr_inv_p(GEN pr)
    1601             : {
    1602       14770 :   if (pr_is_inert(pr)) return matid(pr_get_f(pr));
    1603       14565 :   return ZM_hnfmodid(pr_get_tau(pr), pr_get_p(pr));
    1604             : }
    1605             : GEN
    1606         970 : pr_inv(GEN pr)
    1607             : {
    1608         970 :   GEN p = pr_get_p(pr);
    1609         970 :   if (pr_is_inert(pr)) return scalarmat(ginv(p), pr_get_f(pr));
    1610         845 :   return RgM_Rg_div(ZM_hnfmodid(pr_get_tau(pr),p), p);
    1611             : }
    1612             : 
    1613             : GEN
    1614       32825 : idealinv(GEN nf, GEN x)
    1615             : {
    1616             :   GEN res, ax;
    1617             :   pari_sp av;
    1618       32825 :   long tx = idealtyp(&x,&ax), N;
    1619             : 
    1620       32825 :   res = ax? cgetg(3,t_VEC): NULL;
    1621       32825 :   nf = checknf(nf); av = avma;
    1622       32825 :   N = nf_get_degree(nf);
    1623       32825 :   switch (tx)
    1624             :   {
    1625             :     case id_MAT:
    1626       19420 :       if (lg(x)-1 != N) pari_err_DIM("idealinv");
    1627       19420 :       x = idealHNF_inv(nf,x); break;
    1628             :     case id_PRINCIPAL:
    1629       12665 :       x = nf_to_scalar_or_basis(nf, x);
    1630       12665 :       if (typ(x) != t_COL)
    1631       12635 :         x = idealhnf_principal(nf,ginv(x));
    1632             :       else
    1633             :       { /* nfinv + idealhnf where we already know (x) \cap Z */
    1634             :         GEN c, d;
    1635          30 :         x = Q_remove_denom(x, &c);
    1636          30 :         x = zk_inv(nf, x);
    1637          30 :         x = Q_remove_denom(x, &d); /* true inverse is c/d * x */
    1638          30 :         if (!d) /* x and x^(-1) integral => x a unit */
    1639           5 :           x = scalarmat_shallow(c? c: gen_1, N);
    1640             :         else
    1641             :         {
    1642          25 :           c = c? gdiv(c,d): ginv(d);
    1643          25 :           x = zk_multable(nf, x);
    1644          25 :           x = ZM_Q_mul(ZM_hnfmodid(x,d), c);
    1645             :         }
    1646             :       }
    1647       12665 :       break;
    1648             :     case id_PRIME:
    1649         740 :       x = pr_inv(x); break;
    1650             :   }
    1651       32825 :   x = gerepileupto(av,x); if (!ax) return x;
    1652        2985 :   gel(res,1) = x;
    1653        2985 :   gel(res,2) = ext_inv(nf, ax); return res;
    1654             : }
    1655             : 
    1656             : /* write x = A/B, A,B coprime integral ideals */
    1657             : GEN
    1658       10200 : idealnumden(GEN nf, GEN x)
    1659             : {
    1660       10200 :   pari_sp av = avma;
    1661             :   GEN x0, ax, c, d, A, B, J;
    1662       10200 :   long tx = idealtyp(&x,&ax);
    1663       10200 :   nf = checknf(nf);
    1664       10200 :   switch (tx)
    1665             :   {
    1666             :     case id_PRIME:
    1667           5 :       retmkvec2(idealhnf(nf, x), gen_1);
    1668             :     case id_PRINCIPAL:
    1669             :     {
    1670             :       GEN xZ, mx;
    1671        1145 :       x = nf_to_scalar_or_basis(nf, x);
    1672        1145 :       switch(typ(x))
    1673             :       {
    1674          40 :         case t_INT: return gerepilecopy(av, mkvec2(absi(x),gen_1));
    1675          10 :         case t_FRAC:return gerepilecopy(av, mkvec2(absi(gel(x,1)), gel(x,2)));
    1676             :       }
    1677             :       /* t_COL */
    1678        1095 :       x = Q_remove_denom(x, &d);
    1679        1095 :       if (!d) return gerepilecopy(av, mkvec2(idealhnf(nf, x), gen_1));
    1680          10 :       mx = zk_multable(nf, x);
    1681          10 :       xZ = zkmultable_capZ(mx);
    1682          10 :       x = ZM_hnfmodid(mx, xZ); /* principal ideal (x) */
    1683          10 :       x0 = mkvec2(xZ, mx); /* same, for fast multiplication */
    1684          10 :       break;
    1685             :     }
    1686             :     default: /* id_MAT */
    1687             :     {
    1688        9050 :       long n = lg(x)-1;
    1689        9050 :       if (n == 0) return mkvec2(gen_0, gen_1);
    1690        9050 :       if (n != nf_get_degree(nf)) pari_err_DIM("idealnumden");
    1691        9050 :       x0 = x = Q_remove_denom(x, &d);
    1692        9050 :       if (!d) return gerepilecopy(av, mkvec2(x, gen_1));
    1693          10 :       break;
    1694             :     }
    1695             :   }
    1696          20 :   J = hnfmodid(x, d); /* = d/B */
    1697          20 :   c = gcoeff(J,1,1); /* (d/B) \cap Z, divides d */
    1698          20 :   B = idealHNF_inv_Z(nf, J); /* (d/B \cap Z) B/d */
    1699          20 :   if (!equalii(c,d)) B = ZM_Z_mul(B, diviiexact(d,c)); /* = B ! */
    1700          20 :   A = idealHNF_mul(nf, B, x0); /* d * (original x) * B = d A */
    1701          20 :   A = ZM_Z_divexact(A, d); /* = A ! */
    1702          20 :   return gerepilecopy(av, mkvec2(A, B));
    1703             : }
    1704             : 
    1705             : /* Return x, integral in 2-elt form, such that pr^n = c * x. Assume n != 0.
    1706             :  * nf = true nf */
    1707             : static GEN
    1708       35300 : idealpowprime(GEN nf, GEN pr, GEN n, GEN *pc)
    1709             : {
    1710       35300 :   GEN p = pr_get_p(pr), q, gen;
    1711             : 
    1712       35300 :   *pc = NULL;
    1713       35300 :   if (is_pm1(n)) /* n = 1 special cased for efficiency */
    1714             :   {
    1715       17800 :     q = p;
    1716       17800 :     if (typ(pr_get_tau(pr)) == t_INT) /* inert */
    1717             :     {
    1718           0 :       *pc = (signe(n) >= 0)? p: ginv(p);
    1719           0 :       return mkvec2(gen_1,gen_0);
    1720             :     }
    1721       17800 :     if (signe(n) >= 0) gen = pr_get_gen(pr);
    1722             :     else
    1723             :     {
    1724        1430 :       gen = pr_get_tau(pr); /* possibly t_MAT */
    1725        1430 :       *pc = ginv(p);
    1726             :     }
    1727             :   }
    1728       17500 :   else if (equalis(n,2)) return idealsqrprime(nf, pr, pc);
    1729             :   else
    1730             :   {
    1731        8490 :     long e = pr_get_e(pr), f = pr_get_f(pr);
    1732        8490 :     GEN r, m = truedvmdis(n, e, &r);
    1733        8490 :     if (e * f == nf_get_degree(nf))
    1734             :     { /* pr^e = (p) */
    1735        3740 :       if (signe(m)) *pc = powii(p,m);
    1736        3740 :       if (!signe(r)) return mkvec2(gen_1,gen_0);
    1737        1330 :       q = p;
    1738        1330 :       gen = nfpow(nf, pr_get_gen(pr), r);
    1739             :     }
    1740             :     else
    1741             :     {
    1742        4750 :       m = absi(m);
    1743        4750 :       if (signe(r)) m = addiu(m,1);
    1744        4750 :       q = powii(p,m); /* m = ceil(|n|/e) */
    1745        4750 :       if (signe(n) >= 0) gen = nfpow(nf, pr_get_gen(pr), n);
    1746             :       else
    1747             :       {
    1748         655 :         gen = pr_get_tau(pr);
    1749         655 :         if (typ(gen) == t_MAT) gen = gel(gen,1);
    1750         655 :         n = negi(n);
    1751         655 :         gen = ZC_Z_divexact(nfpow(nf, gen, n), powii(p, subii(n,m)));
    1752         655 :         *pc = ginv(q);
    1753             :       }
    1754             :     }
    1755        6080 :     gen = FpC_red(gen, q);
    1756             :   }
    1757       23880 :   return mkvec2(q, gen);
    1758             : }
    1759             : 
    1760             : /* x * pr^n. Assume x in HNF or scalar (possibly non-integral) */
    1761             : GEN
    1762       24965 : idealmulpowprime(GEN nf, GEN x, GEN pr, GEN n)
    1763             : {
    1764             :   GEN c, cx, y;
    1765             :   long N;
    1766             : 
    1767       24965 :   nf = checknf(nf);
    1768       24965 :   N = nf_get_degree(nf);
    1769       24965 :   if (!signe(n)) return typ(x) == t_MAT? x: scalarmat_shallow(x, N);
    1770             : 
    1771             :   /* inert, special cased for efficiency */
    1772       24885 :   if (pr_is_inert(pr))
    1773             :   {
    1774        2075 :     GEN q = powii(pr_get_p(pr), n);
    1775        2075 :     return typ(x) == t_MAT? RgM_Rg_mul(x,q): scalarmat_shallow(gmul(x,q), N);
    1776             :   }
    1777             : 
    1778       22810 :   y = idealpowprime(nf, pr, n, &c);
    1779       22810 :   if (typ(x) == t_MAT)
    1780       22470 :   { x = Q_primitive_part(x, &cx); if (is_pm1(gcoeff(x,1,1))) x = NULL; }
    1781             :   else
    1782         340 :   { cx = x; x = NULL; }
    1783       22810 :   cx = mul_content(c,cx);
    1784       22810 :   if (x)
    1785       14345 :     x = idealHNF_mul_two(nf,x,y);
    1786             :   else
    1787        8465 :     x = idealhnf_two(nf,y);
    1788       22810 :   if (cx) x = RgM_Rg_mul(x,cx);
    1789       22810 :   return x;
    1790             : }
    1791             : GEN
    1792        3445 : idealdivpowprime(GEN nf, GEN x, GEN pr, GEN n)
    1793             : {
    1794        3445 :   return idealmulpowprime(nf,x,pr, negi(n));
    1795             : }
    1796             : 
    1797             : /* nf = true nf */
    1798             : static GEN
    1799      112065 : idealpow_aux(GEN nf, GEN x, long tx, GEN n)
    1800             : {
    1801      112065 :   GEN T = nf_get_pol(nf), m, cx, n1, a, alpha;
    1802      112065 :   long N = degpol(T), s = signe(n);
    1803      112065 :   if (!s) return matid(N);
    1804      110015 :   switch(tx)
    1805             :   {
    1806             :     case id_PRINCIPAL:
    1807           0 :       return idealhnf_principal(nf, nfpow(nf,x,n));
    1808             :     case id_PRIME:
    1809       43535 :       if (pr_is_inert(x)) return scalarmat(powii(gel(x,1), n), N);
    1810       12490 :       x = idealpowprime(nf, x, n, &cx);
    1811       12490 :       x = idealhnf_two(nf,x);
    1812       12490 :       return cx? RgM_Rg_mul(x, cx): x;
    1813             :     default:
    1814       66480 :       if (is_pm1(n)) return (s < 0)? idealinv(nf, x): gcopy(x);
    1815       37725 :       n1 = (s < 0)? negi(n): n;
    1816             : 
    1817       37725 :       x = Q_primitive_part(x, &cx);
    1818       37725 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    1819       37725 :       alpha = nfpow(nf,alpha,n1);
    1820       37725 :       m = zk_scalar_or_multable(nf, alpha);
    1821       37725 :       if (typ(m) == t_INT) {
    1822          65 :         x = gcdii(powii(a,n1), m);
    1823          65 :         if (s<0) x = ginv(x);
    1824          65 :         if (cx) x = gmul(x, powgi(cx,n));
    1825          65 :         x = scalarmat(x, N);
    1826             :       }
    1827             :       else
    1828             :       {
    1829       37660 :         x = ZM_hnfmodid(m, gcdii(powii(a,n1), zkmultable_capZ(m)));
    1830       37660 :         if (cx) cx = powgi(cx,n);
    1831       37660 :         if (s<0) {
    1832           5 :           GEN xZ = gcoeff(x,1,1);
    1833           5 :           cx = cx ? gdiv(cx, xZ): ginv(xZ);
    1834           5 :           x = idealHNF_inv_Z(nf,x);
    1835             :         }
    1836       37660 :         if (cx) x = RgM_Rg_mul(x, cx);
    1837             :       }
    1838       37725 :       return x;
    1839             :   }
    1840             : }
    1841             : 
    1842             : /* raise the ideal x to the power n (in Z) */
    1843             : GEN
    1844      112065 : idealpow(GEN nf, GEN x, GEN n)
    1845             : {
    1846             :   pari_sp av;
    1847             :   long tx;
    1848             :   GEN res, ax;
    1849             : 
    1850      112065 :   if (typ(n) != t_INT) pari_err_TYPE("idealpow",n);
    1851      112065 :   tx = idealtyp(&x,&ax);
    1852      112065 :   res = ax? cgetg(3,t_VEC): NULL;
    1853      112065 :   av = avma;
    1854      112065 :   x = gerepileupto(av, idealpow_aux(checknf(nf), x, tx, n));
    1855      112065 :   if (!ax) return x;
    1856         290 :   ax = ext_pow(nf, ax, n);
    1857         290 :   gel(res,1) = x;
    1858         290 :   gel(res,2) = ax;
    1859         290 :   return res;
    1860             : }
    1861             : 
    1862             : /* Return ideal^e in number field nf. e is a C integer. */
    1863             : GEN
    1864       10455 : idealpows(GEN nf, GEN ideal, long e)
    1865             : {
    1866       10455 :   long court[] = {evaltyp(t_INT) | _evallg(3),0,0};
    1867       10455 :   affsi(e,court); return idealpow(nf,ideal,court);
    1868             : }
    1869             : 
    1870             : static GEN
    1871       30330 : _idealmulred(GEN nf, GEN x, GEN y)
    1872       30330 : { return idealred(nf,idealmul(nf,x,y)); }
    1873             : static GEN
    1874       43550 : _idealsqrred(GEN nf, GEN x)
    1875       43550 : { return idealred(nf,idealsqr(nf,x)); }
    1876             : static GEN
    1877       18650 : _mul(void *data, GEN x, GEN y) { return _idealmulred((GEN)data,x,y); }
    1878             : static GEN
    1879       43550 : _sqr(void *data, GEN x) { return _idealsqrred((GEN)data, x); }
    1880             : 
    1881             : /* compute x^n (x ideal, n integer), reducing along the way */
    1882             : GEN
    1883       34425 : idealpowred(GEN nf, GEN x, GEN n)
    1884             : {
    1885       34425 :   pari_sp av = avma;
    1886             :   long s;
    1887             :   GEN y;
    1888             : 
    1889       34425 :   if (typ(n) != t_INT) pari_err_TYPE("idealpowred",n);
    1890       34425 :   s = signe(n); if (s == 0) return idealpow(nf,x,n);
    1891       34135 :   y = gen_pow(x, n, (void*)nf, &_sqr, &_mul);
    1892             : 
    1893       34135 :   if (s < 0) y = idealinv(nf,y);
    1894       34135 :   if (s < 0 || is_pm1(n)) y = idealred(nf,y);
    1895       34135 :   return gerepileupto(av,y);
    1896             : }
    1897             : 
    1898             : GEN
    1899       11680 : idealmulred(GEN nf, GEN x, GEN y)
    1900             : {
    1901       11680 :   pari_sp av = avma;
    1902       11680 :   return gerepileupto(av, _idealmulred(nf,x,y));
    1903             : }
    1904             : 
    1905             : long
    1906          65 : isideal(GEN nf,GEN x)
    1907             : {
    1908          65 :   long N, i, j, lx, tx = typ(x);
    1909             :   pari_sp av;
    1910             :   GEN T, xZ;
    1911             : 
    1912          65 :   nf = checknf(nf); T = nf_get_pol(nf); lx = lg(x);
    1913          65 :   if (tx==t_VEC && lx==3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
    1914          65 :   switch(tx)
    1915             :   {
    1916          10 :     case t_INT: case t_FRAC: return 1;
    1917           5 :     case t_POL: return varn(x) == varn(T);
    1918           5 :     case t_POLMOD: return RgX_equal_var(T, gel(x,1));
    1919          10 :     case t_VEC: return get_prid(x)? 1 : 0;
    1920          30 :     case t_MAT: break;
    1921           5 :     default: return 0;
    1922             :   }
    1923          30 :   N = degpol(T);
    1924          30 :   if (lx-1 != N) return (lx == 1);
    1925          20 :   if (nbrows(x) != N) return 0;
    1926             : 
    1927          20 :   av = avma; x = Q_primpart(x);
    1928          20 :   if (!ZM_ishnf(x)) return 0;
    1929          10 :   xZ = gcoeff(x,1,1);
    1930          15 :   for (j=2; j<=N; j++)
    1931          10 :     if (!dvdii(xZ, gcoeff(x,j,j))) { avma = av; return 0; }
    1932          10 :   for (i=2; i<=N; i++)
    1933          10 :     for (j=2; j<=N; j++)
    1934           5 :       if (! hnf_invimage(x, zk_ei_mul(nf,gel(x,i),j))) { avma = av; return 0; }
    1935           5 :   avma=av; return 1;
    1936             : }
    1937             : 
    1938             : GEN
    1939        9830 : idealdiv(GEN nf, GEN x, GEN y)
    1940             : {
    1941        9830 :   pari_sp av = avma, tetpil;
    1942        9830 :   GEN z = idealinv(nf,y);
    1943        9830 :   tetpil = avma; return gerepile(av,tetpil, idealmul(nf,x,z));
    1944             : }
    1945             : 
    1946             : /* This routine computes the quotient x/y of two ideals in the number field nf.
    1947             :  * It assumes that the quotient is an integral ideal.  The idea is to find an
    1948             :  * ideal z dividing y such that gcd(Nx/Nz, Nz) = 1.  Then
    1949             :  *
    1950             :  *   x + (Nx/Nz)    x
    1951             :  *   ----------- = ---
    1952             :  *   y + (Ny/Nz)    y
    1953             :  *
    1954             :  * Proof: we can assume x and y are integral. Let p be any prime ideal
    1955             :  *
    1956             :  * If p | Nz, then it divides neither Nx/Nz nor Ny/Nz (since Nx/Nz is the
    1957             :  * product of the integers N(x/y) and N(y/z)).  Both the numerator and the
    1958             :  * denominator on the left will be coprime to p.  So will x/y, since x/y is
    1959             :  * assumed integral and its norm N(x/y) is coprime to p.
    1960             :  *
    1961             :  * If instead p does not divide Nz, then v_p (Nx/Nz) = v_p (Nx) >= v_p(x).
    1962             :  * Hence v_p (x + Nx/Nz) = v_p(x).  Likewise for the denominators.  QED.
    1963             :  *
    1964             :  *                Peter Montgomery.  July, 1994. */
    1965             : static void
    1966           5 : err_divexact(GEN x, GEN y)
    1967           5 : { pari_err_DOMAIN("idealdivexact","denominator(x/y)", "!=",
    1968           0 :                   gen_1,mkvec2(x,y)); }
    1969             : GEN
    1970         415 : idealdivexact(GEN nf, GEN x0, GEN y0)
    1971             : {
    1972         415 :   pari_sp av = avma;
    1973             :   GEN x, y, yZ, Nx, Ny, Nz, cy, q, r;
    1974             : 
    1975         415 :   nf = checknf(nf);
    1976         415 :   x = idealhnf_shallow(nf, x0);
    1977         415 :   y = idealhnf_shallow(nf, y0);
    1978         415 :   if (lg(y) == 1) pari_err_INV("idealdivexact", y0);
    1979         410 :   if (lg(x) == 1) { avma = av; return cgetg(1, t_MAT); } /* numerator is zero */
    1980         410 :   y = Q_primitive_part(y, &cy);
    1981         410 :   if (cy) x = RgM_Rg_div(x,cy);
    1982         410 :   Nx = idealnorm(nf,x);
    1983         410 :   Ny = idealnorm(nf,y);
    1984         410 :   if (typ(Nx) != t_INT) err_divexact(x,y);
    1985         405 :   q = dvmdii(Nx,Ny, &r);
    1986         405 :   if (signe(r)) err_divexact(x,y);
    1987         405 :   if (is_pm1(q)) { avma = av; return matid(nf_get_degree(nf)); }
    1988             :   /* Find a norm Nz | Ny such that gcd(Nx/Nz, Nz) = 1 */
    1989         245 :   for (Nz = Ny;;) /* q = Nx/Nz */
    1990             :   {
    1991         260 :     GEN p1 = gcdii(Nz, q);
    1992         260 :     if (is_pm1(p1)) break;
    1993          15 :     Nz = diviiexact(Nz,p1);
    1994          15 :     q = mulii(q,p1);
    1995          15 :   }
    1996             :   /* Replace x/y  by  x+(Nx/Nz) / y+(Ny/Nz) */
    1997         245 :   x = ZM_hnfmodid(x, q);
    1998             :   /* y reduced to unit ideal ? */
    1999         245 :   if (Nz == Ny) return gerepileupto(av, x);
    2000             : 
    2001          15 :   y = ZM_hnfmodid(y, diviiexact(Ny,Nz));
    2002          15 :   yZ = gcoeff(y,1,1);
    2003          15 :   y = idealHNF_mul(nf,x, idealHNF_inv_Z(nf,y));
    2004          15 :   return gerepileupto(av, RgM_Rg_div(y, yZ));
    2005             : }
    2006             : 
    2007             : GEN
    2008          15 : idealintersect(GEN nf, GEN x, GEN y)
    2009             : {
    2010          15 :   pari_sp av = avma;
    2011             :   long lz, lx, i;
    2012             :   GEN z, dx, dy, xZ, yZ;;
    2013             : 
    2014          15 :   nf = checknf(nf);
    2015          15 :   x = idealhnf_shallow(nf,x);
    2016          15 :   y = idealhnf_shallow(nf,y);
    2017          15 :   if (lg(x) == 1 || lg(y) == 1) { avma = av; return cgetg(1,t_MAT); }
    2018          10 :   x = Q_remove_denom(x, &dx);
    2019          10 :   y = Q_remove_denom(y, &dy);
    2020          10 :   if (dx) y = ZM_Z_mul(y, dx);
    2021          10 :   if (dy) x = ZM_Z_mul(x, dy);
    2022          10 :   xZ = gcoeff(x,1,1);
    2023          10 :   yZ = gcoeff(y,1,1);
    2024          10 :   dx = mul_denom(dx,dy);
    2025          10 :   z = ZM_lll(shallowconcat(x,y), 0.99, LLL_KER); lz = lg(z);
    2026          10 :   lx = lg(x);
    2027          10 :   for (i=1; i<lz; i++) setlg(z[i], lx);
    2028          10 :   z = ZM_hnfmodid(ZM_mul(x,z), lcmii(xZ, yZ));
    2029          10 :   if (dx) z = RgM_Rg_div(z,dx);
    2030          10 :   return gerepileupto(av,z);
    2031             : }
    2032             : 
    2033             : /*******************************************************************/
    2034             : /*                                                                 */
    2035             : /*                      T2-IDEAL REDUCTION                         */
    2036             : /*                                                                 */
    2037             : /*******************************************************************/
    2038             : 
    2039             : static GEN
    2040          15 : chk_vdir(GEN nf, GEN vdir)
    2041             : {
    2042          15 :   long i, l = lg(vdir);
    2043             :   GEN v;
    2044          15 :   if (l != lg(nf_get_roots(nf))) pari_err_DIM("idealred");
    2045          10 :   switch(typ(vdir))
    2046             :   {
    2047           0 :     case t_VECSMALL: return vdir;
    2048          10 :     case t_VEC: break;
    2049           0 :     default: pari_err_TYPE("idealred",vdir);
    2050             :   }
    2051          10 :   v = cgetg(l, t_VECSMALL);
    2052          10 :   for (i = 1; i < l; i++) v[i] = itos(gceil(gel(vdir,i)));
    2053          10 :   return v;
    2054             : }
    2055             : 
    2056             : static void
    2057       19205 : twistG(GEN G, long r1, long i, long v)
    2058             : {
    2059       19205 :   long j, lG = lg(G);
    2060       19205 :   if (i <= r1) {
    2061       16815 :     for (j=1; j<lG; j++) gcoeff(G,i,j) = gmul2n(gcoeff(G,i,j), v);
    2062             :   } else {
    2063        2390 :     long k = (i<<1) - r1;
    2064       12750 :     for (j=1; j<lG; j++)
    2065             :     {
    2066       10360 :       gcoeff(G,k-1,j) = gmul2n(gcoeff(G,k-1,j), v);
    2067       10360 :       gcoeff(G,k  ,j) = gmul2n(gcoeff(G,k  ,j), v);
    2068             :     }
    2069             :   }
    2070       19205 : }
    2071             : 
    2072             : GEN
    2073      106260 : nf_get_Gtwist(GEN nf, GEN vdir)
    2074             : {
    2075             :   long i, l, v, r1;
    2076             :   GEN G;
    2077             : 
    2078      106260 :   if (!vdir) return nf_get_roundG(nf);
    2079        2110 :   if (typ(vdir) == t_MAT)
    2080             :   {
    2081        2095 :     long N = nf_get_degree(nf);
    2082        2095 :     if (lg(vdir) != N+1 || lgcols(vdir) != N+1) pari_err_DIM("idealred");
    2083        2095 :     return vdir;
    2084             :   }
    2085          15 :   vdir = chk_vdir(nf, vdir);
    2086          10 :   G = RgM_shallowcopy(nf_get_G(nf));
    2087          10 :   r1 = nf_get_r1(nf);
    2088          10 :   l = lg(vdir);
    2089          40 :   for (i=1; i<l; i++)
    2090             :   {
    2091          30 :     v = vdir[i]; if (!v) continue;
    2092          30 :     twistG(G, r1, i, v);
    2093             :   }
    2094          10 :   return RM_round_maxrank(G);
    2095             : }
    2096             : GEN
    2097       19175 : nf_get_Gtwist1(GEN nf, long i)
    2098             : {
    2099       19175 :   GEN G = RgM_shallowcopy( nf_get_G(nf) );
    2100       19175 :   long r1 = nf_get_r1(nf);
    2101       19175 :   twistG(G, r1, i, 10);
    2102       19175 :   return RM_round_maxrank(G);
    2103             : }
    2104             : 
    2105             : GEN
    2106       23195 : RM_round_maxrank(GEN G0)
    2107             : {
    2108       23195 :   long e, r = lg(G0)-1;
    2109       23195 :   pari_sp av = avma;
    2110       23195 :   GEN G = G0;
    2111       23195 :   for (e = 4; ; e <<= 1)
    2112             :   {
    2113       23195 :     GEN H = ground(G);
    2114       46390 :     if (ZM_rank(H) == r) return H; /* maximal rank ? */
    2115           0 :     avma = av;
    2116           0 :     G = gmul2n(G0, e);
    2117           0 :   }
    2118             : }
    2119             : 
    2120             : GEN
    2121      106255 : idealred0(GEN nf, GEN I, GEN vdir)
    2122             : {
    2123      106255 :   pari_sp av = avma;
    2124      106255 :   GEN G, aI, IZ, J, y, yZ, my, c1 = NULL;
    2125             :   long N;
    2126             : 
    2127      106255 :   nf = checknf(nf);
    2128      106255 :   N = nf_get_degree(nf);
    2129             :   /* put first for sanity checks, unused when I obviously principal */
    2130      106255 :   G = nf_get_Gtwist(nf, vdir);
    2131      106250 :   switch (idealtyp(&I,&aI))
    2132             :   {
    2133             :     case id_PRIME:
    2134       14640 :       if (pr_is_inert(I)) {
    2135         415 :         if (!aI) { avma = av; return matid(N); }
    2136         415 :         c1 = gel(I,1); I = matid(N);
    2137         415 :         goto END;
    2138             :       }
    2139       14225 :       IZ = pr_get_p(I);
    2140       14225 :       J = pr_inv_p(I);
    2141       14225 :       I = idealhnf_two(nf,I);
    2142       14225 :       break;
    2143             :     case id_MAT:
    2144       91600 :       I = Q_primitive_part(I, &c1);
    2145       91600 :       IZ = gcoeff(I,1,1);
    2146       91600 :       if (is_pm1(IZ))
    2147             :       {
    2148        5335 :         if (!aI) { avma = av; return matid(N); }
    2149        5295 :         goto END;
    2150             :       }
    2151       86265 :       J = idealHNF_inv_Z(nf, I);
    2152       86265 :       break;
    2153             :     default: /* id_PRINCIPAL, silly case */
    2154          10 :       if (gequal0(I)) I = cgetg(1,t_MAT); else { c1 = I; I = matid(N); }
    2155          10 :       if (!aI) return I;
    2156           5 :       goto END;
    2157             :   }
    2158             :   /* now I integral, HNF; and J = (I\cap Z) I^(-1), integral */
    2159      100490 :   y = idealpseudomin(J, G); /* small elt in (I\cap Z)I^(-1), integral */
    2160      100490 :   if (ZV_isscalar(y))
    2161             :   { /* already reduced */
    2162       38685 :     if (!aI) return gerepilecopy(av, I);
    2163       38400 :     goto END;
    2164             :   }
    2165             : 
    2166       61805 :   my = zk_multable(nf, y);
    2167       61805 :   I = ZM_Z_divexact(ZM_mul(my, I), IZ); /* y I / (I\cap Z), integral */
    2168       61805 :   c1 = mul_content(c1, IZ);
    2169       61805 :   my = ZM_gauss(my, col_ei(N,1)); /* y^-1 */
    2170       61805 :   yZ = Q_denom(my); /* (y) \cap Z */
    2171       61805 :   I = hnfmodid(I, yZ);
    2172       61805 :   if (!aI) return gerepileupto(av, I);
    2173       61615 :   c1 = RgC_Rg_mul(my, c1);
    2174             : END:
    2175      105730 :   if (c1) aI = ext_mul(nf, aI,c1);
    2176      105730 :   return gerepilecopy(av, mkvec2(I, aI));
    2177             : }
    2178             : 
    2179             : GEN
    2180           5 : idealmin(GEN nf, GEN x, GEN vdir)
    2181             : {
    2182           5 :   pari_sp av = avma;
    2183             :   GEN y, dx;
    2184           5 :   nf = checknf(nf);
    2185           5 :   switch( idealtyp(&x,&y) )
    2186             :   {
    2187           0 :     case id_PRINCIPAL: return gcopy(x);
    2188           0 :     case id_PRIME: x = pr_hnf(nf,x); break;
    2189           5 :     case id_MAT: if (lg(x) == 1) return gen_0;
    2190             :   }
    2191           5 :   x = Q_remove_denom(x, &dx);
    2192           5 :   y = idealpseudomin(x, nf_get_Gtwist(nf,vdir));
    2193           5 :   if (dx) y = RgC_Rg_div(y, dx);
    2194           5 :   return gerepileupto(av, y);
    2195             : }
    2196             : 
    2197             : /*******************************************************************/
    2198             : /*                                                                 */
    2199             : /*                   APPROXIMATION THEOREM                         */
    2200             : /*                                                                 */
    2201             : /*******************************************************************/
    2202             : /* a = ppi(a,b) ppo(a,b), where ppi regroups primes common to a and b
    2203             :  * and ppo(a,b) = Z_ppo(a,b) */
    2204             : /* return gcd(a,b),ppi(a,b),ppo(a,b) */
    2205             : GEN
    2206      323040 : Z_ppio(GEN a, GEN b)
    2207             : {
    2208      323040 :   GEN x, y, d = gcdii(a,b);
    2209      323040 :   if (is_pm1(d)) return mkvec3(gen_1, gen_1, a);
    2210      245770 :   x = d; y = diviiexact(a,d);
    2211             :   for(;;)
    2212             :   {
    2213      290440 :     GEN g = gcdii(x,y);
    2214      290440 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2215       44670 :     x = mulii(x,g); y = diviiexact(y,g);
    2216       44670 :   }
    2217             : }
    2218             : /* a = ppg(a,b)pple(a,b), where ppg regroups primes such that v(a) > v(b)
    2219             :  * and pple all others */
    2220             : /* return gcd(a,b),ppg(a,b),pple(a,b) */
    2221             : GEN
    2222           0 : Z_ppgle(GEN a, GEN b)
    2223             : {
    2224           0 :   GEN x, y, g, d = gcdii(a,b);
    2225           0 :   if (equalii(a, d)) return mkvec3(a, gen_1, a);
    2226           0 :   x = diviiexact(a,d); y = d;
    2227             :   for(;;)
    2228             :   {
    2229           0 :     g = gcdii(x,y);
    2230           0 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2231           0 :     x = mulii(x,g); y = diviiexact(y,g);
    2232           0 :   }
    2233             : }
    2234             : static void
    2235           0 : Z_dcba_rec(GEN L, GEN a, GEN b)
    2236             : {
    2237             :   GEN x, r, v, g, h, c, c0;
    2238             :   long n;
    2239           0 :   if (is_pm1(b)) {
    2240           0 :     if (!is_pm1(a)) vectrunc_append(L, a);
    2241           0 :     return;
    2242             :   }
    2243           0 :   v = Z_ppio(a,b);
    2244           0 :   a = gel(v,2);
    2245           0 :   r = gel(v,3);
    2246           0 :   if (!is_pm1(r)) vectrunc_append(L, r);
    2247           0 :   v = Z_ppgle(a,b);
    2248           0 :   g = gel(v,1);
    2249           0 :   h = gel(v,2);
    2250           0 :   x = c0 = gel(v,3);
    2251           0 :   for (n = 1; !is_pm1(h); n++)
    2252             :   {
    2253             :     GEN d, y;
    2254             :     long i;
    2255           0 :     v = Z_ppgle(h,sqri(g));
    2256           0 :     g = gel(v,1);
    2257           0 :     h = gel(v,2);
    2258           0 :     c = gel(v,3); if (is_pm1(c)) continue;
    2259           0 :     d = gcdii(c,b);
    2260           0 :     x = mulii(x,d);
    2261           0 :     y = d; for (i=1; i < n; i++) y = sqri(y);
    2262           0 :     Z_dcba_rec(L, diviiexact(c,y), d);
    2263             :   }
    2264           0 :   Z_dcba_rec(L,diviiexact(b,x), c0);
    2265             : }
    2266             : static GEN
    2267     2188785 : Z_cba_rec(GEN L, GEN a, GEN b)
    2268             : {
    2269             :   GEN g;
    2270     2188785 :   if (lg(L) > 10)
    2271             :   { /* a few naive steps before switching to dcba */
    2272           0 :     Z_dcba_rec(L, a, b);
    2273           0 :     return gel(L, lg(L)-1);
    2274             :   }
    2275     2188785 :   if (is_pm1(a)) return b;
    2276     1300525 :   g = gcdii(a,b);
    2277     1300525 :   if (is_pm1(g)) { vectrunc_append(L, a); return b; }
    2278      971490 :   a = diviiexact(a,g);
    2279      971490 :   b = diviiexact(b,g);
    2280      971490 :   return Z_cba_rec(L, Z_cba_rec(L, a, g), b);
    2281             : }
    2282             : GEN
    2283      245805 : Z_cba(GEN a, GEN b)
    2284             : {
    2285      245805 :   GEN L = vectrunc_init(expi(a) + expi(b) + 2);
    2286      245805 :   GEN t = Z_cba_rec(L, a, b);
    2287      245805 :   if (!is_pm1(t)) vectrunc_append(L, t);
    2288      245805 :   return L;
    2289             : }
    2290             : 
    2291             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2292             : GEN
    2293      789075 : Z_ppo(GEN x, GEN f)
    2294             : {
    2295             :   for (;;)
    2296             :   {
    2297      789075 :     f = gcdii(x, f); if (is_pm1(f)) break;
    2298      537475 :     x = diviiexact(x, f);
    2299      537475 :   }
    2300      251600 :   return x;
    2301             : }
    2302             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2303             : ulong
    2304    27052970 : u_ppo(ulong x, ulong f)
    2305             : {
    2306             :   for (;;)
    2307             :   {
    2308    27052970 :     f = ugcd(x, f); if (f == 1) break;
    2309     5200680 :     x /= f;
    2310     5200680 :   }
    2311    21852290 :   return x;
    2312             : }
    2313             : 
    2314             : /* x t_INT, f ideal. Write x = x1 x2, sqf(x1) | f, (x2,f) = 1. Return x2 */
    2315             : static GEN
    2316           5 : nf_coprime_part(GEN nf, GEN x, GEN listpr)
    2317             : {
    2318           5 :   long v, j, lp = lg(listpr), N = nf_get_degree(nf);
    2319             :   GEN x1, x2, ex;
    2320             : 
    2321             : #if 0 /*1) via many gcds. Expensive ! */
    2322             :   GEN f = idealprodprime(nf, listpr);
    2323             :   f = ZM_hnfmodid(f, x); /* first gcd is less expensive since x in Z */
    2324             :   x = scalarmat(x, N);
    2325             :   for (;;)
    2326             :   {
    2327             :     if (gequal1(gcoeff(f,1,1))) break;
    2328             :     x = idealdivexact(nf, x, f);
    2329             :     f = ZM_hnfmodid(shallowconcat(f,x), gcoeff(x,1,1)); /* gcd(f,x) */
    2330             :   }
    2331             :   x2 = x;
    2332             : #else /*2) from prime decomposition */
    2333           5 :   x1 = NULL;
    2334          15 :   for (j=1; j<lp; j++)
    2335             :   {
    2336          10 :     GEN pr = gel(listpr,j);
    2337          10 :     v = Z_pval(x, pr_get_p(pr)); if (!v) continue;
    2338             : 
    2339           5 :     ex = muluu(v, pr_get_e(pr)); /* = v_pr(x) > 0 */
    2340           5 :     x1 = x1? idealmulpowprime(nf, x1, pr, ex)
    2341           5 :            : idealpow(nf, pr, ex);
    2342             :   }
    2343           5 :   x = scalarmat(x, N);
    2344           5 :   x2 = x1? idealdivexact(nf, x, x1): x;
    2345             : #endif
    2346           5 :   return x2;
    2347             : }
    2348             : 
    2349             : /* L0 in K^*, assume (L0,f) = 1. Return L integral, L0 = L mod f  */
    2350             : GEN
    2351        1385 : make_integral(GEN nf, GEN L0, GEN f, GEN listpr)
    2352             : {
    2353             :   GEN fZ, t, L, D2, d1, d2, d;
    2354             : 
    2355        1385 :   L = Q_remove_denom(L0, &d);
    2356        1385 :   if (!d) return L0;
    2357             : 
    2358             :   /* L0 = L / d, L integral */
    2359         945 :   fZ = gcoeff(f,1,1);
    2360         945 :   if (typ(L) == t_INT) return Fp_mul(L, Fp_inv(d, fZ), fZ);
    2361             :   /* Kill denom part coprime to fZ */
    2362         935 :   d2 = Z_ppo(d, fZ);
    2363         935 :   t = Fp_inv(d2, fZ); if (!is_pm1(t)) L = ZC_Z_mul(L,t);
    2364         935 :   if (equalii(d, d2)) return L;
    2365             : 
    2366           5 :   d1 = diviiexact(d, d2);
    2367             :   /* L0 = (L / d1) mod f. d1 not coprime to f
    2368             :    * write (d1) = D1 D2, D2 minimal, (D2,f) = 1. */
    2369           5 :   D2 = nf_coprime_part(nf, d1, listpr);
    2370           5 :   t = idealaddtoone_i(nf, D2, f); /* in D2, 1 mod f */
    2371           5 :   L = nfmuli(nf,t,L);
    2372             : 
    2373             :   /* if (L0, f) = 1, then L in D1 ==> in D1 D2 = (d1) */
    2374           5 :   return Q_div_to_int(L, d1); /* exact division */
    2375             : }
    2376             : 
    2377             : /* assume L is a list of prime ideals. Return the product */
    2378             : GEN
    2379          90 : idealprodprime(GEN nf, GEN L)
    2380             : {
    2381          90 :   long l = lg(L), i;
    2382             :   GEN z;
    2383          90 :   if (l == 1) return matid(nf_get_degree(nf));
    2384          90 :   z = pr_hnf(nf, gel(L,1));
    2385          90 :   for (i=2; i<l; i++) z = idealHNF_mul_two(nf,z, gel(L,i));
    2386          90 :   return z;
    2387             : }
    2388             : 
    2389             : /* optimize for the frequent case I = nfhnf()[2]: lots of them are 1 */
    2390             : GEN
    2391         815 : idealprod(GEN nf, GEN I)
    2392             : {
    2393         815 :   long i, l = lg(I);
    2394             :   GEN z;
    2395        1275 :   for (i = 1; i < l; i++)
    2396        1260 :     if (!equali1(gel(I,i))) break;
    2397         815 :   if (i == l) return gen_1;
    2398         800 :   z = gel(I,i);
    2399         800 :   for (i++; i<l; i++) z = idealmul(nf, z, gel(I,i));
    2400         800 :   return z;
    2401             : }
    2402             : 
    2403             : /* assume L is a list of prime ideals. Return prod L[i]^e[i] */
    2404             : GEN
    2405        3620 : factorbackprime(GEN nf, GEN L, GEN e)
    2406             : {
    2407        3620 :   long l = lg(L), i;
    2408             :   GEN z;
    2409             : 
    2410        3620 :   if (l == 1) return matid(nf_get_degree(nf));
    2411        3610 :   z = idealpow(nf, gel(L,1), gel(e,1));
    2412        5140 :   for (i=2; i<l; i++)
    2413        1530 :     if (signe(gel(e,i))) z = idealmulpowprime(nf,z, gel(L,i),gel(e,i));
    2414        3610 :   return z;
    2415             : }
    2416             : 
    2417             : /* F in Z, divisible exactly by pr.p. Return F-uniformizer for pr, i.e.
    2418             :  * a t in Z_K such that v_pr(t) = 1 and (t, F/pr) = 1 */
    2419             : GEN
    2420        7285 : pr_uniformizer(GEN pr, GEN F)
    2421             : {
    2422        7285 :   GEN p = pr_get_p(pr), t = pr_get_gen(pr);
    2423        7285 :   if (!equalii(F, p))
    2424             :   {
    2425        2840 :     long e = pr_get_e(pr);
    2426        2840 :     GEN u, v, q = (e == 1)? sqri(p): p;
    2427        2840 :     u = mulii(q, Fp_inv(q, diviiexact(F,p))); /* 1 mod F/p, 0 mod q */
    2428        2840 :     v = subui(1UL, u); /* 0 mod F/p, 1 mod q */
    2429        2840 :     if (pr_is_inert(pr))
    2430           0 :       t = addii(mulii(p, v), u);
    2431             :     else
    2432             :     {
    2433        2840 :       t = ZC_Z_mul(t, v);
    2434        2840 :       gel(t,1) = addii(gel(t,1), u); /* return u + vt */
    2435             :     }
    2436             :   }
    2437        7285 :   return t;
    2438             : }
    2439             : /* L = list of prime ideals, return lcm_i (L[i] \cap \ZM) */
    2440             : GEN
    2441        7460 : prV_lcm_capZ(GEN L)
    2442             : {
    2443        7460 :   long i, r = lg(L);
    2444             :   GEN F;
    2445        7460 :   if (r == 1) return gen_1;
    2446        6680 :   F = pr_get_p(gel(L,1));
    2447        9595 :   for (i = 2; i < r; i++)
    2448             :   {
    2449        2915 :     GEN pr = gel(L,i), p = pr_get_p(pr);
    2450        2915 :     if (!dvdii(F, p)) F = mulii(F,p);
    2451             :   }
    2452        6680 :   return F;
    2453             : }
    2454             : 
    2455             : /* Given a prime ideal factorization with possibly zero or negative
    2456             :  * exponents, gives b such that v_p(b) = v_p(x) for all prime ideals pr | x
    2457             :  * and v_pr(b) >= 0 for all other pr.
    2458             :  * For optimal performance, all [anti-]uniformizers should be precomputed,
    2459             :  * but no support for this yet.
    2460             :  *
    2461             :  * If nored, do not reduce result.
    2462             :  * No garbage collecting */
    2463             : static GEN
    2464        6815 : idealapprfact_i(GEN nf, GEN x, int nored)
    2465             : {
    2466             :   GEN z, d, L, e, e2, F;
    2467             :   long i, r;
    2468             :   int flagden;
    2469             : 
    2470        6815 :   nf = checknf(nf);
    2471        6815 :   L = gel(x,1);
    2472        6815 :   e = gel(x,2);
    2473        6815 :   F = prV_lcm_capZ(L);
    2474        6815 :   flagden = 0;
    2475        6815 :   z = NULL; r = lg(e);
    2476       15625 :   for (i = 1; i < r; i++)
    2477             :   {
    2478        8810 :     long s = signe(gel(e,i));
    2479             :     GEN pi, q;
    2480        8810 :     if (!s) continue;
    2481        7230 :     if (s < 0) flagden = 1;
    2482        7230 :     pi = pr_uniformizer(gel(L,i), F);
    2483        7230 :     q = nfpow(nf, pi, gel(e,i));
    2484        7230 :     z = z? nfmul(nf, z, q): q;
    2485             :   }
    2486        6815 :   if (!z) return gen_1;
    2487        4830 :   if (nored || typ(z) != t_COL) return z;
    2488         495 :   e2 = cgetg(r, t_VEC);
    2489         495 :   for (i=1; i<r; i++) gel(e2,i) = addiu(gel(e,i), 1);
    2490         495 :   x = factorbackprime(nf, L,e2);
    2491         495 :   if (flagden) /* denominator */
    2492             :   {
    2493         485 :     z = Q_remove_denom(z, &d);
    2494         485 :     d = diviiexact(d, Z_ppo(d, F));
    2495         485 :     x = RgM_Rg_mul(x, d);
    2496             :   }
    2497             :   else
    2498          10 :     d = NULL;
    2499         495 :   z = ZC_reducemodlll(z, x);
    2500         495 :   return d? RgC_Rg_div(z,d): z;
    2501             : }
    2502             : 
    2503             : GEN
    2504           0 : idealapprfact(GEN nf, GEN x) {
    2505           0 :   pari_sp av = avma;
    2506           0 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    2507             : }
    2508             : GEN
    2509          10 : idealappr(GEN nf, GEN x) {
    2510          10 :   pari_sp av = avma;
    2511          10 :   if (!is_nf_extfactor(x)) x = idealfactor(nf, x);
    2512          10 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    2513             : }
    2514             : 
    2515             : /* OBSOLETE */
    2516             : GEN
    2517          10 : idealappr0(GEN nf, GEN x, long fl) { (void)fl; return idealappr(nf, x); }
    2518             : 
    2519             : static GEN
    2520          15 : mat_ideal_two_elt2(GEN nf, GEN x, GEN a)
    2521             : {
    2522          15 :   GEN F = idealfactor(nf,a), P = gel(F,1), E = gel(F,2);
    2523          15 :   long i, r = lg(E);
    2524          15 :   for (i=1; i<r; i++) gel(E,i) = stoi( idealval(nf,x,gel(P,i)) );
    2525          15 :   return idealapprfact_i(nf,F,1);
    2526             : }
    2527             : 
    2528             : static void
    2529          10 : not_in_ideal(GEN a) {
    2530          10 :   pari_err_DOMAIN("idealtwoelt2","element mod ideal", "!=", gen_0, a);
    2531           0 : }
    2532             : /* x integral in HNF, a an 'nf' */
    2533             : static int
    2534          20 : in_ideal(GEN x, GEN a)
    2535             : {
    2536          20 :   switch(typ(a))
    2537             :   {
    2538          10 :     case t_INT: return dvdii(a, gcoeff(x,1,1));
    2539           5 :     case t_COL: return RgV_is_ZV(a) && !!hnf_invimage(x, a);
    2540           5 :     default: return 0;
    2541             :   }
    2542             : }
    2543             : 
    2544             : /* Given an integral ideal x and a in x, gives a b such that
    2545             :  * x = aZ_K + bZ_K using the approximation theorem */
    2546             : GEN
    2547          30 : idealtwoelt2(GEN nf, GEN x, GEN a)
    2548             : {
    2549          30 :   pari_sp av = avma;
    2550             :   GEN cx, b;
    2551             : 
    2552          30 :   nf = checknf(nf);
    2553          30 :   a = nf_to_scalar_or_basis(nf, a);
    2554          30 :   x = idealhnf_shallow(nf,x);
    2555          30 :   if (lg(x) == 1)
    2556             :   {
    2557          10 :     if (!isintzero(a)) not_in_ideal(a);
    2558           5 :     avma = av; return gen_0;
    2559             :   }
    2560          20 :   x = Q_primitive_part(x, &cx);
    2561          20 :   if (cx) a = gdiv(a, cx);
    2562          20 :   if (!in_ideal(x, a)) not_in_ideal(a);
    2563          15 :   b = mat_ideal_two_elt2(nf, x, a);
    2564          15 :   if (typ(b) == t_COL)
    2565             :   {
    2566          10 :     GEN mod = idealhnf_principal(nf,a);
    2567          10 :     b = ZC_hnfrem(b,mod);
    2568          10 :     if (ZV_isscalar(b)) b = gel(b,1);
    2569             :   }
    2570             :   else
    2571             :   {
    2572           5 :     GEN aZ = typ(a) == t_COL? Q_denom(zk_inv(nf,a)): a; /* (a) \cap Z */
    2573           5 :     b = centermodii(b, aZ, shifti(aZ,-1));
    2574             :   }
    2575          15 :   b = cx? gmul(b,cx): gcopy(b);
    2576          15 :   return gerepileupto(av, b);
    2577             : }
    2578             : 
    2579             : /* Given 2 integral ideals x and y in nf, returns a beta in nf such that
    2580             :  * beta * x is an integral ideal coprime to y */
    2581             : GEN
    2582        2465 : idealcoprimefact(GEN nf, GEN x, GEN fy)
    2583             : {
    2584        2465 :   GEN L = gel(fy,1), e;
    2585        2465 :   long i, r = lg(L);
    2586             : 
    2587        2465 :   e = cgetg(r, t_COL);
    2588        2465 :   for (i=1; i<r; i++) gel(e,i) = stoi( -idealval(nf,x,gel(L,i)) );
    2589        2465 :   return idealapprfact_i(nf, mkmat2(L,e), 0);
    2590             : }
    2591             : GEN
    2592          50 : idealcoprime(GEN nf, GEN x, GEN y)
    2593             : {
    2594          50 :   pari_sp av = avma;
    2595          50 :   return gerepileupto(av, idealcoprimefact(nf, x, idealfactor(nf,y)));
    2596             : }
    2597             : 
    2598             : GEN
    2599           5 : nfmulmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    2600             : {
    2601           5 :   pari_sp av = avma;
    2602           5 :   GEN z, p, pr = modpr, T;
    2603             : 
    2604           5 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    2605           0 :   x = nf_to_Fq(nf,x,modpr);
    2606           0 :   y = nf_to_Fq(nf,y,modpr);
    2607           0 :   z = Fq_mul(x,y,T,p);
    2608           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    2609             : }
    2610             : 
    2611             : GEN
    2612           0 : nfdivmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    2613             : {
    2614           0 :   pari_sp av = avma;
    2615           0 :   nf = checknf(nf);
    2616           0 :   return gerepileupto(av, nfreducemodpr(nf, nfdiv(nf,x,y), modpr));
    2617             : }
    2618             : 
    2619             : GEN
    2620           0 : nfpowmodpr(GEN nf, GEN x, GEN k, GEN modpr)
    2621             : {
    2622           0 :   pari_sp av=avma;
    2623           0 :   GEN z, T, p, pr = modpr;
    2624             : 
    2625           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    2626           0 :   z = nf_to_Fq(nf,x,modpr);
    2627           0 :   z = Fq_pow(z,k,T,p);
    2628           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    2629             : }
    2630             : 
    2631             : GEN
    2632           0 : nfkermodpr(GEN nf, GEN x, GEN modpr)
    2633             : {
    2634           0 :   pari_sp av = avma;
    2635           0 :   GEN T, p, pr = modpr;
    2636             : 
    2637           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    2638           0 :   if (typ(x)!=t_MAT) pari_err_TYPE("nfkermodpr",x);
    2639           0 :   x = nfM_to_FqM(x, nf, modpr);
    2640           0 :   return gerepilecopy(av, FqM_to_nfM(FqM_ker(x,T,p), modpr));
    2641             : }
    2642             : 
    2643             : GEN
    2644           0 : nfsolvemodpr(GEN nf, GEN a, GEN b, GEN pr)
    2645             : {
    2646           0 :   const char *f = "nfsolvemodpr";
    2647           0 :   pari_sp av = avma;
    2648             :   GEN T, p, modpr;
    2649             : 
    2650           0 :   nf = checknf(nf);
    2651           0 :   modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    2652           0 :   if (typ(a)!=t_MAT) pari_err_TYPE(f,a);
    2653           0 :   a = nfM_to_FqM(a, nf, modpr);
    2654           0 :   switch(typ(b))
    2655             :   {
    2656             :     case t_MAT:
    2657           0 :       b = nfM_to_FqM(b, nf, modpr);
    2658           0 :       b = FqM_gauss(a,b,T,p);
    2659           0 :       if (!b) pari_err_INV(f,a);
    2660           0 :       a = FqM_to_nfM(b, modpr);
    2661           0 :       break;
    2662             :     case t_COL:
    2663           0 :       b = nfV_to_FqV(b, nf, modpr);
    2664           0 :       b = FqM_FqC_gauss(a,b,T,p);
    2665           0 :       if (!b) pari_err_INV(f,a);
    2666           0 :       a = FqV_to_nfV(b, modpr);
    2667           0 :       break;
    2668           0 :     default: pari_err_TYPE(f,b);
    2669             :   }
    2670           0 :   return gerepilecopy(av, a);
    2671             : }

Generated by: LCOV version 1.11