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 to exceed 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.12.1 lcov report (development 24215-a79de5b25) Lines: 1491 1660 89.8 %
Date: 2019-08-24 05:50:50 Functions: 148 163 90.8 %
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 an ideal and F is either an
      36             :  * algebraic number, or a factorization matrix attached to an algebraic number.
      37             :  * All routines work with either extended ideals or ideals (an omitted F is
      38             :  * assumed to be factor(1)). All ideals are output in HNF form. */
      39             : 
      40             : /* types and conversions */
      41             : 
      42             : long
      43     4916883 : idealtyp(GEN *ideal, GEN *arch)
      44             : {
      45     4916883 :   GEN x = *ideal;
      46     4916883 :   long t,lx,tx = typ(x);
      47             : 
      48     4916883 :   if (tx!=t_VEC || lg(x)!=3) *arch = NULL;
      49             :   else
      50             :   {
      51      255155 :     GEN a = gel(x,2);
      52      255155 :     if (typ(a) == t_MAT && lg(a) != 3)
      53             :     { /* allow [;] */
      54          14 :       if (lg(a) != 1) pari_err_TYPE("idealtyp [extended ideal]",x);
      55           7 :       a = trivial_fact();
      56             :     }
      57      255148 :     *arch = a;
      58      255148 :     x = gel(x,1); tx = typ(x);
      59             :   }
      60     4916876 :   switch(tx)
      61             :   {
      62     1843674 :     case t_MAT: lx = lg(x);
      63     1843674 :       if (lx == 1) { t = id_PRINCIPAL; x = gen_0; break; }
      64     1843609 :       if (lx != lgcols(x)) pari_err_TYPE("idealtyp [non-square t_MAT]",x);
      65     1843602 :       t = id_MAT;
      66     1843602 :       break;
      67             : 
      68     2602269 :     case t_VEC: if (lg(x)!=6) pari_err_TYPE("idealtyp",x);
      69     2602255 :       t = id_PRIME; break;
      70             : 
      71             :     case t_POL: case t_POLMOD: case t_COL:
      72             :     case t_INT: case t_FRAC:
      73      470933 :       t = id_PRINCIPAL; break;
      74             :     default:
      75           0 :       pari_err_TYPE("idealtyp",x);
      76             :       return 0; /*LCOV_EXCL_LINE*/
      77             :   }
      78     4916855 :   *ideal = x; return t;
      79             : }
      80             : 
      81             : /* true nf; v = [a,x,...], a in Z. Return (a,x) */
      82             : GEN
      83      149831 : idealhnf_two(GEN nf, GEN v)
      84             : {
      85      149831 :   GEN p = gel(v,1), pi = gel(v,2), m = zk_scalar_or_multable(nf, pi);
      86      149831 :   if (typ(m) == t_INT) return scalarmat(gcdii(m,p), nf_get_degree(nf));
      87      131764 :   return ZM_hnfmodid(m, p);
      88             : }
      89             : /* true nf */
      90             : GEN
      91     1982399 : pr_hnf(GEN nf, GEN pr)
      92             : {
      93     1982399 :   GEN p = pr_get_p(pr), m;
      94     1982388 :   if (pr_is_inert(pr)) return scalarmat(p, nf_get_degree(nf));
      95     1718142 :   m = zk_scalar_or_multable(nf, pr_get_gen(pr));
      96     1718067 :   return ZM_hnfmodprime(m, p);
      97             : }
      98             : 
      99             : GEN
     100      290218 : idealhnf_principal(GEN nf, GEN x)
     101             : {
     102             :   GEN cx;
     103      290218 :   x = nf_to_scalar_or_basis(nf, x);
     104      290218 :   switch(typ(x))
     105             :   {
     106      163640 :     case t_COL: break;
     107       99748 :     case t_INT:  if (!signe(x)) return cgetg(1,t_MAT);
     108       99328 :       return scalarmat(absi_shallow(x), nf_get_degree(nf));
     109             :     case t_FRAC:
     110       26830 :       return scalarmat(Q_abs_shallow(x), nf_get_degree(nf));
     111           0 :     default: pari_err_TYPE("idealhnf",x);
     112             :   }
     113      163640 :   x = Q_primitive_part(x, &cx);
     114      163640 :   RgV_check_ZV(x, "idealhnf");
     115      163640 :   x = zk_multable(nf, x);
     116      163640 :   x = ZM_hnfmodid(x, zkmultable_capZ(x));
     117      163640 :   return cx? ZM_Q_mul(x,cx): x;
     118             : }
     119             : 
     120             : /* x integral ideal in t_MAT form, nx columns */
     121             : static GEN
     122           7 : vec_mulid(GEN nf, GEN x, long nx, long N)
     123             : {
     124           7 :   GEN m = cgetg(nx*N + 1, t_MAT);
     125             :   long i, j, k;
     126          21 :   for (i=k=1; i<=nx; i++)
     127          14 :     for (j=1; j<=N; j++) gel(m, k++) = zk_ei_mul(nf, gel(x,i),j);
     128           7 :   return m;
     129             : }
     130             : /* true nf */
     131             : GEN
     132      371962 : idealhnf_shallow(GEN nf, GEN x)
     133             : {
     134      371962 :   long tx = typ(x), lx = lg(x), N;
     135             : 
     136             :   /* cannot use idealtyp because here we allow non-square matrices */
     137      371962 :   if (tx == t_VEC && lx == 3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
     138      371962 :   if (tx == t_VEC && lx == 6) return pr_hnf(nf,x); /* PRIME */
     139      256844 :   switch(tx)
     140             :   {
     141             :     case t_MAT:
     142             :     {
     143             :       GEN cx;
     144       70707 :       long nx = lx-1;
     145       70707 :       N = nf_get_degree(nf);
     146       70707 :       if (nx == 0) return cgetg(1, t_MAT);
     147       70686 :       if (nbrows(x) != N) pari_err_TYPE("idealhnf [wrong dimension]",x);
     148       70679 :       if (nx == 1) return idealhnf_principal(nf, gel(x,1));
     149             : 
     150       69083 :       if (nx == N && RgM_is_ZM(x) && ZM_ishnf(x)) return x;
     151       40719 :       x = Q_primitive_part(x, &cx);
     152       40719 :       if (nx < N) x = vec_mulid(nf, x, nx, N);
     153       40719 :       x = ZM_hnfmod(x, ZM_detmult(x));
     154       40719 :       return cx? ZM_Q_mul(x,cx): x;
     155             :     }
     156             :     case t_QFI:
     157             :     case t_QFR:
     158             :     {
     159          14 :       pari_sp av = avma;
     160          14 :       GEN u, D = nf_get_disc(nf), T = nf_get_pol(nf), f = nf_get_index(nf);
     161          14 :       GEN A = gel(x,1), B = gel(x,2);
     162          14 :       N = nf_get_degree(nf);
     163          14 :       if (N != 2)
     164           0 :         pari_err_TYPE("idealhnf [Qfb for non-quadratic fields]", x);
     165          14 :       if (!equalii(qfb_disc(x), D))
     166           7 :         pari_err_DOMAIN("idealhnf [Qfb]", "disc(q)", "!=", D, x);
     167             :       /* x -> A Z + (-B + sqrt(D)) / 2 Z
     168             :          K = Q[t]/T(t), t^2 + ut + v = 0,  u^2 - 4v = Df^2
     169             :          => t = (-u + sqrt(D) f)/2
     170             :          => sqrt(D)/2 = (t + u/2)/f */
     171           7 :       u = gel(T,3);
     172           7 :       B = deg1pol_shallow(ginv(f),
     173             :                           gsub(gdiv(u, shifti(f,1)), gdiv(B,gen_2)),
     174           7 :                           varn(T));
     175           7 :       return gerepileupto(av, idealhnf_two(nf, mkvec2(A,B)));
     176             :     }
     177      186123 :     default: return idealhnf_principal(nf, x); /* PRINCIPAL */
     178             :   }
     179             : }
     180             : GEN
     181        7861 : idealhnf(GEN nf, GEN x)
     182             : {
     183        7861 :   pari_sp av = avma;
     184        7861 :   GEN y = idealhnf_shallow(checknf(nf), x);
     185        7847 :   return (avma == av)? gcopy(y): gerepileupto(av, y);
     186             : }
     187             : 
     188             : /* GP functions */
     189             : 
     190             : GEN
     191          63 : idealtwoelt0(GEN nf, GEN x, GEN a)
     192             : {
     193          63 :   if (!a) return idealtwoelt(nf,x);
     194          42 :   return idealtwoelt2(nf,x,a);
     195             : }
     196             : 
     197             : GEN
     198          42 : idealpow0(GEN nf, GEN x, GEN n, long flag)
     199             : {
     200          42 :   if (flag) return idealpowred(nf,x,n);
     201          35 :   return idealpow(nf,x,n);
     202             : }
     203             : 
     204             : GEN
     205          56 : idealmul0(GEN nf, GEN x, GEN y, long flag)
     206             : {
     207          56 :   if (flag) return idealmulred(nf,x,y);
     208          49 :   return idealmul(nf,x,y);
     209             : }
     210             : 
     211             : GEN
     212          49 : idealdiv0(GEN nf, GEN x, GEN y, long flag)
     213             : {
     214          49 :   switch(flag)
     215             :   {
     216          21 :     case 0: return idealdiv(nf,x,y);
     217          28 :     case 1: return idealdivexact(nf,x,y);
     218           0 :     default: pari_err_FLAG("idealdiv");
     219             :   }
     220             :   return NULL; /* LCOV_EXCL_LINE */
     221             : }
     222             : 
     223             : GEN
     224          70 : idealaddtoone0(GEN nf, GEN arg1, GEN arg2)
     225             : {
     226          70 :   if (!arg2) return idealaddmultoone(nf,arg1);
     227          35 :   return idealaddtoone(nf,arg1,arg2);
     228             : }
     229             : 
     230             : /* b not a scalar */
     231             : static GEN
     232          28 : hnf_Z_ZC(GEN nf, GEN a, GEN b) { return hnfmodid(zk_multable(nf,b), a); }
     233             : /* b not a scalar */
     234             : static GEN
     235          21 : hnf_Z_QC(GEN nf, GEN a, GEN b)
     236             : {
     237             :   GEN db;
     238          21 :   b = Q_remove_denom(b, &db);
     239          21 :   if (db) a = mulii(a, db);
     240          21 :   b = hnf_Z_ZC(nf,a,b);
     241          21 :   return db? RgM_Rg_div(b, db): b;
     242             : }
     243             : /* b not a scalar (not point in trying to optimize for this case) */
     244             : static GEN
     245          28 : hnf_Q_QC(GEN nf, GEN a, GEN b)
     246             : {
     247             :   GEN da, db;
     248          28 :   if (typ(a) == t_INT) return hnf_Z_QC(nf, a, b);
     249           7 :   da = gel(a,2);
     250           7 :   a = gel(a,1);
     251           7 :   b = Q_remove_denom(b, &db);
     252             :   /* write da = d*A, db = d*B, gcd(A,B) = 1
     253             :    * gcd(a/(d A), b/(d B)) = gcd(a B, A b) / A B d = gcd(a B, b) / A B d */
     254           7 :   if (db)
     255             :   {
     256           7 :     GEN d = gcdii(da,db);
     257           7 :     if (!is_pm1(d)) db = diviiexact(db,d); /* B */
     258           7 :     if (!is_pm1(db))
     259             :     {
     260           7 :       a = mulii(a, db); /* a B */
     261           7 :       da = mulii(da, db); /* A B d = lcm(denom(a),denom(b)) */
     262             :     }
     263             :   }
     264           7 :   return RgM_Rg_div(hnf_Z_ZC(nf,a,b), da);
     265             : }
     266             : static GEN
     267           7 : hnf_QC_QC(GEN nf, GEN a, GEN b)
     268             : {
     269             :   GEN da, db, d, x;
     270           7 :   a = Q_remove_denom(a, &da);
     271           7 :   b = Q_remove_denom(b, &db);
     272           7 :   if (da) b = ZC_Z_mul(b, da);
     273           7 :   if (db) a = ZC_Z_mul(a, db);
     274           7 :   d = mul_denom(da, db);
     275           7 :   a = zk_multable(nf,a); da = zkmultable_capZ(a);
     276           7 :   b = zk_multable(nf,b); db = zkmultable_capZ(b);
     277           7 :   x = ZM_hnfmodid(shallowconcat(a,b), gcdii(da,db));
     278           7 :   return d? RgM_Rg_div(x, d): x;
     279             : }
     280             : static GEN
     281          21 : hnf_Q_Q(GEN nf, GEN a, GEN b) {return scalarmat(Q_gcd(a,b), nf_get_degree(nf));}
     282             : GEN
     283         140 : idealhnf0(GEN nf, GEN a, GEN b)
     284             : {
     285             :   long ta, tb;
     286             :   pari_sp av;
     287             :   GEN x;
     288         140 :   if (!b) return idealhnf(nf,a);
     289             : 
     290             :   /* HNF of aZ_K+bZ_K */
     291          63 :   av = avma; nf = checknf(nf);
     292          63 :   a = nf_to_scalar_or_basis(nf,a); ta = typ(a);
     293          63 :   b = nf_to_scalar_or_basis(nf,b); tb = typ(b);
     294          56 :   if (ta == t_COL)
     295          14 :     x = (tb==t_COL)? hnf_QC_QC(nf, a,b): hnf_Q_QC(nf, b,a);
     296             :   else
     297          42 :     x = (tb==t_COL)? hnf_Q_QC(nf, a,b): hnf_Q_Q(nf, a,b);
     298          56 :   return gerepileupto(av, x);
     299             : }
     300             : 
     301             : /*******************************************************************/
     302             : /*                                                                 */
     303             : /*                       TWO-ELEMENT FORM                          */
     304             : /*                                                                 */
     305             : /*******************************************************************/
     306             : static GEN idealapprfact_i(GEN nf, GEN x, int nored);
     307             : 
     308             : static int
     309      145884 : ok_elt(GEN x, GEN xZ, GEN y)
     310             : {
     311      145884 :   pari_sp av = avma;
     312      145884 :   return gc_bool(av, ZM_equal(x, ZM_hnfmodid(y, xZ)));
     313             : }
     314             : 
     315             : static GEN
     316       55435 : addmul_col(GEN a, long s, GEN b)
     317             : {
     318             :   long i,l;
     319       55435 :   if (!s) return a? leafcopy(a): a;
     320       55266 :   if (!a) return gmulsg(s,b);
     321       52132 :   l = lg(a);
     322      268988 :   for (i=1; i<l; i++)
     323      216856 :     if (signe(gel(b,i))) gel(a,i) = addii(gel(a,i), mulsi(s, gel(b,i)));
     324       52132 :   return a;
     325             : }
     326             : 
     327             : /* a <-- a + s * b, all coeffs integers */
     328             : static GEN
     329       24357 : addmul_mat(GEN a, long s, GEN b)
     330             : {
     331             :   long j,l;
     332             :   /* copy otherwise next call corrupts a */
     333       24357 :   if (!s) return a? RgM_shallowcopy(a): a;
     334       22851 :   if (!a) return gmulsg(s,b);
     335       12545 :   l = lg(a);
     336       59964 :   for (j=1; j<l; j++)
     337       47419 :     (void)addmul_col(gel(a,j), s, gel(b,j));
     338       12545 :   return a;
     339             : }
     340             : 
     341             : static GEN
     342       77775 : get_random_a(GEN nf, GEN x, GEN xZ)
     343             : {
     344             :   pari_sp av;
     345       77775 :   long i, lm, l = lg(x);
     346             :   GEN a, z, beta, mul;
     347             : 
     348       77775 :   beta= cgetg(l, t_VEC);
     349       77775 :   mul = cgetg(l, t_VEC); lm = 1; /* = lg(mul) */
     350             :   /* look for a in x such that a O/xZ = x O/xZ */
     351      152434 :   for (i = 2; i < l; i++)
     352             :   {
     353      149300 :     GEN xi = gel(x,i);
     354      149300 :     GEN t = FpM_red(zk_multable(nf,xi), xZ); /* ZM, cannot be a scalar */
     355      149300 :     if (gequal0(t)) continue;
     356      135578 :     if (ok_elt(x,xZ, t)) return xi;
     357       60937 :     gel(beta,lm) = xi;
     358             :     /* mul[i] = { canonical generators for x[i] O/xZ as Z-module } */
     359       60937 :     gel(mul,lm) = t; lm++;
     360             :   }
     361        3134 :   setlg(mul, lm);
     362        3134 :   setlg(beta,lm);
     363        3134 :   z = cgetg(lm, t_VECSMALL);
     364       10313 :   for(av = avma;; set_avma(av))
     365             :   {
     366       41849 :     for (a=NULL,i=1; i<lm; i++)
     367             :     {
     368       24357 :       long t = random_bits(4) - 7; /* in [-7,8] */
     369       24357 :       z[i] = t;
     370       24357 :       a = addmul_mat(a, t, gel(mul,i));
     371             :     }
     372             :     /* a = matrix (NOT HNF) of ideal generated by beta.z in O/xZ */
     373       10313 :     if (a && ok_elt(x,xZ, a)) break;
     374             :   }
     375       11150 :   for (a=NULL,i=1; i<lm; i++)
     376        8016 :     a = addmul_col(a, z[i], gel(beta,i));
     377        3134 :   return a;
     378             : }
     379             : 
     380             : /* x square matrix, assume it is HNF */
     381             : static GEN
     382      188633 : mat_ideal_two_elt(GEN nf, GEN x)
     383             : {
     384             :   GEN y, a, cx, xZ;
     385      188633 :   long N = nf_get_degree(nf);
     386             :   pari_sp av, tetpil;
     387             : 
     388      188633 :   if (lg(x)-1 != N) pari_err_DIM("idealtwoelt");
     389      188619 :   if (N == 2) return mkvec2copy(gcoeff(x,1,1), gel(x,2));
     390             : 
     391       89112 :   y = cgetg(3,t_VEC); av = avma;
     392       89112 :   cx = Q_content(x);
     393       89112 :   xZ = gcoeff(x,1,1);
     394       89112 :   if (gequal(xZ, cx)) /* x = (cx) */
     395             :   {
     396        3472 :     gel(y,1) = cx;
     397        3472 :     gel(y,2) = gen_0; return y;
     398             :   }
     399       85640 :   if (equali1(cx)) cx = NULL;
     400             :   else
     401             :   {
     402        1715 :     x = Q_div_to_int(x, cx);
     403        1715 :     xZ = gcoeff(x,1,1);
     404             :   }
     405       85640 :   if (N < 6)
     406       72970 :     a = get_random_a(nf, x, xZ);
     407             :   else
     408             :   {
     409       12670 :     const long FB[] = { _evallg(15+1) | evaltyp(t_VECSMALL),
     410             :       2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47
     411             :     };
     412       12670 :     GEN P, E, a1 = Z_smoothen(xZ, (GEN)FB, &P, &E);
     413       12670 :     if (!a1) /* factors completely */
     414        7865 :       a = idealapprfact_i(nf, idealfactor(nf,x), 1);
     415        4805 :     else if (lg(P) == 1) /* no small factors */
     416        3276 :       a = get_random_a(nf, x, xZ);
     417             :     else /* general case */
     418             :     {
     419             :       GEN A0, A1, a0, u0, u1, v0, v1, pi0, pi1, t, u;
     420        1529 :       a0 = diviiexact(xZ, a1);
     421        1529 :       A0 = ZM_hnfmodid(x, a0); /* smooth part of x */
     422        1529 :       A1 = ZM_hnfmodid(x, a1); /* cofactor */
     423        1529 :       pi0 = idealapprfact_i(nf, idealfactor(nf,A0), 1);
     424        1529 :       pi1 = get_random_a(nf, A1, a1);
     425        1529 :       (void)bezout(a0, a1, &v0,&v1);
     426        1529 :       u0 = mulii(a0, v0);
     427        1529 :       u1 = mulii(a1, v1);
     428        1529 :       if (typ(pi0) != t_COL) t = addmulii(u0, pi0, u1);
     429             :       else
     430        1529 :       { t = ZC_Z_mul(pi0, u1); gel(t,1) = addii(gel(t,1), u0); }
     431        1529 :       u = ZC_Z_mul(pi1, u0); gel(u,1) = addii(gel(u,1), u1);
     432        1529 :       a = nfmuli(nf, centermod(u, xZ), centermod(t, xZ));
     433             :     }
     434             :   }
     435       85640 :   if (cx)
     436             :   {
     437        1715 :     a = centermod(a, xZ);
     438        1715 :     tetpil = avma;
     439        1715 :     if (typ(cx) == t_INT)
     440             :     {
     441         301 :       gel(y,1) = mulii(xZ, cx);
     442         301 :       gel(y,2) = ZC_Z_mul(a, cx);
     443             :     }
     444             :     else
     445             :     {
     446        1414 :       gel(y,1) = gmul(xZ, cx);
     447        1414 :       gel(y,2) = RgC_Rg_mul(a, cx);
     448             :     }
     449             :   }
     450             :   else
     451             :   {
     452       83925 :     tetpil = avma;
     453       83925 :     gel(y,1) = icopy(xZ);
     454       83925 :     gel(y,2) = centermod(a, xZ);
     455             :   }
     456       85640 :   gerepilecoeffssp(av,tetpil,y+1,2); return y;
     457             : }
     458             : 
     459             : /* Given an ideal x, returns [a,alpha] such that a is in Q,
     460             :  * x = a Z_K + alpha Z_K, alpha in K^*
     461             :  * a = 0 or alpha = 0 are possible, but do not try to determine whether
     462             :  * x is principal. */
     463             : GEN
     464       60092 : idealtwoelt(GEN nf, GEN x)
     465             : {
     466             :   pari_sp av;
     467             :   GEN z;
     468       60092 :   long tx = idealtyp(&x,&z);
     469       60085 :   nf = checknf(nf);
     470       60085 :   if (tx == id_MAT) return mat_ideal_two_elt(nf,x);
     471        2002 :   if (tx == id_PRIME) return mkvec2copy(gel(x,1), gel(x,2));
     472             :   /* id_PRINCIPAL */
     473         931 :   av = avma; x = nf_to_scalar_or_basis(nf, x);
     474        1666 :   return gerepilecopy(av, typ(x)==t_COL? mkvec2(gen_0,x):
     475         826 :                                          mkvec2(Q_abs_shallow(x),gen_0));
     476             : }
     477             : 
     478             : /*******************************************************************/
     479             : /*                                                                 */
     480             : /*                         FACTORIZATION                           */
     481             : /*                                                                 */
     482             : /*******************************************************************/
     483             : /* x integral ideal in HNF, Zval = v_p(x \cap Z) > 0; return v_p(Nx) */
     484             : static long
     485      251424 : idealHNF_norm_pval(GEN x, GEN p, long Zval)
     486             : {
     487      251424 :   long i, v = Zval, l = lg(x);
     488      251424 :   for (i = 2; i < l; i++) v += Z_pval(gcoeff(x,i,i), p);
     489      251424 :   return v;
     490             : }
     491             : 
     492             : /* x integral in HNF, f0 = partial factorization of a multiple of
     493             :  * x[1,1] = x\cap Z */
     494             : GEN
     495       59507 : idealHNF_Z_factor_i(GEN x, GEN f0, GEN *pvN, GEN *pvZ)
     496             : {
     497       59507 :   GEN P, E, vN, vZ, xZ = gcoeff(x,1,1), f = f0? f0: Z_factor(xZ);
     498             :   long i, l;
     499       59507 :   P = gel(f,1); l = lg(P);
     500       59507 :   E = gel(f,2);
     501       59507 :   *pvN = vN = cgetg(l, t_VECSMALL);
     502       59507 :   *pvZ = vZ = cgetg(l, t_VECSMALL);
     503      114854 :   for (i = 1; i < l; i++)
     504             :   {
     505       55347 :     GEN p = gel(P,i);
     506       55347 :     vZ[i] = f0? Z_pval(xZ, p): (long) itou(gel(E,i));
     507       55347 :     vN[i] = idealHNF_norm_pval(x,p, vZ[i]);
     508             :   }
     509       59507 :   return P;
     510             : }
     511             : /* return P, primes dividing Nx and xZ = x\cap Z, set v_p(Nx), v_p(xZ);
     512             :  * x integral in HNF */
     513             : GEN
     514           0 : idealHNF_Z_factor(GEN x, GEN *pvN, GEN *pvZ)
     515           0 : { return idealHNF_Z_factor_i(x, NULL, pvN, pvZ); }
     516             : 
     517             : /* v_P(A)*f(P) <= Nval [e.g. Nval = v_p(Norm A)], Zval = v_p(A \cap Z).
     518             :  * Return v_P(A) */
     519             : static long
     520      274439 : idealHNF_val(GEN A, GEN P, long Nval, long Zval)
     521             : {
     522      274439 :   long f = pr_get_f(P), vmax, v, e, i, j, k, l;
     523             :   GEN mul, B, a, y, r, p, pk, cx, vals;
     524             :   pari_sp av;
     525             : 
     526      274439 :   if (Nval < f) return 0;
     527      274334 :   p = pr_get_p(P);
     528      274334 :   e = pr_get_e(P);
     529             :   /* v_P(A) <= max [ e * v_p(A \cap Z), floor[v_p(Nix) / f ] */
     530      274334 :   vmax = minss(Zval * e, Nval / f);
     531      274334 :   mul = pr_get_tau(P);
     532      274334 :   l = lg(mul);
     533      274334 :   B = cgetg(l,t_MAT);
     534             :   /* B[1] not needed: v_pr(A[1]) = v_pr(A \cap Z) is known already */
     535      274334 :   gel(B,1) = gen_0; /* dummy */
     536      801882 :   for (j = 2; j < l; j++)
     537             :   {
     538      615651 :     GEN x = gel(A,j);
     539      615651 :     gel(B,j) = y = cgetg(l, t_COL);
     540     4876656 :     for (i = 1; i < l; i++)
     541             :     { /* compute a = (x.t0)_i, A in HNF ==> x[j+1..l-1] = 0 */
     542     4349108 :       a = mulii(gel(x,1), gcoeff(mul,i,1));
     543     4349108 :       for (k = 2; k <= j; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     544             :       /* p | a ? */
     545     4349108 :       gel(y,i) = dvmdii(a,p,&r); if (signe(r)) return 0;
     546             :     }
     547             :   }
     548      186231 :   vals = cgetg(l, t_VECSMALL);
     549             :   /* vals[1] not needed */
     550      648155 :   for (j = 2; j < l; j++)
     551             :   {
     552      461924 :     gel(B,j) = Q_primitive_part(gel(B,j), &cx);
     553      461924 :     vals[j] = cx? 1 + e * Q_pval(cx, p): 1;
     554             :   }
     555      186231 :   pk = powiu(p, ceildivuu(vmax, e));
     556      186231 :   av = avma; y = cgetg(l,t_COL);
     557             :   /* can compute mod p^ceil((vmax-v)/e) */
     558      265467 :   for (v = 1; v < vmax; v++)
     559             :   { /* we know v_pr(Bj) >= v for all j */
     560       83574 :     if (e == 1 || (vmax - v) % e == 0) pk = diviiexact(pk, p);
     561      589235 :     for (j = 2; j < l; j++)
     562             :     {
     563      509999 :       GEN x = gel(B,j); if (v < vals[j]) continue;
     564     4907828 :       for (i = 1; i < l; i++)
     565             :       {
     566     4536333 :         pari_sp av2 = avma;
     567     4536333 :         a = mulii(gel(x,1), gcoeff(mul,i,1));
     568     4536333 :         for (k = 2; k < l; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     569             :         /* a = (x.t_0)_i; p | a ? */
     570     4536333 :         a = dvmdii(a,p,&r); if (signe(r)) return v;
     571     4531995 :         if (lgefint(a) > lgefint(pk)) a = remii(a, pk);
     572     4531995 :         gel(y,i) = gerepileuptoint(av2, a);
     573             :       }
     574      371495 :       gel(B,j) = y; y = x;
     575      371495 :       if (gc_needed(av,3))
     576             :       {
     577           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"idealval");
     578           0 :         gerepileall(av,3, &y,&B,&pk);
     579             :       }
     580             :     }
     581             :   }
     582      181893 :   return v;
     583             : }
     584             : /* true nf, x != 0 integral ideal in HNF, cx t_INT or NULL,
     585             :  * FA integer factorization matrix or NULL. Return partial factorization of
     586             :  * cx * x above primes in FA (complete factorization if !FA)*/
     587             : static GEN
     588       59507 : idealHNF_factor_i(GEN nf, GEN x, GEN cx, GEN FA)
     589             : {
     590       59507 :   const long N = lg(x)-1;
     591             :   long i, j, k, l, v;
     592       59507 :   GEN vN, vZ, vP, vE, vp = idealHNF_Z_factor_i(x, FA, &vN,&vZ);
     593             : 
     594       59507 :   l = lg(vp);
     595       59507 :   i = cx? expi(cx)+1: 1;
     596       59507 :   vP = cgetg((l+i-2)*N+1, t_COL);
     597       59507 :   vE = cgetg((l+i-2)*N+1, t_COL);
     598      114854 :   for (i = k = 1; i < l; i++)
     599             :   {
     600       55347 :     GEN L, p = gel(vp,i);
     601       55347 :     long Nval = vN[i], Zval = vZ[i], vc = cx? Z_pvalrem(cx,p,&cx): 0;
     602       55347 :     if (vc)
     603             :     {
     604        4578 :       L = idealprimedec(nf,p);
     605        4578 :       if (is_pm1(cx)) cx = NULL;
     606             :     }
     607             :     else
     608       50769 :       L = idealprimedec_limit_f(nf,p,Nval);
     609      133709 :     for (j = 1; Nval && j < lg(L); j++) /* !Nval => only cx contributes */
     610             :     {
     611       78362 :       GEN P = gel(L,j);
     612       78362 :       pari_sp av = avma;
     613       78362 :       v = idealHNF_val(x, P, Nval, Zval);
     614       78362 :       set_avma(av);
     615       78362 :       Nval -= v*pr_get_f(P);
     616       78362 :       v += vc * pr_get_e(P); if (!v) continue;
     617       60663 :       gel(vP,k) = P;
     618       60663 :       gel(vE,k) = utoipos(v); k++;
     619             :     }
     620       57925 :     if (vc) for (; j<lg(L); j++)
     621             :     {
     622        2578 :       GEN P = gel(L,j);
     623        2578 :       gel(vP,k) = P;
     624        2578 :       gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     625             :     }
     626             :   }
     627       59507 :   if (cx && !FA)
     628             :   { /* complete factorization */
     629       11669 :     GEN f = Z_factor(cx), cP = gel(f,1), cE = gel(f,2);
     630       11669 :     long lc = lg(cP);
     631       24374 :     for (i=1; i<lc; i++)
     632             :     {
     633       12705 :       GEN p = gel(cP,i), L = idealprimedec(nf,p);
     634       12705 :       long vc = itos(gel(cE,i));
     635       28224 :       for (j=1; j<lg(L); j++)
     636             :       {
     637       15519 :         GEN P = gel(L,j);
     638       15519 :         gel(vP,k) = P;
     639       15519 :         gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     640             :       }
     641             :     }
     642             :   }
     643       59507 :   setlg(vP, k);
     644       59507 :   setlg(vE, k); return mkmat2(vP, vE);
     645             : }
     646             : /* true nf, x integral ideal */
     647             : static GEN
     648       58562 : idealHNF_factor(GEN nf, GEN x, ulong lim)
     649             : {
     650       58562 :   GEN cx, F = NULL;
     651       58562 :   if (lim)
     652             :   {
     653             :     GEN P, E;
     654             :     long l;
     655          42 :     F = Z_factor_limit(gcoeff(x,1,1), lim);
     656          42 :     P = gel(F,1); l = lg(P);
     657          42 :     E = gel(F,2);
     658          42 :     if (l > 1 && abscmpiu(gel(P,l-1), lim) >= 0) { setlg(P,l-1); setlg(E,l-1); }
     659             :   }
     660       58562 :   x = Q_primitive_part(x, &cx);
     661       58562 :   return idealHNF_factor_i(nf, x, cx, F);
     662             : }
     663             : /* c * vector(#L,i,L[i].e), assume results fit in ulong */
     664             : static GEN
     665        4193 : prV_e_muls(GEN L, long c)
     666             : {
     667        4193 :   long j, l = lg(L);
     668        4193 :   GEN z = cgetg(l, t_COL);
     669        4193 :   for (j = 1; j < l; j++) gel(z,j) = stoi(c * pr_get_e(gel(L,j)));
     670        4193 :   return z;
     671             : }
     672             : /* true nf, y in Q */
     673             : static GEN
     674        4403 : Q_nffactor(GEN nf, GEN y, ulong lim)
     675             : {
     676             :   GEN f, P, E;
     677             :   long l, i;
     678        4403 :   if (typ(y) == t_INT)
     679             :   {
     680        4375 :     if (!signe(y)) pari_err_DOMAIN("idealfactor", "ideal", "=",gen_0,y);
     681        4361 :     if (is_pm1(y)) return trivial_fact();
     682             :   }
     683        3080 :   y = Q_abs_shallow(y);
     684        3080 :   if (!lim) f = Q_factor(y);
     685             :   else
     686             :   {
     687          35 :     f = Q_factor_limit(y, lim);
     688          35 :     P = gel(f,1); l = lg(P);
     689          35 :     E = gel(f,2);
     690          77 :     for (i = l-1; i > 0; i--)
     691             :     {
     692          63 :       if (abscmpiu(gel(P,i), lim) < 0) break;
     693          42 :       setlg(P,i); setlg(E,i);
     694             :     }
     695             :   }
     696        3080 :   P = gel(f,1); l = lg(P); if (l == 1) return f;
     697        3066 :   E = gel(f,2);
     698        7259 :   for (i = 1; i < l; i++)
     699             :   {
     700        4193 :     gel(P,i) = idealprimedec(nf, gel(P,i));
     701        4193 :     gel(E,i) = prV_e_muls(gel(P,i), itos(gel(E,i)));
     702             :   }
     703        3066 :   settyp(P,t_VEC); P = shallowconcat1(P);
     704        3066 :   settyp(E,t_VEC); E = shallowconcat1(E);
     705        3066 :   gel(f,1) = P; settyp(P, t_COL);
     706        3066 :   gel(f,2) = E; return f;
     707             : }
     708             : 
     709             : GEN
     710         448 : idealfactor_partial(GEN nf, GEN x, GEN L)
     711             : {
     712         448 :   pari_sp av = avma;
     713             :   long i, j, l;
     714             :   GEN P, E;
     715         448 :   if (!L) return idealfactor(nf, x);
     716          28 :   if (typ(L) == t_INT) return idealfactor_limit(nf, x, itou(L));
     717           7 :   P = cgetg_copy(L, &l);
     718          35 :   for (i = 1; i < l; i++)
     719             :   {
     720          28 :     GEN p = gel(L,i);
     721          28 :     gel(P,i) = typ(p) == t_INT? idealprimedec(nf, p): mkvec(p);
     722             :   }
     723           7 :   settyp(P, t_VEC); P = shallowconcat1(P);
     724           7 :   settyp(P, t_COL);
     725           7 :   P = gen_sort_uniq(P, (void*)&cmp_prime_over_p, &cmp_nodata);
     726           7 :   E = cgetg_copy(P, &l);
     727          35 :   for (i = j = 1; i < l; i++)
     728             :   {
     729          28 :     long v = idealval(nf, x, gel(P,i));
     730          28 :     if (v) { gel(P,j) = gel(P,i); gel(E,j) = stoi(v); j++; }
     731             :   }
     732           7 :   setlg(P,j);
     733           7 :   setlg(E,j); return gerepilecopy(av, mkmat2(P, E));
     734             : }
     735             : GEN
     736       63028 : idealfactor_limit(GEN nf, GEN x, ulong lim)
     737             : {
     738       63028 :   pari_sp av = avma;
     739             :   GEN fa, y;
     740       63028 :   long tx = idealtyp(&x,&y);
     741             : 
     742       63028 :   if (tx == id_PRIME)
     743             :   {
     744          70 :     if (lim && abscmpiu(pr_get_p(x), lim) >= 0) return trivial_fact();
     745          63 :     retmkmat2(mkcolcopy(x), mkcol(gen_1));
     746             :   }
     747       62958 :   nf = checknf(nf);
     748       62958 :   if (tx == id_PRINCIPAL)
     749             :   {
     750        8344 :     y = nf_to_scalar_or_basis(nf, x);
     751        8344 :     if (typ(y) != t_COL) return gerepilecopy(av, Q_nffactor(nf, y, lim));
     752             :   }
     753       58555 :   y = idealnumden(nf, x);
     754       58555 :   fa = idealHNF_factor(nf, gel(y,1), lim);
     755       58555 :   if (!isint1(gel(y,2)))
     756           7 :     fa = famat_div_shallow(fa, idealHNF_factor(nf, gel(y,2), lim));
     757       58555 :   fa = gerepilecopy(av, fa);
     758       58555 :   return sort_factor(fa, (void*)&cmp_prime_ideal, &cmp_nodata);
     759             : }
     760             : GEN
     761       62867 : idealfactor(GEN nf, GEN x) { return idealfactor_limit(nf, x, 0); }
     762             : GEN
     763         140 : gpidealfactor(GEN nf, GEN x, GEN lim)
     764             : {
     765         140 :   ulong L = 0;
     766         140 :   if (lim)
     767             :   {
     768          70 :     if (typ(lim) != t_INT || signe(lim) < 0) pari_err_FLAG("idealfactor");
     769          70 :     L = itou(lim);
     770             :   }
     771         140 :   return idealfactor_limit(nf, x, L);
     772             : }
     773             : 
     774             : static GEN
     775         182 : ramified_root(GEN nf, GEN R, GEN A, long n)
     776             : {
     777         182 :   GEN v, P = gel(idealfactor(nf, R), 1);
     778         182 :   long i, l = lg(P);
     779         182 :   v = cgetg(l, t_VECSMALL);
     780         203 :   for (i = 1; i < l; i++)
     781             :   {
     782          21 :     long w = idealval(nf, A, gel(P,i));
     783          21 :     if (w % n) return NULL;
     784          21 :     v[i] = w / n;
     785             :   }
     786         182 :   return idealfactorback(nf, P, v, 0);
     787             : }
     788             : static int
     789           0 : ramified_root_simple(GEN nf, long n, GEN P, GEN v)
     790             : {
     791           0 :   long i, l = lg(v);
     792           0 :   for (i = 1; i < l; i++) if (v[i])
     793             :   {
     794           0 :     GEN vpr = idealprimedec(nf, gel(P,i));
     795           0 :     long lpr = lg(vpr), j;
     796           0 :     for (j = 1; j < lpr; j++)
     797             :     {
     798           0 :       long e = pr_get_e(gel(vpr,j));
     799           0 :       if ((e * v[i]) % n) return 0;
     800             :     }
     801             :   }
     802           0 :   return 1;
     803             : }
     804             : /* true nf; A is assumed to be the n-th power of an integral ideal,
     805             :  * return its n-th root; n > 1 */
     806             : static long
     807         182 : idealsqrtn_int(GEN nf, GEN A, long n, GEN *pB)
     808             : {
     809             :   GEN C, root;
     810             :   long i, l;
     811             : 
     812         182 :   if (typ(A) == t_INT) /* > 0 */
     813             :   {
     814          98 :     GEN P = nf_get_ramified_primes(nf), v, q;
     815          98 :     l = lg(P); v = cgetg(l, t_VECSMALL);
     816          98 :     for (i = 1; i < l; i++) v[i] = Z_pvalrem(A, gel(P,i), &A);
     817          98 :     C = gen_1;
     818          98 :     if (!isint1(A) && !Z_ispowerall(A, n, pB? &C: NULL)) return 0;
     819          98 :     if (!pB) return ramified_root_simple(nf, n, P, v);
     820          98 :     q = factorback2(P, v);
     821          98 :     root = ramified_root(nf, q, q, n);
     822          98 :     if (!root) return 0;
     823          98 :     if (!equali1(C)) root = isint1(root)? C: ZM_Z_mul(root, C);
     824          98 :     *pB = root; return 1;
     825             :   }
     826             :   /* compute valuations at ramified primes */
     827          84 :   root = ramified_root(nf, idealadd(nf, nf_get_diff(nf), A), A, n);
     828             :   /* remove ramified primes */
     829          84 :   if (isint1(root))
     830          70 :     root = matid(nf_get_degree(nf));
     831             :   else
     832          14 :     A = idealdivexact(nf, A, idealpows(nf,root,n));
     833          84 :   A = Q_primitive_part(A, &C);
     834          84 :   if (C)
     835             :   {
     836           0 :     if (!Z_ispowerall(C,n,&C)) return 0;
     837           0 :     if (pB) root = ZM_Z_mul(root, C);
     838             :   }
     839             : 
     840             :   /* compute final n-th root, at most degree(nf)-1 iterations */
     841         154 :   for (i = 0;; i++)
     842          70 :   {
     843         154 :     GEN J, b, a = gcoeff(A,1,1); /* A \cap Z */
     844         154 :     if (is_pm1(a)) break;
     845          84 :     if (!Z_ispowerall(a,n,&b)) return 0;
     846          70 :     J = idealadd(nf, b, A);
     847          70 :     A = idealdivexact(nf, idealpows(nf,J,n), A);
     848          70 :     if (pB) root = odd(i)? idealdivexact(nf, root, J): idealmul(nf, root, J);
     849             :   }
     850         140 :   if (pB) *pB = root;
     851          70 :   return 1;
     852             : }
     853             : 
     854             : /* A is assumed to be the n-th power of an ideal in nf
     855             :  returns its n-th root. */
     856             : long
     857         105 : idealispower(GEN nf, GEN A, long n, GEN *pB)
     858             : {
     859         105 :   pari_sp av = avma;
     860             :   GEN v, N, D;
     861         105 :   nf = checknf(nf);
     862         105 :   if (n <= 0) pari_err_DOMAIN("idealispower", "n", "<=", gen_0, stoi(n));
     863         105 :   if (n == 1) { if (pB) *pB = idealhnf(nf,A); return 1; }
     864          98 :   v = idealnumden(nf,A);
     865          98 :   if (gequal0(gel(v,1))) { set_avma(av); if (pB) *pB = cgetg(1,t_MAT); return 1; }
     866          98 :   if (!idealsqrtn_int(nf, gel(v,1), n, pB? &N: NULL)) return 0;
     867          84 :   if (!idealsqrtn_int(nf, gel(v,2), n, pB? &D: NULL)) return 0;
     868          84 :   if (pB) *pB = gerepileupto(av, idealdiv(nf,N,D)); else set_avma(av);
     869          84 :   return 1;
     870             : }
     871             : 
     872             : /* x t_INT or integral non-0 ideal in HNF */
     873             : static GEN
     874        3906 : idealredmodpower_i(GEN nf, GEN x, ulong k, ulong B)
     875             : {
     876             :   GEN cx, y, U, N, F, Q;
     877             :   long nF;
     878        3906 :   if (typ(x) == t_INT)
     879             :   {
     880        2954 :     if (!signe(x) || is_pm1(x)) return gen_1;
     881         875 :     F = Z_factor_limit(x, B);
     882         875 :     gel(F,2) = gdiventgs(gel(F,2), k);
     883         875 :     return ginv(factorback(F));
     884             :   }
     885         952 :   N = gcoeff(x,1,1); if (is_pm1(N)) return gen_1;
     886         945 :   F = Z_factor_limit(N, B); nF=lg(gel(F,1))-1;
     887         945 :   if (BPSW_psp(gcoeff(F,nF,1))) U = NULL;
     888             :   else
     889             :   {
     890          70 :     GEN M = powii(gcoeff(F,nF,1), gcoeff(F,nF,2));
     891          70 :     y = hnfmodid(x, M); /* coprime part to B! */
     892          70 :     if (!idealispower(nf, y, k, &U)) U = NULL;
     893          70 :     x = hnfmodid(x, diviiexact(N, M));
     894          70 :     setlg(gel(F,1), nF); /* remove last entry (unfactored part) */
     895          70 :     setlg(gel(F,2), nF);
     896             :   }
     897             :   /* x = B-smooth part of initial x */
     898         945 :   x = Q_primitive_part(x, &cx);
     899         945 :   F = idealHNF_factor_i(nf, x, cx, F);
     900         945 :   gel(F,2) = gdiventgs(gel(F,2), k);
     901         945 :   Q = idealfactorback(nf, gel(F,1), gel(F,2), 0);
     902         945 :   if (U) Q = idealmul(nf,Q,U);
     903         945 :   if (typ(Q) == t_INT) return Q;
     904         917 :   y = idealred_elt(nf, idealHNF_inv_Z(nf, Q));
     905         917 :   return gdiv(y, gcoeff(Q,1,1));
     906             : }
     907             : GEN
     908        1960 : idealredmodpower(GEN nf, GEN x, ulong n, ulong B)
     909             : {
     910        1960 :   pari_sp av = avma;
     911             :   GEN a, b;
     912        1960 :   nf = checknf(nf);
     913        1960 :   if (!n) pari_err_DOMAIN("idealredmodpower","n", "=", gen_0, gen_0);
     914        1960 :   x = idealnumden(nf, x);
     915        1960 :   a = gel(x,1);
     916        1960 :   if (isintzero(a)) { set_avma(av); return gen_1; }
     917        1953 :   a = idealredmodpower_i(nf, gel(x,1), n, B);
     918        1953 :   b = idealredmodpower_i(nf, gel(x,2), n, B);
     919        1953 :   if (!isint1(b)) a = nf_to_scalar_or_basis(nf, nfdiv(nf, a, b));
     920        1953 :   return gerepilecopy(av, a);
     921             : }
     922             : 
     923             : /* P prime ideal in idealprimedec format. Return valuation(A) at P */
     924             : long
     925      567891 : idealval(GEN nf, GEN A, GEN P)
     926             : {
     927      567891 :   pari_sp av = avma;
     928             :   GEN a, p, cA;
     929      567891 :   long vcA, v, Zval, tx = idealtyp(&A,&a);
     930             : 
     931      567891 :   if (tx == id_PRINCIPAL) return nfval(nf,A,P);
     932      562809 :   checkprid(P);
     933      562809 :   if (tx == id_PRIME) return pr_equal(P, A)? 1: 0;
     934             :   /* id_MAT */
     935      562781 :   nf = checknf(nf);
     936      562781 :   A = Q_primitive_part(A, &cA);
     937      562781 :   p = pr_get_p(P);
     938      562781 :   vcA = cA? Q_pval(cA,p): 0;
     939      562781 :   if (pr_is_inert(P)) return gc_long(av,vcA);
     940      553016 :   Zval = Z_pval(gcoeff(A,1,1), p);
     941      553016 :   if (!Zval) v = 0;
     942             :   else
     943             :   {
     944      196077 :     long Nval = idealHNF_norm_pval(A, p, Zval);
     945      196077 :     v = idealHNF_val(A, P, Nval, Zval);
     946             :   }
     947      553016 :   return gc_long(av, vcA? v + vcA*pr_get_e(P): v);
     948             : }
     949             : GEN
     950        6587 : gpidealval(GEN nf, GEN ix, GEN P)
     951             : {
     952        6587 :   long v = idealval(nf,ix,P);
     953        6587 :   return v == LONG_MAX? mkoo(): stoi(v);
     954             : }
     955             : 
     956             : /* gcd and generalized Bezout */
     957             : 
     958             : GEN
     959       67739 : idealadd(GEN nf, GEN x, GEN y)
     960             : {
     961       67739 :   pari_sp av = avma;
     962             :   long tx, ty;
     963             :   GEN z, a, dx, dy, dz;
     964             : 
     965       67739 :   tx = idealtyp(&x,&z);
     966       67739 :   ty = idealtyp(&y,&z); nf = checknf(nf);
     967       67739 :   if (tx != id_MAT) x = idealhnf_shallow(nf,x);
     968       67739 :   if (ty != id_MAT) y = idealhnf_shallow(nf,y);
     969       67739 :   if (lg(x) == 1) return gerepilecopy(av,y);
     970       67725 :   if (lg(y) == 1) return gerepilecopy(av,x); /* check for 0 ideal */
     971       67417 :   dx = Q_denom(x);
     972       67417 :   dy = Q_denom(y); dz = lcmii(dx,dy);
     973       67417 :   if (is_pm1(dz)) dz = NULL; else {
     974       12964 :     x = Q_muli_to_int(x, dz);
     975       12964 :     y = Q_muli_to_int(y, dz);
     976             :   }
     977       67417 :   a = gcdii(gcoeff(x,1,1), gcoeff(y,1,1));
     978       67417 :   if (is_pm1(a))
     979             :   {
     980       29175 :     long N = lg(x)-1;
     981       29175 :     if (!dz) { set_avma(av); return matid(N); }
     982        3597 :     return gerepileupto(av, scalarmat(ginv(dz), N));
     983             :   }
     984       38242 :   z = ZM_hnfmodid(shallowconcat(x,y), a);
     985       38242 :   if (dz) z = RgM_Rg_div(z,dz);
     986       38242 :   return gerepileupto(av,z);
     987             : }
     988             : 
     989             : static GEN
     990          28 : trivial_merge(GEN x)
     991          28 : { return (lg(x) == 1 || !is_pm1(gcoeff(x,1,1)))? NULL: gen_1; }
     992             : /* true nf */
     993             : static GEN
     994      164466 : _idealaddtoone(GEN nf, GEN x, GEN y, long red)
     995             : {
     996             :   GEN a;
     997      164466 :   long tx = idealtyp(&x, &a/*junk*/);
     998      164461 :   long ty = idealtyp(&y, &a/*junk*/);
     999             :   long ea;
    1000      164461 :   if (tx != id_MAT) x = idealhnf_shallow(nf, x);
    1001      164477 :   if (ty != id_MAT) y = idealhnf_shallow(nf, y);
    1002      164477 :   if (lg(x) == 1)
    1003          14 :     a = trivial_merge(y);
    1004      164463 :   else if (lg(y) == 1)
    1005          14 :     a = trivial_merge(x);
    1006             :   else
    1007      164449 :     a = hnfmerge_get_1(x, y);
    1008      164481 :   if (!a) pari_err_COPRIME("idealaddtoone",x,y);
    1009      164464 :   if (red && (ea = gexpo(a)) > 10)
    1010             :   {
    1011        6756 :     GEN b = (typ(a) == t_COL)? a: scalarcol_shallow(a, nf_get_degree(nf));
    1012        6756 :     b = ZC_reducemodlll(b, idealHNF_mul(nf,x,y));
    1013        6756 :     if (gexpo(b) < ea) a = b;
    1014             :   }
    1015      164464 :   return a;
    1016             : }
    1017             : /* true nf */
    1018             : GEN
    1019       19634 : idealaddtoone_i(GEN nf, GEN x, GEN y)
    1020       19634 : { return _idealaddtoone(nf, x, y, 1); }
    1021             : /* true nf */
    1022             : GEN
    1023      144830 : idealaddtoone_raw(GEN nf, GEN x, GEN y)
    1024      144830 : { return _idealaddtoone(nf, x, y, 0); }
    1025             : 
    1026             : GEN
    1027          91 : idealaddtoone(GEN nf, GEN x, GEN y)
    1028             : {
    1029          91 :   GEN z = cgetg(3,t_VEC), a;
    1030          91 :   pari_sp av = avma;
    1031          91 :   nf = checknf(nf);
    1032          91 :   a = gerepileupto(av, idealaddtoone_i(nf,x,y));
    1033          77 :   gel(z,1) = a;
    1034          77 :   gel(z,2) = typ(a) == t_COL? Z_ZC_sub(gen_1,a): subui(1,a);
    1035          77 :   return z;
    1036             : }
    1037             : 
    1038             : /* assume elements of list are integral ideals */
    1039             : GEN
    1040          35 : idealaddmultoone(GEN nf, GEN list)
    1041             : {
    1042          35 :   pari_sp av = avma;
    1043          35 :   long N, i, l, nz, tx = typ(list);
    1044             :   GEN H, U, perm, L;
    1045             : 
    1046          35 :   nf = checknf(nf); N = nf_get_degree(nf);
    1047          35 :   if (!is_vec_t(tx)) pari_err_TYPE("idealaddmultoone",list);
    1048          35 :   l = lg(list);
    1049          35 :   L = cgetg(l, t_VEC);
    1050          35 :   if (l == 1)
    1051           0 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
    1052          35 :   nz = 0; /* number of non-zero ideals in L */
    1053          98 :   for (i=1; i<l; i++)
    1054             :   {
    1055          70 :     GEN I = gel(list,i);
    1056          70 :     if (typ(I) != t_MAT) I = idealhnf_shallow(nf,I);
    1057          70 :     if (lg(I) != 1)
    1058             :     {
    1059          42 :       nz++; RgM_check_ZM(I,"idealaddmultoone");
    1060          35 :       if (lgcols(I) != N+1) pari_err_TYPE("idealaddmultoone [not an ideal]", I);
    1061             :     }
    1062          63 :     gel(L,i) = I;
    1063             :   }
    1064          28 :   H = ZM_hnfperm(shallowconcat1(L), &U, &perm);
    1065          28 :   if (lg(H) == 1 || !equali1(gcoeff(H,1,1)))
    1066           7 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
    1067          49 :   for (i=1; i<=N; i++)
    1068          49 :     if (perm[i] == 1) break;
    1069          21 :   U = gel(U,(nz-1)*N + i); /* (L[1]|...|L[nz]) U = 1 */
    1070          21 :   nz = 0;
    1071          63 :   for (i=1; i<l; i++)
    1072             :   {
    1073          42 :     GEN c = gel(L,i);
    1074          42 :     if (lg(c) == 1)
    1075          14 :       c = gen_0;
    1076             :     else {
    1077          28 :       c = ZM_ZC_mul(c, vecslice(U, nz*N + 1, (nz+1)*N));
    1078          28 :       nz++;
    1079             :     }
    1080          42 :     gel(L,i) = c;
    1081             :   }
    1082          21 :   return gerepilecopy(av, L);
    1083             : }
    1084             : 
    1085             : /* multiplication */
    1086             : 
    1087             : /* x integral ideal (without archimedean component) in HNF form
    1088             :  * y = [a,alpha] corresponds to the integral ideal aZ_K+alpha Z_K, a in Z,
    1089             :  * alpha a ZV or a ZM (multiplication table). Multiply them */
    1090             : static GEN
    1091     2033212 : idealHNF_mul_two(GEN nf, GEN x, GEN y)
    1092             : {
    1093     2033212 :   GEN m, a = gel(y,1), alpha = gel(y,2);
    1094             :   long i, N;
    1095             : 
    1096     2033212 :   if (typ(alpha) != t_MAT)
    1097             :   {
    1098     1705119 :     alpha = zk_scalar_or_multable(nf, alpha);
    1099     1705119 :     if (typ(alpha) == t_INT) /* e.g. y inert ? 0 should not (but may) occur */
    1100        4017 :       return signe(a)? ZM_Z_mul(x, gcdii(a, alpha)): cgetg(1,t_MAT);
    1101             :   }
    1102     2029195 :   N = lg(x)-1; m = cgetg((N<<1)+1,t_MAT);
    1103     2029195 :   for (i=1; i<=N; i++) gel(m,i)   = ZM_ZC_mul(alpha,gel(x,i));
    1104     2029195 :   for (i=1; i<=N; i++) gel(m,i+N) = ZC_Z_mul(gel(x,i), a);
    1105     2029195 :   return ZM_hnfmodid(m, mulii(a, gcoeff(x,1,1)));
    1106             : }
    1107             : 
    1108             : /* Assume ix and iy are integral in HNF form [NOT extended]. Not memory clean.
    1109             :  * HACK: ideal in iy can be of the form [a,b], a in Z, b in Z_K */
    1110             : GEN
    1111      990565 : idealHNF_mul(GEN nf, GEN x, GEN y)
    1112             : {
    1113             :   GEN z;
    1114      990565 :   if (typ(y) == t_VEC)
    1115      892330 :     z = idealHNF_mul_two(nf,x,y);
    1116             :   else
    1117             :   { /* reduce one ideal to two-elt form. The smallest */
    1118       98235 :     GEN xZ = gcoeff(x,1,1), yZ = gcoeff(y,1,1);
    1119       98235 :     if (cmpii(xZ, yZ) < 0)
    1120             :     {
    1121       34476 :       if (is_pm1(xZ)) return gcopy(y);
    1122       21826 :       z = idealHNF_mul_two(nf, y, mat_ideal_two_elt(nf,x));
    1123             :     }
    1124             :     else
    1125             :     {
    1126       63759 :       if (is_pm1(yZ)) return gcopy(x);
    1127       32644 :       z = idealHNF_mul_two(nf, x, mat_ideal_two_elt(nf,y));
    1128             :     }
    1129             :   }
    1130      946800 :   return z;
    1131             : }
    1132             : 
    1133             : /* operations on elements in factored form */
    1134             : 
    1135             : GEN
    1136       89375 : famat_mul_shallow(GEN f, GEN g)
    1137             : {
    1138       89375 :   if (typ(f) != t_MAT) f = to_famat_shallow(f,gen_1);
    1139       89375 :   if (typ(g) != t_MAT) g = to_famat_shallow(g,gen_1);
    1140       89375 :   if (lgcols(f) == 1) return g;
    1141       73374 :   if (lgcols(g) == 1) return f;
    1142      146580 :   return mkmat2(shallowconcat(gel(f,1), gel(g,1)),
    1143      146580 :                 shallowconcat(gel(f,2), gel(g,2)));
    1144             : }
    1145             : GEN
    1146       63854 : famat_mulpow_shallow(GEN f, GEN g, GEN e)
    1147             : {
    1148       63854 :   if (!signe(e)) return f;
    1149       63539 :   return famat_mul_shallow(f, famat_pow_shallow(g, e));
    1150             : }
    1151             : 
    1152             : GEN
    1153       12376 : famat_mulpows_shallow(GEN f, GEN g, long e)
    1154             : {
    1155       12376 :   if (e==0) return f;
    1156        7412 :   return famat_mul_shallow(f, famat_pows_shallow(g, e));
    1157             : }
    1158             : 
    1159             : GEN
    1160           7 : famat_div_shallow(GEN f, GEN g)
    1161           7 : { return famat_mul_shallow(f, famat_inv_shallow(g)); }
    1162             : 
    1163             : GEN
    1164           0 : to_famat(GEN x, GEN y) { retmkmat2(mkcolcopy(x), mkcolcopy(y)); }
    1165             : GEN
    1166      896107 : to_famat_shallow(GEN x, GEN y) { return mkmat2(mkcol(x), mkcol(y)); }
    1167             : 
    1168             : /* concat the single elt x; not gconcat since x may be a t_COL */
    1169             : static GEN
    1170       31298 : append(GEN v, GEN x)
    1171             : {
    1172       31298 :   long i, l = lg(v);
    1173       31298 :   GEN w = cgetg(l+1, typ(v));
    1174       31298 :   for (i=1; i<l; i++) gel(w,i) = gcopy(gel(v,i));
    1175       31298 :   gel(w,i) = gcopy(x); return w;
    1176             : }
    1177             : /* add x^1 to famat f */
    1178             : static GEN
    1179       81582 : famat_add(GEN f, GEN x)
    1180             : {
    1181       81582 :   GEN h = cgetg(3,t_MAT);
    1182       81582 :   if (lgcols(f) == 1)
    1183             :   {
    1184       50284 :     gel(h,1) = mkcolcopy(x);
    1185       50284 :     gel(h,2) = mkcol(gen_1);
    1186             :   }
    1187             :   else
    1188             :   {
    1189       31298 :     gel(h,1) = append(gel(f,1), x);
    1190       31298 :     gel(h,2) = gconcat(gel(f,2), gen_1);
    1191             :   }
    1192       81582 :   return h;
    1193             : }
    1194             : 
    1195             : GEN
    1196       88582 : famat_mul(GEN f, GEN g)
    1197             : {
    1198             :   GEN h;
    1199       88582 :   if (typ(g) != t_MAT) {
    1200       81582 :     if (typ(f) == t_MAT) return famat_add(f, g);
    1201           0 :     h = cgetg(3, t_MAT);
    1202           0 :     gel(h,1) = mkcol2(gcopy(f), gcopy(g));
    1203           0 :     gel(h,2) = mkcol2(gen_1, gen_1);
    1204             :   }
    1205        7000 :   if (typ(f) != t_MAT) return famat_add(g, f);
    1206        7000 :   if (lgcols(f) == 1) return gcopy(g);
    1207        5075 :   if (lgcols(g) == 1) return gcopy(f);
    1208        2240 :   h = cgetg(3,t_MAT);
    1209        2240 :   gel(h,1) = gconcat(gel(f,1), gel(g,1));
    1210        2240 :   gel(h,2) = gconcat(gel(f,2), gel(g,2));
    1211        2240 :   return h;
    1212             : }
    1213             : 
    1214             : GEN
    1215       16339 : famat_sqr(GEN f)
    1216             : {
    1217             :   GEN h;
    1218       16339 :   if (typ(f) != t_MAT) return to_famat(f,gen_2);
    1219       16339 :   if (lgcols(f) == 1) return gcopy(f);
    1220       11459 :   h = cgetg(3,t_MAT);
    1221       11459 :   gel(h,1) = gcopy(gel(f,1));
    1222       11459 :   gel(h,2) = gmul2n(gel(f,2),1);
    1223       11459 :   return h;
    1224             : }
    1225             : 
    1226             : GEN
    1227       28672 : famat_inv_shallow(GEN f)
    1228             : {
    1229       28672 :   if (typ(f) != t_MAT) return to_famat_shallow(f,gen_m1);
    1230          70 :   if (lgcols(f) == 1) return f;
    1231          70 :   return mkmat2(gel(f,1), ZC_neg(gel(f,2)));
    1232             : }
    1233             : GEN
    1234       17985 : famat_inv(GEN f)
    1235             : {
    1236       17985 :   if (typ(f) != t_MAT) return to_famat(f,gen_m1);
    1237       17985 :   if (lgcols(f) == 1) return gcopy(f);
    1238        6544 :   retmkmat2(gcopy(gel(f,1)), ZC_neg(gel(f,2)));
    1239             : }
    1240             : GEN
    1241        2031 : famat_pow(GEN f, GEN n)
    1242             : {
    1243        2031 :   if (typ(f) != t_MAT) return to_famat(f,n);
    1244        2031 :   if (lgcols(f) == 1) return gcopy(f);
    1245           0 :   retmkmat2(gcopy(gel(f,1)), ZC_Z_mul(gel(f,2),n));
    1246             : }
    1247             : GEN
    1248       63539 : famat_pow_shallow(GEN f, GEN n)
    1249             : {
    1250       63539 :   if (is_pm1(n)) return signe(n) > 0? f: famat_inv_shallow(f);
    1251       33033 :   if (typ(f) != t_MAT) return to_famat_shallow(f,n);
    1252         231 :   if (lgcols(f) == 1) return f;
    1253         231 :   return mkmat2(gel(f,1), ZC_Z_mul(gel(f,2),n));
    1254             : }
    1255             : 
    1256             : GEN
    1257        7412 : famat_pows_shallow(GEN f, long n)
    1258             : {
    1259        7412 :   if (n==1) return f;
    1260        2710 :   if (n==-1) return famat_inv_shallow(f);
    1261        2311 :   if (typ(f) != t_MAT) return to_famat_shallow(f, stoi(n));
    1262        2129 :   if (lgcols(f) == 1) return f;
    1263        2129 :   return mkmat2(gel(f,1), ZC_z_mul(gel(f,2),n));
    1264             : }
    1265             : 
    1266             : GEN
    1267           0 : famat_Z_gcd(GEN M, GEN n)
    1268             : {
    1269           0 :   pari_sp av=avma;
    1270           0 :   long i, j, l=lgcols(M);
    1271           0 :   GEN F=cgetg(3,t_MAT);
    1272           0 :   gel(F,1)=cgetg(l,t_COL);
    1273           0 :   gel(F,2)=cgetg(l,t_COL);
    1274           0 :   for (i=1, j=1; i<l; i++)
    1275             :   {
    1276           0 :     GEN p = gcoeff(M,i,1);
    1277           0 :     GEN e = gminsg(Z_pval(n,p),gcoeff(M,i,2));
    1278           0 :     if (signe(e))
    1279             :     {
    1280           0 :       gcoeff(F,j,1)=p;
    1281           0 :       gcoeff(F,j,2)=e;
    1282           0 :       j++;
    1283             :     }
    1284             :   }
    1285           0 :   setlg(gel(F,1),j); setlg(gel(F,2),j);
    1286           0 :   return gerepilecopy(av,F);
    1287             : }
    1288             : 
    1289             : /* x assumed to be a t_MATs (factorization matrix), or compatible with
    1290             :  * the element_* functions. */
    1291             : static GEN
    1292       26916 : ext_sqr(GEN nf, GEN x)
    1293       26916 : { return (typ(x)==t_MAT)? famat_sqr(x): nfsqr(nf, x); }
    1294             : static GEN
    1295      123568 : ext_mul(GEN nf, GEN x, GEN y)
    1296      123568 : { return (typ(x)==t_MAT)? famat_mul(x,y): nfmul(nf, x, y); }
    1297             : static GEN
    1298       17628 : ext_inv(GEN nf, GEN x)
    1299       17628 : { return (typ(x)==t_MAT)? famat_inv(x): nfinv(nf, x); }
    1300             : static GEN
    1301        2031 : ext_pow(GEN nf, GEN x, GEN n)
    1302        2031 : { return (typ(x)==t_MAT)? famat_pow(x,n): nfpow(nf, x, n); }
    1303             : 
    1304             : GEN
    1305           0 : famat_to_nf(GEN nf, GEN f)
    1306             : {
    1307             :   GEN t, x, e;
    1308             :   long i;
    1309           0 :   if (lgcols(f) == 1) return gen_1;
    1310           0 :   x = gel(f,1);
    1311           0 :   e = gel(f,2);
    1312           0 :   t = nfpow(nf, gel(x,1), gel(e,1));
    1313           0 :   for (i=lg(x)-1; i>1; i--)
    1314           0 :     t = nfmul(nf, t, nfpow(nf, gel(x,i), gel(e,i)));
    1315           0 :   return t;
    1316             : }
    1317             : 
    1318             : GEN
    1319       32963 : famat_reduce(GEN fa)
    1320             : {
    1321             :   GEN E, G, L, g, e;
    1322             :   long i, k, l;
    1323             : 
    1324       32963 :   if (lgcols(fa) == 1) return fa;
    1325       28084 :   g = gel(fa,1); l = lg(g);
    1326       28084 :   e = gel(fa,2);
    1327       28084 :   L = gen_indexsort(g, (void*)&cmp_universal, &cmp_nodata);
    1328       28084 :   G = cgetg(l, t_COL);
    1329       28084 :   E = cgetg(l, t_COL);
    1330             :   /* merge */
    1331       67472 :   for (k=i=1; i<l; i++,k++)
    1332             :   {
    1333       39388 :     gel(G,k) = gel(g,L[i]);
    1334       39388 :     gel(E,k) = gel(e,L[i]);
    1335       39388 :     if (k > 1 && gidentical(gel(G,k), gel(G,k-1)))
    1336             :     {
    1337         973 :       gel(E,k-1) = addii(gel(E,k), gel(E,k-1));
    1338         973 :       k--;
    1339             :     }
    1340             :   }
    1341             :   /* kill 0 exponents */
    1342       28084 :   l = k;
    1343       66499 :   for (k=i=1; i<l; i++)
    1344       38415 :     if (!gequal0(gel(E,i)))
    1345             :     {
    1346       37379 :       gel(G,k) = gel(G,i);
    1347       37379 :       gel(E,k) = gel(E,i); k++;
    1348             :     }
    1349       28084 :   setlg(G, k);
    1350       28084 :   setlg(E, k); return mkmat2(G,E);
    1351             : }
    1352             : 
    1353             : GEN
    1354       14666 : famatsmall_reduce(GEN fa)
    1355             : {
    1356             :   GEN E, G, L, g, e;
    1357             :   long i, k, l;
    1358       14666 :   if (lgcols(fa) == 1) return fa;
    1359       14666 :   g = gel(fa,1); l = lg(g);
    1360       14666 :   e = gel(fa,2);
    1361       14666 :   L = vecsmall_indexsort(g);
    1362       14666 :   G = cgetg(l, t_VECSMALL);
    1363       14666 :   E = cgetg(l, t_VECSMALL);
    1364             :   /* merge */
    1365      131115 :   for (k=i=1; i<l; i++,k++)
    1366             :   {
    1367      116449 :     G[k] = g[L[i]];
    1368      116449 :     E[k] = e[L[i]];
    1369      116449 :     if (k > 1 && G[k] == G[k-1])
    1370             :     {
    1371        7060 :       E[k-1] += E[k];
    1372        7060 :       k--;
    1373             :     }
    1374             :   }
    1375             :   /* kill 0 exponents */
    1376       14666 :   l = k;
    1377      124055 :   for (k=i=1; i<l; i++)
    1378      109389 :     if (E[i])
    1379             :     {
    1380      105656 :       G[k] = G[i];
    1381      105656 :       E[k] = E[i]; k++;
    1382             :     }
    1383       14666 :   setlg(G, k);
    1384       14666 :   setlg(E, k); return mkmat2(G,E);
    1385             : }
    1386             : 
    1387             : GEN
    1388       61929 : ZM_famat_limit(GEN fa, GEN limit)
    1389             : {
    1390             :   pari_sp av;
    1391             :   GEN E, G, g, e, r;
    1392             :   long i, k, l, n, lG;
    1393             : 
    1394       61929 :   if (lgcols(fa) == 1) return fa;
    1395       61922 :   g = gel(fa,1); l = lg(g);
    1396       61922 :   e = gel(fa,2);
    1397      137690 :   for(n=0, i=1; i<l; i++)
    1398       75768 :     if (cmpii(gel(g,i),limit)<=0) n++;
    1399       61922 :   lG = n<l-1 ? n+2 : n+1;
    1400       61922 :   G = cgetg(lG, t_COL);
    1401       61922 :   E = cgetg(lG, t_COL);
    1402       61922 :   av = avma;
    1403      137690 :   for (i=1, k=1, r = gen_1; i<l; i++)
    1404             :   {
    1405       75768 :     if (cmpii(gel(g,i),limit)<=0)
    1406             :     {
    1407       75670 :       gel(G,k) = gel(g,i);
    1408       75670 :       gel(E,k) = gel(e,i);
    1409       75670 :       k++;
    1410          98 :     } else r = mulii(r, powii(gel(g,i), gel(e,i)));
    1411             :   }
    1412       61922 :   if (k<i)
    1413             :   {
    1414          98 :     gel(G, k) = gerepileuptoint(av, r);
    1415          98 :     gel(E, k) = gen_1;
    1416             :   }
    1417       61922 :   return mkmat2(G,E);
    1418             : }
    1419             : 
    1420             : /* assume pr has degree 1 and coprime to Q_denom(x) */
    1421             : static GEN
    1422        4577 : to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1423             : {
    1424        4577 :   GEN d, r, p = modpr_get_p(modpr);
    1425        4577 :   x = nf_to_scalar_or_basis(nf,x);
    1426        4577 :   if (typ(x) != t_COL) return Rg_to_Fp(x,p);
    1427        4297 :   x = Q_remove_denom(x, &d);
    1428        4297 :   r = zk_to_Fq(x, modpr);
    1429        4297 :   if (d) r = Fp_div(r, d, p);
    1430        4297 :   return r;
    1431             : }
    1432             : 
    1433             : /* pr coprime to all denominators occurring in x */
    1434             : static GEN
    1435         538 : famat_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1436             : {
    1437         538 :   GEN p = modpr_get_p(modpr);
    1438         538 :   GEN t = NULL, g = gel(x,1), e = gel(x,2), q = subiu(p,1);
    1439         538 :   long i, l = lg(g);
    1440        1731 :   for (i = 1; i < l; i++)
    1441             :   {
    1442        1193 :     GEN n = modii(gel(e,i), q);
    1443        1193 :     if (signe(n))
    1444             :     {
    1445        1193 :       GEN h = to_Fp_coprime(nf, gel(g,i), modpr);
    1446        1193 :       h = Fp_pow(h, n, p);
    1447        1193 :       t = t? Fp_mul(t, h, p): h;
    1448             :     }
    1449             :   }
    1450         538 :   return t? modii(t, p): gen_1;
    1451             : }
    1452             : 
    1453             : /* cf famat_to_nf_modideal_coprime, modpr attached to prime of degree 1 */
    1454             : GEN
    1455        3922 : nf_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1456             : {
    1457        3922 :   return typ(x)==t_MAT? famat_to_Fp_coprime(nf, x, modpr)
    1458        3922 :                       : to_Fp_coprime(nf, x, modpr);
    1459             : }
    1460             : 
    1461             : static long
    1462      152916 : zk_pvalrem(GEN x, GEN p, GEN *py)
    1463      152916 : { return (typ(x) == t_INT)? Z_pvalrem(x, p, py): ZV_pvalrem(x, p, py); }
    1464             : /* x a QC or Q. Return a ZC or Z, whose content is coprime to Z. Set v, dx
    1465             :  * such that x = p^v (newx / dx); dx = NULL if 1 */
    1466             : static GEN
    1467      286147 : nf_remove_denom_p(GEN nf, GEN x, GEN p, GEN *pdx, long *pv)
    1468             : {
    1469             :   long vcx;
    1470             :   GEN dx;
    1471      286147 :   x = nf_to_scalar_or_basis(nf, x);
    1472      286147 :   x = Q_remove_denom(x, &dx);
    1473      286147 :   if (dx)
    1474             :   {
    1475      179872 :     vcx = - Z_pvalrem(dx, p, &dx);
    1476      179872 :     if (!vcx) vcx = zk_pvalrem(x, p, &x);
    1477      179872 :     if (isint1(dx)) dx = NULL;
    1478             :   }
    1479             :   else
    1480             :   {
    1481      106275 :     vcx = zk_pvalrem(x, p, &x);
    1482      106275 :     dx = NULL;
    1483             :   }
    1484      286147 :   *pv = vcx;
    1485      286147 :   *pdx = dx; return x;
    1486             : }
    1487             : /* x = b^e/p^(e-1) in Z_K; x = 0 mod p/pr^e, (x,pr) = 1. Return NULL
    1488             :  * if p inert (instead of 1) */
    1489             : static GEN
    1490       64995 : p_makecoprime(GEN pr)
    1491             : {
    1492       64995 :   GEN B = pr_get_tau(pr), b;
    1493             :   long i, e;
    1494             : 
    1495       64995 :   if (typ(B) == t_INT) return NULL;
    1496       64855 :   b = gel(B,1); /* B = multiplication table by b */
    1497       64855 :   e = pr_get_e(pr);
    1498       64855 :   if (e == 1) return b;
    1499             :   /* one could also divide (exactly) by p in each iteration */
    1500       18186 :   for (i = 1; i < e; i++) b = ZM_ZC_mul(B, b);
    1501       18186 :   return ZC_Z_divexact(b, powiu(pr_get_p(pr), e-1));
    1502             : }
    1503             : 
    1504             : /* Compute A = prod g[i]^e[i] mod pr^k, assuming (A, pr) = 1.
    1505             :  * Method: modify each g[i] so that it becomes coprime to pr,
    1506             :  * g[i] *= (b/p)^v_pr(g[i]), where b/p = pr^(-1) times something integral
    1507             :  * and prime to p; globally, we multiply by (b/p)^v_pr(A) = 1.
    1508             :  * Optimizations:
    1509             :  * 1) remove all powers of p from contents, and consider extra generator p^vp;
    1510             :  * modified as p * (b/p)^e = b^e / p^(e-1)
    1511             :  * 2) remove denominators, coprime to p, by multiplying by inverse mod prk\cap Z
    1512             :  *
    1513             :  * EX = multiple of exponent of (O_K / pr^k)^* used to reduce the product in
    1514             :  * case the e[i] are large */
    1515             : GEN
    1516      123645 : famat_makecoprime(GEN nf, GEN g, GEN e, GEN pr, GEN prk, GEN EX)
    1517             : {
    1518      123645 :   GEN G, E, t, vp = NULL, p = pr_get_p(pr), prkZ = gcoeff(prk, 1,1);
    1519      123645 :   long i, l = lg(g);
    1520             : 
    1521      123645 :   G = cgetg(l+1, t_VEC);
    1522      123645 :   E = cgetg(l+1, t_VEC); /* l+1: room for "modified p" */
    1523      409792 :   for (i=1; i < l; i++)
    1524             :   {
    1525             :     long vcx;
    1526      286147 :     GEN dx, x = nf_remove_denom_p(nf, gel(g,i), p, &dx, &vcx);
    1527      286147 :     if (vcx) /* = v_p(content(g[i])) */
    1528             :     {
    1529      136178 :       GEN a = mulsi(vcx, gel(e,i));
    1530      136178 :       vp = vp? addii(vp, a): a;
    1531             :     }
    1532             :     /* x integral, content coprime to p; dx coprime to p */
    1533      286147 :     if (typ(x) == t_INT)
    1534             :     { /* x coprime to p, hence to pr */
    1535       42018 :       x = modii(x, prkZ);
    1536       42018 :       if (dx) x = Fp_div(x, dx, prkZ);
    1537             :     }
    1538             :     else
    1539             :     {
    1540      244129 :       (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
    1541      244129 :       x = ZC_hnfrem(FpC_red(x,prkZ), prk);
    1542      244129 :       if (dx) x = FpC_Fp_mul(x, Fp_inv(dx,prkZ), prkZ);
    1543             :     }
    1544      286147 :     gel(G,i) = x;
    1545      286147 :     gel(E,i) = gel(e,i);
    1546             :   }
    1547             : 
    1548      123645 :   t = vp? p_makecoprime(pr): NULL;
    1549      123645 :   if (!t)
    1550             :   { /* no need for extra generator */
    1551       58790 :     setlg(G,l);
    1552       58790 :     setlg(E,l);
    1553             :   }
    1554             :   else
    1555             :   {
    1556       64855 :     gel(G,i) = FpC_red(t, prkZ);
    1557       64855 :     gel(E,i) = vp;
    1558             :   }
    1559      123645 :   return famat_to_nf_modideal_coprime(nf, G, E, prk, EX);
    1560             : }
    1561             : 
    1562             : /* prod g[i]^e[i] mod bid, assume (g[i], id) = 1 and 1 < lg(g) <= lg(e) */
    1563             : GEN
    1564       18928 : famat_to_nf_moddivisor(GEN nf, GEN g, GEN e, GEN bid)
    1565             : {
    1566       18928 :   GEN t, cyc = bid_get_cyc(bid);
    1567       18928 :   if (lg(cyc) == 1)
    1568           0 :     t = gen_1;
    1569             :   else
    1570       18928 :     t = famat_to_nf_modideal_coprime(nf, g, e, bid_get_ideal(bid), gel(cyc,1));
    1571       18928 :   return set_sign_mod_divisor(nf, mkmat2(g,e), t, bid_get_sarch(bid));
    1572             : }
    1573             : 
    1574             : GEN
    1575      221480 : vecmul(GEN x, GEN y)
    1576             : {
    1577      221480 :   if (is_scalar_t(typ(x))) return gmul(x,y);
    1578       22948 :   pari_APPLY_same(vecmul(gel(x,i), gel(y,i)))
    1579             : }
    1580             : 
    1581             : GEN
    1582           0 : vecinv(GEN x)
    1583             : {
    1584           0 :   if (is_scalar_t(typ(x))) return ginv(x);
    1585           0 :   pari_APPLY_same(vecinv(gel(x,i)))
    1586             : }
    1587             : 
    1588             : GEN
    1589       20657 : vecpow(GEN x, GEN n)
    1590             : {
    1591       20657 :   if (is_scalar_t(typ(x))) return powgi(x,n);
    1592        5453 :   pari_APPLY_same(vecpow(gel(x,i), n))
    1593             : }
    1594             : 
    1595             : GEN
    1596         903 : vecdiv(GEN x, GEN y)
    1597             : {
    1598         903 :   if (is_scalar_t(typ(x))) return gdiv(x,y);
    1599         301 :   pari_APPLY_same(vecdiv(gel(x,i), gel(y,i)))
    1600             : }
    1601             : 
    1602             : /* A ideal as a square t_MAT */
    1603             : static GEN
    1604      236646 : idealmulelt(GEN nf, GEN x, GEN A)
    1605             : {
    1606             :   long i, lx;
    1607             :   GEN dx, dA, D;
    1608      236646 :   if (lg(A) == 1) return cgetg(1, t_MAT);
    1609      236646 :   x = nf_to_scalar_or_basis(nf,x);
    1610      236646 :   if (typ(x) != t_COL)
    1611       97054 :     return isintzero(x)? cgetg(1,t_MAT): RgM_Rg_mul(A, Q_abs_shallow(x));
    1612      139592 :   x = Q_remove_denom(x, &dx);
    1613      139592 :   A = Q_remove_denom(A, &dA);
    1614      139592 :   x = zk_multable(nf, x);
    1615      139592 :   D = mulii(zkmultable_capZ(x), gcoeff(A,1,1));
    1616      139592 :   x = zkC_multable_mul(A, x);
    1617      139592 :   settyp(x, t_MAT); lx = lg(x);
    1618             :   /* x may contain scalars (at most 1 since the ideal is non-0)*/
    1619      479502 :   for (i=1; i<lx; i++)
    1620      350206 :     if (typ(gel(x,i)) == t_INT)
    1621             :     {
    1622       10296 :       if (i > 1) swap(gel(x,1), gel(x,i)); /* help HNF */
    1623       10296 :       gel(x,1) = scalarcol_shallow(gel(x,1), lx-1);
    1624       10296 :       break;
    1625             :     }
    1626      139592 :   x = ZM_hnfmodid(x, D);
    1627      139592 :   dx = mul_denom(dx,dA);
    1628      139592 :   return dx? gdiv(x,dx): x;
    1629             : }
    1630             : 
    1631             : /* nf a true nf, tx <= ty */
    1632             : static GEN
    1633     1393432 : idealmul_aux(GEN nf, GEN x, GEN y, long tx, long ty)
    1634             : {
    1635             :   GEN z, cx, cy;
    1636     1393432 :   switch(tx)
    1637             :   {
    1638             :     case id_PRINCIPAL:
    1639      292337 :       switch(ty)
    1640             :       {
    1641             :         case id_PRINCIPAL:
    1642       55495 :           return idealhnf_principal(nf, nfmul(nf,x,y));
    1643             :         case id_PRIME:
    1644             :         {
    1645         196 :           GEN p = pr_get_p(y), pi = pr_get_gen(y), cx;
    1646         196 :           if (pr_is_inert(y)) return RgM_Rg_mul(idealhnf_principal(nf,x),p);
    1647             : 
    1648          42 :           x = nf_to_scalar_or_basis(nf, x);
    1649          42 :           switch(typ(x))
    1650             :           {
    1651             :             case t_INT:
    1652          28 :               if (!signe(x)) return cgetg(1,t_MAT);
    1653          28 :               return ZM_Z_mul(pr_hnf(nf,y), absi_shallow(x));
    1654             :             case t_FRAC:
    1655           7 :               return RgM_Rg_mul(pr_hnf(nf,y), Q_abs_shallow(x));
    1656             :           }
    1657             :           /* t_COL */
    1658           7 :           x = Q_primitive_part(x, &cx);
    1659           7 :           x = zk_multable(nf, x);
    1660           7 :           z = shallowconcat(ZM_Z_mul(x,p), ZM_ZC_mul(x,pi));
    1661           7 :           z = ZM_hnfmodid(z, mulii(p, zkmultable_capZ(x)));
    1662           7 :           return cx? ZM_Q_mul(z, cx): z;
    1663             :         }
    1664             :         default: /* id_MAT */
    1665      236646 :           return idealmulelt(nf, x,y);
    1666             :       }
    1667             :     case id_PRIME:
    1668     1012638 :       if (ty==id_PRIME)
    1669     1008580 :       { y = pr_hnf(nf,y); cy = NULL; }
    1670             :       else
    1671        4058 :         y = Q_primitive_part(y, &cy);
    1672     1012638 :       y = idealHNF_mul_two(nf,y,x);
    1673     1012638 :       return cy? ZM_Q_mul(y,cy): y;
    1674             : 
    1675             :     default: /* id_MAT */
    1676             :     {
    1677       88457 :       long N = nf_get_degree(nf);
    1678       88457 :       if (lg(x)-1 != N || lg(y)-1 != N) pari_err_DIM("idealmul");
    1679       88443 :       x = Q_primitive_part(x, &cx);
    1680       88443 :       y = Q_primitive_part(y, &cy); cx = mul_content(cx,cy);
    1681       88443 :       y = idealHNF_mul(nf,x,y);
    1682       88443 :       return cx? ZM_Q_mul(y,cx): y;
    1683             :     }
    1684             :   }
    1685             : }
    1686             : 
    1687             : /* output the ideal product ix.iy */
    1688             : GEN
    1689     1393432 : idealmul(GEN nf, GEN x, GEN y)
    1690             : {
    1691             :   pari_sp av;
    1692             :   GEN res, ax, ay, z;
    1693     1393432 :   long tx = idealtyp(&x,&ax);
    1694     1393432 :   long ty = idealtyp(&y,&ay), f;
    1695     1393432 :   if (tx>ty) { swap(ax,ay); swap(x,y); lswap(tx,ty); }
    1696     1393432 :   f = (ax||ay); res = f? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1697     1393432 :   av = avma;
    1698     1393432 :   z = gerepileupto(av, idealmul_aux(checknf(nf), x,y, tx,ty));
    1699     1393418 :   if (!f) return z;
    1700       25595 :   if (ax && ay)
    1701       23898 :     ax = ext_mul(nf, ax, ay);
    1702             :   else
    1703        1697 :     ax = gcopy(ax? ax: ay);
    1704       25595 :   gel(res,1) = z; gel(res,2) = ax; return res;
    1705             : }
    1706             : 
    1707             : /* Return x, integral in 2-elt form, such that pr^2 = c * x. cf idealpowprime
    1708             :  * nf = true nf */
    1709             : static GEN
    1710       40959 : idealsqrprime(GEN nf, GEN pr, GEN *pc)
    1711             : {
    1712       40959 :   GEN p = pr_get_p(pr), q, gen;
    1713       40959 :   long e = pr_get_e(pr), f = pr_get_f(pr);
    1714             : 
    1715       40959 :   q = (e == 1)? sqri(p): p;
    1716       40959 :   if (e <= 2 && e * f == nf_get_degree(nf))
    1717             :   { /* pr^e = (p) */
    1718       12838 :     *pc = q;
    1719       12838 :     return mkvec2(gen_1,gen_0);
    1720             :   }
    1721       28121 :   gen = nfsqr(nf, pr_get_gen(pr));
    1722       28121 :   gen = FpC_red(gen, q);
    1723       28121 :   *pc = NULL;
    1724       28121 :   return mkvec2(q, gen);
    1725             : }
    1726             : /* cf idealpow_aux */
    1727             : static GEN
    1728       27266 : idealsqr_aux(GEN nf, GEN x, long tx)
    1729             : {
    1730       27266 :   GEN T = nf_get_pol(nf), m, cx, a, alpha;
    1731       27266 :   long N = degpol(T);
    1732       27266 :   switch(tx)
    1733             :   {
    1734             :     case id_PRINCIPAL:
    1735          56 :       return idealhnf_principal(nf, nfsqr(nf,x));
    1736             :     case id_PRIME:
    1737        8931 :       if (pr_is_inert(x)) return scalarmat(sqri(gel(x,1)), N);
    1738        8763 :       x = idealsqrprime(nf, x, &cx);
    1739        8763 :       x = idealhnf_two(nf,x);
    1740        8763 :       return cx? ZM_Z_mul(x, cx): x;
    1741             :     default:
    1742       18279 :       x = Q_primitive_part(x, &cx);
    1743       18279 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    1744       18279 :       alpha = nfsqr(nf,alpha);
    1745       18279 :       m = zk_scalar_or_multable(nf, alpha);
    1746       18279 :       if (typ(m) == t_INT) {
    1747        1260 :         x = gcdii(sqri(a), m);
    1748        1260 :         if (cx) x = gmul(x, gsqr(cx));
    1749        1260 :         x = scalarmat(x, N);
    1750             :       }
    1751             :       else
    1752             :       {
    1753       17019 :         x = ZM_hnfmodid(m, gcdii(sqri(a), zkmultable_capZ(m)));
    1754       17019 :         if (cx) cx = gsqr(cx);
    1755       17019 :         if (cx) x = ZM_Q_mul(x, cx);
    1756             :       }
    1757       18279 :       return x;
    1758             :   }
    1759             : }
    1760             : GEN
    1761       27266 : idealsqr(GEN nf, GEN x)
    1762             : {
    1763             :   pari_sp av;
    1764             :   GEN res, ax, z;
    1765       27266 :   long tx = idealtyp(&x,&ax);
    1766       27266 :   res = ax? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1767       27266 :   av = avma;
    1768       27266 :   z = gerepileupto(av, idealsqr_aux(checknf(nf), x, tx));
    1769       27266 :   if (!ax) return z;
    1770       26916 :   gel(res,1) = z;
    1771       26916 :   gel(res,2) = ext_sqr(nf, ax); return res;
    1772             : }
    1773             : 
    1774             : /* norm of an ideal */
    1775             : GEN
    1776        8134 : idealnorm(GEN nf, GEN x)
    1777             : {
    1778             :   pari_sp av;
    1779             :   GEN y, T;
    1780             :   long tx;
    1781             : 
    1782        8134 :   switch(idealtyp(&x,&y))
    1783             :   {
    1784         245 :     case id_PRIME: return pr_norm(x);
    1785        5187 :     case id_MAT: return RgM_det_triangular(x);
    1786             :   }
    1787             :   /* id_PRINCIPAL */
    1788        2702 :   nf = checknf(nf); T = nf_get_pol(nf); av = avma;
    1789        2702 :   x = nf_to_scalar_or_alg(nf, x);
    1790        2702 :   x = (typ(x) == t_POL)? RgXQ_norm(x, T): gpowgs(x, degpol(T));
    1791        2702 :   tx = typ(x);
    1792        2702 :   if (tx == t_INT) return gerepileuptoint(av, absi(x));
    1793         637 :   if (tx != t_FRAC) pari_err_TYPE("idealnorm",x);
    1794         637 :   return gerepileupto(av, Q_abs(x));
    1795             : }
    1796             : 
    1797             : /* x \cap Z */
    1798             : GEN
    1799          35 : idealdown(GEN nf, GEN x)
    1800             : {
    1801          35 :   pari_sp av = avma;
    1802             :   GEN y, c;
    1803          35 :   switch(idealtyp(&x,&y))
    1804             :   {
    1805           7 :     case id_PRIME: return icopy(pr_get_p(x));
    1806           7 :     case id_MAT: return gcopy(gcoeff(x,1,1));
    1807             :   }
    1808             :   /* id_PRINCIPAL */
    1809          21 :   nf = checknf(nf); av = avma;
    1810          21 :   x = nf_to_scalar_or_basis(nf, x);
    1811          21 :   if (is_rational_t(typ(x))) return Q_abs(x);
    1812          14 :   x = Q_primitive_part(x, &c);
    1813          14 :   y = zkmultable_capZ(zk_multable(nf, x));
    1814          14 :   return gerepilecopy(av, mul_content(c, y));
    1815             : }
    1816             : 
    1817             : /* true nf */
    1818             : static GEN
    1819          28 : idealismaximal_int(GEN nf, GEN p)
    1820             : {
    1821             :   GEN L;
    1822          28 :   if (!BPSW_psp(p)) return NULL;
    1823          56 :   if (!dvdii(nf_get_index(nf), p) &&
    1824          42 :       !FpX_is_irred(FpX_red(nf_get_pol(nf),p), p)) return NULL;
    1825          14 :   L = idealprimedec(nf, p);
    1826          14 :   return lg(L) == 2? gel(L,1): NULL;
    1827             : }
    1828             : /* true nf */
    1829             : static GEN
    1830           7 : idealismaximal_mat(GEN nf, GEN x)
    1831             : {
    1832             :   GEN p, c, L;
    1833             :   long i, l, f;
    1834           7 :   x = Q_primitive_part(x, &c);
    1835           7 :   p = gcoeff(x,1,1);
    1836           7 :   if (c)
    1837             :   {
    1838           0 :     if (typ(c) == t_FRAC || !equali1(p)) return NULL;
    1839           0 :     return idealismaximal_int(nf, p);
    1840             :   }
    1841           7 :   if (!BPSW_psp(p)) return NULL;
    1842           7 :   l = lg(x); f = 1;
    1843          21 :   for (i = 2; i < l; i++)
    1844             :   {
    1845          14 :     c = gcoeff(x,i,i);
    1846          14 :     if (equalii(c, p)) f++; else if (!equali1(c)) return NULL;
    1847             :   }
    1848           7 :   L = idealprimedec_limit_f(nf, p, f);
    1849          14 :   for (i = lg(L)-1; i; i--)
    1850             :   {
    1851          14 :     GEN pr = gel(L,i);
    1852          14 :     if (pr_get_f(pr) != f) break;
    1853          14 :     if (idealval(nf, x, pr) == 1) return pr;
    1854             :   }
    1855           0 :   return NULL;
    1856             : }
    1857             : /* true nf */
    1858             : static GEN
    1859          42 : idealismaximal_i(GEN nf, GEN x)
    1860             : {
    1861             :   GEN L, p, pr, c;
    1862             :   long i, l;
    1863          42 :   switch(idealtyp(&x,&c))
    1864             :   {
    1865           7 :     case id_PRIME: return x;
    1866           7 :     case id_MAT: return idealismaximal_mat(nf, x);
    1867             :   }
    1868             :   /* id_PRINCIPAL */
    1869          28 :   nf = checknf(nf);
    1870          28 :   x = nf_to_scalar_or_basis(nf, x);
    1871          28 :   switch(typ(x))
    1872             :   {
    1873          28 :     case t_INT: return idealismaximal_int(nf, absi_shallow(x));
    1874           0 :     case t_FRAC: return NULL;
    1875             :   }
    1876           0 :   x = Q_primitive_part(x, &c);
    1877           0 :   if (c) return NULL;
    1878           0 :   p = zkmultable_capZ(zk_multable(nf, x));
    1879           0 :   L = idealprimedec(nf, p); l = lg(L); pr = NULL;
    1880           0 :   for (i = 1; i < l; i++)
    1881             :   {
    1882           0 :     long v = ZC_nfval(x, gel(L,i));
    1883           0 :     if (v > 1 || (v && pr)) return NULL;
    1884           0 :     pr = gel(L,i);
    1885             :   }
    1886           0 :   return pr;
    1887             : }
    1888             : GEN
    1889          42 : idealismaximal(GEN nf, GEN x)
    1890             : {
    1891          42 :   pari_sp av = avma;
    1892          42 :   x = idealismaximal_i(checknf(nf), x);
    1893          42 :   if (!x) { set_avma(av); return gen_0; }
    1894          28 :   return gerepilecopy(av, x);
    1895             : }
    1896             : 
    1897             : /* I^(-1) = { x \in K, Tr(x D^(-1) I) \in Z }, D different of K/Q
    1898             :  *
    1899             :  * nf[5][6] = pp( D^(-1) ) = pp( HNF( T^(-1) ) ), T = (Tr(wi wj))
    1900             :  * nf[5][7] = same in 2-elt form.
    1901             :  * Assume I integral. Return the integral ideal (I\cap Z) I^(-1) */
    1902             : GEN
    1903      200749 : idealHNF_inv_Z(GEN nf, GEN I)
    1904             : {
    1905      200749 :   GEN J, dual, IZ = gcoeff(I,1,1); /* I \cap Z */
    1906      200749 :   if (isint1(IZ)) return matid(lg(I)-1);
    1907      188702 :   J = idealHNF_mul(nf,I, gmael(nf,5,7));
    1908             :  /* I in HNF, hence easily inverted; multiply by IZ to get integer coeffs
    1909             :   * missing content cancels while solving the linear equation */
    1910      188702 :   dual = shallowtrans( hnf_divscale(J, gmael(nf,5,6), IZ) );
    1911      188702 :   return ZM_hnfmodid(dual, IZ);
    1912             : }
    1913             : /* I HNF with rational coefficients (denominator d). */
    1914             : GEN
    1915       74634 : idealHNF_inv(GEN nf, GEN I)
    1916             : {
    1917       74634 :   GEN J, IQ = gcoeff(I,1,1); /* I \cap Q; d IQ = dI \cap Z */
    1918       74634 :   J = idealHNF_inv_Z(nf, Q_remove_denom(I, NULL)); /* = (dI)^(-1) * (d IQ) */
    1919       74634 :   return equali1(IQ)? J: RgM_Rg_div(J, IQ);
    1920             : }
    1921             : 
    1922             : /* return p * P^(-1)  [integral] */
    1923             : GEN
    1924       25951 : pr_inv_p(GEN pr)
    1925             : {
    1926       25951 :   if (pr_is_inert(pr)) return matid(pr_get_f(pr));
    1927       25363 :   return ZM_hnfmodid(pr_get_tau(pr), pr_get_p(pr));
    1928             : }
    1929             : GEN
    1930        5505 : pr_inv(GEN pr)
    1931             : {
    1932        5505 :   GEN p = pr_get_p(pr);
    1933        5505 :   if (pr_is_inert(pr)) return scalarmat(ginv(p), pr_get_f(pr));
    1934        5169 :   return RgM_Rg_div(ZM_hnfmodid(pr_get_tau(pr),p), p);
    1935             : }
    1936             : 
    1937             : GEN
    1938      118933 : idealinv(GEN nf, GEN x)
    1939             : {
    1940             :   GEN res, ax;
    1941             :   pari_sp av;
    1942      118933 :   long tx = idealtyp(&x,&ax), N;
    1943             : 
    1944      118933 :   res = ax? cgetg(3,t_VEC): NULL;
    1945      118933 :   nf = checknf(nf); av = avma;
    1946      118933 :   N = nf_get_degree(nf);
    1947      118933 :   switch (tx)
    1948             :   {
    1949             :     case id_MAT:
    1950       67642 :       if (lg(x)-1 != N) pari_err_DIM("idealinv");
    1951       67642 :       x = idealHNF_inv(nf,x); break;
    1952             :     case id_PRINCIPAL:
    1953       46703 :       x = nf_to_scalar_or_basis(nf, x);
    1954       46703 :       if (typ(x) != t_COL)
    1955       46661 :         x = idealhnf_principal(nf,ginv(x));
    1956             :       else
    1957             :       { /* nfinv + idealhnf where we already know (x) \cap Z */
    1958             :         GEN c, d;
    1959          42 :         x = Q_remove_denom(x, &c);
    1960          42 :         x = zk_inv(nf, x);
    1961          42 :         x = Q_remove_denom(x, &d); /* true inverse is c/d * x */
    1962          42 :         if (!d) /* x and x^(-1) integral => x a unit */
    1963           7 :           x = scalarmat_shallow(c? c: gen_1, N);
    1964             :         else
    1965             :         {
    1966          35 :           c = c? gdiv(c,d): ginv(d);
    1967          35 :           x = zk_multable(nf, x);
    1968          35 :           x = ZM_Q_mul(ZM_hnfmodid(x,d), c);
    1969             :         }
    1970             :       }
    1971       46703 :       break;
    1972             :     case id_PRIME:
    1973        4588 :       x = pr_inv(x); break;
    1974             :   }
    1975      118933 :   x = gerepileupto(av,x); if (!ax) return x;
    1976       17628 :   gel(res,1) = x;
    1977       17628 :   gel(res,2) = ext_inv(nf, ax); return res;
    1978             : }
    1979             : 
    1980             : /* write x = A/B, A,B coprime integral ideals */
    1981             : GEN
    1982       60837 : idealnumden(GEN nf, GEN x)
    1983             : {
    1984       60837 :   pari_sp av = avma;
    1985             :   GEN x0, ax, c, d, A, B, J;
    1986       60837 :   long tx = idealtyp(&x,&ax);
    1987       60837 :   nf = checknf(nf);
    1988       60837 :   switch (tx)
    1989             :   {
    1990             :     case id_PRIME:
    1991           7 :       retmkvec2(idealhnf(nf, x), gen_1);
    1992             :     case id_PRINCIPAL:
    1993             :     {
    1994             :       GEN xZ, mx;
    1995        6125 :       x = nf_to_scalar_or_basis(nf, x);
    1996        6125 :       switch(typ(x))
    1997             :       {
    1998        1113 :         case t_INT: return gerepilecopy(av, mkvec2(absi_shallow(x),gen_1));
    1999          14 :         case t_FRAC:return gerepilecopy(av, mkvec2(absi_shallow(gel(x,1)), gel(x,2)));
    2000             :       }
    2001             :       /* t_COL */
    2002        4998 :       x = Q_remove_denom(x, &d);
    2003        4998 :       if (!d) return gerepilecopy(av, mkvec2(idealhnf(nf, x), gen_1));
    2004          35 :       mx = zk_multable(nf, x);
    2005          35 :       xZ = zkmultable_capZ(mx);
    2006          35 :       x = ZM_hnfmodid(mx, xZ); /* principal ideal (x) */
    2007          35 :       x0 = mkvec2(xZ, mx); /* same, for fast multiplication */
    2008          35 :       break;
    2009             :     }
    2010             :     default: /* id_MAT */
    2011             :     {
    2012       54705 :       long n = lg(x)-1;
    2013       54705 :       if (n == 0) return mkvec2(gen_0, gen_1);
    2014       54705 :       if (n != nf_get_degree(nf)) pari_err_DIM("idealnumden");
    2015       54705 :       x0 = x = Q_remove_denom(x, &d);
    2016       54705 :       if (!d) return gerepilecopy(av, mkvec2(x, gen_1));
    2017          14 :       break;
    2018             :     }
    2019             :   }
    2020          49 :   J = hnfmodid(x, d); /* = d/B */
    2021          49 :   c = gcoeff(J,1,1); /* (d/B) \cap Z, divides d */
    2022          49 :   B = idealHNF_inv_Z(nf, J); /* (d/B \cap Z) B/d */
    2023          49 :   if (!equalii(c,d)) B = ZM_Z_mul(B, diviiexact(d,c)); /* = B ! */
    2024          49 :   A = idealHNF_mul(nf, B, x0); /* d * (original x) * B = d A */
    2025          49 :   A = ZM_Z_divexact(A, d); /* = A ! */
    2026          49 :   return gerepilecopy(av, mkvec2(A, B));
    2027             : }
    2028             : 
    2029             : /* Return x, integral in 2-elt form, such that pr^n = c * x. Assume n != 0.
    2030             :  * nf = true nf */
    2031             : static GEN
    2032      159218 : idealpowprime(GEN nf, GEN pr, GEN n, GEN *pc)
    2033             : {
    2034      159218 :   GEN p = pr_get_p(pr), q, gen;
    2035             : 
    2036      159218 :   *pc = NULL;
    2037      159218 :   if (is_pm1(n)) /* n = 1 special cased for efficiency */
    2038             :   {
    2039       84932 :     q = p;
    2040       84932 :     if (typ(pr_get_tau(pr)) == t_INT) /* inert */
    2041             :     {
    2042           0 :       *pc = (signe(n) >= 0)? p: ginv(p);
    2043           0 :       return mkvec2(gen_1,gen_0);
    2044             :     }
    2045       84932 :     if (signe(n) >= 0) gen = pr_get_gen(pr);
    2046             :     else
    2047             :     {
    2048       18116 :       gen = pr_get_tau(pr); /* possibly t_MAT */
    2049       18116 :       *pc = ginv(p);
    2050             :     }
    2051             :   }
    2052       74286 :   else if (equalis(n,2)) return idealsqrprime(nf, pr, pc);
    2053             :   else
    2054             :   {
    2055       42090 :     long e = pr_get_e(pr), f = pr_get_f(pr);
    2056       42090 :     GEN r, m = truedvmdis(n, e, &r);
    2057       42090 :     if (e * f == nf_get_degree(nf))
    2058             :     { /* pr^e = (p) */
    2059       11578 :       if (signe(m)) *pc = powii(p,m);
    2060       11578 :       if (!signe(r)) return mkvec2(gen_1,gen_0);
    2061        5264 :       q = p;
    2062        5264 :       gen = nfpow(nf, pr_get_gen(pr), r);
    2063             :     }
    2064             :     else
    2065             :     {
    2066       30512 :       m = absi_shallow(m);
    2067       30512 :       if (signe(r)) m = addiu(m,1);
    2068       30512 :       q = powii(p,m); /* m = ceil(|n|/e) */
    2069       30512 :       if (signe(n) >= 0) gen = nfpow(nf, pr_get_gen(pr), n);
    2070             :       else
    2071             :       {
    2072        4578 :         gen = pr_get_tau(pr);
    2073        4578 :         if (typ(gen) == t_MAT) gen = gel(gen,1);
    2074        4578 :         n = negi(n);
    2075        4578 :         gen = ZC_Z_divexact(nfpow(nf, gen, n), powii(p, subii(n,m)));
    2076        4578 :         *pc = ginv(q);
    2077             :       }
    2078             :     }
    2079       35776 :     gen = FpC_red(gen, q);
    2080             :   }
    2081      120708 :   return mkvec2(q, gen);
    2082             : }
    2083             : 
    2084             : /* x * pr^n. Assume x in HNF or scalar (possibly non-integral) */
    2085             : GEN
    2086      130201 : idealmulpowprime(GEN nf, GEN x, GEN pr, GEN n)
    2087             : {
    2088             :   GEN c, cx, y;
    2089             :   long N;
    2090             : 
    2091      130201 :   nf = checknf(nf);
    2092      130201 :   N = nf_get_degree(nf);
    2093      130201 :   if (!signe(n)) return typ(x) == t_MAT? x: scalarmat_shallow(x, N);
    2094             : 
    2095             :   /* inert, special cased for efficiency */
    2096      129809 :   if (pr_is_inert(pr))
    2097             :   {
    2098       10794 :     GEN q = powii(pr_get_p(pr), n);
    2099       10794 :     return typ(x) == t_MAT? RgM_Rg_mul(x,q)
    2100       10794 :                           : scalarmat_shallow(gmul(Q_abs(x),q), N);
    2101             :   }
    2102             : 
    2103      119015 :   y = idealpowprime(nf, pr, n, &c);
    2104      119015 :   if (typ(x) == t_MAT)
    2105      116586 :   { x = Q_primitive_part(x, &cx); if (is_pm1(gcoeff(x,1,1))) x = NULL; }
    2106             :   else
    2107        2429 :   { cx = x; x = NULL; }
    2108      119015 :   cx = mul_content(c,cx);
    2109      119015 :   if (x)
    2110       73746 :     x = idealHNF_mul_two(nf,x,y);
    2111             :   else
    2112       45269 :     x = idealhnf_two(nf,y);
    2113      119015 :   if (cx) x = ZM_Q_mul(x,cx);
    2114      119015 :   return x;
    2115             : }
    2116             : GEN
    2117        4277 : idealdivpowprime(GEN nf, GEN x, GEN pr, GEN n)
    2118             : {
    2119        4277 :   return idealmulpowprime(nf,x,pr, negi(n));
    2120             : }
    2121             : 
    2122             : /* nf = true nf */
    2123             : static GEN
    2124      201985 : idealpow_aux(GEN nf, GEN x, long tx, GEN n)
    2125             : {
    2126      201985 :   GEN T = nf_get_pol(nf), m, cx, n1, a, alpha;
    2127      201985 :   long N = degpol(T), s = signe(n);
    2128      201985 :   if (!s) return matid(N);
    2129      192800 :   switch(tx)
    2130             :   {
    2131             :     case id_PRINCIPAL:
    2132           0 :       return idealhnf_principal(nf, nfpow(nf,x,n));
    2133             :     case id_PRIME:
    2134       87306 :       if (pr_is_inert(x)) return scalarmat(powii(gel(x,1), n), N);
    2135       40203 :       x = idealpowprime(nf, x, n, &cx);
    2136       40203 :       x = idealhnf_two(nf,x);
    2137       40203 :       return cx? ZM_Q_mul(x, cx): x;
    2138             :     default:
    2139      105494 :       if (is_pm1(n)) return (s < 0)? idealinv(nf, x): gcopy(x);
    2140       57801 :       n1 = (s < 0)? negi(n): n;
    2141             : 
    2142       57801 :       x = Q_primitive_part(x, &cx);
    2143       57801 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    2144       57801 :       alpha = nfpow(nf,alpha,n1);
    2145       57801 :       m = zk_scalar_or_multable(nf, alpha);
    2146       57801 :       if (typ(m) == t_INT) {
    2147         301 :         x = gcdii(powii(a,n1), m);
    2148         301 :         if (s<0) x = ginv(x);
    2149         301 :         if (cx) x = gmul(x, powgi(cx,n));
    2150         301 :         x = scalarmat(x, N);
    2151             :       }
    2152             :       else
    2153             :       {
    2154       57500 :         x = ZM_hnfmodid(m, gcdii(powii(a,n1), zkmultable_capZ(m)));
    2155       57500 :         if (cx) cx = powgi(cx,n);
    2156       57500 :         if (s<0) {
    2157           7 :           GEN xZ = gcoeff(x,1,1);
    2158           7 :           cx = cx ? gdiv(cx, xZ): ginv(xZ);
    2159           7 :           x = idealHNF_inv_Z(nf,x);
    2160             :         }
    2161       57500 :         if (cx) x = ZM_Q_mul(x, cx);
    2162             :       }
    2163       57801 :       return x;
    2164             :   }
    2165             : }
    2166             : 
    2167             : /* raise the ideal x to the power n (in Z) */
    2168             : GEN
    2169      201985 : idealpow(GEN nf, GEN x, GEN n)
    2170             : {
    2171             :   pari_sp av;
    2172             :   long tx;
    2173             :   GEN res, ax;
    2174             : 
    2175      201985 :   if (typ(n) != t_INT) pari_err_TYPE("idealpow",n);
    2176      201985 :   tx = idealtyp(&x,&ax);
    2177      201985 :   res = ax? cgetg(3,t_VEC): NULL;
    2178      201985 :   av = avma;
    2179      201985 :   x = gerepileupto(av, idealpow_aux(checknf(nf), x, tx, n));
    2180      201985 :   if (!ax) return x;
    2181        2031 :   ax = ext_pow(nf, ax, n);
    2182        2031 :   gel(res,1) = x;
    2183        2031 :   gel(res,2) = ax;
    2184        2031 :   return res;
    2185             : }
    2186             : 
    2187             : /* Return ideal^e in number field nf. e is a C integer. */
    2188             : GEN
    2189       30625 : idealpows(GEN nf, GEN ideal, long e)
    2190             : {
    2191       30625 :   long court[] = {evaltyp(t_INT) | _evallg(3),0,0};
    2192       30625 :   affsi(e,court); return idealpow(nf,ideal,court);
    2193             : }
    2194             : 
    2195             : static GEN
    2196       25707 : _idealmulred(GEN nf, GEN x, GEN y)
    2197       25707 : { return idealred(nf,idealmul(nf,x,y)); }
    2198             : static GEN
    2199       27007 : _idealsqrred(GEN nf, GEN x)
    2200       27007 : { return idealred(nf,idealsqr(nf,x)); }
    2201             : static GEN
    2202        9083 : _mul(void *data, GEN x, GEN y) { return _idealmulred((GEN)data,x,y); }
    2203             : static GEN
    2204       27007 : _sqr(void *data, GEN x) { return _idealsqrred((GEN)data, x); }
    2205             : 
    2206             : /* compute x^n (x ideal, n integer), reducing along the way */
    2207             : GEN
    2208       54220 : idealpowred(GEN nf, GEN x, GEN n)
    2209             : {
    2210       54220 :   pari_sp av = avma, av2;
    2211             :   long s;
    2212             :   GEN y;
    2213             : 
    2214       54220 :   if (typ(n) != t_INT) pari_err_TYPE("idealpowred",n);
    2215       54220 :   s = signe(n); if (s == 0) return idealpow(nf,x,n);
    2216       52189 :   y = gen_pow_i(x, n, (void*)nf, &_sqr, &_mul);
    2217       52189 :   av2 = avma;
    2218       52189 :   if (s < 0) y = idealinv(nf,y);
    2219       52189 :   if (s < 0 || is_pm1(n)) y = idealred(nf,y);
    2220       52189 :   return avma == av2? gerepilecopy(av,y): gerepileupto(av,y);
    2221             : }
    2222             : 
    2223             : GEN
    2224       16624 : idealmulred(GEN nf, GEN x, GEN y)
    2225             : {
    2226       16624 :   pari_sp av = avma;
    2227       16624 :   return gerepileupto(av, _idealmulred(nf,x,y));
    2228             : }
    2229             : 
    2230             : long
    2231          91 : isideal(GEN nf,GEN x)
    2232             : {
    2233          91 :   long N, i, j, lx, tx = typ(x);
    2234             :   pari_sp av;
    2235             :   GEN T, xZ;
    2236             : 
    2237          91 :   nf = checknf(nf); T = nf_get_pol(nf); lx = lg(x);
    2238          91 :   if (tx==t_VEC && lx==3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
    2239          91 :   switch(tx)
    2240             :   {
    2241          14 :     case t_INT: case t_FRAC: return 1;
    2242           7 :     case t_POL: return varn(x) == varn(T);
    2243           7 :     case t_POLMOD: return RgX_equal_var(T, gel(x,1));
    2244          14 :     case t_VEC: return get_prid(x)? 1 : 0;
    2245          42 :     case t_MAT: break;
    2246           7 :     default: return 0;
    2247             :   }
    2248          42 :   N = degpol(T);
    2249          42 :   if (lx-1 != N) return (lx == 1);
    2250          28 :   if (nbrows(x) != N) return 0;
    2251             : 
    2252          28 :   av = avma; x = Q_primpart(x);
    2253          28 :   if (!ZM_ishnf(x)) return 0;
    2254          14 :   xZ = gcoeff(x,1,1);
    2255          21 :   for (j=2; j<=N; j++)
    2256          14 :     if (!dvdii(xZ, gcoeff(x,j,j))) return gc_long(av,0);
    2257          14 :   for (i=2; i<=N; i++)
    2258          14 :     for (j=2; j<=N; j++)
    2259           7 :        if (! hnf_invimage(x, zk_ei_mul(nf,gel(x,i),j))) return gc_long(av,0);
    2260           7 :   return gc_long(av,1);
    2261             : }
    2262             : 
    2263             : GEN
    2264       31913 : idealdiv(GEN nf, GEN x, GEN y)
    2265             : {
    2266       31913 :   pari_sp av = avma, tetpil;
    2267       31913 :   GEN z = idealinv(nf,y);
    2268       31913 :   tetpil = avma; return gerepile(av,tetpil, idealmul(nf,x,z));
    2269             : }
    2270             : 
    2271             : /* This routine computes the quotient x/y of two ideals in the number field nf.
    2272             :  * It assumes that the quotient is an integral ideal.  The idea is to find an
    2273             :  * ideal z dividing y such that gcd(Nx/Nz, Nz) = 1.  Then
    2274             :  *
    2275             :  *   x + (Nx/Nz)    x
    2276             :  *   ----------- = ---
    2277             :  *   y + (Ny/Nz)    y
    2278             :  *
    2279             :  * Proof: we can assume x and y are integral. Let p be any prime ideal
    2280             :  *
    2281             :  * If p | Nz, then it divides neither Nx/Nz nor Ny/Nz (since Nx/Nz is the
    2282             :  * product of the integers N(x/y) and N(y/z)).  Both the numerator and the
    2283             :  * denominator on the left will be coprime to p.  So will x/y, since x/y is
    2284             :  * assumed integral and its norm N(x/y) is coprime to p.
    2285             :  *
    2286             :  * If instead p does not divide Nz, then v_p (Nx/Nz) = v_p (Nx) >= v_p(x).
    2287             :  * Hence v_p (x + Nx/Nz) = v_p(x).  Likewise for the denominators.  QED.
    2288             :  *
    2289             :  *                Peter Montgomery.  July, 1994. */
    2290             : static void
    2291           7 : err_divexact(GEN x, GEN y)
    2292           7 : { pari_err_DOMAIN("idealdivexact","denominator(x/y)", "!=",
    2293           0 :                   gen_1,mkvec2(x,y)); }
    2294             : GEN
    2295        1365 : idealdivexact(GEN nf, GEN x0, GEN y0)
    2296             : {
    2297        1365 :   pari_sp av = avma;
    2298             :   GEN x, y, xZ, yZ, Nx, Ny, Nz, cy, q, r;
    2299             : 
    2300        1365 :   nf = checknf(nf);
    2301        1365 :   x = idealhnf_shallow(nf, x0);
    2302        1365 :   y = idealhnf_shallow(nf, y0);
    2303        1365 :   if (lg(y) == 1) pari_err_INV("idealdivexact", y0);
    2304        1358 :   if (lg(x) == 1) { set_avma(av); return cgetg(1, t_MAT); } /* numerator is zero */
    2305        1358 :   y = Q_primitive_part(y, &cy);
    2306        1358 :   if (cy) x = RgM_Rg_div(x,cy);
    2307        1358 :   xZ = gcoeff(x,1,1); if (typ(xZ) != t_INT) err_divexact(x,y);
    2308        1351 :   yZ = gcoeff(y,1,1); if (isint1(yZ)) return gerepilecopy(av, x);
    2309         511 :   Nx = idealnorm(nf,x);
    2310         511 :   Ny = idealnorm(nf,y);
    2311         511 :   if (typ(Nx) != t_INT) err_divexact(x,y);
    2312         511 :   q = dvmdii(Nx,Ny, &r);
    2313         511 :   if (signe(r)) err_divexact(x,y);
    2314         511 :   if (is_pm1(q)) { set_avma(av); return matid(nf_get_degree(nf)); }
    2315             :   /* Find a norm Nz | Ny such that gcd(Nx/Nz, Nz) = 1 */
    2316         427 :   for (Nz = Ny;;) /* q = Nx/Nz */
    2317         343 :   {
    2318         770 :     GEN p1 = gcdii(Nz, q);
    2319         770 :     if (is_pm1(p1)) break;
    2320         343 :     Nz = diviiexact(Nz,p1);
    2321         343 :     q = mulii(q,p1);
    2322             :   }
    2323         427 :   xZ = gcoeff(x,1,1); q = gcdii(q, xZ);
    2324         427 :   if (!equalii(xZ,q))
    2325             :   { /* Replace x/y  by  x+(Nx/Nz) / y+(Ny/Nz) */
    2326          91 :     x = ZM_hnfmodid(x, q);
    2327             :     /* y reduced to unit ideal ? */
    2328          91 :     if (Nz == Ny) return gerepileupto(av, x);
    2329             : 
    2330           7 :     yZ = gcoeff(y,1,1); q = gcdii(diviiexact(Ny,Nz), yZ);
    2331           7 :     y = ZM_hnfmodid(y, q);
    2332             :   }
    2333         343 :   yZ = gcoeff(y,1,1);
    2334         343 :   y = idealHNF_mul(nf,x, idealHNF_inv_Z(nf,y));
    2335         343 :   return gerepileupto(av, ZM_Z_divexact(y, yZ));
    2336             : }
    2337             : 
    2338             : GEN
    2339          21 : idealintersect(GEN nf, GEN x, GEN y)
    2340             : {
    2341          21 :   pari_sp av = avma;
    2342             :   long lz, lx, i;
    2343             :   GEN z, dx, dy, xZ, yZ;;
    2344             : 
    2345          21 :   nf = checknf(nf);
    2346          21 :   x = idealhnf_shallow(nf,x);
    2347          21 :   y = idealhnf_shallow(nf,y);
    2348          21 :   if (lg(x) == 1 || lg(y) == 1) { set_avma(av); return cgetg(1,t_MAT); }
    2349          14 :   x = Q_remove_denom(x, &dx);
    2350          14 :   y = Q_remove_denom(y, &dy);
    2351          14 :   if (dx) y = ZM_Z_mul(y, dx);
    2352          14 :   if (dy) x = ZM_Z_mul(x, dy);
    2353          14 :   xZ = gcoeff(x,1,1);
    2354          14 :   yZ = gcoeff(y,1,1);
    2355          14 :   dx = mul_denom(dx,dy);
    2356          14 :   z = ZM_lll(shallowconcat(x,y), 0.99, LLL_KER); lz = lg(z);
    2357          14 :   lx = lg(x);
    2358          14 :   for (i=1; i<lz; i++) setlg(z[i], lx);
    2359          14 :   z = ZM_hnfmodid(ZM_mul(x,z), lcmii(xZ, yZ));
    2360          14 :   if (dx) z = RgM_Rg_div(z,dx);
    2361          14 :   return gerepileupto(av,z);
    2362             : }
    2363             : 
    2364             : /*******************************************************************/
    2365             : /*                                                                 */
    2366             : /*                      T2-IDEAL REDUCTION                         */
    2367             : /*                                                                 */
    2368             : /*******************************************************************/
    2369             : 
    2370             : static GEN
    2371          21 : chk_vdir(GEN nf, GEN vdir)
    2372             : {
    2373          21 :   long i, l = lg(vdir);
    2374             :   GEN v;
    2375          21 :   if (l != lg(nf_get_roots(nf))) pari_err_DIM("idealred");
    2376          14 :   switch(typ(vdir))
    2377             :   {
    2378           0 :     case t_VECSMALL: return vdir;
    2379          14 :     case t_VEC: break;
    2380           0 :     default: pari_err_TYPE("idealred",vdir);
    2381             :   }
    2382          14 :   v = cgetg(l, t_VECSMALL);
    2383          14 :   for (i = 1; i < l; i++) v[i] = itos(gceil(gel(vdir,i)));
    2384          14 :   return v;
    2385             : }
    2386             : 
    2387             : static void
    2388       28880 : twistG(GEN G, long r1, long i, long v)
    2389             : {
    2390       28880 :   long j, lG = lg(G);
    2391       28880 :   if (i <= r1) {
    2392       23595 :     for (j=1; j<lG; j++) gcoeff(G,i,j) = gmul2n(gcoeff(G,i,j), v);
    2393             :   } else {
    2394        5285 :     long k = (i<<1) - r1;
    2395       28798 :     for (j=1; j<lG; j++)
    2396             :     {
    2397       23513 :       gcoeff(G,k-1,j) = gmul2n(gcoeff(G,k-1,j), v);
    2398       23513 :       gcoeff(G,k  ,j) = gmul2n(gcoeff(G,k  ,j), v);
    2399             :     }
    2400             :   }
    2401       28880 : }
    2402             : 
    2403             : GEN
    2404      154229 : nf_get_Gtwist(GEN nf, GEN vdir)
    2405             : {
    2406             :   long i, l, v, r1;
    2407             :   GEN G;
    2408             : 
    2409      154229 :   if (!vdir) return nf_get_roundG(nf);
    2410       26178 :   if (typ(vdir) == t_MAT)
    2411             :   {
    2412       26157 :     long N = nf_get_degree(nf);
    2413       26157 :     if (lg(vdir) != N+1 || lgcols(vdir) != N+1) pari_err_DIM("idealred");
    2414       26157 :     return vdir;
    2415             :   }
    2416          21 :   vdir = chk_vdir(nf, vdir);
    2417          14 :   G = RgM_shallowcopy(nf_get_G(nf));
    2418          14 :   r1 = nf_get_r1(nf);
    2419          14 :   l = lg(vdir);
    2420          56 :   for (i=1; i<l; i++)
    2421             :   {
    2422          42 :     v = vdir[i]; if (!v) continue;
    2423          42 :     twistG(G, r1, i, v);
    2424             :   }
    2425          14 :   return RM_round_maxrank(G);
    2426             : }
    2427             : GEN
    2428       28838 : nf_get_Gtwist1(GEN nf, long i)
    2429             : {
    2430       28838 :   GEN G = RgM_shallowcopy( nf_get_G(nf) );
    2431       28838 :   long r1 = nf_get_r1(nf);
    2432       28838 :   twistG(G, r1, i, 10);
    2433       28838 :   return RM_round_maxrank(G);
    2434             : }
    2435             : 
    2436             : GEN
    2437       46730 : RM_round_maxrank(GEN G0)
    2438             : {
    2439       46730 :   long e, r = lg(G0)-1;
    2440       46730 :   pari_sp av = avma;
    2441       46730 :   GEN G = G0;
    2442       46730 :   for (e = 4; ; e <<= 1)
    2443           0 :   {
    2444       46730 :     GEN H = ground(G);
    2445       93460 :     if (ZM_rank(H) == r) return H; /* maximal rank ? */
    2446           0 :     set_avma(av);
    2447           0 :     G = gmul2n(G0, e);
    2448             :   }
    2449             : }
    2450             : 
    2451             : GEN
    2452      154222 : idealred0(GEN nf, GEN I, GEN vdir)
    2453             : {
    2454      154222 :   pari_sp av = avma;
    2455      154222 :   GEN G, aI, IZ, J, y, yZ, my, c1 = NULL;
    2456             :   long N;
    2457             : 
    2458      154222 :   nf = checknf(nf);
    2459      154222 :   N = nf_get_degree(nf);
    2460             :   /* put first for sanity checks, unused when I obviously principal */
    2461      154222 :   G = nf_get_Gtwist(nf, vdir);
    2462      154215 :   switch (idealtyp(&I,&aI))
    2463             :   {
    2464             :     case id_PRIME:
    2465       24390 :       if (pr_is_inert(I)) {
    2466         581 :         if (!aI) { set_avma(av); return matid(N); }
    2467         581 :         c1 = gel(I,1); I = matid(N);
    2468         581 :         goto END;
    2469             :       }
    2470       23809 :       IZ = pr_get_p(I);
    2471       23809 :       J = pr_inv_p(I);
    2472       23809 :       I = idealhnf_two(nf,I);
    2473       23809 :       break;
    2474             :     case id_MAT:
    2475      129797 :       I = Q_primitive_part(I, &c1);
    2476      129797 :       IZ = gcoeff(I,1,1);
    2477      129797 :       if (is_pm1(IZ))
    2478             :       {
    2479        8484 :         if (!aI) { set_avma(av); return matid(N); }
    2480        8435 :         goto END;
    2481             :       }
    2482      121313 :       J = idealHNF_inv_Z(nf, I);
    2483      121313 :       break;
    2484             :     default: /* id_PRINCIPAL, silly case */
    2485          21 :       if (gequal0(I)) I = cgetg(1,t_MAT); else { c1 = I; I = matid(N); }
    2486          21 :       if (!aI) return I;
    2487          14 :       goto END;
    2488             :   }
    2489             :   /* now I integral, HNF; and J = (I\cap Z) I^(-1), integral */
    2490      145122 :   y = idealpseudomin(J, G); /* small elt in (I\cap Z)I^(-1), integral */
    2491      145122 :   if (ZV_isscalar(y))
    2492             :   { /* already reduced */
    2493       54551 :     if (!aI) return gerepilecopy(av, I);
    2494       54096 :     goto END;
    2495             :   }
    2496             : 
    2497       90571 :   my = zk_multable(nf, y);
    2498       90571 :   I = ZM_Z_divexact(ZM_mul(my, I), IZ); /* y I / (I\cap Z), integral */
    2499       90571 :   c1 = mul_content(c1, IZ);
    2500       90571 :   my = ZM_gauss(my, col_ei(N,1)); /* y^-1 */
    2501       90571 :   yZ = Q_denom(my); /* (y) \cap Z */
    2502       90571 :   I = hnfmodid(I, yZ);
    2503       90571 :   if (!aI) return gerepileupto(av, I);
    2504       89136 :   c1 = RgC_Rg_mul(my, c1);
    2505             : END:
    2506      152262 :   if (c1) aI = ext_mul(nf, aI,c1);
    2507      152262 :   return gerepilecopy(av, mkvec2(I, aI));
    2508             : }
    2509             : 
    2510             : GEN
    2511           7 : idealmin(GEN nf, GEN x, GEN vdir)
    2512             : {
    2513           7 :   pari_sp av = avma;
    2514             :   GEN y, dx;
    2515           7 :   nf = checknf(nf);
    2516           7 :   switch( idealtyp(&x,&y) )
    2517             :   {
    2518           0 :     case id_PRINCIPAL: return gcopy(x);
    2519           0 :     case id_PRIME: x = pr_hnf(nf,x); break;
    2520           7 :     case id_MAT: if (lg(x) == 1) return gen_0;
    2521             :   }
    2522           7 :   x = Q_remove_denom(x, &dx);
    2523           7 :   y = idealpseudomin(x, nf_get_Gtwist(nf,vdir));
    2524           7 :   if (dx) y = RgC_Rg_div(y, dx);
    2525           7 :   return gerepileupto(av, y);
    2526             : }
    2527             : 
    2528             : /*******************************************************************/
    2529             : /*                                                                 */
    2530             : /*                   APPROXIMATION THEOREM                         */
    2531             : /*                                                                 */
    2532             : /*******************************************************************/
    2533             : /* a = ppi(a,b) ppo(a,b), where ppi regroups primes common to a and b
    2534             :  * and ppo(a,b) = Z_ppo(a,b) */
    2535             : /* return gcd(a,b),ppi(a,b),ppo(a,b) */
    2536             : GEN
    2537      454258 : Z_ppio(GEN a, GEN b)
    2538             : {
    2539      454258 :   GEN x, y, d = gcdii(a,b);
    2540      454258 :   if (is_pm1(d)) return mkvec3(gen_1, gen_1, a);
    2541      345296 :   x = d; y = diviiexact(a,d);
    2542             :   for(;;)
    2543       62748 :   {
    2544      408044 :     GEN g = gcdii(x,y);
    2545      408044 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2546       62748 :     x = mulii(x,g); y = diviiexact(y,g);
    2547             :   }
    2548             : }
    2549             : /* a = ppg(a,b)pple(a,b), where ppg regroups primes such that v(a) > v(b)
    2550             :  * and pple all others */
    2551             : /* return gcd(a,b),ppg(a,b),pple(a,b) */
    2552             : GEN
    2553           0 : Z_ppgle(GEN a, GEN b)
    2554             : {
    2555           0 :   GEN x, y, g, d = gcdii(a,b);
    2556           0 :   if (equalii(a, d)) return mkvec3(a, gen_1, a);
    2557           0 :   x = diviiexact(a,d); y = d;
    2558             :   for(;;)
    2559             :   {
    2560           0 :     g = gcdii(x,y);
    2561           0 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2562           0 :     x = mulii(x,g); y = diviiexact(y,g);
    2563             :   }
    2564             : }
    2565             : static void
    2566           0 : Z_dcba_rec(GEN L, GEN a, GEN b)
    2567             : {
    2568             :   GEN x, r, v, g, h, c, c0;
    2569             :   long n;
    2570           0 :   if (is_pm1(b)) {
    2571           0 :     if (!is_pm1(a)) vectrunc_append(L, a);
    2572           0 :     return;
    2573             :   }
    2574           0 :   v = Z_ppio(a,b);
    2575           0 :   a = gel(v,2);
    2576           0 :   r = gel(v,3);
    2577           0 :   if (!is_pm1(r)) vectrunc_append(L, r);
    2578           0 :   v = Z_ppgle(a,b);
    2579           0 :   g = gel(v,1);
    2580           0 :   h = gel(v,2);
    2581           0 :   x = c0 = gel(v,3);
    2582           0 :   for (n = 1; !is_pm1(h); n++)
    2583             :   {
    2584             :     GEN d, y;
    2585             :     long i;
    2586           0 :     v = Z_ppgle(h,sqri(g));
    2587           0 :     g = gel(v,1);
    2588           0 :     h = gel(v,2);
    2589           0 :     c = gel(v,3); if (is_pm1(c)) continue;
    2590           0 :     d = gcdii(c,b);
    2591           0 :     x = mulii(x,d);
    2592           0 :     y = d; for (i=1; i < n; i++) y = sqri(y);
    2593           0 :     Z_dcba_rec(L, diviiexact(c,y), d);
    2594             :   }
    2595           0 :   Z_dcba_rec(L,diviiexact(b,x), c0);
    2596             : }
    2597             : static GEN
    2598     3071838 : Z_cba_rec(GEN L, GEN a, GEN b)
    2599             : {
    2600             :   GEN g;
    2601     3071838 :   if (lg(L) > 10)
    2602             :   { /* a few naive steps before switching to dcba */
    2603           0 :     Z_dcba_rec(L, a, b);
    2604           0 :     return gel(L, lg(L)-1);
    2605             :   }
    2606     3071838 :   if (is_pm1(a)) return b;
    2607     1825229 :   g = gcdii(a,b);
    2608     1825229 :   if (is_pm1(g)) { vectrunc_append(L, a); return b; }
    2609     1363418 :   a = diviiexact(a,g);
    2610     1363418 :   b = diviiexact(b,g);
    2611     1363418 :   return Z_cba_rec(L, Z_cba_rec(L, a, g), b);
    2612             : }
    2613             : GEN
    2614      345002 : Z_cba(GEN a, GEN b)
    2615             : {
    2616      345002 :   GEN L = vectrunc_init(expi(a) + expi(b) + 2);
    2617      345002 :   GEN t = Z_cba_rec(L, a, b);
    2618      345002 :   if (!is_pm1(t)) vectrunc_append(L, t);
    2619      345002 :   return L;
    2620             : }
    2621             : /* P = coprime base, extend it by b; TODO: quadratic for now */
    2622             : GEN
    2623           0 : ZV_cba_extend(GEN P, GEN b)
    2624             : {
    2625           0 :   long i, l = lg(P);
    2626           0 :   GEN w = cgetg(l+1, t_VEC);
    2627           0 :   for (i = 1; i < l; i++)
    2628             :   {
    2629           0 :     GEN v = Z_cba(gel(P,i), b);
    2630           0 :     long nv = lg(v)-1;
    2631           0 :     gel(w,i) = vecslice(v, 1, nv-1); /* those divide P[i] but not b */
    2632           0 :     b = gel(v,nv);
    2633             :   }
    2634           0 :   gel(w,l) = b; return shallowconcat1(w);
    2635             : }
    2636             : GEN
    2637           0 : ZV_cba(GEN v)
    2638             : {
    2639           0 :   long i, l = lg(v);
    2640             :   GEN P;
    2641           0 :   if (l <= 2) return v;
    2642           0 :   P = Z_cba(gel(v,1), gel(v,2));
    2643           0 :   for (i = 3; i < l; i++) P = ZV_cba_extend(P, gel(v,i));
    2644           0 :   return P;
    2645             : }
    2646             : 
    2647             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2648             : GEN
    2649     1787638 : Z_ppo(GEN x, GEN f)
    2650             : {
    2651             :   for (;;)
    2652             :   {
    2653     2772640 :     f = gcdii(x, f); if (is_pm1(f)) break;
    2654      985002 :     x = diviiexact(x, f);
    2655             :   }
    2656      802636 :   return x;
    2657             : }
    2658             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2659             : ulong
    2660    41731203 : u_ppo(ulong x, ulong f)
    2661             : {
    2662             :   for (;;)
    2663             :   {
    2664    49939423 :     f = ugcd(x, f); if (f == 1) break;
    2665     8208220 :     x /= f;
    2666             :   }
    2667    33522983 :   return x;
    2668             : }
    2669             : 
    2670             : /* x t_INT, f ideal. Write x = x1 x2, sqf(x1) | f, (x2,f) = 1. Return x2 */
    2671             : static GEN
    2672         315 : nf_coprime_part(GEN nf, GEN x, GEN listpr)
    2673             : {
    2674         315 :   long v, j, lp = lg(listpr), N = nf_get_degree(nf);
    2675             :   GEN x1, x2, ex;
    2676             : 
    2677             : #if 0 /*1) via many gcds. Expensive ! */
    2678             :   GEN f = idealprodprime(nf, listpr);
    2679             :   f = ZM_hnfmodid(f, x); /* first gcd is less expensive since x in Z */
    2680             :   x = scalarmat(x, N);
    2681             :   for (;;)
    2682             :   {
    2683             :     if (gequal1(gcoeff(f,1,1))) break;
    2684             :     x = idealdivexact(nf, x, f);
    2685             :     f = ZM_hnfmodid(shallowconcat(f,x), gcoeff(x,1,1)); /* gcd(f,x) */
    2686             :   }
    2687             :   x2 = x;
    2688             : #else /*2) from prime decomposition */
    2689         315 :   x1 = NULL;
    2690         889 :   for (j=1; j<lp; j++)
    2691             :   {
    2692         574 :     GEN pr = gel(listpr,j);
    2693         574 :     v = Z_pval(x, pr_get_p(pr)); if (!v) continue;
    2694             : 
    2695         315 :     ex = muluu(v, pr_get_e(pr)); /* = v_pr(x) > 0 */
    2696         315 :     x1 = x1? idealmulpowprime(nf, x1, pr, ex)
    2697         315 :            : idealpow(nf, pr, ex);
    2698             :   }
    2699         315 :   x = scalarmat(x, N);
    2700         315 :   x2 = x1? idealdivexact(nf, x, x1): x;
    2701             : #endif
    2702         315 :   return x2;
    2703             : }
    2704             : 
    2705             : /* L0 in K^*, assume (L0,f) = 1. Return L integral, L0 = L mod f  */
    2706             : GEN
    2707        9457 : make_integral(GEN nf, GEN L0, GEN f, GEN listpr)
    2708             : {
    2709             :   GEN fZ, t, L, D2, d1, d2, d;
    2710             : 
    2711        9457 :   L = Q_remove_denom(L0, &d);
    2712        9457 :   if (!d) return L0;
    2713             : 
    2714             :   /* L0 = L / d, L integral */
    2715        3017 :   fZ = gcoeff(f,1,1);
    2716        3017 :   if (typ(L) == t_INT) return Fp_mul(L, Fp_inv(d, fZ), fZ);
    2717             :   /* Kill denom part coprime to fZ */
    2718        2555 :   d2 = Z_ppo(d, fZ);
    2719        2555 :   t = Fp_inv(d2, fZ); if (!is_pm1(t)) L = ZC_Z_mul(L,t);
    2720        2555 :   if (equalii(d, d2)) return L;
    2721             : 
    2722         315 :   d1 = diviiexact(d, d2);
    2723             :   /* L0 = (L / d1) mod f. d1 not coprime to f
    2724             :    * write (d1) = D1 D2, D2 minimal, (D2,f) = 1. */
    2725         315 :   D2 = nf_coprime_part(nf, d1, listpr);
    2726         315 :   t = idealaddtoone_i(nf, D2, f); /* in D2, 1 mod f */
    2727         315 :   L = nfmuli(nf,t,L);
    2728             : 
    2729             :   /* if (L0, f) = 1, then L in D1 ==> in D1 D2 = (d1) */
    2730         315 :   return Q_div_to_int(L, d1); /* exact division */
    2731             : }
    2732             : 
    2733             : /* assume L is a list of prime ideals. Return the product */
    2734             : GEN
    2735         329 : idealprodprime(GEN nf, GEN L)
    2736             : {
    2737         329 :   long l = lg(L), i;
    2738             :   GEN z;
    2739         329 :   if (l == 1) return matid(nf_get_degree(nf));
    2740         329 :   z = pr_hnf(nf, gel(L,1));
    2741         329 :   for (i=2; i<l; i++) z = idealHNF_mul_two(nf,z, gel(L,i));
    2742         329 :   return z;
    2743             : }
    2744             : 
    2745             : /* optimize for the frequent case I = nfhnf()[2]: lots of them are 1 */
    2746             : GEN
    2747        1043 : idealprod(GEN nf, GEN I)
    2748             : {
    2749        1043 :   long i, l = lg(I);
    2750             :   GEN z;
    2751        1645 :   for (i = 1; i < l; i++)
    2752        1617 :     if (!equali1(gel(I,i))) break;
    2753        1043 :   if (i == l) return gen_1;
    2754        1015 :   z = gel(I,i);
    2755        1015 :   for (i++; i<l; i++) z = idealmul(nf, z, gel(I,i));
    2756        1015 :   return z;
    2757             : }
    2758             : 
    2759             : /* v_pr(idealprod(nf,I)) */
    2760             : long
    2761        2128 : idealprodval(GEN nf, GEN I, GEN pr)
    2762             : {
    2763        2128 :   long i, l = lg(I), v = 0;
    2764       12579 :   for (i = 1; i < l; i++)
    2765       10451 :     if (!equali1(gel(I,i))) v += idealval(nf, gel(I,i), pr);
    2766        2128 :   return v;
    2767             : }
    2768             : 
    2769             : /* assume L is a list of prime ideals. Return prod L[i]^e[i] */
    2770             : GEN
    2771       10717 : factorbackprime(GEN nf, GEN L, GEN e)
    2772             : {
    2773       10717 :   long l = lg(L), i;
    2774             :   GEN z;
    2775             : 
    2776       10717 :   if (l == 1) return matid(nf_get_degree(nf));
    2777       10703 :   z = idealpow(nf, gel(L,1), gel(e,1));
    2778       17927 :   for (i=2; i<l; i++)
    2779        7224 :     if (signe(gel(e,i))) z = idealmulpowprime(nf,z, gel(L,i),gel(e,i));
    2780       10703 :   return z;
    2781             : }
    2782             : 
    2783             : /* F in Z, divisible exactly by pr.p. Return F-uniformizer for pr, i.e.
    2784             :  * a t in Z_K such that v_pr(t) = 1 and (t, F/pr) = 1 */
    2785             : GEN
    2786       24475 : pr_uniformizer(GEN pr, GEN F)
    2787             : {
    2788       24475 :   GEN p = pr_get_p(pr), t = pr_get_gen(pr);
    2789       24475 :   if (!equalii(F, p))
    2790             :   {
    2791       10924 :     long e = pr_get_e(pr);
    2792       10924 :     GEN u, v, q = (e == 1)? sqri(p): p;
    2793       10924 :     u = mulii(q, Fp_inv(q, diviiexact(F,p))); /* 1 mod F/p, 0 mod q */
    2794       10924 :     v = subui(1UL, u); /* 0 mod F/p, 1 mod q */
    2795       10924 :     if (pr_is_inert(pr))
    2796           0 :       t = addii(mulii(p, v), u);
    2797             :     else
    2798             :     {
    2799       10924 :       t = ZC_Z_mul(t, v);
    2800       10924 :       gel(t,1) = addii(gel(t,1), u); /* return u + vt */
    2801             :     }
    2802             :   }
    2803       24475 :   return t;
    2804             : }
    2805             : /* L = list of prime ideals, return lcm_i (L[i] \cap \ZM) */
    2806             : GEN
    2807       55972 : prV_lcm_capZ(GEN L)
    2808             : {
    2809       55972 :   long i, r = lg(L);
    2810             :   GEN F;
    2811       55972 :   if (r == 1) return gen_1;
    2812       46949 :   F = pr_get_p(gel(L,1));
    2813       71774 :   for (i = 2; i < r; i++)
    2814             :   {
    2815       24825 :     GEN pr = gel(L,i), p = pr_get_p(pr);
    2816       24825 :     if (!dvdii(F, p)) F = mulii(F,p);
    2817             :   }
    2818       46949 :   return F;
    2819             : }
    2820             : 
    2821             : /* Given a prime ideal factorization with possibly zero or negative
    2822             :  * exponents, gives b such that v_p(b) = v_p(x) for all prime ideals pr | x
    2823             :  * and v_pr(b) >= 0 for all other pr.
    2824             :  * For optimal performance, all [anti-]uniformizers should be precomputed,
    2825             :  * but no support for this yet.
    2826             :  *
    2827             :  * If nored, do not reduce result.
    2828             :  * No garbage collecting */
    2829             : static GEN
    2830       30184 : idealapprfact_i(GEN nf, GEN x, int nored)
    2831             : {
    2832             :   GEN z, d, L, e, e2, F;
    2833             :   long i, r;
    2834             :   int flagden;
    2835             : 
    2836       30184 :   nf = checknf(nf);
    2837       30184 :   L = gel(x,1);
    2838       30184 :   e = gel(x,2);
    2839       30184 :   F = prV_lcm_capZ(L);
    2840       30184 :   flagden = 0;
    2841       30184 :   z = NULL; r = lg(e);
    2842       63318 :   for (i = 1; i < r; i++)
    2843             :   {
    2844       33134 :     long s = signe(gel(e,i));
    2845             :     GEN pi, q;
    2846       33134 :     if (!s) continue;
    2847       20331 :     if (s < 0) flagden = 1;
    2848       20331 :     pi = pr_uniformizer(gel(L,i), F);
    2849       20331 :     q = nfpow(nf, pi, gel(e,i));
    2850       20331 :     z = z? nfmul(nf, z, q): q;
    2851             :   }
    2852       30184 :   if (!z) return gen_1;
    2853       14091 :   if (nored || typ(z) != t_COL) return z;
    2854        4683 :   e2 = cgetg(r, t_VEC);
    2855        4683 :   for (i=1; i<r; i++) gel(e2,i) = addiu(gel(e,i), 1);
    2856        4683 :   x = factorbackprime(nf, L,e2);
    2857        4683 :   if (flagden) /* denominator */
    2858             :   {
    2859        4669 :     z = Q_remove_denom(z, &d);
    2860        4669 :     d = diviiexact(d, Z_ppo(d, F));
    2861        4669 :     x = RgM_Rg_mul(x, d);
    2862             :   }
    2863             :   else
    2864          14 :     d = NULL;
    2865        4683 :   z = ZC_reducemodlll(z, x);
    2866        4683 :   return d? RgC_Rg_div(z,d): z;
    2867             : }
    2868             : 
    2869             : GEN
    2870           0 : idealapprfact(GEN nf, GEN x) {
    2871           0 :   pari_sp av = avma;
    2872           0 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    2873             : }
    2874             : GEN
    2875          14 : idealappr(GEN nf, GEN x) {
    2876          14 :   pari_sp av = avma;
    2877          14 :   if (!is_nf_extfactor(x)) x = idealfactor(nf, x);
    2878          14 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    2879             : }
    2880             : 
    2881             : /* OBSOLETE */
    2882             : GEN
    2883          14 : idealappr0(GEN nf, GEN x, long fl) { (void)fl; return idealappr(nf, x); }
    2884             : 
    2885             : static GEN
    2886          21 : mat_ideal_two_elt2(GEN nf, GEN x, GEN a)
    2887             : {
    2888          21 :   GEN F = idealfactor(nf,a), P = gel(F,1), E = gel(F,2);
    2889          21 :   long i, r = lg(E);
    2890          21 :   for (i=1; i<r; i++) gel(E,i) = stoi( idealval(nf,x,gel(P,i)) );
    2891          21 :   return idealapprfact_i(nf,F,1);
    2892             : }
    2893             : 
    2894             : static void
    2895          14 : not_in_ideal(GEN a) {
    2896          14 :   pari_err_DOMAIN("idealtwoelt2","element mod ideal", "!=", gen_0, a);
    2897           0 : }
    2898             : /* x integral in HNF, a an 'nf' */
    2899             : static int
    2900          28 : in_ideal(GEN x, GEN a)
    2901             : {
    2902          28 :   switch(typ(a))
    2903             :   {
    2904          14 :     case t_INT: return dvdii(a, gcoeff(x,1,1));
    2905           7 :     case t_COL: return RgV_is_ZV(a) && !!hnf_invimage(x, a);
    2906           7 :     default: return 0;
    2907             :   }
    2908             : }
    2909             : 
    2910             : /* Given an integral ideal x and a in x, gives a b such that
    2911             :  * x = aZ_K + bZ_K using the approximation theorem */
    2912             : GEN
    2913          42 : idealtwoelt2(GEN nf, GEN x, GEN a)
    2914             : {
    2915          42 :   pari_sp av = avma;
    2916             :   GEN cx, b;
    2917             : 
    2918          42 :   nf = checknf(nf);
    2919          42 :   a = nf_to_scalar_or_basis(nf, a);
    2920          42 :   x = idealhnf_shallow(nf,x);
    2921          42 :   if (lg(x) == 1)
    2922             :   {
    2923          14 :     if (!isintzero(a)) not_in_ideal(a);
    2924           7 :     set_avma(av); return gen_0;
    2925             :   }
    2926          28 :   x = Q_primitive_part(x, &cx);
    2927          28 :   if (cx) a = gdiv(a, cx);
    2928          28 :   if (!in_ideal(x, a)) not_in_ideal(a);
    2929          21 :   b = mat_ideal_two_elt2(nf, x, a);
    2930          21 :   if (typ(b) == t_COL)
    2931             :   {
    2932          14 :     GEN mod = idealhnf_principal(nf,a);
    2933          14 :     b = ZC_hnfrem(b,mod);
    2934          14 :     if (ZV_isscalar(b)) b = gel(b,1);
    2935             :   }
    2936             :   else
    2937             :   {
    2938           7 :     GEN aZ = typ(a) == t_COL? Q_denom(zk_inv(nf,a)): a; /* (a) \cap Z */
    2939           7 :     b = centermodii(b, aZ, shifti(aZ,-1));
    2940             :   }
    2941          21 :   b = cx? gmul(b,cx): gcopy(b);
    2942          21 :   return gerepileupto(av, b);
    2943             : }
    2944             : 
    2945             : /* Given 2 integral ideals x and y in nf, returns a beta in nf such that
    2946             :  * beta * x is an integral ideal coprime to y */
    2947             : GEN
    2948       20755 : idealcoprimefact(GEN nf, GEN x, GEN fy)
    2949             : {
    2950       20755 :   GEN L = gel(fy,1), e;
    2951       20755 :   long i, r = lg(L);
    2952             : 
    2953       20755 :   e = cgetg(r, t_COL);
    2954       20755 :   for (i=1; i<r; i++) gel(e,i) = stoi( -idealval(nf,x,gel(L,i)) );
    2955       20755 :   return idealapprfact_i(nf, mkmat2(L,e), 0);
    2956             : }
    2957             : GEN
    2958          63 : idealcoprime(GEN nf, GEN x, GEN y)
    2959             : {
    2960          63 :   pari_sp av = avma;
    2961          63 :   return gerepileupto(av, idealcoprimefact(nf, x, idealfactor(nf,y)));
    2962             : }
    2963             : 
    2964             : GEN
    2965           7 : nfmulmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    2966             : {
    2967           7 :   pari_sp av = avma;
    2968           7 :   GEN z, p, pr = modpr, T;
    2969             : 
    2970           7 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    2971           0 :   x = nf_to_Fq(nf,x,modpr);
    2972           0 :   y = nf_to_Fq(nf,y,modpr);
    2973           0 :   z = Fq_mul(x,y,T,p);
    2974           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    2975             : }
    2976             : 
    2977             : GEN
    2978           0 : nfdivmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    2979             : {
    2980           0 :   pari_sp av = avma;
    2981           0 :   nf = checknf(nf);
    2982           0 :   return gerepileupto(av, nfreducemodpr(nf, nfdiv(nf,x,y), modpr));
    2983             : }
    2984             : 
    2985             : GEN
    2986           0 : nfpowmodpr(GEN nf, GEN x, GEN k, GEN modpr)
    2987             : {
    2988           0 :   pari_sp av=avma;
    2989           0 :   GEN z, T, p, pr = modpr;
    2990             : 
    2991           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    2992           0 :   z = nf_to_Fq(nf,x,modpr);
    2993           0 :   z = Fq_pow(z,k,T,p);
    2994           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    2995             : }
    2996             : 
    2997             : GEN
    2998           0 : nfkermodpr(GEN nf, GEN x, GEN modpr)
    2999             : {
    3000           0 :   pari_sp av = avma;
    3001           0 :   GEN T, p, pr = modpr;
    3002             : 
    3003           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    3004           0 :   if (typ(x)!=t_MAT) pari_err_TYPE("nfkermodpr",x);
    3005           0 :   x = nfM_to_FqM(x, nf, modpr);
    3006           0 :   return gerepilecopy(av, FqM_to_nfM(FqM_ker(x,T,p), modpr));
    3007             : }
    3008             : 
    3009             : GEN
    3010           0 : nfsolvemodpr(GEN nf, GEN a, GEN b, GEN pr)
    3011             : {
    3012           0 :   const char *f = "nfsolvemodpr";
    3013           0 :   pari_sp av = avma;
    3014             :   GEN T, p, modpr;
    3015             : 
    3016           0 :   nf = checknf(nf);
    3017           0 :   modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    3018           0 :   if (typ(a)!=t_MAT) pari_err_TYPE(f,a);
    3019           0 :   a = nfM_to_FqM(a, nf, modpr);
    3020           0 :   switch(typ(b))
    3021             :   {
    3022             :     case t_MAT:
    3023           0 :       b = nfM_to_FqM(b, nf, modpr);
    3024           0 :       b = FqM_gauss(a,b,T,p);
    3025           0 :       if (!b) pari_err_INV(f,a);
    3026           0 :       a = FqM_to_nfM(b, modpr);
    3027           0 :       break;
    3028             :     case t_COL:
    3029           0 :       b = nfV_to_FqV(b, nf, modpr);
    3030           0 :       b = FqM_FqC_gauss(a,b,T,p);
    3031           0 :       if (!b) pari_err_INV(f,a);
    3032           0 :       a = FqV_to_nfV(b, modpr);
    3033           0 :       break;
    3034           0 :     default: pari_err_TYPE(f,b);
    3035             :   }
    3036           0 :   return gerepilecopy(av, a);
    3037             : }

Generated by: LCOV version 1.13