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.0 lcov report (development 23724-b3bdf5af3) Lines: 1491 1661 89.8 %
Date: 2019-03-25 05:45:24 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     4832226 : idealtyp(GEN *ideal, GEN *arch)
      44             : {
      45     4832226 :   GEN x = *ideal;
      46     4832226 :   long t,lx,tx = typ(x);
      47             : 
      48     4832226 :   if (tx!=t_VEC || lg(x)!=3) *arch = NULL;
      49             :   else
      50             :   {
      51      255887 :     GEN a = gel(x,2);
      52      255887 :     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      255880 :     *arch = a;
      58      255880 :     x = gel(x,1); tx = typ(x);
      59             :   }
      60     4832219 :   switch(tx)
      61             :   {
      62     1835520 :     case t_MAT: lx = lg(x);
      63     1835520 :       if (lx == 1) { t = id_PRINCIPAL; x = gen_0; break; }
      64     1835461 :       if (lx != lgcols(x)) pari_err_TYPE("idealtyp [non-square t_MAT]",x);
      65     1835450 :       t = id_MAT;
      66     1835450 :       break;
      67             : 
      68     2527194 :     case t_VEC: if (lg(x)!=6) pari_err_TYPE("idealtyp",x);
      69     2527181 :       t = id_PRIME; break;
      70             : 
      71             :     case t_POL: case t_POLMOD: case t_COL:
      72             :     case t_INT: case t_FRAC:
      73      469505 :       t = id_PRINCIPAL; break;
      74             :     default:
      75           0 :       pari_err_TYPE("idealtyp",x);
      76             :       return 0; /*LCOV_EXCL_LINE*/
      77             :   }
      78     4832195 :   *ideal = x; return t;
      79             : }
      80             : 
      81             : /* true nf; v = [a,x,...], a in Z. Return (a,x) */
      82             : GEN
      83      149731 : idealhnf_two(GEN nf, GEN v)
      84             : {
      85      149731 :   GEN p = gel(v,1), pi = gel(v,2), m = zk_scalar_or_multable(nf, pi);
      86      149731 :   if (typ(m) == t_INT) return scalarmat(gcdii(m,p), nf_get_degree(nf));
      87      131679 :   return ZM_hnfmodid(m, p);
      88             : }
      89             : /* true nf */
      90             : GEN
      91     1940070 : pr_hnf(GEN nf, GEN pr)
      92             : {
      93     1940070 :   GEN p = pr_get_p(pr), m;
      94     1940064 :   if (pr_is_inert(pr)) return scalarmat(p, nf_get_degree(nf));
      95     1675974 :   m = zk_scalar_or_multable(nf, pr_get_gen(pr));
      96     1675915 :   return ZM_hnfmodprime(m, p);
      97             : }
      98             : 
      99             : GEN
     100      289677 : idealhnf_principal(GEN nf, GEN x)
     101             : {
     102             :   GEN cx;
     103      289677 :   x = nf_to_scalar_or_basis(nf, x);
     104      289677 :   switch(typ(x))
     105             :   {
     106      163088 :     case t_COL: break;
     107       99829 :     case t_INT:  if (!signe(x)) return cgetg(1,t_MAT);
     108       99409 :       return scalarmat(absi_shallow(x), nf_get_degree(nf));
     109             :     case t_FRAC:
     110       26760 :       return scalarmat(Q_abs_shallow(x), nf_get_degree(nf));
     111           0 :     default: pari_err_TYPE("idealhnf",x);
     112             :   }
     113      163088 :   x = Q_primitive_part(x, &cx);
     114      163088 :   RgV_check_ZV(x, "idealhnf");
     115      163088 :   x = zk_multable(nf, x);
     116      163088 :   x = ZM_hnfmodid(x, zkmultable_capZ(x));
     117      163088 :   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      370980 : idealhnf_shallow(GEN nf, GEN x)
     133             : {
     134      370980 :   long tx = typ(x), lx = lg(x), N;
     135             : 
     136             :   /* cannot use idealtyp because here we allow non-square matrices */
     137      370980 :   if (tx == t_VEC && lx == 3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
     138      370980 :   if (tx == t_VEC && lx == 6) return pr_hnf(nf,x); /* PRIME */
     139      255981 :   switch(tx)
     140             :   {
     141             :     case t_MAT:
     142             :     {
     143             :       GEN cx;
     144       70217 :       long nx = lx-1;
     145       70217 :       N = nf_get_degree(nf);
     146       70217 :       if (nx == 0) return cgetg(1, t_MAT);
     147       70196 :       if (nbrows(x) != N) pari_err_TYPE("idealhnf [wrong dimension]",x);
     148       70189 :       if (nx == 1) return idealhnf_principal(nf, gel(x,1));
     149             : 
     150       68593 :       if (nx == N && RgM_is_ZM(x) && ZM_ishnf(x)) return x;
     151       40502 :       x = Q_primitive_part(x, &cx);
     152       40502 :       if (nx < N) x = vec_mulid(nf, x, nx, N);
     153       40502 :       x = ZM_hnfmod(x, ZM_detmult(x));
     154       40502 :       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      185750 :     default: return idealhnf_principal(nf, x); /* PRINCIPAL */
     178             :   }
     179             : }
     180             : GEN
     181        7728 : idealhnf(GEN nf, GEN x)
     182             : {
     183        7728 :   pari_sp av = avma;
     184        7728 :   GEN y = idealhnf_shallow(checknf(nf), x);
     185        7714 :   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      148730 : ok_elt(GEN x, GEN xZ, GEN y)
     310             : {
     311      148730 :   pari_sp av = avma;
     312      148730 :   return gc_bool(av, ZM_equal(x, ZM_hnfmodid(y, xZ)));
     313             : }
     314             : 
     315             : static GEN
     316       59127 : addmul_col(GEN a, long s, GEN b)
     317             : {
     318             :   long i,l;
     319       59127 :   if (!s) return a? leafcopy(a): a;
     320       58938 :   if (!a) return gmulsg(s,b);
     321       55621 :   l = lg(a);
     322      290425 :   for (i=1; i<l; i++)
     323      234804 :     if (signe(gel(b,i))) gel(a,i) = addii(gel(a,i), mulsi(s, gel(b,i)));
     324       55621 :   return a;
     325             : }
     326             : 
     327             : /* a <-- a + s * b, all coeffs integers */
     328             : static GEN
     329       25691 : addmul_mat(GEN a, long s, GEN b)
     330             : {
     331             :   long j,l;
     332             :   /* copy otherwise next call corrupts a */
     333       25691 :   if (!s) return a? RgM_shallowcopy(a): a;
     334       24033 :   if (!a) return gmulsg(s,b);
     335       13239 :   l = lg(a);
     336       63780 :   for (j=1; j<l; j++)
     337       50541 :     (void)addmul_col(gel(a,j), s, gel(b,j));
     338       13239 :   return a;
     339             : }
     340             : 
     341             : static GEN
     342       78028 : get_random_a(GEN nf, GEN x, GEN xZ)
     343             : {
     344             :   pari_sp av;
     345       78028 :   long i, lm, l = lg(x);
     346             :   GEN a, z, beta, mul;
     347             : 
     348       78028 :   beta= cgetg(l, t_VEC);
     349       78028 :   mul = cgetg(l, t_VEC); lm = 1; /* = lg(mul) */
     350             :   /* look for a in x such that a O/xZ = x O/xZ */
     351      154870 :   for (i = 2; i < l; i++)
     352             :   {
     353      151553 :     GEN xi = gel(x,i);
     354      151553 :     GEN t = FpM_red(zk_multable(nf,xi), xZ); /* ZM, cannot be a scalar */
     355      151553 :     if (gequal0(t)) continue;
     356      137936 :     if (ok_elt(x,xZ, t)) return xi;
     357       63225 :     gel(beta,lm) = xi;
     358             :     /* mul[i] = { canonical generators for x[i] O/xZ as Z-module } */
     359       63225 :     gel(mul,lm) = t; lm++;
     360             :   }
     361        3317 :   setlg(mul, lm);
     362        3317 :   setlg(beta,lm);
     363        3317 :   z = cgetg(lm, t_VECSMALL);
     364       10808 :   for(av = avma;; set_avma(av))
     365             :   {
     366       43990 :     for (a=NULL,i=1; i<lm; i++)
     367             :     {
     368       25691 :       long t = random_bits(4) - 7; /* in [-7,8] */
     369       25691 :       z[i] = t;
     370       25691 :       a = addmul_mat(a, t, gel(mul,i));
     371             :     }
     372             :     /* a = matrix (NOT HNF) of ideal generated by beta.z in O/xZ */
     373       10808 :     if (a && ok_elt(x,xZ, a)) break;
     374             :   }
     375       11903 :   for (a=NULL,i=1; i<lm; i++)
     376        8586 :     a = addmul_col(a, z[i], gel(beta,i));
     377        3317 :   return a;
     378             : }
     379             : 
     380             : /* x square matrix, assume it is HNF */
     381             : static GEN
     382      188032 : mat_ideal_two_elt(GEN nf, GEN x)
     383             : {
     384             :   GEN y, a, cx, xZ;
     385      188032 :   long N = nf_get_degree(nf);
     386             :   pari_sp av, tetpil;
     387             : 
     388      188032 :   if (lg(x)-1 != N) pari_err_DIM("idealtwoelt");
     389      188018 :   if (N == 2) return mkvec2copy(gcoeff(x,1,1), gel(x,2));
     390             : 
     391       88959 :   y = cgetg(3,t_VEC); av = avma;
     392       88959 :   cx = Q_content(x);
     393       88959 :   xZ = gcoeff(x,1,1);
     394       88959 :   if (gequal(xZ, cx)) /* x = (cx) */
     395             :   {
     396        3577 :     gel(y,1) = cx;
     397        3577 :     gel(y,2) = gen_0; return y;
     398             :   }
     399       85382 :   if (equali1(cx)) cx = NULL;
     400             :   else
     401             :   {
     402        1883 :     x = Q_div_to_int(x, cx);
     403        1883 :     xZ = gcoeff(x,1,1);
     404             :   }
     405       85382 :   if (N < 6)
     406       72555 :     a = get_random_a(nf, x, xZ);
     407             :   else
     408             :   {
     409       12827 :     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       12827 :     GEN P, E, a1 = Z_smoothen(xZ, (GEN)FB, &P, &E);
     413       12827 :     if (!a1) /* factors completely */
     414        7354 :       a = idealapprfact_i(nf, idealfactor(nf,x), 1);
     415        5473 :     else if (lg(P) == 1) /* no small factors */
     416        4030 :       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        1443 :       a0 = diviiexact(xZ, a1);
     421        1443 :       A0 = ZM_hnfmodid(x, a0); /* smooth part of x */
     422        1443 :       A1 = ZM_hnfmodid(x, a1); /* cofactor */
     423        1443 :       pi0 = idealapprfact_i(nf, idealfactor(nf,A0), 1);
     424        1443 :       pi1 = get_random_a(nf, A1, a1);
     425        1443 :       (void)bezout(a0, a1, &v0,&v1);
     426        1443 :       u0 = mulii(a0, v0);
     427        1443 :       u1 = mulii(a1, v1);
     428        1443 :       if (typ(pi0) != t_COL) t = addmulii(u0, pi0, u1);
     429             :       else
     430        1443 :       { t = ZC_Z_mul(pi0, u1); gel(t,1) = addii(gel(t,1), u0); }
     431        1443 :       u = ZC_Z_mul(pi1, u0); gel(u,1) = addii(gel(u,1), u1);
     432        1443 :       a = nfmuli(nf, centermod(u, xZ), centermod(t, xZ));
     433             :     }
     434             :   }
     435       85382 :   if (cx)
     436             :   {
     437        1883 :     a = centermod(a, xZ);
     438        1883 :     tetpil = avma;
     439        1883 :     if (typ(cx) == t_INT)
     440             :     {
     441         469 :       gel(y,1) = mulii(xZ, cx);
     442         469 :       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       83499 :     tetpil = avma;
     453       83499 :     gel(y,1) = icopy(xZ);
     454       83499 :     gel(y,2) = centermod(a, xZ);
     455             :   }
     456       85382 :   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       59749 : idealtwoelt(GEN nf, GEN x)
     465             : {
     466             :   pari_sp av;
     467             :   GEN z;
     468       59749 :   long tx = idealtyp(&x,&z);
     469       59742 :   nf = checknf(nf);
     470       59742 :   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      249852 : idealHNF_norm_pval(GEN x, GEN p, long Zval)
     486             : {
     487      249852 :   long i, v = Zval, l = lg(x);
     488      249852 :   for (i = 2; i < l; i++) v += Z_pval(gcoeff(x,i,i), p);
     489      249852 :   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       58826 : idealHNF_Z_factor_i(GEN x, GEN f0, GEN *pvN, GEN *pvZ)
     496             : {
     497       58826 :   GEN P, E, vN, vZ, xZ = gcoeff(x,1,1), f = f0? f0: Z_factor(xZ);
     498             :   long i, l;
     499       58826 :   P = gel(f,1); l = lg(P);
     500       58826 :   E = gel(f,2);
     501       58826 :   *pvN = vN = cgetg(l, t_VECSMALL);
     502       58826 :   *pvZ = vZ = cgetg(l, t_VECSMALL);
     503      113438 :   for (i = 1; i < l; i++)
     504             :   {
     505       54612 :     GEN p = gel(P,i);
     506       54612 :     vZ[i] = f0? Z_pval(xZ, p): itou(gel(E,i));
     507       54612 :     vN[i] = idealHNF_norm_pval(x,p, vZ[i]);
     508             :   }
     509       58826 :   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      272178 : idealHNF_val(GEN A, GEN P, long Nval, long Zval)
     521             : {
     522      272178 :   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      272178 :   if (Nval < f) return 0;
     527      272073 :   p = pr_get_p(P);
     528      272073 :   e = pr_get_e(P);
     529             :   /* v_P(A) <= max [ e * v_p(A \cap Z), floor[v_p(Nix) / f ] */
     530      272073 :   vmax = minss(Zval * e, Nval / f);
     531      272073 :   mul = pr_get_tau(P);
     532      272073 :   l = lg(mul);
     533      272073 :   B = cgetg(l,t_MAT);
     534             :   /* B[1] not needed: v_pr(A[1]) = v_pr(A \cap Z) is known already */
     535      272073 :   gel(B,1) = gen_0; /* dummy */
     536      781783 :   for (j = 2; j < l; j++)
     537             :   {
     538      597318 :     GEN x = gel(A,j);
     539      597318 :     gel(B,j) = y = cgetg(l, t_COL);
     540     4623998 :     for (i = 1; i < l; i++)
     541             :     { /* compute a = (x.t0)_i, A in HNF ==> x[j+1..l-1] = 0 */
     542     4114288 :       a = mulii(gel(x,1), gcoeff(mul,i,1));
     543     4114288 :       for (k = 2; k <= j; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     544             :       /* p | a ? */
     545     4114288 :       gel(y,i) = dvmdii(a,p,&r); if (signe(r)) return 0;
     546             :     }
     547             :   }
     548      184465 :   vals = cgetg(l, t_VECSMALL);
     549             :   /* vals[1] not needed */
     550      632051 :   for (j = 2; j < l; j++)
     551             :   {
     552      447586 :     gel(B,j) = Q_primitive_part(gel(B,j), &cx);
     553      447586 :     vals[j] = cx? 1 + e * Q_pval(cx, p): 1;
     554             :   }
     555      184465 :   pk = powiu(p, ceildivuu(vmax, e));
     556      184465 :   av = avma; y = cgetg(l,t_COL);
     557             :   /* can compute mod p^ceil((vmax-v)/e) */
     558      257847 :   for (v = 1; v < vmax; v++)
     559             :   { /* we know v_pr(Bj) >= v for all j */
     560       77475 :     if (e == 1 || (vmax - v) % e == 0) pk = diviiexact(pk, p);
     561      543270 :     for (j = 2; j < l; j++)
     562             :     {
     563      469888 :       GEN x = gel(B,j); if (v < vals[j]) continue;
     564     4585509 :       for (i = 1; i < l; i++)
     565             :       {
     566     4245147 :         pari_sp av2 = avma;
     567     4245147 :         a = mulii(gel(x,1), gcoeff(mul,i,1));
     568     4245147 :         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     4245147 :         a = dvmdii(a,p,&r); if (signe(r)) return v;
     571     4241054 :         if (lgefint(a) > lgefint(pk)) a = remii(a, pk);
     572     4241054 :         gel(y,i) = gerepileuptoint(av2, a);
     573             :       }
     574      340362 :       gel(B,j) = y; y = x;
     575      340362 :       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      180372 :   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       58826 : idealHNF_factor_i(GEN nf, GEN x, GEN cx, GEN FA)
     589             : {
     590       58826 :   const long N = lg(x)-1;
     591             :   long i, j, k, l, v;
     592       58826 :   GEN vN, vZ, vP, vE, vp = idealHNF_Z_factor_i(x, FA, &vN,&vZ);
     593             : 
     594       58826 :   l = lg(vp);
     595       58826 :   i = cx? expi(cx)+1: 1;
     596       58826 :   vP = cgetg((l+i-2)*N+1, t_COL);
     597       58826 :   vE = cgetg((l+i-2)*N+1, t_COL);
     598      113438 :   for (i = k = 1; i < l; i++)
     599             :   {
     600       54612 :     GEN L, p = gel(vp,i);
     601       54612 :     long Nval = vN[i], Zval = vZ[i], vc = cx? Z_pvalrem(cx,p,&cx): 0;
     602       54612 :     if (vc)
     603             :     {
     604        4571 :       L = idealprimedec(nf,p);
     605        4571 :       if (is_pm1(cx)) cx = NULL;
     606             :     }
     607             :     else
     608       50041 :       L = idealprimedec_limit_f(nf,p,Nval);
     609      131550 :     for (j = 1; Nval && j < lg(L); j++) /* !Nval => only cx contributes */
     610             :     {
     611       76938 :       GEN P = gel(L,j);
     612       76938 :       pari_sp av = avma;
     613       76938 :       v = idealHNF_val(x, P, Nval, Zval);
     614       76938 :       set_avma(av);
     615       76938 :       Nval -= v*pr_get_f(P);
     616       76938 :       v += vc * pr_get_e(P); if (!v) continue;
     617       59477 :       gel(vP,k) = P;
     618       59477 :       gel(vE,k) = utoipos(v); k++;
     619             :     }
     620       57190 :     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       58826 :   if (cx && !FA)
     628             :   { /* complete factorization */
     629       11655 :     GEN f = Z_factor(cx), cP = gel(f,1), cE = gel(f,2);
     630       11655 :     long lc = lg(cP);
     631       24353 :     for (i=1; i<lc; i++)
     632             :     {
     633       12698 :       GEN p = gel(cP,i), L = idealprimedec(nf,p);
     634       12698 :       long vc = itos(gel(cE,i));
     635       28196 :       for (j=1; j<lg(L); j++)
     636             :       {
     637       15498 :         GEN P = gel(L,j);
     638       15498 :         gel(vP,k) = P;
     639       15498 :         gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     640             :       }
     641             :     }
     642             :   }
     643       58826 :   setlg(vP, k);
     644       58826 :   setlg(vE, k); return mkmat2(vP, vE);
     645             : }
     646             : /* true nf, x integral ideal */
     647             : static GEN
     648       57881 : idealHNF_factor(GEN nf, GEN x, ulong lim)
     649             : {
     650       57881 :   GEN cx, F = NULL;
     651       57881 :   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       57881 :   x = Q_primitive_part(x, &cx);
     661       57881 :   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        4172 : prV_e_muls(GEN L, long c)
     666             : {
     667        4172 :   long j, l = lg(L);
     668        4172 :   GEN z = cgetg(l, t_COL);
     669        4172 :   for (j = 1; j < l; j++) gel(z,j) = stoi(c * pr_get_e(gel(L,j)));
     670        4172 :   return z;
     671             : }
     672             : /* true nf, y in Q */
     673             : static GEN
     674        4375 : Q_nffactor(GEN nf, GEN y, ulong lim)
     675             : {
     676             :   GEN f, P, E;
     677             :   long l, i;
     678        4375 :   if (typ(y) == t_INT)
     679             :   {
     680        4347 :     if (!signe(y)) pari_err_DOMAIN("idealfactor", "ideal", "=",gen_0,y);
     681        4333 :     if (is_pm1(y)) return trivial_fact();
     682             :   }
     683        3059 :   y = Q_abs_shallow(y);
     684        3059 :   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        3059 :   P = gel(f,1); l = lg(P); if (l == 1) return f;
     697        3045 :   E = gel(f,2);
     698        7217 :   for (i = 1; i < l; i++)
     699             :   {
     700        4172 :     gel(P,i) = idealprimedec(nf, gel(P,i));
     701        4172 :     gel(E,i) = prV_e_muls(gel(P,i), itos(gel(E,i)));
     702             :   }
     703        3045 :   settyp(P,t_VEC); P = shallowconcat1(P);
     704        3045 :   settyp(E,t_VEC); E = shallowconcat1(E);
     705        3045 :   gel(f,1) = P; settyp(P, t_COL);
     706        3045 :   gel(f,2) = E; return f;
     707             : }
     708             : 
     709             : GEN
     710         448 : idealfactor_partial(GEN nf, GEN x, GEN L)
     711             : {
     712             :   long i, j, l;
     713             :   GEN P, E;
     714         448 :   if (!L) return idealfactor(nf, x);
     715          28 :   if (typ(L) == t_INT) return idealfactor_limit(nf, x, itou(L));
     716           7 :   P = cgetg_copy(L, &l);
     717          35 :   for (i = 1; i < l; i++)
     718             :   {
     719          28 :     GEN p = gel(L,i);
     720          28 :     gel(P,i) = typ(p) == t_INT? idealprimedec(nf, p): mkvec(p);
     721             :   }
     722           7 :   settyp(P, t_VEC); P = shallowconcat1(P);
     723           7 :   settyp(P, t_COL);
     724           7 :   P = gen_sort_uniq(P, (void*)&cmp_prime_over_p, &cmp_nodata);
     725           7 :   E = cgetg_copy(P, &l);
     726          35 :   for (i = j = 1; i < l; i++)
     727             :   {
     728          28 :     long v = idealval(nf, x, gel(P,i));
     729          28 :     if (v) { gel(P,j) = gel(P,i); gel(E,j) = stoi(v); j++; }
     730             :   }
     731           7 :   setlg(P,j);
     732           7 :   setlg(E,j); return mkmat2(P, E);
     733             : }
     734             : GEN
     735       62319 : idealfactor_limit(GEN nf, GEN x, ulong lim)
     736             : {
     737       62319 :   pari_sp av = avma;
     738             :   GEN fa, y;
     739       62319 :   long tx = idealtyp(&x,&y);
     740             : 
     741       62319 :   nf = checknf(nf);
     742       62319 :   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       62249 :   if (tx == id_PRINCIPAL)
     748             :   {
     749        8246 :     y = nf_to_scalar_or_basis(nf, x);
     750        8246 :     if (typ(y) != t_COL) return gerepilecopy(av, Q_nffactor(nf, y, lim));
     751             :   }
     752       57874 :   y = idealnumden(nf, x);
     753       57874 :   fa = idealHNF_factor(nf, gel(y,1), lim);
     754       57874 :   if (!isint1(gel(y,2)))
     755           7 :     fa = famat_div_shallow(fa, idealHNF_factor(nf, gel(y,2), lim));
     756       57874 :   fa = gerepilecopy(av, fa);
     757       57874 :   return sort_factor(fa, (void*)&cmp_prime_ideal, &cmp_nodata);
     758             : }
     759             : GEN
     760       62158 : idealfactor(GEN nf, GEN x) { return idealfactor_limit(nf, x, 0); }
     761             : GEN
     762         140 : gpidealfactor(GEN nf, GEN x, GEN lim)
     763             : {
     764         140 :   ulong L = 0;
     765         140 :   if (lim)
     766             :   {
     767          70 :     if (typ(lim) != t_INT || signe(lim) < 0) pari_err_FLAG("idealfactor");
     768          70 :     L = itou(lim);
     769             :   }
     770         140 :   return idealfactor_limit(nf, x, L);
     771             : }
     772             : 
     773             : static GEN
     774         196 : ramified_root(GEN nf, GEN R, GEN A, long n)
     775             : {
     776         196 :   GEN v, P = gel(idealfactor(nf, R), 1);
     777         196 :   long i, l = lg(P);
     778         196 :   v = cgetg(l, t_VECSMALL);
     779         217 :   for (i = 1; i < l; i++)
     780             :   {
     781          21 :     long w = idealval(nf, A, gel(P,i));
     782          21 :     if (w % n) return NULL;
     783          21 :     v[i] = w / n;
     784             :   }
     785         196 :   return idealfactorback(nf, P, v, 0);
     786             : }
     787             : static int
     788           0 : ramified_root_simple(GEN nf, long n, GEN P, GEN v)
     789             : {
     790           0 :   long i, l = lg(v);
     791           0 :   for (i = 1; i < l; i++) if (v[i])
     792             :   {
     793           0 :     GEN vpr = idealprimedec(nf, gel(P,i));
     794           0 :     long lpr = lg(vpr), j;
     795           0 :     for (j = 1; j < lpr; j++)
     796             :     {
     797           0 :       long e = pr_get_e(gel(vpr,j));
     798           0 :       if ((e * v[i]) % n) return 0;
     799             :     }
     800             :   }
     801           0 :   return 1;
     802             : }
     803             : /* true nf; A is assumed to be the n-th power of an integral ideal,
     804             :  * return its n-th root; n > 1 */
     805             : static long
     806         196 : idealsqrtn_int(GEN nf, GEN A, long n, GEN *pB)
     807             : {
     808             :   GEN C, root;
     809             :   long i, l;
     810             : 
     811         196 :   if (typ(A) == t_INT) /* > 0 */
     812             :   {
     813         105 :     GEN P = nf_get_ramified_primes(nf), v, q;
     814         105 :     l = lg(P); v = cgetg(l, t_VECSMALL);
     815         105 :     for (i = 1; i < l; i++) v[i] = Z_pvalrem(A, gel(P,i), &A);
     816         105 :     C = gen_1;
     817         105 :     if (!isint1(A) && !Z_ispowerall(A, n, pB? &C: NULL)) return 0;
     818         105 :     if (!pB) return ramified_root_simple(nf, n, P, v);
     819         105 :     q = factorback2(P, v);
     820         105 :     root = ramified_root(nf, q, q, n);
     821         105 :     if (!root) return 0;
     822         105 :     if (!equali1(C)) root = isint1(root)? C: ZM_Z_mul(root, C);
     823         105 :     *pB = root; return 1;
     824             :   }
     825             :   /* compute valuations at ramified primes */
     826          91 :   root = ramified_root(nf, idealadd(nf, nf_get_diff(nf), A), A, n);
     827             :   /* remove ramified primes */
     828          91 :   if (isint1(root))
     829          77 :     root = matid(nf_get_degree(nf));
     830             :   else
     831          14 :     A = idealdivexact(nf, A, idealpows(nf,root,n));
     832          91 :   A = Q_primitive_part(A, &C);
     833          91 :   if (C)
     834             :   {
     835           0 :     if (!Z_ispowerall(C,n,&C)) return 0;
     836           0 :     if (pB) root = ZM_Z_mul(root, C);
     837             :   }
     838             : 
     839             :   /* compute final n-th root, at most degree(nf)-1 iterations */
     840         168 :   for (i = 0;; i++)
     841          77 :   {
     842         168 :     GEN J, b, a = gcoeff(A,1,1); /* A \cap Z */
     843         168 :     if (is_pm1(a)) break;
     844          91 :     if (!Z_ispowerall(a,n,&b)) return 0;
     845          77 :     J = idealadd(nf, b, A);
     846          77 :     A = idealdivexact(nf, idealpows(nf,J,n), A);
     847          77 :     if (pB) root = odd(i)? idealdivexact(nf, root, J): idealmul(nf, root, J);
     848             :   }
     849         154 :   if (pB) *pB = root;
     850          77 :   return 1;
     851             : }
     852             : 
     853             : /* A is assumed to be the n-th power of an ideal in nf
     854             :  returns its n-th root. */
     855             : long
     856         112 : idealispower(GEN nf, GEN A, long n, GEN *pB)
     857             : {
     858         112 :   pari_sp av = avma;
     859             :   GEN v, N, D;
     860         112 :   nf = checknf(nf);
     861         112 :   if (n <= 0) pari_err_DOMAIN("idealispower", "n", "<=", gen_0, stoi(n));
     862         112 :   if (n == 1) { if (pB) *pB = idealhnf(nf,A); return 1; }
     863         105 :   v = idealnumden(nf,A);
     864         105 :   if (gequal0(gel(v,1))) { set_avma(av); if (pB) *pB = cgetg(1,t_MAT); return 1; }
     865         105 :   if (!idealsqrtn_int(nf, gel(v,1), n, pB? &N: NULL)) return 0;
     866          91 :   if (!idealsqrtn_int(nf, gel(v,2), n, pB? &D: NULL)) return 0;
     867          91 :   if (pB) *pB = gerepileupto(av, idealdiv(nf,N,D)); else set_avma(av);
     868          91 :   return 1;
     869             : }
     870             : 
     871             : /* x t_INT or integral non-0 ideal in HNF */
     872             : static GEN
     873        3892 : idealredmodpower_i(GEN nf, GEN x, ulong k, ulong B)
     874             : {
     875             :   GEN cx, y, U, N, F, Q;
     876             :   long nF;
     877        3892 :   if (typ(x) == t_INT)
     878             :   {
     879        2940 :     if (!signe(x) || is_pm1(x)) return gen_1;
     880         868 :     F = Z_factor_limit(x, B);
     881         868 :     gel(F,2) = gdiventgs(gel(F,2), k);
     882         868 :     return ginv(factorback(F));
     883             :   }
     884         952 :   N = gcoeff(x,1,1); if (is_pm1(N)) return gen_1;
     885         945 :   F = Z_factor_limit(N, B); nF=lg(gel(F,1))-1;
     886         945 :   if (BPSW_psp(gcoeff(F,nF,1))) U = NULL;
     887             :   else
     888             :   {
     889          77 :     GEN M = powii(gcoeff(F,nF,1), gcoeff(F,nF,2));
     890          77 :     y = hnfmodid(x, M); /* coprime part to B! */
     891          77 :     if (!idealispower(nf, y, k, &U)) U = NULL;
     892          77 :     x = hnfmodid(x, diviiexact(N, M));
     893          77 :     setlg(gel(F,1), nF); /* remove last entry (unfactored part) */
     894          77 :     setlg(gel(F,2), nF);
     895             :   }
     896             :   /* x = B-smooth part of initial x */
     897         945 :   x = Q_primitive_part(x, &cx);
     898         945 :   F = idealHNF_factor_i(nf, x, cx, F);
     899         945 :   gel(F,2) = gdiventgs(gel(F,2), k);
     900         945 :   Q = idealfactorback(nf, gel(F,1), gel(F,2), 0);
     901         945 :   if (U) Q = idealmul(nf,Q,U);
     902         945 :   if (typ(Q) == t_INT) return Q;
     903         917 :   y = idealred_elt(nf, idealHNF_inv_Z(nf, Q));
     904         917 :   return gdiv(y, gcoeff(Q,1,1));
     905             : }
     906             : GEN
     907        1953 : idealredmodpower(GEN nf, GEN x, ulong n, ulong B)
     908             : {
     909        1953 :   pari_sp av = avma;
     910             :   GEN a, b;
     911        1953 :   nf = checknf(nf);
     912        1953 :   if (!n) pari_err_DOMAIN("idealredmodpower","n", "=", gen_0, gen_0);
     913        1953 :   x = idealnumden(nf, x);
     914        1953 :   a = gel(x,1);
     915        1953 :   if (isintzero(a)) { set_avma(av); return gen_1; }
     916        1946 :   a = idealredmodpower_i(nf, gel(x,1), n, B);
     917        1946 :   b = idealredmodpower_i(nf, gel(x,2), n, B);
     918        1946 :   if (!isint1(b)) a = nf_to_scalar_or_basis(nf, nfdiv(nf, a, b));
     919        1946 :   return gerepilecopy(av, a);
     920             : }
     921             : 
     922             : /* P prime ideal in idealprimedec format. Return valuation(A) at P */
     923             : long
     924      565865 : idealval(GEN nf, GEN A, GEN P)
     925             : {
     926      565865 :   pari_sp av = avma;
     927             :   GEN a, p, cA;
     928      565865 :   long vcA, v, Zval, tx = idealtyp(&A,&a);
     929             : 
     930      565865 :   if (tx == id_PRINCIPAL) return nfval(nf,A,P);
     931      560783 :   checkprid(P);
     932      560783 :   if (tx == id_PRIME) return pr_equal(P, A)? 1: 0;
     933             :   /* id_MAT */
     934      560755 :   nf = checknf(nf);
     935      560755 :   A = Q_primitive_part(A, &cA);
     936      560755 :   p = pr_get_p(P);
     937      560755 :   vcA = cA? Q_pval(cA,p): 0;
     938      560755 :   if (pr_is_inert(P)) return gc_long(av,vcA);
     939      551081 :   Zval = Z_pval(gcoeff(A,1,1), p);
     940      551081 :   if (!Zval) v = 0;
     941             :   else
     942             :   {
     943      195240 :     long Nval = idealHNF_norm_pval(A, p, Zval);
     944      195240 :     v = idealHNF_val(A, P, Nval, Zval);
     945             :   }
     946      551081 :   return gc_long(av, vcA? v + vcA*pr_get_e(P): v);
     947             : }
     948             : GEN
     949        6587 : gpidealval(GEN nf, GEN ix, GEN P)
     950             : {
     951        6587 :   long v = idealval(nf,ix,P);
     952        6587 :   return v == LONG_MAX? mkoo(): stoi(v);
     953             : }
     954             : 
     955             : /* gcd and generalized Bezout */
     956             : 
     957             : GEN
     958       67536 : idealadd(GEN nf, GEN x, GEN y)
     959             : {
     960       67536 :   pari_sp av = avma;
     961             :   long tx, ty;
     962             :   GEN z, a, dx, dy, dz;
     963             : 
     964       67536 :   tx = idealtyp(&x,&z);
     965       67536 :   ty = idealtyp(&y,&z); nf = checknf(nf);
     966       67536 :   if (tx != id_MAT) x = idealhnf_shallow(nf,x);
     967       67536 :   if (ty != id_MAT) y = idealhnf_shallow(nf,y);
     968       67536 :   if (lg(x) == 1) return gerepilecopy(av,y);
     969       67522 :   if (lg(y) == 1) return gerepilecopy(av,x); /* check for 0 ideal */
     970       67214 :   dx = Q_denom(x);
     971       67214 :   dy = Q_denom(y); dz = lcmii(dx,dy);
     972       67214 :   if (is_pm1(dz)) dz = NULL; else {
     973       12915 :     x = Q_muli_to_int(x, dz);
     974       12915 :     y = Q_muli_to_int(y, dz);
     975             :   }
     976       67214 :   a = gcdii(gcoeff(x,1,1), gcoeff(y,1,1));
     977       67214 :   if (is_pm1(a))
     978             :   {
     979       29315 :     long N = lg(x)-1;
     980       29315 :     if (!dz) { set_avma(av); return matid(N); }
     981        3639 :     return gerepileupto(av, scalarmat(ginv(dz), N));
     982             :   }
     983       37899 :   z = ZM_hnfmodid(shallowconcat(x,y), a);
     984       37899 :   if (dz) z = RgM_Rg_div(z,dz);
     985       37899 :   return gerepileupto(av,z);
     986             : }
     987             : 
     988             : static GEN
     989          28 : trivial_merge(GEN x)
     990          28 : { return (lg(x) == 1 || !is_pm1(gcoeff(x,1,1)))? NULL: gen_1; }
     991             : /* true nf */
     992             : static GEN
     993      164043 : _idealaddtoone(GEN nf, GEN x, GEN y, long red)
     994             : {
     995             :   GEN a;
     996      164043 :   long tx = idealtyp(&x, &a/*junk*/);
     997      164042 :   long ty = idealtyp(&y, &a/*junk*/);
     998             :   long ea;
     999      164031 :   if (tx != id_MAT) x = idealhnf_shallow(nf, x);
    1000      164033 :   if (ty != id_MAT) y = idealhnf_shallow(nf, y);
    1001      164033 :   if (lg(x) == 1)
    1002          14 :     a = trivial_merge(y);
    1003      164019 :   else if (lg(y) == 1)
    1004          14 :     a = trivial_merge(x);
    1005             :   else
    1006      164005 :     a = hnfmerge_get_1(x, y);
    1007      164049 :   if (!a) pari_err_COPRIME("idealaddtoone",x,y);
    1008      164032 :   if (red && (ea = gexpo(a)) > 10)
    1009             :   {
    1010        6652 :     GEN b = (typ(a) == t_COL)? a: scalarcol_shallow(a, nf_get_degree(nf));
    1011        6652 :     b = ZC_reducemodlll(b, idealHNF_mul(nf,x,y));
    1012        6652 :     if (gexpo(b) < ea) a = b;
    1013             :   }
    1014      164032 :   return a;
    1015             : }
    1016             : /* true nf */
    1017             : GEN
    1018       19439 : idealaddtoone_i(GEN nf, GEN x, GEN y)
    1019       19439 : { return _idealaddtoone(nf, x, y, 1); }
    1020             : /* true nf */
    1021             : GEN
    1022      144606 : idealaddtoone_raw(GEN nf, GEN x, GEN y)
    1023      144606 : { return _idealaddtoone(nf, x, y, 0); }
    1024             : 
    1025             : GEN
    1026          98 : idealaddtoone(GEN nf, GEN x, GEN y)
    1027             : {
    1028          98 :   GEN z = cgetg(3,t_VEC), a;
    1029          98 :   pari_sp av = avma;
    1030          98 :   nf = checknf(nf);
    1031          98 :   a = gerepileupto(av, idealaddtoone_i(nf,x,y));
    1032          84 :   gel(z,1) = a;
    1033          84 :   gel(z,2) = typ(a) == t_COL? Z_ZC_sub(gen_1,a): subui(1,a);
    1034          84 :   return z;
    1035             : }
    1036             : 
    1037             : /* assume elements of list are integral ideals */
    1038             : GEN
    1039          35 : idealaddmultoone(GEN nf, GEN list)
    1040             : {
    1041          35 :   pari_sp av = avma;
    1042          35 :   long N, i, l, nz, tx = typ(list);
    1043             :   GEN H, U, perm, L;
    1044             : 
    1045          35 :   nf = checknf(nf); N = nf_get_degree(nf);
    1046          35 :   if (!is_vec_t(tx)) pari_err_TYPE("idealaddmultoone",list);
    1047          35 :   l = lg(list);
    1048          35 :   L = cgetg(l, t_VEC);
    1049          35 :   if (l == 1)
    1050           0 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
    1051          35 :   nz = 0; /* number of non-zero ideals in L */
    1052          98 :   for (i=1; i<l; i++)
    1053             :   {
    1054          70 :     GEN I = gel(list,i);
    1055          70 :     if (typ(I) != t_MAT) I = idealhnf_shallow(nf,I);
    1056          70 :     if (lg(I) != 1)
    1057             :     {
    1058          42 :       nz++; RgM_check_ZM(I,"idealaddmultoone");
    1059          35 :       if (lgcols(I) != N+1) pari_err_TYPE("idealaddmultoone [not an ideal]", I);
    1060             :     }
    1061          63 :     gel(L,i) = I;
    1062             :   }
    1063          28 :   H = ZM_hnfperm(shallowconcat1(L), &U, &perm);
    1064          28 :   if (lg(H) == 1 || !equali1(gcoeff(H,1,1)))
    1065           7 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
    1066          49 :   for (i=1; i<=N; i++)
    1067          49 :     if (perm[i] == 1) break;
    1068          21 :   U = gel(U,(nz-1)*N + i); /* (L[1]|...|L[nz]) U = 1 */
    1069          21 :   nz = 0;
    1070          63 :   for (i=1; i<l; i++)
    1071             :   {
    1072          42 :     GEN c = gel(L,i);
    1073          42 :     if (lg(c) == 1)
    1074          14 :       c = gen_0;
    1075             :     else {
    1076          28 :       c = ZM_ZC_mul(c, vecslice(U, nz*N + 1, (nz+1)*N));
    1077          28 :       nz++;
    1078             :     }
    1079          42 :     gel(L,i) = c;
    1080             :   }
    1081          21 :   return gerepilecopy(av, L);
    1082             : }
    1083             : 
    1084             : /* multiplication */
    1085             : 
    1086             : /* x integral ideal (without archimedean component) in HNF form
    1087             :  * y = [a,alpha] corresponds to the integral ideal aZ_K+alpha Z_K, a in Z,
    1088             :  * alpha a ZV or a ZM (multiplication table). Multiply them */
    1089             : static GEN
    1090     1993688 : idealHNF_mul_two(GEN nf, GEN x, GEN y)
    1091             : {
    1092     1993688 :   GEN m, a = gel(y,1), alpha = gel(y,2);
    1093             :   long i, N;
    1094             : 
    1095     1993688 :   if (typ(alpha) != t_MAT)
    1096             :   {
    1097     1667079 :     alpha = zk_scalar_or_multable(nf, alpha);
    1098     1667079 :     if (typ(alpha) == t_INT) /* e.g. y inert ? 0 should not (but may) occur */
    1099        4229 :       return signe(a)? ZM_Z_mul(x, gcdii(a, alpha)): cgetg(1,t_MAT);
    1100             :   }
    1101     1989459 :   N = lg(x)-1; m = cgetg((N<<1)+1,t_MAT);
    1102     1989459 :   for (i=1; i<=N; i++) gel(m,i)   = ZM_ZC_mul(alpha,gel(x,i));
    1103     1989459 :   for (i=1; i<=N; i++) gel(m,i+N) = ZC_Z_mul(gel(x,i), a);
    1104     1989459 :   return ZM_hnfmodid(m, mulii(a, gcoeff(x,1,1)));
    1105             : }
    1106             : 
    1107             : /* Assume ix and iy are integral in HNF form [NOT extended]. Not memory clean.
    1108             :  * HACK: ideal in iy can be of the form [a,b], a in Z, b in Z_K */
    1109             : GEN
    1110      988138 : idealHNF_mul(GEN nf, GEN x, GEN y)
    1111             : {
    1112             :   GEN z;
    1113      988138 :   if (typ(y) == t_VEC)
    1114      891050 :     z = idealHNF_mul_two(nf,x,y);
    1115             :   else
    1116             :   { /* reduce one ideal to two-elt form. The smallest */
    1117       97088 :     GEN xZ = gcoeff(x,1,1), yZ = gcoeff(y,1,1);
    1118       97088 :     if (cmpii(xZ, yZ) < 0)
    1119             :     {
    1120       34071 :       if (is_pm1(xZ)) return gcopy(y);
    1121       21508 :       z = idealHNF_mul_two(nf, y, mat_ideal_two_elt(nf,x));
    1122             :     }
    1123             :     else
    1124             :     {
    1125       63017 :       if (is_pm1(yZ)) return gcopy(x);
    1126       32189 :       z = idealHNF_mul_two(nf, x, mat_ideal_two_elt(nf,y));
    1127             :     }
    1128             :   }
    1129      944747 :   return z;
    1130             : }
    1131             : 
    1132             : /* operations on elements in factored form */
    1133             : 
    1134             : GEN
    1135       88933 : famat_mul_shallow(GEN f, GEN g)
    1136             : {
    1137       88933 :   if (typ(f) != t_MAT) f = to_famat_shallow(f,gen_1);
    1138       88933 :   if (typ(g) != t_MAT) g = to_famat_shallow(g,gen_1);
    1139       88933 :   if (lgcols(f) == 1) return g;
    1140       73169 :   if (lgcols(g) == 1) return f;
    1141      146198 :   return mkmat2(shallowconcat(gel(f,1), gel(g,1)),
    1142      146198 :                 shallowconcat(gel(f,2), gel(g,2)));
    1143             : }
    1144             : GEN
    1145       63301 : famat_mulpow_shallow(GEN f, GEN g, GEN e)
    1146             : {
    1147       63301 :   if (!signe(e)) return f;
    1148       62993 :   return famat_mul_shallow(f, famat_pow_shallow(g, e));
    1149             : }
    1150             : 
    1151             : GEN
    1152       11977 : famat_mulpows_shallow(GEN f, GEN g, long e)
    1153             : {
    1154       11977 :   if (e==0) return f;
    1155        7223 :   return famat_mul_shallow(f, famat_pows_shallow(g, e));
    1156             : }
    1157             : 
    1158             : GEN
    1159           7 : famat_div_shallow(GEN f, GEN g)
    1160           7 : { return famat_mul_shallow(f, famat_inv_shallow(g)); }
    1161             : 
    1162             : GEN
    1163           0 : to_famat(GEN x, GEN y) { retmkmat2(mkcolcopy(x), mkcolcopy(y)); }
    1164             : GEN
    1165      895963 : to_famat_shallow(GEN x, GEN y) { return mkmat2(mkcol(x), mkcol(y)); }
    1166             : 
    1167             : /* concat the single elt x; not gconcat since x may be a t_COL */
    1168             : static GEN
    1169       31782 : append(GEN v, GEN x)
    1170             : {
    1171       31782 :   long i, l = lg(v);
    1172       31782 :   GEN w = cgetg(l+1, typ(v));
    1173       31782 :   for (i=1; i<l; i++) gel(w,i) = gcopy(gel(v,i));
    1174       31782 :   gel(w,i) = gcopy(x); return w;
    1175             : }
    1176             : /* add x^1 to famat f */
    1177             : static GEN
    1178       81916 : famat_add(GEN f, GEN x)
    1179             : {
    1180       81916 :   GEN h = cgetg(3,t_MAT);
    1181       81916 :   if (lgcols(f) == 1)
    1182             :   {
    1183       50134 :     gel(h,1) = mkcolcopy(x);
    1184       50134 :     gel(h,2) = mkcol(gen_1);
    1185             :   }
    1186             :   else
    1187             :   {
    1188       31782 :     gel(h,1) = append(gel(f,1), x);
    1189       31782 :     gel(h,2) = gconcat(gel(f,2), gen_1);
    1190             :   }
    1191       81916 :   return h;
    1192             : }
    1193             : 
    1194             : GEN
    1195       88913 : famat_mul(GEN f, GEN g)
    1196             : {
    1197             :   GEN h;
    1198       88913 :   if (typ(g) != t_MAT) {
    1199       81916 :     if (typ(f) == t_MAT) return famat_add(f, g);
    1200           0 :     h = cgetg(3, t_MAT);
    1201           0 :     gel(h,1) = mkcol2(gcopy(f), gcopy(g));
    1202           0 :     gel(h,2) = mkcol2(gen_1, gen_1);
    1203             :   }
    1204        6997 :   if (typ(f) != t_MAT) return famat_add(g, f);
    1205        6997 :   if (lgcols(f) == 1) return gcopy(g);
    1206        4818 :   if (lgcols(g) == 1) return gcopy(f);
    1207        2011 :   h = cgetg(3,t_MAT);
    1208        2011 :   gel(h,1) = gconcat(gel(f,1), gel(g,1));
    1209        2011 :   gel(h,2) = gconcat(gel(f,2), gel(g,2));
    1210        2011 :   return h;
    1211             : }
    1212             : 
    1213             : GEN
    1214       17179 : famat_sqr(GEN f)
    1215             : {
    1216             :   GEN h;
    1217       17179 :   if (typ(f) != t_MAT) return to_famat(f,gen_2);
    1218       17179 :   if (lgcols(f) == 1) return gcopy(f);
    1219       12121 :   h = cgetg(3,t_MAT);
    1220       12121 :   gel(h,1) = gcopy(gel(f,1));
    1221       12121 :   gel(h,2) = gmul2n(gel(f,2),1);
    1222       12121 :   return h;
    1223             : }
    1224             : 
    1225             : GEN
    1226       28224 : famat_inv_shallow(GEN f)
    1227             : {
    1228       28224 :   if (typ(f) != t_MAT) return to_famat_shallow(f,gen_m1);
    1229          49 :   if (lgcols(f) == 1) return f;
    1230          49 :   return mkmat2(gel(f,1), ZC_neg(gel(f,2)));
    1231             : }
    1232             : GEN
    1233       17675 : famat_inv(GEN f)
    1234             : {
    1235       17675 :   if (typ(f) != t_MAT) return to_famat(f,gen_m1);
    1236       17675 :   if (lgcols(f) == 1) return gcopy(f);
    1237        6455 :   retmkmat2(gcopy(gel(f,1)), ZC_neg(gel(f,2)));
    1238             : }
    1239             : GEN
    1240        2077 : famat_pow(GEN f, GEN n)
    1241             : {
    1242        2077 :   if (typ(f) != t_MAT) return to_famat(f,n);
    1243        2077 :   if (lgcols(f) == 1) return gcopy(f);
    1244           0 :   retmkmat2(gcopy(gel(f,1)), ZC_Z_mul(gel(f,2),n));
    1245             : }
    1246             : GEN
    1247       62993 : famat_pow_shallow(GEN f, GEN n)
    1248             : {
    1249       62993 :   if (is_pm1(n)) return signe(n) > 0? f: famat_inv_shallow(f);
    1250       32732 :   if (typ(f) != t_MAT) return to_famat_shallow(f,n);
    1251         245 :   if (lgcols(f) == 1) return f;
    1252         245 :   return mkmat2(gel(f,1), ZC_Z_mul(gel(f,2),n));
    1253             : }
    1254             : 
    1255             : GEN
    1256        7223 : famat_pows_shallow(GEN f, long n)
    1257             : {
    1258        7223 :   if (n==1) return f;
    1259        2618 :   if (n==-1) return famat_inv_shallow(f);
    1260        2254 :   if (typ(f) != t_MAT) return to_famat_shallow(f, stoi(n));
    1261        2064 :   if (lgcols(f) == 1) return f;
    1262        2064 :   return mkmat2(gel(f,1), ZC_z_mul(gel(f,2),n));
    1263             : }
    1264             : 
    1265             : GEN
    1266           0 : famat_Z_gcd(GEN M, GEN n)
    1267             : {
    1268           0 :   pari_sp av=avma;
    1269           0 :   long i, j, l=lgcols(M);
    1270           0 :   GEN F=cgetg(3,t_MAT);
    1271           0 :   gel(F,1)=cgetg(l,t_COL);
    1272           0 :   gel(F,2)=cgetg(l,t_COL);
    1273           0 :   for (i=1, j=1; i<l; i++)
    1274             :   {
    1275           0 :     GEN p = gcoeff(M,i,1);
    1276           0 :     GEN e = gminsg(Z_pval(n,p),gcoeff(M,i,2));
    1277           0 :     if (signe(e))
    1278             :     {
    1279           0 :       gcoeff(F,j,1)=p;
    1280           0 :       gcoeff(F,j,2)=e;
    1281           0 :       j++;
    1282             :     }
    1283             :   }
    1284           0 :   setlg(gel(F,1),j); setlg(gel(F,2),j);
    1285           0 :   return gerepilecopy(av,F);
    1286             : }
    1287             : 
    1288             : /* x assumed to be a t_MATs (factorization matrix), or compatible with
    1289             :  * the element_* functions. */
    1290             : static GEN
    1291       27756 : ext_sqr(GEN nf, GEN x)
    1292       27756 : { return (typ(x)==t_MAT)? famat_sqr(x): nfsqr(nf, x); }
    1293             : static GEN
    1294      123885 : ext_mul(GEN nf, GEN x, GEN y)
    1295      123885 : { return (typ(x)==t_MAT)? famat_mul(x,y): nfmul(nf, x, y); }
    1296             : static GEN
    1297       17325 : ext_inv(GEN nf, GEN x)
    1298       17325 : { return (typ(x)==t_MAT)? famat_inv(x): nfinv(nf, x); }
    1299             : static GEN
    1300        2077 : ext_pow(GEN nf, GEN x, GEN n)
    1301        2077 : { return (typ(x)==t_MAT)? famat_pow(x,n): nfpow(nf, x, n); }
    1302             : 
    1303             : GEN
    1304           0 : famat_to_nf(GEN nf, GEN f)
    1305             : {
    1306             :   GEN t, x, e;
    1307             :   long i;
    1308           0 :   if (lgcols(f) == 1) return gen_1;
    1309           0 :   x = gel(f,1);
    1310           0 :   e = gel(f,2);
    1311           0 :   t = nfpow(nf, gel(x,1), gel(e,1));
    1312           0 :   for (i=lg(x)-1; i>1; i--)
    1313           0 :     t = nfmul(nf, t, nfpow(nf, gel(x,i), gel(e,i)));
    1314           0 :   return t;
    1315             : }
    1316             : 
    1317             : GEN
    1318       32872 : famat_reduce(GEN fa)
    1319             : {
    1320             :   GEN E, G, L, g, e;
    1321             :   long i, k, l;
    1322             : 
    1323       32872 :   if (lgcols(fa) == 1) return fa;
    1324       27930 :   g = gel(fa,1); l = lg(g);
    1325       27930 :   e = gel(fa,2);
    1326       27930 :   L = gen_indexsort(g, (void*)&cmp_universal, &cmp_nodata);
    1327       27930 :   G = cgetg(l, t_COL);
    1328       27930 :   E = cgetg(l, t_COL);
    1329             :   /* merge */
    1330       66331 :   for (k=i=1; i<l; i++,k++)
    1331             :   {
    1332       38401 :     gel(G,k) = gel(g,L[i]);
    1333       38401 :     gel(E,k) = gel(e,L[i]);
    1334       38401 :     if (k > 1 && gidentical(gel(G,k), gel(G,k-1)))
    1335             :     {
    1336         833 :       gel(E,k-1) = addii(gel(E,k), gel(E,k-1));
    1337         833 :       k--;
    1338             :     }
    1339             :   }
    1340             :   /* kill 0 exponents */
    1341       27930 :   l = k;
    1342       65498 :   for (k=i=1; i<l; i++)
    1343       37568 :     if (!gequal0(gel(E,i)))
    1344             :     {
    1345       36532 :       gel(G,k) = gel(G,i);
    1346       36532 :       gel(E,k) = gel(E,i); k++;
    1347             :     }
    1348       27930 :   setlg(G, k);
    1349       27930 :   setlg(E, k); return mkmat2(G,E);
    1350             : }
    1351             : 
    1352             : GEN
    1353       14662 : famatsmall_reduce(GEN fa)
    1354             : {
    1355             :   GEN E, G, L, g, e;
    1356             :   long i, k, l;
    1357       14662 :   if (lgcols(fa) == 1) return fa;
    1358       14662 :   g = gel(fa,1); l = lg(g);
    1359       14662 :   e = gel(fa,2);
    1360       14662 :   L = vecsmall_indexsort(g);
    1361       14663 :   G = cgetg(l, t_VECSMALL);
    1362       14663 :   E = cgetg(l, t_VECSMALL);
    1363             :   /* merge */
    1364      131089 :   for (k=i=1; i<l; i++,k++)
    1365             :   {
    1366      116426 :     G[k] = g[L[i]];
    1367      116426 :     E[k] = e[L[i]];
    1368      116426 :     if (k > 1 && G[k] == G[k-1])
    1369             :     {
    1370        7058 :       E[k-1] += E[k];
    1371        7058 :       k--;
    1372             :     }
    1373             :   }
    1374             :   /* kill 0 exponents */
    1375       14663 :   l = k;
    1376      124031 :   for (k=i=1; i<l; i++)
    1377      109368 :     if (E[i])
    1378             :     {
    1379      105637 :       G[k] = G[i];
    1380      105637 :       E[k] = E[i]; k++;
    1381             :     }
    1382       14663 :   setlg(G, k);
    1383       14663 :   setlg(E, k); return mkmat2(G,E);
    1384             : }
    1385             : 
    1386             : GEN
    1387       61957 : ZM_famat_limit(GEN fa, GEN limit)
    1388             : {
    1389             :   pari_sp av;
    1390             :   GEN E, G, g, e, r;
    1391             :   long i, k, l, n, lG;
    1392             : 
    1393       61957 :   if (lgcols(fa) == 1) return fa;
    1394       61950 :   g = gel(fa,1); l = lg(g);
    1395       61950 :   e = gel(fa,2);
    1396      137746 :   for(n=0, i=1; i<l; i++)
    1397       75796 :     if (cmpii(gel(g,i),limit)<=0) n++;
    1398       61950 :   lG = n<l-1 ? n+2 : n+1;
    1399       61950 :   G = cgetg(lG, t_COL);
    1400       61950 :   E = cgetg(lG, t_COL);
    1401       61950 :   av = avma;
    1402      137746 :   for (i=1, k=1, r = gen_1; i<l; i++)
    1403             :   {
    1404       75796 :     if (cmpii(gel(g,i),limit)<=0)
    1405             :     {
    1406       75698 :       gel(G,k) = gel(g,i);
    1407       75698 :       gel(E,k) = gel(e,i);
    1408       75698 :       k++;
    1409          98 :     } else r = mulii(r, powii(gel(g,i), gel(e,i)));
    1410             :   }
    1411       61950 :   if (k<i)
    1412             :   {
    1413          98 :     gel(G, k) = gerepileuptoint(av, r);
    1414          98 :     gel(E, k) = gen_1;
    1415             :   }
    1416       61950 :   return mkmat2(G,E);
    1417             : }
    1418             : 
    1419             : /* assume pr has degree 1 and coprime to Q_denom(x) */
    1420             : static GEN
    1421        4742 : to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1422             : {
    1423        4742 :   GEN d, r, p = modpr_get_p(modpr);
    1424        4742 :   x = nf_to_scalar_or_basis(nf,x);
    1425        4742 :   if (typ(x) != t_COL) return Rg_to_Fp(x,p);
    1426        4434 :   x = Q_remove_denom(x, &d);
    1427        4434 :   r = zk_to_Fq(x, modpr);
    1428        4434 :   if (d) r = Fp_div(r, d, p);
    1429        4434 :   return r;
    1430             : }
    1431             : 
    1432             : /* pr coprime to all denominators occurring in x */
    1433             : static GEN
    1434         620 : famat_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1435             : {
    1436         620 :   GEN p = modpr_get_p(modpr);
    1437         620 :   GEN t = NULL, g = gel(x,1), e = gel(x,2), q = subiu(p,1);
    1438         620 :   long i, l = lg(g);
    1439        1940 :   for (i = 1; i < l; i++)
    1440             :   {
    1441        1320 :     GEN n = modii(gel(e,i), q);
    1442        1320 :     if (signe(n))
    1443             :     {
    1444        1320 :       GEN h = to_Fp_coprime(nf, gel(g,i), modpr);
    1445        1320 :       h = Fp_pow(h, n, p);
    1446        1320 :       t = t? Fp_mul(t, h, p): h;
    1447             :     }
    1448             :   }
    1449         620 :   return t? modii(t, p): gen_1;
    1450             : }
    1451             : 
    1452             : /* cf famat_to_nf_modideal_coprime, modpr attached to prime of degree 1 */
    1453             : GEN
    1454        4042 : nf_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1455             : {
    1456        4042 :   return typ(x)==t_MAT? famat_to_Fp_coprime(nf, x, modpr)
    1457        4042 :                       : to_Fp_coprime(nf, x, modpr);
    1458             : }
    1459             : 
    1460             : static long
    1461      153056 : zk_pvalrem(GEN x, GEN p, GEN *py)
    1462      153056 : { return (typ(x) == t_INT)? Z_pvalrem(x, p, py): ZV_pvalrem(x, p, py); }
    1463             : /* x a QC or Q. Return a ZC or Z, whose content is coprime to Z. Set v, dx
    1464             :  * such that x = p^v (newx / dx); dx = NULL if 1 */
    1465             : static GEN
    1466      284222 : nf_remove_denom_p(GEN nf, GEN x, GEN p, GEN *pdx, long *pv)
    1467             : {
    1468             :   long vcx;
    1469             :   GEN dx;
    1470      284222 :   x = nf_to_scalar_or_basis(nf, x);
    1471      284222 :   x = Q_remove_denom(x, &dx);
    1472      284222 :   if (dx)
    1473             :   {
    1474      177779 :     vcx = - Z_pvalrem(dx, p, &dx);
    1475      177779 :     if (!vcx) vcx = zk_pvalrem(x, p, &x);
    1476      177779 :     if (isint1(dx)) dx = NULL;
    1477             :   }
    1478             :   else
    1479             :   {
    1480      106443 :     vcx = zk_pvalrem(x, p, &x);
    1481      106443 :     dx = NULL;
    1482             :   }
    1483      284222 :   *pv = vcx;
    1484      284222 :   *pdx = dx; return x;
    1485             : }
    1486             : /* x = b^e/p^(e-1) in Z_K; x = 0 mod p/pr^e, (x,pr) = 1. Return NULL
    1487             :  * if p inert (instead of 1) */
    1488             : static GEN
    1489       64918 : p_makecoprime(GEN pr)
    1490             : {
    1491       64918 :   GEN B = pr_get_tau(pr), b;
    1492             :   long i, e;
    1493             : 
    1494       64918 :   if (typ(B) == t_INT) return NULL;
    1495       64778 :   b = gel(B,1); /* B = multiplication table by b */
    1496       64778 :   e = pr_get_e(pr);
    1497       64778 :   if (e == 1) return b;
    1498             :   /* one could also divide (exactly) by p in each iteration */
    1499       18109 :   for (i = 1; i < e; i++) b = ZM_ZC_mul(B, b);
    1500       18109 :   return ZC_Z_divexact(b, powiu(pr_get_p(pr), e-1));
    1501             : }
    1502             : 
    1503             : /* Compute A = prod g[i]^e[i] mod pr^k, assuming (A, pr) = 1.
    1504             :  * Method: modify each g[i] so that it becomes coprime to pr,
    1505             :  * g[i] *= (b/p)^v_pr(g[i]), where b/p = pr^(-1) times something integral
    1506             :  * and prime to p; globally, we multiply by (b/p)^v_pr(A) = 1.
    1507             :  * Optimizations:
    1508             :  * 1) remove all powers of p from contents, and consider extra generator p^vp;
    1509             :  * modified as p * (b/p)^e = b^e / p^(e-1)
    1510             :  * 2) remove denominators, coprime to p, by multiplying by inverse mod prk\cap Z
    1511             :  *
    1512             :  * EX = multiple of exponent of (O_K / pr^k)^* used to reduce the product in
    1513             :  * case the e[i] are large */
    1514             : GEN
    1515      123085 : famat_makecoprime(GEN nf, GEN g, GEN e, GEN pr, GEN prk, GEN EX)
    1516             : {
    1517      123085 :   GEN G, E, t, vp = NULL, p = pr_get_p(pr), prkZ = gcoeff(prk, 1,1);
    1518      123085 :   long i, l = lg(g);
    1519             : 
    1520      123085 :   G = cgetg(l+1, t_VEC);
    1521      123085 :   E = cgetg(l+1, t_VEC); /* l+1: room for "modified p" */
    1522      407307 :   for (i=1; i < l; i++)
    1523             :   {
    1524             :     long vcx;
    1525      284222 :     GEN dx, x = nf_remove_denom_p(nf, gel(g,i), p, &dx, &vcx);
    1526      284222 :     if (vcx) /* = v_p(content(g[i])) */
    1527             :     {
    1528      133609 :       GEN a = mulsi(vcx, gel(e,i));
    1529      133609 :       vp = vp? addii(vp, a): a;
    1530             :     }
    1531             :     /* x integral, content coprime to p; dx coprime to p */
    1532      284222 :     if (typ(x) == t_INT)
    1533             :     { /* x coprime to p, hence to pr */
    1534       42529 :       x = modii(x, prkZ);
    1535       42529 :       if (dx) x = Fp_div(x, dx, prkZ);
    1536             :     }
    1537             :     else
    1538             :     {
    1539      241693 :       (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
    1540      241693 :       x = ZC_hnfrem(FpC_red(x,prkZ), prk);
    1541      241693 :       if (dx) x = FpC_Fp_mul(x, Fp_inv(dx,prkZ), prkZ);
    1542             :     }
    1543      284222 :     gel(G,i) = x;
    1544      284222 :     gel(E,i) = gel(e,i);
    1545             :   }
    1546             : 
    1547      123085 :   t = vp? p_makecoprime(pr): NULL;
    1548      123085 :   if (!t)
    1549             :   { /* no need for extra generator */
    1550       58307 :     setlg(G,l);
    1551       58307 :     setlg(E,l);
    1552             :   }
    1553             :   else
    1554             :   {
    1555       64778 :     gel(G,i) = FpC_red(t, prkZ);
    1556       64778 :     gel(E,i) = vp;
    1557             :   }
    1558      123085 :   return famat_to_nf_modideal_coprime(nf, G, E, prk, EX);
    1559             : }
    1560             : 
    1561             : /* prod g[i]^e[i] mod bid, assume (g[i], id) = 1 and 1 < lg(g) <= lg(e) */
    1562             : GEN
    1563       19061 : famat_to_nf_moddivisor(GEN nf, GEN g, GEN e, GEN bid)
    1564             : {
    1565       19061 :   GEN t, cyc = bid_get_cyc(bid);
    1566       19061 :   if (lg(cyc) == 1)
    1567           0 :     t = gen_1;
    1568             :   else
    1569       19061 :     t = famat_to_nf_modideal_coprime(nf, g, e, bid_get_ideal(bid), gel(cyc,1));
    1570       19061 :   return set_sign_mod_divisor(nf, mkmat2(g,e), t, bid_get_sarch(bid));
    1571             : }
    1572             : 
    1573             : GEN
    1574      220486 : vecmul(GEN x, GEN y)
    1575             : {
    1576      220486 :   if (is_scalar_t(typ(x))) return gmul(x,y);
    1577       22710 :   pari_APPLY_same(vecmul(gel(x,i), gel(y,i)))
    1578             : }
    1579             : 
    1580             : GEN
    1581           0 : vecinv(GEN x)
    1582             : {
    1583           0 :   if (is_scalar_t(typ(x))) return ginv(x);
    1584           0 :   pari_APPLY_same(vecinv(gel(x,i)))
    1585             : }
    1586             : 
    1587             : GEN
    1588       20223 : vecpow(GEN x, GEN n)
    1589             : {
    1590       20223 :   if (is_scalar_t(typ(x))) return powgi(x,n);
    1591        5369 :   pari_APPLY_same(vecpow(gel(x,i), n))
    1592             : }
    1593             : 
    1594             : GEN
    1595         903 : vecdiv(GEN x, GEN y)
    1596             : {
    1597         903 :   if (is_scalar_t(typ(x))) return gdiv(x,y);
    1598         301 :   pari_APPLY_same(vecdiv(gel(x,i), gel(y,i)))
    1599             : }
    1600             : 
    1601             : /* A ideal as a square t_MAT */
    1602             : static GEN
    1603      235547 : idealmulelt(GEN nf, GEN x, GEN A)
    1604             : {
    1605             :   long i, lx;
    1606             :   GEN dx, dA, D;
    1607      235547 :   if (lg(A) == 1) return cgetg(1, t_MAT);
    1608      235547 :   x = nf_to_scalar_or_basis(nf,x);
    1609      235547 :   if (typ(x) != t_COL)
    1610       96543 :     return isintzero(x)? cgetg(1,t_MAT): RgM_Rg_mul(A, Q_abs_shallow(x));
    1611      139004 :   x = Q_remove_denom(x, &dx);
    1612      139004 :   A = Q_remove_denom(A, &dA);
    1613      139004 :   x = zk_multable(nf, x);
    1614      139004 :   D = mulii(zkmultable_capZ(x), gcoeff(A,1,1));
    1615      139004 :   x = zkC_multable_mul(A, x);
    1616      139004 :   settyp(x, t_MAT); lx = lg(x);
    1617             :   /* x may contain scalars (at most 1 since the ideal is non-0)*/
    1618      477087 :   for (i=1; i<lx; i++)
    1619      348400 :     if (typ(gel(x,i)) == t_INT)
    1620             :     {
    1621       10317 :       if (i > 1) swap(gel(x,1), gel(x,i)); /* help HNF */
    1622       10317 :       gel(x,1) = scalarcol_shallow(gel(x,1), lx-1);
    1623       10317 :       break;
    1624             :     }
    1625      139004 :   x = ZM_hnfmodid(x, D);
    1626      139004 :   dx = mul_denom(dx,dA);
    1627      139004 :   return dx? gdiv(x,dx): x;
    1628             : }
    1629             : 
    1630             : /* nf a true nf, tx <= ty */
    1631             : static GEN
    1632     1354424 : idealmul_aux(GEN nf, GEN x, GEN y, long tx, long ty)
    1633             : {
    1634             :   GEN z, cx, cy;
    1635     1354424 :   switch(tx)
    1636             :   {
    1637             :     case id_PRINCIPAL:
    1638      291175 :       switch(ty)
    1639             :       {
    1640             :         case id_PRINCIPAL:
    1641       55432 :           return idealhnf_principal(nf, nfmul(nf,x,y));
    1642             :         case id_PRIME:
    1643             :         {
    1644         196 :           GEN p = pr_get_p(y), pi = pr_get_gen(y), cx;
    1645         196 :           if (pr_is_inert(y)) return RgM_Rg_mul(idealhnf_principal(nf,x),p);
    1646             : 
    1647          42 :           x = nf_to_scalar_or_basis(nf, x);
    1648          42 :           switch(typ(x))
    1649             :           {
    1650             :             case t_INT:
    1651          28 :               if (!signe(x)) return cgetg(1,t_MAT);
    1652          28 :               return ZM_Z_mul(pr_hnf(nf,y), absi_shallow(x));
    1653             :             case t_FRAC:
    1654           7 :               return RgM_Rg_mul(pr_hnf(nf,y), Q_abs_shallow(x));
    1655             :           }
    1656             :           /* t_COL */
    1657           7 :           x = Q_primitive_part(x, &cx);
    1658           7 :           x = zk_multable(nf, x);
    1659           7 :           z = shallowconcat(ZM_Z_mul(x,p), ZM_ZC_mul(x,pi));
    1660           7 :           z = ZM_hnfmodid(z, mulii(p, zkmultable_capZ(x)));
    1661           7 :           return cx? ZM_Q_mul(z, cx): z;
    1662             :         }
    1663             :         default: /* id_MAT */
    1664      235547 :           return idealmulelt(nf, x,y);
    1665             :       }
    1666             :     case id_PRIME:
    1667      975740 :       if (ty==id_PRIME)
    1668      971315 :       { y = pr_hnf(nf,y); cy = NULL; }
    1669             :       else
    1670        4425 :         y = Q_primitive_part(y, &cy);
    1671      975740 :       y = idealHNF_mul_two(nf,y,x);
    1672      975740 :       return cy? ZM_Q_mul(y,cy): y;
    1673             : 
    1674             :     default: /* id_MAT */
    1675             :     {
    1676       87509 :       long N = nf_get_degree(nf);
    1677       87509 :       if (lg(x)-1 != N || lg(y)-1 != N) pari_err_DIM("idealmul");
    1678       87495 :       x = Q_primitive_part(x, &cx);
    1679       87495 :       y = Q_primitive_part(y, &cy); cx = mul_content(cx,cy);
    1680       87495 :       y = idealHNF_mul(nf,x,y);
    1681       87495 :       return cx? ZM_Q_mul(y,cx): y;
    1682             :     }
    1683             :   }
    1684             : }
    1685             : 
    1686             : /* output the ideal product ix.iy */
    1687             : GEN
    1688     1354424 : idealmul(GEN nf, GEN x, GEN y)
    1689             : {
    1690             :   pari_sp av;
    1691             :   GEN res, ax, ay, z;
    1692     1354424 :   long tx = idealtyp(&x,&ax);
    1693     1354424 :   long ty = idealtyp(&y,&ay), f;
    1694     1354424 :   if (tx>ty) { swap(ax,ay); swap(x,y); lswap(tx,ty); }
    1695     1354424 :   f = (ax||ay); res = f? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1696     1354424 :   av = avma;
    1697     1354424 :   z = gerepileupto(av, idealmul_aux(checknf(nf), x,y, tx,ty));
    1698     1354410 :   if (!f) return z;
    1699       25644 :   if (ax && ay)
    1700       23895 :     ax = ext_mul(nf, ax, ay);
    1701             :   else
    1702        1749 :     ax = gcopy(ax? ax: ay);
    1703       25644 :   gel(res,1) = z; gel(res,2) = ax; return res;
    1704             : }
    1705             : 
    1706             : /* Return x, integral in 2-elt form, such that pr^2 = c * x. cf idealpowprime
    1707             :  * nf = true nf */
    1708             : static GEN
    1709       41080 : idealsqrprime(GEN nf, GEN pr, GEN *pc)
    1710             : {
    1711       41080 :   GEN p = pr_get_p(pr), q, gen;
    1712       41080 :   long e = pr_get_e(pr), f = pr_get_f(pr);
    1713             : 
    1714       41080 :   q = (e == 1)? sqri(p): p;
    1715       41080 :   if (e <= 2 && e * f == nf_get_degree(nf))
    1716             :   { /* pr^e = (p) */
    1717       12831 :     *pc = q;
    1718       12831 :     return mkvec2(gen_1,gen_0);
    1719             :   }
    1720       28249 :   gen = nfsqr(nf, pr_get_gen(pr));
    1721       28249 :   gen = FpC_red(gen, q);
    1722       28249 :   *pc = NULL;
    1723       28249 :   return mkvec2(q, gen);
    1724             : }
    1725             : /* cf idealpow_aux */
    1726             : static GEN
    1727       28022 : idealsqr_aux(GEN nf, GEN x, long tx)
    1728             : {
    1729       28022 :   GEN T = nf_get_pol(nf), m, cx, a, alpha;
    1730       28022 :   long N = degpol(T);
    1731       28022 :   switch(tx)
    1732             :   {
    1733             :     case id_PRINCIPAL:
    1734          49 :       return idealhnf_principal(nf, nfsqr(nf,x));
    1735             :     case id_PRIME:
    1736        8983 :       if (pr_is_inert(x)) return scalarmat(sqri(gel(x,1)), N);
    1737        8815 :       x = idealsqrprime(nf, x, &cx);
    1738        8815 :       x = idealhnf_two(nf,x);
    1739        8815 :       return cx? ZM_Z_mul(x, cx): x;
    1740             :     default:
    1741       18990 :       x = Q_primitive_part(x, &cx);
    1742       18990 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    1743       18990 :       alpha = nfsqr(nf,alpha);
    1744       18990 :       m = zk_scalar_or_multable(nf, alpha);
    1745       18990 :       if (typ(m) == t_INT) {
    1746        1253 :         x = gcdii(sqri(a), m);
    1747        1253 :         if (cx) x = gmul(x, gsqr(cx));
    1748        1253 :         x = scalarmat(x, N);
    1749             :       }
    1750             :       else
    1751             :       {
    1752       17737 :         x = ZM_hnfmodid(m, gcdii(sqri(a), zkmultable_capZ(m)));
    1753       17737 :         if (cx) cx = gsqr(cx);
    1754       17737 :         if (cx) x = ZM_Q_mul(x, cx);
    1755             :       }
    1756       18990 :       return x;
    1757             :   }
    1758             : }
    1759             : GEN
    1760       28022 : idealsqr(GEN nf, GEN x)
    1761             : {
    1762             :   pari_sp av;
    1763             :   GEN res, ax, z;
    1764       28022 :   long tx = idealtyp(&x,&ax);
    1765       28022 :   res = ax? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1766       28022 :   av = avma;
    1767       28022 :   z = gerepileupto(av, idealsqr_aux(checknf(nf), x, tx));
    1768       28022 :   if (!ax) return z;
    1769       27756 :   gel(res,1) = z;
    1770       27756 :   gel(res,2) = ext_sqr(nf, ax); return res;
    1771             : }
    1772             : 
    1773             : /* norm of an ideal */
    1774             : GEN
    1775        8001 : idealnorm(GEN nf, GEN x)
    1776             : {
    1777             :   pari_sp av;
    1778             :   GEN y, T;
    1779             :   long tx;
    1780             : 
    1781        8001 :   switch(idealtyp(&x,&y))
    1782             :   {
    1783         245 :     case id_PRIME: return pr_norm(x);
    1784        5068 :     case id_MAT: return RgM_det_triangular(x);
    1785             :   }
    1786             :   /* id_PRINCIPAL */
    1787        2688 :   nf = checknf(nf); T = nf_get_pol(nf); av = avma;
    1788        2688 :   x = nf_to_scalar_or_alg(nf, x);
    1789        2688 :   x = (typ(x) == t_POL)? RgXQ_norm(x, T): gpowgs(x, degpol(T));
    1790        2688 :   tx = typ(x);
    1791        2688 :   if (tx == t_INT) return gerepileuptoint(av, absi(x));
    1792         637 :   if (tx != t_FRAC) pari_err_TYPE("idealnorm",x);
    1793         637 :   return gerepileupto(av, Q_abs(x));
    1794             : }
    1795             : 
    1796             : /* x \cap Z */
    1797             : GEN
    1798          35 : idealdown(GEN nf, GEN x)
    1799             : {
    1800          35 :   pari_sp av = avma;
    1801             :   GEN y, c;
    1802          35 :   switch(idealtyp(&x,&y))
    1803             :   {
    1804           7 :     case id_PRIME: return icopy(pr_get_p(x));
    1805           7 :     case id_MAT: return gcopy(gcoeff(x,1,1));
    1806             :   }
    1807             :   /* id_PRINCIPAL */
    1808          21 :   nf = checknf(nf); av = avma;
    1809          21 :   x = nf_to_scalar_or_basis(nf, x);
    1810          21 :   if (is_rational_t(typ(x))) return Q_abs(x);
    1811          14 :   x = Q_primitive_part(x, &c);
    1812          14 :   y = zkmultable_capZ(zk_multable(nf, x));
    1813          14 :   return gerepilecopy(av, mul_content(c, y));
    1814             : }
    1815             : 
    1816             : /* true nf */
    1817             : static GEN
    1818          28 : idealismaximal_int(GEN nf, GEN p)
    1819             : {
    1820             :   GEN L;
    1821          28 :   if (!BPSW_psp(p)) return NULL;
    1822          56 :   if (!dvdii(nf_get_index(nf), p) &&
    1823          42 :       !FpX_is_irred(FpX_red(nf_get_pol(nf),p), p)) return NULL;
    1824          14 :   L = idealprimedec(nf, p);
    1825          14 :   return lg(L) == 2? gel(L,1): NULL;
    1826             : }
    1827             : /* true nf */
    1828             : static GEN
    1829           7 : idealismaximal_mat(GEN nf, GEN x)
    1830             : {
    1831             :   GEN p, c, L;
    1832             :   long i, l, f;
    1833           7 :   x = Q_primitive_part(x, &c);
    1834           7 :   p = gcoeff(x,1,1);
    1835           7 :   if (c)
    1836             :   {
    1837           0 :     if (typ(c) == t_FRAC || !equali1(p)) return NULL;
    1838           0 :     return idealismaximal_int(nf, p);
    1839             :   }
    1840           7 :   if (!BPSW_psp(p)) return NULL;
    1841           7 :   l = lg(x); f = 1;
    1842          21 :   for (i = 2; i < l; i++)
    1843             :   {
    1844          14 :     c = gcoeff(x,i,i);
    1845          14 :     if (equalii(c, p)) f++; else if (!equali1(c)) return NULL;
    1846             :   }
    1847           7 :   L = idealprimedec_limit_f(nf, p, f); l = lg(L);
    1848           7 :   for (i = 1; i < l; i++)
    1849             :   {
    1850           7 :     GEN pr = gel(L,i);
    1851             :     long v;
    1852           7 :     if (pr_get_f(pr) != f) continue;
    1853           7 :     v = idealval(nf, x, pr);
    1854           7 :     if (v == 1) return pr;
    1855           0 :     if (!v) break;
    1856             :   }
    1857           0 :   return NULL;
    1858             : }
    1859             : /* true nf */
    1860             : static GEN
    1861          42 : idealismaximal_i(GEN nf, GEN x)
    1862             : {
    1863             :   GEN L, p, pr, c;
    1864             :   long i, l;
    1865          42 :   switch(idealtyp(&x,&c))
    1866             :   {
    1867           7 :     case id_PRIME: return x;
    1868           7 :     case id_MAT: return idealismaximal_mat(nf, x);
    1869             :   }
    1870             :   /* id_PRINCIPAL */
    1871          28 :   nf = checknf(nf);
    1872          28 :   x = nf_to_scalar_or_basis(nf, x);
    1873          28 :   switch(typ(x))
    1874             :   {
    1875          28 :     case t_INT: return idealismaximal_int(nf, absi_shallow(x));
    1876           0 :     case t_FRAC: return NULL;
    1877             :   }
    1878           0 :   x = Q_primitive_part(x, &c);
    1879           0 :   if (c) return NULL;
    1880           0 :   p = zkmultable_capZ(zk_multable(nf, x));
    1881           0 :   L = idealprimedec(nf, p); l = lg(L); pr = NULL;
    1882           0 :   for (i = 1; i < l; i++)
    1883             :   {
    1884           0 :     long v = ZC_nfval(x, gel(L,i));
    1885           0 :     if (v > 1 || (v && pr)) return NULL;
    1886           0 :     pr = gel(L,i);
    1887             :   }
    1888           0 :   return pr;
    1889             : }
    1890             : GEN
    1891          42 : idealismaximal(GEN nf, GEN x)
    1892             : {
    1893          42 :   pari_sp av = avma;
    1894          42 :   x = idealismaximal_i(checknf(nf), x);
    1895          42 :   if (!x) { set_avma(av); return gen_0; }
    1896          28 :   return gerepilecopy(av, x);
    1897             : }
    1898             : 
    1899             : /* I^(-1) = { x \in K, Tr(x D^(-1) I) \in Z }, D different of K/Q
    1900             :  *
    1901             :  * nf[5][6] = pp( D^(-1) ) = pp( HNF( T^(-1) ) ), T = (Tr(wi wj))
    1902             :  * nf[5][7] = same in 2-elt form.
    1903             :  * Assume I integral. Return the integral ideal (I\cap Z) I^(-1) */
    1904             : GEN
    1905      199635 : idealHNF_inv_Z(GEN nf, GEN I)
    1906             : {
    1907      199635 :   GEN J, dual, IZ = gcoeff(I,1,1); /* I \cap Z */
    1908      199635 :   if (isint1(IZ)) return matid(lg(I)-1);
    1909      187539 :   J = idealHNF_mul(nf,I, gmael(nf,5,7));
    1910             :  /* I in HNF, hence easily inverted; multiply by IZ to get integer coeffs
    1911             :   * missing content cancels while solving the linear equation */
    1912      187539 :   dual = shallowtrans( hnf_divscale(J, gmael(nf,5,6), IZ) );
    1913      187539 :   return ZM_hnfmodid(dual, IZ);
    1914             : }
    1915             : /* I HNF with rational coefficients (denominator d). */
    1916             : GEN
    1917       73835 : idealHNF_inv(GEN nf, GEN I)
    1918             : {
    1919       73835 :   GEN J, IQ = gcoeff(I,1,1); /* I \cap Q; d IQ = dI \cap Z */
    1920       73835 :   J = idealHNF_inv_Z(nf, Q_remove_denom(I, NULL)); /* = (dI)^(-1) * (d IQ) */
    1921       73835 :   return equali1(IQ)? J: RgM_Rg_div(J, IQ);
    1922             : }
    1923             : 
    1924             : /* return p * P^(-1)  [integral] */
    1925             : GEN
    1926       25984 : pr_inv_p(GEN pr)
    1927             : {
    1928       25984 :   if (pr_is_inert(pr)) return matid(pr_get_f(pr));
    1929       25396 :   return ZM_hnfmodid(pr_get_tau(pr), pr_get_p(pr));
    1930             : }
    1931             : GEN
    1932        5427 : pr_inv(GEN pr)
    1933             : {
    1934        5427 :   GEN p = pr_get_p(pr);
    1935        5427 :   if (pr_is_inert(pr)) return scalarmat(ginv(p), pr_get_f(pr));
    1936        5091 :   return RgM_Rg_div(ZM_hnfmodid(pr_get_tau(pr),p), p);
    1937             : }
    1938             : 
    1939             : GEN
    1940      118076 : idealinv(GEN nf, GEN x)
    1941             : {
    1942             :   GEN res, ax;
    1943             :   pari_sp av;
    1944      118076 :   long tx = idealtyp(&x,&ax), N;
    1945             : 
    1946      118076 :   res = ax? cgetg(3,t_VEC): NULL;
    1947      118076 :   nf = checknf(nf); av = avma;
    1948      118076 :   N = nf_get_degree(nf);
    1949      118076 :   switch (tx)
    1950             :   {
    1951             :     case id_MAT:
    1952       66926 :       if (lg(x)-1 != N) pari_err_DIM("idealinv");
    1953       66926 :       x = idealHNF_inv(nf,x); break;
    1954             :     case id_PRINCIPAL:
    1955       46640 :       x = nf_to_scalar_or_basis(nf, x);
    1956       46640 :       if (typ(x) != t_COL)
    1957       46598 :         x = idealhnf_principal(nf,ginv(x));
    1958             :       else
    1959             :       { /* nfinv + idealhnf where we already know (x) \cap Z */
    1960             :         GEN c, d;
    1961          42 :         x = Q_remove_denom(x, &c);
    1962          42 :         x = zk_inv(nf, x);
    1963          42 :         x = Q_remove_denom(x, &d); /* true inverse is c/d * x */
    1964          42 :         if (!d) /* x and x^(-1) integral => x a unit */
    1965           7 :           x = scalarmat_shallow(c? c: gen_1, N);
    1966             :         else
    1967             :         {
    1968          35 :           c = c? gdiv(c,d): ginv(d);
    1969          35 :           x = zk_multable(nf, x);
    1970          35 :           x = ZM_Q_mul(ZM_hnfmodid(x,d), c);
    1971             :         }
    1972             :       }
    1973       46640 :       break;
    1974             :     case id_PRIME:
    1975        4510 :       x = pr_inv(x); break;
    1976             :   }
    1977      118076 :   x = gerepileupto(av,x); if (!ax) return x;
    1978       17325 :   gel(res,1) = x;
    1979       17325 :   gel(res,2) = ext_inv(nf, ax); return res;
    1980             : }
    1981             : 
    1982             : /* write x = A/B, A,B coprime integral ideals */
    1983             : GEN
    1984       60156 : idealnumden(GEN nf, GEN x)
    1985             : {
    1986       60156 :   pari_sp av = avma;
    1987             :   GEN x0, ax, c, d, A, B, J;
    1988       60156 :   long tx = idealtyp(&x,&ax);
    1989       60156 :   nf = checknf(nf);
    1990       60156 :   switch (tx)
    1991             :   {
    1992             :     case id_PRIME:
    1993           7 :       retmkvec2(idealhnf(nf, x), gen_1);
    1994             :     case id_PRINCIPAL:
    1995             :     {
    1996             :       GEN xZ, mx;
    1997        6048 :       x = nf_to_scalar_or_basis(nf, x);
    1998        6048 :       switch(typ(x))
    1999             :       {
    2000        1106 :         case t_INT: return gerepilecopy(av, mkvec2(absi_shallow(x),gen_1));
    2001          14 :         case t_FRAC:return gerepilecopy(av, mkvec2(absi_shallow(gel(x,1)), gel(x,2)));
    2002             :       }
    2003             :       /* t_COL */
    2004        4928 :       x = Q_remove_denom(x, &d);
    2005        4928 :       if (!d) return gerepilecopy(av, mkvec2(idealhnf(nf, x), gen_1));
    2006          35 :       mx = zk_multable(nf, x);
    2007          35 :       xZ = zkmultable_capZ(mx);
    2008          35 :       x = ZM_hnfmodid(mx, xZ); /* principal ideal (x) */
    2009          35 :       x0 = mkvec2(xZ, mx); /* same, for fast multiplication */
    2010          35 :       break;
    2011             :     }
    2012             :     default: /* id_MAT */
    2013             :     {
    2014       54101 :       long n = lg(x)-1;
    2015       54101 :       if (n == 0) return mkvec2(gen_0, gen_1);
    2016       54101 :       if (n != nf_get_degree(nf)) pari_err_DIM("idealnumden");
    2017       54101 :       x0 = x = Q_remove_denom(x, &d);
    2018       54101 :       if (!d) return gerepilecopy(av, mkvec2(x, gen_1));
    2019          14 :       break;
    2020             :     }
    2021             :   }
    2022          49 :   J = hnfmodid(x, d); /* = d/B */
    2023          49 :   c = gcoeff(J,1,1); /* (d/B) \cap Z, divides d */
    2024          49 :   B = idealHNF_inv_Z(nf, J); /* (d/B \cap Z) B/d */
    2025          49 :   if (!equalii(c,d)) B = ZM_Z_mul(B, diviiexact(d,c)); /* = B ! */
    2026          49 :   A = idealHNF_mul(nf, B, x0); /* d * (original x) * B = d A */
    2027          49 :   A = ZM_Z_divexact(A, d); /* = A ! */
    2028          49 :   return gerepilecopy(av, mkvec2(A, B));
    2029             : }
    2030             : 
    2031             : /* Return x, integral in 2-elt form, such that pr^n = c * x. Assume n != 0.
    2032             :  * nf = true nf */
    2033             : static GEN
    2034      158565 : idealpowprime(GEN nf, GEN pr, GEN n, GEN *pc)
    2035             : {
    2036      158565 :   GEN p = pr_get_p(pr), q, gen;
    2037             : 
    2038      158565 :   *pc = NULL;
    2039      158565 :   if (is_pm1(n)) /* n = 1 special cased for efficiency */
    2040             :   {
    2041       84526 :     q = p;
    2042       84526 :     if (typ(pr_get_tau(pr)) == t_INT) /* inert */
    2043             :     {
    2044           0 :       *pc = (signe(n) >= 0)? p: ginv(p);
    2045           0 :       return mkvec2(gen_1,gen_0);
    2046             :     }
    2047       84526 :     if (signe(n) >= 0) gen = pr_get_gen(pr);
    2048             :     else
    2049             :     {
    2050       18095 :       gen = pr_get_tau(pr); /* possibly t_MAT */
    2051       18095 :       *pc = ginv(p);
    2052             :     }
    2053             :   }
    2054       74039 :   else if (equalis(n,2)) return idealsqrprime(nf, pr, pc);
    2055             :   else
    2056             :   {
    2057       41774 :     long e = pr_get_e(pr), f = pr_get_f(pr);
    2058       41774 :     GEN r, m = truedvmdis(n, e, &r);
    2059       41774 :     if (e * f == nf_get_degree(nf))
    2060             :     { /* pr^e = (p) */
    2061       11578 :       if (signe(m)) *pc = powii(p,m);
    2062       11578 :       if (!signe(r)) return mkvec2(gen_1,gen_0);
    2063        5264 :       q = p;
    2064        5264 :       gen = nfpow(nf, pr_get_gen(pr), r);
    2065             :     }
    2066             :     else
    2067             :     {
    2068       30196 :       m = absi_shallow(m);
    2069       30196 :       if (signe(r)) m = addiu(m,1);
    2070       30196 :       q = powii(p,m); /* m = ceil(|n|/e) */
    2071       30196 :       if (signe(n) >= 0) gen = nfpow(nf, pr_get_gen(pr), n);
    2072             :       else
    2073             :       {
    2074        4599 :         gen = pr_get_tau(pr);
    2075        4599 :         if (typ(gen) == t_MAT) gen = gel(gen,1);
    2076        4599 :         n = negi(n);
    2077        4599 :         gen = ZC_Z_divexact(nfpow(nf, gen, n), powii(p, subii(n,m)));
    2078        4599 :         *pc = ginv(q);
    2079             :       }
    2080             :     }
    2081       35460 :     gen = FpC_red(gen, q);
    2082             :   }
    2083      119986 :   return mkvec2(q, gen);
    2084             : }
    2085             : 
    2086             : /* x * pr^n. Assume x in HNF or scalar (possibly non-integral) */
    2087             : GEN
    2088      129593 : idealmulpowprime(GEN nf, GEN x, GEN pr, GEN n)
    2089             : {
    2090             :   GEN c, cx, y;
    2091             :   long N;
    2092             : 
    2093      129593 :   nf = checknf(nf);
    2094      129593 :   N = nf_get_degree(nf);
    2095      129593 :   if (!signe(n)) return typ(x) == t_MAT? x: scalarmat_shallow(x, N);
    2096             : 
    2097             :   /* inert, special cased for efficiency */
    2098      129222 :   if (pr_is_inert(pr))
    2099             :   {
    2100       10794 :     GEN q = powii(pr_get_p(pr), n);
    2101       10794 :     return typ(x) == t_MAT? RgM_Rg_mul(x,q)
    2102       10794 :                           : scalarmat_shallow(gmul(Q_abs(x),q), N);
    2103             :   }
    2104             : 
    2105      118428 :   y = idealpowprime(nf, pr, n, &c);
    2106      118428 :   if (typ(x) == t_MAT)
    2107      115999 :   { x = Q_primitive_part(x, &cx); if (is_pm1(gcoeff(x,1,1))) x = NULL; }
    2108             :   else
    2109        2429 :   { cx = x; x = NULL; }
    2110      118428 :   cx = mul_content(c,cx);
    2111      118428 :   if (x)
    2112       73173 :     x = idealHNF_mul_two(nf,x,y);
    2113             :   else
    2114       45255 :     x = idealhnf_two(nf,y);
    2115      118428 :   if (cx) x = ZM_Q_mul(x,cx);
    2116      118428 :   return x;
    2117             : }
    2118             : GEN
    2119       28889 : idealdivpowprime(GEN nf, GEN x, GEN pr, GEN n)
    2120             : {
    2121       28889 :   return idealmulpowprime(nf,x,pr, negi(n));
    2122             : }
    2123             : 
    2124             : /* nf = true nf */
    2125             : static GEN
    2126      201545 : idealpow_aux(GEN nf, GEN x, long tx, GEN n)
    2127             : {
    2128      201545 :   GEN T = nf_get_pol(nf), m, cx, n1, a, alpha;
    2129      201545 :   long N = degpol(T), s = signe(n);
    2130      201545 :   if (!s) return matid(N);
    2131      192342 :   switch(tx)
    2132             :   {
    2133             :     case id_PRINCIPAL:
    2134           0 :       return idealhnf_principal(nf, nfpow(nf,x,n));
    2135             :     case id_PRIME:
    2136       87198 :       if (pr_is_inert(x)) return scalarmat(powii(gel(x,1), n), N);
    2137       40137 :       x = idealpowprime(nf, x, n, &cx);
    2138       40137 :       x = idealhnf_two(nf,x);
    2139       40137 :       return cx? ZM_Q_mul(x, cx): x;
    2140             :     default:
    2141      105144 :       if (is_pm1(n)) return (s < 0)? idealinv(nf, x): gcopy(x);
    2142       57605 :       n1 = (s < 0)? negi(n): n;
    2143             : 
    2144       57605 :       x = Q_primitive_part(x, &cx);
    2145       57605 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    2146       57605 :       alpha = nfpow(nf,alpha,n1);
    2147       57605 :       m = zk_scalar_or_multable(nf, alpha);
    2148       57605 :       if (typ(m) == t_INT) {
    2149         301 :         x = gcdii(powii(a,n1), m);
    2150         301 :         if (s<0) x = ginv(x);
    2151         301 :         if (cx) x = gmul(x, powgi(cx,n));
    2152         301 :         x = scalarmat(x, N);
    2153             :       }
    2154             :       else
    2155             :       {
    2156       57304 :         x = ZM_hnfmodid(m, gcdii(powii(a,n1), zkmultable_capZ(m)));
    2157       57304 :         if (cx) cx = powgi(cx,n);
    2158       57304 :         if (s<0) {
    2159           7 :           GEN xZ = gcoeff(x,1,1);
    2160           7 :           cx = cx ? gdiv(cx, xZ): ginv(xZ);
    2161           7 :           x = idealHNF_inv_Z(nf,x);
    2162             :         }
    2163       57304 :         if (cx) x = ZM_Q_mul(x, cx);
    2164             :       }
    2165       57605 :       return x;
    2166             :   }
    2167             : }
    2168             : 
    2169             : /* raise the ideal x to the power n (in Z) */
    2170             : GEN
    2171      201545 : idealpow(GEN nf, GEN x, GEN n)
    2172             : {
    2173             :   pari_sp av;
    2174             :   long tx;
    2175             :   GEN res, ax;
    2176             : 
    2177      201545 :   if (typ(n) != t_INT) pari_err_TYPE("idealpow",n);
    2178      201545 :   tx = idealtyp(&x,&ax);
    2179      201545 :   res = ax? cgetg(3,t_VEC): NULL;
    2180      201545 :   av = avma;
    2181      201545 :   x = gerepileupto(av, idealpow_aux(checknf(nf), x, tx, n));
    2182      201545 :   if (!ax) return x;
    2183        2077 :   ax = ext_pow(nf, ax, n);
    2184        2077 :   gel(res,1) = x;
    2185        2077 :   gel(res,2) = ax;
    2186        2077 :   return res;
    2187             : }
    2188             : 
    2189             : /* Return ideal^e in number field nf. e is a C integer. */
    2190             : GEN
    2191       30590 : idealpows(GEN nf, GEN ideal, long e)
    2192             : {
    2193       30590 :   long court[] = {evaltyp(t_INT) | _evallg(3),0,0};
    2194       30590 :   affsi(e,court); return idealpow(nf,ideal,court);
    2195             : }
    2196             : 
    2197             : static GEN
    2198       25665 : _idealmulred(GEN nf, GEN x, GEN y)
    2199       25665 : { return idealred(nf,idealmul(nf,x,y)); }
    2200             : static GEN
    2201       27770 : _idealsqrred(GEN nf, GEN x)
    2202       27770 : { return idealred(nf,idealsqr(nf,x)); }
    2203             : static GEN
    2204        9049 : _mul(void *data, GEN x, GEN y) { return _idealmulred((GEN)data,x,y); }
    2205             : static GEN
    2206       27770 : _sqr(void *data, GEN x) { return _idealsqrred((GEN)data, x); }
    2207             : 
    2208             : /* compute x^n (x ideal, n integer), reducing along the way */
    2209             : GEN
    2210       54007 : idealpowred(GEN nf, GEN x, GEN n)
    2211             : {
    2212       54007 :   pari_sp av = avma, av2;
    2213             :   long s;
    2214             :   GEN y;
    2215             : 
    2216       54007 :   if (typ(n) != t_INT) pari_err_TYPE("idealpowred",n);
    2217       54007 :   s = signe(n); if (s == 0) return idealpow(nf,x,n);
    2218       51930 :   y = gen_pow_i(x, n, (void*)nf, &_sqr, &_mul);
    2219       51930 :   av2 = avma;
    2220       51930 :   if (s < 0) y = idealinv(nf,y);
    2221       51930 :   if (s < 0 || is_pm1(n)) y = idealred(nf,y);
    2222       51930 :   return avma == av2? gerepilecopy(av,y): gerepileupto(av,y);
    2223             : }
    2224             : 
    2225             : GEN
    2226       16616 : idealmulred(GEN nf, GEN x, GEN y)
    2227             : {
    2228       16616 :   pari_sp av = avma;
    2229       16616 :   return gerepileupto(av, _idealmulred(nf,x,y));
    2230             : }
    2231             : 
    2232             : long
    2233          91 : isideal(GEN nf,GEN x)
    2234             : {
    2235          91 :   long N, i, j, lx, tx = typ(x);
    2236             :   pari_sp av;
    2237             :   GEN T, xZ;
    2238             : 
    2239          91 :   nf = checknf(nf); T = nf_get_pol(nf); lx = lg(x);
    2240          91 :   if (tx==t_VEC && lx==3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
    2241          91 :   switch(tx)
    2242             :   {
    2243          14 :     case t_INT: case t_FRAC: return 1;
    2244           7 :     case t_POL: return varn(x) == varn(T);
    2245           7 :     case t_POLMOD: return RgX_equal_var(T, gel(x,1));
    2246          14 :     case t_VEC: return get_prid(x)? 1 : 0;
    2247          42 :     case t_MAT: break;
    2248           7 :     default: return 0;
    2249             :   }
    2250          42 :   N = degpol(T);
    2251          42 :   if (lx-1 != N) return (lx == 1);
    2252          28 :   if (nbrows(x) != N) return 0;
    2253             : 
    2254          28 :   av = avma; x = Q_primpart(x);
    2255          28 :   if (!ZM_ishnf(x)) return 0;
    2256          14 :   xZ = gcoeff(x,1,1);
    2257          21 :   for (j=2; j<=N; j++)
    2258          14 :     if (!dvdii(xZ, gcoeff(x,j,j))) return gc_long(av,0);
    2259          14 :   for (i=2; i<=N; i++)
    2260          14 :     for (j=2; j<=N; j++)
    2261           7 :        if (! hnf_invimage(x, zk_ei_mul(nf,gel(x,i),j))) return gc_long(av,0);
    2262           7 :   return gc_long(av,1);
    2263             : }
    2264             : 
    2265             : GEN
    2266       31542 : idealdiv(GEN nf, GEN x, GEN y)
    2267             : {
    2268       31542 :   pari_sp av = avma, tetpil;
    2269       31542 :   GEN z = idealinv(nf,y);
    2270       31542 :   tetpil = avma; return gerepile(av,tetpil, idealmul(nf,x,z));
    2271             : }
    2272             : 
    2273             : /* This routine computes the quotient x/y of two ideals in the number field nf.
    2274             :  * It assumes that the quotient is an integral ideal.  The idea is to find an
    2275             :  * ideal z dividing y such that gcd(Nx/Nz, Nz) = 1.  Then
    2276             :  *
    2277             :  *   x + (Nx/Nz)    x
    2278             :  *   ----------- = ---
    2279             :  *   y + (Ny/Nz)    y
    2280             :  *
    2281             :  * Proof: we can assume x and y are integral. Let p be any prime ideal
    2282             :  *
    2283             :  * If p | Nz, then it divides neither Nx/Nz nor Ny/Nz (since Nx/Nz is the
    2284             :  * product of the integers N(x/y) and N(y/z)).  Both the numerator and the
    2285             :  * denominator on the left will be coprime to p.  So will x/y, since x/y is
    2286             :  * assumed integral and its norm N(x/y) is coprime to p.
    2287             :  *
    2288             :  * If instead p does not divide Nz, then v_p (Nx/Nz) = v_p (Nx) >= v_p(x).
    2289             :  * Hence v_p (x + Nx/Nz) = v_p(x).  Likewise for the denominators.  QED.
    2290             :  *
    2291             :  *                Peter Montgomery.  July, 1994. */
    2292             : static void
    2293           7 : err_divexact(GEN x, GEN y)
    2294           7 : { pari_err_DOMAIN("idealdivexact","denominator(x/y)", "!=",
    2295           0 :                   gen_1,mkvec2(x,y)); }
    2296             : GEN
    2297        1330 : idealdivexact(GEN nf, GEN x0, GEN y0)
    2298             : {
    2299        1330 :   pari_sp av = avma;
    2300             :   GEN x, y, xZ, yZ, Nx, Ny, Nz, cy, q, r;
    2301             : 
    2302        1330 :   nf = checknf(nf);
    2303        1330 :   x = idealhnf_shallow(nf, x0);
    2304        1330 :   y = idealhnf_shallow(nf, y0);
    2305        1330 :   if (lg(y) == 1) pari_err_INV("idealdivexact", y0);
    2306        1323 :   if (lg(x) == 1) { set_avma(av); return cgetg(1, t_MAT); } /* numerator is zero */
    2307        1323 :   y = Q_primitive_part(y, &cy);
    2308        1323 :   if (cy) x = RgM_Rg_div(x,cy);
    2309        1323 :   xZ = gcoeff(x,1,1); if (typ(xZ) != t_INT) err_divexact(x,y);
    2310        1316 :   yZ = gcoeff(y,1,1); if (isint1(yZ)) return gerepilecopy(av, x);
    2311         476 :   Nx = idealnorm(nf,x);
    2312         476 :   Ny = idealnorm(nf,y);
    2313         476 :   if (typ(Nx) != t_INT) err_divexact(x,y);
    2314         476 :   q = dvmdii(Nx,Ny, &r);
    2315         476 :   if (signe(r)) err_divexact(x,y);
    2316         476 :   if (is_pm1(q)) { set_avma(av); return matid(nf_get_degree(nf)); }
    2317             :   /* Find a norm Nz | Ny such that gcd(Nx/Nz, Nz) = 1 */
    2318         385 :   for (Nz = Ny;;) /* q = Nx/Nz */
    2319         301 :   {
    2320         686 :     GEN p1 = gcdii(Nz, q);
    2321         686 :     if (is_pm1(p1)) break;
    2322         301 :     Nz = diviiexact(Nz,p1);
    2323         301 :     q = mulii(q,p1);
    2324             :   }
    2325         385 :   xZ = gcoeff(x,1,1); q = gcdii(q, xZ);
    2326         385 :   if (!equalii(xZ,q))
    2327             :   { /* Replace x/y  by  x+(Nx/Nz) / y+(Ny/Nz) */
    2328          91 :     x = ZM_hnfmodid(x, q);
    2329             :     /* y reduced to unit ideal ? */
    2330          91 :     if (Nz == Ny) return gerepileupto(av, x);
    2331             : 
    2332           7 :     yZ = gcoeff(y,1,1); q = gcdii(diviiexact(Ny,Nz), yZ);
    2333           7 :     y = ZM_hnfmodid(y, q);
    2334             :   }
    2335         301 :   yZ = gcoeff(y,1,1);
    2336         301 :   y = idealHNF_mul(nf,x, idealHNF_inv_Z(nf,y));
    2337         301 :   return gerepileupto(av, ZM_Z_divexact(y, yZ));
    2338             : }
    2339             : 
    2340             : GEN
    2341          21 : idealintersect(GEN nf, GEN x, GEN y)
    2342             : {
    2343          21 :   pari_sp av = avma;
    2344             :   long lz, lx, i;
    2345             :   GEN z, dx, dy, xZ, yZ;;
    2346             : 
    2347          21 :   nf = checknf(nf);
    2348          21 :   x = idealhnf_shallow(nf,x);
    2349          21 :   y = idealhnf_shallow(nf,y);
    2350          21 :   if (lg(x) == 1 || lg(y) == 1) { set_avma(av); return cgetg(1,t_MAT); }
    2351          14 :   x = Q_remove_denom(x, &dx);
    2352          14 :   y = Q_remove_denom(y, &dy);
    2353          14 :   if (dx) y = ZM_Z_mul(y, dx);
    2354          14 :   if (dy) x = ZM_Z_mul(x, dy);
    2355          14 :   xZ = gcoeff(x,1,1);
    2356          14 :   yZ = gcoeff(y,1,1);
    2357          14 :   dx = mul_denom(dx,dy);
    2358          14 :   z = ZM_lll(shallowconcat(x,y), 0.99, LLL_KER); lz = lg(z);
    2359          14 :   lx = lg(x);
    2360          14 :   for (i=1; i<lz; i++) setlg(z[i], lx);
    2361          14 :   z = ZM_hnfmodid(ZM_mul(x,z), lcmii(xZ, yZ));
    2362          14 :   if (dx) z = RgM_Rg_div(z,dx);
    2363          14 :   return gerepileupto(av,z);
    2364             : }
    2365             : 
    2366             : /*******************************************************************/
    2367             : /*                                                                 */
    2368             : /*                      T2-IDEAL REDUCTION                         */
    2369             : /*                                                                 */
    2370             : /*******************************************************************/
    2371             : 
    2372             : static GEN
    2373          21 : chk_vdir(GEN nf, GEN vdir)
    2374             : {
    2375          21 :   long i, l = lg(vdir);
    2376             :   GEN v;
    2377          21 :   if (l != lg(nf_get_roots(nf))) pari_err_DIM("idealred");
    2378          14 :   switch(typ(vdir))
    2379             :   {
    2380           0 :     case t_VECSMALL: return vdir;
    2381          14 :     case t_VEC: break;
    2382           0 :     default: pari_err_TYPE("idealred",vdir);
    2383             :   }
    2384          14 :   v = cgetg(l, t_VECSMALL);
    2385          14 :   for (i = 1; i < l; i++) v[i] = itos(gceil(gel(vdir,i)));
    2386          14 :   return v;
    2387             : }
    2388             : 
    2389             : static void
    2390       28632 : twistG(GEN G, long r1, long i, long v)
    2391             : {
    2392       28632 :   long j, lG = lg(G);
    2393       28632 :   if (i <= r1) {
    2394       23588 :     for (j=1; j<lG; j++) gcoeff(G,i,j) = gmul2n(gcoeff(G,i,j), v);
    2395             :   } else {
    2396        5044 :     long k = (i<<1) - r1;
    2397       26776 :     for (j=1; j<lG; j++)
    2398             :     {
    2399       21732 :       gcoeff(G,k-1,j) = gmul2n(gcoeff(G,k-1,j), v);
    2400       21732 :       gcoeff(G,k  ,j) = gmul2n(gcoeff(G,k  ,j), v);
    2401             :     }
    2402             :   }
    2403       28632 : }
    2404             : 
    2405             : GEN
    2406      154122 : nf_get_Gtwist(GEN nf, GEN vdir)
    2407             : {
    2408             :   long i, l, v, r1;
    2409             :   GEN G;
    2410             : 
    2411      154122 :   if (!vdir) return nf_get_roundG(nf);
    2412       26148 :   if (typ(vdir) == t_MAT)
    2413             :   {
    2414       26127 :     long N = nf_get_degree(nf);
    2415       26127 :     if (lg(vdir) != N+1 || lgcols(vdir) != N+1) pari_err_DIM("idealred");
    2416       26127 :     return vdir;
    2417             :   }
    2418          21 :   vdir = chk_vdir(nf, vdir);
    2419          14 :   G = RgM_shallowcopy(nf_get_G(nf));
    2420          14 :   r1 = nf_get_r1(nf);
    2421          14 :   l = lg(vdir);
    2422          56 :   for (i=1; i<l; i++)
    2423             :   {
    2424          42 :     v = vdir[i]; if (!v) continue;
    2425          42 :     twistG(G, r1, i, v);
    2426             :   }
    2427          14 :   return RM_round_maxrank(G);
    2428             : }
    2429             : GEN
    2430       28590 : nf_get_Gtwist1(GEN nf, long i)
    2431             : {
    2432       28590 :   GEN G = RgM_shallowcopy( nf_get_G(nf) );
    2433       28590 :   long r1 = nf_get_r1(nf);
    2434       28590 :   twistG(G, r1, i, 10);
    2435       28590 :   return RM_round_maxrank(G);
    2436             : }
    2437             : 
    2438             : GEN
    2439       46370 : RM_round_maxrank(GEN G0)
    2440             : {
    2441       46370 :   long e, r = lg(G0)-1;
    2442       46370 :   pari_sp av = avma;
    2443       46370 :   GEN G = G0;
    2444       46370 :   for (e = 4; ; e <<= 1)
    2445           0 :   {
    2446       46370 :     GEN H = ground(G);
    2447       92740 :     if (ZM_rank(H) == r) return H; /* maximal rank ? */
    2448           0 :     set_avma(av);
    2449           0 :     G = gmul2n(G0, e);
    2450             :   }
    2451             : }
    2452             : 
    2453             : GEN
    2454      154115 : idealred0(GEN nf, GEN I, GEN vdir)
    2455             : {
    2456      154115 :   pari_sp av = avma;
    2457      154115 :   GEN G, aI, IZ, J, y, yZ, my, c1 = NULL;
    2458             :   long N;
    2459             : 
    2460      154115 :   nf = checknf(nf);
    2461      154115 :   N = nf_get_degree(nf);
    2462             :   /* put first for sanity checks, unused when I obviously principal */
    2463      154115 :   G = nf_get_Gtwist(nf, vdir);
    2464      154108 :   switch (idealtyp(&I,&aI))
    2465             :   {
    2466             :     case id_PRIME:
    2467       24423 :       if (pr_is_inert(I)) {
    2468         581 :         if (!aI) { set_avma(av); return matid(N); }
    2469         581 :         c1 = gel(I,1); I = matid(N);
    2470         581 :         goto END;
    2471             :       }
    2472       23842 :       IZ = pr_get_p(I);
    2473       23842 :       J = pr_inv_p(I);
    2474       23842 :       I = idealhnf_two(nf,I);
    2475       23842 :       break;
    2476             :     case id_MAT:
    2477      129657 :       I = Q_primitive_part(I, &c1);
    2478      129657 :       IZ = gcoeff(I,1,1);
    2479      129657 :       if (is_pm1(IZ))
    2480             :       {
    2481        8554 :         if (!aI) { set_avma(av); return matid(N); }
    2482        8498 :         goto END;
    2483             :       }
    2484      121103 :       J = idealHNF_inv_Z(nf, I);
    2485      121103 :       break;
    2486             :     default: /* id_PRINCIPAL, silly case */
    2487          21 :       if (gequal0(I)) I = cgetg(1,t_MAT); else { c1 = I; I = matid(N); }
    2488          21 :       if (!aI) return I;
    2489          14 :       goto END;
    2490             :   }
    2491             :   /* now I integral, HNF; and J = (I\cap Z) I^(-1), integral */
    2492      144945 :   y = idealpseudomin(J, G); /* small elt in (I\cap Z)I^(-1), integral */
    2493      144945 :   if (ZV_isscalar(y))
    2494             :   { /* already reduced */
    2495       54124 :     if (!aI) return gerepilecopy(av, I);
    2496       53725 :     goto END;
    2497             :   }
    2498             : 
    2499       90821 :   my = zk_multable(nf, y);
    2500       90821 :   I = ZM_Z_divexact(ZM_mul(my, I), IZ); /* y I / (I\cap Z), integral */
    2501       90821 :   c1 = mul_content(c1, IZ);
    2502       90821 :   my = ZM_gauss(my, col_ei(N,1)); /* y^-1 */
    2503       90821 :   yZ = Q_denom(my); /* (y) \cap Z */
    2504       90821 :   I = hnfmodid(I, yZ);
    2505       90821 :   if (!aI) return gerepileupto(av, I);
    2506       89547 :   c1 = RgC_Rg_mul(my, c1);
    2507             : END:
    2508      152365 :   if (c1) aI = ext_mul(nf, aI,c1);
    2509      152365 :   return gerepilecopy(av, mkvec2(I, aI));
    2510             : }
    2511             : 
    2512             : GEN
    2513           7 : idealmin(GEN nf, GEN x, GEN vdir)
    2514             : {
    2515           7 :   pari_sp av = avma;
    2516             :   GEN y, dx;
    2517           7 :   nf = checknf(nf);
    2518           7 :   switch( idealtyp(&x,&y) )
    2519             :   {
    2520           0 :     case id_PRINCIPAL: return gcopy(x);
    2521           0 :     case id_PRIME: x = pr_hnf(nf,x); break;
    2522           7 :     case id_MAT: if (lg(x) == 1) return gen_0;
    2523             :   }
    2524           7 :   x = Q_remove_denom(x, &dx);
    2525           7 :   y = idealpseudomin(x, nf_get_Gtwist(nf,vdir));
    2526           7 :   if (dx) y = RgC_Rg_div(y, dx);
    2527           7 :   return gerepileupto(av, y);
    2528             : }
    2529             : 
    2530             : /*******************************************************************/
    2531             : /*                                                                 */
    2532             : /*                   APPROXIMATION THEOREM                         */
    2533             : /*                                                                 */
    2534             : /*******************************************************************/
    2535             : /* a = ppi(a,b) ppo(a,b), where ppi regroups primes common to a and b
    2536             :  * and ppo(a,b) = Z_ppo(a,b) */
    2537             : /* return gcd(a,b),ppi(a,b),ppo(a,b) */
    2538             : GEN
    2539      454027 : Z_ppio(GEN a, GEN b)
    2540             : {
    2541      454027 :   GEN x, y, d = gcdii(a,b);
    2542      454027 :   if (is_pm1(d)) return mkvec3(gen_1, gen_1, a);
    2543      345079 :   x = d; y = diviiexact(a,d);
    2544             :   for(;;)
    2545       62713 :   {
    2546      407792 :     GEN g = gcdii(x,y);
    2547      407792 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2548       62713 :     x = mulii(x,g); y = diviiexact(y,g);
    2549             :   }
    2550             : }
    2551             : /* a = ppg(a,b)pple(a,b), where ppg regroups primes such that v(a) > v(b)
    2552             :  * and pple all others */
    2553             : /* return gcd(a,b),ppg(a,b),pple(a,b) */
    2554             : GEN
    2555           0 : Z_ppgle(GEN a, GEN b)
    2556             : {
    2557           0 :   GEN x, y, g, d = gcdii(a,b);
    2558           0 :   if (equalii(a, d)) return mkvec3(a, gen_1, a);
    2559           0 :   x = diviiexact(a,d); y = d;
    2560             :   for(;;)
    2561             :   {
    2562           0 :     g = gcdii(x,y);
    2563           0 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2564           0 :     x = mulii(x,g); y = diviiexact(y,g);
    2565             :   }
    2566             : }
    2567             : static void
    2568           0 : Z_dcba_rec(GEN L, GEN a, GEN b)
    2569             : {
    2570             :   GEN x, r, v, g, h, c, c0;
    2571             :   long n;
    2572           0 :   if (is_pm1(b)) {
    2573           0 :     if (!is_pm1(a)) vectrunc_append(L, a);
    2574           0 :     return;
    2575             :   }
    2576           0 :   v = Z_ppio(a,b);
    2577           0 :   a = gel(v,2);
    2578           0 :   r = gel(v,3);
    2579           0 :   if (!is_pm1(r)) vectrunc_append(L, r);
    2580           0 :   v = Z_ppgle(a,b);
    2581           0 :   g = gel(v,1);
    2582           0 :   h = gel(v,2);
    2583           0 :   x = c0 = gel(v,3);
    2584           0 :   for (n = 1; !is_pm1(h); n++)
    2585             :   {
    2586             :     GEN d, y;
    2587             :     long i;
    2588           0 :     v = Z_ppgle(h,sqri(g));
    2589           0 :     g = gel(v,1);
    2590           0 :     h = gel(v,2);
    2591           0 :     c = gel(v,3); if (is_pm1(c)) continue;
    2592           0 :     d = gcdii(c,b);
    2593           0 :     x = mulii(x,d);
    2594           0 :     y = d; for (i=1; i < n; i++) y = sqri(y);
    2595           0 :     Z_dcba_rec(L, diviiexact(c,y), d);
    2596             :   }
    2597           0 :   Z_dcba_rec(L,diviiexact(b,x), c0);
    2598             : }
    2599             : static GEN
    2600     3069773 : Z_cba_rec(GEN L, GEN a, GEN b)
    2601             : {
    2602             :   GEN g;
    2603     3069773 :   if (lg(L) > 10)
    2604             :   { /* a few naive steps before switching to dcba */
    2605           0 :     Z_dcba_rec(L, a, b);
    2606           0 :     return gel(L, lg(L)-1);
    2607             :   }
    2608     3069773 :   if (is_pm1(a)) return b;
    2609     1823962 :   g = gcdii(a,b);
    2610     1823962 :   if (is_pm1(g)) { vectrunc_append(L, a); return b; }
    2611     1362494 :   a = diviiexact(a,g);
    2612     1362494 :   b = diviiexact(b,g);
    2613     1362494 :   return Z_cba_rec(L, Z_cba_rec(L, a, g), b);
    2614             : }
    2615             : GEN
    2616      344785 : Z_cba(GEN a, GEN b)
    2617             : {
    2618      344785 :   GEN L = vectrunc_init(expi(a) + expi(b) + 2);
    2619      344785 :   GEN t = Z_cba_rec(L, a, b);
    2620      344785 :   if (!is_pm1(t)) vectrunc_append(L, t);
    2621      344785 :   return L;
    2622             : }
    2623             : /* P = coprime base, extend it by b; TODO: quadratic for now */
    2624             : GEN
    2625           0 : ZV_cba_extend(GEN P, GEN b)
    2626             : {
    2627           0 :   long i, l = lg(P);
    2628           0 :   GEN w = cgetg(l+1, t_VEC);
    2629           0 :   for (i = 1; i < l; i++)
    2630             :   {
    2631           0 :     GEN v = Z_cba(gel(P,i), b);
    2632           0 :     long nv = lg(v)-1;
    2633           0 :     gel(w,i) = vecslice(v, 1, nv-1); /* those divide P[i] but not b */
    2634           0 :     b = gel(v,nv);
    2635             :   }
    2636           0 :   gel(w,l) = b; return shallowconcat1(w);
    2637             : }
    2638             : GEN
    2639           0 : ZV_cba(GEN v)
    2640             : {
    2641           0 :   long i, l = lg(v);
    2642             :   GEN P;
    2643           0 :   if (l <= 2) return v;
    2644           0 :   P = Z_cba(gel(v,1), gel(v,2));
    2645           0 :   for (i = 3; i < l; i++) P = ZV_cba_extend(P, gel(v,i));
    2646           0 :   return P;
    2647             : }
    2648             : 
    2649             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2650             : GEN
    2651     1790696 : Z_ppo(GEN x, GEN f)
    2652             : {
    2653             :   for (;;)
    2654             :   {
    2655     2774728 :     f = gcdii(x, f); if (is_pm1(f)) break;
    2656      984032 :     x = diviiexact(x, f);
    2657             :   }
    2658      806664 :   return x;
    2659             : }
    2660             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2661             : ulong
    2662    41609138 : u_ppo(ulong x, ulong f)
    2663             : {
    2664             :   for (;;)
    2665             :   {
    2666    49795224 :     f = ugcd(x, f); if (f == 1) break;
    2667     8186086 :     x /= f;
    2668             :   }
    2669    33423052 :   return x;
    2670             : }
    2671             : 
    2672             : /* x t_INT, f ideal. Write x = x1 x2, sqf(x1) | f, (x2,f) = 1. Return x2 */
    2673             : static GEN
    2674         273 : nf_coprime_part(GEN nf, GEN x, GEN listpr)
    2675             : {
    2676         273 :   long v, j, lp = lg(listpr), N = nf_get_degree(nf);
    2677             :   GEN x1, x2, ex;
    2678             : 
    2679             : #if 0 /*1) via many gcds. Expensive ! */
    2680             :   GEN f = idealprodprime(nf, listpr);
    2681             :   f = ZM_hnfmodid(f, x); /* first gcd is less expensive since x in Z */
    2682             :   x = scalarmat(x, N);
    2683             :   for (;;)
    2684             :   {
    2685             :     if (gequal1(gcoeff(f,1,1))) break;
    2686             :     x = idealdivexact(nf, x, f);
    2687             :     f = ZM_hnfmodid(shallowconcat(f,x), gcoeff(x,1,1)); /* gcd(f,x) */
    2688             :   }
    2689             :   x2 = x;
    2690             : #else /*2) from prime decomposition */
    2691         273 :   x1 = NULL;
    2692         777 :   for (j=1; j<lp; j++)
    2693             :   {
    2694         504 :     GEN pr = gel(listpr,j);
    2695         504 :     v = Z_pval(x, pr_get_p(pr)); if (!v) continue;
    2696             : 
    2697         273 :     ex = muluu(v, pr_get_e(pr)); /* = v_pr(x) > 0 */
    2698         273 :     x1 = x1? idealmulpowprime(nf, x1, pr, ex)
    2699         273 :            : idealpow(nf, pr, ex);
    2700             :   }
    2701         273 :   x = scalarmat(x, N);
    2702         273 :   x2 = x1? idealdivexact(nf, x, x1): x;
    2703             : #endif
    2704         273 :   return x2;
    2705             : }
    2706             : 
    2707             : /* L0 in K^*, assume (L0,f) = 1. Return L integral, L0 = L mod f  */
    2708             : GEN
    2709        9044 : make_integral(GEN nf, GEN L0, GEN f, GEN listpr)
    2710             : {
    2711             :   GEN fZ, t, L, D2, d1, d2, d;
    2712             : 
    2713        9044 :   L = Q_remove_denom(L0, &d);
    2714        9044 :   if (!d) return L0;
    2715             : 
    2716             :   /* L0 = L / d, L integral */
    2717        2863 :   fZ = gcoeff(f,1,1);
    2718        2863 :   if (typ(L) == t_INT) return Fp_mul(L, Fp_inv(d, fZ), fZ);
    2719             :   /* Kill denom part coprime to fZ */
    2720        2429 :   d2 = Z_ppo(d, fZ);
    2721        2429 :   t = Fp_inv(d2, fZ); if (!is_pm1(t)) L = ZC_Z_mul(L,t);
    2722        2429 :   if (equalii(d, d2)) return L;
    2723             : 
    2724         273 :   d1 = diviiexact(d, d2);
    2725             :   /* L0 = (L / d1) mod f. d1 not coprime to f
    2726             :    * write (d1) = D1 D2, D2 minimal, (D2,f) = 1. */
    2727         273 :   D2 = nf_coprime_part(nf, d1, listpr);
    2728         273 :   t = idealaddtoone_i(nf, D2, f); /* in D2, 1 mod f */
    2729         273 :   L = nfmuli(nf,t,L);
    2730             : 
    2731             :   /* if (L0, f) = 1, then L in D1 ==> in D1 D2 = (d1) */
    2732         273 :   return Q_div_to_int(L, d1); /* exact division */
    2733             : }
    2734             : 
    2735             : /* assume L is a list of prime ideals. Return the product */
    2736             : GEN
    2737         329 : idealprodprime(GEN nf, GEN L)
    2738             : {
    2739         329 :   long l = lg(L), i;
    2740             :   GEN z;
    2741         329 :   if (l == 1) return matid(nf_get_degree(nf));
    2742         329 :   z = pr_hnf(nf, gel(L,1));
    2743         329 :   for (i=2; i<l; i++) z = idealHNF_mul_two(nf,z, gel(L,i));
    2744         329 :   return z;
    2745             : }
    2746             : 
    2747             : /* optimize for the frequent case I = nfhnf()[2]: lots of them are 1 */
    2748             : GEN
    2749        1036 : idealprod(GEN nf, GEN I)
    2750             : {
    2751        1036 :   long i, l = lg(I);
    2752             :   GEN z;
    2753        1631 :   for (i = 1; i < l; i++)
    2754        1603 :     if (!equali1(gel(I,i))) break;
    2755        1036 :   if (i == l) return gen_1;
    2756        1008 :   z = gel(I,i);
    2757        1008 :   for (i++; i<l; i++) z = idealmul(nf, z, gel(I,i));
    2758        1008 :   return z;
    2759             : }
    2760             : 
    2761             : /* v_pr(idealprod(nf,I)) */
    2762             : long
    2763        2128 : idealprodval(GEN nf, GEN I, GEN pr)
    2764             : {
    2765        2128 :   long i, l = lg(I), v = 0;
    2766       12579 :   for (i = 1; i < l; i++)
    2767       10451 :     if (!equali1(gel(I,i))) v += idealval(nf, gel(I,i), pr);
    2768        2128 :   return v;
    2769             : }
    2770             : 
    2771             : /* assume L is a list of prime ideals. Return prod L[i]^e[i] */
    2772             : GEN
    2773       10724 : factorbackprime(GEN nf, GEN L, GEN e)
    2774             : {
    2775       10724 :   long l = lg(L), i;
    2776             :   GEN z;
    2777             : 
    2778       10724 :   if (l == 1) return matid(nf_get_degree(nf));
    2779       10710 :   z = idealpow(nf, gel(L,1), gel(e,1));
    2780       17913 :   for (i=2; i<l; i++)
    2781        7203 :     if (signe(gel(e,i))) z = idealmulpowprime(nf,z, gel(L,i),gel(e,i));
    2782       10710 :   return z;
    2783             : }
    2784             : 
    2785             : /* F in Z, divisible exactly by pr.p. Return F-uniformizer for pr, i.e.
    2786             :  * a t in Z_K such that v_pr(t) = 1 and (t, F/pr) = 1 */
    2787             : GEN
    2788       23583 : pr_uniformizer(GEN pr, GEN F)
    2789             : {
    2790       23583 :   GEN p = pr_get_p(pr), t = pr_get_gen(pr);
    2791       23583 :   if (!equalii(F, p))
    2792             :   {
    2793       10724 :     long e = pr_get_e(pr);
    2794       10724 :     GEN u, v, q = (e == 1)? sqri(p): p;
    2795       10724 :     u = mulii(q, Fp_inv(q, diviiexact(F,p))); /* 1 mod F/p, 0 mod q */
    2796       10724 :     v = subui(1UL, u); /* 0 mod F/p, 1 mod q */
    2797       10724 :     if (pr_is_inert(pr))
    2798           0 :       t = addii(mulii(p, v), u);
    2799             :     else
    2800             :     {
    2801       10724 :       t = ZC_Z_mul(t, v);
    2802       10724 :       gel(t,1) = addii(gel(t,1), u); /* return u + vt */
    2803             :     }
    2804             :   }
    2805       23583 :   return t;
    2806             : }
    2807             : /* L = list of prime ideals, return lcm_i (L[i] \cap \ZM) */
    2808             : GEN
    2809       55361 : prV_lcm_capZ(GEN L)
    2810             : {
    2811       55361 :   long i, r = lg(L);
    2812             :   GEN F;
    2813       55361 :   if (r == 1) return gen_1;
    2814       46345 :   F = pr_get_p(gel(L,1));
    2815       70903 :   for (i = 2; i < r; i++)
    2816             :   {
    2817       24558 :     GEN pr = gel(L,i), p = pr_get_p(pr);
    2818       24558 :     if (!dvdii(F, p)) F = mulii(F,p);
    2819             :   }
    2820       46345 :   return F;
    2821             : }
    2822             : 
    2823             : /* Given a prime ideal factorization with possibly zero or negative
    2824             :  * exponents, gives b such that v_p(b) = v_p(x) for all prime ideals pr | x
    2825             :  * and v_pr(b) >= 0 for all other pr.
    2826             :  * For optimal performance, all [anti-]uniformizers should be precomputed,
    2827             :  * but no support for this yet.
    2828             :  *
    2829             :  * If nored, do not reduce result.
    2830             :  * No garbage collecting */
    2831             : static GEN
    2832       29573 : idealapprfact_i(GEN nf, GEN x, int nored)
    2833             : {
    2834             :   GEN z, d, L, e, e2, F;
    2835             :   long i, r;
    2836             :   int flagden;
    2837             : 
    2838       29573 :   nf = checknf(nf);
    2839       29573 :   L = gel(x,1);
    2840       29573 :   e = gel(x,2);
    2841       29573 :   F = prV_lcm_capZ(L);
    2842       29573 :   flagden = 0;
    2843       29573 :   z = NULL; r = lg(e);
    2844       61836 :   for (i = 1; i < r; i++)
    2845             :   {
    2846       32263 :     long s = signe(gel(e,i));
    2847             :     GEN pi, q;
    2848       32263 :     if (!s) continue;
    2849       19488 :     if (s < 0) flagden = 1;
    2850       19488 :     pi = pr_uniformizer(gel(L,i), F);
    2851       19488 :     q = nfpow(nf, pi, gel(e,i));
    2852       19488 :     z = z? nfmul(nf, z, q): q;
    2853             :   }
    2854       29573 :   if (!z) return gen_1;
    2855       13494 :   if (nored || typ(z) != t_COL) return z;
    2856        4683 :   e2 = cgetg(r, t_VEC);
    2857        4683 :   for (i=1; i<r; i++) gel(e2,i) = addiu(gel(e,i), 1);
    2858        4683 :   x = factorbackprime(nf, L,e2);
    2859        4683 :   if (flagden) /* denominator */
    2860             :   {
    2861        4669 :     z = Q_remove_denom(z, &d);
    2862        4669 :     d = diviiexact(d, Z_ppo(d, F));
    2863        4669 :     x = RgM_Rg_mul(x, d);
    2864             :   }
    2865             :   else
    2866          14 :     d = NULL;
    2867        4683 :   z = ZC_reducemodlll(z, x);
    2868        4683 :   return d? RgC_Rg_div(z,d): z;
    2869             : }
    2870             : 
    2871             : GEN
    2872           0 : idealapprfact(GEN nf, GEN x) {
    2873           0 :   pari_sp av = avma;
    2874           0 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    2875             : }
    2876             : GEN
    2877          14 : idealappr(GEN nf, GEN x) {
    2878          14 :   pari_sp av = avma;
    2879          14 :   if (!is_nf_extfactor(x)) x = idealfactor(nf, x);
    2880          14 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    2881             : }
    2882             : 
    2883             : /* OBSOLETE */
    2884             : GEN
    2885          14 : idealappr0(GEN nf, GEN x, long fl) { (void)fl; return idealappr(nf, x); }
    2886             : 
    2887             : static GEN
    2888          21 : mat_ideal_two_elt2(GEN nf, GEN x, GEN a)
    2889             : {
    2890          21 :   GEN F = idealfactor(nf,a), P = gel(F,1), E = gel(F,2);
    2891          21 :   long i, r = lg(E);
    2892          21 :   for (i=1; i<r; i++) gel(E,i) = stoi( idealval(nf,x,gel(P,i)) );
    2893          21 :   return idealapprfact_i(nf,F,1);
    2894             : }
    2895             : 
    2896             : static void
    2897          14 : not_in_ideal(GEN a) {
    2898          14 :   pari_err_DOMAIN("idealtwoelt2","element mod ideal", "!=", gen_0, a);
    2899           0 : }
    2900             : /* x integral in HNF, a an 'nf' */
    2901             : static int
    2902          28 : in_ideal(GEN x, GEN a)
    2903             : {
    2904          28 :   switch(typ(a))
    2905             :   {
    2906          14 :     case t_INT: return dvdii(a, gcoeff(x,1,1));
    2907           7 :     case t_COL: return RgV_is_ZV(a) && !!hnf_invimage(x, a);
    2908           7 :     default: return 0;
    2909             :   }
    2910             : }
    2911             : 
    2912             : /* Given an integral ideal x and a in x, gives a b such that
    2913             :  * x = aZ_K + bZ_K using the approximation theorem */
    2914             : GEN
    2915          42 : idealtwoelt2(GEN nf, GEN x, GEN a)
    2916             : {
    2917          42 :   pari_sp av = avma;
    2918             :   GEN cx, b;
    2919             : 
    2920          42 :   nf = checknf(nf);
    2921          42 :   a = nf_to_scalar_or_basis(nf, a);
    2922          42 :   x = idealhnf_shallow(nf,x);
    2923          42 :   if (lg(x) == 1)
    2924             :   {
    2925          14 :     if (!isintzero(a)) not_in_ideal(a);
    2926           7 :     set_avma(av); return gen_0;
    2927             :   }
    2928          28 :   x = Q_primitive_part(x, &cx);
    2929          28 :   if (cx) a = gdiv(a, cx);
    2930          28 :   if (!in_ideal(x, a)) not_in_ideal(a);
    2931          21 :   b = mat_ideal_two_elt2(nf, x, a);
    2932          21 :   if (typ(b) == t_COL)
    2933             :   {
    2934          14 :     GEN mod = idealhnf_principal(nf,a);
    2935          14 :     b = ZC_hnfrem(b,mod);
    2936          14 :     if (ZV_isscalar(b)) b = gel(b,1);
    2937             :   }
    2938             :   else
    2939             :   {
    2940           7 :     GEN aZ = typ(a) == t_COL? Q_denom(zk_inv(nf,a)): a; /* (a) \cap Z */
    2941           7 :     b = centermodii(b, aZ, shifti(aZ,-1));
    2942             :   }
    2943          21 :   b = cx? gmul(b,cx): gcopy(b);
    2944          21 :   return gerepileupto(av, b);
    2945             : }
    2946             : 
    2947             : /* Given 2 integral ideals x and y in nf, returns a beta in nf such that
    2948             :  * beta * x is an integral ideal coprime to y */
    2949             : GEN
    2950       20741 : idealcoprimefact(GEN nf, GEN x, GEN fy)
    2951             : {
    2952       20741 :   GEN L = gel(fy,1), e;
    2953       20741 :   long i, r = lg(L);
    2954             : 
    2955       20741 :   e = cgetg(r, t_COL);
    2956       20741 :   for (i=1; i<r; i++) gel(e,i) = stoi( -idealval(nf,x,gel(L,i)) );
    2957       20741 :   return idealapprfact_i(nf, mkmat2(L,e), 0);
    2958             : }
    2959             : GEN
    2960          70 : idealcoprime(GEN nf, GEN x, GEN y)
    2961             : {
    2962          70 :   pari_sp av = avma;
    2963          70 :   return gerepileupto(av, idealcoprimefact(nf, x, idealfactor(nf,y)));
    2964             : }
    2965             : 
    2966             : GEN
    2967           7 : nfmulmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    2968             : {
    2969           7 :   pari_sp av = avma;
    2970           7 :   GEN z, p, pr = modpr, T;
    2971             : 
    2972           7 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    2973           0 :   x = nf_to_Fq(nf,x,modpr);
    2974           0 :   y = nf_to_Fq(nf,y,modpr);
    2975           0 :   z = Fq_mul(x,y,T,p);
    2976           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    2977             : }
    2978             : 
    2979             : GEN
    2980           0 : nfdivmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    2981             : {
    2982           0 :   pari_sp av = avma;
    2983           0 :   nf = checknf(nf);
    2984           0 :   return gerepileupto(av, nfreducemodpr(nf, nfdiv(nf,x,y), modpr));
    2985             : }
    2986             : 
    2987             : GEN
    2988           0 : nfpowmodpr(GEN nf, GEN x, GEN k, GEN modpr)
    2989             : {
    2990           0 :   pari_sp av=avma;
    2991           0 :   GEN z, T, p, pr = modpr;
    2992             : 
    2993           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    2994           0 :   z = nf_to_Fq(nf,x,modpr);
    2995           0 :   z = Fq_pow(z,k,T,p);
    2996           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    2997             : }
    2998             : 
    2999             : GEN
    3000           0 : nfkermodpr(GEN nf, GEN x, GEN modpr)
    3001             : {
    3002           0 :   pari_sp av = avma;
    3003           0 :   GEN T, p, pr = modpr;
    3004             : 
    3005           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    3006           0 :   if (typ(x)!=t_MAT) pari_err_TYPE("nfkermodpr",x);
    3007           0 :   x = nfM_to_FqM(x, nf, modpr);
    3008           0 :   return gerepilecopy(av, FqM_to_nfM(FqM_ker(x,T,p), modpr));
    3009             : }
    3010             : 
    3011             : GEN
    3012           0 : nfsolvemodpr(GEN nf, GEN a, GEN b, GEN pr)
    3013             : {
    3014           0 :   const char *f = "nfsolvemodpr";
    3015           0 :   pari_sp av = avma;
    3016             :   GEN T, p, modpr;
    3017             : 
    3018           0 :   nf = checknf(nf);
    3019           0 :   modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    3020           0 :   if (typ(a)!=t_MAT) pari_err_TYPE(f,a);
    3021           0 :   a = nfM_to_FqM(a, nf, modpr);
    3022           0 :   switch(typ(b))
    3023             :   {
    3024             :     case t_MAT:
    3025           0 :       b = nfM_to_FqM(b, nf, modpr);
    3026           0 :       b = FqM_gauss(a,b,T,p);
    3027           0 :       if (!b) pari_err_INV(f,a);
    3028           0 :       a = FqM_to_nfM(b, modpr);
    3029           0 :       break;
    3030             :     case t_COL:
    3031           0 :       b = nfV_to_FqV(b, nf, modpr);
    3032           0 :       b = FqM_FqC_gauss(a,b,T,p);
    3033           0 :       if (!b) pari_err_INV(f,a);
    3034           0 :       a = FqV_to_nfV(b, modpr);
    3035           0 :       break;
    3036           0 :     default: pari_err_TYPE(f,b);
    3037             :   }
    3038           0 :   return gerepilecopy(av, a);
    3039             : }

Generated by: LCOV version 1.13