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 20916-a74d914) Lines: 1314 1462 89.9 %
Date: 2017-08-18 06:23:59 Functions: 131 144 91.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /*******************************************************************/
      15             : /*                                                                 */
      16             : /*                       BASIC NF OPERATIONS                       */
      17             : /*                           (continued)                           */
      18             : /*                                                                 */
      19             : /*******************************************************************/
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : 
      23             : /*******************************************************************/
      24             : /*                                                                 */
      25             : /*                     IDEAL OPERATIONS                            */
      26             : /*                                                                 */
      27             : /*******************************************************************/
      28             : 
      29             : /* A valid ideal is either principal (valid nf_element), or prime, or a matrix
      30             :  * on the integer basis in HNF.
      31             :  * A prime ideal is of the form [p,a,e,f,b], where the ideal is p.Z_K+a.Z_K,
      32             :  * p is a rational prime, a belongs to Z_K, e=e(P/p), f=f(P/p), and b
      33             :  * is Lenstra's constant, such that p.P^(-1)= p Z_K + b Z_K.
      34             :  *
      35             :  * An extended ideal is a couple [I,F] where I is a valid ideal and F is
      36             :  * either an algebraic number, or a factorization matrix attached to an
      37             :  * algebraic number. All routines work with either extended ideals or ideals
      38             :  * (an omitted F is assumed to be [;] <-> 1).
      39             :  * All ideals are output in HNF form. */
      40             : 
      41             : /* types and conversions */
      42             : 
      43             : long
      44     3138006 : idealtyp(GEN *ideal, GEN *arch)
      45             : {
      46     3138006 :   GEN x = *ideal;
      47     3138006 :   long t,lx,tx = typ(x);
      48             : 
      49     3138006 :   if (tx==t_VEC && lg(x)==3)
      50      329778 :   { *arch = gel(x,2); x = gel(x,1); tx = typ(x); }
      51             :   else
      52     2808228 :     *arch = NULL;
      53     3138006 :   switch(tx)
      54             :   {
      55     1584090 :     case t_MAT: lx = lg(x);
      56     1584090 :       if (lx == 1) { t = id_PRINCIPAL; x = gen_0; break; }
      57     1584013 :       if (lx != lgcols(x)) pari_err_TYPE("idealtyp [non-square t_MAT]",x);
      58     1584006 :       t = id_MAT;
      59     1584006 :       break;
      60             : 
      61     1155993 :     case t_VEC: if (lg(x)!=6) pari_err_TYPE("idealtyp",x);
      62     1155979 :       t = id_PRIME; break;
      63             : 
      64             :     case t_POL: case t_POLMOD: case t_COL:
      65             :     case t_INT: case t_FRAC:
      66      397923 :       t = id_PRINCIPAL; break;
      67             :     default:
      68           0 :       pari_err_TYPE("idealtyp",x);
      69             :       return 0; /*LCOV_EXCL_LINE*/
      70             :   }
      71     3137985 :   *ideal = x; return t;
      72             : }
      73             : 
      74             : /* true nf; v = [a,x,...], a in Z. Return (a,x) */
      75             : GEN
      76      111000 : idealhnf_two(GEN nf, GEN v)
      77             : {
      78      111000 :   GEN p = gel(v,1), pi = gel(v,2), m = zk_scalar_or_multable(nf, pi);
      79      111000 :   if (typ(m) == t_INT) return scalarmat(gcdii(m,p), nf_get_degree(nf));
      80       96769 :   return ZM_hnfmodid(m, p);
      81             : }
      82             : /* true nf */
      83             : GEN
      84     1167171 : pr_hnf(GEN nf, GEN pr)
      85             : {
      86     1167171 :   GEN p = pr_get_p(pr), m;
      87     1167171 :   if (pr_is_inert(pr)) return scalarmat(p, nf_get_degree(nf));
      88      909837 :   m = zk_scalar_or_multable(nf, pr_get_gen(pr));
      89      909837 :   return ZM_hnfmodprime(m, p);
      90             : }
      91             : 
      92             : static GEN
      93       66273 : ZM_Q_mul(GEN x, GEN y)
      94       66273 : { return typ(y) == t_INT? ZM_Z_mul(x,y): RgM_Rg_mul(x,y); }
      95             : 
      96             : 
      97             : GEN
      98      268357 : idealhnf_principal(GEN nf, GEN x)
      99             : {
     100             :   GEN cx;
     101      268357 :   x = nf_to_scalar_or_basis(nf, x);
     102      268357 :   switch(typ(x))
     103             :   {
     104      154169 :     case t_COL: break;
     105       90550 :     case t_INT:  if (!signe(x)) return cgetg(1,t_MAT);
     106       90403 :       return scalarmat(absi(x), nf_get_degree(nf));
     107             :     case t_FRAC:
     108       23638 :       return scalarmat(Q_abs_shallow(x), nf_get_degree(nf));
     109           0 :     default: pari_err_TYPE("idealhnf",x);
     110             :   }
     111      154169 :   x = Q_primitive_part(x, &cx);
     112      154169 :   RgV_check_ZV(x, "idealhnf");
     113      154169 :   x = zk_multable(nf, x);
     114      154169 :   x = ZM_hnfmodid(x, zkmultable_capZ(x));
     115      154169 :   return cx? ZM_Q_mul(x,cx): x;
     116             : }
     117             : 
     118             : /* x integral ideal in t_MAT form, nx columns */
     119             : static GEN
     120           7 : vec_mulid(GEN nf, GEN x, long nx, long N)
     121             : {
     122           7 :   GEN m = cgetg(nx*N + 1, t_MAT);
     123             :   long i, j, k;
     124          21 :   for (i=k=1; i<=nx; i++)
     125          14 :     for (j=1; j<=N; j++) gel(m, k++) = zk_ei_mul(nf, gel(x,i),j);
     126           7 :   return m;
     127             : }
     128             : /* true nf */
     129             : GEN
     130      321781 : idealhnf_shallow(GEN nf, GEN x)
     131             : {
     132      321781 :   long tx = typ(x), lx = lg(x), N;
     133             : 
     134             :   /* cannot use idealtyp because here we allow non-square matrices */
     135      321781 :   if (tx == t_VEC && lx == 3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
     136      321781 :   if (tx == t_VEC && lx == 6) return pr_hnf(nf,x); /* PRIME */
     137      222278 :   switch(tx)
     138             :   {
     139             :     case t_MAT:
     140             :     {
     141             :       GEN cx;
     142       46788 :       long nx = lx-1;
     143       46788 :       N = nf_get_degree(nf);
     144       46788 :       if (nx == 0) return cgetg(1, t_MAT);
     145       46767 :       if (nbrows(x) != N) pari_err_TYPE("idealhnf [wrong dimension]",x);
     146       46760 :       if (nx == 1) return idealhnf_principal(nf, gel(x,1));
     147             : 
     148       45549 :       if (nx == N && RgM_is_ZM(x) && ZM_ishnf(x)) return x;
     149       22330 :       x = Q_primitive_part(x, &cx);
     150       22330 :       if (nx < N) x = vec_mulid(nf, x, nx, N);
     151       22330 :       x = ZM_hnfmod(x, ZM_detmult(x));
     152       22330 :       return cx? ZM_Q_mul(x,cx): x;
     153             :     }
     154             :     case t_QFI:
     155             :     case t_QFR:
     156             :     {
     157          14 :       pari_sp av = avma;
     158          14 :       GEN u, D = nf_get_disc(nf), T = nf_get_pol(nf), f = nf_get_index(nf);
     159          14 :       GEN A = gel(x,1), B = gel(x,2);
     160          14 :       N = nf_get_degree(nf);
     161          14 :       if (N != 2)
     162           0 :         pari_err_TYPE("idealhnf [Qfb for non-quadratic fields]", x);
     163          14 :       if (!equalii(qfb_disc(x), D))
     164           7 :         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           7 :       u = gel(T,3);
     170           7 :       B = deg1pol_shallow(ginv(f),
     171             :                           gsub(gdiv(u, shifti(f,1)), gdiv(B,gen_2)),
     172           7 :                           varn(T));
     173           7 :       return gerepileupto(av, idealhnf_two(nf, mkvec2(A,B)));
     174             :     }
     175      175476 :     default: return idealhnf_principal(nf, x); /* PRINCIPAL */
     176             :   }
     177             : }
     178             : GEN
     179        3101 : idealhnf(GEN nf, GEN x)
     180             : {
     181        3101 :   pari_sp av = avma;
     182        3101 :   GEN y = idealhnf_shallow(checknf(nf), x);
     183        3087 :   return (avma == av)? gcopy(y): gerepileupto(av, y);
     184             : }
     185             : 
     186             : /* GP functions */
     187             : 
     188             : GEN
     189          63 : idealtwoelt0(GEN nf, GEN x, GEN a)
     190             : {
     191          63 :   if (!a) return idealtwoelt(nf,x);
     192          42 :   return idealtwoelt2(nf,x,a);
     193             : }
     194             : 
     195             : GEN
     196          42 : idealpow0(GEN nf, GEN x, GEN n, long flag)
     197             : {
     198          42 :   if (flag) return idealpowred(nf,x,n);
     199          35 :   return idealpow(nf,x,n);
     200             : }
     201             : 
     202             : GEN
     203          56 : idealmul0(GEN nf, GEN x, GEN y, long flag)
     204             : {
     205          56 :   if (flag) return idealmulred(nf,x,y);
     206          49 :   return idealmul(nf,x,y);
     207             : }
     208             : 
     209             : GEN
     210          42 : idealdiv0(GEN nf, GEN x, GEN y, long flag)
     211             : {
     212          42 :   switch(flag)
     213             :   {
     214          21 :     case 0: return idealdiv(nf,x,y);
     215          21 :     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          70 : idealaddtoone0(GEN nf, GEN arg1, GEN arg2)
     223             : {
     224          70 :   if (!arg2) return idealaddmultoone(nf,arg1);
     225          35 :   return idealaddtoone(nf,arg1,arg2);
     226             : }
     227             : 
     228             : /* b not a scalar */
     229             : static GEN
     230          28 : 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          21 : hnf_Z_QC(GEN nf, GEN a, GEN b)
     234             : {
     235             :   GEN db;
     236          21 :   b = Q_remove_denom(b, &db);
     237          21 :   if (db) a = mulii(a, db);
     238          21 :   b = hnf_Z_ZC(nf,a,b);
     239          21 :   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          28 : hnf_Q_QC(GEN nf, GEN a, GEN b)
     244             : {
     245             :   GEN da, db;
     246          28 :   if (typ(a) == t_INT) return hnf_Z_QC(nf, a, b);
     247           7 :   da = gel(a,2);
     248           7 :   a = gel(a,1);
     249           7 :   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           7 :   if (db)
     253             :   {
     254           7 :     GEN d = gcdii(da,db);
     255           7 :     if (!is_pm1(d)) db = diviiexact(db,d); /* B */
     256           7 :     if (!is_pm1(db))
     257             :     {
     258           7 :       a = mulii(a, db); /* a B */
     259           7 :       da = mulii(da, db); /* A B d = lcm(denom(a),denom(b)) */
     260             :     }
     261             :   }
     262           7 :   return RgM_Rg_div(hnf_Z_ZC(nf,a,b), da);
     263             : }
     264             : static GEN
     265           7 : hnf_QC_QC(GEN nf, GEN a, GEN b)
     266             : {
     267             :   GEN da, db, d, x;
     268           7 :   a = Q_remove_denom(a, &da);
     269           7 :   b = Q_remove_denom(b, &db);
     270           7 :   if (da) b = ZC_Z_mul(b, da);
     271           7 :   if (db) a = ZC_Z_mul(a, db);
     272           7 :   d = mul_denom(da, db);
     273           7 :   a = zk_multable(nf,a); da = zkmultable_capZ(a);
     274           7 :   b = zk_multable(nf,b); db = zkmultable_capZ(b);
     275           7 :   x = ZM_hnfmodid(shallowconcat(a,b), gcdii(da,db));
     276           7 :   return d? RgM_Rg_div(x, d): x;
     277             : }
     278             : static GEN
     279          21 : hnf_Q_Q(GEN nf, GEN a, GEN b) {return scalarmat(Q_gcd(a,b), nf_get_degree(nf));}
     280             : GEN
     281         119 : idealhnf0(GEN nf, GEN a, GEN b)
     282             : {
     283             :   long ta, tb;
     284             :   pari_sp av;
     285             :   GEN x;
     286         119 :   if (!b) return idealhnf(nf,a);
     287             : 
     288             :   /* HNF of aZ_K+bZ_K */
     289          56 :   av = avma; nf = checknf(nf);
     290          56 :   a = nf_to_scalar_or_basis(nf,a); ta = typ(a);
     291          56 :   b = nf_to_scalar_or_basis(nf,b); tb = typ(b);
     292          56 :   if (ta == t_COL)
     293          14 :     x = (tb==t_COL)? hnf_QC_QC(nf, a,b): hnf_Q_QC(nf, b,a);
     294             :   else
     295          42 :     x = (tb==t_COL)? hnf_Q_QC(nf, a,b): hnf_Q_Q(nf, a,b);
     296          56 :   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      155491 : ok_elt(GEN x, GEN xZ, GEN y)
     308             : {
     309      155491 :   pari_sp av = avma;
     310      155491 :   int r = ZM_equal(x, ZM_hnfmodid(y, xZ));
     311      155491 :   avma = av; return r;
     312             : }
     313             : 
     314             : static GEN
     315       53264 : addmul_col(GEN a, long s, GEN b)
     316             : {
     317             :   long i,l;
     318       53264 :   if (!s) return a? leafcopy(a): a;
     319       53090 :   if (!a) return gmulsg(s,b);
     320       49948 :   l = lg(a);
     321      266592 :   for (i=1; i<l; i++)
     322      216644 :     if (signe(gel(b,i))) gel(a,i) = addii(gel(a,i), mulsi(s, gel(b,i)));
     323       49948 :   return a;
     324             : }
     325             : 
     326             : /* a <-- a + s * b, all coeffs integers */
     327             : static GEN
     328       23387 : addmul_mat(GEN a, long s, GEN b)
     329             : {
     330             :   long j,l;
     331             :   /* copy otherwise next call corrupts a */
     332       23387 :   if (!s) return a? RgM_shallowcopy(a): a;
     333       21804 :   if (!a) return gmulsg(s,b);
     334       11809 :   l = lg(a);
     335       57081 :   for (j=1; j<l; j++)
     336       45272 :     (void)addmul_col(gel(a,j), s, gel(b,j));
     337       11809 :   return a;
     338             : }
     339             : 
     340             : static GEN
     341       80415 : get_random_a(GEN nf, GEN x, GEN xZ)
     342             : {
     343             :   pari_sp av;
     344       80415 :   long i, lm, l = lg(x);
     345             :   GEN a, z, beta, mul;
     346             : 
     347       80415 :   beta= cgetg(l, t_VEC);
     348       80415 :   mul = cgetg(l, t_VEC); lm = 1; /* = lg(mul) */
     349             :   /* look for a in x such that a O/xZ = x O/xZ */
     350      159985 :   for (i = 2; i < l; i++)
     351             :   {
     352      156843 :     GEN xi = gel(x,i);
     353      156843 :     GEN t = FpM_red(zk_multable(nf,xi), xZ); /* ZM, cannot be a scalar */
     354      156843 :     if (gequal0(t)) continue;
     355      145496 :     if (ok_elt(x,xZ, t)) return xi;
     356       68223 :     gel(beta,lm) = xi;
     357             :     /* mul[i] = { canonical generators for x[i] O/xZ as Z-module } */
     358       68223 :     gel(mul,lm) = t; lm++;
     359             :   }
     360        3142 :   setlg(mul, lm);
     361        3142 :   setlg(beta,lm);
     362        3142 :   z = cgetg(lm, t_VECSMALL);
     363       10051 :   for(av = avma;; avma = av)
     364             :   {
     365       33438 :     for (a=NULL,i=1; i<lm; i++)
     366             :     {
     367       23387 :       long t = random_bits(4) - 7; /* in [-7,8] */
     368       23387 :       z[i] = t;
     369       23387 :       a = addmul_mat(a, t, gel(mul,i));
     370             :     }
     371             :     /* a = matrix (NOT HNF) of ideal generated by beta.z in O/xZ */
     372       10051 :     if (a && ok_elt(x,xZ, a)) break;
     373        6909 :   }
     374       11134 :   for (a=NULL,i=1; i<lm; i++)
     375        7992 :     a = addmul_col(a, z[i], gel(beta,i));
     376        3142 :   return a;
     377             : }
     378             : 
     379             : /* x square matrix, assume it is HNF */
     380             : static GEN
     381      198865 : mat_ideal_two_elt(GEN nf, GEN x)
     382             : {
     383             :   GEN y, a, cx, xZ;
     384      198865 :   long N = nf_get_degree(nf);
     385             :   pari_sp av, tetpil;
     386             : 
     387      198865 :   if (lg(x)-1 != N) pari_err_DIM("idealtwoelt");
     388      198851 :   if (N == 2) return mkvec2copy(gcoeff(x,1,1), gel(x,2));
     389             : 
     390       90513 :   y = cgetg(3,t_VEC); av = avma;
     391       90513 :   cx = Q_content(x);
     392       90513 :   xZ = gcoeff(x,1,1);
     393       90513 :   if (gequal(xZ, cx)) /* x = (cx) */
     394             :   {
     395        3290 :     gel(y,1) = cx;
     396        3290 :     gel(y,2) = gen_0; return y;
     397             :   }
     398       87223 :   if (equali1(cx)) cx = NULL;
     399             :   else
     400             :   {
     401        1477 :     x = Q_div_to_int(x, cx);
     402        1477 :     xZ = gcoeff(x,1,1);
     403             :   }
     404       87223 :   if (N < 6)
     405       75924 :     a = get_random_a(nf, x, xZ);
     406             :   else
     407             :   {
     408       11299 :     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       11299 :     GEN P, E, a1 = Z_smoothen(xZ, (GEN)FB, &P, &E);
     412       11299 :     if (!a1) /* factors completely */
     413        6808 :       a = idealapprfact_i(nf, idealfactor(nf,x), 1);
     414        4491 :     else if (lg(P) == 1) /* no small factors */
     415        3164 :       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        1327 :       a0 = diviiexact(xZ, a1);
     420        1327 :       A0 = ZM_hnfmodid(x, a0); /* smooth part of x */
     421        1327 :       A1 = ZM_hnfmodid(x, a1); /* cofactor */
     422        1327 :       pi0 = idealapprfact_i(nf, idealfactor(nf,A0), 1);
     423        1327 :       pi1 = get_random_a(nf, A1, a1);
     424        1327 :       (void)bezout(a0, a1, &v0,&v1);
     425        1327 :       u0 = mulii(a0, v0);
     426        1327 :       u1 = mulii(a1, v1);
     427        1327 :       if (typ(pi0) != t_COL) t = addmulii(u0, pi0, u1);
     428             :       else
     429        1327 :       { t = ZC_Z_mul(pi0, u1); gel(t,1) = addii(gel(t,1), u0); }
     430        1327 :       u = ZC_Z_mul(pi1, u0); gel(u,1) = addii(gel(u,1), u1);
     431        1327 :       a = nfmuli(nf, centermod(u, xZ), centermod(t, xZ));
     432             :     }
     433             :   }
     434       87223 :   if (cx)
     435             :   {
     436        1477 :     a = centermod(a, xZ);
     437        1477 :     tetpil = avma;
     438        1477 :     if (typ(cx) == t_INT)
     439             :     {
     440         357 :       gel(y,1) = mulii(xZ, cx);
     441         357 :       gel(y,2) = ZC_Z_mul(a, cx);
     442             :     }
     443             :     else
     444             :     {
     445        1120 :       gel(y,1) = gmul(xZ, cx);
     446        1120 :       gel(y,2) = RgC_Rg_mul(a, cx);
     447             :     }
     448             :   }
     449             :   else
     450             :   {
     451       85746 :     tetpil = avma;
     452       85746 :     gel(y,1) = icopy(xZ);
     453       85746 :     gel(y,2) = centermod(a, xZ);
     454             :   }
     455       87223 :   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       42208 : idealtwoelt(GEN nf, GEN x)
     464             : {
     465             :   pari_sp av;
     466             :   GEN z;
     467       42208 :   long tx = idealtyp(&x,&z);
     468       42201 :   nf = checknf(nf);
     469       42201 :   if (tx == id_MAT) return mat_ideal_two_elt(nf,x);
     470        1673 :   if (tx == id_PRIME) return mkvec2copy(gel(x,1), gel(x,2));
     471             :   /* id_PRINCIPAL */
     472         868 :   av = avma; x = nf_to_scalar_or_basis(nf, x);
     473        1540 :   return gerepilecopy(av, typ(x)==t_COL? mkvec2(gen_0,x):
     474         763 :                                          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      201345 : idealHNF_norm_pval(GEN x, GEN p, long Zval)
     485             : {
     486      201345 :   long i, v = Zval, l = lg(x);
     487      201345 :   for (i = 2; i < l; i++) v += Z_pval(gcoeff(x,i,i), p);
     488      201345 :   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       37010 : idealHNF_Z_factor(GEN x, GEN *pvN, GEN *pvZ)
     495             : {
     496       37010 :   GEN xZ = gcoeff(x,1,1), f, P, E, vN, vZ;
     497             :   long i, l;
     498       37010 :   if (typ(xZ) != t_INT) pari_err_TYPE("idealfactor",x);
     499       37010 :   f = Z_factor(xZ);
     500       37010 :   P = gel(f,1); l = lg(P);
     501       37010 :   E = gel(f,2);
     502       37010 :   *pvN = vN = cgetg(l, t_VECSMALL);
     503       37010 :   *pvZ = vZ = cgetg(l, t_VECSMALL);
     504       69924 :   for (i = 1; i < l; i++)
     505             :   {
     506       32914 :     vZ[i] = itou(gel(E,i));
     507       32914 :     vN[i] = idealHNF_norm_pval(x,gel(P,i), vZ[i]);
     508             :   }
     509       37010 :   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      218866 : idealHNF_val(GEN A, GEN P, long Nval, long Zval)
     516             : {
     517      218866 :   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      218866 :   if (Nval < f) return 0;
     522      218803 :   p = pr_get_p(P);
     523      218803 :   e = pr_get_e(P);
     524             :   /* v_P(A) <= max [ e * v_p(A \cap Z), floor[v_p(Nix) / f ] */
     525      218803 :   vmax = minss(Zval * e, Nval / f);
     526      218803 :   mul = pr_get_tau(P);
     527      218803 :   l = lg(mul);
     528      218803 :   B = cgetg(l,t_MAT);
     529             :   /* B[1] not needed: v_pr(A[1]) = v_pr(A \cap Z) is known already */
     530      218803 :   gel(B,1) = gen_0; /* dummy */
     531      675760 :   for (j = 2; j < l; j++)
     532             :   {
     533      528813 :     GEN x = gel(A,j);
     534      528813 :     gel(B,j) = y = cgetg(l, t_COL);
     535     4296400 :     for (i = 1; i < l; i++)
     536             :     { /* compute a = (x.t0)_i, A in HNF ==> x[j+1..l-1] = 0 */
     537     3839443 :       a = mulii(gel(x,1), gcoeff(mul,i,1));
     538     3839443 :       for (k = 2; k <= j; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     539             :       /* p | a ? */
     540     3839443 :       gel(y,i) = dvmdii(a,p,&r); if (signe(r)) return 0;
     541             :     }
     542             :   }
     543      146947 :   vals = cgetg(l, t_VECSMALL);
     544             :   /* vals[1] not needed */
     545      538682 :   for (j = 2; j < l; j++)
     546             :   {
     547      391735 :     gel(B,j) = Q_primitive_part(gel(B,j), &cx);
     548      391735 :     vals[j] = cx? 1 + e * Q_pval(cx, p): 1;
     549             :   }
     550      146947 :   pk = powiu(p, ceildivuu(vmax, e));
     551      146947 :   av = avma; y = cgetg(l,t_COL);
     552             :   /* can compute mod p^ceil((vmax-v)/e) */
     553      209602 :   for (v = 1; v < vmax; v++)
     554             :   { /* we know v_pr(Bj) >= v for all j */
     555       66012 :     if (e == 1 || (vmax - v) % e == 0) pk = diviiexact(pk, p);
     556      510686 :     for (j = 2; j < l; j++)
     557             :     {
     558      448031 :       GEN x = gel(B,j); if (v < vals[j]) continue;
     559     4481126 :       for (i = 1; i < l; i++)
     560             :       {
     561     4155904 :         pari_sp av2 = avma;
     562     4155904 :         a = mulii(gel(x,1), gcoeff(mul,i,1));
     563     4155904 :         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     4155904 :         a = dvmdii(a,p,&r); if (signe(r)) return v;
     566     4152547 :         if (lgefint(a) > lgefint(pk)) a = remii(a, pk);
     567     4152547 :         gel(y,i) = gerepileuptoint(av2, a);
     568             :       }
     569      325222 :       gel(B,j) = y; y = x;
     570      325222 :       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      143590 :   return v;
     578             : }
     579             : /* true nf, x integral ideal */
     580             : static GEN
     581       37010 : idealHNF_factor(GEN nf, GEN x)
     582             : {
     583       37010 :   const long N = lg(x)-1;
     584             :   long i, j, k, l, v;
     585             :   GEN vp, vN, vZ, vP, vE, cx;
     586             : 
     587       37010 :   x = Q_primitive_part(x, &cx);
     588       37010 :   vp = idealHNF_Z_factor(x, &vN,&vZ);
     589       37010 :   l = lg(vp);
     590       37010 :   i = cx? expi(cx)+1: 1;
     591       37010 :   vP = cgetg((l+i-2)*N+1, t_COL);
     592       37010 :   vE = cgetg((l+i-2)*N+1, t_COL);
     593       69924 :   for (i = k = 1; i < l; i++)
     594             :   {
     595       32914 :     GEN L, p = gel(vp,i);
     596       32914 :     long Nval = vN[i], Zval = vZ[i], vc = cx? Z_pvalrem(cx,p,&cx): 0;
     597       32914 :     if (vc)
     598             :     {
     599        1757 :       L = idealprimedec(nf,p);
     600        1757 :       if (is_pm1(cx)) cx = NULL;
     601             :     }
     602             :     else
     603       31157 :       L = idealprimedec_limit_f(nf,p,Nval);
     604       50442 :     for (j = 1; j < lg(L); j++)
     605             :     {
     606       50435 :       GEN P = gel(L,j);
     607       50435 :       pari_sp av = avma;
     608       50435 :       v = idealHNF_val(x, P, Nval, Zval);
     609       50435 :       avma = av;
     610       50435 :       Nval -= v*pr_get_f(P);
     611       50435 :       v += vc * pr_get_e(P); if (!v) continue;
     612       37442 :       gel(vP,k) = P;
     613       37442 :       gel(vE,k) = utoipos(v); k++;
     614       37442 :       if (!Nval) break; /* now only the content contributes */
     615             :     }
     616       33629 :     if (vc) for (j++; j<lg(L); j++)
     617             :     {
     618         715 :       GEN P = gel(L,j);
     619         715 :       gel(vP,k) = P;
     620         715 :       gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     621             :     }
     622             :   }
     623       37010 :   if (cx)
     624             :   {
     625        7462 :     GEN f = Z_factor(cx), cP = gel(f,1), cE = gel(f,2);
     626        7462 :     long lc = lg(cP);
     627       15666 :     for (i=1; i<lc; i++)
     628             :     {
     629        8204 :       GEN p = gel(cP,i), L = idealprimedec(nf,p);
     630        8204 :       long vc = itos(gel(cE,i));
     631       18179 :       for (j=1; j<lg(L); j++)
     632             :       {
     633        9975 :         GEN P = gel(L,j);
     634        9975 :         gel(vP,k) = P;
     635        9975 :         gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     636             :       }
     637             :     }
     638             :   }
     639       37010 :   setlg(vP, k);
     640       37010 :   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        3031 : prV_e_muls(GEN L, long c)
     645             : {
     646        3031 :   long j, l = lg(L);
     647        3031 :   GEN z = cgetg(l, t_COL);
     648        3031 :   for (j = 1; j < l; j++) gel(z,j) = stoi(c * pr_get_e(gel(L,j)));
     649        3031 :   return z;
     650             : }
     651             : /* true nf, y in Q */
     652             : static GEN
     653        3059 : Q_nffactor(GEN nf, GEN y)
     654             : {
     655             :   GEN f, P, E;
     656             :   long lfa, i;
     657        3059 :   if (typ(y) == t_INT)
     658             :   {
     659        3045 :     if (!signe(y)) pari_err_DOMAIN("idealfactor", "ideal", "=",gen_0,y);
     660        3024 :     if (is_pm1(y)) return trivial_fact();
     661             :   }
     662        2289 :   f = factor(Q_abs_shallow(y));
     663        2289 :   P = gel(f,1); lfa = lg(P);
     664        2289 :   E = gel(f,2);
     665        5320 :   for (i = 1; i < lfa; i++)
     666             :   {
     667        3031 :     gel(P,i) = idealprimedec(nf, gel(P,i));
     668        3031 :     gel(E,i) = prV_e_muls(gel(P,i), itos(gel(E,i)));
     669             :   }
     670        2289 :   settyp(P,t_VEC); P = shallowconcat1(P);
     671        2289 :   settyp(E,t_VEC); E = shallowconcat1(E);
     672        2289 :   gel(f,1) = P; settyp(P, t_COL);
     673        2289 :   gel(f,2) = E; return f;
     674             : }
     675             : 
     676             : GEN
     677       40097 : idealfactor(GEN nf, GEN x)
     678             : {
     679       40097 :   pari_sp av = avma;
     680             :   GEN fa, y;
     681       40097 :   long tx = idealtyp(&x,&y);
     682             : 
     683       40097 :   nf = checknf(nf);
     684       40097 :   if (tx == id_PRIME) retmkmat2(mkcolcopy(x), mkcol(gen_1));
     685       40062 :   if (tx == id_PRINCIPAL)
     686             :   {
     687        4991 :     y = nf_to_scalar_or_basis(nf, x);
     688        4991 :     if (typ(y) != t_COL) return gerepilecopy(av, Q_nffactor(nf, y));
     689             :   }
     690       37003 :   y = idealnumden(nf, x);
     691       37003 :   fa = idealHNF_factor(nf, gel(y,1));
     692       37003 :   if (!isint1(gel(y,2)))
     693             :   {
     694           7 :     GEN F = idealHNF_factor(nf, gel(y,2));
     695           7 :     fa = famat_mul_shallow(fa, famat_inv_shallow(F));
     696             :   }
     697       37003 :   fa = gerepilecopy(av, fa);
     698       37003 :   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      496656 : idealval(GEN nf, GEN A, GEN P)
     704             : {
     705      496656 :   pari_sp av = avma;
     706             :   GEN a, p, cA;
     707      496656 :   long vcA, v, Zval, tx = idealtyp(&A,&a);
     708             : 
     709      496656 :   if (tx == id_PRINCIPAL) return nfval(nf,A,P);
     710      492225 :   checkprid(P);
     711      492225 :   if (tx == id_PRIME) return pr_equal(P, A)? 1: 0;
     712             :   /* id_MAT */
     713      492197 :   nf = checknf(nf);
     714      492197 :   A = Q_primitive_part(A, &cA);
     715      492197 :   p = pr_get_p(P);
     716      492197 :   vcA = cA? Q_pval(cA,p): 0;
     717      492197 :   if (pr_is_inert(P)) { avma = av; return vcA; }
     718      484196 :   Zval = Z_pval(gcoeff(A,1,1), p);
     719      484196 :   if (!Zval) v = 0;
     720             :   else
     721             :   {
     722      168431 :     long Nval = idealHNF_norm_pval(A, p, Zval);
     723      168431 :     v = idealHNF_val(A, P, Nval, Zval);
     724             :   }
     725      484196 :   avma = av; return vcA? v + vcA*pr_get_e(P): v;
     726             : }
     727             : GEN
     728        6573 : gpidealval(GEN nf, GEN ix, GEN P)
     729             : {
     730        6573 :   long v = idealval(nf,ix,P);
     731        6573 :   return v == LONG_MAX? mkoo(): stoi(v);
     732             : }
     733             : 
     734             : /* gcd and generalized Bezout */
     735             : 
     736             : GEN
     737       58919 : idealadd(GEN nf, GEN x, GEN y)
     738             : {
     739       58919 :   pari_sp av = avma;
     740             :   long tx, ty;
     741             :   GEN z, a, dx, dy, dz;
     742             : 
     743       58919 :   tx = idealtyp(&x,&z);
     744       58919 :   ty = idealtyp(&y,&z); nf = checknf(nf);
     745       58919 :   if (tx != id_MAT) x = idealhnf_shallow(nf,x);
     746       58919 :   if (ty != id_MAT) y = idealhnf_shallow(nf,y);
     747       58919 :   if (lg(x) == 1) return gerepilecopy(av,y);
     748       58912 :   if (lg(y) == 1) return gerepilecopy(av,x); /* check for 0 ideal */
     749       58870 :   dx = Q_denom(x);
     750       58870 :   dy = Q_denom(y); dz = lcmii(dx,dy);
     751       58870 :   if (is_pm1(dz)) dz = NULL; else {
     752       12453 :     x = Q_muli_to_int(x, dz);
     753       12453 :     y = Q_muli_to_int(y, dz);
     754             :   }
     755       58870 :   a = gcdii(gcoeff(x,1,1), gcoeff(y,1,1));
     756       58870 :   if (is_pm1(a))
     757             :   {
     758       27516 :     long N = lg(x)-1;
     759       27516 :     if (!dz) { avma = av; return matid(N); }
     760        3611 :     return gerepileupto(av, scalarmat(ginv(dz), N));
     761             :   }
     762       31354 :   z = ZM_hnfmodid(shallowconcat(x,y), a);
     763       31354 :   if (dz) z = RgM_Rg_div(z,dz);
     764       31354 :   return gerepileupto(av,z);
     765             : }
     766             : 
     767             : static GEN
     768          28 : trivial_merge(GEN x)
     769          28 : { return (lg(x) == 1 || !is_pm1(gcoeff(x,1,1)))? NULL: gen_1; }
     770             : /* true nf */
     771             : static GEN
     772      120617 : _idealaddtoone(GEN nf, GEN x, GEN y, long red)
     773             : {
     774             :   GEN a;
     775      120617 :   long tx = idealtyp(&x, &a/*junk*/);
     776      120617 :   long ty = idealtyp(&y, &a/*junk*/);
     777             :   long ea;
     778      120617 :   if (tx != id_MAT) x = idealhnf_shallow(nf, x);
     779      120617 :   if (ty != id_MAT) y = idealhnf_shallow(nf, y);
     780      120617 :   if (lg(x) == 1)
     781          14 :     a = trivial_merge(y);
     782      120603 :   else if (lg(y) == 1)
     783          14 :     a = trivial_merge(x);
     784             :   else
     785      120589 :     a = hnfmerge_get_1(x, y);
     786      120617 :   if (!a) pari_err_COPRIME("idealaddtoone",x,y);
     787      120603 :   if (red && (ea = gexpo(a)) > 10)
     788             :   {
     789        6736 :     GEN b = (typ(a) == t_COL)? a: scalarcol_shallow(a, nf_get_degree(nf));
     790        6736 :     b = ZC_reducemodlll(b, idealHNF_mul(nf,x,y));
     791        6736 :     if (gexpo(b) < ea) a = b;
     792             :   }
     793      120603 :   return a;
     794             : }
     795             : /* true nf */
     796             : GEN
     797       12229 : idealaddtoone_i(GEN nf, GEN x, GEN y)
     798       12229 : { return _idealaddtoone(nf, x, y, 1); }
     799             : /* true nf */
     800             : GEN
     801      108388 : idealaddtoone_raw(GEN nf, GEN x, GEN y)
     802      108388 : { return _idealaddtoone(nf, x, y, 0); }
     803             : 
     804             : GEN
     805          98 : idealaddtoone(GEN nf, GEN x, GEN y)
     806             : {
     807          98 :   GEN z = cgetg(3,t_VEC), a;
     808          98 :   pari_sp av = avma;
     809          98 :   nf = checknf(nf);
     810          98 :   a = gerepileupto(av, idealaddtoone_i(nf,x,y));
     811          84 :   gel(z,1) = a;
     812          84 :   gel(z,2) = typ(a) == t_COL? Z_ZC_sub(gen_1,a): subui(1,a);
     813          84 :   return z;
     814             : }
     815             : 
     816             : /* assume elements of list are integral ideals */
     817             : GEN
     818          35 : idealaddmultoone(GEN nf, GEN list)
     819             : {
     820          35 :   pari_sp av = avma;
     821          35 :   long N, i, l, nz, tx = typ(list);
     822             :   GEN H, U, perm, L;
     823             : 
     824          35 :   nf = checknf(nf); N = nf_get_degree(nf);
     825          35 :   if (!is_vec_t(tx)) pari_err_TYPE("idealaddmultoone",list);
     826          35 :   l = lg(list);
     827          35 :   L = cgetg(l, t_VEC);
     828          35 :   if (l == 1)
     829           0 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
     830          35 :   nz = 0; /* number of non-zero ideals in L */
     831          98 :   for (i=1; i<l; i++)
     832             :   {
     833          70 :     GEN I = gel(list,i);
     834          70 :     if (typ(I) != t_MAT) I = idealhnf_shallow(nf,I);
     835          70 :     if (lg(I) != 1)
     836             :     {
     837          42 :       nz++; RgM_check_ZM(I,"idealaddmultoone");
     838          35 :       if (lgcols(I) != N+1) pari_err_TYPE("idealaddmultoone [not an ideal]", I);
     839             :     }
     840          63 :     gel(L,i) = I;
     841             :   }
     842          28 :   H = ZM_hnfperm(shallowconcat1(L), &U, &perm);
     843          28 :   if (lg(H) == 1 || !equali1(gcoeff(H,1,1)))
     844           7 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
     845          49 :   for (i=1; i<=N; i++)
     846          49 :     if (perm[i] == 1) break;
     847          21 :   U = gel(U,(nz-1)*N + i); /* (L[1]|...|L[nz]) U = 1 */
     848          21 :   nz = 0;
     849          63 :   for (i=1; i<l; i++)
     850             :   {
     851          42 :     GEN c = gel(L,i);
     852          42 :     if (lg(c) == 1)
     853          14 :       c = gen_0;
     854             :     else {
     855          28 :       c = ZM_ZC_mul(c, vecslice(U, nz*N + 1, (nz+1)*N));
     856          28 :       nz++;
     857             :     }
     858          42 :     gel(L,i) = c;
     859             :   }
     860          21 :   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      632313 : idealHNF_mul_two(GEN nf, GEN x, GEN y)
     870             : {
     871      632313 :   GEN m, a = gel(y,1), alpha = gel(y,2);
     872             :   long i, N;
     873             : 
     874      632313 :   if (typ(alpha) != t_MAT)
     875             :   {
     876      427373 :     alpha = zk_scalar_or_multable(nf, alpha);
     877      427373 :     if (typ(alpha) == t_INT) /* e.g. y inert ? 0 should not (but may) occur */
     878        3003 :       return signe(a)? ZM_Z_mul(x, gcdii(a, alpha)): cgetg(1,t_MAT);
     879             :   }
     880      629310 :   N = lg(x)-1; m = cgetg((N<<1)+1,t_MAT);
     881      629310 :   for (i=1; i<=N; i++) gel(m,i)   = ZM_ZC_mul(alpha,gel(x,i));
     882      629310 :   for (i=1; i<=N; i++) gel(m,i+N) = ZC_Z_mul(gel(x,i), a);
     883      629310 :   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      311293 : idealHNF_mul(GEN nf, GEN x, GEN y)
     890             : {
     891             :   GEN z;
     892      311293 :   if (typ(y) == t_VEC)
     893      213305 :     z = idealHNF_mul_two(nf,x,y);
     894             :   else
     895             :   { /* reduce one ideal to two-elt form. The smallest */
     896       97988 :     GEN xZ = gcoeff(x,1,1), yZ = gcoeff(y,1,1);
     897       97988 :     if (cmpii(xZ, yZ) < 0)
     898             :     {
     899       34977 :       if (is_pm1(xZ)) return gcopy(y);
     900       24616 :       z = idealHNF_mul_two(nf, y, mat_ideal_two_elt(nf,x));
     901             :     }
     902             :     else
     903             :     {
     904       63011 :       if (is_pm1(yZ)) return gcopy(x);
     905       39057 :       z = idealHNF_mul_two(nf, x, mat_ideal_two_elt(nf,y));
     906             :     }
     907             :   }
     908      276978 :   return z;
     909             : }
     910             : 
     911             : /* operations on elements in factored form */
     912             : 
     913             : GEN
     914       93887 : famat_mul_shallow(GEN f, GEN g)
     915             : {
     916       93887 :   if (typ(f) != t_MAT) f = to_famat_shallow(f,gen_1);
     917       93887 :   if (typ(g) != t_MAT) g = to_famat_shallow(g,gen_1);
     918       93887 :   if (lg(f) == 1) return g;
     919       74362 :   if (lg(g) == 1) return f;
     920      145996 :   return mkmat2(shallowconcat(gel(f,1), gel(g,1)),
     921      145996 :                 shallowconcat(gel(f,2), gel(g,2)));
     922             : }
     923             : GEN
     924       63707 : famat_mulpow_shallow(GEN f, GEN g, GEN e)
     925             : {
     926       63707 :   if (!signe(e)) return f;
     927       61908 :   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      804461 : 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       59889 : append(GEN v, GEN x)
     938             : {
     939       59889 :   long i, l = lg(v);
     940       59889 :   GEN w = cgetg(l+1, typ(v));
     941       59889 :   for (i=1; i<l; i++) gel(w,i) = gcopy(gel(v,i));
     942       59889 :   gel(w,i) = gcopy(x); return w;
     943             : }
     944             : /* add x^1 to famat f */
     945             : static GEN
     946       86819 : famat_add(GEN f, GEN x)
     947             : {
     948       86819 :   GEN h = cgetg(3,t_MAT);
     949       86819 :   if (lg(f) == 1)
     950             :   {
     951       26930 :     gel(h,1) = mkcolcopy(x);
     952       26930 :     gel(h,2) = mkcol(gen_1);
     953             :   }
     954             :   else
     955             :   {
     956       59889 :     gel(h,1) = append(gel(f,1), x);
     957       59889 :     gel(h,2) = gconcat(gel(f,2), gen_1);
     958             :   }
     959       86819 :   return h;
     960             : }
     961             : 
     962             : GEN
     963      111019 : famat_mul(GEN f, GEN g)
     964             : {
     965             :   GEN h;
     966      111019 :   if (typ(g) != t_MAT) {
     967       86819 :     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       24200 :   if (typ(f) != t_MAT) return famat_add(g, f);
     973       24200 :   if (lg(f) == 1) return gcopy(g);
     974        4389 :   if (lg(g) == 1) return gcopy(f);
     975        1897 :   h = cgetg(3,t_MAT);
     976        1897 :   gel(h,1) = gconcat(gel(f,1), gel(g,1));
     977        1897 :   gel(h,2) = gconcat(gel(f,2), gel(g,2));
     978        1897 :   return h;
     979             : }
     980             : 
     981             : GEN
     982       51125 : famat_sqr(GEN f)
     983             : {
     984             :   GEN h;
     985       51125 :   if (lg(f) == 1) return cgetg(1,t_MAT);
     986       25465 :   if (typ(f) != t_MAT) return to_famat(f,gen_2);
     987       25465 :   h = cgetg(3,t_MAT);
     988       25465 :   gel(h,1) = gcopy(gel(f,1));
     989       25465 :   gel(h,2) = gmul2n(gel(f,2),1);
     990       25465 :   return h;
     991             : }
     992             : 
     993             : GEN
     994       27034 : famat_inv_shallow(GEN f)
     995             : {
     996       27034 :   if (lg(f) == 1) return f;
     997       27034 :   if (typ(f) != t_MAT) return to_famat_shallow(f,gen_m1);
     998          14 :   return mkmat2(gel(f,1), ZC_neg(gel(f,2)));
     999             : }
    1000             : GEN
    1001       11441 : famat_inv(GEN f)
    1002             : {
    1003       11441 :   if (lg(f) == 1) return cgetg(1,t_MAT);
    1004        4289 :   if (typ(f) != t_MAT) return to_famat(f,gen_m1);
    1005        4289 :   retmkmat2(gcopy(gel(f,1)), ZC_neg(gel(f,2)));
    1006             : }
    1007             : GEN
    1008        1268 : famat_pow(GEN f, GEN n)
    1009             : {
    1010        1268 :   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       61908 : famat_pow_shallow(GEN f, GEN n)
    1016             : {
    1017       61908 :   if (is_pm1(n)) return signe(n) > 0? f: famat_inv_shallow(f);
    1018       31458 :   if (lg(f) == 1) return f;
    1019       31458 :   if (typ(f) != t_MAT) return to_famat_shallow(f,n);
    1020        1239 :   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       61695 : ext_sqr(GEN nf, GEN x)
    1050       61695 : { return (typ(x)==t_MAT)? famat_sqr(x): nfsqr(nf, x); }
    1051             : static GEN
    1052      145725 : ext_mul(GEN nf, GEN x, GEN y)
    1053      145725 : { return (typ(x)==t_MAT)? famat_mul(x,y): nfmul(nf, x, y); }
    1054             : static GEN
    1055       11301 : ext_inv(GEN nf, GEN x)
    1056       11301 : { return (typ(x)==t_MAT)? famat_inv(x): nfinv(nf, x); }
    1057             : static GEN
    1058        1268 : ext_pow(GEN nf, GEN x, GEN n)
    1059        1268 : { 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       17948 : famat_reduce(GEN fa)
    1078             : {
    1079             :   GEN E, G, L, g, e;
    1080             :   long i, k, l;
    1081             : 
    1082       17948 :   if (lg(fa) == 1) return fa;
    1083       15393 :   g = gel(fa,1); l = lg(g);
    1084       15393 :   e = gel(fa,2);
    1085       15393 :   L = gen_indexsort(g, (void*)&cmp_universal, &cmp_nodata);
    1086       15393 :   G = cgetg(l, t_COL);
    1087       15393 :   E = cgetg(l, t_COL);
    1088             :   /* merge */
    1089       37723 :   for (k=i=1; i<l; i++,k++)
    1090             :   {
    1091       22330 :     gel(G,k) = gel(g,L[i]);
    1092       22330 :     gel(E,k) = gel(e,L[i]);
    1093       22330 :     if (k > 1 && gidentical(gel(G,k), gel(G,k-1)))
    1094             :     {
    1095         763 :       gel(E,k-1) = addii(gel(E,k), gel(E,k-1));
    1096         763 :       k--;
    1097             :     }
    1098             :   }
    1099             :   /* kill 0 exponents */
    1100       15393 :   l = k;
    1101       36960 :   for (k=i=1; i<l; i++)
    1102       21567 :     if (!gequal0(gel(E,i)))
    1103             :     {
    1104       20902 :       gel(G,k) = gel(G,i);
    1105       20902 :       gel(E,k) = gel(E,i); k++;
    1106             :     }
    1107       15393 :   setlg(G, k);
    1108       15393 :   setlg(E, k); return mkmat2(G,E);
    1109             : }
    1110             : 
    1111             : GEN
    1112       12725 : famatsmall_reduce(GEN fa)
    1113             : {
    1114             :   GEN E, G, L, g, e;
    1115             :   long i, k, l;
    1116       12725 :   if (lg(fa) == 1) return fa;
    1117       12725 :   g = gel(fa,1); l = lg(g);
    1118       12725 :   e = gel(fa,2);
    1119       12725 :   L = vecsmall_indexsort(g);
    1120       12725 :   G = cgetg(l, t_VECSMALL);
    1121       12725 :   E = cgetg(l, t_VECSMALL);
    1122             :   /* merge */
    1123      113909 :   for (k=i=1; i<l; i++,k++)
    1124             :   {
    1125      101184 :     G[k] = g[L[i]];
    1126      101184 :     E[k] = e[L[i]];
    1127      101184 :     if (k > 1 && G[k] == G[k-1])
    1128             :     {
    1129        5919 :       E[k-1] += E[k];
    1130        5919 :       k--;
    1131             :     }
    1132             :   }
    1133             :   /* kill 0 exponents */
    1134       12725 :   l = k;
    1135      107990 :   for (k=i=1; i<l; i++)
    1136       95265 :     if (E[i])
    1137             :     {
    1138       92320 :       G[k] = G[i];
    1139       92320 :       E[k] = E[i]; k++;
    1140             :     }
    1141       12725 :   setlg(G, k);
    1142       12725 :   setlg(E, k); return mkmat2(G,E);
    1143             : }
    1144             : 
    1145             : GEN
    1146       55958 : 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       55958 :   if (lg(fa) == 1) return fa;
    1153       55958 :   g = gel(fa,1); l = lg(g);
    1154       55958 :   e = gel(fa,2);
    1155      124208 :   for(n=0, i=1; i<l; i++)
    1156       68250 :     if (cmpii(gel(g,i),limit)<=0) n++;
    1157       55958 :   lG = n<l-1 ? n+2 : n+1;
    1158       55958 :   G = cgetg(lG, t_COL);
    1159       55958 :   E = cgetg(lG, t_COL);
    1160       55958 :   av = avma;
    1161      124208 :   for (i=1, k=1, r = gen_1; i<l; i++)
    1162             :   {
    1163       68250 :     if (cmpii(gel(g,i),limit)<=0)
    1164             :     {
    1165       68166 :       gel(G,k) = gel(g,i);
    1166       68166 :       gel(E,k) = gel(e,i);
    1167       68166 :       k++;
    1168          84 :     } else r = mulii(r, powii(gel(g,i), gel(e,i)));
    1169             :   }
    1170       55958 :   if (k<i)
    1171             :   {
    1172          84 :     gel(G, k) = gerepileuptoint(av, r);
    1173          84 :     gel(E, k) = gen_1;
    1174             :   }
    1175       55958 :   return mkmat2(G,E);
    1176             : }
    1177             : 
    1178             : /* assume pr has degree 1 and coprime to Q_denom(x) */
    1179             : static GEN
    1180        5108 : to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1181             : {
    1182        5108 :   GEN d, r, p = modpr_get_p(modpr);
    1183        5108 :   x = nf_to_scalar_or_basis(nf,x);
    1184        5108 :   if (typ(x) != t_COL) return Rg_to_Fp(x,p);
    1185        4744 :   x = Q_remove_denom(x, &d);
    1186        4744 :   r = zk_to_Fq(x, modpr);
    1187        4744 :   if (d) r = Fp_div(r, d, p);
    1188        4744 :   return r;
    1189             : }
    1190             : 
    1191             : /* pr coprime to all denominators occurring in x */
    1192             : static GEN
    1193         788 : famat_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1194             : {
    1195         788 :   GEN p = modpr_get_p(modpr);
    1196         788 :   GEN t = NULL, g = gel(x,1), e = gel(x,2), q = subiu(p,1);
    1197         788 :   long i, l = lg(g);
    1198        2426 :   for (i = 1; i < l; i++)
    1199             :   {
    1200        1638 :     GEN n = modii(gel(e,i), q);
    1201        1638 :     if (signe(n))
    1202             :     {
    1203        1638 :       GEN h = to_Fp_coprime(nf, gel(g,i), modpr);
    1204        1638 :       h = Fp_pow(h, n, p);
    1205        1638 :       t = t? Fp_mul(t, h, p): h;
    1206             :     }
    1207             :   }
    1208         788 :   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        4258 : nf_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1214             : {
    1215        8516 :   return typ(x)==t_MAT? famat_to_Fp_coprime(nf, x, modpr)
    1216        4258 :                       : to_Fp_coprime(nf, x, modpr);
    1217             : }
    1218             : 
    1219             : static long
    1220      136487 : zk_pvalrem(GEN x, GEN p, GEN *py)
    1221      136487 : { 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      264118 : nf_remove_denom_p(GEN nf, GEN x, GEN p, GEN *pdx, long *pv)
    1226             : {
    1227             :   long vcx;
    1228             :   GEN dx;
    1229      264118 :   x = nf_to_scalar_or_basis(nf, x);
    1230      264118 :   x = Q_remove_denom(x, &dx);
    1231      264118 :   if (dx)
    1232             :   {
    1233      170968 :     vcx = - Z_pvalrem(dx, p, &dx);
    1234      170968 :     if (!vcx) vcx = zk_pvalrem(x, p, &x);
    1235      170968 :     if (isint1(dx)) dx = NULL;
    1236             :   }
    1237             :   else
    1238             :   {
    1239       93150 :     vcx = zk_pvalrem(x, p, &x);
    1240       93150 :     dx = NULL;
    1241             :   }
    1242      264118 :   *pv = vcx;
    1243      264118 :   *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       62083 : p_makecoprime(GEN pr)
    1249             : {
    1250       62083 :   GEN B = pr_get_tau(pr), b;
    1251             :   long i, e;
    1252             : 
    1253       62083 :   if (typ(B) == t_INT) return NULL;
    1254       61943 :   b = gel(B,1); /* B = multiplication table by b */
    1255       61943 :   e = pr_get_e(pr);
    1256       61943 :   if (e == 1) return b;
    1257             :   /* one could also divide (exactly) by p in each iteration */
    1258       17164 :   for (i = 1; i < e; i++) b = ZM_ZC_mul(B, b);
    1259       17164 :   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      111899 : famat_makecoprime(GEN nf, GEN g, GEN e, GEN pr, GEN prk, GEN EX)
    1275             : {
    1276      111899 :   GEN G, E, t, vp = NULL, p = pr_get_p(pr), prkZ = gcoeff(prk, 1,1);
    1277      111899 :   long i, l = lg(g);
    1278             : 
    1279      111899 :   G = cgetg(l+1, t_VEC);
    1280      111899 :   E = cgetg(l+1, t_VEC); /* l+1: room for "modified p" */
    1281      376017 :   for (i=1; i < l; i++)
    1282             :   {
    1283             :     long vcx;
    1284      264118 :     GEN dx, x = nf_remove_denom_p(nf, gel(g,i), p, &dx, &vcx);
    1285      264118 :     if (vcx) /* = v_p(content(g[i])) */
    1286             :     {
    1287      129290 :       GEN a = mulsi(vcx, gel(e,i));
    1288      129290 :       vp = vp? addii(vp, a): a;
    1289             :     }
    1290             :     /* x integral, content coprime to p; dx coprime to p */
    1291      264118 :     if (typ(x) == t_INT)
    1292             :     { /* x coprime to p, hence to pr */
    1293       38847 :       x = modii(x, prkZ);
    1294       38847 :       if (dx) x = Fp_div(x, dx, prkZ);
    1295             :     }
    1296             :     else
    1297             :     {
    1298      225271 :       (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
    1299      225271 :       x = ZC_hnfrem(FpC_red(x,prkZ), prk);
    1300      225271 :       if (dx) x = FpC_Fp_mul(x, Fp_inv(dx,prkZ), prkZ);
    1301             :     }
    1302      264118 :     gel(G,i) = x;
    1303      264118 :     gel(E,i) = gel(e,i);
    1304             :   }
    1305             : 
    1306      111899 :   t = vp? p_makecoprime(pr): NULL;
    1307      111899 :   if (!t)
    1308             :   { /* no need for extra generator */
    1309       49956 :     setlg(G,l);
    1310       49956 :     setlg(E,l);
    1311             :   }
    1312             :   else
    1313             :   {
    1314       61943 :     gel(G,i) = FpC_red(t, prkZ);
    1315       61943 :     gel(E,i) = vp;
    1316             :   }
    1317      111899 :   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       10962 : famat_to_nf_moddivisor(GEN nf, GEN g, GEN e, GEN bid)
    1323             : {
    1324             :   GEN t, cyc;
    1325       10962 :   if (lg(g) == 1) return gen_1;
    1326       10962 :   cyc = bid_get_cyc(bid);
    1327       10962 :   if (lg(cyc) == 1)
    1328           0 :     t = gen_1;
    1329             :   else
    1330       10962 :     t = famat_to_nf_modideal_coprime(nf, g, e, bid_get_ideal(bid), gel(cyc,1));
    1331       10962 :   return set_sign_mod_divisor(nf, mkmat2(g,e), t, bid_get_sarch(bid));
    1332             : }
    1333             : 
    1334             : GEN
    1335      180278 : vecmul(GEN x, GEN y)
    1336             : {
    1337      180278 :   long i,lx, tx = typ(x);
    1338             :   GEN z;
    1339      180278 :   if (is_scalar_t(tx)) return gmul(x,y);
    1340       15939 :   z = cgetg_copy(x, &lx);
    1341       15939 :   for (i=1; i<lx; i++) gel(z,i) = vecmul(gel(x,i), gel(y,i));
    1342       15939 :   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       15729 : vecpow(GEN x, GEN n)
    1358             : {
    1359       15729 :   long i,lx, tx = typ(x);
    1360             :   GEN z;
    1361       15729 :   if (is_scalar_t(tx)) return powgi(x,n);
    1362        4270 :   z = cgetg_copy(x, &lx);
    1363        4270 :   for (i=1; i<lx; i++) gel(z,i) = vecpow(gel(x,i), n);
    1364        4270 :   return z;
    1365             : }
    1366             : 
    1367             : GEN
    1368         903 : vecdiv(GEN x, GEN y)
    1369             : {
    1370         903 :   long i,lx, tx = typ(x);
    1371             :   GEN z;
    1372         903 :   if (is_scalar_t(tx)) return gdiv(x,y);
    1373         301 :   z = cgetg_copy(x, &lx);
    1374         301 :   for (i=1; i<lx; i++) gel(z,i) = vecdiv(gel(x,i), gel(y,i));
    1375         301 :   return z;
    1376             : }
    1377             : 
    1378             : /* A ideal as a square t_MAT */
    1379             : static GEN
    1380      196900 : idealmulelt(GEN nf, GEN x, GEN A)
    1381             : {
    1382             :   long i, lx;
    1383             :   GEN dx, dA, D;
    1384      196900 :   if (lg(A) == 1) return cgetg(1, t_MAT);
    1385      196900 :   x = nf_to_scalar_or_basis(nf,x);
    1386      196900 :   if (typ(x) != t_COL)
    1387       67479 :     return isintzero(x)? cgetg(1,t_MAT): RgM_Rg_mul(A, Q_abs_shallow(x));
    1388      129421 :   x = Q_remove_denom(x, &dx);
    1389      129421 :   A = Q_remove_denom(A, &dA);
    1390      129421 :   x = zk_multable(nf, x);
    1391      129421 :   D = mulii(zkmultable_capZ(x), gcoeff(A,1,1));
    1392      129421 :   x = zkC_multable_mul(A, x);
    1393      129421 :   settyp(x, t_MAT); lx = lg(x);
    1394             :   /* x may contain scalars (at most 1 since the ideal is non-0)*/
    1395      447211 :   for (i=1; i<lx; i++)
    1396      326273 :     if (typ(gel(x,i)) == t_INT)
    1397             :     {
    1398        8483 :       if (i > 1) swap(gel(x,1), gel(x,i)); /* help HNF */
    1399        8483 :       gel(x,1) = scalarcol_shallow(gel(x,1), lx-1);
    1400        8483 :       break;
    1401             :     }
    1402      129421 :   x = ZM_hnfmodid(x, D);
    1403      129421 :   dx = mul_denom(dx,dA);
    1404      129421 :   return dx? gdiv(x,dx): x;
    1405             : }
    1406             : 
    1407             : /* nf a true nf, tx <= ty */
    1408             : static GEN
    1409      641047 : idealmul_aux(GEN nf, GEN x, GEN y, long tx, long ty)
    1410             : {
    1411             :   GEN z, cx, cy;
    1412      641047 :   switch(tx)
    1413             :   {
    1414             :     case id_PRINCIPAL:
    1415      246060 :       switch(ty)
    1416             :       {
    1417             :         case id_PRINCIPAL:
    1418       49034 :           return idealhnf_principal(nf, nfmul(nf,x,y));
    1419             :         case id_PRIME:
    1420             :         {
    1421         126 :           GEN p = pr_get_p(y), pi = pr_get_gen(y), cx;
    1422         126 :           if (pr_is_inert(y)) return RgM_Rg_mul(idealhnf_principal(nf,x),p);
    1423             : 
    1424          42 :           x = nf_to_scalar_or_basis(nf, x);
    1425          42 :           switch(typ(x))
    1426             :           {
    1427             :             case t_INT:
    1428          28 :               if (!signe(x)) return cgetg(1,t_MAT);
    1429          28 :               return ZM_Z_mul(pr_hnf(nf,y), absi_shallow(x));
    1430             :             case t_FRAC:
    1431           7 :               return RgM_Rg_mul(pr_hnf(nf,y), Q_abs_shallow(x));
    1432             :           }
    1433             :           /* t_COL */
    1434           7 :           x = Q_primitive_part(x, &cx);
    1435           7 :           x = zk_multable(nf, x);
    1436           7 :           z = shallowconcat(ZM_Z_mul(x,p), ZM_ZC_mul(x,pi));
    1437           7 :           z = ZM_hnfmodid(z, mulii(p, zkmultable_capZ(x)));
    1438           7 :           return cx? ZM_Q_mul(z, cx): z;
    1439             :         }
    1440             :         default: /* id_MAT */
    1441      196900 :           return idealmulelt(nf, x,y);
    1442             :       }
    1443             :     case id_PRIME:
    1444      321266 :       if (ty==id_PRIME)
    1445      299220 :       { y = pr_hnf(nf,y); cy = NULL; }
    1446             :       else
    1447       22046 :         y = Q_primitive_part(y, &cy);
    1448      321266 :       y = idealHNF_mul_two(nf,y,x);
    1449      321266 :       return cy? RgM_Rg_mul(y,cy): y;
    1450             : 
    1451             :     default: /* id_MAT */
    1452             :     {
    1453       73721 :       long N = nf_get_degree(nf);
    1454       73721 :       if (lg(x)-1 != N || lg(y)-1 != N) pari_err_DIM("idealmul");
    1455       73707 :       x = Q_primitive_part(x, &cx);
    1456       73707 :       y = Q_primitive_part(y, &cy); cx = mul_content(cx,cy);
    1457       73707 :       y = idealHNF_mul(nf,x,y);
    1458       73707 :       return cx? ZM_Q_mul(y,cx): y;
    1459             :     }
    1460             :   }
    1461             : }
    1462             : 
    1463             : /* output the ideal product ix.iy */
    1464             : GEN
    1465      641047 : idealmul(GEN nf, GEN x, GEN y)
    1466             : {
    1467             :   pari_sp av;
    1468             :   GEN res, ax, ay, z;
    1469      641047 :   long tx = idealtyp(&x,&ax);
    1470      641047 :   long ty = idealtyp(&y,&ay), f;
    1471      641047 :   if (tx>ty) { swap(ax,ay); swap(x,y); lswap(tx,ty); }
    1472      641047 :   f = (ax||ay); res = f? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1473      641047 :   av = avma;
    1474      641047 :   z = gerepileupto(av, idealmul_aux(checknf(nf), x,y, tx,ty));
    1475      641033 :   if (!f) return z;
    1476       42773 :   if (ax && ay)
    1477       41098 :     ax = ext_mul(nf, ax, ay);
    1478             :   else
    1479        1675 :     ax = gcopy(ax? ax: ay);
    1480       42773 :   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       44606 : idealsqrprime(GEN nf, GEN pr, GEN *pc)
    1487             : {
    1488       44606 :   GEN p = pr_get_p(pr), q, gen;
    1489       44606 :   long e = pr_get_e(pr), f = pr_get_f(pr);
    1490             : 
    1491       44606 :   q = (e == 1)? sqri(p): p;
    1492       44606 :   if (e <= 2 && e * f == nf_get_degree(nf))
    1493             :   { /* pr^e = (p) */
    1494        9947 :     *pc = q;
    1495        9947 :     return mkvec2(gen_1,gen_0);
    1496             :   }
    1497       34659 :   gen = nfsqr(nf, pr_get_gen(pr));
    1498       34659 :   gen = FpC_red(gen, q);
    1499       34659 :   *pc = NULL;
    1500       34659 :   return mkvec2(q, gen);
    1501             : }
    1502             : /* cf idealpow_aux */
    1503             : static GEN
    1504       62451 : idealsqr_aux(GEN nf, GEN x, long tx)
    1505             : {
    1506       62451 :   GEN T = nf_get_pol(nf), m, cx, a, alpha;
    1507       62451 :   long N = degpol(T);
    1508       62451 :   switch(tx)
    1509             :   {
    1510             :     case id_PRINCIPAL:
    1511          77 :       return idealhnf_principal(nf, nfsqr(nf,x));
    1512             :     case id_PRIME:
    1513       23495 :       if (pr_is_inert(x)) return scalarmat(sqri(gel(x,1)), N);
    1514       23327 :       x = idealsqrprime(nf, x, &cx);
    1515       23327 :       x = idealhnf_two(nf,x);
    1516       23327 :       return cx? ZM_Z_mul(x, cx): x;
    1517             :     default:
    1518       38879 :       x = Q_primitive_part(x, &cx);
    1519       38879 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    1520       38879 :       alpha = nfsqr(nf,alpha);
    1521       38879 :       m = zk_scalar_or_multable(nf, alpha);
    1522       38879 :       if (typ(m) == t_INT) {
    1523        1484 :         x = gcdii(sqri(a), m);
    1524        1484 :         if (cx) x = gmul(x, gsqr(cx));
    1525        1484 :         x = scalarmat(x, N);
    1526             :       }
    1527             :       else
    1528             :       {
    1529       37395 :         x = ZM_hnfmodid(m, gcdii(sqri(a), zkmultable_capZ(m)));
    1530       37395 :         if (cx) cx = gsqr(cx);
    1531       37395 :         if (cx) x = RgM_Rg_mul(x, cx);
    1532             :       }
    1533       38879 :       return x;
    1534             :   }
    1535             : }
    1536             : GEN
    1537       62451 : idealsqr(GEN nf, GEN x)
    1538             : {
    1539             :   pari_sp av;
    1540             :   GEN res, ax, z;
    1541       62451 :   long tx = idealtyp(&x,&ax);
    1542       62451 :   res = ax? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1543       62451 :   av = avma;
    1544       62451 :   z = gerepileupto(av, idealsqr_aux(checknf(nf), x, tx));
    1545       62451 :   if (!ax) return z;
    1546       61695 :   gel(res,1) = z;
    1547       61695 :   gel(res,2) = ext_sqr(nf, ax); return res;
    1548             : }
    1549             : 
    1550             : /* norm of an ideal */
    1551             : GEN
    1552        6286 : idealnorm(GEN nf, GEN x)
    1553             : {
    1554             :   pari_sp av;
    1555             :   GEN y, T;
    1556             :   long tx;
    1557             : 
    1558        6286 :   switch(idealtyp(&x,&y))
    1559             :   {
    1560         175 :     case id_PRIME: return pr_norm(x);
    1561        4053 :     case id_MAT: return RgM_det_triangular(x);
    1562             :   }
    1563             :   /* id_PRINCIPAL */
    1564        2058 :   nf = checknf(nf); T = nf_get_pol(nf); av = avma;
    1565        2058 :   x = nf_to_scalar_or_alg(nf, x);
    1566        2058 :   x = (typ(x) == t_POL)? RgXQ_norm(x, T): gpowgs(x, degpol(T));
    1567        2058 :   tx = typ(x);
    1568        2058 :   if (tx == t_INT) return gerepileuptoint(av, absi(x));
    1569         532 :   if (tx != t_FRAC) pari_err_TYPE("idealnorm",x);
    1570         532 :   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      192443 : idealHNF_inv_Z(GEN nf, GEN I)
    1580             : {
    1581      192443 :   GEN J, dual, IZ = gcoeff(I,1,1); /* I \cap Z */
    1582      192443 :   if (isint1(IZ)) return matid(lg(I)-1);
    1583      181341 :   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      181341 :   dual = shallowtrans( hnf_divscale(J, gmael(nf,5,6), IZ) );
    1587      181341 :   return ZM_hnfmodid(dual, IZ);
    1588             : }
    1589             : /* I HNF with rational coefficients (denominator d). */
    1590             : GEN
    1591       58090 : idealHNF_inv(GEN nf, GEN I)
    1592             : {
    1593       58090 :   GEN J, IQ = gcoeff(I,1,1); /* I \cap Q; d IQ = dI \cap Z */
    1594       58090 :   J = idealHNF_inv_Z(nf, Q_remove_denom(I, NULL)); /* = (dI)^(-1) * (d IQ) */
    1595       58090 :   return equali1(IQ)? J: RgM_Rg_div(J, IQ);
    1596             : }
    1597             : 
    1598             : /* return p * P^(-1)  [integral] */
    1599             : GEN
    1600       24543 : pr_inv_p(GEN pr)
    1601             : {
    1602       24543 :   if (pr_is_inert(pr)) return matid(pr_get_f(pr));
    1603       24060 :   return ZM_hnfmodid(pr_get_tau(pr), pr_get_p(pr));
    1604             : }
    1605             : GEN
    1606        3675 : pr_inv(GEN pr)
    1607             : {
    1608        3675 :   GEN p = pr_get_p(pr);
    1609        3675 :   if (pr_is_inert(pr)) return scalarmat(ginv(p), pr_get_f(pr));
    1610        3332 :   return RgM_Rg_div(ZM_hnfmodid(pr_get_tau(pr),p), p);
    1611             : }
    1612             : 
    1613             : GEN
    1614       97912 : idealinv(GEN nf, GEN x)
    1615             : {
    1616             :   GEN res, ax;
    1617             :   pari_sp av;
    1618       97912 :   long tx = idealtyp(&x,&ax), N;
    1619             : 
    1620       97912 :   res = ax? cgetg(3,t_VEC): NULL;
    1621       97912 :   nf = checknf(nf); av = avma;
    1622       97912 :   N = nf_get_degree(nf);
    1623       97912 :   switch (tx)
    1624             :   {
    1625             :     case id_MAT:
    1626       52658 :       if (lg(x)-1 != N) pari_err_DIM("idealinv");
    1627       52658 :       x = idealHNF_inv(nf,x); break;
    1628             :     case id_PRINCIPAL:
    1629       42426 :       x = nf_to_scalar_or_basis(nf, x);
    1630       42426 :       if (typ(x) != t_COL)
    1631       42384 :         x = idealhnf_principal(nf,ginv(x));
    1632             :       else
    1633             :       { /* nfinv + idealhnf where we already know (x) \cap Z */
    1634             :         GEN c, d;
    1635          42 :         x = Q_remove_denom(x, &c);
    1636          42 :         x = zk_inv(nf, x);
    1637          42 :         x = Q_remove_denom(x, &d); /* true inverse is c/d * x */
    1638          42 :         if (!d) /* x and x^(-1) integral => x a unit */
    1639           7 :           x = scalarmat_shallow(c? c: gen_1, N);
    1640             :         else
    1641             :         {
    1642          35 :           c = c? gdiv(c,d): ginv(d);
    1643          35 :           x = zk_multable(nf, x);
    1644          35 :           x = ZM_Q_mul(ZM_hnfmodid(x,d), c);
    1645             :         }
    1646             :       }
    1647       42426 :       break;
    1648             :     case id_PRIME:
    1649        2828 :       x = pr_inv(x); break;
    1650             :   }
    1651       97912 :   x = gerepileupto(av,x); if (!ax) return x;
    1652       11301 :   gel(res,1) = x;
    1653       11301 :   gel(res,2) = ext_inv(nf, ax); return res;
    1654             : }
    1655             : 
    1656             : /* write x = A/B, A,B coprime integral ideals */
    1657             : GEN
    1658       37227 : idealnumden(GEN nf, GEN x)
    1659             : {
    1660       37227 :   pari_sp av = avma;
    1661             :   GEN x0, ax, c, d, A, B, J;
    1662       37227 :   long tx = idealtyp(&x,&ax);
    1663       37227 :   nf = checknf(nf);
    1664       37227 :   switch (tx)
    1665             :   {
    1666             :     case id_PRIME:
    1667           7 :       retmkvec2(idealhnf(nf, x), gen_1);
    1668             :     case id_PRINCIPAL:
    1669             :     {
    1670             :       GEN xZ, mx;
    1671        2142 :       x = nf_to_scalar_or_basis(nf, x);
    1672        2142 :       switch(typ(x))
    1673             :       {
    1674          77 :         case t_INT: return gerepilecopy(av, mkvec2(absi(x),gen_1));
    1675          14 :         case t_FRAC:return gerepilecopy(av, mkvec2(absi(gel(x,1)), gel(x,2)));
    1676             :       }
    1677             :       /* t_COL */
    1678        2051 :       x = Q_remove_denom(x, &d);
    1679        2051 :       if (!d) return gerepilecopy(av, mkvec2(idealhnf(nf, x), gen_1));
    1680          21 :       mx = zk_multable(nf, x);
    1681          21 :       xZ = zkmultable_capZ(mx);
    1682          21 :       x = ZM_hnfmodid(mx, xZ); /* principal ideal (x) */
    1683          21 :       x0 = mkvec2(xZ, mx); /* same, for fast multiplication */
    1684          21 :       break;
    1685             :     }
    1686             :     default: /* id_MAT */
    1687             :     {
    1688       35078 :       long n = lg(x)-1;
    1689       35078 :       if (n == 0) return mkvec2(gen_0, gen_1);
    1690       35078 :       if (n != nf_get_degree(nf)) pari_err_DIM("idealnumden");
    1691       35078 :       x0 = x = Q_remove_denom(x, &d);
    1692       35078 :       if (!d) return gerepilecopy(av, mkvec2(x, gen_1));
    1693          14 :       break;
    1694             :     }
    1695             :   }
    1696          35 :   J = hnfmodid(x, d); /* = d/B */
    1697          35 :   c = gcoeff(J,1,1); /* (d/B) \cap Z, divides d */
    1698          35 :   B = idealHNF_inv_Z(nf, J); /* (d/B \cap Z) B/d */
    1699          35 :   if (!equalii(c,d)) B = ZM_Z_mul(B, diviiexact(d,c)); /* = B ! */
    1700          35 :   A = idealHNF_mul(nf, B, x0); /* d * (original x) * B = d A */
    1701          35 :   A = ZM_Z_divexact(A, d); /* = A ! */
    1702          35 :   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       88903 : idealpowprime(GEN nf, GEN pr, GEN n, GEN *pc)
    1709             : {
    1710       88903 :   GEN p = pr_get_p(pr), q, gen;
    1711             : 
    1712       88903 :   *pc = NULL;
    1713       88903 :   if (is_pm1(n)) /* n = 1 special cased for efficiency */
    1714             :   {
    1715       50808 :     q = p;
    1716       50808 :     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       50808 :     if (signe(n) >= 0) gen = pr_get_gen(pr);
    1722             :     else
    1723             :     {
    1724        8155 :       gen = pr_get_tau(pr); /* possibly t_MAT */
    1725        8155 :       *pc = ginv(p);
    1726             :     }
    1727             :   }
    1728       38095 :   else if (equalis(n,2)) return idealsqrprime(nf, pr, pc);
    1729             :   else
    1730             :   {
    1731       16816 :     long e = pr_get_e(pr), f = pr_get_f(pr);
    1732       16816 :     GEN r, m = truedvmdis(n, e, &r);
    1733       16816 :     if (e * f == nf_get_degree(nf))
    1734             :     { /* pr^e = (p) */
    1735        7770 :       if (signe(m)) *pc = powii(p,m);
    1736        7770 :       if (!signe(r)) return mkvec2(gen_1,gen_0);
    1737        3171 :       q = p;
    1738        3171 :       gen = nfpow(nf, pr_get_gen(pr), r);
    1739             :     }
    1740             :     else
    1741             :     {
    1742        9046 :       m = absi(m);
    1743        9046 :       if (signe(r)) m = addiu(m,1);
    1744        9046 :       q = powii(p,m); /* m = ceil(|n|/e) */
    1745        9046 :       if (signe(n) >= 0) gen = nfpow(nf, pr_get_gen(pr), n);
    1746             :       else
    1747             :       {
    1748        2191 :         gen = pr_get_tau(pr);
    1749        2191 :         if (typ(gen) == t_MAT) gen = gel(gen,1);
    1750        2191 :         n = negi(n);
    1751        2191 :         gen = ZC_Z_divexact(nfpow(nf, gen, n), powii(p, subii(n,m)));
    1752        2191 :         *pc = ginv(q);
    1753             :       }
    1754             :     }
    1755       12217 :     gen = FpC_red(gen, q);
    1756             :   }
    1757       63025 :   return mkvec2(q, gen);
    1758             : }
    1759             : 
    1760             : /* x * pr^n. Assume x in HNF or scalar (possibly non-integral) */
    1761             : GEN
    1762       67599 : idealmulpowprime(GEN nf, GEN x, GEN pr, GEN n)
    1763             : {
    1764             :   GEN c, cx, y;
    1765             :   long N;
    1766             : 
    1767       67599 :   nf = checknf(nf);
    1768       67599 :   N = nf_get_degree(nf);
    1769       67599 :   if (!signe(n)) return typ(x) == t_MAT? x: scalarmat_shallow(x, N);
    1770             : 
    1771             :   /* inert, special cased for efficiency */
    1772       67487 :   if (pr_is_inert(pr))
    1773             :   {
    1774        5530 :     GEN q = powii(pr_get_p(pr), n);
    1775        5530 :     return typ(x) == t_MAT? RgM_Rg_mul(x,q): scalarmat_shallow(gmul(x,q), N);
    1776             :   }
    1777             : 
    1778       61957 :   y = idealpowprime(nf, pr, n, &c);
    1779       61957 :   if (typ(x) == t_MAT)
    1780       60634 :   { x = Q_primitive_part(x, &cx); if (is_pm1(gcoeff(x,1,1))) x = NULL; }
    1781             :   else
    1782        1323 :   { cx = x; x = NULL; }
    1783       61957 :   cx = mul_content(c,cx);
    1784       61957 :   if (x)
    1785       34048 :     x = idealHNF_mul_two(nf,x,y);
    1786             :   else
    1787       27909 :     x = idealhnf_two(nf,y);
    1788       61957 :   if (cx) x = RgM_Rg_mul(x,cx);
    1789       61957 :   return x;
    1790             : }
    1791             : GEN
    1792       13475 : idealdivpowprime(GEN nf, GEN x, GEN pr, GEN n)
    1793             : {
    1794       13475 :   return idealmulpowprime(nf,x,pr, negi(n));
    1795             : }
    1796             : 
    1797             : /* nf = true nf */
    1798             : static GEN
    1799      178802 : idealpow_aux(GEN nf, GEN x, long tx, GEN n)
    1800             : {
    1801      178802 :   GEN T = nf_get_pol(nf), m, cx, n1, a, alpha;
    1802      178802 :   long N = degpol(T), s = signe(n);
    1803      178802 :   if (!s) return matid(N);
    1804      173166 :   switch(tx)
    1805             :   {
    1806             :     case id_PRINCIPAL:
    1807           0 :       return idealhnf_principal(nf, nfpow(nf,x,n));
    1808             :     case id_PRIME:
    1809       70948 :       if (pr_is_inert(x)) return scalarmat(powii(gel(x,1), n), N);
    1810       26946 :       x = idealpowprime(nf, x, n, &cx);
    1811       26946 :       x = idealhnf_two(nf,x);
    1812       26946 :       return cx? RgM_Rg_mul(x, cx): x;
    1813             :     default:
    1814      102218 :       if (is_pm1(n)) return (s < 0)? idealinv(nf, x): gcopy(x);
    1815       55785 :       n1 = (s < 0)? negi(n): n;
    1816             : 
    1817       55785 :       x = Q_primitive_part(x, &cx);
    1818       55785 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    1819       55785 :       alpha = nfpow(nf,alpha,n1);
    1820       55785 :       m = zk_scalar_or_multable(nf, alpha);
    1821       55785 :       if (typ(m) == t_INT) {
    1822         189 :         x = gcdii(powii(a,n1), m);
    1823         189 :         if (s<0) x = ginv(x);
    1824         189 :         if (cx) x = gmul(x, powgi(cx,n));
    1825         189 :         x = scalarmat(x, N);
    1826             :       }
    1827             :       else
    1828             :       {
    1829       55596 :         x = ZM_hnfmodid(m, gcdii(powii(a,n1), zkmultable_capZ(m)));
    1830       55596 :         if (cx) cx = powgi(cx,n);
    1831       55596 :         if (s<0) {
    1832           7 :           GEN xZ = gcoeff(x,1,1);
    1833           7 :           cx = cx ? gdiv(cx, xZ): ginv(xZ);
    1834           7 :           x = idealHNF_inv_Z(nf,x);
    1835             :         }
    1836       55596 :         if (cx) x = RgM_Rg_mul(x, cx);
    1837             :       }
    1838       55785 :       return x;
    1839             :   }
    1840             : }
    1841             : 
    1842             : /* raise the ideal x to the power n (in Z) */
    1843             : GEN
    1844      178802 : idealpow(GEN nf, GEN x, GEN n)
    1845             : {
    1846             :   pari_sp av;
    1847             :   long tx;
    1848             :   GEN res, ax;
    1849             : 
    1850      178802 :   if (typ(n) != t_INT) pari_err_TYPE("idealpow",n);
    1851      178802 :   tx = idealtyp(&x,&ax);
    1852      178802 :   res = ax? cgetg(3,t_VEC): NULL;
    1853      178802 :   av = avma;
    1854      178802 :   x = gerepileupto(av, idealpow_aux(checknf(nf), x, tx, n));
    1855      178802 :   if (!ax) return x;
    1856        1268 :   ax = ext_pow(nf, ax, n);
    1857        1268 :   gel(res,1) = x;
    1858        1268 :   gel(res,2) = ax;
    1859        1268 :   return res;
    1860             : }
    1861             : 
    1862             : /* Return ideal^e in number field nf. e is a C integer. */
    1863             : GEN
    1864       21196 : idealpows(GEN nf, GEN ideal, long e)
    1865             : {
    1866       21196 :   long court[] = {evaltyp(t_INT) | _evallg(3),0,0};
    1867       21196 :   affsi(e,court); return idealpow(nf,ideal,court);
    1868             : }
    1869             : 
    1870             : static GEN
    1871       42822 : _idealmulred(GEN nf, GEN x, GEN y)
    1872       42822 : { return idealred(nf,idealmul(nf,x,y)); }
    1873             : static GEN
    1874       61751 : _idealsqrred(GEN nf, GEN x)
    1875       61751 : { return idealred(nf,idealsqr(nf,x)); }
    1876             : static GEN
    1877       26382 : _mul(void *data, GEN x, GEN y) { return _idealmulred((GEN)data,x,y); }
    1878             : static GEN
    1879       61751 : _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       59400 : idealpowred(GEN nf, GEN x, GEN n)
    1884             : {
    1885       59400 :   pari_sp av = avma;
    1886             :   long s;
    1887             :   GEN y;
    1888             : 
    1889       59400 :   if (typ(n) != t_INT) pari_err_TYPE("idealpowred",n);
    1890       59400 :   s = signe(n); if (s == 0) return idealpow(nf,x,n);
    1891       58132 :   y = gen_pow(x, n, (void*)nf, &_sqr, &_mul);
    1892             : 
    1893       58132 :   if (s < 0) y = idealinv(nf,y);
    1894       58132 :   if (s < 0 || is_pm1(n)) y = idealred(nf,y);
    1895       58132 :   return gerepileupto(av,y);
    1896             : }
    1897             : 
    1898             : GEN
    1899       16440 : idealmulred(GEN nf, GEN x, GEN y)
    1900             : {
    1901       16440 :   pari_sp av = avma;
    1902       16440 :   return gerepileupto(av, _idealmulred(nf,x,y));
    1903             : }
    1904             : 
    1905             : long
    1906          91 : isideal(GEN nf,GEN x)
    1907             : {
    1908          91 :   long N, i, j, lx, tx = typ(x);
    1909             :   pari_sp av;
    1910             :   GEN T, xZ;
    1911             : 
    1912          91 :   nf = checknf(nf); T = nf_get_pol(nf); lx = lg(x);
    1913          91 :   if (tx==t_VEC && lx==3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
    1914          91 :   switch(tx)
    1915             :   {
    1916          14 :     case t_INT: case t_FRAC: return 1;
    1917           7 :     case t_POL: return varn(x) == varn(T);
    1918           7 :     case t_POLMOD: return RgX_equal_var(T, gel(x,1));
    1919          14 :     case t_VEC: return get_prid(x)? 1 : 0;
    1920          42 :     case t_MAT: break;
    1921           7 :     default: return 0;
    1922             :   }
    1923          42 :   N = degpol(T);
    1924          42 :   if (lx-1 != N) return (lx == 1);
    1925          28 :   if (nbrows(x) != N) return 0;
    1926             : 
    1927          28 :   av = avma; x = Q_primpart(x);
    1928          28 :   if (!ZM_ishnf(x)) return 0;
    1929          14 :   xZ = gcoeff(x,1,1);
    1930          21 :   for (j=2; j<=N; j++)
    1931          14 :     if (!dvdii(xZ, gcoeff(x,j,j))) { avma = av; return 0; }
    1932          14 :   for (i=2; i<=N; i++)
    1933          14 :     for (j=2; j<=N; j++)
    1934           7 :       if (! hnf_invimage(x, zk_ei_mul(nf,gel(x,i),j))) { avma = av; return 0; }
    1935           7 :   avma=av; return 1;
    1936             : }
    1937             : 
    1938             : GEN
    1939       20797 : idealdiv(GEN nf, GEN x, GEN y)
    1940             : {
    1941       20797 :   pari_sp av = avma, tetpil;
    1942       20797 :   GEN z = idealinv(nf,y);
    1943       20797 :   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           7 : err_divexact(GEN x, GEN y)
    1967           7 : { pari_err_DOMAIN("idealdivexact","denominator(x/y)", "!=",
    1968           0 :                   gen_1,mkvec2(x,y)); }
    1969             : GEN
    1970         763 : idealdivexact(GEN nf, GEN x0, GEN y0)
    1971             : {
    1972         763 :   pari_sp av = avma;
    1973             :   GEN x, y, yZ, Nx, Ny, Nz, cy, q, r;
    1974             : 
    1975         763 :   nf = checknf(nf);
    1976         763 :   x = idealhnf_shallow(nf, x0);
    1977         763 :   y = idealhnf_shallow(nf, y0);
    1978         763 :   if (lg(y) == 1) pari_err_INV("idealdivexact", y0);
    1979         756 :   if (lg(x) == 1) { avma = av; return cgetg(1, t_MAT); } /* numerator is zero */
    1980         756 :   y = Q_primitive_part(y, &cy);
    1981         756 :   if (cy) x = RgM_Rg_div(x,cy);
    1982         756 :   Nx = idealnorm(nf,x);
    1983         756 :   Ny = idealnorm(nf,y);
    1984         756 :   if (typ(Nx) != t_INT) err_divexact(x,y);
    1985         749 :   q = dvmdii(Nx,Ny, &r);
    1986         749 :   if (signe(r)) err_divexact(x,y);
    1987         749 :   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         525 :   for (Nz = Ny;;) /* q = Nx/Nz */
    1990             :   {
    1991         679 :     GEN p1 = gcdii(Nz, q);
    1992         679 :     if (is_pm1(p1)) break;
    1993         154 :     Nz = diviiexact(Nz,p1);
    1994         154 :     q = mulii(q,p1);
    1995         154 :   }
    1996             :   /* Replace x/y  by  x+(Nx/Nz) / y+(Ny/Nz) */
    1997         525 :   x = ZM_hnfmodid(x, q);
    1998             :   /* y reduced to unit ideal ? */
    1999         525 :   if (Nz == Ny) return gerepileupto(av, x);
    2000             : 
    2001         154 :   y = ZM_hnfmodid(y, diviiexact(Ny,Nz));
    2002         154 :   yZ = gcoeff(y,1,1);
    2003         154 :   y = idealHNF_mul(nf,x, idealHNF_inv_Z(nf,y));
    2004         154 :   return gerepileupto(av, RgM_Rg_div(y, yZ));
    2005             : }
    2006             : 
    2007             : GEN
    2008          21 : idealintersect(GEN nf, GEN x, GEN y)
    2009             : {
    2010          21 :   pari_sp av = avma;
    2011             :   long lz, lx, i;
    2012             :   GEN z, dx, dy, xZ, yZ;;
    2013             : 
    2014          21 :   nf = checknf(nf);
    2015          21 :   x = idealhnf_shallow(nf,x);
    2016          21 :   y = idealhnf_shallow(nf,y);
    2017          21 :   if (lg(x) == 1 || lg(y) == 1) { avma = av; return cgetg(1,t_MAT); }
    2018          14 :   x = Q_remove_denom(x, &dx);
    2019          14 :   y = Q_remove_denom(y, &dy);
    2020          14 :   if (dx) y = ZM_Z_mul(y, dx);
    2021          14 :   if (dy) x = ZM_Z_mul(x, dy);
    2022          14 :   xZ = gcoeff(x,1,1);
    2023          14 :   yZ = gcoeff(y,1,1);
    2024          14 :   dx = mul_denom(dx,dy);
    2025          14 :   z = ZM_lll(shallowconcat(x,y), 0.99, LLL_KER); lz = lg(z);
    2026          14 :   lx = lg(x);
    2027          14 :   for (i=1; i<lz; i++) setlg(z[i], lx);
    2028          14 :   z = ZM_hnfmodid(ZM_mul(x,z), lcmii(xZ, yZ));
    2029          14 :   if (dx) z = RgM_Rg_div(z,dx);
    2030          14 :   return gerepileupto(av,z);
    2031             : }
    2032             : 
    2033             : /*******************************************************************/
    2034             : /*                                                                 */
    2035             : /*                      T2-IDEAL REDUCTION                         */
    2036             : /*                                                                 */
    2037             : /*******************************************************************/
    2038             : 
    2039             : static GEN
    2040          21 : chk_vdir(GEN nf, GEN vdir)
    2041             : {
    2042          21 :   long i, l = lg(vdir);
    2043             :   GEN v;
    2044          21 :   if (l != lg(nf_get_roots(nf))) pari_err_DIM("idealred");
    2045          14 :   switch(typ(vdir))
    2046             :   {
    2047           0 :     case t_VECSMALL: return vdir;
    2048          14 :     case t_VEC: break;
    2049           0 :     default: pari_err_TYPE("idealred",vdir);
    2050             :   }
    2051          14 :   v = cgetg(l, t_VECSMALL);
    2052          14 :   for (i = 1; i < l; i++) v[i] = itos(gceil(gel(vdir,i)));
    2053          14 :   return v;
    2054             : }
    2055             : 
    2056             : static void
    2057       26913 : twistG(GEN G, long r1, long i, long v)
    2058             : {
    2059       26913 :   long j, lG = lg(G);
    2060       26913 :   if (i <= r1) {
    2061       23560 :     for (j=1; j<lG; j++) gcoeff(G,i,j) = gmul2n(gcoeff(G,i,j), v);
    2062             :   } else {
    2063        3353 :     long k = (i<<1) - r1;
    2064       17871 :     for (j=1; j<lG; j++)
    2065             :     {
    2066       14518 :       gcoeff(G,k-1,j) = gmul2n(gcoeff(G,k-1,j), v);
    2067       14518 :       gcoeff(G,k  ,j) = gmul2n(gcoeff(G,k  ,j), v);
    2068             :     }
    2069             :   }
    2070       26913 : }
    2071             : 
    2072             : GEN
    2073      165581 : nf_get_Gtwist(GEN nf, GEN vdir)
    2074             : {
    2075             :   long i, l, v, r1;
    2076             :   GEN G;
    2077             : 
    2078      165581 :   if (!vdir) return nf_get_roundG(nf);
    2079        2964 :   if (typ(vdir) == t_MAT)
    2080             :   {
    2081        2943 :     long N = nf_get_degree(nf);
    2082        2943 :     if (lg(vdir) != N+1 || lgcols(vdir) != N+1) pari_err_DIM("idealred");
    2083        2943 :     return vdir;
    2084             :   }
    2085          21 :   vdir = chk_vdir(nf, vdir);
    2086          14 :   G = RgM_shallowcopy(nf_get_G(nf));
    2087          14 :   r1 = nf_get_r1(nf);
    2088          14 :   l = lg(vdir);
    2089          56 :   for (i=1; i<l; i++)
    2090             :   {
    2091          42 :     v = vdir[i]; if (!v) continue;
    2092          42 :     twistG(G, r1, i, v);
    2093             :   }
    2094          14 :   return RM_round_maxrank(G);
    2095             : }
    2096             : GEN
    2097       26871 : nf_get_Gtwist1(GEN nf, long i)
    2098             : {
    2099       26871 :   GEN G = RgM_shallowcopy( nf_get_G(nf) );
    2100       26871 :   long r1 = nf_get_r1(nf);
    2101       26871 :   twistG(G, r1, i, 10);
    2102       26871 :   return RM_round_maxrank(G);
    2103             : }
    2104             : 
    2105             : GEN
    2106       39702 : RM_round_maxrank(GEN G0)
    2107             : {
    2108       39702 :   long e, r = lg(G0)-1;
    2109       39702 :   pari_sp av = avma;
    2110       39702 :   GEN G = G0;
    2111       39702 :   for (e = 4; ; e <<= 1)
    2112             :   {
    2113       39702 :     GEN H = ground(G);
    2114       79404 :     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      165574 : idealred0(GEN nf, GEN I, GEN vdir)
    2122             : {
    2123      165574 :   pari_sp av = avma;
    2124      165574 :   GEN G, aI, IZ, J, y, yZ, my, c1 = NULL;
    2125             :   long N;
    2126             : 
    2127      165574 :   nf = checknf(nf);
    2128      165574 :   N = nf_get_degree(nf);
    2129             :   /* put first for sanity checks, unused when I obviously principal */
    2130      165574 :   G = nf_get_Gtwist(nf, vdir);
    2131      165567 :   switch (idealtyp(&I,&aI))
    2132             :   {
    2133             :     case id_PRIME:
    2134       23318 :       if (pr_is_inert(I)) {
    2135         581 :         if (!aI) { avma = av; return matid(N); }
    2136         581 :         c1 = gel(I,1); I = matid(N);
    2137         581 :         goto END;
    2138             :       }
    2139       22737 :       IZ = pr_get_p(I);
    2140       22737 :       J = pr_inv_p(I);
    2141       22737 :       I = idealhnf_two(nf,I);
    2142       22737 :       break;
    2143             :     case id_MAT:
    2144      142235 :       I = Q_primitive_part(I, &c1);
    2145      142235 :       IZ = gcoeff(I,1,1);
    2146      142235 :       if (is_pm1(IZ))
    2147             :       {
    2148        8078 :         if (!aI) { avma = av; return matid(N); }
    2149        8022 :         goto END;
    2150             :       }
    2151      134157 :       J = idealHNF_inv_Z(nf, I);
    2152      134157 :       break;
    2153             :     default: /* id_PRINCIPAL, silly case */
    2154          14 :       if (gequal0(I)) I = cgetg(1,t_MAT); else { c1 = I; I = matid(N); }
    2155          14 :       if (!aI) return I;
    2156           7 :       goto END;
    2157             :   }
    2158             :   /* now I integral, HNF; and J = (I\cap Z) I^(-1), integral */
    2159      156894 :   y = idealpseudomin(J, G); /* small elt in (I\cap Z)I^(-1), integral */
    2160      156894 :   if (ZV_isscalar(y))
    2161             :   { /* already reduced */
    2162       66250 :     if (!aI) return gerepilecopy(av, I);
    2163       65851 :     goto END;
    2164             :   }
    2165             : 
    2166       90644 :   my = zk_multable(nf, y);
    2167       90644 :   I = ZM_Z_divexact(ZM_mul(my, I), IZ); /* y I / (I\cap Z), integral */
    2168       90644 :   c1 = mul_content(c1, IZ);
    2169       90644 :   my = ZM_gauss(my, col_ei(N,1)); /* y^-1 */
    2170       90644 :   yZ = Q_denom(my); /* (y) \cap Z */
    2171       90644 :   I = hnfmodid(I, yZ);
    2172       90644 :   if (!aI) return gerepileupto(av, I);
    2173       90378 :   c1 = RgC_Rg_mul(my, c1);
    2174             : END:
    2175      164839 :   if (c1) aI = ext_mul(nf, aI,c1);
    2176      164839 :   return gerepilecopy(av, mkvec2(I, aI));
    2177             : }
    2178             : 
    2179             : GEN
    2180           7 : idealmin(GEN nf, GEN x, GEN vdir)
    2181             : {
    2182           7 :   pari_sp av = avma;
    2183             :   GEN y, dx;
    2184           7 :   nf = checknf(nf);
    2185           7 :   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           7 :     case id_MAT: if (lg(x) == 1) return gen_0;
    2190             :   }
    2191           7 :   x = Q_remove_denom(x, &dx);
    2192           7 :   y = idealpseudomin(x, nf_get_Gtwist(nf,vdir));
    2193           7 :   if (dx) y = RgC_Rg_div(y, dx);
    2194           7 :   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      452893 : Z_ppio(GEN a, GEN b)
    2207             : {
    2208      452893 :   GEN x, y, d = gcdii(a,b);
    2209      452893 :   if (is_pm1(d)) return mkvec3(gen_1, gen_1, a);
    2210      344596 :   x = d; y = diviiexact(a,d);
    2211             :   for(;;)
    2212             :   {
    2213      407316 :     GEN g = gcdii(x,y);
    2214      407316 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2215       62720 :     x = mulii(x,g); y = diviiexact(y,g);
    2216       62720 :   }
    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     3069115 : Z_cba_rec(GEN L, GEN a, GEN b)
    2268             : {
    2269             :   GEN g;
    2270     3069115 :   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     3069115 :   if (is_pm1(a)) return b;
    2276     1823640 :   g = gcdii(a,b);
    2277     1823640 :   if (is_pm1(g)) { vectrunc_append(L, a); return b; }
    2278     1362235 :   a = diviiexact(a,g);
    2279     1362235 :   b = diviiexact(b,g);
    2280     1362235 :   return Z_cba_rec(L, Z_cba_rec(L, a, g), b);
    2281             : }
    2282             : GEN
    2283      344645 : Z_cba(GEN a, GEN b)
    2284             : {
    2285      344645 :   GEN L = vectrunc_init(expi(a) + expi(b) + 2);
    2286      344645 :   GEN t = Z_cba_rec(L, a, b);
    2287      344645 :   if (!is_pm1(t)) vectrunc_append(L, t);
    2288      344645 :   return L;
    2289             : }
    2290             : /* P = coprime base, extend it by b; TODO: quadratic for now */
    2291             : GEN
    2292           0 : ZV_cba_extend(GEN P, GEN b)
    2293             : {
    2294           0 :   long i, l = lg(P);
    2295           0 :   GEN w = cgetg(l+1, t_VEC);
    2296           0 :   for (i = 1; i < l; i++)
    2297             :   {
    2298           0 :     GEN v = Z_cba(gel(P,i), b);
    2299           0 :     long nv = lg(v)-1;
    2300           0 :     gel(w,i) = vecslice(v, 1, nv-1); /* those divide P[i] but not b */
    2301           0 :     b = gel(v,nv);
    2302             :   }
    2303           0 :   gel(w,l) = b; return shallowconcat1(w);
    2304             : }
    2305             : GEN
    2306           0 : ZV_cba(GEN v)
    2307             : {
    2308           0 :   long i, l = lg(v);
    2309             :   GEN P;
    2310           0 :   if (l <= 2) return v;
    2311           0 :   P = Z_cba(gel(v,1), gel(v,2));
    2312           0 :   for (i = 3; i < l; i++) P = ZV_cba_extend(P, gel(v,i));
    2313           0 :   return P;
    2314             : }
    2315             : 
    2316             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2317             : GEN
    2318     1112774 : Z_ppo(GEN x, GEN f)
    2319             : {
    2320             :   for (;;)
    2321             :   {
    2322     1112774 :     f = gcdii(x, f); if (is_pm1(f)) break;
    2323      756524 :     x = diviiexact(x, f);
    2324      756524 :   }
    2325      356250 :   return x;
    2326             : }
    2327             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2328             : ulong
    2329    38344635 : u_ppo(ulong x, ulong f)
    2330             : {
    2331             :   for (;;)
    2332             :   {
    2333    38344635 :     f = ugcd(x, f); if (f == 1) break;
    2334     7376670 :     x /= f;
    2335     7376670 :   }
    2336    30967965 :   return x;
    2337             : }
    2338             : 
    2339             : /* x t_INT, f ideal. Write x = x1 x2, sqf(x1) | f, (x2,f) = 1. Return x2 */
    2340             : static GEN
    2341         140 : nf_coprime_part(GEN nf, GEN x, GEN listpr)
    2342             : {
    2343         140 :   long v, j, lp = lg(listpr), N = nf_get_degree(nf);
    2344             :   GEN x1, x2, ex;
    2345             : 
    2346             : #if 0 /*1) via many gcds. Expensive ! */
    2347             :   GEN f = idealprodprime(nf, listpr);
    2348             :   f = ZM_hnfmodid(f, x); /* first gcd is less expensive since x in Z */
    2349             :   x = scalarmat(x, N);
    2350             :   for (;;)
    2351             :   {
    2352             :     if (gequal1(gcoeff(f,1,1))) break;
    2353             :     x = idealdivexact(nf, x, f);
    2354             :     f = ZM_hnfmodid(shallowconcat(f,x), gcoeff(x,1,1)); /* gcd(f,x) */
    2355             :   }
    2356             :   x2 = x;
    2357             : #else /*2) from prime decomposition */
    2358         140 :   x1 = NULL;
    2359         399 :   for (j=1; j<lp; j++)
    2360             :   {
    2361         259 :     GEN pr = gel(listpr,j);
    2362         259 :     v = Z_pval(x, pr_get_p(pr)); if (!v) continue;
    2363             : 
    2364         140 :     ex = muluu(v, pr_get_e(pr)); /* = v_pr(x) > 0 */
    2365         140 :     x1 = x1? idealmulpowprime(nf, x1, pr, ex)
    2366         140 :            : idealpow(nf, pr, ex);
    2367             :   }
    2368         140 :   x = scalarmat(x, N);
    2369         140 :   x2 = x1? idealdivexact(nf, x, x1): x;
    2370             : #endif
    2371         140 :   return x2;
    2372             : }
    2373             : 
    2374             : /* L0 in K^*, assume (L0,f) = 1. Return L integral, L0 = L mod f  */
    2375             : GEN
    2376        5614 : make_integral(GEN nf, GEN L0, GEN f, GEN listpr)
    2377             : {
    2378             :   GEN fZ, t, L, D2, d1, d2, d;
    2379             : 
    2380        5614 :   L = Q_remove_denom(L0, &d);
    2381        5614 :   if (!d) return L0;
    2382             : 
    2383             :   /* L0 = L / d, L integral */
    2384        2163 :   fZ = gcoeff(f,1,1);
    2385        2163 :   if (typ(L) == t_INT) return Fp_mul(L, Fp_inv(d, fZ), fZ);
    2386             :   /* Kill denom part coprime to fZ */
    2387        1932 :   d2 = Z_ppo(d, fZ);
    2388        1932 :   t = Fp_inv(d2, fZ); if (!is_pm1(t)) L = ZC_Z_mul(L,t);
    2389        1932 :   if (equalii(d, d2)) return L;
    2390             : 
    2391         140 :   d1 = diviiexact(d, d2);
    2392             :   /* L0 = (L / d1) mod f. d1 not coprime to f
    2393             :    * write (d1) = D1 D2, D2 minimal, (D2,f) = 1. */
    2394         140 :   D2 = nf_coprime_part(nf, d1, listpr);
    2395         140 :   t = idealaddtoone_i(nf, D2, f); /* in D2, 1 mod f */
    2396         140 :   L = nfmuli(nf,t,L);
    2397             : 
    2398             :   /* if (L0, f) = 1, then L in D1 ==> in D1 D2 = (d1) */
    2399         140 :   return Q_div_to_int(L, d1); /* exact division */
    2400             : }
    2401             : 
    2402             : /* assume L is a list of prime ideals. Return the product */
    2403             : GEN
    2404         126 : idealprodprime(GEN nf, GEN L)
    2405             : {
    2406         126 :   long l = lg(L), i;
    2407             :   GEN z;
    2408         126 :   if (l == 1) return matid(nf_get_degree(nf));
    2409         126 :   z = pr_hnf(nf, gel(L,1));
    2410         126 :   for (i=2; i<l; i++) z = idealHNF_mul_two(nf,z, gel(L,i));
    2411         126 :   return z;
    2412             : }
    2413             : 
    2414             : /* optimize for the frequent case I = nfhnf()[2]: lots of them are 1 */
    2415             : GEN
    2416        1463 : idealprod(GEN nf, GEN I)
    2417             : {
    2418        1463 :   long i, l = lg(I);
    2419             :   GEN z;
    2420        2527 :   for (i = 1; i < l; i++)
    2421        2506 :     if (!equali1(gel(I,i))) break;
    2422        1463 :   if (i == l) return gen_1;
    2423        1442 :   z = gel(I,i);
    2424        1442 :   for (i++; i<l; i++) z = idealmul(nf, z, gel(I,i));
    2425        1442 :   return z;
    2426             : }
    2427             : 
    2428             : /* assume L is a list of prime ideals. Return prod L[i]^e[i] */
    2429             : GEN
    2430        7189 : factorbackprime(GEN nf, GEN L, GEN e)
    2431             : {
    2432        7189 :   long l = lg(L), i;
    2433             :   GEN z;
    2434             : 
    2435        7189 :   if (l == 1) return matid(nf_get_degree(nf));
    2436        7175 :   z = idealpow(nf, gel(L,1), gel(e,1));
    2437       10990 :   for (i=2; i<l; i++)
    2438        3815 :     if (signe(gel(e,i))) z = idealmulpowprime(nf,z, gel(L,i),gel(e,i));
    2439        7175 :   return z;
    2440             : }
    2441             : 
    2442             : /* F in Z, divisible exactly by pr.p. Return F-uniformizer for pr, i.e.
    2443             :  * a t in Z_K such that v_pr(t) = 1 and (t, F/pr) = 1 */
    2444             : GEN
    2445       18130 : pr_uniformizer(GEN pr, GEN F)
    2446             : {
    2447       18130 :   GEN p = pr_get_p(pr), t = pr_get_gen(pr);
    2448       18130 :   if (!equalii(F, p))
    2449             :   {
    2450        7629 :     long e = pr_get_e(pr);
    2451        7629 :     GEN u, v, q = (e == 1)? sqri(p): p;
    2452        7629 :     u = mulii(q, Fp_inv(q, diviiexact(F,p))); /* 1 mod F/p, 0 mod q */
    2453        7629 :     v = subui(1UL, u); /* 0 mod F/p, 1 mod q */
    2454        7629 :     if (pr_is_inert(pr))
    2455           0 :       t = addii(mulii(p, v), u);
    2456             :     else
    2457             :     {
    2458        7629 :       t = ZC_Z_mul(t, v);
    2459        7629 :       gel(t,1) = addii(gel(t,1), u); /* return u + vt */
    2460             :     }
    2461             :   }
    2462       18130 :   return t;
    2463             : }
    2464             : /* L = list of prime ideals, return lcm_i (L[i] \cap \ZM) */
    2465             : GEN
    2466       35043 : prV_lcm_capZ(GEN L)
    2467             : {
    2468       35043 :   long i, r = lg(L);
    2469             :   GEN F;
    2470       35043 :   if (r == 1) return gen_1;
    2471       29688 :   F = pr_get_p(gel(L,1));
    2472       44443 :   for (i = 2; i < r; i++)
    2473             :   {
    2474       14755 :     GEN pr = gel(L,i), p = pr_get_p(pr);
    2475       14755 :     if (!dvdii(F, p)) F = mulii(F,p);
    2476             :   }
    2477       29688 :   return F;
    2478             : }
    2479             : 
    2480             : /* Given a prime ideal factorization with possibly zero or negative
    2481             :  * exponents, gives b such that v_p(b) = v_p(x) for all prime ideals pr | x
    2482             :  * and v_pr(b) >= 0 for all other pr.
    2483             :  * For optimal performance, all [anti-]uniformizers should be precomputed,
    2484             :  * but no support for this yet.
    2485             :  *
    2486             :  * If nored, do not reduce result.
    2487             :  * No garbage collecting */
    2488             : static GEN
    2489       20665 : idealapprfact_i(GEN nf, GEN x, int nored)
    2490             : {
    2491             :   GEN z, d, L, e, e2, F;
    2492             :   long i, r;
    2493             :   int flagden;
    2494             : 
    2495       20665 :   nf = checknf(nf);
    2496       20665 :   L = gel(x,1);
    2497       20665 :   e = gel(x,2);
    2498       20665 :   F = prV_lcm_capZ(L);
    2499       20665 :   flagden = 0;
    2500       20665 :   z = NULL; r = lg(e);
    2501       44108 :   for (i = 1; i < r; i++)
    2502             :   {
    2503       23443 :     long s = signe(gel(e,i));
    2504             :     GEN pi, q;
    2505       23443 :     if (!s) continue;
    2506       15967 :     if (s < 0) flagden = 1;
    2507       15967 :     pi = pr_uniformizer(gel(L,i), F);
    2508       15967 :     q = nfpow(nf, pi, gel(e,i));
    2509       15967 :     z = z? nfmul(nf, z, q): q;
    2510             :   }
    2511       20665 :   if (!z) return gen_1;
    2512       10865 :   if (nored || typ(z) != t_COL) return z;
    2513        2716 :   e2 = cgetg(r, t_VEC);
    2514        2716 :   for (i=1; i<r; i++) gel(e2,i) = addiu(gel(e,i), 1);
    2515        2716 :   x = factorbackprime(nf, L,e2);
    2516        2716 :   if (flagden) /* denominator */
    2517             :   {
    2518        2702 :     z = Q_remove_denom(z, &d);
    2519        2702 :     d = diviiexact(d, Z_ppo(d, F));
    2520        2702 :     x = RgM_Rg_mul(x, d);
    2521             :   }
    2522             :   else
    2523          14 :     d = NULL;
    2524        2716 :   z = ZC_reducemodlll(z, x);
    2525        2716 :   return d? RgC_Rg_div(z,d): z;
    2526             : }
    2527             : 
    2528             : GEN
    2529           0 : idealapprfact(GEN nf, GEN x) {
    2530           0 :   pari_sp av = avma;
    2531           0 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    2532             : }
    2533             : GEN
    2534          14 : idealappr(GEN nf, GEN x) {
    2535          14 :   pari_sp av = avma;
    2536          14 :   if (!is_nf_extfactor(x)) x = idealfactor(nf, x);
    2537          14 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    2538             : }
    2539             : 
    2540             : /* OBSOLETE */
    2541             : GEN
    2542          14 : idealappr0(GEN nf, GEN x, long fl) { (void)fl; return idealappr(nf, x); }
    2543             : 
    2544             : static GEN
    2545          21 : mat_ideal_two_elt2(GEN nf, GEN x, GEN a)
    2546             : {
    2547          21 :   GEN F = idealfactor(nf,a), P = gel(F,1), E = gel(F,2);
    2548          21 :   long i, r = lg(E);
    2549          21 :   for (i=1; i<r; i++) gel(E,i) = stoi( idealval(nf,x,gel(P,i)) );
    2550          21 :   return idealapprfact_i(nf,F,1);
    2551             : }
    2552             : 
    2553             : static void
    2554          14 : not_in_ideal(GEN a) {
    2555          14 :   pari_err_DOMAIN("idealtwoelt2","element mod ideal", "!=", gen_0, a);
    2556           0 : }
    2557             : /* x integral in HNF, a an 'nf' */
    2558             : static int
    2559          28 : in_ideal(GEN x, GEN a)
    2560             : {
    2561          28 :   switch(typ(a))
    2562             :   {
    2563          14 :     case t_INT: return dvdii(a, gcoeff(x,1,1));
    2564           7 :     case t_COL: return RgV_is_ZV(a) && !!hnf_invimage(x, a);
    2565           7 :     default: return 0;
    2566             :   }
    2567             : }
    2568             : 
    2569             : /* Given an integral ideal x and a in x, gives a b such that
    2570             :  * x = aZ_K + bZ_K using the approximation theorem */
    2571             : GEN
    2572          42 : idealtwoelt2(GEN nf, GEN x, GEN a)
    2573             : {
    2574          42 :   pari_sp av = avma;
    2575             :   GEN cx, b;
    2576             : 
    2577          42 :   nf = checknf(nf);
    2578          42 :   a = nf_to_scalar_or_basis(nf, a);
    2579          42 :   x = idealhnf_shallow(nf,x);
    2580          42 :   if (lg(x) == 1)
    2581             :   {
    2582          14 :     if (!isintzero(a)) not_in_ideal(a);
    2583           7 :     avma = av; return gen_0;
    2584             :   }
    2585          28 :   x = Q_primitive_part(x, &cx);
    2586          28 :   if (cx) a = gdiv(a, cx);
    2587          28 :   if (!in_ideal(x, a)) not_in_ideal(a);
    2588          21 :   b = mat_ideal_two_elt2(nf, x, a);
    2589          21 :   if (typ(b) == t_COL)
    2590             :   {
    2591          14 :     GEN mod = idealhnf_principal(nf,a);
    2592          14 :     b = ZC_hnfrem(b,mod);
    2593          14 :     if (ZV_isscalar(b)) b = gel(b,1);
    2594             :   }
    2595             :   else
    2596             :   {
    2597           7 :     GEN aZ = typ(a) == t_COL? Q_denom(zk_inv(nf,a)): a; /* (a) \cap Z */
    2598           7 :     b = centermodii(b, aZ, shifti(aZ,-1));
    2599             :   }
    2600          21 :   b = cx? gmul(b,cx): gcopy(b);
    2601          21 :   return gerepileupto(av, b);
    2602             : }
    2603             : 
    2604             : /* Given 2 integral ideals x and y in nf, returns a beta in nf such that
    2605             :  * beta * x is an integral ideal coprime to y */
    2606             : GEN
    2607       12495 : idealcoprimefact(GEN nf, GEN x, GEN fy)
    2608             : {
    2609       12495 :   GEN L = gel(fy,1), e;
    2610       12495 :   long i, r = lg(L);
    2611             : 
    2612       12495 :   e = cgetg(r, t_COL);
    2613       12495 :   for (i=1; i<r; i++) gel(e,i) = stoi( -idealval(nf,x,gel(L,i)) );
    2614       12495 :   return idealapprfact_i(nf, mkmat2(L,e), 0);
    2615             : }
    2616             : GEN
    2617          70 : idealcoprime(GEN nf, GEN x, GEN y)
    2618             : {
    2619          70 :   pari_sp av = avma;
    2620          70 :   return gerepileupto(av, idealcoprimefact(nf, x, idealfactor(nf,y)));
    2621             : }
    2622             : 
    2623             : GEN
    2624           7 : nfmulmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    2625             : {
    2626           7 :   pari_sp av = avma;
    2627           7 :   GEN z, p, pr = modpr, T;
    2628             : 
    2629           7 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    2630           0 :   x = nf_to_Fq(nf,x,modpr);
    2631           0 :   y = nf_to_Fq(nf,y,modpr);
    2632           0 :   z = Fq_mul(x,y,T,p);
    2633           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    2634             : }
    2635             : 
    2636             : GEN
    2637           0 : nfdivmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    2638             : {
    2639           0 :   pari_sp av = avma;
    2640           0 :   nf = checknf(nf);
    2641           0 :   return gerepileupto(av, nfreducemodpr(nf, nfdiv(nf,x,y), modpr));
    2642             : }
    2643             : 
    2644             : GEN
    2645           0 : nfpowmodpr(GEN nf, GEN x, GEN k, GEN modpr)
    2646             : {
    2647           0 :   pari_sp av=avma;
    2648           0 :   GEN z, T, p, pr = modpr;
    2649             : 
    2650           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    2651           0 :   z = nf_to_Fq(nf,x,modpr);
    2652           0 :   z = Fq_pow(z,k,T,p);
    2653           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    2654             : }
    2655             : 
    2656             : GEN
    2657           0 : nfkermodpr(GEN nf, GEN x, GEN modpr)
    2658             : {
    2659           0 :   pari_sp av = avma;
    2660           0 :   GEN T, p, pr = modpr;
    2661             : 
    2662           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    2663           0 :   if (typ(x)!=t_MAT) pari_err_TYPE("nfkermodpr",x);
    2664           0 :   x = nfM_to_FqM(x, nf, modpr);
    2665           0 :   return gerepilecopy(av, FqM_to_nfM(FqM_ker(x,T,p), modpr));
    2666             : }
    2667             : 
    2668             : GEN
    2669           0 : nfsolvemodpr(GEN nf, GEN a, GEN b, GEN pr)
    2670             : {
    2671           0 :   const char *f = "nfsolvemodpr";
    2672           0 :   pari_sp av = avma;
    2673             :   GEN T, p, modpr;
    2674             : 
    2675           0 :   nf = checknf(nf);
    2676           0 :   modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    2677           0 :   if (typ(a)!=t_MAT) pari_err_TYPE(f,a);
    2678           0 :   a = nfM_to_FqM(a, nf, modpr);
    2679           0 :   switch(typ(b))
    2680             :   {
    2681             :     case t_MAT:
    2682           0 :       b = nfM_to_FqM(b, nf, modpr);
    2683           0 :       b = FqM_gauss(a,b,T,p);
    2684           0 :       if (!b) pari_err_INV(f,a);
    2685           0 :       a = FqM_to_nfM(b, modpr);
    2686           0 :       break;
    2687             :     case t_COL:
    2688           0 :       b = nfV_to_FqV(b, nf, modpr);
    2689           0 :       b = FqM_FqC_gauss(a,b,T,p);
    2690           0 :       if (!b) pari_err_INV(f,a);
    2691           0 :       a = FqV_to_nfV(b, modpr);
    2692           0 :       break;
    2693           0 :     default: pari_err_TYPE(f,b);
    2694             :   }
    2695           0 :   return gerepilecopy(av, a);
    2696             : }

Generated by: LCOV version 1.11