Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is to exceed 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - basemath - base4.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 25406-bf255ab81b) Lines: 1569 1762 89.0 %
Date: 2020-06-04 05:59:24 Functions: 155 173 89.6 %
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     5476801 : idealtyp(GEN *ideal, GEN *arch)
      44             : {
      45     5476801 :   GEN x = *ideal;
      46     5476801 :   long t,lx,tx = typ(x);
      47             : 
      48     5476801 :   if (tx!=t_VEC || lg(x)!=3) *arch = NULL;
      49             :   else
      50             :   {
      51      202541 :     GEN a = gel(x,2);
      52      202541 :     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      202534 :     *arch = a;
      58      202534 :     x = gel(x,1); tx = typ(x);
      59             :   }
      60     5476794 :   switch(tx)
      61             :   {
      62     1801504 :     case t_MAT: lx = lg(x);
      63     1801504 :       if (lx == 1) { t = id_PRINCIPAL; x = gen_0; break; }
      64     1801427 :       if (lx != lgcols(x)) pari_err_TYPE("idealtyp [non-square t_MAT]",x);
      65     1801415 :       t = id_MAT;
      66     1801415 :       break;
      67             : 
      68     3226788 :     case t_VEC: if (lg(x)!=6) pari_err_TYPE("idealtyp",x);
      69     3226773 :       t = id_PRIME; break;
      70             : 
      71      448522 :     case t_POL: case t_POLMOD: case t_COL:
      72             :     case t_INT: case t_FRAC:
      73      448522 :       t = id_PRINCIPAL; break;
      74           0 :     default:
      75           0 :       pari_err_TYPE("idealtyp",x);
      76             :       return 0; /*LCOV_EXCL_LINE*/
      77             :   }
      78     5476787 :   *ideal = x; return t;
      79             : }
      80             : 
      81             : /* true nf; v = [a,x,...], a in Z. Return (a,x) */
      82             : GEN
      83      137597 : idealhnf_two(GEN nf, GEN v)
      84             : {
      85      137597 :   GEN p = gel(v,1), pi = gel(v,2), m = zk_scalar_or_multable(nf, pi);
      86      137597 :   if (typ(m) == t_INT) return scalarmat(gcdii(m,p), nf_get_degree(nf));
      87      126692 :   return ZM_hnfmodid(m, p);
      88             : }
      89             : /* true nf */
      90             : GEN
      91     2297744 : pr_hnf(GEN nf, GEN pr)
      92             : {
      93     2297744 :   GEN p = pr_get_p(pr), m;
      94     2297737 :   if (pr_is_inert(pr)) return scalarmat(p, nf_get_degree(nf));
      95     2034589 :   m = zk_scalar_or_multable(nf, pr_get_gen(pr));
      96     2034496 :   return ZM_hnfmodprime(m, p);
      97             : }
      98             : 
      99             : GEN
     100      275885 : idealhnf_principal(GEN nf, GEN x)
     101             : {
     102             :   GEN cx;
     103      275885 :   x = nf_to_scalar_or_basis(nf, x);
     104      275885 :   switch(typ(x))
     105             :   {
     106      150682 :     case t_COL: break;
     107       98498 :     case t_INT:  if (!signe(x)) return cgetg(1,t_MAT);
     108       98078 :       return scalarmat(absi_shallow(x), nf_get_degree(nf));
     109       26705 :     case t_FRAC:
     110       26705 :       return scalarmat(Q_abs_shallow(x), nf_get_degree(nf));
     111           0 :     default: pari_err_TYPE("idealhnf",x);
     112             :   }
     113      150682 :   x = Q_primitive_part(x, &cx);
     114      150682 :   RgV_check_ZV(x, "idealhnf");
     115      150682 :   x = zk_multable(nf, x);
     116      150682 :   x = ZM_hnfmodid(x, zkmultable_capZ(x));
     117      150682 :   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          56 :     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      359154 : idealhnf_shallow(GEN nf, GEN x)
     133             : {
     134      359154 :   long tx = typ(x), lx = lg(x), N;
     135             : 
     136             :   /* cannot use idealtyp because here we allow non-square matrices */
     137      359154 :   if (tx == t_VEC && lx == 3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
     138      359154 :   if (tx == t_VEC && lx == 6) return pr_hnf(nf,x); /* PRIME */
     139      245341 :   switch(tx)
     140             :   {
     141       66946 :     case t_MAT:
     142             :     {
     143             :       GEN cx;
     144       66946 :       long nx = lx-1;
     145       66946 :       N = nf_get_degree(nf);
     146       66946 :       if (nx == 0) return cgetg(1, t_MAT);
     147       66925 :       if (nbrows(x) != N) pari_err_TYPE("idealhnf [wrong dimension]",x);
     148       66918 :       if (nx == 1) return idealhnf_principal(nf, gel(x,1));
     149             : 
     150       65308 :       if (nx == N && RgM_is_ZM(x) && ZM_ishnf(x)) return x;
     151       40628 :       x = Q_primitive_part(x, &cx);
     152       40628 :       if (nx < N) x = vec_mulid(nf, x, nx, N);
     153       40628 :       x = ZM_hnfmod(x, ZM_detmult(x));
     154       40628 :       return cx? ZM_Q_mul(x,cx): x;
     155             :     }
     156          14 :     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      178381 :     default: return idealhnf_principal(nf, x); /* PRINCIPAL */
     178             :   }
     179             : }
     180             : GEN
     181        5047 : idealhnf(GEN nf, GEN x)
     182             : {
     183        5047 :   pari_sp av = avma;
     184        5047 :   GEN y = idealhnf_shallow(checknf(nf), x);
     185        5033 :   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          49 : idealpow0(GEN nf, GEN x, GEN n, long flag)
     199             : {
     200          49 :   if (flag) return idealpowred(nf,x,n);
     201          42 :   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         147 : idealhnf0(GEN nf, GEN a, GEN b)
     284             : {
     285             :   long ta, tb;
     286             :   pari_sp av;
     287             :   GEN x;
     288         147 :   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      122112 : ok_elt(GEN x, GEN xZ, GEN y)
     310             : {
     311      122112 :   pari_sp av = avma;
     312      122112 :   return gc_bool(av, ZM_equal(x, ZM_hnfmodid(y, xZ)));
     313             : }
     314             : 
     315             : static GEN
     316       41685 : addmul_col(GEN a, long s, GEN b)
     317             : {
     318             :   long i,l;
     319       41685 :   if (!s) return a? leafcopy(a): a;
     320       41579 :   if (!a) return gmulsg(s,b);
     321       38982 :   l = lg(a);
     322      187482 :   for (i=1; i<l; i++)
     323      148500 :     if (signe(gel(b,i))) gel(a,i) = addii(gel(a,i), mulsi(s, gel(b,i)));
     324       38982 :   return a;
     325             : }
     326             : 
     327             : /* a <-- a + s * b, all coeffs integers */
     328             : static GEN
     329       20703 : addmul_mat(GEN a, long s, GEN b)
     330             : {
     331             :   long j,l;
     332             :   /* copy otherwise next call corrupts a */
     333       20703 :   if (!s) return a? RgM_shallowcopy(a): a;
     334       19350 :   if (!a) return gmulsg(s,b);
     335       10069 :   l = lg(a);
     336       45650 :   for (j=1; j<l; j++)
     337       35581 :     (void)addmul_col(gel(a,j), s, gel(b,j));
     338       10069 :   return a;
     339             : }
     340             : 
     341             : static GEN
     342       66213 : get_random_a(GEN nf, GEN x, GEN xZ)
     343             : {
     344             :   pari_sp av;
     345       66213 :   long i, lm, l = lg(x);
     346             :   GEN a, z, beta, mul;
     347             : 
     348       66213 :   beta= cgetg(l, t_VEC);
     349       66213 :   mul = cgetg(l, t_VEC); lm = 1; /* = lg(mul) */
     350             :   /* look for a in x such that a O/xZ = x O/xZ */
     351      126884 :   for (i = 2; i < l; i++)
     352             :   {
     353      124287 :     GEN xi = gel(x,i);
     354      124287 :     GEN t = FpM_red(zk_multable(nf,xi), xZ); /* ZM, cannot be a scalar */
     355      124287 :     if (gequal0(t)) continue;
     356      112831 :     if (ok_elt(x,xZ, t)) return xi;
     357       49215 :     gel(beta,lm) = xi;
     358             :     /* mul[i] = { canonical generators for x[i] O/xZ as Z-module } */
     359       49215 :     gel(mul,lm) = t; lm++;
     360             :   }
     361        2597 :   setlg(mul, lm);
     362        2597 :   setlg(beta,lm);
     363        2597 :   z = cgetg(lm, t_VECSMALL);
     364        9295 :   for(av = avma;; set_avma(av))
     365             :   {
     366       29998 :     for (a=NULL,i=1; i<lm; i++)
     367             :     {
     368       20703 :       long t = random_bits(4) - 7; /* in [-7,8] */
     369       20703 :       z[i] = t;
     370       20703 :       a = addmul_mat(a, t, gel(mul,i));
     371             :     }
     372             :     /* a = matrix (NOT HNF) of ideal generated by beta.z in O/xZ */
     373        9295 :     if (a && ok_elt(x,xZ, a)) break;
     374             :   }
     375        8701 :   for (a=NULL,i=1; i<lm; i++)
     376        6104 :     a = addmul_col(a, z[i], gel(beta,i));
     377        2597 :   return a;
     378             : }
     379             : 
     380             : /* x square matrix, assume it is HNF */
     381             : static GEN
     382      157546 : mat_ideal_two_elt(GEN nf, GEN x)
     383             : {
     384             :   GEN y, a, cx, xZ;
     385      157546 :   long N = nf_get_degree(nf);
     386             :   pari_sp av, tetpil;
     387             : 
     388      157546 :   if (lg(x)-1 != N) pari_err_DIM("idealtwoelt");
     389      157532 :   if (N == 2) return mkvec2copy(gcoeff(x,1,1), gel(x,2));
     390             : 
     391       73805 :   y = cgetg(3,t_VEC); av = avma;
     392       73805 :   cx = Q_content(x);
     393       73805 :   xZ = gcoeff(x,1,1);
     394       73805 :   if (gequal(xZ, cx)) /* x = (cx) */
     395             :   {
     396        3202 :     gel(y,1) = cx;
     397        3202 :     gel(y,2) = gen_0; return y;
     398             :   }
     399       70603 :   if (equali1(cx)) cx = NULL;
     400             :   else
     401             :   {
     402        1435 :     x = Q_div_to_int(x, cx);
     403        1435 :     xZ = gcoeff(x,1,1);
     404             :   }
     405       70603 :   if (N < 6)
     406       62722 :     a = get_random_a(nf, x, xZ);
     407             :   else
     408             :   {
     409        7881 :     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        7881 :     GEN P, E, a1 = Z_smoothen(xZ, (GEN)FB, &P, &E);
     413        7881 :     if (!a1) /* factors completely */
     414        4390 :       a = idealapprfact_i(nf, idealfactor(nf,x), 1);
     415        3491 :     else if (lg(P) == 1) /* no small factors */
     416        1830 :       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        1661 :       a0 = diviiexact(xZ, a1);
     421        1661 :       A0 = ZM_hnfmodid(x, a0); /* smooth part of x */
     422        1661 :       A1 = ZM_hnfmodid(x, a1); /* cofactor */
     423        1661 :       pi0 = idealapprfact_i(nf, idealfactor(nf,A0), 1);
     424        1661 :       pi1 = get_random_a(nf, A1, a1);
     425        1661 :       (void)bezout(a0, a1, &v0,&v1);
     426        1661 :       u0 = mulii(a0, v0);
     427        1661 :       u1 = mulii(a1, v1);
     428        1661 :       if (typ(pi0) != t_COL) t = addmulii(u0, pi0, u1);
     429             :       else
     430        1661 :       { t = ZC_Z_mul(pi0, u1); gel(t,1) = addii(gel(t,1), u0); }
     431        1661 :       u = ZC_Z_mul(pi1, u0); gel(u,1) = addii(gel(u,1), u1);
     432        1661 :       a = nfmuli(nf, centermod(u, xZ), centermod(t, xZ));
     433             :     }
     434             :   }
     435       70603 :   if (cx)
     436             :   {
     437        1435 :     a = centermod(a, xZ);
     438        1435 :     tetpil = avma;
     439        1435 :     if (typ(cx) == t_INT)
     440             :     {
     441          14 :       gel(y,1) = mulii(xZ, cx);
     442          14 :       gel(y,2) = ZC_Z_mul(a, cx);
     443             :     }
     444             :     else
     445             :     {
     446        1421 :       gel(y,1) = gmul(xZ, cx);
     447        1421 :       gel(y,2) = RgC_Rg_mul(a, cx);
     448             :     }
     449             :   }
     450             :   else
     451             :   {
     452       69168 :     tetpil = avma;
     453       69168 :     gel(y,1) = icopy(xZ);
     454       69168 :     gel(y,2) = centermod(a, xZ);
     455             :   }
     456       70603 :   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       34170 : idealtwoelt(GEN nf, GEN x)
     465             : {
     466             :   pari_sp av;
     467             :   GEN z;
     468       34170 :   long tx = idealtyp(&x,&z);
     469       34163 :   nf = checknf(nf);
     470       34163 :   if (tx == id_MAT) return mat_ideal_two_elt(nf,x);
     471        1960 :   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      260785 : idealHNF_norm_pval(GEN x, GEN p, long Zval)
     486             : {
     487      260785 :   long i, v = Zval, l = lg(x);
     488      880283 :   for (i = 2; i < l; i++) v += Z_pval(gcoeff(x,i,i), p);
     489      260785 :   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       53427 : idealHNF_Z_factor_i(GEN x, GEN f0, GEN *pvN, GEN *pvZ)
     496             : {
     497       53427 :   GEN P, E, vN, vZ, xZ = gcoeff(x,1,1), f = f0? f0: Z_factor(xZ);
     498             :   long i, l;
     499       53427 :   P = gel(f,1); l = lg(P);
     500       53427 :   E = gel(f,2);
     501       53427 :   *pvN = vN = cgetg(l, t_VECSMALL);
     502       53427 :   *pvZ = vZ = cgetg(l, t_VECSMALL);
     503      107123 :   for (i = 1; i < l; i++)
     504             :   {
     505       53696 :     GEN p = gel(P,i);
     506       53696 :     vZ[i] = f0? Z_pval(xZ, p): (long) itou(gel(E,i));
     507       53696 :     vN[i] = idealHNF_norm_pval(x,p, vZ[i]);
     508             :   }
     509       53427 :   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      287070 : idealHNF_val(GEN A, GEN P, long Nval, long Zval)
     521             : {
     522      287070 :   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      287070 :   if (Nval < f) return 0;
     527      286902 :   p = pr_get_p(P);
     528      286902 :   e = pr_get_e(P);
     529             :   /* v_P(A) <= max [ e * v_p(A \cap Z), floor[v_p(Nix) / f ] */
     530      286902 :   vmax = minss(Zval * e, Nval / f);
     531      286902 :   mul = pr_get_tau(P);
     532      286902 :   l = lg(mul);
     533      286902 :   B = cgetg(l,t_MAT);
     534             :   /* B[1] not needed: v_pr(A[1]) = v_pr(A \cap Z) is known already */
     535      286902 :   gel(B,1) = gen_0; /* dummy */
     536      875344 :   for (j = 2; j < l; j++)
     537             :   {
     538      681255 :     GEN x = gel(A,j);
     539      681255 :     gel(B,j) = y = cgetg(l, t_COL);
     540     5789846 :     for (i = 1; i < l; i++)
     541             :     { /* compute a = (x.t0)_i, A in HNF ==> x[j+1..l-1] = 0 */
     542     5201404 :       a = mulii(gel(x,1), gcoeff(mul,i,1));
     543    44268128 :       for (k = 2; k <= j; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     544             :       /* p | a ? */
     545     5201404 :       gel(y,i) = dvmdii(a,p,&r); if (signe(r)) return 0;
     546             :     }
     547             :   }
     548      194089 :   vals = cgetg(l, t_VECSMALL);
     549             :   /* vals[1] not needed */
     550      706506 :   for (j = 2; j < l; j++)
     551             :   {
     552      512417 :     gel(B,j) = Q_primitive_part(gel(B,j), &cx);
     553      512417 :     vals[j] = cx? 1 + e * Q_pval(cx, p): 1;
     554             :   }
     555      194089 :   pk = powiu(p, ceildivuu(vmax, e));
     556      194089 :   av = avma; y = cgetg(l,t_COL);
     557             :   /* can compute mod p^ceil((vmax-v)/e) */
     558      352606 :   for (v = 1; v < vmax; v++)
     559             :   { /* we know v_pr(Bj) >= v for all j */
     560      166461 :     if (e == 1 || (vmax - v) % e == 0) pk = diviiexact(pk, p);
     561     1010221 :     for (j = 2; j < l; j++)
     562             :     {
     563      851704 :       GEN x = gel(B,j); if (v < vals[j]) continue;
     564     5955678 :       for (i = 1; i < l; i++)
     565             :       {
     566     5481384 :         pari_sp av2 = avma;
     567     5481384 :         a = mulii(gel(x,1), gcoeff(mul,i,1));
     568   104791537 :         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     5481384 :         a = dvmdii(a,p,&r); if (signe(r)) return v;
     571     5473440 :         if (lgefint(a) > lgefint(pk)) a = remii(a, pk);
     572     5473440 :         gel(y,i) = gerepileuptoint(av2, a);
     573             :       }
     574      474294 :       gel(B,j) = y; y = x;
     575      474294 :       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      186145 :   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       53427 : idealHNF_factor_i(GEN nf, GEN x, GEN cx, GEN FA)
     589             : {
     590       53427 :   const long N = lg(x)-1;
     591             :   long i, j, k, l, v;
     592       53427 :   GEN vN, vZ, vP, vE, vp = idealHNF_Z_factor_i(x, FA, &vN,&vZ);
     593             : 
     594       53427 :   l = lg(vp);
     595       53427 :   i = cx? expi(cx)+1: 1;
     596       53427 :   vP = cgetg((l+i-2)*N+1, t_COL);
     597       53427 :   vE = cgetg((l+i-2)*N+1, t_COL);
     598      107123 :   for (i = k = 1; i < l; i++)
     599             :   {
     600       53696 :     GEN L, p = gel(vp,i);
     601       53696 :     long Nval = vN[i], Zval = vZ[i], vc = cx? Z_pvalrem(cx,p,&cx): 0;
     602       53696 :     if (vc)
     603             :     {
     604        4673 :       L = idealprimedec(nf,p);
     605        4673 :       if (is_pm1(cx)) cx = NULL;
     606             :     }
     607             :     else
     608       49023 :       L = idealprimedec_limit_f(nf,p,Nval);
     609      133677 :     for (j = 1; Nval && j < lg(L); j++) /* !Nval => only cx contributes */
     610             :     {
     611       79981 :       GEN P = gel(L,j);
     612       79981 :       pari_sp av = avma;
     613       79981 :       v = idealHNF_val(x, P, Nval, Zval);
     614       79981 :       set_avma(av);
     615       79981 :       Nval -= v*pr_get_f(P);
     616       79981 :       v += vc * pr_get_e(P); if (!v) continue;
     617       64164 :       gel(vP,k) = P;
     618       64164 :       gel(vE,k) = utoipos(v); k++;
     619             :     }
     620       56307 :     if (vc) for (; j<lg(L); j++)
     621             :     {
     622        2611 :       GEN P = gel(L,j);
     623        2611 :       gel(vP,k) = P;
     624        2611 :       gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     625             :     }
     626             :   }
     627       53427 :   if (cx && !FA)
     628             :   { /* complete factorization */
     629       10619 :     GEN f = Z_factor(cx), cP = gel(f,1), cE = gel(f,2);
     630       10619 :     long lc = lg(cP);
     631       22127 :     for (i=1; i<lc; i++)
     632             :     {
     633       11508 :       GEN p = gel(cP,i), L = idealprimedec(nf,p);
     634       11508 :       long vc = itos(gel(cE,i));
     635       25704 :       for (j=1; j<lg(L); j++)
     636             :       {
     637       14196 :         GEN P = gel(L,j);
     638       14196 :         gel(vP,k) = P;
     639       14196 :         gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     640             :       }
     641             :     }
     642             :   }
     643       53427 :   setlg(vP, k);
     644       53427 :   setlg(vE, k); return mkmat2(vP, vE);
     645             : }
     646             : /* true nf, x integral ideal */
     647             : static GEN
     648       52468 : idealHNF_factor(GEN nf, GEN x, ulong lim)
     649             : {
     650       52468 :   GEN cx, F = NULL;
     651       52468 :   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       52468 :   x = Q_primitive_part(x, &cx);
     661       52468 :   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        4480 : prV_e_muls(GEN L, long c)
     666             : {
     667        4480 :   long j, l = lg(L);
     668        4480 :   GEN z = cgetg(l, t_COL);
     669        9758 :   for (j = 1; j < l; j++) gel(z,j) = stoi(c * pr_get_e(gel(L,j)));
     670        4480 :   return z;
     671             : }
     672             : /* true nf, y in Q */
     673             : static GEN
     674        5089 : Q_nffactor(GEN nf, GEN y, ulong lim)
     675             : {
     676             :   GEN f, P, E;
     677             :   long l, i;
     678        5089 :   if (typ(y) == t_INT)
     679             :   {
     680        5061 :     if (!signe(y)) pari_err_DOMAIN("idealfactor", "ideal", "=",gen_0,y);
     681        5047 :     if (is_pm1(y)) return trivial_fact();
     682             :   }
     683        3339 :   y = Q_abs_shallow(y);
     684        3339 :   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        3339 :   P = gel(f,1); l = lg(P); if (l == 1) return f;
     697        3325 :   E = gel(f,2);
     698        7805 :   for (i = 1; i < l; i++)
     699             :   {
     700        4480 :     gel(P,i) = idealprimedec(nf, gel(P,i));
     701        4480 :     gel(E,i) = prV_e_muls(gel(P,i), itos(gel(E,i)));
     702             :   }
     703        3325 :   settyp(P,t_VEC); P = shallowconcat1(P);
     704        3325 :   settyp(E,t_VEC); E = shallowconcat1(E);
     705        3325 :   gel(f,1) = P; settyp(P, t_COL);
     706        3325 :   gel(f,2) = E; return f;
     707             : }
     708             : 
     709             : GEN
     710         476 : idealfactor_partial(GEN nf, GEN x, GEN L)
     711             : {
     712         476 :   pari_sp av = avma;
     713             :   long i, j, l;
     714             :   GEN P, E;
     715         476 :   if (!L) return idealfactor(nf, x);
     716          28 :   if (typ(L) == t_INT) return idealfactor_limit(nf, x, itou(L));
     717           7 :   P = cgetg_copy(L, &l);
     718          35 :   for (i = 1; i < l; i++)
     719             :   {
     720          28 :     GEN p = gel(L,i);
     721          28 :     gel(P,i) = typ(p) == t_INT? idealprimedec(nf, p): mkvec(p);
     722             :   }
     723           7 :   settyp(P, t_VEC); P = shallowconcat1(P);
     724           7 :   settyp(P, t_COL);
     725           7 :   P = gen_sort_uniq(P, (void*)&cmp_prime_over_p, &cmp_nodata);
     726           7 :   E = cgetg_copy(P, &l);
     727          35 :   for (i = j = 1; i < l; i++)
     728             :   {
     729          28 :     long v = idealval(nf, x, gel(P,i));
     730          28 :     if (v) { gel(P,j) = gel(P,i); gel(E,j) = stoi(v); j++; }
     731             :   }
     732           7 :   setlg(P,j);
     733           7 :   setlg(E,j); return gerepilecopy(av, mkmat2(P, E));
     734             : }
     735             : GEN
     736       57613 : idealfactor_limit(GEN nf, GEN x, ulong lim)
     737             : {
     738       57613 :   pari_sp av = avma;
     739             :   GEN fa, y;
     740       57613 :   long tx = idealtyp(&x,&y);
     741             : 
     742       57613 :   if (tx == id_PRIME)
     743             :   {
     744          63 :     if (lim && abscmpiu(pr_get_p(x), lim) >= 0) return trivial_fact();
     745          56 :     retmkmat2(mkcolcopy(x), mkcol(gen_1));
     746             :   }
     747       57550 :   nf = checknf(nf);
     748       57550 :   if (tx == id_PRINCIPAL)
     749             :   {
     750        7343 :     y = nf_to_scalar_or_basis(nf, x);
     751        7343 :     if (typ(y) != t_COL) return gerepilecopy(av, Q_nffactor(nf, y, lim));
     752             :   }
     753       52461 :   y = idealnumden(nf, x);
     754       52461 :   fa = idealHNF_factor(nf, gel(y,1), lim);
     755       52461 :   if (!isint1(gel(y,2)))
     756           7 :     fa = famat_div_shallow(fa, idealHNF_factor(nf, gel(y,2), lim));
     757       52461 :   fa = gerepilecopy(av, fa);
     758       52461 :   return sort_factor(fa, (void*)&cmp_prime_ideal, &cmp_nodata);
     759             : }
     760             : GEN
     761       57452 : idealfactor(GEN nf, GEN x) { return idealfactor_limit(nf, x, 0); }
     762             : GEN
     763         140 : gpidealfactor(GEN nf, GEN x, GEN lim)
     764             : {
     765         140 :   ulong L = 0;
     766         140 :   if (lim)
     767             :   {
     768          70 :     if (typ(lim) != t_INT || signe(lim) < 0) pari_err_FLAG("idealfactor");
     769          70 :     L = itou(lim);
     770             :   }
     771         140 :   return idealfactor_limit(nf, x, L);
     772             : }
     773             : 
     774             : static GEN
     775         924 : ramified_root(GEN nf, GEN R, GEN A, long n)
     776             : {
     777         924 :   GEN v, P = gel(idealfactor(nf, R), 1);
     778         924 :   long i, l = lg(P);
     779         924 :   v = cgetg(l, t_VECSMALL);
     780        1135 :   for (i = 1; i < l; i++)
     781             :   {
     782         218 :     long w = idealval(nf, A, gel(P,i));
     783         218 :     if (w % n) return NULL;
     784         211 :     v[i] = w / n;
     785             :   }
     786         917 :   return idealfactorback(nf, P, v, 0);
     787             : }
     788             : static int
     789           0 : ramified_root_simple(GEN nf, long n, GEN P, GEN v)
     790             : {
     791           0 :   long i, l = lg(v);
     792           0 :   for (i = 1; i < l; i++) if (v[i])
     793             :   {
     794           0 :     GEN vpr = idealprimedec(nf, gel(P,i));
     795           0 :     long lpr = lg(vpr), j;
     796           0 :     for (j = 1; j < lpr; j++)
     797             :     {
     798           0 :       long e = pr_get_e(gel(vpr,j));
     799           0 :       if ((e * v[i]) % n) return 0;
     800             :     }
     801             :   }
     802           0 :   return 1;
     803             : }
     804             : /* true nf; A is assumed to be the n-th power of an integral ideal,
     805             :  * return its n-th root; n > 1 */
     806             : static long
     807         924 : idealsqrtn_int(GEN nf, GEN A, long n, GEN *pB)
     808             : {
     809             :   GEN C, root;
     810             :   long i, l;
     811             : 
     812         924 :   if (typ(A) == t_INT) /* > 0 */
     813             :   {
     814         469 :     GEN P = nf_get_ramified_primes(nf), v, q;
     815         469 :     l = lg(P); v = cgetg(l, t_VECSMALL);
     816        1631 :     for (i = 1; i < l; i++) v[i] = Z_pvalrem(A, gel(P,i), &A);
     817         469 :     C = gen_1;
     818         469 :     if (!isint1(A) && !Z_ispowerall(A, n, pB? &C: NULL)) return 0;
     819         469 :     if (!pB) return ramified_root_simple(nf, n, P, v);
     820         469 :     q = factorback2(P, v);
     821         469 :     root = ramified_root(nf, q, q, n);
     822         469 :     if (!root) return 0;
     823         469 :     if (!equali1(C)) root = isint1(root)? C: ZM_Z_mul(root, C);
     824         469 :     *pB = root; return 1;
     825             :   }
     826             :   /* compute valuations at ramified primes */
     827         455 :   root = ramified_root(nf, idealadd(nf, nf_get_diff(nf), A), A, n);
     828         455 :   if (!root) return 0;
     829             :   /* remove ramified primes */
     830         448 :   if (isint1(root))
     831         272 :     root = matid(nf_get_degree(nf));
     832             :   else
     833         176 :     A = idealdivexact(nf, A, idealpows(nf,root,n));
     834         448 :   A = Q_primitive_part(A, &C);
     835         448 :   if (C)
     836             :   {
     837           0 :     if (!Z_ispowerall(C,n,&C)) return 0;
     838           0 :     if (pB) root = ZM_Z_mul(root, C);
     839             :   }
     840             : 
     841             :   /* compute final n-th root, at most degree(nf)-1 iterations */
     842         448 :   for (i = 0;; i++)
     843         505 :   {
     844         953 :     GEN J, b, a = gcoeff(A,1,1); /* A \cap Z */
     845         953 :     if (is_pm1(a)) break;
     846         512 :     if (!Z_ispowerall(a,n,&b)) return 0;
     847         505 :     J = idealadd(nf, b, A);
     848         505 :     A = idealdivexact(nf, idealpows(nf,J,n), A);
     849             :     /* div and not divexact here */
     850         505 :     if (pB) root = odd(i)? idealdiv(nf, root, J): idealmul(nf, root, J);
     851             :   }
     852         441 :   if (pB) *pB = root;
     853         441 :   return 1;
     854             : }
     855             : 
     856             : /* A is assumed to be the n-th power of an ideal in nf
     857             :  returns its n-th root. */
     858             : long
     859         476 : idealispower(GEN nf, GEN A, long n, GEN *pB)
     860             : {
     861         476 :   pari_sp av = avma;
     862             :   GEN v, N, D;
     863         476 :   nf = checknf(nf);
     864         476 :   if (n <= 0) pari_err_DOMAIN("idealispower", "n", "<=", gen_0, stoi(n));
     865         476 :   if (n == 1) { if (pB) *pB = idealhnf(nf,A); return 1; }
     866         469 :   v = idealnumden(nf,A);
     867         469 :   if (gequal0(gel(v,1))) { set_avma(av); if (pB) *pB = cgetg(1,t_MAT); return 1; }
     868         469 :   if (!idealsqrtn_int(nf, gel(v,1), n, pB? &N: NULL)) return 0;
     869         455 :   if (!idealsqrtn_int(nf, gel(v,2), n, pB? &D: NULL)) return 0;
     870         455 :   if (pB) *pB = gerepileupto(av, idealdiv(nf,N,D)); else set_avma(av);
     871         455 :   return 1;
     872             : }
     873             : 
     874             : /* x t_INT or integral non-0 ideal in HNF */
     875             : static GEN
     876        3920 : idealredmodpower_i(GEN nf, GEN x, ulong k, ulong B)
     877             : {
     878             :   GEN cx, y, U, N, F, Q;
     879             :   long nF;
     880        3920 :   if (typ(x) == t_INT)
     881             :   {
     882        2954 :     if (!signe(x) || is_pm1(x)) return gen_1;
     883         868 :     F = Z_factor_limit(x, B);
     884         868 :     gel(F,2) = gdiventgs(gel(F,2), k);
     885         868 :     return ginv(factorback(F));
     886             :   }
     887         966 :   N = gcoeff(x,1,1); if (is_pm1(N)) return gen_1;
     888         959 :   F = Z_factor_limit(N, B); nF=lg(gel(F,1))-1;
     889         959 :   if (BPSW_psp(gcoeff(F,nF,1))) U = NULL;
     890             :   else
     891             :   {
     892          63 :     GEN M = powii(gcoeff(F,nF,1), gcoeff(F,nF,2));
     893          63 :     y = hnfmodid(x, M); /* coprime part to B! */
     894          63 :     if (!idealispower(nf, y, k, &U)) U = NULL;
     895          63 :     x = hnfmodid(x, diviiexact(N, M));
     896          63 :     setlg(gel(F,1), nF); /* remove last entry (unfactored part) */
     897          63 :     setlg(gel(F,2), nF);
     898             :   }
     899             :   /* x = B-smooth part of initial x */
     900         959 :   x = Q_primitive_part(x, &cx);
     901         959 :   F = idealHNF_factor_i(nf, x, cx, F);
     902         959 :   gel(F,2) = gdiventgs(gel(F,2), k);
     903         959 :   Q = idealfactorback(nf, gel(F,1), gel(F,2), 0);
     904         959 :   if (U) Q = idealmul(nf,Q,U);
     905         959 :   if (typ(Q) == t_INT) return Q;
     906         931 :   y = idealred_elt(nf, idealHNF_inv_Z(nf, Q));
     907         931 :   return gdiv(y, gcoeff(Q,1,1));
     908             : }
     909             : GEN
     910        1967 : idealredmodpower(GEN nf, GEN x, ulong n, ulong B)
     911             : {
     912        1967 :   pari_sp av = avma;
     913             :   GEN a, b;
     914        1967 :   nf = checknf(nf);
     915        1967 :   if (!n) pari_err_DOMAIN("idealredmodpower","n", "=", gen_0, gen_0);
     916        1967 :   x = idealnumden(nf, x);
     917        1967 :   a = gel(x,1);
     918        1967 :   if (isintzero(a)) { set_avma(av); return gen_1; }
     919        1960 :   a = idealredmodpower_i(nf, gel(x,1), n, B);
     920        1960 :   b = idealredmodpower_i(nf, gel(x,2), n, B);
     921        1960 :   if (!isint1(b)) a = nf_to_scalar_or_basis(nf, nfdiv(nf, a, b));
     922        1960 :   return gerepilecopy(av, a);
     923             : }
     924             : 
     925             : /* P prime ideal in idealprimedec format. Return valuation(A) at P */
     926             : long
     927      646582 : idealval(GEN nf, GEN A, GEN P)
     928             : {
     929      646582 :   pari_sp av = avma;
     930             :   GEN a, p, cA;
     931      646582 :   long vcA, v, Zval, tx = idealtyp(&A,&a);
     932             : 
     933      646582 :   if (tx == id_PRINCIPAL) return nfval(nf,A,P);
     934      641689 :   checkprid(P);
     935      641689 :   if (tx == id_PRIME) return pr_equal(P, A)? 1: 0;
     936             :   /* id_MAT */
     937      641661 :   nf = checknf(nf);
     938      641661 :   A = Q_primitive_part(A, &cA);
     939      641661 :   p = pr_get_p(P);
     940      641661 :   vcA = cA? Q_pval(cA,p): 0;
     941      641661 :   if (pr_is_inert(P)) return gc_long(av,vcA);
     942      632638 :   Zval = Z_pval(gcoeff(A,1,1), p);
     943      632638 :   if (!Zval) v = 0;
     944             :   else
     945             :   {
     946      207089 :     long Nval = idealHNF_norm_pval(A, p, Zval);
     947      207089 :     v = idealHNF_val(A, P, Nval, Zval);
     948             :   }
     949      632638 :   return gc_long(av, vcA? v + vcA*pr_get_e(P): v);
     950             : }
     951             : GEN
     952        6615 : gpidealval(GEN nf, GEN ix, GEN P)
     953             : {
     954        6615 :   long v = idealval(nf,ix,P);
     955        6615 :   return v == LONG_MAX? mkoo(): stoi(v);
     956             : }
     957             : 
     958             : /* gcd and generalized Bezout */
     959             : 
     960             : GEN
     961       62665 : idealadd(GEN nf, GEN x, GEN y)
     962             : {
     963       62665 :   pari_sp av = avma;
     964             :   long tx, ty;
     965             :   GEN z, a, dx, dy, dz;
     966             : 
     967       62665 :   tx = idealtyp(&x,&z);
     968       62665 :   ty = idealtyp(&y,&z); nf = checknf(nf);
     969       62665 :   if (tx != id_MAT) x = idealhnf_shallow(nf,x);
     970       62665 :   if (ty != id_MAT) y = idealhnf_shallow(nf,y);
     971       62665 :   if (lg(x) == 1) return gerepilecopy(av,y);
     972       62651 :   if (lg(y) == 1) return gerepilecopy(av,x); /* check for 0 ideal */
     973       62343 :   dx = Q_denom(x);
     974       62343 :   dy = Q_denom(y); dz = lcmii(dx,dy);
     975       62343 :   if (is_pm1(dz)) dz = NULL; else {
     976       12915 :     x = Q_muli_to_int(x, dz);
     977       12915 :     y = Q_muli_to_int(y, dz);
     978             :   }
     979       62343 :   a = gcdii(gcoeff(x,1,1), gcoeff(y,1,1));
     980       62343 :   if (is_pm1(a))
     981             :   {
     982       29511 :     long N = lg(x)-1;
     983       29511 :     if (!dz) { set_avma(av); return matid(N); }
     984        3633 :     return gerepileupto(av, scalarmat(ginv(dz), N));
     985             :   }
     986       32832 :   z = ZM_hnfmodid(shallowconcat(x,y), a);
     987       32832 :   if (dz) z = RgM_Rg_div(z,dz);
     988       32832 :   return gerepileupto(av,z);
     989             : }
     990             : 
     991             : static GEN
     992          28 : trivial_merge(GEN x)
     993          28 : { return (lg(x) == 1 || !is_pm1(gcoeff(x,1,1)))? NULL: gen_1; }
     994             : /* true nf */
     995             : static GEN
     996      163290 : _idealaddtoone(GEN nf, GEN x, GEN y, long red)
     997             : {
     998             :   GEN a;
     999      163290 :   long tx = idealtyp(&x, &a/*junk*/);
    1000      163287 :   long ty = idealtyp(&y, &a/*junk*/);
    1001             :   long ea;
    1002      163276 :   if (tx != id_MAT) x = idealhnf_shallow(nf, x);
    1003      163289 :   if (ty != id_MAT) y = idealhnf_shallow(nf, y);
    1004      163289 :   if (lg(x) == 1)
    1005          14 :     a = trivial_merge(y);
    1006      163275 :   else if (lg(y) == 1)
    1007          14 :     a = trivial_merge(x);
    1008             :   else
    1009      163261 :     a = hnfmerge_get_1(x, y);
    1010      163291 :   if (!a) pari_err_COPRIME("idealaddtoone",x,y);
    1011      163273 :   if (red && (ea = gexpo(a)) > 10)
    1012             :   {
    1013        5425 :     GEN b = (typ(a) == t_COL)? a: scalarcol_shallow(a, nf_get_degree(nf));
    1014        5425 :     b = ZC_reducemodlll(b, idealHNF_mul(nf,x,y));
    1015        5425 :     if (gexpo(b) < ea) a = b;
    1016             :   }
    1017      163273 :   return a;
    1018             : }
    1019             : /* true nf */
    1020             : GEN
    1021       18144 : idealaddtoone_i(GEN nf, GEN x, GEN y)
    1022       18144 : { return _idealaddtoone(nf, x, y, 1); }
    1023             : /* true nf */
    1024             : GEN
    1025      145148 : idealaddtoone_raw(GEN nf, GEN x, GEN y)
    1026      145148 : { return _idealaddtoone(nf, x, y, 0); }
    1027             : 
    1028             : GEN
    1029          98 : idealaddtoone(GEN nf, GEN x, GEN y)
    1030             : {
    1031          98 :   GEN z = cgetg(3,t_VEC), a;
    1032          98 :   pari_sp av = avma;
    1033          98 :   nf = checknf(nf);
    1034          98 :   a = gerepileupto(av, idealaddtoone_i(nf,x,y));
    1035          84 :   gel(z,1) = a;
    1036          84 :   gel(z,2) = typ(a) == t_COL? Z_ZC_sub(gen_1,a): subui(1,a);
    1037          84 :   return z;
    1038             : }
    1039             : 
    1040             : /* assume elements of list are integral ideals */
    1041             : GEN
    1042          35 : idealaddmultoone(GEN nf, GEN list)
    1043             : {
    1044          35 :   pari_sp av = avma;
    1045          35 :   long N, i, l, nz, tx = typ(list);
    1046             :   GEN H, U, perm, L;
    1047             : 
    1048          35 :   nf = checknf(nf); N = nf_get_degree(nf);
    1049          35 :   if (!is_vec_t(tx)) pari_err_TYPE("idealaddmultoone",list);
    1050          35 :   l = lg(list);
    1051          35 :   L = cgetg(l, t_VEC);
    1052          35 :   if (l == 1)
    1053           0 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
    1054          35 :   nz = 0; /* number of non-zero ideals in L */
    1055          98 :   for (i=1; i<l; i++)
    1056             :   {
    1057          70 :     GEN I = gel(list,i);
    1058          70 :     if (typ(I) != t_MAT) I = idealhnf_shallow(nf,I);
    1059          70 :     if (lg(I) != 1)
    1060             :     {
    1061          42 :       nz++; RgM_check_ZM(I,"idealaddmultoone");
    1062          35 :       if (lgcols(I) != N+1) pari_err_TYPE("idealaddmultoone [not an ideal]", I);
    1063             :     }
    1064          63 :     gel(L,i) = I;
    1065             :   }
    1066          28 :   H = ZM_hnfperm(shallowconcat1(L), &U, &perm);
    1067          28 :   if (lg(H) == 1 || !equali1(gcoeff(H,1,1)))
    1068           7 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
    1069          49 :   for (i=1; i<=N; i++)
    1070          49 :     if (perm[i] == 1) break;
    1071          21 :   U = gel(U,(nz-1)*N + i); /* (L[1]|...|L[nz]) U = 1 */
    1072          21 :   nz = 0;
    1073          63 :   for (i=1; i<l; i++)
    1074             :   {
    1075          42 :     GEN c = gel(L,i);
    1076          42 :     if (lg(c) == 1)
    1077          14 :       c = gen_0;
    1078             :     else {
    1079          28 :       c = ZM_ZC_mul(c, vecslice(U, nz*N + 1, (nz+1)*N));
    1080          28 :       nz++;
    1081             :     }
    1082          42 :     gel(L,i) = c;
    1083             :   }
    1084          21 :   return gerepilecopy(av, L);
    1085             : }
    1086             : 
    1087             : /* multiplication */
    1088             : 
    1089             : /* x integral ideal (without archimedean component) in HNF form
    1090             :  * y = [a,alpha] corresponds to the integral ideal aZ_K+alpha Z_K, a in Z,
    1091             :  * alpha a ZV or a ZM (multiplication table). Multiply them */
    1092             : static GEN
    1093     2115817 : idealHNF_mul_two(GEN nf, GEN x, GEN y)
    1094             : {
    1095     2115817 :   GEN m, a = gel(y,1), alpha = gel(y,2);
    1096             :   long i, N;
    1097             : 
    1098     2115817 :   if (typ(alpha) != t_MAT)
    1099             :   {
    1100     1969405 :     alpha = zk_scalar_or_multable(nf, alpha);
    1101     1969405 :     if (typ(alpha) == t_INT) /* e.g. y inert ? 0 should not (but may) occur */
    1102        3624 :       return signe(a)? ZM_Z_mul(x, gcdii(a, alpha)): cgetg(1,t_MAT);
    1103             :   }
    1104     2112193 :   N = lg(x)-1; m = cgetg((N<<1)+1,t_MAT);
    1105     7104763 :   for (i=1; i<=N; i++) gel(m,i)   = ZM_ZC_mul(alpha,gel(x,i));
    1106     7104762 :   for (i=1; i<=N; i++) gel(m,i+N) = ZC_Z_mul(gel(x,i), a);
    1107     2112192 :   return ZM_hnfmodid(m, mulii(a, gcoeff(x,1,1)));
    1108             : }
    1109             : 
    1110             : /* Assume ix and iy are integral in HNF form [NOT extended]. Not memory clean.
    1111             :  * HACK: ideal in iy can be of the form [a,b], a in Z, b in Z_K */
    1112             : GEN
    1113      707152 : idealHNF_mul(GEN nf, GEN x, GEN y)
    1114             : {
    1115             :   GEN z;
    1116      707152 :   if (typ(y) == t_VEC)
    1117      616134 :     z = idealHNF_mul_two(nf,x,y);
    1118             :   else
    1119             :   { /* reduce one ideal to two-elt form. The smallest */
    1120       91018 :     GEN xZ = gcoeff(x,1,1), yZ = gcoeff(y,1,1);
    1121       91018 :     if (cmpii(xZ, yZ) < 0)
    1122             :     {
    1123       31465 :       if (is_pm1(xZ)) return gcopy(y);
    1124       20182 :       z = idealHNF_mul_two(nf, y, mat_ideal_two_elt(nf,x));
    1125             :     }
    1126             :     else
    1127             :     {
    1128       59553 :       if (is_pm1(yZ)) return gcopy(x);
    1129       29145 :       z = idealHNF_mul_two(nf, x, mat_ideal_two_elt(nf,y));
    1130             :     }
    1131             :   }
    1132      665461 :   return z;
    1133             : }
    1134             : 
    1135             : /* operations on elements in factored form */
    1136             : 
    1137             : GEN
    1138       78637 : famat_mul_shallow(GEN f, GEN g)
    1139             : {
    1140       78637 :   if (typ(f) != t_MAT) f = to_famat_shallow(f,gen_1);
    1141       78637 :   if (typ(g) != t_MAT) g = to_famat_shallow(g,gen_1);
    1142       78637 :   if (lgcols(f) == 1) return g;
    1143       63564 :   if (lgcols(g) == 1) return f;
    1144       63396 :   return mkmat2(shallowconcat(gel(f,1), gel(g,1)),
    1145       63396 :                 shallowconcat(gel(f,2), gel(g,2)));
    1146             : }
    1147             : GEN
    1148       38931 : famat_mulpow_shallow(GEN f, GEN g, GEN e)
    1149             : {
    1150       38931 :   if (!signe(e)) return f;
    1151       38074 :   return famat_mul_shallow(f, famat_pow_shallow(g, e));
    1152             : }
    1153             : 
    1154             : GEN
    1155       20832 : famat_mulpows_shallow(GEN f, GEN g, long e)
    1156             : {
    1157       20832 :   if (e==0) return f;
    1158       15798 :   return famat_mul_shallow(f, famat_pows_shallow(g, e));
    1159             : }
    1160             : 
    1161             : GEN
    1162        5285 : famat_div_shallow(GEN f, GEN g)
    1163        5285 : { return famat_mul_shallow(f, famat_inv_shallow(g)); }
    1164             : 
    1165             : GEN
    1166           0 : to_famat(GEN x, GEN y) { retmkmat2(mkcolcopy(x), mkcolcopy(y)); }
    1167             : GEN
    1168     1111171 : to_famat_shallow(GEN x, GEN y) { return mkmat2(mkcol(x), mkcol(y)); }
    1169             : 
    1170             : /* concat the single elt x; not gconcat since x may be a t_COL */
    1171             : static GEN
    1172       26016 : append(GEN v, GEN x)
    1173             : {
    1174       26016 :   long i, l = lg(v);
    1175       26016 :   GEN w = cgetg(l+1, typ(v));
    1176      156209 :   for (i=1; i<l; i++) gel(w,i) = gcopy(gel(v,i));
    1177       26016 :   gel(w,i) = gcopy(x); return w;
    1178             : }
    1179             : /* add x^1 to famat f */
    1180             : static GEN
    1181       47907 : famat_add(GEN f, GEN x)
    1182             : {
    1183       47907 :   GEN h = cgetg(3,t_MAT);
    1184       47907 :   if (lgcols(f) == 1)
    1185             :   {
    1186       21975 :     gel(h,1) = mkcolcopy(x);
    1187       21975 :     gel(h,2) = mkcol(gen_1);
    1188             :   }
    1189             :   else
    1190             :   {
    1191       25932 :     gel(h,1) = append(gel(f,1), x);
    1192       25932 :     gel(h,2) = gconcat(gel(f,2), gen_1);
    1193             :   }
    1194       47907 :   return h;
    1195             : }
    1196             : /* add x^-1 to famat f */
    1197             : static GEN
    1198          84 : famat_sub(GEN f, GEN x)
    1199             : {
    1200          84 :   GEN h = cgetg(3,t_MAT);
    1201          84 :   if (lgcols(f) == 1)
    1202             :   {
    1203           0 :     gel(h,1) = mkcolcopy(x);
    1204           0 :     gel(h,2) = mkcol(gen_m1);
    1205             :   }
    1206             :   else
    1207             :   {
    1208          84 :     gel(h,1) = append(gel(f,1), x);
    1209          84 :     gel(h,2) = gconcat(gel(f,2), gen_m1);
    1210             :   }
    1211          84 :   return h;
    1212             : }
    1213             : 
    1214             : GEN
    1215       54497 : famat_mul(GEN f, GEN g)
    1216             : {
    1217             :   GEN h;
    1218       54497 :   if (typ(g) != t_MAT) {
    1219       47865 :     if (typ(f) == t_MAT) return famat_add(f, g);
    1220           0 :     h = cgetg(3, t_MAT);
    1221           0 :     gel(h,1) = mkcol2(gcopy(f), gcopy(g));
    1222           0 :     gel(h,2) = mkcol2(gen_1, gen_1);
    1223             :   }
    1224        6632 :   if (typ(f) != t_MAT) return famat_add(g, f);
    1225        6590 :   if (lgcols(f) == 1) return gcopy(g);
    1226        4274 :   if (lgcols(g) == 1) return gcopy(f);
    1227        1879 :   h = cgetg(3,t_MAT);
    1228        1879 :   gel(h,1) = gconcat(gel(f,1), gel(g,1));
    1229        1879 :   gel(h,2) = gconcat(gel(f,2), gel(g,2));
    1230        1879 :   return h;
    1231             : }
    1232             : 
    1233             : GEN
    1234          91 : famat_div(GEN f, GEN g)
    1235             : {
    1236             :   GEN h;
    1237          91 :   if (typ(g) != t_MAT) {
    1238          42 :     if (typ(f) == t_MAT) return famat_sub(f, g);
    1239           0 :     h = cgetg(3, t_MAT);
    1240           0 :     gel(h,1) = mkcol2(gcopy(f), gcopy(g));
    1241           0 :     gel(h,2) = mkcol2(gen_1, gen_m1);
    1242             :   }
    1243          49 :   if (typ(f) != t_MAT) return famat_sub(g, f);
    1244           7 :   if (lgcols(f) == 1) return famat_inv(g);
    1245           7 :   if (lgcols(g) == 1) return gcopy(f);
    1246           7 :   h = cgetg(3,t_MAT);
    1247           7 :   gel(h,1) = gconcat(gel(f,1), gel(g,1));
    1248           7 :   gel(h,2) = gconcat(gel(f,2), gneg(gel(g,2)));
    1249           7 :   return h;
    1250             : }
    1251             : 
    1252             : GEN
    1253       16382 : famat_sqr(GEN f)
    1254             : {
    1255             :   GEN h;
    1256       16382 :   if (typ(f) != t_MAT) return to_famat(f,gen_2);
    1257       16382 :   if (lgcols(f) == 1) return gcopy(f);
    1258       10136 :   h = cgetg(3,t_MAT);
    1259       10136 :   gel(h,1) = gcopy(gel(f,1));
    1260       10136 :   gel(h,2) = gmul2n(gel(f,2),1);
    1261       10136 :   return h;
    1262             : }
    1263             : 
    1264             : GEN
    1265       21357 : famat_inv_shallow(GEN f)
    1266             : {
    1267       21357 :   if (typ(f) != t_MAT) return to_famat_shallow(f,gen_m1);
    1268        5425 :   if (lgcols(f) == 1) return f;
    1269        5425 :   return mkmat2(gel(f,1), ZC_neg(gel(f,2)));
    1270             : }
    1271             : GEN
    1272        7559 : famat_inv(GEN f)
    1273             : {
    1274        7559 :   if (typ(f) != t_MAT) return to_famat(f,gen_m1);
    1275        7559 :   if (lgcols(f) == 1) return gcopy(f);
    1276        1618 :   retmkmat2(gcopy(gel(f,1)), ZC_neg(gel(f,2)));
    1277             : }
    1278             : GEN
    1279          14 : famat_pow(GEN f, GEN n)
    1280             : {
    1281          14 :   if (typ(f) != t_MAT) return to_famat(f,n);
    1282          14 :   if (lgcols(f) == 1) return gcopy(f);
    1283          14 :   retmkmat2(gcopy(gel(f,1)), ZC_Z_mul(gel(f,2),n));
    1284             : }
    1285             : GEN
    1286       38074 : famat_pow_shallow(GEN f, GEN n)
    1287             : {
    1288       38074 :   if (is_pm1(n)) return signe(n) > 0? f: famat_inv_shallow(f);
    1289       21120 :   if (typ(f) != t_MAT) return to_famat_shallow(f,n);
    1290         442 :   if (lgcols(f) == 1) return f;
    1291         183 :   return mkmat2(gel(f,1), ZC_Z_mul(gel(f,2),n));
    1292             : }
    1293             : 
    1294             : GEN
    1295       21076 : famat_pows_shallow(GEN f, long n)
    1296             : {
    1297       21076 :   if (n==1) return f;
    1298       16285 :   if (n==-1) return famat_inv_shallow(f);
    1299       16278 :   if (typ(f) != t_MAT) return to_famat_shallow(f, stoi(n));
    1300        8235 :   if (lgcols(f) == 1) return f;
    1301        8235 :   return mkmat2(gel(f,1), ZC_z_mul(gel(f,2),n));
    1302             : }
    1303             : 
    1304             : GEN
    1305           0 : famat_Z_gcd(GEN M, GEN n)
    1306             : {
    1307           0 :   pari_sp av=avma;
    1308           0 :   long i, j, l=lgcols(M);
    1309           0 :   GEN F=cgetg(3,t_MAT);
    1310           0 :   gel(F,1)=cgetg(l,t_COL);
    1311           0 :   gel(F,2)=cgetg(l,t_COL);
    1312           0 :   for (i=1, j=1; i<l; i++)
    1313             :   {
    1314           0 :     GEN p = gcoeff(M,i,1);
    1315           0 :     GEN e = gminsg(Z_pval(n,p),gcoeff(M,i,2));
    1316           0 :     if (signe(e))
    1317             :     {
    1318           0 :       gcoeff(F,j,1)=p;
    1319           0 :       gcoeff(F,j,2)=e;
    1320           0 :       j++;
    1321             :     }
    1322             :   }
    1323           0 :   setlg(gel(F,1),j); setlg(gel(F,2),j);
    1324           0 :   return gerepilecopy(av,F);
    1325             : }
    1326             : 
    1327             : /* x assumed to be a t_MATs (factorization matrix), or compatible with
    1328             :  * the element_* functions. */
    1329             : static GEN
    1330       26973 : ext_sqr(GEN nf, GEN x)
    1331       26973 : { return (typ(x)==t_MAT)? famat_sqr(x): nfsqr(nf, x); }
    1332             : static GEN
    1333       89819 : ext_mul(GEN nf, GEN x, GEN y)
    1334       89819 : { return (typ(x)==t_MAT)? famat_mul(x,y): nfmul(nf, x, y); }
    1335             : static GEN
    1336        7559 : ext_inv(GEN nf, GEN x)
    1337        7559 : { return (typ(x)==t_MAT)? famat_inv(x): nfinv(nf, x); }
    1338             : static GEN
    1339           0 : ext_pow(GEN nf, GEN x, GEN n)
    1340           0 : { return (typ(x)==t_MAT)? famat_pow(x,n): nfpow(nf, x, n); }
    1341             : 
    1342             : GEN
    1343           0 : famat_to_nf(GEN nf, GEN f)
    1344             : {
    1345             :   GEN t, x, e;
    1346             :   long i;
    1347           0 :   if (lgcols(f) == 1) return gen_1;
    1348           0 :   x = gel(f,1);
    1349           0 :   e = gel(f,2);
    1350           0 :   t = nfpow(nf, gel(x,1), gel(e,1));
    1351           0 :   for (i=lg(x)-1; i>1; i--)
    1352           0 :     t = nfmul(nf, t, nfpow(nf, gel(x,i), gel(e,i)));
    1353           0 :   return t;
    1354             : }
    1355             : 
    1356             : GEN
    1357           0 : famat_idealfactor(GEN nf, GEN x)
    1358             : {
    1359             :   long i, l;
    1360           0 :   GEN g = gel(x,1), e = gel(x,2), h = cgetg_copy(g, &l);
    1361           0 :   for (i = 1; i < l; i++) gel(h,i) = idealfactor(nf, gel(g,i));
    1362           0 :   h = famat_reduce(famatV_factorback(h,e));
    1363           0 :   return sort_factor(h, (void*)&cmp_prime_ideal, &cmp_nodata);
    1364             : }
    1365             : 
    1366             : GEN
    1367       47502 : famat_reduce(GEN fa)
    1368             : {
    1369             :   GEN E, G, L, g, e;
    1370             :   long i, k, l;
    1371             : 
    1372       47502 :   if (lgcols(fa) == 1) return fa;
    1373       41735 :   g = gel(fa,1); l = lg(g);
    1374       41735 :   e = gel(fa,2);
    1375       41735 :   L = gen_indexsort(g, (void*)&cmp_universal, &cmp_nodata);
    1376       41735 :   G = cgetg(l, t_COL);
    1377       41735 :   E = cgetg(l, t_COL);
    1378             :   /* merge */
    1379      170055 :   for (k=i=1; i<l; i++,k++)
    1380             :   {
    1381      128320 :     gel(G,k) = gel(g,L[i]);
    1382      128320 :     gel(E,k) = gel(e,L[i]);
    1383      128320 :     if (k > 1 && gidentical(gel(G,k), gel(G,k-1)))
    1384             :     {
    1385       24837 :       gel(E,k-1) = addii(gel(E,k), gel(E,k-1));
    1386       24837 :       k--;
    1387             :     }
    1388             :   }
    1389             :   /* kill 0 exponents */
    1390       41735 :   l = k;
    1391      145218 :   for (k=i=1; i<l; i++)
    1392      103483 :     if (!gequal0(gel(E,i)))
    1393             :     {
    1394       98479 :       gel(G,k) = gel(G,i);
    1395       98479 :       gel(E,k) = gel(E,i); k++;
    1396             :     }
    1397       41735 :   setlg(G, k);
    1398       41735 :   setlg(E, k); return mkmat2(G,E);
    1399             : }
    1400             : GEN
    1401          35 : matreduce(GEN f)
    1402          35 : { pari_sp av = avma;
    1403          35 :   if (typ(f) != t_MAT || lg(f) != 3) pari_err_TYPE("matreduce", f);
    1404          21 :   if (typ(gel(f,1)) == t_VECSMALL)
    1405           0 :     f = famatsmall_reduce(f);
    1406             :   else
    1407             :   {
    1408          21 :     if (!RgV_is_ZV(gel(f,2))) pari_err_TYPE("matreduce",f);
    1409          14 :     f = famat_reduce(f);
    1410             :   }
    1411          14 :   return gerepilecopy(av, f);
    1412             : }
    1413             : 
    1414             : GEN
    1415       14658 : famatsmall_reduce(GEN fa)
    1416             : {
    1417             :   GEN E, G, L, g, e;
    1418             :   long i, k, l;
    1419       14658 :   if (lgcols(fa) == 1) return fa;
    1420       14658 :   g = gel(fa,1); l = lg(g);
    1421       14658 :   e = gel(fa,2);
    1422       14658 :   L = vecsmall_indexsort(g);
    1423       14659 :   G = cgetg(l, t_VECSMALL);
    1424       14659 :   E = cgetg(l, t_VECSMALL);
    1425             :   /* merge */
    1426      131069 :   for (k=i=1; i<l; i++,k++)
    1427             :   {
    1428      116410 :     G[k] = g[L[i]];
    1429      116410 :     E[k] = e[L[i]];
    1430      116410 :     if (k > 1 && G[k] == G[k-1])
    1431             :     {
    1432        7074 :       E[k-1] += E[k];
    1433        7074 :       k--;
    1434             :     }
    1435             :   }
    1436             :   /* kill 0 exponents */
    1437       14659 :   l = k;
    1438      123995 :   for (k=i=1; i<l; i++)
    1439      109336 :     if (E[i])
    1440             :     {
    1441      105625 :       G[k] = G[i];
    1442      105625 :       E[k] = E[i]; k++;
    1443             :     }
    1444       14659 :   setlg(G, k);
    1445       14659 :   setlg(E, k); return mkmat2(G,E);
    1446             : }
    1447             : 
    1448             : GEN
    1449        3698 : famat_remove_trivial(GEN fa)
    1450             : {
    1451        3698 :   GEN P, E, p = gel(fa,1), e = gel(fa,2);
    1452        3698 :   long j, k, l = lg(p);
    1453        3698 :   P = cgetg(l, t_COL);
    1454        3698 :   E = cgetg(l, t_COL);
    1455      283155 :   for (j = k = 1; j < l; j++)
    1456      279457 :     if (signe(gel(e,j))) { gel(P,k) = gel(p,j); gel(E,k++) = gel(e,j); }
    1457        3698 :   setlg(P, k); setlg(E, k); return mkmat2(P,E);
    1458             : }
    1459             : 
    1460             : GEN
    1461         581 : famatV_factorback(GEN v, GEN e)
    1462             : {
    1463         581 :   long i, l = lg(e);
    1464         581 :   GEN V = trivial_fact();
    1465        2097 :   for (i=1; i<l; i++) V = famat_mulpow_shallow(V, gel(v,i), gel(e,i));
    1466         581 :   return V;
    1467             : }
    1468             : 
    1469             : GEN
    1470        3388 : famatV_zv_factorback(GEN v, GEN e)
    1471             : {
    1472        3388 :   long i, l = lg(e);
    1473        3388 :   GEN V = trivial_fact();
    1474       13825 :   for (i=1; i<l; i++) V = famat_mulpows_shallow(V, gel(v,i), uel(e,i));
    1475        3388 :   return V;
    1476             : }
    1477             : 
    1478             : GEN
    1479       13384 : ZM_famat_limit(GEN fa, GEN limit)
    1480             : {
    1481             :   pari_sp av;
    1482             :   GEN E, G, g, e, r;
    1483             :   long i, k, l, n, lG;
    1484             : 
    1485       13384 :   if (lgcols(fa) == 1) return fa;
    1486       13377 :   g = gel(fa,1); l = lg(g);
    1487       13377 :   e = gel(fa,2);
    1488       27139 :   for(n=0, i=1; i<l; i++)
    1489       13762 :     if (cmpii(gel(g,i),limit)<=0) n++;
    1490       13377 :   lG = n<l-1 ? n+2 : n+1;
    1491       13377 :   G = cgetg(lG, t_COL);
    1492       13377 :   E = cgetg(lG, t_COL);
    1493       13377 :   av = avma;
    1494       27139 :   for (i=1, k=1, r = gen_1; i<l; i++)
    1495             :   {
    1496       13762 :     if (cmpii(gel(g,i),limit)<=0)
    1497             :     {
    1498       13643 :       gel(G,k) = gel(g,i);
    1499       13643 :       gel(E,k) = gel(e,i);
    1500       13643 :       k++;
    1501         119 :     } else r = mulii(r, powii(gel(g,i), gel(e,i)));
    1502             :   }
    1503       13377 :   if (k<i)
    1504             :   {
    1505         119 :     gel(G, k) = gerepileuptoint(av, r);
    1506         119 :     gel(E, k) = gen_1;
    1507             :   }
    1508       13377 :   return mkmat2(G,E);
    1509             : }
    1510             : 
    1511             : /* assume pr has degree 1 and coprime to Q_denom(x) */
    1512             : static GEN
    1513       23499 : to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1514             : {
    1515       23499 :   GEN d, r, p = modpr_get_p(modpr);
    1516       23499 :   x = nf_to_scalar_or_basis(nf,x);
    1517       23499 :   if (typ(x) != t_COL) return Rg_to_Fp(x,p);
    1518       22981 :   x = Q_remove_denom(x, &d);
    1519       22981 :   r = zk_to_Fq(x, modpr);
    1520       22981 :   if (d) r = Fp_div(r, d, p);
    1521       22981 :   return r;
    1522             : }
    1523             : 
    1524             : /* pr coprime to all denominators occurring in x */
    1525             : static GEN
    1526         777 : famat_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1527             : {
    1528         777 :   GEN p = modpr_get_p(modpr);
    1529         777 :   GEN t = NULL, g = gel(x,1), e = gel(x,2), q = subiu(p,1);
    1530         777 :   long i, l = lg(g);
    1531        3206 :   for (i = 1; i < l; i++)
    1532             :   {
    1533        2429 :     GEN n = modii(gel(e,i), q);
    1534        2429 :     if (signe(n))
    1535             :     {
    1536        2387 :       GEN h = to_Fp_coprime(nf, gel(g,i), modpr);
    1537        2387 :       h = Fp_pow(h, n, p);
    1538        2387 :       t = t? Fp_mul(t, h, p): h;
    1539             :     }
    1540             :   }
    1541         777 :   return t? modii(t, p): gen_1;
    1542             : }
    1543             : 
    1544             : /* cf famat_to_nf_modideal_coprime, modpr attached to prime of degree 1 */
    1545             : GEN
    1546       21889 : nf_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1547             : {
    1548         777 :   return typ(x)==t_MAT? famat_to_Fp_coprime(nf, x, modpr)
    1549       22666 :                       : to_Fp_coprime(nf, x, modpr);
    1550             : }
    1551             : 
    1552             : static long
    1553      688168 : zk_pvalrem(GEN x, GEN p, GEN *py)
    1554      688168 : { return (typ(x) == t_INT)? Z_pvalrem(x, p, py): ZV_pvalrem(x, p, py); }
    1555             : /* x a QC or Q. Return a ZC or Z, whose content is coprime to Z. Set v, dx
    1556             :  * such that x = p^v (newx / dx); dx = NULL if 1 */
    1557             : static GEN
    1558      782124 : nf_remove_denom_p(GEN nf, GEN x, GEN p, GEN *pdx, long *pv)
    1559             : {
    1560             :   long vcx;
    1561             :   GEN dx;
    1562      782124 :   x = nf_to_scalar_or_basis(nf, x);
    1563      782124 :   x = Q_remove_denom(x, &dx);
    1564      782124 :   if (dx)
    1565             :   {
    1566      183098 :     vcx = - Z_pvalrem(dx, p, &dx);
    1567      183098 :     if (!vcx) vcx = zk_pvalrem(x, p, &x);
    1568      183098 :     if (isint1(dx)) dx = NULL;
    1569             :   }
    1570             :   else
    1571             :   {
    1572      599026 :     vcx = zk_pvalrem(x, p, &x);
    1573      599026 :     dx = NULL;
    1574             :   }
    1575      782124 :   *pv = vcx;
    1576      782124 :   *pdx = dx; return x;
    1577             : }
    1578             : /* x = b^e/p^(e-1) in Z_K; x = 0 mod p/pr^e, (x,pr) = 1. Return NULL
    1579             :  * if p inert (instead of 1) */
    1580             : static GEN
    1581       55223 : p_makecoprime(GEN pr)
    1582             : {
    1583       55223 :   GEN B = pr_get_tau(pr), b;
    1584             :   long i, e;
    1585             : 
    1586       55223 :   if (typ(B) == t_INT) return NULL;
    1587       55083 :   b = gel(B,1); /* B = multiplication table by b */
    1588       55083 :   e = pr_get_e(pr);
    1589       55083 :   if (e == 1) return b;
    1590             :   /* one could also divide (exactly) by p in each iteration */
    1591       35016 :   for (i = 1; i < e; i++) b = ZM_ZC_mul(B, b);
    1592       16949 :   return ZC_Z_divexact(b, powiu(pr_get_p(pr), e-1));
    1593             : }
    1594             : 
    1595             : /* Compute A = prod g[i]^e[i] mod pr^k, assuming (A, pr) = 1.
    1596             :  * Method: modify each g[i] so that it becomes coprime to pr,
    1597             :  * g[i] *= (b/p)^v_pr(g[i]), where b/p = pr^(-1) times something integral
    1598             :  * and prime to p; globally, we multiply by (b/p)^v_pr(A) = 1.
    1599             :  * Optimizations:
    1600             :  * 1) remove all powers of p from contents, and consider extra generator p^vp;
    1601             :  * modified as p * (b/p)^e = b^e / p^(e-1)
    1602             :  * 2) remove denominators, coprime to p, by multiplying by inverse mod prk\cap Z
    1603             :  *
    1604             :  * EX = multiple of exponent of (O_K / pr^k)^* used to reduce the product in
    1605             :  * case the e[i] are large */
    1606             : GEN
    1607      345903 : famat_makecoprime(GEN nf, GEN g, GEN e, GEN pr, GEN prk, GEN EX)
    1608             : {
    1609      345903 :   GEN G, E, t, vp = NULL, p = pr_get_p(pr), prkZ = gcoeff(prk, 1,1);
    1610      345903 :   long i, l = lg(g);
    1611             : 
    1612      345903 :   G = cgetg(l+1, t_VEC);
    1613      345903 :   E = cgetg(l+1, t_VEC); /* l+1: room for "modified p" */
    1614     1128027 :   for (i=1; i < l; i++)
    1615             :   {
    1616             :     long vcx;
    1617      782124 :     GEN dx, x = nf_remove_denom_p(nf, gel(g,i), p, &dx, &vcx);
    1618      782124 :     if (vcx) /* = v_p(content(g[i])) */
    1619             :     {
    1620      101156 :       GEN a = mulsi(vcx, gel(e,i));
    1621      101156 :       vp = vp? addii(vp, a): a;
    1622             :     }
    1623             :     /* x integral, content coprime to p; dx coprime to p */
    1624      782124 :     if (typ(x) == t_INT)
    1625             :     { /* x coprime to p, hence to pr */
    1626      107853 :       x = modii(x, prkZ);
    1627      107853 :       if (dx) x = Fp_div(x, dx, prkZ);
    1628             :     }
    1629             :     else
    1630             :     {
    1631      674271 :       (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
    1632      674271 :       x = ZC_hnfrem(FpC_red(x,prkZ), prk);
    1633      674271 :       if (dx) x = FpC_Fp_mul(x, Fp_inv(dx,prkZ), prkZ);
    1634             :     }
    1635      782124 :     gel(G,i) = x;
    1636      782124 :     gel(E,i) = gel(e,i);
    1637             :   }
    1638             : 
    1639      345903 :   t = vp? p_makecoprime(pr): NULL;
    1640      345903 :   if (!t)
    1641             :   { /* no need for extra generator */
    1642      290841 :     setlg(G,l);
    1643      290841 :     setlg(E,l);
    1644             :   }
    1645             :   else
    1646             :   {
    1647       55062 :     gel(G,i) = FpC_red(t, prkZ);
    1648       55062 :     gel(E,i) = vp;
    1649             :   }
    1650      345903 :   return famat_to_nf_modideal_coprime(nf, G, E, prk, EX);
    1651             : }
    1652             : 
    1653             : /* simplified version of famat_makecoprime for X = SUnits[1] */
    1654             : GEN
    1655          42 : sunits_makecoprime(GEN X, GEN pr, GEN prk)
    1656             : {
    1657          42 :   GEN G, p = pr_get_p(pr), prkZ = gcoeff(prk,1,1);
    1658          42 :   long i, l = lg(X);
    1659             : 
    1660          42 :   G = cgetg(l, t_VEC);
    1661        3899 :   for (i = 1; i < l; i++)
    1662             :   {
    1663        3857 :     GEN x = gel(X,i);
    1664        3857 :     if (typ(x) == t_INT) /* a prime */
    1665         952 :       x = equalii(x,p)? p_makecoprime(pr): modii(x, prkZ);
    1666             :     else
    1667             :     {
    1668        2905 :       (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
    1669        2905 :       x = ZC_hnfrem(FpC_red(x,prkZ), prk);
    1670             :     }
    1671        3857 :     gel(G,i) = x;
    1672             :   }
    1673          42 :   return G;
    1674             : }
    1675             : 
    1676             : /* prod g[i]^e[i] mod bid, assume (g[i], id) = 1 and 1 < lg(g) <= lg(e) */
    1677             : GEN
    1678       18823 : famat_to_nf_moddivisor(GEN nf, GEN g, GEN e, GEN bid)
    1679             : {
    1680       18823 :   GEN t, cyc = bid_get_cyc(bid);
    1681       18823 :   if (lg(cyc) == 1)
    1682           0 :     t = gen_1;
    1683             :   else
    1684       18823 :     t = famat_to_nf_modideal_coprime(nf, g, e, bid_get_ideal(bid),
    1685             :                                      cyc_get_expo(cyc));
    1686       18823 :   return set_sign_mod_divisor(nf, mkmat2(g,e), t, bid_get_sarch(bid));
    1687             : }
    1688             : 
    1689             : GEN
    1690      210693 : vecmul(GEN x, GEN y)
    1691             : {
    1692      210693 :   if (is_scalar_t(typ(x))) return gmul(x,y);
    1693      210693 :   pari_APPLY_same(vecmul(gel(x,i), gel(y,i)))
    1694             : }
    1695             : 
    1696             : GEN
    1697           0 : vecinv(GEN x)
    1698             : {
    1699           0 :   if (is_scalar_t(typ(x))) return ginv(x);
    1700           0 :   pari_APPLY_same(vecinv(gel(x,i)))
    1701             : }
    1702             : 
    1703             : GEN
    1704           0 : vecpow(GEN x, GEN n)
    1705             : {
    1706           0 :   if (is_scalar_t(typ(x))) return powgi(x,n);
    1707           0 :   pari_APPLY_same(vecpow(gel(x,i), n))
    1708             : }
    1709             : 
    1710             : GEN
    1711         903 : vecdiv(GEN x, GEN y)
    1712             : {
    1713         903 :   if (is_scalar_t(typ(x))) return gdiv(x,y);
    1714         903 :   pari_APPLY_same(vecdiv(gel(x,i), gel(y,i)))
    1715             : }
    1716             : 
    1717             : /* A ideal as a square t_MAT */
    1718             : static GEN
    1719      230156 : idealmulelt(GEN nf, GEN x, GEN A)
    1720             : {
    1721             :   long i, lx;
    1722             :   GEN dx, dA, D;
    1723      230156 :   if (lg(A) == 1) return cgetg(1, t_MAT);
    1724      230156 :   x = nf_to_scalar_or_basis(nf,x);
    1725      230156 :   if (typ(x) != t_COL)
    1726       95795 :     return isintzero(x)? cgetg(1,t_MAT): RgM_Rg_mul(A, Q_abs_shallow(x));
    1727      134361 :   x = Q_remove_denom(x, &dx);
    1728      134361 :   A = Q_remove_denom(A, &dA);
    1729      134361 :   x = zk_multable(nf, x);
    1730      134361 :   D = mulii(zkmultable_capZ(x), gcoeff(A,1,1));
    1731      134361 :   x = zkC_multable_mul(A, x);
    1732      134361 :   settyp(x, t_MAT); lx = lg(x);
    1733             :   /* x may contain scalars (at most 1 since the ideal is non-0)*/
    1734      459357 :   for (i=1; i<lx; i++)
    1735      334607 :     if (typ(gel(x,i)) == t_INT)
    1736             :     {
    1737        9611 :       if (i > 1) swap(gel(x,1), gel(x,i)); /* help HNF */
    1738        9611 :       gel(x,1) = scalarcol_shallow(gel(x,1), lx-1);
    1739        9611 :       break;
    1740             :     }
    1741      134361 :   x = ZM_hnfmodid(x, D);
    1742      134361 :   dx = mul_denom(dx,dA);
    1743      134361 :   return dx? gdiv(x,dx): x;
    1744             : }
    1745             : 
    1746             : /* nf a true nf, tx <= ty */
    1747             : static GEN
    1748     1664487 : idealmul_aux(GEN nf, GEN x, GEN y, long tx, long ty)
    1749             : {
    1750             :   GEN z, cx, cy;
    1751     1664487 :   switch(tx)
    1752             :   {
    1753      279114 :     case id_PRINCIPAL:
    1754      279114 :       switch(ty)
    1755             :       {
    1756       48762 :         case id_PRINCIPAL:
    1757       48762 :           return idealhnf_principal(nf, nfmul(nf,x,y));
    1758         196 :         case id_PRIME:
    1759             :         {
    1760         196 :           GEN p = pr_get_p(y), pi = pr_get_gen(y), cx;
    1761         196 :           if (pr_is_inert(y)) return RgM_Rg_mul(idealhnf_principal(nf,x),p);
    1762             : 
    1763          42 :           x = nf_to_scalar_or_basis(nf, x);
    1764          42 :           switch(typ(x))
    1765             :           {
    1766          28 :             case t_INT:
    1767          28 :               if (!signe(x)) return cgetg(1,t_MAT);
    1768          28 :               return ZM_Z_mul(pr_hnf(nf,y), absi_shallow(x));
    1769           7 :             case t_FRAC:
    1770           7 :               return RgM_Rg_mul(pr_hnf(nf,y), Q_abs_shallow(x));
    1771             :           }
    1772             :           /* t_COL */
    1773           7 :           x = Q_primitive_part(x, &cx);
    1774           7 :           x = zk_multable(nf, x);
    1775           7 :           z = shallowconcat(ZM_Z_mul(x,p), ZM_ZC_mul(x,pi));
    1776           7 :           z = ZM_hnfmodid(z, mulii(p, zkmultable_capZ(x)));
    1777           7 :           return cx? ZM_Q_mul(z, cx): z;
    1778             :         }
    1779      230156 :         default: /* id_MAT */
    1780      230156 :           return idealmulelt(nf, x,y);
    1781             :       }
    1782     1301920 :     case id_PRIME:
    1783     1301920 :       if (ty==id_PRIME)
    1784     1297834 :       { y = pr_hnf(nf,y); cy = NULL; }
    1785             :       else
    1786        4086 :         y = Q_primitive_part(y, &cy);
    1787     1301920 :       y = idealHNF_mul_two(nf,y,x);
    1788     1301920 :       return cy? ZM_Q_mul(y,cy): y;
    1789             : 
    1790       83453 :     default: /* id_MAT */
    1791             :     {
    1792       83453 :       long N = nf_get_degree(nf);
    1793       83453 :       if (lg(x)-1 != N || lg(y)-1 != N) pari_err_DIM("idealmul");
    1794       83439 :       x = Q_primitive_part(x, &cx);
    1795       83439 :       y = Q_primitive_part(y, &cy); cx = mul_content(cx,cy);
    1796       83439 :       y = idealHNF_mul(nf,x,y);
    1797       83439 :       return cx? ZM_Q_mul(y,cx): y;
    1798             :     }
    1799             :   }
    1800             : }
    1801             : 
    1802             : /* output the ideal product ix.iy */
    1803             : GEN
    1804     1664487 : idealmul(GEN nf, GEN x, GEN y)
    1805             : {
    1806             :   pari_sp av;
    1807             :   GEN res, ax, ay, z;
    1808     1664487 :   long tx = idealtyp(&x,&ax);
    1809     1664487 :   long ty = idealtyp(&y,&ay), f;
    1810     1664487 :   if (tx>ty) { swap(ax,ay); swap(x,y); lswap(tx,ty); }
    1811     1664487 :   f = (ax||ay); res = f? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1812     1664487 :   av = avma;
    1813     1664487 :   z = gerepileupto(av, idealmul_aux(checknf(nf), x,y, tx,ty));
    1814     1664473 :   if (!f) return z;
    1815       25111 :   if (ax && ay)
    1816       23488 :     ax = ext_mul(nf, ax, ay);
    1817             :   else
    1818        1623 :     ax = gcopy(ax? ax: ay);
    1819       25111 :   gel(res,1) = z; gel(res,2) = ax; return res;
    1820             : }
    1821             : 
    1822             : /* Return x, integral in 2-elt form, such that pr^2 = c * x. cf idealpowprime
    1823             :  * nf = true nf */
    1824             : static GEN
    1825       42502 : idealsqrprime(GEN nf, GEN pr, GEN *pc)
    1826             : {
    1827       42502 :   GEN p = pr_get_p(pr), q, gen;
    1828       42502 :   long e = pr_get_e(pr), f = pr_get_f(pr);
    1829             : 
    1830       42502 :   q = (e == 1)? sqri(p): p;
    1831       42502 :   if (e <= 2 && e * f == nf_get_degree(nf))
    1832             :   { /* pr^e = (p) */
    1833        7733 :     *pc = q;
    1834        7733 :     return mkvec2(gen_1,gen_0);
    1835             :   }
    1836       34769 :   gen = nfsqr(nf, pr_get_gen(pr));
    1837       34769 :   gen = FpC_red(gen, q);
    1838       34769 :   *pc = NULL;
    1839       34769 :   return mkvec2(q, gen);
    1840             : }
    1841             : /* cf idealpow_aux */
    1842             : static GEN
    1843       27304 : idealsqr_aux(GEN nf, GEN x, long tx)
    1844             : {
    1845       27304 :   GEN T = nf_get_pol(nf), m, cx, a, alpha;
    1846       27304 :   long N = degpol(T);
    1847       27304 :   switch(tx)
    1848             :   {
    1849          63 :     case id_PRINCIPAL:
    1850          63 :       return idealhnf_principal(nf, nfsqr(nf,x));
    1851        9257 :     case id_PRIME:
    1852        9257 :       if (pr_is_inert(x)) return scalarmat(sqri(gel(x,1)), N);
    1853        9089 :       x = idealsqrprime(nf, x, &cx);
    1854        9089 :       x = idealhnf_two(nf,x);
    1855        9089 :       return cx? ZM_Z_mul(x, cx): x;
    1856       17984 :     default:
    1857       17984 :       x = Q_primitive_part(x, &cx);
    1858       17984 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    1859       17984 :       alpha = nfsqr(nf,alpha);
    1860       17984 :       m = zk_scalar_or_multable(nf, alpha);
    1861       17984 :       if (typ(m) == t_INT) {
    1862        1239 :         x = gcdii(sqri(a), m);
    1863        1239 :         if (cx) x = gmul(x, gsqr(cx));
    1864        1239 :         x = scalarmat(x, N);
    1865             :       }
    1866             :       else
    1867             :       { /* could use gcdii(sqri(a), zkmultable_capZ(m)), but costly */
    1868       16745 :         x = ZM_hnfmodid(m, sqri(a));
    1869       16745 :         if (cx) cx = gsqr(cx);
    1870       16745 :         if (cx) x = ZM_Q_mul(x, cx);
    1871             :       }
    1872       17984 :       return x;
    1873             :   }
    1874             : }
    1875             : GEN
    1876       27304 : idealsqr(GEN nf, GEN x)
    1877             : {
    1878             :   pari_sp av;
    1879             :   GEN res, ax, z;
    1880       27304 :   long tx = idealtyp(&x,&ax);
    1881       27304 :   res = ax? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1882       27304 :   av = avma;
    1883       27304 :   z = gerepileupto(av, idealsqr_aux(checknf(nf), x, tx));
    1884       27304 :   if (!ax) return z;
    1885       26973 :   gel(res,1) = z;
    1886       26973 :   gel(res,2) = ext_sqr(nf, ax); return res;
    1887             : }
    1888             : 
    1889             : /* norm of an ideal */
    1890             : GEN
    1891        8229 : idealnorm(GEN nf, GEN x)
    1892             : {
    1893             :   pari_sp av;
    1894             :   GEN y;
    1895             :   long tx;
    1896             : 
    1897        8229 :   switch(idealtyp(&x,&y))
    1898             :   {
    1899         245 :     case id_PRIME: return pr_norm(x);
    1900        5009 :     case id_MAT: return RgM_det_triangular(x);
    1901             :   }
    1902             :   /* id_PRINCIPAL */
    1903        2975 :   nf = checknf(nf); av = avma;
    1904        2975 :   x = nfnorm(nf, x);
    1905        2975 :   tx = typ(x);
    1906        2975 :   if (tx == t_INT) return gerepileuptoint(av, absi(x));
    1907         665 :   if (tx != t_FRAC) pari_err_TYPE("idealnorm",x);
    1908         665 :   return gerepileupto(av, Q_abs(x));
    1909             : }
    1910             : 
    1911             : /* x \cap Z */
    1912             : GEN
    1913         700 : idealdown(GEN nf, GEN x)
    1914             : {
    1915         700 :   pari_sp av = avma;
    1916             :   GEN y, c;
    1917         700 :   switch(idealtyp(&x,&y))
    1918             :   {
    1919           7 :     case id_PRIME: return icopy(pr_get_p(x));
    1920         378 :     case id_MAT: return gcopy(gcoeff(x,1,1));
    1921             :   }
    1922             :   /* id_PRINCIPAL */
    1923         315 :   nf = checknf(nf); av = avma;
    1924         315 :   x = nf_to_scalar_or_basis(nf, x);
    1925         315 :   if (is_rational_t(typ(x))) return Q_abs(x);
    1926          14 :   x = Q_primitive_part(x, &c);
    1927          14 :   y = zkmultable_capZ(zk_multable(nf, x));
    1928          14 :   return gerepilecopy(av, mul_content(c, y));
    1929             : }
    1930             : 
    1931             : /* true nf */
    1932             : static GEN
    1933          28 : idealismaximal_int(GEN nf, GEN p)
    1934             : {
    1935             :   GEN L;
    1936          28 :   if (!BPSW_psp(p)) return NULL;
    1937          56 :   if (!dvdii(nf_get_index(nf), p) &&
    1938          42 :       !FpX_is_irred(FpX_red(nf_get_pol(nf),p), p)) return NULL;
    1939          14 :   L = idealprimedec(nf, p);
    1940          14 :   return lg(L) == 2? gel(L,1): NULL;
    1941             : }
    1942             : /* true nf */
    1943             : static GEN
    1944           7 : idealismaximal_mat(GEN nf, GEN x)
    1945             : {
    1946             :   GEN p, c, L;
    1947             :   long i, l, f;
    1948           7 :   x = Q_primitive_part(x, &c);
    1949           7 :   p = gcoeff(x,1,1);
    1950           7 :   if (c)
    1951             :   {
    1952           0 :     if (typ(c) == t_FRAC || !equali1(p)) return NULL;
    1953           0 :     return idealismaximal_int(nf, p);
    1954             :   }
    1955           7 :   if (!BPSW_psp(p)) return NULL;
    1956           7 :   l = lg(x); f = 1;
    1957          21 :   for (i = 2; i < l; i++)
    1958             :   {
    1959          14 :     c = gcoeff(x,i,i);
    1960          14 :     if (equalii(c, p)) f++; else if (!equali1(c)) return NULL;
    1961             :   }
    1962           7 :   L = idealprimedec_limit_f(nf, p, f);
    1963          14 :   for (i = lg(L)-1; i; i--)
    1964             :   {
    1965          14 :     GEN pr = gel(L,i);
    1966          14 :     if (pr_get_f(pr) != f) break;
    1967          14 :     if (idealval(nf, x, pr) == 1) return pr;
    1968             :   }
    1969           0 :   return NULL;
    1970             : }
    1971             : /* true nf */
    1972             : static GEN
    1973          42 : idealismaximal_i(GEN nf, GEN x)
    1974             : {
    1975             :   GEN L, p, pr, c;
    1976             :   long i, l;
    1977          42 :   switch(idealtyp(&x,&c))
    1978             :   {
    1979           7 :     case id_PRIME: return x;
    1980           7 :     case id_MAT: return idealismaximal_mat(nf, x);
    1981             :   }
    1982             :   /* id_PRINCIPAL */
    1983          28 :   nf = checknf(nf);
    1984          28 :   x = nf_to_scalar_or_basis(nf, x);
    1985          28 :   switch(typ(x))
    1986             :   {
    1987          28 :     case t_INT: return idealismaximal_int(nf, absi_shallow(x));
    1988           0 :     case t_FRAC: return NULL;
    1989             :   }
    1990           0 :   x = Q_primitive_part(x, &c);
    1991           0 :   if (c) return NULL;
    1992           0 :   p = zkmultable_capZ(zk_multable(nf, x));
    1993           0 :   L = idealprimedec(nf, p); l = lg(L); pr = NULL;
    1994           0 :   for (i = 1; i < l; i++)
    1995             :   {
    1996           0 :     long v = ZC_nfval(x, gel(L,i));
    1997           0 :     if (v > 1 || (v && pr)) return NULL;
    1998           0 :     pr = gel(L,i);
    1999             :   }
    2000           0 :   return pr;
    2001             : }
    2002             : GEN
    2003          42 : idealismaximal(GEN nf, GEN x)
    2004             : {
    2005          42 :   pari_sp av = avma;
    2006          42 :   x = idealismaximal_i(checknf(nf), x);
    2007          42 :   if (!x) { set_avma(av); return gen_0; }
    2008          28 :   return gerepilecopy(av, x);
    2009             : }
    2010             : 
    2011             : /* I^(-1) = { x \in K, Tr(x D^(-1) I) \in Z }, D different of K/Q
    2012             :  *
    2013             :  * nf[5][6] = pp( D^(-1) ) = pp( HNF( T^(-1) ) ), T = (Tr(wi wj))
    2014             :  * nf[5][7] = same in 2-elt form.
    2015             :  * Assume I integral. Return the integral ideal (I\cap Z) I^(-1) */
    2016             : GEN
    2017      144571 : idealHNF_inv_Z(GEN nf, GEN I)
    2018             : {
    2019      144571 :   GEN J, dual, IZ = gcoeff(I,1,1); /* I \cap Z */
    2020      144571 :   if (isint1(IZ)) return matid(lg(I)-1);
    2021      133301 :   J = idealHNF_mul(nf,I, gmael(nf,5,7));
    2022             :  /* I in HNF, hence easily inverted; multiply by IZ to get integer coeffs
    2023             :   * missing content cancels while solving the linear equation */
    2024      133301 :   dual = shallowtrans( hnf_divscale(J, gmael(nf,5,6), IZ) );
    2025      133301 :   return ZM_hnfmodid(dual, IZ);
    2026             : }
    2027             : /* I HNF with rational coefficients (denominator d). */
    2028             : GEN
    2029       57458 : idealHNF_inv(GEN nf, GEN I)
    2030             : {
    2031       57458 :   GEN J, IQ = gcoeff(I,1,1); /* I \cap Q; d IQ = dI \cap Z */
    2032       57458 :   J = idealHNF_inv_Z(nf, Q_remove_denom(I, NULL)); /* = (dI)^(-1) * (d IQ) */
    2033       57458 :   return equali1(IQ)? J: RgM_Rg_div(J, IQ);
    2034             : }
    2035             : 
    2036             : /* return p * P^(-1)  [integral] */
    2037             : GEN
    2038       26225 : pr_inv_p(GEN pr)
    2039             : {
    2040       26225 :   if (pr_is_inert(pr)) return matid(pr_get_f(pr));
    2041       25637 :   return ZM_hnfmodid(pr_get_tau(pr), pr_get_p(pr));
    2042             : }
    2043             : GEN
    2044        5773 : pr_inv(GEN pr)
    2045             : {
    2046        5773 :   GEN p = pr_get_p(pr);
    2047        5773 :   if (pr_is_inert(pr)) return scalarmat(ginv(p), pr_get_f(pr));
    2048        5437 :   return RgM_Rg_div(ZM_hnfmodid(pr_get_tau(pr),p), p);
    2049             : }
    2050             : 
    2051             : GEN
    2052      104153 : idealinv(GEN nf, GEN x)
    2053             : {
    2054             :   GEN res, ax;
    2055             :   pari_sp av;
    2056      104153 :   long tx = idealtyp(&x,&ax), N;
    2057             : 
    2058      104153 :   res = ax? cgetg(3,t_VEC): NULL;
    2059      104153 :   nf = checknf(nf); av = avma;
    2060      104153 :   N = nf_get_degree(nf);
    2061      104153 :   switch (tx)
    2062             :   {
    2063       52397 :     case id_MAT:
    2064       52397 :       if (lg(x)-1 != N) pari_err_DIM("idealinv");
    2065       52397 :       x = idealHNF_inv(nf,x); break;
    2066       46907 :     case id_PRINCIPAL:
    2067       46907 :       x = nf_to_scalar_or_basis(nf, x);
    2068       46907 :       if (typ(x) != t_COL)
    2069       46865 :         x = idealhnf_principal(nf,ginv(x));
    2070             :       else
    2071             :       { /* nfinv + idealhnf where we already know (x) \cap Z */
    2072             :         GEN c, d;
    2073          42 :         x = Q_remove_denom(x, &c);
    2074          42 :         x = zk_inv(nf, x);
    2075          42 :         x = Q_remove_denom(x, &d); /* true inverse is c/d * x */
    2076          42 :         if (!d) /* x and x^(-1) integral => x a unit */
    2077           7 :           x = scalarmat_shallow(c? c: gen_1, N);
    2078             :         else
    2079             :         {
    2080          35 :           c = c? gdiv(c,d): ginv(d);
    2081          35 :           x = zk_multable(nf, x);
    2082          35 :           x = ZM_Q_mul(ZM_hnfmodid(x,d), c);
    2083             :         }
    2084             :       }
    2085       46907 :       break;
    2086        4849 :     case id_PRIME:
    2087        4849 :       x = pr_inv(x); break;
    2088             :   }
    2089      104153 :   x = gerepileupto(av,x); if (!ax) return x;
    2090        7559 :   gel(res,1) = x;
    2091        7559 :   gel(res,2) = ext_inv(nf, ax); return res;
    2092             : }
    2093             : 
    2094             : /* write x = A/B, A,B coprime integral ideals */
    2095             : GEN
    2096       55121 : idealnumden(GEN nf, GEN x)
    2097             : {
    2098       55121 :   pari_sp av = avma;
    2099             :   GEN x0, ax, c, d, A, B, J;
    2100       55121 :   long tx = idealtyp(&x,&ax);
    2101       55121 :   nf = checknf(nf);
    2102       55121 :   switch (tx)
    2103             :   {
    2104           7 :     case id_PRIME:
    2105           7 :       retmkvec2(idealhnf(nf, x), gen_1);
    2106        4823 :     case id_PRINCIPAL:
    2107             :     {
    2108             :       GEN xZ, mx;
    2109        4823 :       x = nf_to_scalar_or_basis(nf, x);
    2110        4823 :       switch(typ(x))
    2111             :       {
    2112        1106 :         case t_INT: return gerepilecopy(av, mkvec2(absi_shallow(x),gen_1));
    2113          14 :         case t_FRAC:return gerepilecopy(av, mkvec2(absi_shallow(gel(x,1)), gel(x,2)));
    2114             :       }
    2115             :       /* t_COL */
    2116        3703 :       x = Q_remove_denom(x, &d);
    2117        3703 :       if (!d) return gerepilecopy(av, mkvec2(idealhnf(nf, x), gen_1));
    2118          35 :       mx = zk_multable(nf, x);
    2119          35 :       xZ = zkmultable_capZ(mx);
    2120          35 :       x = ZM_hnfmodid(mx, xZ); /* principal ideal (x) */
    2121          35 :       x0 = mkvec2(xZ, mx); /* same, for fast multiplication */
    2122          35 :       break;
    2123             :     }
    2124       50291 :     default: /* id_MAT */
    2125             :     {
    2126       50291 :       long n = lg(x)-1;
    2127       50291 :       if (n == 0) return mkvec2(gen_0, gen_1);
    2128       50291 :       if (n != nf_get_degree(nf)) pari_err_DIM("idealnumden");
    2129       50291 :       x0 = x = Q_remove_denom(x, &d);
    2130       50291 :       if (!d) return gerepilecopy(av, mkvec2(x, gen_1));
    2131          14 :       break;
    2132             :     }
    2133             :   }
    2134          49 :   J = hnfmodid(x, d); /* = d/B */
    2135          49 :   c = gcoeff(J,1,1); /* (d/B) \cap Z, divides d */
    2136          49 :   B = idealHNF_inv_Z(nf, J); /* (d/B \cap Z) B/d */
    2137          49 :   if (!equalii(c,d)) B = ZM_Z_mul(B, diviiexact(d,c)); /* = B ! */
    2138          49 :   A = idealHNF_mul(nf, B, x0); /* d * (original x) * B = d A */
    2139          49 :   A = ZM_Z_divexact(A, d); /* = A ! */
    2140          49 :   return gerepilecopy(av, mkvec2(A, B));
    2141             : }
    2142             : 
    2143             : /* Return x, integral in 2-elt form, such that pr^n = c * x. Assume n != 0.
    2144             :  * nf = true nf */
    2145             : static GEN
    2146      246701 : idealpowprime(GEN nf, GEN pr, GEN n, GEN *pc)
    2147             : {
    2148      246701 :   GEN p = pr_get_p(pr), q, gen;
    2149             : 
    2150      246701 :   *pc = NULL;
    2151      246701 :   if (is_pm1(n)) /* n = 1 special cased for efficiency */
    2152             :   {
    2153       87687 :     q = p;
    2154       87687 :     if (typ(pr_get_tau(pr)) == t_INT) /* inert */
    2155             :     {
    2156           0 :       *pc = (signe(n) >= 0)? p: ginv(p);
    2157           0 :       return mkvec2(gen_1,gen_0);
    2158             :     }
    2159       87687 :     if (signe(n) >= 0) gen = pr_get_gen(pr);
    2160             :     else
    2161             :     {
    2162       18130 :       gen = pr_get_tau(pr); /* possibly t_MAT */
    2163       18130 :       *pc = ginv(p);
    2164             :     }
    2165             :   }
    2166      159014 :   else if (equalis(n,2)) return idealsqrprime(nf, pr, pc);
    2167             :   else
    2168             :   {
    2169      125601 :     long e = pr_get_e(pr), f = pr_get_f(pr);
    2170      125601 :     GEN r, m = truedvmdis(n, e, &r);
    2171      125601 :     if (e * f == nf_get_degree(nf))
    2172             :     { /* pr^e = (p) */
    2173       10836 :       if (signe(m)) *pc = powii(p,m);
    2174       10836 :       if (!signe(r)) return mkvec2(gen_1,gen_0);
    2175        5888 :       q = p;
    2176        5888 :       gen = nfpow(nf, pr_get_gen(pr), r);
    2177             :     }
    2178             :     else
    2179             :     {
    2180      114765 :       m = absi_shallow(m);
    2181      114765 :       if (signe(r)) m = addiu(m,1);
    2182      114765 :       q = powii(p,m); /* m = ceil(|n|/e) */
    2183      114765 :       if (signe(n) >= 0) gen = nfpow(nf, pr_get_gen(pr), n);
    2184             :       else
    2185             :       {
    2186        4641 :         gen = pr_get_tau(pr);
    2187        4641 :         if (typ(gen) == t_MAT) gen = gel(gen,1);
    2188        4641 :         n = negi(n);
    2189        4641 :         gen = ZC_Z_divexact(nfpow(nf, gen, n), powii(p, subii(n,m)));
    2190        4641 :         *pc = ginv(q);
    2191             :       }
    2192             :     }
    2193      120653 :     gen = FpC_red(gen, q);
    2194             :   }
    2195      208340 :   return mkvec2(q, gen);
    2196             : }
    2197             : 
    2198             : /* x * pr^n. Assume x in HNF or scalar (possibly non-integral) */
    2199             : GEN
    2200      203043 : idealmulpowprime(GEN nf, GEN x, GEN pr, GEN n)
    2201             : {
    2202             :   GEN c, cx, y;
    2203             :   long N;
    2204             : 
    2205      203043 :   nf = checknf(nf);
    2206      203043 :   N = nf_get_degree(nf);
    2207      203043 :   if (!signe(n)) return typ(x) == t_MAT? x: scalarmat_shallow(x, N);
    2208             : 
    2209             :   /* inert, special cased for efficiency */
    2210      202573 :   if (pr_is_inert(pr))
    2211             :   {
    2212       10710 :     GEN q = powii(pr_get_p(pr), n);
    2213        8911 :     return typ(x) == t_MAT? RgM_Rg_mul(x,q)
    2214       19621 :                           : scalarmat_shallow(gmul(Q_abs(x),q), N);
    2215             :   }
    2216             : 
    2217      191863 :   y = idealpowprime(nf, pr, n, &c);
    2218      191863 :   if (typ(x) == t_MAT)
    2219      189420 :   { x = Q_primitive_part(x, &cx); if (is_pm1(gcoeff(x,1,1))) x = NULL; }
    2220             :   else
    2221        2443 :   { cx = x; x = NULL; }
    2222      191863 :   cx = mul_content(c,cx);
    2223      191863 :   if (x)
    2224      148408 :     x = idealHNF_mul_two(nf,x,y);
    2225             :   else
    2226       43455 :     x = idealhnf_two(nf,y);
    2227      191863 :   if (cx) x = ZM_Q_mul(x,cx);
    2228      191863 :   return x;
    2229             : }
    2230             : GEN
    2231        4214 : idealdivpowprime(GEN nf, GEN x, GEN pr, GEN n)
    2232             : {
    2233        4214 :   return idealmulpowprime(nf,x,pr, negi(n));
    2234             : }
    2235             : 
    2236             : /* nf = true nf */
    2237             : static GEN
    2238      213136 : idealpow_aux(GEN nf, GEN x, long tx, GEN n)
    2239             : {
    2240      213136 :   GEN T = nf_get_pol(nf), m, cx, n1, a, alpha;
    2241      213136 :   long N = degpol(T), s = signe(n);
    2242      213136 :   if (!s) return matid(N);
    2243      206893 :   switch(tx)
    2244             :   {
    2245           0 :     case id_PRINCIPAL:
    2246           0 :       return idealhnf_principal(nf, nfpow(nf,x,n));
    2247      101815 :     case id_PRIME:
    2248      101815 :       if (pr_is_inert(x)) return scalarmat(powii(gel(x,1), n), N);
    2249       54838 :       x = idealpowprime(nf, x, n, &cx);
    2250       54838 :       x = idealhnf_two(nf,x);
    2251       54838 :       return cx? ZM_Q_mul(x, cx): x;
    2252      105078 :     default:
    2253      105078 :       if (is_pm1(n)) return (s < 0)? idealinv(nf, x): gcopy(x);
    2254       58032 :       n1 = (s < 0)? negi(n): n;
    2255             : 
    2256       58032 :       x = Q_primitive_part(x, &cx);
    2257       58032 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    2258       58032 :       alpha = nfpow(nf,alpha,n1);
    2259       58032 :       m = zk_scalar_or_multable(nf, alpha);
    2260       58032 :       if (typ(m) == t_INT) {
    2261         294 :         x = gcdii(powii(a,n1), m);
    2262         294 :         if (s<0) x = ginv(x);
    2263         294 :         if (cx) x = gmul(x, powgi(cx,n));
    2264         294 :         x = scalarmat(x, N);
    2265             :       }
    2266             :       else
    2267             :       { /* could use gcdii(powii(a,n1), zkmultable_capZ(m)), but costly */
    2268       57738 :         x = ZM_hnfmodid(m, powii(a,n1));
    2269       57738 :         if (cx) cx = powgi(cx,n);
    2270       57738 :         if (s<0) {
    2271           7 :           GEN xZ = gcoeff(x,1,1);
    2272           7 :           cx = cx ? gdiv(cx, xZ): ginv(xZ);
    2273           7 :           x = idealHNF_inv_Z(nf,x);
    2274             :         }
    2275       57738 :         if (cx) x = ZM_Q_mul(x, cx);
    2276             :       }
    2277       58032 :       return x;
    2278             :   }
    2279             : }
    2280             : 
    2281             : /* raise the ideal x to the power n (in Z) */
    2282             : GEN
    2283      213136 : idealpow(GEN nf, GEN x, GEN n)
    2284             : {
    2285             :   pari_sp av;
    2286             :   long tx;
    2287             :   GEN res, ax;
    2288             : 
    2289      213136 :   if (typ(n) != t_INT) pari_err_TYPE("idealpow",n);
    2290      213136 :   tx = idealtyp(&x,&ax);
    2291      213136 :   res = ax? cgetg(3,t_VEC): NULL;
    2292      213136 :   av = avma;
    2293      213136 :   x = gerepileupto(av, idealpow_aux(checknf(nf), x, tx, n));
    2294      213136 :   if (!ax) return x;
    2295           0 :   ax = ext_pow(nf, ax, n);
    2296           0 :   gel(res,1) = x;
    2297           0 :   gel(res,2) = ax;
    2298           0 :   return res;
    2299             : }
    2300             : 
    2301             : /* Return ideal^e in number field nf. e is a C integer. */
    2302             : GEN
    2303       23592 : idealpows(GEN nf, GEN ideal, long e)
    2304             : {
    2305       23592 :   long court[] = {evaltyp(t_INT) | _evallg(3),0,0};
    2306       23592 :   affsi(e,court); return idealpow(nf,ideal,court);
    2307             : }
    2308             : 
    2309             : static GEN
    2310       25200 : _idealmulred(GEN nf, GEN x, GEN y)
    2311       25200 : { return idealred(nf,idealmul(nf,x,y)); }
    2312             : static GEN
    2313       27038 : _idealsqrred(GEN nf, GEN x)
    2314       27038 : { return idealred(nf,idealsqr(nf,x)); }
    2315             : static GEN
    2316        8647 : _mul(void *data, GEN x, GEN y) { return _idealmulred((GEN)data,x,y); }
    2317             : static GEN
    2318       27038 : _sqr(void *data, GEN x) { return _idealsqrred((GEN)data, x); }
    2319             : 
    2320             : /* compute x^n (x ideal, n integer), reducing along the way */
    2321             : GEN
    2322       52271 : idealpowred(GEN nf, GEN x, GEN n)
    2323             : {
    2324       52271 :   pari_sp av = avma, av2;
    2325             :   long s;
    2326             :   GEN y;
    2327             : 
    2328       52271 :   if (typ(n) != t_INT) pari_err_TYPE("idealpowred",n);
    2329       52271 :   s = signe(n); if (s == 0) return idealpow(nf,x,n);
    2330       52271 :   y = gen_pow_i(x, n, (void*)nf, &_sqr, &_mul);
    2331       52271 :   av2 = avma;
    2332       52271 :   if (s < 0) y = idealinv(nf,y);
    2333       52271 :   if (s < 0 || is_pm1(n)) y = idealred(nf,y);
    2334       52271 :   return avma == av2? gerepilecopy(av,y): gerepileupto(av,y);
    2335             : }
    2336             : 
    2337             : GEN
    2338       16553 : idealmulred(GEN nf, GEN x, GEN y)
    2339             : {
    2340       16553 :   pari_sp av = avma;
    2341       16553 :   return gerepileupto(av, _idealmulred(nf,x,y));
    2342             : }
    2343             : 
    2344             : long
    2345          91 : isideal(GEN nf,GEN x)
    2346             : {
    2347          91 :   long N, i, j, lx, tx = typ(x);
    2348             :   pari_sp av;
    2349             :   GEN T, xZ;
    2350             : 
    2351          91 :   nf = checknf(nf); T = nf_get_pol(nf); lx = lg(x);
    2352          91 :   if (tx==t_VEC && lx==3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
    2353          91 :   switch(tx)
    2354             :   {
    2355          14 :     case t_INT: case t_FRAC: return 1;
    2356           7 :     case t_POL: return varn(x) == varn(T);
    2357           7 :     case t_POLMOD: return RgX_equal_var(T, gel(x,1));
    2358          14 :     case t_VEC: return get_prid(x)? 1 : 0;
    2359          42 :     case t_MAT: break;
    2360           7 :     default: return 0;
    2361             :   }
    2362          42 :   N = degpol(T);
    2363          42 :   if (lx-1 != N) return (lx == 1);
    2364          28 :   if (nbrows(x) != N) return 0;
    2365             : 
    2366          28 :   av = avma; x = Q_primpart(x);
    2367          28 :   if (!ZM_ishnf(x)) return 0;
    2368          14 :   xZ = gcoeff(x,1,1);
    2369          21 :   for (j=2; j<=N; j++)
    2370          14 :     if (!dvdii(xZ, gcoeff(x,j,j))) return gc_long(av,0);
    2371          14 :   for (i=2; i<=N; i++)
    2372          14 :     for (j=2; j<=N; j++)
    2373           7 :        if (! hnf_invimage(x, zk_ei_mul(nf,gel(x,i),j))) return gc_long(av,0);
    2374           7 :   return gc_long(av,1);
    2375             : }
    2376             : 
    2377             : GEN
    2378       31277 : idealdiv(GEN nf, GEN x, GEN y)
    2379             : {
    2380       31277 :   pari_sp av = avma, tetpil;
    2381       31277 :   GEN z = idealinv(nf,y);
    2382       31277 :   tetpil = avma; return gerepile(av,tetpil, idealmul(nf,x,z));
    2383             : }
    2384             : 
    2385             : /* This routine computes the quotient x/y of two ideals in the number field nf.
    2386             :  * It assumes that the quotient is an integral ideal.  The idea is to find an
    2387             :  * ideal z dividing y such that gcd(Nx/Nz, Nz) = 1.  Then
    2388             :  *
    2389             :  *   x + (Nx/Nz)    x
    2390             :  *   ----------- = ---
    2391             :  *   y + (Ny/Nz)    y
    2392             :  *
    2393             :  * Proof: we can assume x and y are integral. Let p be any prime ideal
    2394             :  *
    2395             :  * If p | Nz, then it divides neither Nx/Nz nor Ny/Nz (since Nx/Nz is the
    2396             :  * product of the integers N(x/y) and N(y/z)).  Both the numerator and the
    2397             :  * denominator on the left will be coprime to p.  So will x/y, since x/y is
    2398             :  * assumed integral and its norm N(x/y) is coprime to p.
    2399             :  *
    2400             :  * If instead p does not divide Nz, then v_p (Nx/Nz) = v_p (Nx) >= v_p(x).
    2401             :  * Hence v_p (x + Nx/Nz) = v_p(x).  Likewise for the denominators.  QED.
    2402             :  *
    2403             :  *                Peter Montgomery.  July, 1994. */
    2404             : static void
    2405           7 : err_divexact(GEN x, GEN y)
    2406           7 : { pari_err_DOMAIN("idealdivexact","denominator(x/y)", "!=",
    2407           0 :                   gen_1,mkvec2(x,y)); }
    2408             : GEN
    2409        1927 : idealdivexact(GEN nf, GEN x0, GEN y0)
    2410             : {
    2411        1927 :   pari_sp av = avma;
    2412             :   GEN x, y, xZ, yZ, Nx, Ny, Nz, cy, q, r;
    2413             : 
    2414        1927 :   nf = checknf(nf);
    2415        1927 :   x = idealhnf_shallow(nf, x0);
    2416        1927 :   y = idealhnf_shallow(nf, y0);
    2417        1927 :   if (lg(y) == 1) pari_err_INV("idealdivexact", y0);
    2418        1920 :   if (lg(x) == 1) { set_avma(av); return cgetg(1, t_MAT); } /* numerator is zero */
    2419        1920 :   y = Q_primitive_part(y, &cy);
    2420        1920 :   if (cy) x = RgM_Rg_div(x,cy);
    2421        1920 :   xZ = gcoeff(x,1,1); if (typ(xZ) != t_INT) err_divexact(x,y);
    2422        1913 :   yZ = gcoeff(y,1,1); if (isint1(yZ)) return gerepilecopy(av, x);
    2423        1073 :   Nx = idealnorm(nf,x);
    2424        1073 :   Ny = idealnorm(nf,y);
    2425        1073 :   if (typ(Nx) != t_INT) err_divexact(x,y);
    2426        1073 :   q = dvmdii(Nx,Ny, &r);
    2427        1073 :   if (signe(r)) err_divexact(x,y);
    2428        1073 :   if (is_pm1(q)) { set_avma(av); return matid(nf_get_degree(nf)); }
    2429             :   /* Find a norm Nz | Ny such that gcd(Nx/Nz, Nz) = 1 */
    2430         618 :   for (Nz = Ny;;) /* q = Nx/Nz */
    2431         506 :   {
    2432        1124 :     GEN p1 = gcdii(Nz, q);
    2433        1124 :     if (is_pm1(p1)) break;
    2434         506 :     Nz = diviiexact(Nz,p1);
    2435         506 :     q = mulii(q,p1);
    2436             :   }
    2437         618 :   xZ = gcoeff(x,1,1); q = gcdii(q, xZ);
    2438         618 :   if (!equalii(xZ,q))
    2439             :   { /* Replace x/y  by  x+(Nx/Nz) / y+(Ny/Nz) */
    2440         289 :     x = ZM_hnfmodid(x, q);
    2441             :     /* y reduced to unit ideal ? */
    2442         289 :     if (Nz == Ny) return gerepileupto(av, x);
    2443             : 
    2444          64 :     yZ = gcoeff(y,1,1); q = gcdii(diviiexact(Ny,Nz), yZ);
    2445          64 :     y = ZM_hnfmodid(y, q);
    2446             :   }
    2447         393 :   yZ = gcoeff(y,1,1);
    2448         393 :   y = idealHNF_mul(nf,x, idealHNF_inv_Z(nf,y));
    2449         393 :   return gerepileupto(av, ZM_Z_divexact(y, yZ));
    2450             : }
    2451             : 
    2452             : GEN
    2453          21 : idealintersect(GEN nf, GEN x, GEN y)
    2454             : {
    2455          21 :   pari_sp av = avma;
    2456             :   long lz, lx, i;
    2457             :   GEN z, dx, dy, xZ, yZ;;
    2458             : 
    2459          21 :   nf = checknf(nf);
    2460          21 :   x = idealhnf_shallow(nf,x);
    2461          21 :   y = idealhnf_shallow(nf,y);
    2462          21 :   if (lg(x) == 1 || lg(y) == 1) { set_avma(av); return cgetg(1,t_MAT); }
    2463          14 :   x = Q_remove_denom(x, &dx);
    2464          14 :   y = Q_remove_denom(y, &dy);
    2465          14 :   if (dx) y = ZM_Z_mul(y, dx);
    2466          14 :   if (dy) x = ZM_Z_mul(x, dy);
    2467          14 :   xZ = gcoeff(x,1,1);
    2468          14 :   yZ = gcoeff(y,1,1);
    2469          14 :   dx = mul_denom(dx,dy);
    2470          14 :   z = ZM_lll(shallowconcat(x,y), 0.99, LLL_KER); lz = lg(z);
    2471          14 :   lx = lg(x);
    2472          63 :   for (i=1; i<lz; i++) setlg(z[i], lx);
    2473          14 :   z = ZM_hnfmodid(ZM_mul(x,z), lcmii(xZ, yZ));
    2474          14 :   if (dx) z = RgM_Rg_div(z,dx);
    2475          14 :   return gerepileupto(av,z);
    2476             : }
    2477             : 
    2478             : /*******************************************************************/
    2479             : /*                                                                 */
    2480             : /*                      T2-IDEAL REDUCTION                         */
    2481             : /*                                                                 */
    2482             : /*******************************************************************/
    2483             : 
    2484             : static GEN
    2485          21 : chk_vdir(GEN nf, GEN vdir)
    2486             : {
    2487          21 :   long i, l = lg(vdir);
    2488             :   GEN v;
    2489          21 :   if (l != lg(nf_get_roots(nf))) pari_err_DIM("idealred");
    2490          14 :   switch(typ(vdir))
    2491             :   {
    2492           0 :     case t_VECSMALL: return vdir;
    2493          14 :     case t_VEC: break;
    2494           0 :     default: pari_err_TYPE("idealred",vdir);
    2495             :   }
    2496          14 :   v = cgetg(l, t_VECSMALL);
    2497          56 :   for (i = 1; i < l; i++) v[i] = itos(gceil(gel(vdir,i)));
    2498          14 :   return v;
    2499             : }
    2500             : 
    2501             : static void
    2502       30194 : twistG(GEN G, long r1, long i, long v)
    2503             : {
    2504       30194 :   long j, lG = lg(G);
    2505       30194 :   if (i <= r1) {
    2506       83031 :     for (j=1; j<lG; j++) gcoeff(G,i,j) = gmul2n(gcoeff(G,i,j), v);
    2507             :   } else {
    2508        6108 :     long k = (i<<1) - r1;
    2509       31841 :     for (j=1; j<lG; j++)
    2510             :     {
    2511       25733 :       gcoeff(G,k-1,j) = gmul2n(gcoeff(G,k-1,j), v);
    2512       25733 :       gcoeff(G,k  ,j) = gmul2n(gcoeff(G,k  ,j), v);
    2513             :     }
    2514             :   }
    2515       30194 : }
    2516             : 
    2517             : GEN
    2518      114464 : nf_get_Gtwist(GEN nf, GEN vdir)
    2519             : {
    2520             :   long i, l, v, r1;
    2521             :   GEN G;
    2522             : 
    2523      114464 :   if (!vdir) return nf_get_roundG(nf);
    2524          21 :   if (typ(vdir) == t_MAT)
    2525             :   {
    2526           0 :     long N = nf_get_degree(nf);
    2527           0 :     if (lg(vdir) != N+1 || lgcols(vdir) != N+1) pari_err_DIM("idealred");
    2528           0 :     return vdir;
    2529             :   }
    2530          21 :   vdir = chk_vdir(nf, vdir);
    2531          14 :   G = RgM_shallowcopy(nf_get_G(nf));
    2532          14 :   r1 = nf_get_r1(nf);
    2533          14 :   l = lg(vdir);
    2534          56 :   for (i=1; i<l; i++)
    2535             :   {
    2536          42 :     v = vdir[i]; if (!v) continue;
    2537          42 :     twistG(G, r1, i, v);
    2538             :   }
    2539          14 :   return RM_round_maxrank(G);
    2540             : }
    2541             : GEN
    2542       30152 : nf_get_Gtwist1(GEN nf, long i)
    2543             : {
    2544       30152 :   GEN G = RgM_shallowcopy( nf_get_G(nf) );
    2545       30152 :   long r1 = nf_get_r1(nf);
    2546       30152 :   twistG(G, r1, i, 10);
    2547       30152 :   return RM_round_maxrank(G);
    2548             : }
    2549             : 
    2550             : GEN
    2551       49587 : RM_round_maxrank(GEN G0)
    2552             : {
    2553       49587 :   long e, r = lg(G0)-1;
    2554       49587 :   pari_sp av = avma;
    2555       49587 :   for (e = 4; ; e <<= 1, set_avma(av))
    2556           0 :   {
    2557       49587 :     GEN G = gmul2n(G0, e), H = ground(G);
    2558       49587 :     if (ZM_rank(H) == r) return H; /* maximal rank ? */
    2559             :   }
    2560             : }
    2561             : 
    2562             : GEN
    2563      114457 : idealred0(GEN nf, GEN I, GEN vdir)
    2564             : {
    2565      114457 :   pari_sp av = avma;
    2566      114457 :   GEN G, aI, IZ, J, y, yZ, my, c1 = NULL;
    2567             :   long N;
    2568             : 
    2569      114457 :   nf = checknf(nf);
    2570      114457 :   N = nf_get_degree(nf);
    2571             :   /* put first for sanity checks, unused when I obviously principal */
    2572      114457 :   G = nf_get_Gtwist(nf, vdir);
    2573      114450 :   switch (idealtyp(&I,&aI))
    2574             :   {
    2575       24657 :     case id_PRIME:
    2576       24657 :       if (pr_is_inert(I)) {
    2577         581 :         if (!aI) { set_avma(av); return matid(N); }
    2578         581 :         c1 = gel(I,1); I = matid(N);
    2579         581 :         goto END;
    2580             :       }
    2581       24076 :       IZ = pr_get_p(I);
    2582       24076 :       J = pr_inv_p(I);
    2583       24076 :       I = idealhnf_two(nf,I);
    2584       24076 :       break;
    2585       89765 :     case id_MAT:
    2586       89765 :       I = Q_primitive_part(I, &c1);
    2587       89765 :       IZ = gcoeff(I,1,1);
    2588       89765 :       if (is_pm1(IZ))
    2589             :       {
    2590        7980 :         if (!aI) { set_avma(av); return matid(N); }
    2591        7938 :         goto END;
    2592             :       }
    2593       81785 :       J = idealHNF_inv_Z(nf, I);
    2594       81785 :       break;
    2595          21 :     default: /* id_PRINCIPAL, silly case */
    2596          21 :       if (gequal0(I)) I = cgetg(1,t_MAT); else { c1 = I; I = matid(N); }
    2597          21 :       if (!aI) return I;
    2598          14 :       goto END;
    2599             :   }
    2600             :   /* now I integral, HNF; and J = (I\cap Z) I^(-1), integral */
    2601      105861 :   y = idealpseudomin(J, G); /* small elt in (I\cap Z)I^(-1), integral */
    2602      105861 :   if (equalii(ZV_content(y), IZ))
    2603             :   { /* already reduced */
    2604       49428 :     if (!aI) return gerepilecopy(av, I);
    2605       49016 :     goto END;
    2606             :   }
    2607             : 
    2608       56433 :   my = zk_multable(nf, y);
    2609       56433 :   I = ZM_Z_divexact(ZM_mul(my, I), IZ); /* y I / (I\cap Z), integral */
    2610       56433 :   c1 = mul_content(c1, IZ);
    2611       56433 :   my = ZM_gauss(my, col_ei(N,1)); /* y^-1 */
    2612       56433 :   yZ = Q_denom(my); /* (y) \cap Z */
    2613       56433 :   I = hnfmodid(I, yZ);
    2614       56433 :   if (!aI) return gerepileupto(av, I);
    2615       55309 :   c1 = RgC_Rg_mul(my, c1);
    2616      112858 : END:
    2617      112858 :   if (c1) aI = ext_mul(nf, aI,c1);
    2618      112858 :   return gerepilecopy(av, mkvec2(I, aI));
    2619             : }
    2620             : 
    2621             : GEN
    2622           7 : idealmin(GEN nf, GEN x, GEN vdir)
    2623             : {
    2624           7 :   pari_sp av = avma;
    2625             :   GEN y, dx;
    2626           7 :   nf = checknf(nf);
    2627           7 :   switch( idealtyp(&x,&y) )
    2628             :   {
    2629           0 :     case id_PRINCIPAL: return gcopy(x);
    2630           0 :     case id_PRIME: x = pr_hnf(nf,x); break;
    2631           7 :     case id_MAT: if (lg(x) == 1) return gen_0;
    2632             :   }
    2633           7 :   x = Q_remove_denom(x, &dx);
    2634           7 :   y = idealpseudomin(x, nf_get_Gtwist(nf,vdir));
    2635           7 :   if (dx) y = RgC_Rg_div(y, dx);
    2636           7 :   return gerepileupto(av, y);
    2637             : }
    2638             : 
    2639             : /*******************************************************************/
    2640             : /*                                                                 */
    2641             : /*                   APPROXIMATION THEOREM                         */
    2642             : /*                                                                 */
    2643             : /*******************************************************************/
    2644             : /* a = ppi(a,b) ppo(a,b), where ppi regroups primes common to a and b
    2645             :  * and ppo(a,b) = Z_ppo(a,b) */
    2646             : /* return gcd(a,b),ppi(a,b),ppo(a,b) */
    2647             : GEN
    2648      455196 : Z_ppio(GEN a, GEN b)
    2649             : {
    2650      455196 :   GEN x, y, d = gcdii(a,b);
    2651      455196 :   if (is_pm1(d)) return mkvec3(gen_1, gen_1, a);
    2652      345786 :   x = d; y = diviiexact(a,d);
    2653             :   for(;;)
    2654       62951 :   {
    2655      408737 :     GEN g = gcdii(x,y);
    2656      408737 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2657       62951 :     x = mulii(x,g); y = diviiexact(y,g);
    2658             :   }
    2659             : }
    2660             : /* a = ppg(a,b)pple(a,b), where ppg regroups primes such that v(a) > v(b)
    2661             :  * and pple all others */
    2662             : /* return gcd(a,b),ppg(a,b),pple(a,b) */
    2663             : GEN
    2664           0 : Z_ppgle(GEN a, GEN b)
    2665             : {
    2666           0 :   GEN x, y, g, d = gcdii(a,b);
    2667           0 :   if (equalii(a, d)) return mkvec3(a, gen_1, a);
    2668           0 :   x = diviiexact(a,d); y = d;
    2669             :   for(;;)
    2670             :   {
    2671           0 :     g = gcdii(x,y);
    2672           0 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2673           0 :     x = mulii(x,g); y = diviiexact(y,g);
    2674             :   }
    2675             : }
    2676             : static void
    2677           0 : Z_dcba_rec(GEN L, GEN a, GEN b)
    2678             : {
    2679             :   GEN x, r, v, g, h, c, c0;
    2680             :   long n;
    2681           0 :   if (is_pm1(b)) {
    2682           0 :     if (!is_pm1(a)) vectrunc_append(L, a);
    2683           0 :     return;
    2684             :   }
    2685           0 :   v = Z_ppio(a,b);
    2686           0 :   a = gel(v,2);
    2687           0 :   r = gel(v,3);
    2688           0 :   if (!is_pm1(r)) vectrunc_append(L, r);
    2689           0 :   v = Z_ppgle(a,b);
    2690           0 :   g = gel(v,1);
    2691           0 :   h = gel(v,2);
    2692           0 :   x = c0 = gel(v,3);
    2693           0 :   for (n = 1; !is_pm1(h); n++)
    2694             :   {
    2695             :     GEN d, y;
    2696             :     long i;
    2697           0 :     v = Z_ppgle(h,sqri(g));
    2698           0 :     g = gel(v,1);
    2699           0 :     h = gel(v,2);
    2700           0 :     c = gel(v,3); if (is_pm1(c)) continue;
    2701           0 :     d = gcdii(c,b);
    2702           0 :     x = mulii(x,d);
    2703           0 :     y = d; for (i=1; i < n; i++) y = sqri(y);
    2704           0 :     Z_dcba_rec(L, diviiexact(c,y), d);
    2705             :   }
    2706           0 :   Z_dcba_rec(L,diviiexact(b,x), c0);
    2707             : }
    2708             : static GEN
    2709     3077130 : Z_cba_rec(GEN L, GEN a, GEN b)
    2710             : {
    2711             :   GEN g;
    2712     3077130 :   if (lg(L) > 10)
    2713             :   { /* a few naive steps before switching to dcba */
    2714           0 :     Z_dcba_rec(L, a, b);
    2715           0 :     return gel(L, lg(L)-1);
    2716             :   }
    2717     3077130 :   if (is_pm1(a)) return b;
    2718     1828316 :   g = gcdii(a,b);
    2719     1828316 :   if (is_pm1(g)) { vectrunc_append(L, a); return b; }
    2720     1365805 :   a = diviiexact(a,g);
    2721     1365805 :   b = diviiexact(b,g);
    2722     1365805 :   return Z_cba_rec(L, Z_cba_rec(L, a, g), b);
    2723             : }
    2724             : GEN
    2725      345520 : Z_cba(GEN a, GEN b)
    2726             : {
    2727      345520 :   GEN L = vectrunc_init(expi(a) + expi(b) + 2);
    2728      345520 :   GEN t = Z_cba_rec(L, a, b);
    2729      345520 :   if (!is_pm1(t)) vectrunc_append(L, t);
    2730      345520 :   return L;
    2731             : }
    2732             : /* P = coprime base, extend it by b; TODO: quadratic for now */
    2733             : GEN
    2734           0 : ZV_cba_extend(GEN P, GEN b)
    2735             : {
    2736           0 :   long i, l = lg(P);
    2737           0 :   GEN w = cgetg(l+1, t_VEC);
    2738           0 :   for (i = 1; i < l; i++)
    2739             :   {
    2740           0 :     GEN v = Z_cba(gel(P,i), b);
    2741           0 :     long nv = lg(v)-1;
    2742           0 :     gel(w,i) = vecslice(v, 1, nv-1); /* those divide P[i] but not b */
    2743           0 :     b = gel(v,nv);
    2744             :   }
    2745           0 :   gel(w,l) = b; return shallowconcat1(w);
    2746             : }
    2747             : GEN
    2748           0 : ZV_cba(GEN v)
    2749             : {
    2750           0 :   long i, l = lg(v);
    2751             :   GEN P;
    2752           0 :   if (l <= 2) return v;
    2753           0 :   P = Z_cba(gel(v,1), gel(v,2));
    2754           0 :   for (i = 3; i < l; i++) P = ZV_cba_extend(P, gel(v,i));
    2755           0 :   return P;
    2756             : }
    2757             : 
    2758             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2759             : GEN
    2760     2682771 : Z_ppo(GEN x, GEN f)
    2761             : {
    2762             :   for (;;)
    2763             :   {
    2764     2682771 :     f = gcdii(x, f); if (is_pm1(f)) break;
    2765     1559097 :     x = diviiexact(x, f);
    2766             :   }
    2767     1123674 :   return x;
    2768             : }
    2769             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2770             : ulong
    2771    41371383 : u_ppo(ulong x, ulong f)
    2772             : {
    2773             :   for (;;)
    2774             :   {
    2775    41371383 :     f = ugcd(x, f); if (f == 1) break;
    2776     8123788 :     x /= f;
    2777             :   }
    2778    33247595 :   return x;
    2779             : }
    2780             : 
    2781             : /* result known to be representable as an ulong */
    2782             : static ulong
    2783     2104316 : lcmuu(ulong a, ulong b) { ulong d = ugcd(a,b); return (a/d) * b; }
    2784             : 
    2785             : /* assume 0 < x < N; return u in (Z/NZ)^* such that u x = gcd(x,N) (mod N);
    2786             :  * set *pd = gcd(x,N) */
    2787             : ulong
    2788     3757813 : Fl_invgen(ulong x, ulong N, ulong *pd)
    2789             : {
    2790             :   ulong d, d0, e, v, v1;
    2791             :   long s;
    2792     3757813 :   *pd = d = xgcduu(N, x, 0, &v, &v1, &s);
    2793     3757813 :   if (s > 0) v = N - v;
    2794     3757813 :   if (d == 1) return v;
    2795             :   /* vx = gcd(x,N) (mod N), v coprime to N/d but need not be coprime to N */
    2796     2673478 :   e = N / d;
    2797     2673478 :   d0 = u_ppo(d, e); /* d = d0 d1, d0 coprime to N/d, rad(d1) | N/d */
    2798     2673478 :   if (d0 == 1) return v;
    2799     2104316 :   e = lcmuu(e, d / d0);
    2800     2104316 :   return u_chinese_coprime(v, 1, e, d0, e*d0);
    2801             : }
    2802             : 
    2803             : /* x t_INT, f ideal. Write x = x1 x2, sqf(x1) | f, (x2,f) = 1. Return x2 */
    2804             : static GEN
    2805         280 : nf_coprime_part(GEN nf, GEN x, GEN listpr)
    2806             : {
    2807         280 :   long v, j, lp = lg(listpr), N = nf_get_degree(nf);
    2808             :   GEN x1, x2, ex;
    2809             : 
    2810             : #if 0 /*1) via many gcds. Expensive ! */
    2811             :   GEN f = idealprodprime(nf, listpr);
    2812             :   f = ZM_hnfmodid(f, x); /* first gcd is less expensive since x in Z */
    2813             :   x = scalarmat(x, N);
    2814             :   for (;;)
    2815             :   {
    2816             :     if (gequal1(gcoeff(f,1,1))) break;
    2817             :     x = idealdivexact(nf, x, f);
    2818             :     f = ZM_hnfmodid(shallowconcat(f,x), gcoeff(x,1,1)); /* gcd(f,x) */
    2819             :   }
    2820             :   x2 = x;
    2821             : #else /*2) from prime decomposition */
    2822         280 :   x1 = NULL;
    2823         784 :   for (j=1; j<lp; j++)
    2824             :   {
    2825         504 :     GEN pr = gel(listpr,j);
    2826         504 :     v = Z_pval(x, pr_get_p(pr)); if (!v) continue;
    2827             : 
    2828         294 :     ex = muluu(v, pr_get_e(pr)); /* = v_pr(x) > 0 */
    2829         294 :     x1 = x1? idealmulpowprime(nf, x1, pr, ex)
    2830         294 :            : idealpow(nf, pr, ex);
    2831             :   }
    2832         280 :   x = scalarmat(x, N);
    2833         280 :   x2 = x1? idealdivexact(nf, x, x1): x;
    2834             : #endif
    2835         280 :   return x2;
    2836             : }
    2837             : 
    2838             : /* L0 in K^*, assume (L0,f) = 1. Return L integral, L0 = L mod f  */
    2839             : GEN
    2840        6727 : make_integral(GEN nf, GEN L0, GEN f, GEN listpr)
    2841             : {
    2842             :   GEN fZ, t, L, D2, d1, d2, d;
    2843             : 
    2844        6727 :   L = Q_remove_denom(L0, &d);
    2845        6727 :   if (!d) return L0;
    2846             : 
    2847             :   /* L0 = L / d, L integral */
    2848        1183 :   fZ = gcoeff(f,1,1);
    2849        1183 :   if (typ(L) == t_INT) return Fp_mul(L, Fp_inv(d, fZ), fZ);
    2850             :   /* Kill denom part coprime to fZ */
    2851         714 :   d2 = Z_ppo(d, fZ);
    2852         714 :   t = Fp_inv(d2, fZ); if (!is_pm1(t)) L = ZC_Z_mul(L,t);
    2853         714 :   if (equalii(d, d2)) return L;
    2854             : 
    2855         280 :   d1 = diviiexact(d, d2);
    2856             :   /* L0 = (L / d1) mod f. d1 not coprime to f
    2857             :    * write (d1) = D1 D2, D2 minimal, (D2,f) = 1. */
    2858         280 :   D2 = nf_coprime_part(nf, d1, listpr);
    2859         280 :   t = idealaddtoone_i(nf, D2, f); /* in D2, 1 mod f */
    2860         280 :   L = nfmuli(nf,t,L);
    2861             : 
    2862             :   /* if (L0, f) = 1, then L in D1 ==> in D1 D2 = (d1) */
    2863         280 :   return Q_div_to_int(L, d1); /* exact division */
    2864             : }
    2865             : 
    2866             : /* assume L is a list of prime ideals. Return the product */
    2867             : GEN
    2868         329 : idealprodprime(GEN nf, GEN L)
    2869             : {
    2870         329 :   long l = lg(L), i;
    2871             :   GEN z;
    2872         329 :   if (l == 1) return matid(nf_get_degree(nf));
    2873         329 :   z = pr_hnf(nf, gel(L,1));
    2874         357 :   for (i=2; i<l; i++) z = idealHNF_mul_two(nf,z, gel(L,i));
    2875         329 :   return z;
    2876             : }
    2877             : 
    2878             : /* optimize for the frequent case I = nfhnf()[2]: lots of them are 1 */
    2879             : GEN
    2880         378 : idealprod(GEN nf, GEN I)
    2881             : {
    2882         378 :   long i, l = lg(I);
    2883             :   GEN z;
    2884         945 :   for (i = 1; i < l; i++)
    2885         938 :     if (!equali1(gel(I,i))) break;
    2886         378 :   if (i == l) return gen_1;
    2887         371 :   z = gel(I,i);
    2888         595 :   for (i++; i<l; i++) z = idealmul(nf, z, gel(I,i));
    2889         371 :   return z;
    2890             : }
    2891             : 
    2892             : /* v_pr(idealprod(nf,I)) */
    2893             : long
    2894        2135 : idealprodval(GEN nf, GEN I, GEN pr)
    2895             : {
    2896        2135 :   long i, l = lg(I), v = 0;
    2897       12600 :   for (i = 1; i < l; i++)
    2898       10465 :     if (!equali1(gel(I,i))) v += idealval(nf, gel(I,i), pr);
    2899        2135 :   return v;
    2900             : }
    2901             : 
    2902             : /* assume L is a list of prime ideals. Return prod L[i]^e[i] */
    2903             : GEN
    2904       12481 : factorbackprime(GEN nf, GEN L, GEN e)
    2905             : {
    2906       12481 :   long l = lg(L), i;
    2907             :   GEN z;
    2908             : 
    2909       12481 :   if (l == 1) return matid(nf_get_degree(nf));
    2910       11865 :   z = idealpow(nf, gel(L,1), gel(e,1));
    2911       18214 :   for (i=2; i<l; i++)
    2912        6349 :     if (signe(gel(e,i))) z = idealmulpowprime(nf,z, gel(L,i),gel(e,i));
    2913       11865 :   return z;
    2914             : }
    2915             : 
    2916             : /* F in Z, divisible exactly by pr.p. Return F-uniformizer for pr, i.e.
    2917             :  * a t in Z_K such that v_pr(t) = 1 and (t, F/pr) = 1 */
    2918             : GEN
    2919       22650 : pr_uniformizer(GEN pr, GEN F)
    2920             : {
    2921       22650 :   GEN p = pr_get_p(pr), t = pr_get_gen(pr);
    2922       22650 :   if (!equalii(F, p))
    2923             :   {
    2924       12275 :     long e = pr_get_e(pr);
    2925       12275 :     GEN u, v, q = (e == 1)? sqri(p): p;
    2926       12275 :     u = mulii(q, Fp_inv(q, diviiexact(F,p))); /* 1 mod F/p, 0 mod q */
    2927       12275 :     v = subui(1UL, u); /* 0 mod F/p, 1 mod q */
    2928       12275 :     if (pr_is_inert(pr))
    2929           0 :       t = addii(mulii(p, v), u);
    2930             :     else
    2931             :     {
    2932       12275 :       t = ZC_Z_mul(t, v);
    2933       12275 :       gel(t,1) = addii(gel(t,1), u); /* return u + vt */
    2934             :     }
    2935             :   }
    2936       22650 :   return t;
    2937             : }
    2938             : /* L = list of prime ideals, return lcm_i (L[i] \cap \ZM) */
    2939             : GEN
    2940       53112 : prV_lcm_capZ(GEN L)
    2941             : {
    2942       53112 :   long i, r = lg(L);
    2943             :   GEN F;
    2944       53112 :   if (r == 1) return gen_1;
    2945       44019 :   F = pr_get_p(gel(L,1));
    2946       71062 :   for (i = 2; i < r; i++)
    2947             :   {
    2948       27043 :     GEN pr = gel(L,i), p = pr_get_p(pr);
    2949       27043 :     if (!dvdii(F, p)) F = mulii(F,p);
    2950             :   }
    2951       44019 :   return F;
    2952             : }
    2953             : 
    2954             : /* Given a prime ideal factorization with possibly zero or negative
    2955             :  * exponents, gives b such that v_p(b) = v_p(x) for all prime ideals pr | x
    2956             :  * and v_pr(b) >= 0 for all other pr.
    2957             :  * For optimal performance, all [anti-]uniformizers should be precomputed,
    2958             :  * but no support for this yet.
    2959             :  *
    2960             :  * If nored, do not reduce result.
    2961             :  * No garbage collecting */
    2962             : static GEN
    2963       27394 : idealapprfact_i(GEN nf, GEN x, int nored)
    2964             : {
    2965             :   GEN z, d, L, e, e2, F;
    2966             :   long i, r;
    2967             :   int flagden;
    2968             : 
    2969       27394 :   nf = checknf(nf);
    2970       27394 :   L = gel(x,1);
    2971       27394 :   e = gel(x,2);
    2972       27394 :   F = prV_lcm_capZ(L);
    2973       27394 :   flagden = 0;
    2974       27394 :   z = NULL; r = lg(e);
    2975       59921 :   for (i = 1; i < r; i++)
    2976             :   {
    2977       32527 :     long s = signe(gel(e,i));
    2978             :     GEN pi, q;
    2979       32527 :     if (!s) continue;
    2980       18919 :     if (s < 0) flagden = 1;
    2981       18919 :     pi = pr_uniformizer(gel(L,i), F);
    2982       18919 :     q = nfpow(nf, pi, gel(e,i));
    2983       18919 :     z = z? nfmul(nf, z, q): q;
    2984             :   }
    2985       27394 :   if (!z) return gen_1;
    2986       10412 :   if (nored || typ(z) != t_COL) return z;
    2987        4347 :   e2 = cgetg(r, t_VEC);
    2988       11949 :   for (i=1; i<r; i++) gel(e2,i) = addiu(gel(e,i), 1);
    2989        4347 :   x = factorbackprime(nf, L,e2);
    2990        4347 :   if (flagden) /* denominator */
    2991             :   {
    2992        4333 :     z = Q_remove_denom(z, &d);
    2993        4333 :     d = diviiexact(d, Z_ppo(d, F));
    2994        4333 :     x = RgM_Rg_mul(x, d);
    2995             :   }
    2996             :   else
    2997          14 :     d = NULL;
    2998        4347 :   z = ZC_reducemodlll(z, x);
    2999        4347 :   return d? RgC_Rg_div(z,d): z;
    3000             : }
    3001             : 
    3002             : GEN
    3003           0 : idealapprfact(GEN nf, GEN x) {
    3004           0 :   pari_sp av = avma;
    3005           0 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    3006             : }
    3007             : GEN
    3008          14 : idealappr(GEN nf, GEN x) {
    3009          14 :   pari_sp av = avma;
    3010          14 :   if (!is_nf_extfactor(x)) x = idealfactor(nf, x);
    3011          14 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    3012             : }
    3013             : 
    3014             : /* OBSOLETE */
    3015             : GEN
    3016          14 : idealappr0(GEN nf, GEN x, long fl) { (void)fl; return idealappr(nf, x); }
    3017             : 
    3018             : static GEN
    3019          21 : mat_ideal_two_elt2(GEN nf, GEN x, GEN a)
    3020             : {
    3021          21 :   GEN F = idealfactor(nf,a), P = gel(F,1), E = gel(F,2);
    3022          21 :   long i, r = lg(E);
    3023          84 :   for (i=1; i<r; i++) gel(E,i) = stoi( idealval(nf,x,gel(P,i)) );
    3024          21 :   return idealapprfact_i(nf,F,1);
    3025             : }
    3026             : 
    3027             : static void
    3028          14 : not_in_ideal(GEN a) {
    3029          14 :   pari_err_DOMAIN("idealtwoelt2","element mod ideal", "!=", gen_0, a);
    3030           0 : }
    3031             : /* x integral in HNF, a an 'nf' */
    3032             : static int
    3033          28 : in_ideal(GEN x, GEN a)
    3034             : {
    3035          28 :   switch(typ(a))
    3036             :   {
    3037          14 :     case t_INT: return dvdii(a, gcoeff(x,1,1));
    3038           7 :     case t_COL: return RgV_is_ZV(a) && !!hnf_invimage(x, a);
    3039           7 :     default: return 0;
    3040             :   }
    3041             : }
    3042             : 
    3043             : /* Given an integral ideal x and a in x, gives a b such that
    3044             :  * x = aZ_K + bZ_K using the approximation theorem */
    3045             : GEN
    3046          42 : idealtwoelt2(GEN nf, GEN x, GEN a)
    3047             : {
    3048          42 :   pari_sp av = avma;
    3049             :   GEN cx, b;
    3050             : 
    3051          42 :   nf = checknf(nf);
    3052          42 :   a = nf_to_scalar_or_basis(nf, a);
    3053          42 :   x = idealhnf_shallow(nf,x);
    3054          42 :   if (lg(x) == 1)
    3055             :   {
    3056          14 :     if (!isintzero(a)) not_in_ideal(a);
    3057           7 :     set_avma(av); return gen_0;
    3058             :   }
    3059          28 :   x = Q_primitive_part(x, &cx);
    3060          28 :   if (cx) a = gdiv(a, cx);
    3061          28 :   if (!in_ideal(x, a)) not_in_ideal(a);
    3062          21 :   b = mat_ideal_two_elt2(nf, x, a);
    3063          21 :   if (typ(b) == t_COL)
    3064             :   {
    3065          14 :     GEN mod = idealhnf_principal(nf,a);
    3066          14 :     b = ZC_hnfrem(b,mod);
    3067          14 :     if (ZV_isscalar(b)) b = gel(b,1);
    3068             :   }
    3069             :   else
    3070             :   {
    3071           7 :     GEN aZ = typ(a) == t_COL? Q_denom(zk_inv(nf,a)): a; /* (a) \cap Z */
    3072           7 :     b = centermodii(b, aZ, shifti(aZ,-1));
    3073             :   }
    3074          21 :   b = cx? gmul(b,cx): gcopy(b);
    3075          21 :   return gerepileupto(av, b);
    3076             : }
    3077             : 
    3078             : /* Given 2 integral ideals x and y in nf, returns a beta in nf such that
    3079             :  * beta * x is an integral ideal coprime to y */
    3080             : GEN
    3081       21308 : idealcoprimefact(GEN nf, GEN x, GEN fy)
    3082             : {
    3083       21308 :   GEN L = gel(fy,1), e;
    3084       21308 :   long i, r = lg(L);
    3085             : 
    3086       21308 :   e = cgetg(r, t_COL);
    3087       39235 :   for (i=1; i<r; i++) gel(e,i) = stoi( -idealval(nf,x,gel(L,i)) );
    3088       21308 :   return idealapprfact_i(nf, mkmat2(L,e), 0);
    3089             : }
    3090             : GEN
    3091          70 : idealcoprime(GEN nf, GEN x, GEN y)
    3092             : {
    3093          70 :   pari_sp av = avma;
    3094          70 :   return gerepileupto(av, idealcoprimefact(nf, x, idealfactor(nf,y)));
    3095             : }
    3096             : 
    3097             : GEN
    3098           7 : nfmulmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    3099             : {
    3100           7 :   pari_sp av = avma;
    3101           7 :   GEN z, p, pr = modpr, T;
    3102             : 
    3103           7 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    3104           0 :   x = nf_to_Fq(nf,x,modpr);
    3105           0 :   y = nf_to_Fq(nf,y,modpr);
    3106           0 :   z = Fq_mul(x,y,T,p);
    3107           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    3108             : }
    3109             : 
    3110             : GEN
    3111           0 : nfdivmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    3112             : {
    3113           0 :   pari_sp av = avma;
    3114           0 :   nf = checknf(nf);
    3115           0 :   return gerepileupto(av, nfreducemodpr(nf, nfdiv(nf,x,y), modpr));
    3116             : }
    3117             : 
    3118             : GEN
    3119           0 : nfpowmodpr(GEN nf, GEN x, GEN k, GEN modpr)
    3120             : {
    3121           0 :   pari_sp av=avma;
    3122           0 :   GEN z, T, p, pr = modpr;
    3123             : 
    3124           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    3125           0 :   z = nf_to_Fq(nf,x,modpr);
    3126           0 :   z = Fq_pow(z,k,T,p);
    3127           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    3128             : }
    3129             : 
    3130             : GEN
    3131           0 : nfkermodpr(GEN nf, GEN x, GEN modpr)
    3132             : {
    3133           0 :   pari_sp av = avma;
    3134           0 :   GEN T, p, pr = modpr;
    3135             : 
    3136           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    3137           0 :   if (typ(x)!=t_MAT) pari_err_TYPE("nfkermodpr",x);
    3138           0 :   x = nfM_to_FqM(x, nf, modpr);
    3139           0 :   return gerepilecopy(av, FqM_to_nfM(FqM_ker(x,T,p), modpr));
    3140             : }
    3141             : 
    3142             : GEN
    3143           0 : nfsolvemodpr(GEN nf, GEN a, GEN b, GEN pr)
    3144             : {
    3145           0 :   const char *f = "nfsolvemodpr";
    3146           0 :   pari_sp av = avma;
    3147             :   GEN T, p, modpr;
    3148             : 
    3149           0 :   nf = checknf(nf);
    3150           0 :   modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    3151           0 :   if (typ(a)!=t_MAT) pari_err_TYPE(f,a);
    3152           0 :   a = nfM_to_FqM(a, nf, modpr);
    3153           0 :   switch(typ(b))
    3154             :   {
    3155           0 :     case t_MAT:
    3156           0 :       b = nfM_to_FqM(b, nf, modpr);
    3157           0 :       b = FqM_gauss(a,b,T,p);
    3158           0 :       if (!b) pari_err_INV(f,a);
    3159           0 :       a = FqM_to_nfM(b, modpr);
    3160           0 :       break;
    3161           0 :     case t_COL:
    3162           0 :       b = nfV_to_FqV(b, nf, modpr);
    3163           0 :       b = FqM_FqC_gauss(a,b,T,p);
    3164           0 :       if (!b) pari_err_INV(f,a);
    3165           0 :       a = FqV_to_nfV(b, modpr);
    3166           0 :       break;
    3167           0 :     default: pari_err_TYPE(f,b);
    3168             :   }
    3169           0 :   return gerepilecopy(av, a);
    3170             : }

Generated by: LCOV version 1.13