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.16.2 lcov report (development 29375-be3334fb81) Lines: 1658 1810 91.6 %
Date: 2024-05-26 08:08:42 Functions: 166 181 91.7 %
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; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : /*******************************************************************/
      16             : /*                                                                 */
      17             : /*                       BASIC NF OPERATIONS                       */
      18             : /*                           (continued)                           */
      19             : /*                                                                 */
      20             : /*******************************************************************/
      21             : #include "pari.h"
      22             : #include "paripriv.h"
      23             : 
      24             : #define DEBUGLEVEL DEBUGLEVEL_nf
      25             : 
      26             : /*******************************************************************/
      27             : /*                                                                 */
      28             : /*                     IDEAL OPERATIONS                            */
      29             : /*                                                                 */
      30             : /*******************************************************************/
      31             : 
      32             : /* A valid ideal is either principal (valid nf_element), or prime, or a matrix
      33             :  * on the integer basis in HNF.
      34             :  * A prime ideal is of the form [p,a,e,f,b], where the ideal is p.Z_K+a.Z_K,
      35             :  * p is a rational prime, a belongs to Z_K, e=e(P/p), f=f(P/p), and b
      36             :  * is Lenstra's constant, such that p.P^(-1)= p Z_K + b Z_K.
      37             :  *
      38             :  * An extended ideal is a couple [I,F] where I is an ideal and F is either an
      39             :  * algebraic number, or a factorization matrix attached to an algebraic number.
      40             :  * All routines work with either extended ideals or ideals (an omitted F is
      41             :  * assumed to be factor(1)). All ideals are output in HNF form. */
      42             : 
      43             : /* types and conversions */
      44             : 
      45             : long
      46    16075066 : idealtyp(GEN *ideal, GEN *arch)
      47             : {
      48    16075066 :   GEN x = *ideal;
      49    16075066 :   long t,lx,tx = typ(x);
      50             : 
      51    16075066 :   if (tx!=t_VEC || lg(x)!=3) { if (arch) *arch = NULL; }
      52             :   else
      53             :   {
      54     1239709 :     GEN a = gel(x,2);
      55     1239709 :     if (typ(a) == t_MAT && lg(a) != 3)
      56             :     { /* allow [;] */
      57          14 :       if (lg(a) != 1) pari_err_TYPE("idealtyp [extended ideal]",x);
      58           7 :       if (arch) *arch = trivial_fact();
      59             :     }
      60             :     else
      61     1239695 :       if (arch) *arch = a;
      62     1239702 :     x = gel(x,1); tx = typ(x);
      63             :   }
      64    16075059 :   switch(tx)
      65             :   {
      66    12222155 :     case t_MAT: lx = lg(x);
      67    12222155 :       if (lx == 1) { t = id_PRINCIPAL; x = gen_0; break; }
      68    12221994 :       if (lx != lgcols(x)) pari_err_TYPE("idealtyp [nonsquare t_MAT]",x);
      69    12221962 :       t = id_MAT;
      70    12221962 :       break;
      71             : 
      72     2819949 :     case t_VEC:
      73     2819949 :       if (!checkprid_i(x)) pari_err_TYPE("idealtyp [fake prime ideal]",x);
      74     2819900 :       t = id_PRIME; break;
      75             : 
      76     1033294 :     case t_POL: case t_POLMOD: case t_COL:
      77             :     case t_INT: case t_FRAC:
      78     1033294 :       t = id_PRINCIPAL; break;
      79           0 :     default:
      80           0 :       pari_err_TYPE("idealtyp",x);
      81             :       return 0; /*LCOV_EXCL_LINE*/
      82             :   }
      83    16075317 :   *ideal = x; return t;
      84             : }
      85             : 
      86             : /* true nf; v = [a,x,...], a in Z. Return (a,x) */
      87             : GEN
      88      683216 : idealhnf_two(GEN nf, GEN v)
      89             : {
      90      683216 :   GEN p = gel(v,1), pi = gel(v,2), m = zk_scalar_or_multable(nf, pi);
      91      683196 :   if (typ(m) == t_INT) return scalarmat(gcdii(m,p), nf_get_degree(nf));
      92      610597 :   return ZM_hnfmodid(m, p);
      93             : }
      94             : /* true nf */
      95             : GEN
      96     3605906 : pr_hnf(GEN nf, GEN pr)
      97             : {
      98     3605906 :   GEN p = pr_get_p(pr), m;
      99     3605869 :   if (pr_is_inert(pr)) return scalarmat(p, nf_get_degree(nf));
     100     3170317 :   m = zk_scalar_or_multable(nf, pr_get_gen(pr));
     101     3169894 :   return ZM_hnfmodprime(m, p);
     102             : }
     103             : 
     104             : GEN
     105     1366631 : idealhnf_principal(GEN nf, GEN x)
     106             : {
     107             :   GEN cx;
     108     1366631 :   x = nf_to_scalar_or_basis(nf, x);
     109     1366630 :   switch(typ(x))
     110             :   {
     111     1051893 :     case t_COL: break;
     112      281953 :     case t_INT:  if (!signe(x)) return cgetg(1,t_MAT);
     113      280322 :       return scalarmat(absi_shallow(x), nf_get_degree(nf));
     114       32784 :     case t_FRAC:
     115       32784 :       return scalarmat(Q_abs_shallow(x), nf_get_degree(nf));
     116           0 :     default: pari_err_TYPE("idealhnf",x);
     117             :   }
     118     1051893 :   x = Q_primitive_part(x, &cx);
     119     1051885 :   RgV_check_ZV(x, "idealhnf");
     120     1051884 :   x = zk_multable(nf, x);
     121     1051892 :   x = ZM_hnfmodid(x, zkmultable_capZ(x));
     122     1051895 :   return cx? ZM_Q_mul(x,cx): x;
     123             : }
     124             : 
     125             : /* x integral ideal in t_MAT form, nx columns */
     126             : static GEN
     127           7 : vec_mulid(GEN nf, GEN x, long nx, long N)
     128             : {
     129           7 :   GEN m = cgetg(nx*N + 1, t_MAT);
     130             :   long i, j, k;
     131          21 :   for (i=k=1; i<=nx; i++)
     132          56 :     for (j=1; j<=N; j++) gel(m, k++) = zk_ei_mul(nf, gel(x,i),j);
     133           7 :   return m;
     134             : }
     135             : /* true nf */
     136             : GEN
     137     1743734 : idealhnf_shallow(GEN nf, GEN x)
     138             : {
     139     1743734 :   long tx = typ(x), lx = lg(x), N;
     140             : 
     141             :   /* cannot use idealtyp because here we allow nonsquare matrices */
     142     1743734 :   if (tx == t_VEC && lx == 3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
     143     1743734 :   if (tx == t_VEC && lx == 6)
     144             :   {
     145      483930 :     if (!checkprid_i(x)) pari_err_TYPE("idealhnf [fake prime ideal]",x);
     146      483920 :     return pr_hnf(nf,x); /* PRIME */
     147             :   }
     148     1259804 :   switch(tx)
     149             :   {
     150      101922 :     case t_MAT:
     151             :     {
     152             :       GEN cx;
     153      101922 :       long nx = lx-1;
     154      101922 :       N = nf_get_degree(nf);
     155      101921 :       if (nx == 0) return cgetg(1, t_MAT);
     156      101900 :       if (nbrows(x) != N) pari_err_TYPE("idealhnf [wrong dimension]",x);
     157      101893 :       if (nx == 1) return idealhnf_principal(nf, gel(x,1));
     158             : 
     159       85584 :       if (nx == N && RgM_is_ZM(x) && ZM_ishnf(x)) return x;
     160       49637 :       x = Q_primitive_part(x, &cx);
     161       49637 :       if (nx < N) x = vec_mulid(nf, x, nx, N);
     162       49637 :       x = ZM_hnfmod(x, ZM_detmult(x));
     163       49637 :       return cx? ZM_Q_mul(x,cx): x;
     164             :     }
     165          14 :     case t_QFB:
     166             :     {
     167          14 :       pari_sp av = avma;
     168          14 :       GEN u, D = nf_get_disc(nf), T = nf_get_pol(nf), f = nf_get_index(nf);
     169          14 :       GEN A = gel(x,1), B = gel(x,2);
     170          14 :       N = nf_get_degree(nf);
     171          14 :       if (N != 2)
     172           0 :         pari_err_TYPE("idealhnf [Qfb for nonquadratic fields]", x);
     173          14 :       if (!equalii(qfb_disc(x), D))
     174           7 :         pari_err_DOMAIN("idealhnf [Qfb]", "disc(q)", "!=", D, x);
     175             :       /* x -> A Z + (-B + sqrt(D)) / 2 Z
     176             :          K = Q[t]/T(t), t^2 + ut + v = 0,  u^2 - 4v = Df^2
     177             :          => t = (-u + sqrt(D) f)/2
     178             :          => sqrt(D)/2 = (t + u/2)/f */
     179           7 :       u = gel(T,3);
     180           7 :       B = deg1pol_shallow(ginv(f),
     181             :                           gsub(gdiv(u, shifti(f,1)), gdiv(B,gen_2)),
     182           7 :                           varn(T));
     183           7 :       return gerepileupto(av, idealhnf_two(nf, mkvec2(A,B)));
     184             :     }
     185     1157868 :     default: return idealhnf_principal(nf, x); /* PRINCIPAL */
     186             :   }
     187             : }
     188             : /* true nf */
     189             : GEN
     190         308 : idealhnf(GEN nf, GEN x)
     191             : {
     192         308 :   pari_sp av = avma;
     193         308 :   GEN y = idealhnf_shallow(nf, x);
     194         294 :   return (avma == av)? gcopy(y): gerepileupto(av, y);
     195             : }
     196             : 
     197             : /* GP functions */
     198             : 
     199             : GEN
     200          70 : idealtwoelt0(GEN nf, GEN x, GEN a)
     201             : {
     202          70 :   if (!a) return idealtwoelt(nf,x);
     203          42 :   return idealtwoelt2(nf,x,a);
     204             : }
     205             : 
     206             : GEN
     207          84 : idealpow0(GEN nf, GEN x, GEN n, long flag)
     208             : {
     209          84 :   if (flag) return idealpowred(nf,x,n);
     210          77 :   return idealpow(nf,x,n);
     211             : }
     212             : 
     213             : GEN
     214          70 : idealmul0(GEN nf, GEN x, GEN y, long flag)
     215             : {
     216          70 :   if (flag) return idealmulred(nf,x,y);
     217          63 :   return idealmul(nf,x,y);
     218             : }
     219             : 
     220             : GEN
     221          56 : idealdiv0(GEN nf, GEN x, GEN y, long flag)
     222             : {
     223          56 :   switch(flag)
     224             :   {
     225          28 :     case 0: return idealdiv(nf,x,y);
     226          28 :     case 1: return idealdivexact(nf,x,y);
     227           0 :     default: pari_err_FLAG("idealdiv");
     228             :   }
     229             :   return NULL; /* LCOV_EXCL_LINE */
     230             : }
     231             : 
     232             : GEN
     233          70 : idealaddtoone0(GEN nf, GEN arg1, GEN arg2)
     234             : {
     235          70 :   if (!arg2) return idealaddmultoone(nf,arg1);
     236          35 :   return idealaddtoone(nf,arg1,arg2);
     237             : }
     238             : 
     239             : /* b not a scalar */
     240             : static GEN
     241          77 : hnf_Z_ZC(GEN nf, GEN a, GEN b) { return hnfmodid(zk_multable(nf,b), a); }
     242             : /* b not a scalar */
     243             : static GEN
     244          70 : hnf_Z_QC(GEN nf, GEN a, GEN b)
     245             : {
     246             :   GEN db;
     247          70 :   b = Q_remove_denom(b, &db);
     248          70 :   if (db) a = mulii(a, db);
     249          70 :   b = hnf_Z_ZC(nf,a,b);
     250          70 :   return db? RgM_Rg_div(b, db): b;
     251             : }
     252             : /* b not a scalar (not point in trying to optimize for this case) */
     253             : static GEN
     254          77 : hnf_Q_QC(GEN nf, GEN a, GEN b)
     255             : {
     256             :   GEN da, db;
     257          77 :   if (typ(a) == t_INT) return hnf_Z_QC(nf, a, b);
     258           7 :   da = gel(a,2);
     259           7 :   a = gel(a,1);
     260           7 :   b = Q_remove_denom(b, &db);
     261             :   /* write da = d*A, db = d*B, gcd(A,B) = 1
     262             :    * gcd(a/(d A), b/(d B)) = gcd(a B, A b) / A B d = gcd(a B, b) / A B d */
     263           7 :   if (db)
     264             :   {
     265           7 :     GEN d = gcdii(da,db);
     266           7 :     if (!is_pm1(d)) db = diviiexact(db,d); /* B */
     267           7 :     if (!is_pm1(db))
     268             :     {
     269           7 :       a = mulii(a, db); /* a B */
     270           7 :       da = mulii(da, db); /* A B d = lcm(denom(a),denom(b)) */
     271             :     }
     272             :   }
     273           7 :   return RgM_Rg_div(hnf_Z_ZC(nf,a,b), da);
     274             : }
     275             : static GEN
     276           7 : hnf_QC_QC(GEN nf, GEN a, GEN b)
     277             : {
     278             :   GEN da, db, d, x;
     279           7 :   a = Q_remove_denom(a, &da);
     280           7 :   b = Q_remove_denom(b, &db);
     281           7 :   if (da) b = ZC_Z_mul(b, da);
     282           7 :   if (db) a = ZC_Z_mul(a, db);
     283           7 :   d = mul_denom(da, db);
     284           7 :   a = zk_multable(nf,a); da = zkmultable_capZ(a);
     285           7 :   b = zk_multable(nf,b); db = zkmultable_capZ(b);
     286           7 :   x = ZM_hnfmodid(shallowconcat(a,b), gcdii(da,db));
     287           7 :   return d? RgM_Rg_div(x, d): x;
     288             : }
     289             : static GEN
     290          21 : hnf_Q_Q(GEN nf, GEN a, GEN b) {return scalarmat(Q_gcd(a,b), nf_get_degree(nf));}
     291             : GEN
     292         217 : idealhnf0(GEN nf, GEN a, GEN b)
     293             : {
     294             :   long ta, tb;
     295             :   pari_sp av;
     296             :   GEN x;
     297         217 :   nf = checknf(nf);
     298         217 :   if (!b) return idealhnf(nf,a);
     299             : 
     300             :   /* HNF of aZ_K+bZ_K */
     301         112 :   av = avma;
     302         112 :   a = nf_to_scalar_or_basis(nf,a); ta = typ(a);
     303         112 :   b = nf_to_scalar_or_basis(nf,b); tb = typ(b);
     304         105 :   if (ta == t_COL)
     305          14 :     x = (tb==t_COL)? hnf_QC_QC(nf, a,b): hnf_Q_QC(nf, b,a);
     306             :   else
     307          91 :     x = (tb==t_COL)? hnf_Q_QC(nf, a,b): hnf_Q_Q(nf, a,b);
     308         105 :   return gerepileupto(av, x);
     309             : }
     310             : 
     311             : /*******************************************************************/
     312             : /*                                                                 */
     313             : /*                       TWO-ELEMENT FORM                          */
     314             : /*                                                                 */
     315             : /*******************************************************************/
     316             : static GEN idealapprfact_i(GEN nf, GEN x, int nored);
     317             : 
     318             : static int
     319      229385 : ok_elt(GEN x, GEN xZ, GEN y)
     320             : {
     321      229385 :   pari_sp av = avma;
     322      229385 :   return gc_bool(av, ZM_equal(x, ZM_hnfmodid(y, xZ)));
     323             : }
     324             : 
     325             : /* a + s * b, a and b ZM, s integer */
     326             : static GEN
     327       67256 : addmul_mat(GEN a, GEN s, GEN b)
     328             : {
     329       67256 :   if (!signe(s)) return a;
     330       58402 :   if (!equali1(s)) b = ZM_Z_mul(b, s);
     331       58402 :   return a? ZM_add(a, b): b;
     332             : }
     333             : 
     334             : static GEN
     335      121813 : get_random_a(GEN nf, GEN x, GEN xZ)
     336             : {
     337             :   pari_sp av;
     338      121813 :   long i, lm, l = lg(x);
     339             :   GEN z, beta, mul;
     340             : 
     341      121813 :   beta= cgetg(l, t_MAT);
     342      121813 :   mul = cgetg(l, t_VEC); lm = 1; /* = lg(mul) */
     343             :   /* look for a in x such that a O/xZ = x O/xZ */
     344      255550 :   for (i = 2; i < l; i++)
     345             :   {
     346      245587 :     GEN xi = gel(x,i);
     347      245587 :     GEN t = FpM_red(zk_multable(nf,xi), xZ); /* ZM, cannot be a scalar */
     348      245580 :     if (gequal0(t)) continue;
     349      200668 :     if (ok_elt(x,xZ, t)) return xi;
     350       88817 :     gel(beta,lm) = xi;
     351             :     /* mul[i] = { canonical generators for x[i] O/xZ as Z-module } */
     352       88817 :     gel(mul,lm) = t; lm++;
     353             :   }
     354        9963 :   setlg(mul, lm);
     355        9963 :   setlg(beta,lm); z = cgetg(lm, t_VEC);
     356       30556 :   for(av = avma;; set_avma(av))
     357       20593 :   {
     358       30556 :     GEN a = NULL;
     359       97812 :     for (i = 1; i < lm; i++)
     360             :     {
     361       67256 :       gel(z,i) = randomi(xZ);
     362       67256 :       a = addmul_mat(a, gel(z,i), gel(mul,i));
     363             :     }
     364             :     /* a = matrix (NOT HNF) of ideal generated by beta.z in O/xZ */
     365       30556 :     if (a && ok_elt(x,xZ, a)) break;
     366             :   }
     367        9963 :   return ZM_ZC_mul(beta, z);
     368             : }
     369             : 
     370             : /* x square matrix, assume it is HNF */
     371             : static GEN
     372      257713 : mat_ideal_two_elt(GEN nf, GEN x)
     373             : {
     374             :   GEN y, a, cx, xZ;
     375      257713 :   long N = nf_get_degree(nf);
     376             :   pari_sp av, tetpil;
     377             : 
     378      257713 :   if (lg(x)-1 != N) pari_err_DIM("idealtwoelt");
     379      257699 :   if (N == 2) return mkvec2copy(gcoeff(x,1,1), gel(x,2));
     380             : 
     381      137994 :   y = cgetg(3,t_VEC); av = avma;
     382      137994 :   cx = Q_content(x);
     383      137993 :   xZ = gcoeff(x,1,1);
     384      137993 :   if (gequal(xZ, cx)) /* x = (cx) */
     385             :   {
     386        5341 :     gel(y,1) = cx;
     387        5341 :     gel(y,2) = gen_0; return y;
     388             :   }
     389      132651 :   if (equali1(cx)) cx = NULL;
     390             :   else
     391             :   {
     392        3744 :     x = Q_div_to_int(x, cx);
     393        3744 :     xZ = gcoeff(x,1,1);
     394             :   }
     395      132651 :   if (N < 6)
     396      113426 :     a = get_random_a(nf, x, xZ);
     397             :   else
     398             :   {
     399       19225 :     const long FB[] = { _evallg(15+1) | evaltyp(t_VECSMALL),
     400             :       2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47
     401             :     };
     402       19225 :     GEN P, E, a1 = Z_lsmoothen(xZ, (GEN)FB, &P, &E);
     403       19225 :     if (!a1) /* factors completely */
     404       10838 :       a = idealapprfact_i(nf, idealfactor(nf,x), 1);
     405        8387 :     else if (lg(P) == 1) /* no small factors */
     406        2589 :       a = get_random_a(nf, x, xZ);
     407             :     else /* general case */
     408             :     {
     409             :       GEN A0, A1, a0, u0, u1, v0, v1, pi0, pi1, t, u;
     410        5798 :       a0 = diviiexact(xZ, a1);
     411        5798 :       A0 = ZM_hnfmodid(x, a0); /* smooth part of x */
     412        5798 :       A1 = ZM_hnfmodid(x, a1); /* cofactor */
     413        5798 :       pi0 = idealapprfact_i(nf, idealfactor(nf,A0), 1);
     414        5798 :       pi1 = get_random_a(nf, A1, a1);
     415        5798 :       (void)bezout(a0, a1, &v0,&v1);
     416        5798 :       u0 = mulii(a0, v0);
     417        5798 :       u1 = mulii(a1, v1);
     418        5798 :       if (typ(pi0) != t_COL) t = addmulii(u0, pi0, u1);
     419             :       else
     420        5798 :       { t = ZC_Z_mul(pi0, u1); gel(t,1) = addii(gel(t,1), u0); }
     421        5798 :       u = ZC_Z_mul(pi1, u0); gel(u,1) = addii(gel(u,1), u1);
     422        5798 :       a = nfmuli(nf, centermod(u, xZ), centermod(t, xZ));
     423             :     }
     424             :   }
     425      132653 :   if (cx)
     426             :   {
     427        3744 :     a = centermod(a, xZ);
     428        3744 :     tetpil = avma;
     429        3744 :     if (typ(cx) == t_INT)
     430             :     {
     431          98 :       gel(y,1) = mulii(xZ, cx);
     432          98 :       gel(y,2) = ZC_Z_mul(a, cx);
     433             :     }
     434             :     else
     435             :     {
     436        3646 :       gel(y,1) = gmul(xZ, cx);
     437        3646 :       gel(y,2) = RgC_Rg_mul(a, cx);
     438             :     }
     439             :   }
     440             :   else
     441             :   {
     442      128909 :     tetpil = avma;
     443      128909 :     gel(y,1) = icopy(xZ);
     444      128909 :     gel(y,2) = centermod(a, xZ);
     445             :   }
     446      132652 :   gerepilecoeffssp(av,tetpil,y+1,2); return y;
     447             : }
     448             : 
     449             : /* Given an ideal x, returns [a,alpha] such that a is in Q,
     450             :  * x = a Z_K + alpha Z_K, alpha in K^*
     451             :  * a = 0 or alpha = 0 are possible, but do not try to determine whether
     452             :  * x is principal. */
     453             : GEN
     454      102825 : idealtwoelt(GEN nf, GEN x)
     455             : {
     456             :   pari_sp av;
     457      102825 :   long tx = idealtyp(&x, NULL);
     458      102818 :   nf = checknf(nf);
     459      102818 :   if (tx == id_MAT) return mat_ideal_two_elt(nf,x);
     460        1015 :   if (tx == id_PRIME) return mkvec2copy(gel(x,1), gel(x,2));
     461             :   /* id_PRINCIPAL */
     462        1008 :   av = avma; x = nf_to_scalar_or_basis(nf, x);
     463        1820 :   return gerepilecopy(av, typ(x)==t_COL? mkvec2(gen_0,x):
     464         903 :                                          mkvec2(Q_abs_shallow(x),gen_0));
     465             : }
     466             : 
     467             : /*******************************************************************/
     468             : /*                                                                 */
     469             : /*                         FACTORIZATION                           */
     470             : /*                                                                 */
     471             : /*******************************************************************/
     472             : /* x integral ideal in HNF, Zval = v_p(x \cap Z) > 0; return v_p(Nx) */
     473             : static long
     474     3990734 : idealHNF_norm_pval(GEN x, GEN p, long Zval)
     475             : {
     476     3990734 :   long i, v = Zval, l = lg(x);
     477    31630708 :   for (i = 2; i < l; i++) v += Z_pval(gcoeff(x,i,i), p);
     478     3990750 :   return v;
     479             : }
     480             : 
     481             : /* x integral in HNF, f0 = partial factorization of a multiple of
     482             :  * x[1,1] = x\cap Z */
     483             : GEN
     484      284513 : idealHNF_Z_factor_i(GEN x, GEN f0, GEN *pvN, GEN *pvZ)
     485             : {
     486      284513 :   GEN P, E, vN, vZ, xZ = gcoeff(x,1,1), f = f0? f0: Z_factor(xZ);
     487             :   long i, l;
     488      284530 :   P = gel(f,1); l = lg(P);
     489      284530 :   E = gel(f,2);
     490      284530 :   *pvN = vN = cgetg(l, t_VECSMALL);
     491      284535 :   *pvZ = vZ = cgetg(l, t_VECSMALL);
     492      708504 :   for (i = 1; i < l; i++)
     493             :   {
     494      423969 :     GEN p = gel(P,i);
     495      423969 :     vZ[i] = f0? Z_pval(xZ, p): (long) itou(gel(E,i));
     496      423968 :     vN[i] = idealHNF_norm_pval(x,p, vZ[i]);
     497             :   }
     498      284535 :   return P;
     499             : }
     500             : /* return P, primes dividing Nx and xZ = x\cap Z, set v_p(Nx), v_p(xZ);
     501             :  * x integral in HNF */
     502             : GEN
     503           0 : idealHNF_Z_factor(GEN x, GEN *pvN, GEN *pvZ)
     504           0 : { return idealHNF_Z_factor_i(x, NULL, pvN, pvZ); }
     505             : 
     506             : /* v_P(A)*f(P) <= Nval [e.g. Nval = v_p(Norm A)], Zval = v_p(A \cap Z).
     507             :  * Return v_P(A) */
     508             : static long
     509     4126969 : idealHNF_val(GEN A, GEN P, long Nval, long Zval)
     510             : {
     511     4126969 :   long f = pr_get_f(P), vmax, v, e, i, j, k, l;
     512             :   GEN mul, B, a, y, r, p, pk, cx, vals;
     513             :   pari_sp av;
     514             : 
     515     4126969 :   if (Nval < f) return 0;
     516     4123792 :   p = pr_get_p(P);
     517     4123795 :   e = pr_get_e(P);
     518             :   /* v_P(A) <= max [ e * v_p(A \cap Z), floor[v_p(Nix) / f ] */
     519     4123799 :   vmax = minss(Zval * e, Nval / f);
     520     4123800 :   mul = pr_get_tau(P);
     521     4123795 :   l = lg(mul);
     522     4123795 :   B = cgetg(l,t_MAT);
     523             :   /* B[1] not needed: v_pr(A[1]) = v_pr(A \cap Z) is known already */
     524     4130481 :   gel(B,1) = gen_0; /* dummy */
     525    23187080 :   for (j = 2; j < l; j++)
     526             :   {
     527    20862373 :     GEN x = gel(A,j);
     528    20862373 :     gel(B,j) = y = cgetg(l, t_COL);
     529   227660787 :     for (i = 1; i < l; i++)
     530             :     { /* compute a = (x.t0)_i, A in HNF ==> x[j+1..l-1] = 0 */
     531   208604188 :       a = mulii(gel(x,1), gcoeff(mul,i,1));
     532  1461079491 :       for (k = 2; k <= j; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     533             :       /* p | a ? */
     534   208597198 :       gel(y,i) = dvmdii(a,p,&r); if (signe(r)) return 0;
     535             :     }
     536             :   }
     537     2324707 :   vals = cgetg(l, t_VECSMALL);
     538             :   /* vals[1] not needed */
     539    16537643 :   for (j = 2; j < l; j++)
     540             :   {
     541    14212783 :     gel(B,j) = Q_primitive_part(gel(B,j), &cx);
     542    14212793 :     vals[j] = cx? 1 + e * Q_pval(cx, p): 1;
     543             :   }
     544     2324860 :   pk = powiu(p, ceildivuu(vmax, e));
     545     2324823 :   av = avma; y = cgetg(l,t_COL);
     546             :   /* can compute mod p^ceil((vmax-v)/e) */
     547     3445808 :   for (v = 1; v < vmax; v++)
     548             :   { /* we know v_pr(Bj) >= v for all j */
     549     1150955 :     if (e == 1 || (vmax - v) % e == 0) pk = diviiexact(pk, p);
     550     7441656 :     for (j = 2; j < l; j++)
     551             :     {
     552     6320682 :       GEN x = gel(B,j); if (v < vals[j]) continue;
     553    43379335 :       for (i = 1; i < l; i++)
     554             :       {
     555    39285892 :         pari_sp av2 = avma;
     556    39285892 :         a = mulii(gel(x,1), gcoeff(mul,i,1));
     557   517366703 :         for (k = 2; k < l; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     558             :         /* a = (x.t_0)_i; p | a ? */
     559    39285559 :         a = dvmdii(a,p,&r); if (signe(r)) return v;
     560    39255515 :         if (lgefint(a) > lgefint(pk)) a = remii(a, pk);
     561    39255504 :         gel(y,i) = gerepileuptoint(av2, a);
     562             :       }
     563     4093443 :       gel(B,j) = y; y = x;
     564     4093443 :       if (gc_needed(av,3))
     565             :       {
     566           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"idealval");
     567           0 :         gerepileall(av,3, &y,&B,&pk);
     568             :       }
     569             :     }
     570             :   }
     571     2294853 :   return v;
     572             : }
     573             : /* true nf, x != 0 integral ideal in HNF, cx t_INT or NULL,
     574             :  * FA integer factorization matrix or NULL. Return partial factorization of
     575             :  * cx * x above primes in FA (complete factorization if !FA)*/
     576             : static GEN
     577      284513 : idealHNF_factor_i(GEN nf, GEN x, GEN cx, GEN FA)
     578             : {
     579      284513 :   const long N = lg(x)-1;
     580             :   long i, j, k, l, v;
     581      284513 :   GEN vN, vZ, vP, vE, vp = idealHNF_Z_factor_i(x, FA, &vN,&vZ);
     582             : 
     583      284534 :   l = lg(vp);
     584      284534 :   i = cx? expi(cx)+1: 1;
     585      284542 :   vP = cgetg((l+i-2)*N+1, t_COL);
     586      284535 :   vE = cgetg((l+i-2)*N+1, t_COL);
     587      708500 :   for (i = k = 1; i < l; i++)
     588             :   {
     589      423969 :     GEN L, p = gel(vp,i);
     590      423969 :     long Nval = vN[i], Zval = vZ[i], vc = cx? Z_pvalrem(cx,p,&cx): 0;
     591      423963 :     if (vc)
     592             :     {
     593       43256 :       L = idealprimedec(nf,p);
     594       43256 :       if (is_pm1(cx)) cx = NULL;
     595             :     }
     596             :     else
     597      380707 :       L = idealprimedec_limit_f(nf,p,Nval);
     598      984144 :     for (j = 1; Nval && j < lg(L); j++) /* !Nval => only cx contributes */
     599             :     {
     600      560180 :       GEN P = gel(L,j);
     601      560180 :       pari_sp av = avma;
     602      560180 :       v = idealHNF_val(x, P, Nval, Zval);
     603      560150 :       set_avma(av);
     604      560152 :       Nval -= v*pr_get_f(P);
     605      560153 :       v += vc * pr_get_e(P); if (!v) continue;
     606      471221 :       gel(vP,k) = P;
     607      471221 :       gel(vE,k) = utoipos(v); k++;
     608             :     }
     609      466662 :     if (vc) for (; j<lg(L); j++)
     610             :     {
     611       42707 :       GEN P = gel(L,j);
     612       42707 :       gel(vP,k) = P;
     613       42707 :       gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     614             :     }
     615             :   }
     616      284531 :   if (cx && !FA)
     617             :   { /* complete factorization */
     618       73505 :     GEN f = Z_factor(cx), cP = gel(f,1), cE = gel(f,2);
     619       73506 :     long lc = lg(cP);
     620      159856 :     for (i=1; i<lc; i++)
     621             :     {
     622       86351 :       GEN p = gel(cP,i), L = idealprimedec(nf,p);
     623       86350 :       long vc = itos(gel(cE,i));
     624      189087 :       for (j=1; j<lg(L); j++)
     625             :       {
     626      102737 :         GEN P = gel(L,j);
     627      102737 :         gel(vP,k) = P;
     628      102737 :         gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     629             :       }
     630             :     }
     631             :   }
     632      284531 :   setlg(vP, k);
     633      284531 :   setlg(vE, k); return mkmat2(vP, vE);
     634             : }
     635             : /* true nf, x integral ideal */
     636             : static GEN
     637      239069 : idealHNF_factor(GEN nf, GEN x, ulong lim)
     638             : {
     639      239069 :   GEN cx, F = NULL;
     640      239069 :   if (lim)
     641             :   {
     642             :     GEN P, E;
     643             :     long i;
     644             :     /* strict useless because of prime table */
     645          70 :     F = absZ_factor_limit(gcoeff(x,1,1), lim);
     646          70 :     P = gel(F,1);
     647          70 :     E = gel(F,2);
     648             :     /* filter out entries > lim */
     649         119 :     for (i = lg(P)-1; i; i--)
     650         119 :       if (cmpiu(gel(P,i), lim) <= 0) break;
     651          70 :     setlg(P, i+1);
     652          70 :     setlg(E, i+1);
     653             :   }
     654      239069 :   x = Q_primitive_part(x, &cx);
     655      239046 :   return idealHNF_factor_i(nf, x, cx, F);
     656             : }
     657             : /* c * vector(#L,i,L[i].e), assume results fit in ulong */
     658             : static GEN
     659       27519 : prV_e_muls(GEN L, long c)
     660             : {
     661       27519 :   long j, l = lg(L);
     662       27519 :   GEN z = cgetg(l, t_COL);
     663       56644 :   for (j = 1; j < l; j++) gel(z,j) = stoi(c * pr_get_e(gel(L,j)));
     664       27521 :   return z;
     665             : }
     666             : /* true nf, y in Q */
     667             : static GEN
     668       26870 : Q_nffactor(GEN nf, GEN y, ulong lim)
     669             : {
     670             :   GEN f, P, E;
     671             :   long l, i;
     672       26870 :   if (typ(y) == t_INT)
     673             :   {
     674       26842 :     if (!signe(y)) pari_err_DOMAIN("idealfactor", "ideal", "=",gen_0,y);
     675       26828 :     if (is_pm1(y)) return trivial_fact();
     676             :   }
     677       17140 :   y = Q_abs_shallow(y);
     678       17134 :   if (!lim) f = Q_factor(y);
     679             :   else
     680             :   {
     681         154 :     f = Q_factor_limit(y, lim);
     682         154 :     P = gel(f,1);
     683         154 :     E = gel(f,2);
     684         224 :     for (i = lg(P)-1; i > 0; i--)
     685         203 :       if (abscmpiu(gel(P,i), lim) < 0) break;
     686         154 :     setlg(P,i+1); setlg(E,i+1);
     687             :   }
     688       17147 :   P = gel(f,1); l = lg(P); if (l == 1) return f;
     689       17126 :   E = gel(f,2);
     690       44669 :   for (i = 1; i < l; i++)
     691             :   {
     692       27537 :     gel(P,i) = idealprimedec(nf, gel(P,i));
     693       27523 :     gel(E,i) = prV_e_muls(gel(P,i), itos(gel(E,i)));
     694             :   }
     695       17132 :   P = shallowconcat1(P); gel(f,1) = P; settyp(P, t_COL);
     696       17140 :   E = shallowconcat1(E); gel(f,2) = E; return f;
     697             : }
     698             : 
     699             : GEN
     700       25424 : idealfactor_partial(GEN nf, GEN x, GEN L)
     701             : {
     702       25424 :   pari_sp av = avma;
     703             :   long i, j, l;
     704             :   GEN P, E;
     705       25424 :   if (!L) return idealfactor(nf, x);
     706       24584 :   if (typ(L) == t_INT) return idealfactor_limit(nf, x, itou(L));
     707       24556 :   l = lg(L); if (l == 1) return trivial_fact();
     708       23772 :   P = cgetg(l, t_VEC);
     709       89467 :   for (i = 1; i < l; i++)
     710             :   {
     711       65695 :     GEN p = gel(L,i);
     712       65695 :     gel(P,i) = typ(p) == t_INT? idealprimedec(nf, p): mkvec(p);
     713             :   }
     714       23772 :   P = shallowconcat1(P); settyp(P, t_COL);
     715       23772 :   P = gen_sort_uniq(P, (void*)&cmp_prime_ideal, &cmp_nodata);
     716       23772 :   E = cgetg_copy(P, &l);
     717      113974 :   for (i = j = 1; i < l; i++)
     718             :   {
     719       90202 :     long v = idealval(nf, x, gel(P,i));
     720       90202 :     if (v) { gel(P,j) = gel(P,i); gel(E,j) = stoi(v); j++; }
     721             :   }
     722       23772 :   setlg(P,j);
     723       23772 :   setlg(E,j); return gerepilecopy(av, mkmat2(P, E));
     724             : }
     725             : GEN
     726      266049 : idealfactor_limit(GEN nf, GEN x, ulong lim)
     727             : {
     728      266049 :   pari_sp av = avma;
     729             :   GEN fa, y;
     730      266049 :   long tx = idealtyp(&x, NULL);
     731             : 
     732      266048 :   if (tx == id_PRIME)
     733             :   {
     734         119 :     if (lim && abscmpiu(pr_get_p(x), lim) >= 0) return trivial_fact();
     735         112 :     retmkmat2(mkcolcopy(x), mkcol(gen_1));
     736             :   }
     737      265929 :   nf = checknf(nf);
     738      265922 :   if (tx == id_PRINCIPAL)
     739             :   {
     740       28123 :     y = nf_to_scalar_or_basis(nf, x);
     741       28123 :     if (typ(y) != t_COL) return gerepilecopy(av, Q_nffactor(nf, y, lim));
     742             :   }
     743      239052 :   y = idealnumden(nf, x);
     744      239056 :   fa = idealHNF_factor(nf, gel(y,1), lim);
     745      239048 :   if (!isint1(gel(y,2)))
     746          14 :     fa = famat_div_shallow(fa, idealHNF_factor(nf, gel(y,2), lim));
     747      239048 :   fa = gerepilecopy(av, fa);
     748      239057 :   return sort_factor(fa, (void*)&cmp_prime_ideal, &cmp_nodata);
     749             : }
     750             : GEN
     751      265669 : idealfactor(GEN nf, GEN x) { return idealfactor_limit(nf, x, 0); }
     752             : GEN
     753         182 : gpidealfactor(GEN nf, GEN x, GEN lim)
     754             : {
     755         182 :   ulong L = 0;
     756         182 :   if (lim)
     757             :   {
     758          70 :     if (typ(lim) != t_INT || signe(lim) < 0) pari_err_FLAG("idealfactor");
     759          70 :     L = itou(lim);
     760             :   }
     761         182 :   return idealfactor_limit(nf, x, L);
     762             : }
     763             : 
     764             : static GEN
     765        7257 : ramified_root(GEN nf, GEN R, GEN A, long n)
     766             : {
     767        7257 :   GEN v, P = gel(idealfactor(nf, R), 1);
     768        7257 :   long i, l = lg(P);
     769        7257 :   v = cgetg(l, t_VECSMALL);
     770        7901 :   for (i = 1; i < l; i++)
     771             :   {
     772         651 :     long w = idealval(nf, A, gel(P,i));
     773         651 :     if (w % n) return NULL;
     774         644 :     v[i] = w / n;
     775             :   }
     776        7250 :   return idealfactorback(nf, P, v, 0);
     777             : }
     778             : static int
     779           7 : ramified_root_simple(GEN nf, long n, GEN P, GEN v)
     780             : {
     781           7 :   long i, l = lg(v);
     782          21 :   for (i = 1; i < l; i++)
     783             :   {
     784          14 :     long w = v[i] % n;
     785          14 :     if (w)
     786             :     {
     787           7 :       GEN vpr = idealprimedec(nf, gel(P,i));
     788           7 :       long lpr = lg(vpr), j;
     789          14 :       for (j = 1; j < lpr; j++)
     790             :       {
     791           7 :         long e = pr_get_e(gel(vpr,j));
     792           7 :         if ((e * w) % n) return 0;
     793             :       }
     794             :     }
     795             :   }
     796           7 :   return 1;
     797             : }
     798             : /* true nf, n > 1, A a non-zero integral ideal; check whether A is the n-th
     799             :  * power of an ideal and set *pB to its n-th root if so */
     800             : static long
     801        7264 : idealsqrtn_int(GEN nf, GEN A, long n, GEN *pB)
     802             : {
     803             :   GEN C, root;
     804             :   long i, l;
     805             : 
     806        7264 :   if (typ(A) == t_MAT && ZM_isscalar(A, NULL)) A = gcoeff(A,1,1);
     807        7264 :   if (typ(A) == t_INT) /* > 0 */
     808             :   {
     809        5060 :     GEN P = nf_get_ramified_primes(nf), v, q;
     810        5060 :     l = lg(P); v = cgetg(l, t_VECSMALL);
     811       23966 :     for (i = 1; i < l; i++) v[i] = Z_pvalrem(A, gel(P,i), &A);
     812        5060 :     C = gen_1;
     813        5060 :     if (!isint1(A) && !Z_ispowerall(A, n, pB? &C: NULL)) return 0;
     814        5060 :     if (!pB) return ramified_root_simple(nf, n, P, v);
     815        5053 :     q = factorback2(P, v);
     816        5053 :     root = ramified_root(nf, q, q, n);
     817        5053 :     if (!root) return 0;
     818        5053 :     if (!equali1(C)) root = isint1(root)? C: ZM_Z_mul(root, C);
     819        5053 :     *pB = root; return 1;
     820             :   }
     821             :   /* compute valuations at ramified primes */
     822        2204 :   root = ramified_root(nf, idealadd(nf, nf_get_diff(nf), A), A, n);
     823        2204 :   if (!root) return 0;
     824             :   /* remove ramified primes */
     825        2197 :   if (isint1(root))
     826        1854 :     root = matid(nf_get_degree(nf));
     827             :   else
     828         343 :     A = idealdivexact(nf, A, idealpows(nf,root,n));
     829        2197 :   A = Q_primitive_part(A, &C);
     830        2197 :   if (C)
     831             :   {
     832           7 :     if (!Z_ispowerall(C,n,&C)) return 0;
     833           0 :     if (pB) root = ZM_Z_mul(root, C);
     834             :   }
     835             : 
     836             :   /* compute final n-th root, at most degree(nf)-1 iterations */
     837        2190 :   for (i = 0;; i++)
     838        2064 :   {
     839        4254 :     GEN J, b, a = gcoeff(A,1,1); /* A \cap Z */
     840        4254 :     if (is_pm1(a)) break;
     841        2092 :     if (!Z_ispowerall(a,n,&b)) return 0;
     842        2064 :     J = idealadd(nf, b, A);
     843        2064 :     A = idealdivexact(nf, idealpows(nf,J,n), A);
     844             :     /* div and not divexact here */
     845        2064 :     if (pB) root = odd(i)? idealdiv(nf, root, J): idealmul(nf, root, J);
     846             :   }
     847        2162 :   if (pB) *pB = root;
     848        2162 :   return 1;
     849             : }
     850             : 
     851             : /* A is assumed to be the n-th power of an ideal in nf
     852             :  returns its n-th root. */
     853             : long
     854        3660 : idealispower(GEN nf, GEN A, long n, GEN *pB)
     855             : {
     856        3660 :   pari_sp av = avma;
     857             :   GEN v, N, D;
     858        3660 :   nf = checknf(nf);
     859        3660 :   if (n <= 0) pari_err_DOMAIN("idealispower", "n", "<=", gen_0, stoi(n));
     860        3660 :   if (n == 1) { if (pB) *pB = idealhnf(nf,A); return 1; }
     861        3653 :   v = idealnumden(nf,A);
     862        3653 :   if (gequal0(gel(v,1))) { set_avma(av); if (pB) *pB = cgetg(1,t_MAT); return 1; }
     863        3653 :   if (!idealsqrtn_int(nf, gel(v,1), n, pB? &N: NULL)) return 0;
     864        3611 :   if (!idealsqrtn_int(nf, gel(v,2), n, pB? &D: NULL)) return 0;
     865        3611 :   if (pB) *pB = gerepileupto(av, idealdiv(nf,N,D)); else set_avma(av);
     866        3611 :   return 1;
     867             : }
     868             : 
     869             : /* x t_INT or integral nonzero ideal in HNF */
     870             : static GEN
     871       97230 : idealredmodpower_i(GEN nf, GEN x, ulong k, ulong B)
     872             : {
     873             :   GEN cx, y, U, N, F, Q;
     874       97230 :   if (typ(x) == t_INT)
     875             :   {
     876       51212 :     if (!signe(x) || is_pm1(x)) return gen_1;
     877        1918 :     F = Z_factor_limit(x, B);
     878        1918 :     gel(F,2) = gdiventgs(gel(F,2), k);
     879        1918 :     return ginv(factorback(F));
     880             :   }
     881       46018 :   N = gcoeff(x,1,1); if (is_pm1(N)) return gen_1;
     882       45471 :   F = absZ_factor_limit_strict(N, B, &U);
     883       45471 :   if (U)
     884             :   {
     885         146 :     GEN M = powii(gel(U,1), gel(U,2));
     886         146 :     y = hnfmodid(x, M); /* coprime part to B! */
     887         146 :     if (!idealispower(nf, y, k, &U)) U = NULL;
     888         146 :     x = hnfmodid(x, diviiexact(N, M));
     889             :   }
     890             :   /* x = B-smooth part of initial x */
     891       45471 :   x = Q_primitive_part(x, &cx);
     892       45469 :   F = idealHNF_factor_i(nf, x, cx, F);
     893       45471 :   gel(F,2) = gdiventgs(gel(F,2), k);
     894       45471 :   Q = idealfactorback(nf, gel(F,1), gel(F,2), 0);
     895       45471 :   if (U) Q = idealmul(nf,Q,U);
     896       45471 :   if (typ(Q) == t_INT) return Q;
     897       10865 :   y = idealred_elt(nf, idealHNF_inv_Z(nf, Q));
     898       10865 :   return gdiv(y, gcoeff(Q,1,1));
     899             : }
     900             : GEN
     901       48622 : idealredmodpower(GEN nf, GEN x, ulong n, ulong B)
     902             : {
     903       48622 :   pari_sp av = avma;
     904             :   GEN a, b;
     905       48622 :   nf = checknf(nf);
     906       48621 :   if (!n) pari_err_DOMAIN("idealredmodpower","n", "=", gen_0, gen_0);
     907       48621 :   x = idealnumden(nf, x);
     908       48622 :   a = gel(x,1);
     909       48622 :   if (isintzero(a)) { set_avma(av); return gen_1; }
     910       48615 :   a = idealredmodpower_i(nf, gel(x,1), n, B);
     911       48615 :   b = idealredmodpower_i(nf, gel(x,2), n, B);
     912       48615 :   if (!isint1(b)) a = nf_to_scalar_or_basis(nf, nfdiv(nf, a, b));
     913       48615 :   return gerepilecopy(av, a);
     914             : }
     915             : 
     916             : /* P prime ideal in idealprimedec format. Return valuation(A) at P */
     917             : long
     918     9470525 : idealval(GEN nf, GEN A, GEN P)
     919             : {
     920     9470525 :   pari_sp av = avma;
     921             :   GEN p, cA;
     922     9470525 :   long vcA, v, Zval, tx = idealtyp(&A, NULL);
     923             : 
     924     9470550 :   if (tx == id_PRINCIPAL) return nfval(nf,A,P);
     925     9371506 :   checkprid(P);
     926     9371727 :   if (tx == id_PRIME) return pr_equal(P, A)? 1: 0;
     927             :   /* id_MAT */
     928     9371699 :   nf = checknf(nf);
     929     9371911 :   A = Q_primitive_part(A, &cA);
     930     9372032 :   p = pr_get_p(P);
     931     9372047 :   vcA = cA? Q_pval(cA,p): 0;
     932     9372053 :   if (pr_is_inert(P)) return gc_long(av,vcA);
     933     8973661 :   Zval = Z_pval(gcoeff(A,1,1), p);
     934     8973675 :   if (!Zval) v = 0;
     935             :   else
     936             :   {
     937     3566670 :     long Nval = idealHNF_norm_pval(A, p, Zval);
     938     3566786 :     v = idealHNF_val(A, P, Nval, Zval);
     939             :   }
     940     8973581 :   return gc_long(av, vcA? v + vcA*pr_get_e(P): v);
     941             : }
     942             : GEN
     943        7119 : gpidealval(GEN nf, GEN ix, GEN P)
     944             : {
     945        7119 :   long v = idealval(nf,ix,P);
     946        7105 :   return v == LONG_MAX? mkoo(): stoi(v);
     947             : }
     948             : 
     949             : /* gcd and generalized Bezout */
     950             : 
     951             : GEN
     952      108265 : idealadd(GEN nf, GEN x, GEN y)
     953             : {
     954      108265 :   pari_sp av = avma;
     955             :   long tx, ty;
     956             :   GEN z, a, dx, dy, dz;
     957             : 
     958      108265 :   tx = idealtyp(&x, NULL);
     959      108265 :   ty = idealtyp(&y, NULL); nf = checknf(nf);
     960      108265 :   if (tx != id_MAT) x = idealhnf_shallow(nf,x);
     961      108265 :   if (ty != id_MAT) y = idealhnf_shallow(nf,y);
     962      108265 :   if (lg(x) == 1) return gerepilecopy(av,y);
     963      107271 :   if (lg(y) == 1) return gerepilecopy(av,x); /* check for 0 ideal */
     964      106816 :   dx = Q_denom(x);
     965      106816 :   dy = Q_denom(y); dz = lcmii(dx,dy);
     966      106816 :   if (is_pm1(dz)) dz = NULL; else {
     967       15834 :     x = Q_muli_to_int(x, dz);
     968       15834 :     y = Q_muli_to_int(y, dz);
     969             :   }
     970      106816 :   a = gcdii(gcoeff(x,1,1), gcoeff(y,1,1));
     971      106816 :   if (is_pm1(a))
     972             :   {
     973       37918 :     long N = lg(x)-1;
     974       37918 :     if (!dz) { set_avma(av); return matid(N); }
     975        3906 :     return gerepileupto(av, scalarmat(ginv(dz), N));
     976             :   }
     977       68898 :   z = ZM_hnfmodid(shallowconcat(x,y), a);
     978       68898 :   if (dz) z = RgM_Rg_div(z,dz);
     979       68898 :   return gerepileupto(av,z);
     980             : }
     981             : 
     982             : static GEN
     983          28 : trivial_merge(GEN x)
     984          28 : { return (lg(x) == 1 || !is_pm1(gcoeff(x,1,1)))? NULL: gen_1; }
     985             : /* true nf */
     986             : static GEN
     987      731763 : _idealaddtoone(GEN nf, GEN x, GEN y, long red)
     988             : {
     989             :   GEN a;
     990      731763 :   long tx = idealtyp(&x, NULL);
     991      731731 :   long ty = idealtyp(&y, NULL);
     992             :   long ea;
     993      731723 :   if (tx != id_MAT) x = idealhnf_shallow(nf, x);
     994      731756 :   if (ty != id_MAT) y = idealhnf_shallow(nf, y);
     995      731756 :   if (lg(x) == 1)
     996          14 :     a = trivial_merge(y);
     997      731742 :   else if (lg(y) == 1)
     998          14 :     a = trivial_merge(x);
     999             :   else
    1000      731728 :     a = hnfmerge_get_1(x, y);
    1001      731722 :   if (!a) pari_err_COPRIME("idealaddtoone",x,y);
    1002      731707 :   if (red && (ea = gexpo(a)) > 10)
    1003             :   {
    1004        5152 :     GEN b = (typ(a) == t_COL)? a: scalarcol_shallow(a, nf_get_degree(nf));
    1005        5152 :     b = ZC_reducemodlll(b, idealHNF_mul(nf,x,y));
    1006        5152 :     if (gexpo(b) < ea) a = b;
    1007             :   }
    1008      731707 :   return a;
    1009             : }
    1010             : /* true nf */
    1011             : GEN
    1012       19768 : idealaddtoone_i(GEN nf, GEN x, GEN y)
    1013       19768 : { return _idealaddtoone(nf, x, y, 1); }
    1014             : /* true nf */
    1015             : GEN
    1016      711994 : idealaddtoone_raw(GEN nf, GEN x, GEN y)
    1017      711994 : { return _idealaddtoone(nf, x, y, 0); }
    1018             : 
    1019             : GEN
    1020          98 : idealaddtoone(GEN nf, GEN x, GEN y)
    1021             : {
    1022          98 :   GEN z = cgetg(3,t_VEC), a;
    1023          98 :   pari_sp av = avma;
    1024          98 :   nf = checknf(nf);
    1025          98 :   a = gerepileupto(av, idealaddtoone_i(nf,x,y));
    1026          84 :   gel(z,1) = a;
    1027          84 :   gel(z,2) = typ(a) == t_COL? Z_ZC_sub(gen_1,a): subui(1,a);
    1028          84 :   return z;
    1029             : }
    1030             : 
    1031             : /* assume elements of list are integral ideals */
    1032             : GEN
    1033          35 : idealaddmultoone(GEN nf, GEN list)
    1034             : {
    1035          35 :   pari_sp av = avma;
    1036          35 :   long N, i, l, nz, tx = typ(list);
    1037             :   GEN H, U, perm, L;
    1038             : 
    1039          35 :   nf = checknf(nf); N = nf_get_degree(nf);
    1040          35 :   if (!is_vec_t(tx)) pari_err_TYPE("idealaddmultoone",list);
    1041          35 :   l = lg(list);
    1042          35 :   L = cgetg(l, t_VEC);
    1043          35 :   if (l == 1)
    1044           0 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
    1045          35 :   nz = 0; /* number of nonzero ideals in L */
    1046          98 :   for (i=1; i<l; i++)
    1047             :   {
    1048          70 :     GEN I = gel(list,i);
    1049          70 :     if (typ(I) != t_MAT) I = idealhnf_shallow(nf,I);
    1050          70 :     if (lg(I) != 1)
    1051             :     {
    1052          42 :       nz++; RgM_check_ZM(I,"idealaddmultoone");
    1053          35 :       if (lgcols(I) != N+1) pari_err_TYPE("idealaddmultoone [not an ideal]", I);
    1054             :     }
    1055          63 :     gel(L,i) = I;
    1056             :   }
    1057          28 :   H = ZM_hnfperm(shallowconcat1(L), &U, &perm);
    1058          28 :   if (lg(H) == 1 || !equali1(gcoeff(H,1,1)))
    1059           7 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
    1060          49 :   for (i=1; i<=N; i++)
    1061          49 :     if (perm[i] == 1) break;
    1062          21 :   U = gel(U,(nz-1)*N + i); /* (L[1]|...|L[nz]) U = 1 */
    1063          21 :   nz = 0;
    1064          63 :   for (i=1; i<l; i++)
    1065             :   {
    1066          42 :     GEN c = gel(L,i);
    1067          42 :     if (lg(c) == 1)
    1068          14 :       c = gen_0;
    1069             :     else {
    1070          28 :       c = ZM_ZC_mul(c, vecslice(U, nz*N + 1, (nz+1)*N));
    1071          28 :       nz++;
    1072             :     }
    1073          42 :     gel(L,i) = c;
    1074             :   }
    1075          21 :   return gerepilecopy(av, L);
    1076             : }
    1077             : 
    1078             : /* multiplication */
    1079             : 
    1080             : /* x integral ideal (without archimedean component) in HNF form
    1081             :  * y = [a,alpha] corresponds to the integral ideal aZ_K+alpha Z_K, a in Z,
    1082             :  * alpha a ZV or a ZM (multiplication table). Multiply them */
    1083             : static GEN
    1084      799752 : idealHNF_mul_two(GEN nf, GEN x, GEN y)
    1085             : {
    1086      799752 :   GEN m, a = gel(y,1), alpha = gel(y,2);
    1087             :   long i, N;
    1088             : 
    1089      799752 :   if (typ(alpha) != t_MAT)
    1090             :   {
    1091      462015 :     alpha = zk_scalar_or_multable(nf, alpha);
    1092      461980 :     if (typ(alpha) == t_INT) /* e.g. y inert ? 0 should not (but may) occur */
    1093       14048 :       return signe(a)? ZM_Z_mul(x, gcdii(a, alpha)): cgetg(1,t_MAT);
    1094             :   }
    1095      785669 :   N = lg(x)-1; m = cgetg((N<<1)+1,t_MAT);
    1096     3271940 :   for (i=1; i<=N; i++) gel(m,i)   = ZM_ZC_mul(alpha,gel(x,i));
    1097     3270645 :   for (i=1; i<=N; i++) gel(m,i+N) = ZC_Z_mul(gel(x,i), a);
    1098      784879 :   return ZM_hnfmodid(m, mulii(a, gcoeff(x,1,1)));
    1099             : }
    1100             : 
    1101             : /* Assume x and y are integral in HNF form [NOT extended]. Not memory clean.
    1102             :  * HACK: ideal in y can be of the form [a,b], a in Z, b in Z_K */
    1103             : GEN
    1104      388686 : idealHNF_mul(GEN nf, GEN x, GEN y)
    1105             : {
    1106             :   GEN z;
    1107      388686 :   if (typ(y) == t_VEC)
    1108      208754 :     z = idealHNF_mul_two(nf,x,y);
    1109             :   else
    1110             :   { /* reduce one ideal to two-elt form. The smallest */
    1111      179932 :     GEN xZ = gcoeff(x,1,1), yZ = gcoeff(y,1,1);
    1112      179932 :     if (cmpii(xZ, yZ) < 0)
    1113             :     {
    1114       39146 :       if (is_pm1(xZ)) return gcopy(y);
    1115       23070 :       z = idealHNF_mul_two(nf, y, mat_ideal_two_elt(nf,x));
    1116             :     }
    1117             :     else
    1118             :     {
    1119      140786 :       if (is_pm1(yZ)) return gcopy(x);
    1120       35992 :       z = idealHNF_mul_two(nf, x, mat_ideal_two_elt(nf,y));
    1121             :     }
    1122             :   }
    1123      267816 :   return z;
    1124             : }
    1125             : 
    1126             : /* operations on elements in factored form */
    1127             : 
    1128             : GEN
    1129      199529 : famat_mul_shallow(GEN f, GEN g)
    1130             : {
    1131      199529 :   if (typ(f) != t_MAT) f = to_famat_shallow(f,gen_1);
    1132      199529 :   if (typ(g) != t_MAT) g = to_famat_shallow(g,gen_1);
    1133      199529 :   if (lgcols(f) == 1) return g;
    1134      147727 :   if (lgcols(g) == 1) return f;
    1135      145829 :   return mkmat2(shallowconcat(gel(f,1), gel(g,1)),
    1136      145830 :                 shallowconcat(gel(f,2), gel(g,2)));
    1137             : }
    1138             : GEN
    1139       88316 : famat_mulpow_shallow(GEN f, GEN g, GEN e)
    1140             : {
    1141       88316 :   if (!signe(e)) return f;
    1142       54610 :   return famat_mul_shallow(f, famat_pow_shallow(g, e));
    1143             : }
    1144             : 
    1145             : GEN
    1146      117981 : famat_mulpows_shallow(GEN f, GEN g, long e)
    1147             : {
    1148      117981 :   if (e==0) return f;
    1149       94444 :   return famat_mul_shallow(f, famat_pows_shallow(g, e));
    1150             : }
    1151             : 
    1152             : GEN
    1153       10304 : famat_div_shallow(GEN f, GEN g)
    1154       10304 : { return famat_mul_shallow(f, famat_inv_shallow(g)); }
    1155             : 
    1156             : GEN
    1157      376251 : Z_to_famat(GEN x)
    1158             : {
    1159             :   long k;
    1160      376251 :   if (equali1(x)) return trivial_fact();
    1161      192262 :   k = Z_isanypower(x, &x) ;
    1162      192262 :   return to_famat_shallow(x, k? utoi(k): gen_1);
    1163             : }
    1164             : GEN
    1165      197053 : Q_to_famat(GEN x)
    1166             : {
    1167      197053 :   if (typ(x) == t_INT) return Z_to_famat(x);
    1168      179198 :   return famat_div(Z_to_famat(gel(x,1)), Z_to_famat(gel(x,2)));
    1169             : }
    1170             : GEN
    1171           0 : to_famat(GEN x, GEN y) { retmkmat2(mkcolcopy(x), mkcolcopy(y)); }
    1172             : GEN
    1173     2651289 : to_famat_shallow(GEN x, GEN y) { return mkmat2(mkcol(x), mkcol(y)); }
    1174             : 
    1175             : /* concat the single elt x; not gconcat since x may be a t_COL */
    1176             : static GEN
    1177      151946 : append(GEN v, GEN x)
    1178             : {
    1179      151946 :   long i, l = lg(v);
    1180      151946 :   GEN w = cgetg(l+1, typ(v));
    1181      654040 :   for (i=1; i<l; i++) gel(w,i) = gcopy(gel(v,i));
    1182      151946 :   gel(w,i) = gcopy(x); return w;
    1183             : }
    1184             : /* add x^1 to famat f */
    1185             : static GEN
    1186      158599 : famat_add(GEN f, GEN x)
    1187             : {
    1188      158599 :   GEN h = cgetg(3,t_MAT);
    1189      158599 :   if (lgcols(f) == 1)
    1190             :   {
    1191       13182 :     gel(h,1) = mkcolcopy(x);
    1192       13182 :     gel(h,2) = mkcol(gen_1);
    1193             :   }
    1194             :   else
    1195             :   {
    1196      145417 :     gel(h,1) = append(gel(f,1), x);
    1197      145417 :     gel(h,2) = gconcat(gel(f,2), gen_1);
    1198             :   }
    1199      158599 :   return h;
    1200             : }
    1201             : /* add x^-1 to famat f */
    1202             : static GEN
    1203       20846 : famat_sub(GEN f, GEN x)
    1204             : {
    1205       20846 :   GEN h = cgetg(3,t_MAT);
    1206       20846 :   if (lgcols(f) == 1)
    1207             :   {
    1208       14317 :     gel(h,1) = mkcolcopy(x);
    1209       14317 :     gel(h,2) = mkcol(gen_m1);
    1210             :   }
    1211             :   else
    1212             :   {
    1213        6529 :     gel(h,1) = append(gel(f,1), x);
    1214        6529 :     gel(h,2) = gconcat(gel(f,2), gen_m1);
    1215             :   }
    1216       20846 :   return h;
    1217             : }
    1218             : 
    1219             : GEN
    1220      447356 : famat_mul(GEN f, GEN g)
    1221             : {
    1222             :   GEN h;
    1223      447356 :   if (typ(g) != t_MAT) {
    1224       30499 :     if (typ(f) == t_MAT) return famat_add(f, g);
    1225           0 :     h = cgetg(3, t_MAT);
    1226           0 :     gel(h,1) = mkcol2(gcopy(f), gcopy(g));
    1227           0 :     gel(h,2) = mkcol2(gen_1, gen_1);
    1228             :   }
    1229      416857 :   if (typ(f) != t_MAT) return famat_add(g, f);
    1230      288757 :   if (lgcols(f) == 1) return gcopy(g);
    1231      265433 :   if (lgcols(g) == 1) return gcopy(f);
    1232      259441 :   h = cgetg(3,t_MAT);
    1233      259441 :   gel(h,1) = gconcat(gel(f,1), gel(g,1));
    1234      259441 :   gel(h,2) = gconcat(gel(f,2), gel(g,2));
    1235      259441 :   return h;
    1236             : }
    1237             : 
    1238             : GEN
    1239      200051 : famat_div(GEN f, GEN g)
    1240             : {
    1241             :   GEN h;
    1242      200051 :   if (typ(g) != t_MAT) {
    1243       20804 :     if (typ(f) == t_MAT) return famat_sub(f, g);
    1244           0 :     h = cgetg(3, t_MAT);
    1245           0 :     gel(h,1) = mkcol2(gcopy(f), gcopy(g));
    1246           0 :     gel(h,2) = mkcol2(gen_1, gen_m1);
    1247             :   }
    1248      179247 :   if (typ(f) != t_MAT) return famat_sub(g, f);
    1249      179205 :   if (lgcols(f) == 1) return famat_inv(g);
    1250         260 :   if (lgcols(g) == 1) return gcopy(f);
    1251         260 :   h = cgetg(3,t_MAT);
    1252         260 :   gel(h,1) = gconcat(gel(f,1), gel(g,1));
    1253         260 :   gel(h,2) = gconcat(gel(f,2), gneg(gel(g,2)));
    1254         260 :   return h;
    1255             : }
    1256             : 
    1257             : GEN
    1258       22943 : famat_sqr(GEN f)
    1259             : {
    1260             :   GEN h;
    1261       22943 :   if (typ(f) != t_MAT) return to_famat(f,gen_2);
    1262       22943 :   if (lgcols(f) == 1) return gcopy(f);
    1263       13131 :   h = cgetg(3,t_MAT);
    1264       13131 :   gel(h,1) = gcopy(gel(f,1));
    1265       13131 :   gel(h,2) = gmul2n(gel(f,2),1);
    1266       13131 :   return h;
    1267             : }
    1268             : 
    1269             : GEN
    1270       26968 : famat_inv_shallow(GEN f)
    1271             : {
    1272       26968 :   if (typ(f) != t_MAT) return to_famat_shallow(f,gen_m1);
    1273       10444 :   if (lgcols(f) == 1) return f;
    1274       10444 :   return mkmat2(gel(f,1), ZC_neg(gel(f,2)));
    1275             : }
    1276             : GEN
    1277      199313 : famat_inv(GEN f)
    1278             : {
    1279      199313 :   if (typ(f) != t_MAT) return to_famat(f,gen_m1);
    1280      199313 :   if (lgcols(f) == 1) return gcopy(f);
    1281      180807 :   retmkmat2(gcopy(gel(f,1)), ZC_neg(gel(f,2)));
    1282             : }
    1283             : GEN
    1284       60642 : famat_pow(GEN f, GEN n)
    1285             : {
    1286       60642 :   if (typ(f) != t_MAT) return to_famat(f,n);
    1287       60642 :   if (lgcols(f) == 1) return gcopy(f);
    1288       60642 :   retmkmat2(gcopy(gel(f,1)), ZC_Z_mul(gel(f,2),n));
    1289             : }
    1290             : GEN
    1291       62037 : famat_pow_shallow(GEN f, GEN n)
    1292             : {
    1293       62037 :   if (is_pm1(n)) return signe(n) > 0? f: famat_inv_shallow(f);
    1294       35681 :   if (typ(f) != t_MAT) return to_famat_shallow(f,n);
    1295        7983 :   if (lgcols(f) == 1) return f;
    1296        6063 :   return mkmat2(gel(f,1), ZC_Z_mul(gel(f,2),n));
    1297             : }
    1298             : 
    1299             : GEN
    1300      122838 : famat_pows_shallow(GEN f, long n)
    1301             : {
    1302      122838 :   if (n==1) return f;
    1303       34703 :   if (n==-1) return famat_inv_shallow(f);
    1304       34696 :   if (typ(f) != t_MAT) return to_famat_shallow(f, stoi(n));
    1305       26474 :   if (lgcols(f) == 1) return f;
    1306       26474 :   return mkmat2(gel(f,1), ZC_z_mul(gel(f,2),n));
    1307             : }
    1308             : 
    1309             : GEN
    1310           0 : famat_Z_gcd(GEN M, GEN n)
    1311             : {
    1312           0 :   pari_sp av=avma;
    1313           0 :   long i, j, l=lgcols(M);
    1314           0 :   GEN F=cgetg(3,t_MAT);
    1315           0 :   gel(F,1)=cgetg(l,t_COL);
    1316           0 :   gel(F,2)=cgetg(l,t_COL);
    1317           0 :   for (i=1, j=1; i<l; i++)
    1318             :   {
    1319           0 :     GEN p = gcoeff(M,i,1);
    1320           0 :     GEN e = gminsg(Z_pval(n,p),gcoeff(M,i,2));
    1321           0 :     if (signe(e))
    1322             :     {
    1323           0 :       gcoeff(F,j,1)=p;
    1324           0 :       gcoeff(F,j,2)=e;
    1325           0 :       j++;
    1326             :     }
    1327             :   }
    1328           0 :   setlg(gel(F,1),j); setlg(gel(F,2),j);
    1329           0 :   return gerepilecopy(av,F);
    1330             : }
    1331             : 
    1332             : /* x assumed to be a t_MATs (factorization matrix), or compatible with
    1333             :  * the element_* functions. */
    1334             : static GEN
    1335       33891 : ext_sqr(GEN nf, GEN x)
    1336       33891 : { return (typ(x)==t_MAT)? famat_sqr(x): nfsqr(nf, x); }
    1337             : static GEN
    1338       60757 : ext_mul(GEN nf, GEN x, GEN y)
    1339       60757 : { return (typ(x)==t_MAT)? famat_mul(x,y): nfmul(nf, x, y); }
    1340             : static GEN
    1341       20368 : ext_inv(GEN nf, GEN x)
    1342       20368 : { return (typ(x)==t_MAT)? famat_inv(x): nfinv(nf, x); }
    1343             : static GEN
    1344           0 : ext_pow(GEN nf, GEN x, GEN n)
    1345           0 : { return (typ(x)==t_MAT)? famat_pow(x,n): nfpow(nf, x, n); }
    1346             : 
    1347             : GEN
    1348           0 : famat_to_nf(GEN nf, GEN f)
    1349             : {
    1350             :   GEN t, x, e;
    1351             :   long i;
    1352           0 :   if (lgcols(f) == 1) return gen_1;
    1353           0 :   x = gel(f,1);
    1354           0 :   e = gel(f,2);
    1355           0 :   t = nfpow(nf, gel(x,1), gel(e,1));
    1356           0 :   for (i=lg(x)-1; i>1; i--)
    1357           0 :     t = nfmul(nf, t, nfpow(nf, gel(x,i), gel(e,i)));
    1358           0 :   return t;
    1359             : }
    1360             : 
    1361             : GEN
    1362           0 : famat_idealfactor(GEN nf, GEN x)
    1363             : {
    1364             :   long i, l;
    1365           0 :   GEN g = gel(x,1), e = gel(x,2), h = cgetg_copy(g, &l);
    1366           0 :   for (i = 1; i < l; i++) gel(h,i) = idealfactor(nf, gel(g,i));
    1367           0 :   h = famat_reduce(famatV_factorback(h,e));
    1368           0 :   return sort_factor(h, (void*)&cmp_prime_ideal, &cmp_nodata);
    1369             : }
    1370             : 
    1371             : GEN
    1372      295257 : famat_reduce(GEN fa)
    1373             : {
    1374             :   GEN E, G, L, g, e;
    1375             :   long i, k, l;
    1376             : 
    1377      295257 :   if (typ(fa) != t_MAT || lgcols(fa) == 1) return fa;
    1378      284538 :   g = gel(fa,1); l = lg(g);
    1379      284538 :   e = gel(fa,2);
    1380      284538 :   L = gen_indexsort(g, (void*)&cmp_universal, &cmp_nodata);
    1381      284537 :   G = cgetg(l, t_COL);
    1382      284537 :   E = cgetg(l, t_COL);
    1383             :   /* merge */
    1384     2293163 :   for (k=i=1; i<l; i++,k++)
    1385             :   {
    1386     2008626 :     gel(G,k) = gel(g,L[i]);
    1387     2008626 :     gel(E,k) = gel(e,L[i]);
    1388     2008626 :     if (k > 1 && gidentical(gel(G,k), gel(G,k-1)))
    1389             :     {
    1390      838439 :       gel(E,k-1) = addii(gel(E,k), gel(E,k-1));
    1391      838437 :       k--;
    1392             :     }
    1393             :   }
    1394             :   /* kill 0 exponents */
    1395      284537 :   l = k;
    1396     1454727 :   for (k=i=1; i<l; i++)
    1397     1170190 :     if (!gequal0(gel(E,i)))
    1398             :     {
    1399     1146638 :       gel(G,k) = gel(G,i);
    1400     1146638 :       gel(E,k) = gel(E,i); k++;
    1401             :     }
    1402      284537 :   setlg(G, k);
    1403      284538 :   setlg(E, k); return mkmat2(G,E);
    1404             : }
    1405             : GEN
    1406          63 : matreduce(GEN f)
    1407          63 : { pari_sp av = avma;
    1408          63 :   switch(typ(f))
    1409             :   {
    1410          21 :     case t_VEC: case t_COL:
    1411             :     {
    1412          21 :       GEN e; f = vec_reduce(f, &e); settyp(f, t_COL);
    1413          21 :       return gerepilecopy(av, mkmat2(f, zc_to_ZC(e)));
    1414             :     }
    1415          35 :     case t_MAT:
    1416          35 :       if (lg(f) == 3) break;
    1417             :     default:
    1418          14 :       pari_err_TYPE("matreduce", f);
    1419             :   }
    1420          28 :   if (typ(gel(f,1)) == t_VECSMALL)
    1421           0 :     f = famatsmall_reduce(f);
    1422             :   else
    1423             :   {
    1424          28 :     if (!RgV_is_ZV(gel(f,2))) pari_err_TYPE("matreduce",f);
    1425          21 :     f = famat_reduce(f);
    1426             :   }
    1427          21 :   return gerepilecopy(av, f);
    1428             : }
    1429             : 
    1430             : GEN
    1431      173579 : famatsmall_reduce(GEN fa)
    1432             : {
    1433             :   GEN E, G, L, g, e;
    1434             :   long i, k, l;
    1435      173579 :   if (lgcols(fa) == 1) return fa;
    1436      173579 :   g = gel(fa,1); l = lg(g);
    1437      173579 :   e = gel(fa,2);
    1438      173579 :   L = vecsmall_indexsort(g);
    1439      173579 :   G = cgetg(l, t_VECSMALL);
    1440      173579 :   E = cgetg(l, t_VECSMALL);
    1441             :   /* merge */
    1442      478012 :   for (k=i=1; i<l; i++,k++)
    1443             :   {
    1444      304433 :     G[k] = g[L[i]];
    1445      304433 :     E[k] = e[L[i]];
    1446      304433 :     if (k > 1 && G[k] == G[k-1])
    1447             :     {
    1448        7966 :       E[k-1] += E[k];
    1449        7966 :       k--;
    1450             :     }
    1451             :   }
    1452             :   /* kill 0 exponents */
    1453      173579 :   l = k;
    1454      470046 :   for (k=i=1; i<l; i++)
    1455      296467 :     if (E[i])
    1456             :     {
    1457      292810 :       G[k] = G[i];
    1458      292810 :       E[k] = E[i]; k++;
    1459             :     }
    1460      173579 :   setlg(G, k);
    1461      173579 :   setlg(E, k); return mkmat2(G,E);
    1462             : }
    1463             : 
    1464             : GEN
    1465       53617 : famat_remove_trivial(GEN fa)
    1466             : {
    1467       53617 :   GEN P, E, p = gel(fa,1), e = gel(fa,2);
    1468       53617 :   long j, k, l = lg(p);
    1469       53617 :   P = cgetg(l, t_COL);
    1470       53617 :   E = cgetg(l, t_COL);
    1471     1762573 :   for (j = k = 1; j < l; j++)
    1472     1708956 :     if (signe(gel(e,j))) { gel(P,k) = gel(p,j); gel(E,k++) = gel(e,j); }
    1473       53617 :   setlg(P, k); setlg(E, k); return mkmat2(P,E);
    1474             : }
    1475             : 
    1476             : GEN
    1477       13759 : famatV_factorback(GEN v, GEN e)
    1478             : {
    1479       13759 :   long i, l = lg(e);
    1480             :   GEN V;
    1481       13759 :   if (l == 1) return trivial_fact();
    1482       13374 :   V = signe(gel(e,1))? famat_pow_shallow(gel(v,1), gel(e,1)): trivial_fact();
    1483       56649 :   for (i = 2; i < l; i++) V = famat_mulpow_shallow(V, gel(v,i), gel(e,i));
    1484       13374 :   return V;
    1485             : }
    1486             : 
    1487             : GEN
    1488       46529 : famatV_zv_factorback(GEN v, GEN e)
    1489             : {
    1490       46529 :   long i, l = lg(e);
    1491             :   GEN V;
    1492       46529 :   if (l == 1) return trivial_fact();
    1493       44142 :   V = uel(e,1)? famat_pows_shallow(gel(v,1), uel(e,1)): trivial_fact();
    1494      138675 :   for (i = 2; i < l; i++) V = famat_mulpows_shallow(V, gel(v,i), uel(e,i));
    1495       44143 :   return V;
    1496             : }
    1497             : 
    1498             : GEN
    1499      568117 : ZM_famat_limit(GEN fa, GEN limit)
    1500             : {
    1501             :   pari_sp av;
    1502             :   GEN E, G, g, e, r;
    1503             :   long i, k, l, n, lG;
    1504             : 
    1505      568117 :   if (lgcols(fa) == 1) return fa;
    1506      568110 :   g = gel(fa,1); l = lg(g);
    1507      568110 :   e = gel(fa,2);
    1508     1137368 :   for(n=0, i=1; i<l; i++)
    1509      569258 :     if (cmpii(gel(g,i),limit)<=0) n++;
    1510      568110 :   lG = n<l-1 ? n+2 : n+1;
    1511      568110 :   G = cgetg(lG, t_COL);
    1512      568111 :   E = cgetg(lG, t_COL);
    1513      568111 :   av = avma;
    1514     1137370 :   for (i=1, k=1, r = gen_1; i<l; i++)
    1515             :   {
    1516      569259 :     if (cmpii(gel(g,i),limit)<=0)
    1517             :     {
    1518      569126 :       gel(G,k) = gel(g,i);
    1519      569126 :       gel(E,k) = gel(e,i);
    1520      569126 :       k++;
    1521         133 :     } else r = mulii(r, powii(gel(g,i), gel(e,i)));
    1522             :   }
    1523      568111 :   if (k<i)
    1524             :   {
    1525         133 :     gel(G, k) = gerepileuptoint(av, r);
    1526         133 :     gel(E, k) = gen_1;
    1527             :   }
    1528      568111 :   return mkmat2(G,E);
    1529             : }
    1530             : 
    1531             : /* assume pr has degree 1 and coprime to Q_denom(x) */
    1532             : static GEN
    1533      122430 : to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1534             : {
    1535      122430 :   GEN d, r, p = modpr_get_p(modpr);
    1536      122430 :   x = nf_to_scalar_or_basis(nf,x);
    1537      122430 :   if (typ(x) != t_COL) return Rg_to_Fp(x,p);
    1538      120792 :   x = Q_remove_denom(x, &d);
    1539      120792 :   r = zk_to_Fq(x, modpr);
    1540      120792 :   if (d) r = Fp_div(r, d, p);
    1541      120792 :   return r;
    1542             : }
    1543             : 
    1544             : /* pr coprime to all denominators occurring in x */
    1545             : static GEN
    1546         693 : famat_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1547             : {
    1548         693 :   GEN p = modpr_get_p(modpr);
    1549         693 :   GEN t = NULL, g = gel(x,1), e = gel(x,2), q = subiu(p,1);
    1550         693 :   long i, l = lg(g);
    1551        4571 :   for (i = 1; i < l; i++)
    1552             :   {
    1553        3878 :     GEN n = modii(gel(e,i), q);
    1554        3878 :     if (signe(n))
    1555             :     {
    1556        3871 :       GEN h = to_Fp_coprime(nf, gel(g,i), modpr);
    1557        3871 :       h = Fp_pow(h, n, p);
    1558        3871 :       t = t? Fp_mul(t, h, p): h;
    1559             :     }
    1560             :   }
    1561         693 :   return t? modii(t, p): gen_1;
    1562             : }
    1563             : 
    1564             : /* cf famat_to_nf_modideal_coprime, modpr attached to prime of degree 1 */
    1565             : GEN
    1566      119252 : nf_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1567             : {
    1568         693 :   return typ(x)==t_MAT? famat_to_Fp_coprime(nf, x, modpr)
    1569      119945 :                       : to_Fp_coprime(nf, x, modpr);
    1570             : }
    1571             : 
    1572             : static long
    1573     4057068 : zk_pvalrem(GEN x, GEN p, GEN *py)
    1574     4057068 : { return (typ(x) == t_INT)? Z_pvalrem(x, p, py): ZV_pvalrem(x, p, py); }
    1575             : /* x a QC or Q. Return a ZC or Z, whose content is coprime to Z. Set v, dx
    1576             :  * such that x = p^v (newx / dx); dx = NULL if 1 */
    1577             : static GEN
    1578     4102887 : nf_remove_denom_p(GEN nf, GEN x, GEN p, GEN *pdx, long *pv)
    1579             : {
    1580             :   long vcx;
    1581             :   GEN dx;
    1582     4102887 :   x = nf_to_scalar_or_basis(nf, x);
    1583     4102894 :   x = Q_remove_denom(x, &dx);
    1584     4102896 :   if (dx)
    1585             :   {
    1586       61230 :     vcx = - Z_pvalrem(dx, p, &dx);
    1587       61231 :     if (!vcx) vcx = zk_pvalrem(x, p, &x);
    1588       61230 :     if (isint1(dx)) dx = NULL;
    1589             :   }
    1590             :   else
    1591             :   {
    1592     4041666 :     vcx = zk_pvalrem(x, p, &x);
    1593     4041671 :     dx = NULL;
    1594             :   }
    1595     4102901 :   *pv = vcx;
    1596     4102901 :   *pdx = dx; return x;
    1597             : }
    1598             : /* x = b^e/p^(e-1) in Z_K; x = 0 mod p/pr^e, (x,pr) = 1. Return NULL
    1599             :  * if p inert (instead of 1) */
    1600             : static GEN
    1601       88855 : p_makecoprime(GEN pr)
    1602             : {
    1603       88855 :   GEN B = pr_get_tau(pr), b;
    1604             :   long i, e;
    1605             : 
    1606       88855 :   if (typ(B) == t_INT) return NULL;
    1607       66098 :   b = gel(B,1); /* B = multiplication table by b */
    1608       66098 :   e = pr_get_e(pr);
    1609       66098 :   if (e == 1) return b;
    1610             :   /* one could also divide (exactly) by p in each iteration */
    1611       41964 :   for (i = 1; i < e; i++) b = ZM_ZC_mul(B, b);
    1612       20468 :   return ZC_Z_divexact(b, powiu(pr_get_p(pr), e-1));
    1613             : }
    1614             : 
    1615             : /* Compute A = prod g[i]^e[i] mod pr^k, assuming (A, pr) = 1.
    1616             :  * Method: modify each g[i] so that it becomes coprime to pr,
    1617             :  * g[i] *= (b/p)^v_pr(g[i]), where b/p = pr^(-1) times something integral
    1618             :  * and prime to p; globally, we multiply by (b/p)^v_pr(A) = 1.
    1619             :  * Optimizations:
    1620             :  * 1) remove all powers of p from contents, and consider extra generator p^vp;
    1621             :  * modified as p * (b/p)^e = b^e / p^(e-1)
    1622             :  * 2) remove denominators, coprime to p, by multiplying by inverse mod prk\cap Z
    1623             :  *
    1624             :  * EX = multiple of exponent of (O_K / pr^k)^* used to reduce the product in
    1625             :  * case the e[i] are large */
    1626             : GEN
    1627     2003497 : famat_makecoprime(GEN nf, GEN g, GEN e, GEN pr, GEN prk, GEN EX)
    1628             : {
    1629     2003497 :   GEN G, E, t, vp = NULL, p = pr_get_p(pr), prkZ = gcoeff(prk, 1,1);
    1630     2003494 :   long i, l = lg(g);
    1631             : 
    1632     2003494 :   G = cgetg(l+1, t_VEC);
    1633     2003519 :   E = cgetg(l+1, t_VEC); /* l+1: room for "modified p" */
    1634     6106391 :   for (i=1; i < l; i++)
    1635             :   {
    1636             :     long vcx;
    1637     4102884 :     GEN dx, x = nf_remove_denom_p(nf, gel(g,i), p, &dx, &vcx);
    1638     4102894 :     if (vcx) /* = v_p(content(g[i])) */
    1639             :     {
    1640      136035 :       GEN a = mulsi(vcx, gel(e,i));
    1641      136044 :       vp = vp? addii(vp, a): a;
    1642             :     }
    1643             :     /* x integral, content coprime to p; dx coprime to p */
    1644     4102904 :     if (typ(x) == t_INT)
    1645             :     { /* x coprime to p, hence to pr */
    1646     1109161 :       x = modii(x, prkZ);
    1647     1109160 :       if (dx) x = Fp_div(x, dx, prkZ);
    1648             :     }
    1649             :     else
    1650             :     {
    1651     2993743 :       (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
    1652     2993707 :       x = ZC_hnfrem(FpC_red(x,prkZ), prk);
    1653     2993711 :       if (dx) x = FpC_Fp_mul(x, Fp_inv(dx,prkZ), prkZ);
    1654             :     }
    1655     4102861 :     gel(G,i) = x;
    1656     4102861 :     gel(E,i) = gel(e,i);
    1657             :   }
    1658             : 
    1659     2003507 :   t = vp? p_makecoprime(pr): NULL;
    1660     2003519 :   if (!t)
    1661             :   { /* no need for extra generator */
    1662     1937497 :     setlg(G,l);
    1663     1937497 :     setlg(E,l);
    1664             :   }
    1665             :   else
    1666             :   {
    1667       66022 :     gel(G,i) = FpC_red(t, prkZ);
    1668       66022 :     gel(E,i) = vp;
    1669             :   }
    1670     2003520 :   return famat_to_nf_modideal_coprime(nf, G, E, prk, EX);
    1671             : }
    1672             : 
    1673             : /* simplified version of famat_makecoprime for X = SUnits[1] */
    1674             : GEN
    1675          98 : sunits_makecoprime(GEN X, GEN pr, GEN prk)
    1676             : {
    1677          98 :   GEN G, p = pr_get_p(pr), prkZ = gcoeff(prk,1,1);
    1678          98 :   long i, l = lg(X);
    1679             : 
    1680          98 :   G = cgetg(l, t_VEC);
    1681        9093 :   for (i = 1; i < l; i++)
    1682             :   {
    1683        8995 :     GEN x = gel(X,i);
    1684        8995 :     if (typ(x) == t_INT) /* a prime */
    1685        1421 :       x = equalii(x,p)? p_makecoprime(pr): modii(x, prkZ);
    1686             :     else
    1687             :     {
    1688        7574 :       (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
    1689        7574 :       x = ZC_hnfrem(FpC_red(x,prkZ), prk);
    1690             :     }
    1691        8995 :     gel(G,i) = x;
    1692             :   }
    1693          98 :   return G;
    1694             : }
    1695             : 
    1696             : /* prod g[i]^e[i] mod bid, assume (g[i], id) = 1 and 1 < lg(g) <= lg(e) */
    1697             : GEN
    1698       20118 : famat_to_nf_moddivisor(GEN nf, GEN g, GEN e, GEN bid)
    1699             : {
    1700       20118 :   GEN t, cyc = bid_get_cyc(bid);
    1701       20118 :   if (lg(cyc) == 1)
    1702           0 :     t = gen_1;
    1703             :   else
    1704       20118 :     t = famat_to_nf_modideal_coprime(nf, g, e, bid_get_ideal(bid),
    1705             :                                      cyc_get_expo(cyc));
    1706       20118 :   return set_sign_mod_divisor(nf, mkmat2(g,e), t, bid_get_sarch(bid));
    1707             : }
    1708             : 
    1709             : GEN
    1710    16007736 : vecmul(GEN x, GEN y)
    1711             : {
    1712    16007736 :   if (is_scalar_t(typ(x))) return gmul(x,y);
    1713     3522808 :   pari_APPLY_same(vecmul(gel(x,i), gel(y,i)))
    1714             : }
    1715             : 
    1716             : GEN
    1717      193473 : vecsqr(GEN x)
    1718             : {
    1719      193473 :   if (is_scalar_t(typ(x))) return gsqr(x);
    1720       54096 :   pari_APPLY_same(vecsqr(gel(x,i)))
    1721             : }
    1722             : 
    1723             : GEN
    1724         770 : vecinv(GEN x)
    1725             : {
    1726         770 :   if (is_scalar_t(typ(x))) return ginv(x);
    1727           0 :   pari_APPLY_same(vecinv(gel(x,i)))
    1728             : }
    1729             : 
    1730             : GEN
    1731           0 : vecpow(GEN x, GEN n)
    1732             : {
    1733           0 :   if (is_scalar_t(typ(x))) return powgi(x,n);
    1734           0 :   pari_APPLY_same(vecpow(gel(x,i), n))
    1735             : }
    1736             : 
    1737             : GEN
    1738         903 : vecdiv(GEN x, GEN y)
    1739             : {
    1740         903 :   if (is_scalar_t(typ(x))) return gdiv(x,y);
    1741         903 :   pari_APPLY_same(vecdiv(gel(x,i), gel(y,i)))
    1742             : }
    1743             : 
    1744             : /* A ideal as a square t_MAT */
    1745             : static GEN
    1746      304682 : idealmulelt(GEN nf, GEN x, GEN A)
    1747             : {
    1748             :   long i, lx;
    1749             :   GEN dx, dA, D;
    1750      304682 :   if (lg(A) == 1) return cgetg(1, t_MAT);
    1751      304682 :   x = nf_to_scalar_or_basis(nf,x);
    1752      304682 :   if (typ(x) != t_COL)
    1753       99296 :     return isintzero(x)? cgetg(1,t_MAT): RgM_Rg_mul(A, Q_abs_shallow(x));
    1754      205386 :   x = Q_remove_denom(x, &dx);
    1755      205386 :   A = Q_remove_denom(A, &dA);
    1756      205386 :   x = zk_multable(nf, x);
    1757      205386 :   D = mulii(zkmultable_capZ(x), gcoeff(A,1,1));
    1758      205386 :   x = zkC_multable_mul(A, x);
    1759      205386 :   settyp(x, t_MAT); lx = lg(x);
    1760             :   /* x may contain scalars (at most 1 since the ideal is nonzero)*/
    1761      782611 :   for (i=1; i<lx; i++)
    1762      591946 :     if (typ(gel(x,i)) == t_INT)
    1763             :     {
    1764       14721 :       if (i > 1) swap(gel(x,1), gel(x,i)); /* help HNF */
    1765       14721 :       gel(x,1) = scalarcol_shallow(gel(x,1), lx-1);
    1766       14721 :       break;
    1767             :     }
    1768      205386 :   x = ZM_hnfmodid(x, D);
    1769      205386 :   dx = mul_denom(dx,dA);
    1770      205386 :   return dx? gdiv(x,dx): x;
    1771             : }
    1772             : 
    1773             : /* nf a true nf, tx <= ty */
    1774             : static GEN
    1775      575314 : idealmul_aux(GEN nf, GEN x, GEN y, long tx, long ty)
    1776             : {
    1777             :   GEN z, cx, cy;
    1778      575314 :   switch(tx)
    1779             :   {
    1780      361671 :     case id_PRINCIPAL:
    1781      361671 :       switch(ty)
    1782             :       {
    1783       56597 :         case id_PRINCIPAL:
    1784       56597 :           return idealhnf_principal(nf, nfmul(nf,x,y));
    1785         392 :         case id_PRIME:
    1786             :         {
    1787         392 :           GEN p = pr_get_p(y), pi = pr_get_gen(y), cx;
    1788         392 :           if (pr_is_inert(y)) return RgM_Rg_mul(idealhnf_principal(nf,x),p);
    1789             : 
    1790         217 :           x = nf_to_scalar_or_basis(nf, x);
    1791         217 :           switch(typ(x))
    1792             :           {
    1793         203 :             case t_INT:
    1794         203 :               if (!signe(x)) return cgetg(1,t_MAT);
    1795         203 :               return ZM_Z_mul(pr_hnf(nf,y), absi_shallow(x));
    1796           7 :             case t_FRAC:
    1797           7 :               return RgM_Rg_mul(pr_hnf(nf,y), Q_abs_shallow(x));
    1798             :           }
    1799             :           /* t_COL */
    1800           7 :           x = Q_primitive_part(x, &cx);
    1801           7 :           x = zk_multable(nf, x);
    1802           7 :           z = shallowconcat(ZM_Z_mul(x,p), ZM_ZC_mul(x,pi));
    1803           7 :           z = ZM_hnfmodid(z, mulii(p, zkmultable_capZ(x)));
    1804           7 :           return cx? ZM_Q_mul(z, cx): z;
    1805             :         }
    1806      304682 :         default: /* id_MAT */
    1807      304682 :           return idealmulelt(nf, x,y);
    1808             :       }
    1809       42621 :     case id_PRIME:
    1810       42621 :       if (ty==id_PRIME)
    1811        4340 :       { y = pr_hnf(nf,y); cy = NULL; }
    1812             :       else
    1813       38281 :         y = Q_primitive_part(y, &cy);
    1814       42620 :       y = idealHNF_mul_two(nf,y,x);
    1815       42621 :       return cy? ZM_Q_mul(y,cy): y;
    1816             : 
    1817      171022 :     default: /* id_MAT */
    1818             :     {
    1819      171022 :       long N = nf_get_degree(nf);
    1820      171022 :       if (lg(x)-1 != N || lg(y)-1 != N) pari_err_DIM("idealmul");
    1821      171008 :       x = Q_primitive_part(x, &cx);
    1822      171008 :       y = Q_primitive_part(y, &cy); cx = mul_content(cx,cy);
    1823      171008 :       y = idealHNF_mul(nf,x,y);
    1824      171008 :       return cx? ZM_Q_mul(y,cx): y;
    1825             :     }
    1826             :   }
    1827             : }
    1828             : 
    1829             : /* output the ideal product x.y */
    1830             : GEN
    1831      575311 : idealmul(GEN nf, GEN x, GEN y)
    1832             : {
    1833             :   pari_sp av;
    1834             :   GEN res, ax, ay, z;
    1835      575311 :   long tx = idealtyp(&x,&ax);
    1836      575311 :   long ty = idealtyp(&y,&ay), f;
    1837      575314 :   if (tx>ty) { swap(ax,ay); swap(x,y); lswap(tx,ty); }
    1838      575314 :   f = (ax||ay); res = f? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1839      575314 :   av = avma;
    1840      575314 :   z = gerepileupto(av, idealmul_aux(checknf(nf), x,y, tx,ty));
    1841      575300 :   if (!f) return z;
    1842       28015 :   if (ax && ay)
    1843       26531 :     ax = ext_mul(nf, ax, ay);
    1844             :   else
    1845        1484 :     ax = gcopy(ax? ax: ay);
    1846       28015 :   gel(res,1) = z; gel(res,2) = ax; return res;
    1847             : }
    1848             : 
    1849             : /* Return x, integral in 2-elt form, such that pr^2 = c * x. cf idealpowprime
    1850             :  * nf = true nf */
    1851             : static GEN
    1852      281925 : idealsqrprime(GEN nf, GEN pr, GEN *pc)
    1853             : {
    1854      281925 :   GEN p = pr_get_p(pr), q, gen;
    1855      281923 :   long e = pr_get_e(pr), f = pr_get_f(pr);
    1856             : 
    1857      281927 :   q = (e == 1)? sqri(p): p;
    1858      281916 :   if (e <= 2 && e * f == nf_get_degree(nf))
    1859             :   { /* pr^e = (p) */
    1860       45269 :     *pc = q;
    1861       45269 :     return mkvec2(gen_1,gen_0);
    1862             :   }
    1863      236646 :   gen = nfsqr(nf, pr_get_gen(pr));
    1864      236651 :   gen = FpC_red(gen, q);
    1865      236639 :   *pc = NULL;
    1866      236639 :   return mkvec2(q, gen);
    1867             : }
    1868             : /* cf idealpow_aux */
    1869             : static GEN
    1870       38560 : idealsqr_aux(GEN nf, GEN x, long tx)
    1871             : {
    1872       38560 :   GEN T = nf_get_pol(nf), m, cx, a, alpha;
    1873       38560 :   long N = degpol(T);
    1874       38560 :   switch(tx)
    1875             :   {
    1876          84 :     case id_PRINCIPAL:
    1877          84 :       return idealhnf_principal(nf, nfsqr(nf,x));
    1878       10773 :     case id_PRIME:
    1879       10773 :       if (pr_is_inert(x)) return scalarmat(sqri(gel(x,1)), N);
    1880       10605 :       x = idealsqrprime(nf, x, &cx);
    1881       10605 :       x = idealhnf_two(nf,x);
    1882       10605 :       return cx? ZM_Z_mul(x, cx): x;
    1883       27703 :     default:
    1884       27703 :       x = Q_primitive_part(x, &cx);
    1885       27703 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    1886       27703 :       alpha = nfsqr(nf,alpha);
    1887       27703 :       m = zk_scalar_or_multable(nf, alpha);
    1888       27703 :       if (typ(m) == t_INT) {
    1889        1635 :         x = gcdii(sqri(a), m);
    1890        1635 :         if (cx) x = gmul(x, gsqr(cx));
    1891        1635 :         x = scalarmat(x, N);
    1892             :       }
    1893             :       else
    1894             :       { /* could use gcdii(sqri(a), zkmultable_capZ(m)), but costly */
    1895       26068 :         x = ZM_hnfmodid(m, sqri(a));
    1896       26068 :         if (cx) cx = gsqr(cx);
    1897       26068 :         if (cx) x = ZM_Q_mul(x, cx);
    1898             :       }
    1899       27703 :       return x;
    1900             :   }
    1901             : }
    1902             : GEN
    1903       38560 : idealsqr(GEN nf, GEN x)
    1904             : {
    1905             :   pari_sp av;
    1906             :   GEN res, ax, z;
    1907       38560 :   long tx = idealtyp(&x,&ax);
    1908       38560 :   res = ax? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1909       38560 :   av = avma;
    1910       38560 :   z = gerepileupto(av, idealsqr_aux(checknf(nf), x, tx));
    1911       38560 :   if (!ax) return z;
    1912       33891 :   gel(res,1) = z;
    1913       33891 :   gel(res,2) = ext_sqr(nf, ax); return res;
    1914             : }
    1915             : 
    1916             : /* norm of an ideal */
    1917             : GEN
    1918       99801 : idealnorm(GEN nf, GEN x)
    1919             : {
    1920             :   pari_sp av;
    1921             :   long tx;
    1922             : 
    1923       99801 :   switch(idealtyp(&x, NULL))
    1924             :   {
    1925         952 :     case id_PRIME: return pr_norm(x);
    1926        9697 :     case id_MAT: return RgM_det_triangular(x);
    1927             :   }
    1928             :   /* id_PRINCIPAL */
    1929       89152 :   nf = checknf(nf); av = avma;
    1930       89152 :   x = nfnorm(nf, x);
    1931       89152 :   tx = typ(x);
    1932       89152 :   if (tx == t_INT) return gerepileuptoint(av, absi(x));
    1933         406 :   if (tx != t_FRAC) pari_err_TYPE("idealnorm",x);
    1934         406 :   return gerepileupto(av, Q_abs(x));
    1935             : }
    1936             : 
    1937             : /* x \cap Z */
    1938             : GEN
    1939        2982 : idealdown(GEN nf, GEN x)
    1940             : {
    1941        2982 :   pari_sp av = avma;
    1942             :   GEN y, c;
    1943        2982 :   switch(idealtyp(&x, NULL))
    1944             :   {
    1945           7 :     case id_PRIME: return icopy(pr_get_p(x));
    1946        2107 :     case id_MAT: return gcopy(gcoeff(x,1,1));
    1947             :   }
    1948             :   /* id_PRINCIPAL */
    1949         868 :   nf = checknf(nf); av = avma;
    1950         868 :   x = nf_to_scalar_or_basis(nf, x);
    1951         868 :   if (is_rational_t(typ(x))) return Q_abs(x);
    1952          14 :   x = Q_primitive_part(x, &c);
    1953          14 :   y = zkmultable_capZ(zk_multable(nf, x));
    1954          14 :   return gerepilecopy(av, mul_content(c, y));
    1955             : }
    1956             : 
    1957             : /* true nf */
    1958             : static GEN
    1959          35 : idealismaximal_int(GEN nf, GEN p)
    1960             : {
    1961             :   GEN L;
    1962          35 :   if (!BPSW_psp(p)) return NULL;
    1963          70 :   if (!dvdii(nf_get_index(nf), p) &&
    1964          49 :       !FpX_is_irred(FpX_red(nf_get_pol(nf),p), p)) return NULL;
    1965          21 :   L = idealprimedec(nf, p);
    1966          21 :   return lg(L) == 2? gel(L,1): NULL;
    1967             : }
    1968             : /* true nf */
    1969             : static GEN
    1970          21 : idealismaximal_mat(GEN nf, GEN x)
    1971             : {
    1972             :   GEN p, c, L;
    1973             :   long i, l, f;
    1974          21 :   x = Q_primitive_part(x, &c);
    1975          21 :   p = gcoeff(x,1,1);
    1976          21 :   if (c)
    1977             :   {
    1978           7 :     if (typ(c) == t_FRAC || !equali1(p)) return NULL;
    1979           7 :     return idealismaximal_int(nf, c);
    1980             :   }
    1981          14 :   if (!BPSW_psp(p)) return NULL;
    1982          14 :   l = lg(x); f = 1;
    1983          35 :   for (i = 2; i < l; i++)
    1984             :   {
    1985          21 :     c = gcoeff(x,i,i);
    1986          21 :     if (equalii(c, p)) f++; else if (!equali1(c)) return NULL;
    1987             :   }
    1988          14 :   L = idealprimedec_limit_f(nf, p, f);
    1989          28 :   for (i = lg(L)-1; i; i--)
    1990             :   {
    1991          28 :     GEN pr = gel(L,i);
    1992          28 :     if (pr_get_f(pr) != f) break;
    1993          28 :     if (idealval(nf, x, pr) == 1) return pr;
    1994             :   }
    1995           0 :   return NULL;
    1996             : }
    1997             : /* true nf */
    1998             : static GEN
    1999          63 : idealismaximal_i(GEN nf, GEN x)
    2000             : {
    2001             :   GEN L, p, pr, c;
    2002             :   long i, l;
    2003          63 :   switch(idealtyp(&x, NULL))
    2004             :   {
    2005           7 :     case id_PRIME: return x;
    2006          21 :     case id_MAT: return idealismaximal_mat(nf, x);
    2007             :   }
    2008             :   /* id_PRINCIPAL */
    2009          35 :   x = nf_to_scalar_or_basis(nf, x);
    2010          35 :   switch(typ(x))
    2011             :   {
    2012          28 :     case t_INT: return idealismaximal_int(nf, absi_shallow(x));
    2013           0 :     case t_FRAC: return NULL;
    2014             :   }
    2015           7 :   x = Q_primitive_part(x, &c);
    2016           7 :   if (c) return NULL;
    2017           7 :   p = zkmultable_capZ(zk_multable(nf, x));
    2018           7 :   L = idealprimedec(nf, p); l = lg(L); pr = NULL;
    2019          21 :   for (i = 1; i < l; i++)
    2020             :   {
    2021          14 :     long v = ZC_nfval(x, gel(L,i));
    2022          14 :     if (v > 1 || (v && pr)) return NULL;
    2023          14 :     pr = gel(L,i);
    2024             :   }
    2025           7 :   return pr;
    2026             : }
    2027             : GEN
    2028          63 : idealismaximal(GEN nf, GEN x)
    2029             : {
    2030          63 :   pari_sp av = avma;
    2031          63 :   x = idealismaximal_i(checknf(nf), x);
    2032          63 :   if (!x) { set_avma(av); return gen_0; }
    2033          49 :   return gerepilecopy(av, x);
    2034             : }
    2035             : 
    2036             : /* I^(-1) = { x \in K, Tr(x D^(-1) I) \in Z }, D different of K/Q
    2037             :  *
    2038             :  * nf[5][6] = pp( D^(-1) ) = pp( HNF( T^(-1) ) ), T = (Tr(wi wj))
    2039             :  * nf[5][7] = same in 2-elt form.
    2040             :  * Assume I integral. Return the integral ideal (I\cap Z) I^(-1) */
    2041             : GEN
    2042      217556 : idealHNF_inv_Z(GEN nf, GEN I)
    2043             : {
    2044      217556 :   GEN J, dual, IZ = gcoeff(I,1,1); /* I \cap Z */
    2045      217556 :   if (isint1(IZ)) return matid(lg(I)-1);
    2046      197697 :   J = idealHNF_mul(nf,I, gmael(nf,5,7));
    2047             :  /* I in HNF, hence easily inverted; multiply by IZ to get integer coeffs
    2048             :   * missing content cancels while solving the linear equation */
    2049      197697 :   dual = shallowtrans( hnf_divscale(J, gmael(nf,5,6), IZ) );
    2050      197697 :   return ZM_hnfmodid(dual, IZ);
    2051             : }
    2052             : /* I HNF with rational coefficients (denominator d). */
    2053             : GEN
    2054       78908 : idealHNF_inv(GEN nf, GEN I)
    2055             : {
    2056       78908 :   GEN J, IQ = gcoeff(I,1,1); /* I \cap Q; d IQ = dI \cap Z */
    2057       78908 :   J = idealHNF_inv_Z(nf, Q_remove_denom(I, NULL)); /* = (dI)^(-1) * (d IQ) */
    2058       78908 :   return equali1(IQ)? J: RgM_Rg_div(J, IQ);
    2059             : }
    2060             : 
    2061             : /* return p * P^(-1)  [integral] */
    2062             : GEN
    2063       39360 : pr_inv_p(GEN pr)
    2064             : {
    2065       39360 :   if (pr_is_inert(pr)) return matid(pr_get_f(pr));
    2066       38576 :   return ZM_hnfmodid(pr_get_tau(pr), pr_get_p(pr));
    2067             : }
    2068             : GEN
    2069       18257 : pr_inv(GEN pr)
    2070             : {
    2071       18257 :   GEN p = pr_get_p(pr);
    2072       18257 :   if (pr_is_inert(pr)) return scalarmat(ginv(p), pr_get_f(pr));
    2073       17865 :   return RgM_Rg_div(ZM_hnfmodid(pr_get_tau(pr),p), p);
    2074             : }
    2075             : 
    2076             : GEN
    2077      149137 : idealinv(GEN nf, GEN x)
    2078             : {
    2079             :   GEN res, ax;
    2080             :   pari_sp av;
    2081      149137 :   long tx = idealtyp(&x,&ax), N;
    2082             : 
    2083      149137 :   res = ax? cgetg(3,t_VEC): NULL;
    2084      149137 :   nf = checknf(nf); av = avma;
    2085      149137 :   N = nf_get_degree(nf);
    2086      149137 :   switch (tx)
    2087             :   {
    2088       71784 :     case id_MAT:
    2089       71784 :       if (lg(x)-1 != N) pari_err_DIM("idealinv");
    2090       71784 :       x = idealHNF_inv(nf,x); break;
    2091       60376 :     case id_PRINCIPAL:
    2092       60376 :       x = nf_to_scalar_or_basis(nf, x);
    2093       60376 :       if (typ(x) != t_COL)
    2094       60327 :         x = idealhnf_principal(nf,ginv(x));
    2095             :       else
    2096             :       { /* nfinv + idealhnf where we already know (x) \cap Z */
    2097             :         GEN c, d;
    2098          49 :         x = Q_remove_denom(x, &c);
    2099          49 :         x = zk_inv(nf, x);
    2100          49 :         x = Q_remove_denom(x, &d); /* true inverse is c/d * x */
    2101          49 :         if (!d) /* x and x^(-1) integral => x a unit */
    2102          14 :           x = c? scalarmat(c, N): matid(N);
    2103             :         else
    2104             :         {
    2105          35 :           c = c? gdiv(c,d): ginv(d);
    2106          35 :           x = zk_multable(nf, x);
    2107          35 :           x = ZM_Q_mul(ZM_hnfmodid(x,d), c);
    2108             :         }
    2109             :       }
    2110       60376 :       break;
    2111       16977 :     case id_PRIME:
    2112       16977 :       x = pr_inv(x); break;
    2113             :   }
    2114      149137 :   x = gerepileupto(av,x); if (!ax) return x;
    2115       20368 :   gel(res,1) = x;
    2116       20368 :   gel(res,2) = ext_inv(nf, ax); return res;
    2117             : }
    2118             : 
    2119             : /* write x = A/B, A,B coprime integral ideals */
    2120             : GEN
    2121      377892 : idealnumden(GEN nf, GEN x)
    2122             : {
    2123      377892 :   pari_sp av = avma;
    2124             :   GEN x0, c, d, A, B, J;
    2125      377892 :   long tx = idealtyp(&x, NULL);
    2126      377890 :   nf = checknf(nf);
    2127      377894 :   switch (tx)
    2128             :   {
    2129           7 :     case id_PRIME:
    2130           7 :       retmkvec2(idealhnf(nf, x), gen_1);
    2131      137521 :     case id_PRINCIPAL:
    2132             :     {
    2133             :       GEN xZ, mx;
    2134      137521 :       x = nf_to_scalar_or_basis(nf, x);
    2135      137521 :       switch(typ(x))
    2136             :       {
    2137       86198 :         case t_INT: return gerepilecopy(av, mkvec2(absi_shallow(x),gen_1));
    2138        2639 :         case t_FRAC:return gerepilecopy(av, mkvec2(absi_shallow(gel(x,1)), gel(x,2)));
    2139             :       }
    2140             :       /* t_COL */
    2141       48684 :       x = Q_remove_denom(x, &d);
    2142       48684 :       if (!d) return gerepilecopy(av, mkvec2(idealhnf_shallow(nf, x), gen_1));
    2143         105 :       mx = zk_multable(nf, x);
    2144         105 :       xZ = zkmultable_capZ(mx);
    2145         105 :       x = ZM_hnfmodid(mx, xZ); /* principal ideal (x) */
    2146         105 :       x0 = mkvec2(xZ, mx); /* same, for fast multiplication */
    2147         105 :       break;
    2148             :     }
    2149      240366 :     default: /* id_MAT */
    2150             :     {
    2151      240366 :       long n = lg(x)-1;
    2152      240366 :       if (n == 0) return mkvec2(gen_0, gen_1);
    2153      240366 :       if (n != nf_get_degree(nf)) pari_err_DIM("idealnumden");
    2154      240366 :       x0 = x = Q_remove_denom(x, &d);
    2155      240364 :       if (!d) return gerepilecopy(av, mkvec2(x, gen_1));
    2156          21 :       break;
    2157             :     }
    2158             :   }
    2159         126 :   J = hnfmodid(x, d); /* = d/B */
    2160         126 :   c = gcoeff(J,1,1); /* (d/B) \cap Z, divides d */
    2161         126 :   B = idealHNF_inv_Z(nf, J); /* (d/B \cap Z) B/d */
    2162         126 :   if (!equalii(c,d)) B = ZM_Z_mul(B, diviiexact(d,c)); /* = B ! */
    2163         126 :   A = idealHNF_mul(nf, B, x0); /* d * (original x) * B = d A */
    2164         126 :   A = ZM_Z_divexact(A, d); /* = A ! */
    2165         126 :   return gerepilecopy(av, mkvec2(A, B));
    2166             : }
    2167             : 
    2168             : /* Return x, integral in 2-elt form, such that pr^n = c * x. Assume n != 0.
    2169             :  * nf = true nf */
    2170             : static GEN
    2171     1104796 : idealpowprime(GEN nf, GEN pr, GEN n, GEN *pc)
    2172             : {
    2173     1104796 :   GEN p = pr_get_p(pr), q, gen;
    2174             : 
    2175     1104784 :   *pc = NULL;
    2176     1104784 :   if (is_pm1(n)) /* n = 1 special cased for efficiency */
    2177             :   {
    2178      596736 :     q = p;
    2179      596736 :     if (typ(pr_get_tau(pr)) == t_INT) /* inert */
    2180             :     {
    2181           0 :       *pc = (signe(n) >= 0)? p: ginv(p);
    2182           0 :       return mkvec2(gen_1,gen_0);
    2183             :     }
    2184      596721 :     if (signe(n) >= 0) gen = pr_get_gen(pr);
    2185             :     else
    2186             :     {
    2187      155558 :       gen = pr_get_tau(pr); /* possibly t_MAT */
    2188      155572 :       *pc = ginv(p);
    2189             :     }
    2190             :   }
    2191      508107 :   else if (equalis(n,2)) return idealsqrprime(nf, pr, pc);
    2192             :   else
    2193             :   {
    2194      236786 :     long e = pr_get_e(pr), f = pr_get_f(pr);
    2195      236792 :     GEN r, m = truedvmdis(n, e, &r);
    2196      236784 :     if (e * f == nf_get_degree(nf))
    2197             :     { /* pr^e = (p) */
    2198       75660 :       if (signe(m)) *pc = powii(p,m);
    2199       75660 :       if (!signe(r)) return mkvec2(gen_1,gen_0);
    2200       36017 :       q = p;
    2201       36017 :       gen = nfpow(nf, pr_get_gen(pr), r);
    2202             :     }
    2203             :     else
    2204             :     {
    2205      161132 :       m = absi_shallow(m);
    2206      161132 :       if (signe(r)) m = addiu(m,1);
    2207      161132 :       q = powii(p,m); /* m = ceil(|n|/e) */
    2208      161132 :       if (signe(n) >= 0) gen = nfpow(nf, pr_get_gen(pr), n);
    2209             :       else
    2210             :       {
    2211       24271 :         gen = pr_get_tau(pr);
    2212       24271 :         if (typ(gen) == t_MAT) gen = gel(gen,1);
    2213       24271 :         n = negi(n);
    2214       24271 :         gen = ZC_Z_divexact(nfpow(nf, gen, n), powii(p, subii(n,m)));
    2215       24269 :         *pc = ginv(q);
    2216             :       }
    2217             :     }
    2218      197152 :     gen = FpC_red(gen, q);
    2219             :   }
    2220      793872 :   return mkvec2(q, gen);
    2221             : }
    2222             : 
    2223             : /* True nf. x * pr^n. Assume x in HNF or scalar (possibly nonintegral) */
    2224             : GEN
    2225      729903 : idealmulpowprime(GEN nf, GEN x, GEN pr, GEN n)
    2226             : {
    2227             :   GEN c, cx, y;
    2228      729903 :   long N = nf_get_degree(nf);
    2229             : 
    2230      729885 :   if (!signe(n)) return typ(x) == t_MAT? x: scalarmat_shallow(x, N);
    2231             : 
    2232             :   /* inert, special cased for efficiency */
    2233      729878 :   if (pr_is_inert(pr))
    2234             :   {
    2235       75017 :     GEN q = powii(pr_get_p(pr), n);
    2236       72958 :     return typ(x) == t_MAT? RgM_Rg_mul(x,q)
    2237      147966 :                           : scalarmat_shallow(gmul(Q_abs(x),q), N);
    2238             :   }
    2239             : 
    2240      654884 :   y = idealpowprime(nf, pr, n, &c);
    2241      654853 :   if (typ(x) == t_MAT)
    2242      651726 :   { x = Q_primitive_part(x, &cx); if (is_pm1(gcoeff(x,1,1))) x = NULL; }
    2243             :   else
    2244        3127 :   { cx = x; x = NULL; }
    2245      654622 :   cx = mul_content(c,cx);
    2246      654636 :   if (x)
    2247      489275 :     x = idealHNF_mul_two(nf,x,y);
    2248             :   else
    2249      165361 :     x = idealhnf_two(nf,y);
    2250      655091 :   if (cx) x = ZM_Q_mul(x,cx);
    2251      654679 :   return x;
    2252             : }
    2253             : GEN
    2254        9997 : idealdivpowprime(GEN nf, GEN x, GEN pr, GEN n)
    2255             : {
    2256        9997 :   return idealmulpowprime(nf,x,pr, negi(n));
    2257             : }
    2258             : 
    2259             : /* nf = true nf */
    2260             : static GEN
    2261      825411 : idealpow_aux(GEN nf, GEN x, long tx, GEN n)
    2262             : {
    2263      825411 :   GEN T = nf_get_pol(nf), m, cx, n1, a, alpha;
    2264      825410 :   long N = degpol(T), s = signe(n);
    2265      825409 :   if (!s) return matid(N);
    2266      810125 :   switch(tx)
    2267             :   {
    2268       75171 :     case id_PRINCIPAL:
    2269       75171 :       return idealhnf_principal(nf, nfpow(nf,x,n));
    2270      541472 :     case id_PRIME:
    2271      541472 :       if (pr_is_inert(x)) return scalarmat(powii(gel(x,1), n), N);
    2272      449864 :       x = idealpowprime(nf, x, n, &cx);
    2273      449847 :       x = idealhnf_two(nf,x);
    2274      449868 :       return cx? ZM_Q_mul(x, cx): x;
    2275      193482 :     default:
    2276      193482 :       if (is_pm1(n)) return (s < 0)? idealinv(nf, x): gcopy(x);
    2277       69145 :       n1 = (s < 0)? negi(n): n;
    2278             : 
    2279       69145 :       x = Q_primitive_part(x, &cx);
    2280       69145 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    2281       69145 :       alpha = nfpow(nf,alpha,n1);
    2282       69145 :       m = zk_scalar_or_multable(nf, alpha);
    2283       69145 :       if (typ(m) == t_INT) {
    2284         553 :         x = gcdii(powii(a,n1), m);
    2285         553 :         if (s<0) x = ginv(x);
    2286         553 :         if (cx) x = gmul(x, powgi(cx,n));
    2287         553 :         x = scalarmat(x, N);
    2288             :       }
    2289             :       else
    2290             :       { /* could use gcdii(powii(a,n1), zkmultable_capZ(m)), but costly */
    2291       68592 :         x = ZM_hnfmodid(m, powii(a,n1));
    2292       68592 :         if (cx) cx = powgi(cx,n);
    2293       68592 :         if (s<0) {
    2294           7 :           GEN xZ = gcoeff(x,1,1);
    2295           7 :           cx = cx ? gdiv(cx, xZ): ginv(xZ);
    2296           7 :           x = idealHNF_inv_Z(nf,x);
    2297             :         }
    2298       68592 :         if (cx) x = ZM_Q_mul(x, cx);
    2299             :       }
    2300       69145 :       return x;
    2301             :   }
    2302             : }
    2303             : 
    2304             : /* raise the ideal x to the power n (in Z) */
    2305             : GEN
    2306      825411 : idealpow(GEN nf, GEN x, GEN n)
    2307             : {
    2308             :   pari_sp av;
    2309             :   long tx;
    2310             :   GEN res, ax;
    2311             : 
    2312      825411 :   if (typ(n) != t_INT) pari_err_TYPE("idealpow",n);
    2313      825411 :   tx = idealtyp(&x,&ax);
    2314      825411 :   res = ax? cgetg(3,t_VEC): NULL;
    2315      825411 :   av = avma;
    2316      825411 :   x = gerepileupto(av, idealpow_aux(checknf(nf), x, tx, n));
    2317      825412 :   if (!ax) return x;
    2318           0 :   gel(res,1) = x;
    2319           0 :   gel(res,2) = ext_pow(nf, ax, n);
    2320           0 :   return res;
    2321             : }
    2322             : 
    2323             : /* Return ideal^e in number field nf. e is a C integer. */
    2324             : GEN
    2325      274575 : idealpows(GEN nf, GEN ideal, long e)
    2326             : {
    2327      274575 :   long court[] = {evaltyp(t_INT) | _evallg(3),0,0};
    2328      274575 :   affsi(e,court); return idealpow(nf,ideal,court);
    2329             : }
    2330             : 
    2331             : static GEN
    2332       28589 : _idealmulred(GEN nf, GEN x, GEN y)
    2333       28589 : { return idealred(nf,idealmul(nf,x,y)); }
    2334             : static GEN
    2335       35676 : _idealsqrred(GEN nf, GEN x)
    2336       35676 : { return idealred(nf,idealsqr(nf,x)); }
    2337             : static GEN
    2338       11382 : _mul(void *data, GEN x, GEN y) { return _idealmulred((GEN)data,x,y); }
    2339             : static GEN
    2340       35676 : _sqr(void *data, GEN x) { return _idealsqrred((GEN)data, x); }
    2341             : 
    2342             : /* compute x^n (x ideal, n integer), reducing along the way */
    2343             : GEN
    2344       80202 : idealpowred(GEN nf, GEN x, GEN n)
    2345             : {
    2346       80202 :   pari_sp av = avma, av2;
    2347             :   long s;
    2348             :   GEN y;
    2349             : 
    2350       80202 :   if (typ(n) != t_INT) pari_err_TYPE("idealpowred",n);
    2351       80201 :   s = signe(n); if (s == 0) return idealpow(nf,x,n);
    2352       80201 :   y = gen_pow_i(x, n, (void*)nf, &_sqr, &_mul);
    2353       80201 :   av2 = avma;
    2354       80201 :   if (s < 0) y = idealinv(nf,y);
    2355       80201 :   if (s < 0 || is_pm1(n)) y = idealred(nf,y);
    2356       80202 :   return avma == av2? gerepilecopy(av,y): gerepileupto(av,y);
    2357             : }
    2358             : 
    2359             : GEN
    2360       17207 : idealmulred(GEN nf, GEN x, GEN y)
    2361             : {
    2362       17207 :   pari_sp av = avma;
    2363       17207 :   return gerepileupto(av, _idealmulred(nf,x,y));
    2364             : }
    2365             : 
    2366             : long
    2367          91 : isideal(GEN nf,GEN x)
    2368             : {
    2369          91 :   long N, i, j, lx, tx = typ(x);
    2370             :   pari_sp av;
    2371             :   GEN T, xZ;
    2372             : 
    2373          91 :   nf = checknf(nf); T = nf_get_pol(nf); lx = lg(x);
    2374          91 :   if (tx==t_VEC && lx==3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
    2375          91 :   switch(tx)
    2376             :   {
    2377          14 :     case t_INT: case t_FRAC: return 1;
    2378           7 :     case t_POL: return varn(x) == varn(T);
    2379           7 :     case t_POLMOD: return RgX_equal_var(T, gel(x,1));
    2380          14 :     case t_VEC: return get_prid(x)? 1 : 0;
    2381          42 :     case t_MAT: break;
    2382           7 :     default: return 0;
    2383             :   }
    2384          42 :   N = degpol(T);
    2385          42 :   if (lx-1 != N) return (lx == 1);
    2386          28 :   if (nbrows(x) != N) return 0;
    2387             : 
    2388          28 :   av = avma; x = Q_primpart(x);
    2389          28 :   if (!ZM_ishnf(x)) return 0;
    2390          14 :   xZ = gcoeff(x,1,1);
    2391          21 :   for (j=2; j<=N; j++)
    2392          14 :     if (!dvdii(xZ, gcoeff(x,j,j))) return gc_long(av,0);
    2393          14 :   for (i=2; i<=N; i++)
    2394          14 :     for (j=2; j<=N; j++)
    2395           7 :        if (! hnf_invimage(x, zk_ei_mul(nf,gel(x,i),j))) return gc_long(av,0);
    2396           7 :   return gc_long(av,1);
    2397             : }
    2398             : 
    2399             : GEN
    2400       39792 : idealdiv(GEN nf, GEN x, GEN y)
    2401             : {
    2402       39792 :   pari_sp av = avma, tetpil;
    2403       39792 :   GEN z = idealinv(nf,y);
    2404       39792 :   tetpil = avma; return gerepile(av,tetpil, idealmul(nf,x,z));
    2405             : }
    2406             : 
    2407             : /* This routine computes the quotient x/y of two ideals in the number field nf.
    2408             :  * It assumes that the quotient is an integral ideal.  The idea is to find an
    2409             :  * ideal z dividing y such that gcd(Nx/Nz, Nz) = 1.  Then
    2410             :  *
    2411             :  *   x + (Nx/Nz)    x
    2412             :  *   ----------- = ---
    2413             :  *   y + (Ny/Nz)    y
    2414             :  *
    2415             :  * Proof: we can assume x and y are integral. Let p be any prime ideal
    2416             :  *
    2417             :  * If p | Nz, then it divides neither Nx/Nz nor Ny/Nz (since Nx/Nz is the
    2418             :  * product of the integers N(x/y) and N(y/z)).  Both the numerator and the
    2419             :  * denominator on the left will be coprime to p.  So will x/y, since x/y is
    2420             :  * assumed integral and its norm N(x/y) is coprime to p.
    2421             :  *
    2422             :  * If instead p does not divide Nz, then v_p (Nx/Nz) = v_p (Nx) >= v_p(x).
    2423             :  * Hence v_p (x + Nx/Nz) = v_p(x).  Likewise for the denominators.  QED.
    2424             :  *
    2425             :  *                Peter Montgomery.  July, 1994. */
    2426             : static void
    2427           7 : err_divexact(GEN x, GEN y)
    2428           7 : { pari_err_DOMAIN("idealdivexact","denominator(x/y)", "!=",
    2429           0 :                   gen_1,mkvec2(x,y)); }
    2430             : GEN
    2431        4404 : idealdivexact(GEN nf, GEN x0, GEN y0)
    2432             : {
    2433        4404 :   pari_sp av = avma;
    2434             :   GEN x, y, xZ, yZ, Nx, Ny, Nz, cy, q, r;
    2435             : 
    2436        4404 :   nf = checknf(nf);
    2437        4404 :   x = idealhnf_shallow(nf, x0);
    2438        4404 :   y = idealhnf_shallow(nf, y0);
    2439        4404 :   if (lg(y) == 1) pari_err_INV("idealdivexact", y0);
    2440        4397 :   if (lg(x) == 1) { set_avma(av); return cgetg(1, t_MAT); } /* numerator is zero */
    2441        4397 :   y = Q_primitive_part(y, &cy);
    2442        4397 :   if (cy) x = RgM_Rg_div(x,cy);
    2443        4397 :   xZ = gcoeff(x,1,1); if (typ(xZ) != t_INT) err_divexact(x,y);
    2444        4390 :   yZ = gcoeff(y,1,1); if (isint1(yZ)) return gerepilecopy(av, x);
    2445        2661 :   Nx = idealnorm(nf,x);
    2446        2661 :   Ny = idealnorm(nf,y);
    2447        2661 :   if (typ(Nx) != t_INT) err_divexact(x,y);
    2448        2661 :   q = dvmdii(Nx,Ny, &r);
    2449        2661 :   if (signe(r)) err_divexact(x,y);
    2450        2661 :   if (is_pm1(q)) { set_avma(av); return matid(nf_get_degree(nf)); }
    2451             :   /* Find a norm Nz | Ny such that gcd(Nx/Nz, Nz) = 1 */
    2452         485 :   for (Nz = Ny;;) /* q = Nx/Nz */
    2453         465 :   {
    2454         950 :     GEN p1 = gcdii(Nz, q);
    2455         950 :     if (is_pm1(p1)) break;
    2456         465 :     Nz = diviiexact(Nz,p1);
    2457         465 :     q = mulii(q,p1);
    2458             :   }
    2459         485 :   xZ = gcoeff(x,1,1); q = gcdii(q, xZ);
    2460         485 :   if (!equalii(xZ,q))
    2461             :   { /* Replace x/y  by  x+(Nx/Nz) / y+(Ny/Nz) */
    2462         335 :     x = ZM_hnfmodid(x, q);
    2463             :     /* y reduced to unit ideal ? */
    2464         335 :     if (Nz == Ny) return gerepileupto(av, x);
    2465             : 
    2466         118 :     yZ = gcoeff(y,1,1); q = gcdii(diviiexact(Ny,Nz), yZ);
    2467         118 :     y = ZM_hnfmodid(y, q);
    2468             :   }
    2469         268 :   yZ = gcoeff(y,1,1);
    2470         268 :   y = idealHNF_mul(nf,x, idealHNF_inv_Z(nf,y));
    2471         268 :   return gerepileupto(av, ZM_Z_divexact(y, yZ));
    2472             : }
    2473             : 
    2474             : GEN
    2475          21 : idealintersect(GEN nf, GEN x, GEN y)
    2476             : {
    2477          21 :   pari_sp av = avma;
    2478             :   long lz, lx, i;
    2479             :   GEN z, dx, dy, xZ, yZ;;
    2480             : 
    2481          21 :   nf = checknf(nf);
    2482          21 :   x = idealhnf_shallow(nf,x);
    2483          21 :   y = idealhnf_shallow(nf,y);
    2484          21 :   if (lg(x) == 1 || lg(y) == 1) { set_avma(av); return cgetg(1,t_MAT); }
    2485          14 :   x = Q_remove_denom(x, &dx);
    2486          14 :   y = Q_remove_denom(y, &dy);
    2487          14 :   if (dx) y = ZM_Z_mul(y, dx);
    2488          14 :   if (dy) x = ZM_Z_mul(x, dy);
    2489          14 :   xZ = gcoeff(x,1,1);
    2490          14 :   yZ = gcoeff(y,1,1);
    2491          14 :   dx = mul_denom(dx,dy);
    2492          14 :   z = ZM_lll(shallowconcat(x,y), 0.99, LLL_KER); lz = lg(z);
    2493          14 :   lx = lg(x);
    2494          63 :   for (i=1; i<lz; i++) setlg(z[i], lx);
    2495          14 :   z = ZM_hnfmodid(ZM_mul(x,z), lcmii(xZ, yZ));
    2496          14 :   if (dx) z = RgM_Rg_div(z,dx);
    2497          14 :   return gerepileupto(av,z);
    2498             : }
    2499             : 
    2500             : /*******************************************************************/
    2501             : /*                                                                 */
    2502             : /*                      T2-IDEAL REDUCTION                         */
    2503             : /*                                                                 */
    2504             : /*******************************************************************/
    2505             : 
    2506             : static GEN
    2507          21 : chk_vdir(GEN nf, GEN vdir)
    2508             : {
    2509          21 :   long i, l = lg(vdir);
    2510             :   GEN v;
    2511          21 :   if (l != lg(nf_get_roots(nf))) pari_err_DIM("idealred");
    2512          14 :   switch(typ(vdir))
    2513             :   {
    2514           0 :     case t_VECSMALL: return vdir;
    2515          14 :     case t_VEC: break;
    2516           0 :     default: pari_err_TYPE("idealred",vdir);
    2517             :   }
    2518          14 :   v = cgetg(l, t_VECSMALL);
    2519          56 :   for (i = 1; i < l; i++) v[i] = itos(gceil(gel(vdir,i)));
    2520          14 :   return v;
    2521             : }
    2522             : 
    2523             : static void
    2524       12625 : twistG(GEN G, long r1, long i, long v)
    2525             : {
    2526       12625 :   long j, lG = lg(G);
    2527       12625 :   if (i <= r1) {
    2528       37233 :     for (j=1; j<lG; j++) gcoeff(G,i,j) = gmul2n(gcoeff(G,i,j), v);
    2529             :   } else {
    2530         578 :     long k = (i<<1) - r1;
    2531        4402 :     for (j=1; j<lG; j++)
    2532             :     {
    2533        3824 :       gcoeff(G,k-1,j) = gmul2n(gcoeff(G,k-1,j), v);
    2534        3824 :       gcoeff(G,k  ,j) = gmul2n(gcoeff(G,k  ,j), v);
    2535             :     }
    2536             :   }
    2537       12625 : }
    2538             : 
    2539             : GEN
    2540      138605 : nf_get_Gtwist(GEN nf, GEN vdir)
    2541             : {
    2542             :   long i, l, v, r1;
    2543             :   GEN G;
    2544             : 
    2545      138605 :   if (!vdir) return nf_get_roundG(nf);
    2546          21 :   if (typ(vdir) == t_MAT)
    2547             :   {
    2548           0 :     long N = nf_get_degree(nf);
    2549           0 :     if (lg(vdir) != N+1 || lgcols(vdir) != N+1) pari_err_DIM("idealred");
    2550           0 :     return vdir;
    2551             :   }
    2552          21 :   vdir = chk_vdir(nf, vdir);
    2553          14 :   G = RgM_shallowcopy(nf_get_G(nf));
    2554          14 :   r1 = nf_get_r1(nf);
    2555          14 :   l = lg(vdir);
    2556          56 :   for (i=1; i<l; i++)
    2557             :   {
    2558          42 :     v = vdir[i]; if (!v) continue;
    2559          42 :     twistG(G, r1, i, v);
    2560             :   }
    2561          14 :   return RM_round_maxrank(G);
    2562             : }
    2563             : GEN
    2564       12583 : nf_get_Gtwist1(GEN nf, long i)
    2565             : {
    2566       12583 :   GEN G = RgM_shallowcopy( nf_get_G(nf) );
    2567       12583 :   long r1 = nf_get_r1(nf);
    2568       12583 :   twistG(G, r1, i, 10);
    2569       12583 :   return RM_round_maxrank(G);
    2570             : }
    2571             : 
    2572             : GEN
    2573       96327 : RM_round_maxrank(GEN G0)
    2574             : {
    2575       96327 :   long e, r = lg(G0)-1;
    2576       96327 :   pari_sp av = avma;
    2577       96327 :   for (e = 4; ; e <<= 1, set_avma(av))
    2578           0 :   {
    2579       96327 :     GEN G = gmul2n(G0, e), H = ground(G);
    2580       96326 :     if (ZM_rank(H) == r) return H; /* maximal rank ? */
    2581             :   }
    2582             : }
    2583             : 
    2584             : GEN
    2585      138598 : idealred0(GEN nf, GEN I, GEN vdir)
    2586             : {
    2587      138598 :   pari_sp av = avma;
    2588      138598 :   GEN G, aI, IZ, J, y, my, dyi, yi, c1 = NULL;
    2589             :   long N;
    2590             : 
    2591      138598 :   nf = checknf(nf);
    2592      138598 :   N = nf_get_degree(nf);
    2593             :   /* put first for sanity checks, unused when I obviously principal */
    2594      138598 :   G = nf_get_Gtwist(nf, vdir);
    2595      138591 :   switch (idealtyp(&I,&aI))
    2596             :   {
    2597       37195 :     case id_PRIME:
    2598       37195 :       if (pr_is_inert(I)) {
    2599         655 :         if (!aI) { set_avma(av); return matid(N); }
    2600         655 :         c1 = gel(I,1); I = matid(N);
    2601         655 :         goto END;
    2602             :       }
    2603       36540 :       IZ = pr_get_p(I);
    2604       36540 :       J = pr_inv_p(I);
    2605       36541 :       I = idealhnf_two(nf,I);
    2606       36542 :       break;
    2607      101367 :     case id_MAT:
    2608      101367 :       if (lg(I)-1 != N) pari_err_DIM("idealred");
    2609      101360 :       I = Q_primitive_part(I, &c1);
    2610      101360 :       IZ = gcoeff(I,1,1);
    2611      101360 :       if (is_pm1(IZ))
    2612             :       {
    2613        8831 :         if (!aI) { set_avma(av); return matid(N); }
    2614        8747 :         goto END;
    2615             :       }
    2616       92529 :       J = idealHNF_inv_Z(nf, I);
    2617       92529 :       break;
    2618          21 :     default: /* id_PRINCIPAL, silly case */
    2619          21 :       if (gequal0(I)) I = cgetg(1,t_MAT); else { c1 = I; I = matid(N); }
    2620          21 :       if (!aI) return I;
    2621          14 :       goto END;
    2622             :   }
    2623             :   /* now I integral, HNF; and J = (I\cap Z) I^(-1), integral */
    2624      129071 :   y = idealpseudomin(J, G); /* small elt in (I\cap Z)I^(-1), integral */
    2625      129067 :   if (ZV_isscalar(y))
    2626             :   { /* already reduced */
    2627       71153 :     if (!aI) return gerepilecopy(av, I);
    2628       67974 :     goto END;
    2629             :   }
    2630             : 
    2631       57915 :   my = zk_multable(nf, y);
    2632       57916 :   I = ZM_Z_divexact(ZM_mul(my, I), IZ); /* y I / (I\cap Z), integral */
    2633       57913 :   c1 = mul_content(c1, IZ);
    2634       57913 :   if (equali1(c1)) c1 = NULL; /* can be simplified with IZ */
    2635       57913 :   yi = ZM_gauss(my, col_ei(N,1)); /* y^-1 */
    2636       57917 :   dyi = Q_denom(yi); /* generates (y) \cap Z */
    2637       57917 :   I = hnfmodid(I, dyi);
    2638       57917 :   if (!aI) return gerepileupto(av, I);
    2639       55944 :   if (typ(aI) == t_MAT)
    2640             :   {
    2641       39207 :     GEN nyi = Q_muli_to_int(yi, dyi);
    2642       39207 :     if (gexpo(nyi) >= gexpo(y))
    2643       20762 :       aI = famat_div(aI, y); /* yi "larger" than y, keep the latter */
    2644             :     else
    2645             :     { /* use yi */
    2646       18445 :       aI = famat_mul(aI, nyi);
    2647       18445 :       c1 = div_content(c1, dyi);
    2648             :     }
    2649       39207 :     if (c1) { aI = famat_mul(aI, Q_to_famat(c1)); c1 = NULL; }
    2650             :   }
    2651             :   else
    2652       16737 :     c1 = c1? RgC_Rg_mul(yi, c1): yi;
    2653      133333 : END:
    2654      133333 :   if (c1) aI = ext_mul(nf, aI,c1);
    2655      133333 :   return gerepilecopy(av, mkvec2(I, aI));
    2656             : }
    2657             : 
    2658             : /* I integral ZM (not HNF), G ZM, rounded Cholesky form of a weighted
    2659             :  * T2 matrix. Reduce I wrt G */
    2660             : GEN
    2661     1284117 : idealpseudored(GEN I, GEN G)
    2662     1284117 : { return ZM_mul(I, ZM_lll(ZM_mul(G, I), 0.99, LLL_IM)); }
    2663             : 
    2664             : /* Same I, G; m in I with T2(m) small */
    2665             : GEN
    2666      139964 : idealpseudomin(GEN I, GEN G)
    2667             : {
    2668      139964 :   GEN u = ZM_lll(ZM_mul(G, I), 0.99, LLL_IM);
    2669      139964 :   return ZM_ZC_mul(I, gel(u,1));
    2670             : }
    2671             : /* Same I,G; irrational m in I with T2(m) small */
    2672             : GEN
    2673           0 : idealpseudomin_nonscalar(GEN I, GEN G)
    2674             : {
    2675           0 :   GEN u = ZM_lll(ZM_mul(G, I), 0.99, LLL_IM);
    2676           0 :   GEN m = ZM_ZC_mul(I, gel(u,1));
    2677           0 :   if (ZV_isscalar(m) && lg(u) > 2) m = ZM_ZC_mul(I, gel(u,2));
    2678           0 :   return m;
    2679             : }
    2680             : /* Same I,G; t_VEC of irrational m in I with T2(m) small */
    2681             : GEN
    2682     1204388 : idealpseudominvec(GEN I, GEN G)
    2683             : {
    2684     1204388 :   long i, j, k, n = lg(I)-1;
    2685     1204388 :   GEN x, L, b = idealpseudored(I, G);
    2686     1204391 :   L = cgetg(1 + (n*(n+1))/2, t_VEC);
    2687     4256946 :   for (i = k = 1; i <= n; i++)
    2688             :   {
    2689     3052551 :     x = gel(b,i);
    2690     3052551 :     if (!ZV_isscalar(x)) gel(L,k++) = x;
    2691             :   }
    2692     3052554 :   for (i = 2; i <= n; i++)
    2693             :   {
    2694     1848160 :     long J = minss(i, 4);
    2695     4590745 :     for (j = 1; j < J; j++)
    2696             :     {
    2697     2742586 :       x = ZC_add(gel(b,i),gel(b,j));
    2698     2742585 :       if (!ZV_isscalar(x)) gel(L,k++) = x;
    2699             :     }
    2700             :   }
    2701     1204394 :   setlg(L,k); return L;
    2702             : }
    2703             : 
    2704             : GEN
    2705       10886 : idealred_elt(GEN nf, GEN I)
    2706             : {
    2707       10886 :   pari_sp av = avma;
    2708       10886 :   GEN u = idealpseudomin(I, nf_get_roundG(nf));
    2709       10886 :   return gerepileupto(av, u);
    2710             : }
    2711             : 
    2712             : GEN
    2713           7 : idealmin(GEN nf, GEN x, GEN vdir)
    2714             : {
    2715           7 :   pari_sp av = avma;
    2716             :   GEN y, dx;
    2717           7 :   nf = checknf(nf);
    2718           7 :   switch( idealtyp(&x, NULL) )
    2719             :   {
    2720           0 :     case id_PRINCIPAL: return gcopy(x);
    2721           0 :     case id_PRIME: x = pr_hnf(nf,x); break;
    2722           7 :     case id_MAT: if (lg(x) == 1) return gen_0;
    2723             :   }
    2724           7 :   x = Q_remove_denom(x, &dx);
    2725           7 :   y = idealpseudomin(x, nf_get_Gtwist(nf,vdir));
    2726           7 :   if (dx) y = RgC_Rg_div(y, dx);
    2727           7 :   return gerepileupto(av, y);
    2728             : }
    2729             : 
    2730             : /*******************************************************************/
    2731             : /*                                                                 */
    2732             : /*                   APPROXIMATION THEOREM                         */
    2733             : /*                                                                 */
    2734             : /*******************************************************************/
    2735             : /* a = ppi(a,b) ppo(a,b), where ppi regroups primes common to a and b
    2736             :  * and ppo(a,b) = Z_ppo(a,b) */
    2737             : /* return gcd(a,b),ppi(a,b),ppo(a,b) */
    2738             : GEN
    2739      512533 : Z_ppio(GEN a, GEN b)
    2740             : {
    2741      512533 :   GEN x, y, d = gcdii(a,b);
    2742      512533 :   if (is_pm1(d)) return mkvec3(gen_1, gen_1, a);
    2743      395122 :   x = d; y = diviiexact(a,d);
    2744             :   for(;;)
    2745       67837 :   {
    2746      462959 :     GEN g = gcdii(x,y);
    2747      462959 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2748       67837 :     x = mulii(x,g); y = diviiexact(y,g);
    2749             :   }
    2750             : }
    2751             : /* a = ppg(a,b)pple(a,b), where ppg regroups primes such that v(a) > v(b)
    2752             :  * and pple all others */
    2753             : /* return gcd(a,b),ppg(a,b),pple(a,b) */
    2754             : GEN
    2755           0 : Z_ppgle(GEN a, GEN b)
    2756             : {
    2757           0 :   GEN x, y, g, d = gcdii(a,b);
    2758           0 :   if (equalii(a, d)) return mkvec3(a, gen_1, a);
    2759           0 :   x = diviiexact(a,d); y = d;
    2760             :   for(;;)
    2761             :   {
    2762           0 :     g = gcdii(x,y);
    2763           0 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2764           0 :     x = mulii(x,g); y = diviiexact(y,g);
    2765             :   }
    2766             : }
    2767             : static void
    2768           0 : Z_dcba_rec(GEN L, GEN a, GEN b)
    2769             : {
    2770             :   GEN x, r, v, g, h, c, c0;
    2771             :   long n;
    2772           0 :   if (is_pm1(b)) {
    2773           0 :     if (!is_pm1(a)) vectrunc_append(L, a);
    2774           0 :     return;
    2775             :   }
    2776           0 :   v = Z_ppio(a,b);
    2777           0 :   a = gel(v,2);
    2778           0 :   r = gel(v,3);
    2779           0 :   if (!is_pm1(r)) vectrunc_append(L, r);
    2780           0 :   v = Z_ppgle(a,b);
    2781           0 :   g = gel(v,1);
    2782           0 :   h = gel(v,2);
    2783           0 :   x = c0 = gel(v,3);
    2784           0 :   for (n = 1; !is_pm1(h); n++)
    2785             :   {
    2786             :     GEN d, y;
    2787             :     long i;
    2788           0 :     v = Z_ppgle(h,sqri(g));
    2789           0 :     g = gel(v,1);
    2790           0 :     h = gel(v,2);
    2791           0 :     c = gel(v,3); if (is_pm1(c)) continue;
    2792           0 :     d = gcdii(c,b);
    2793           0 :     x = mulii(x,d);
    2794           0 :     y = d; for (i=1; i < n; i++) y = sqri(y);
    2795           0 :     Z_dcba_rec(L, diviiexact(c,y), d);
    2796             :   }
    2797           0 :   Z_dcba_rec(L,diviiexact(b,x), c0);
    2798             : }
    2799             : static GEN
    2800     3608668 : Z_cba_rec(GEN L, GEN a, GEN b)
    2801             : {
    2802             :   GEN g;
    2803             :   /* a few naive steps before switching to dcba */
    2804     3608668 :   if (lg(L) > 10) { Z_dcba_rec(L, a, b); return veclast(L); }
    2805     3608668 :   if (is_pm1(a)) return b;
    2806     2149973 :   g = gcdii(a,b);
    2807     2149973 :   if (is_pm1(g)) { vectrunc_append(L, a); return b; }
    2808     1606871 :   a = diviiexact(a,g);
    2809     1606871 :   b = diviiexact(b,g);
    2810     1606871 :   return Z_cba_rec(L, Z_cba_rec(L, a, g), b);
    2811             : }
    2812             : GEN
    2813      394926 : Z_cba(GEN a, GEN b)
    2814             : {
    2815      394926 :   GEN L = vectrunc_init(expi(a) + expi(b) + 2);
    2816      394926 :   GEN t = Z_cba_rec(L, a, b);
    2817      394926 :   if (!is_pm1(t)) vectrunc_append(L, t);
    2818      394926 :   return L;
    2819             : }
    2820             : /* P = coprime base, extend it by b; TODO: quadratic for now */
    2821             : GEN
    2822          35 : ZV_cba_extend(GEN P, GEN b)
    2823             : {
    2824          35 :   long i, l = lg(P);
    2825          35 :   GEN w = cgetg(l+1, t_VEC);
    2826         133 :   for (i = 1; i < l; i++)
    2827             :   {
    2828          98 :     GEN v = Z_cba(gel(P,i), b);
    2829          98 :     long nv = lg(v)-1;
    2830          98 :     gel(w,i) = vecslice(v, 1, nv-1); /* those divide P[i] but not b */
    2831          98 :     b = gel(v,nv);
    2832             :   }
    2833          35 :   gel(w,l) = b; return shallowconcat1(w);
    2834             : }
    2835             : GEN
    2836          28 : ZV_cba(GEN v)
    2837             : {
    2838          28 :   long i, l = lg(v);
    2839             :   GEN P;
    2840          28 :   if (l <= 2) return v;
    2841          14 :   P = Z_cba(gel(v,1), gel(v,2));
    2842          42 :   for (i = 3; i < l; i++) P = ZV_cba_extend(P, gel(v,i));
    2843          14 :   return P;
    2844             : }
    2845             : 
    2846             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2847             : GEN
    2848    60159225 : Z_ppo(GEN x, GEN f)
    2849             : {
    2850             :   for (;;)
    2851             :   {
    2852    60159225 :     f = gcdii(x, f); if (is_pm1(f)) break;
    2853    40268260 :     x = diviiexact(x, f);
    2854             :   }
    2855    19888971 :   return x;
    2856             : }
    2857             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2858             : ulong
    2859    69426071 : u_ppo(ulong x, ulong f)
    2860             : {
    2861             :   for (;;)
    2862             :   {
    2863    69426071 :     f = ugcd(x, f); if (f == 1) break;
    2864    15595787 :     x /= f;
    2865             :   }
    2866    53830236 :   return x;
    2867             : }
    2868             : 
    2869             : /* result known to be representable as an ulong */
    2870             : static ulong
    2871     1463401 : lcmuu(ulong a, ulong b) { ulong d = ugcd(a,b); return (a/d) * b; }
    2872             : 
    2873             : /* assume 0 < x < N; return u in (Z/NZ)^* such that u x = gcd(x,N) (mod N);
    2874             :  * set *pd = gcd(x,N) */
    2875             : ulong
    2876     5431918 : Fl_invgen(ulong x, ulong N, ulong *pd)
    2877             : {
    2878             :   ulong d, d0, e, v, v1;
    2879             :   long s;
    2880     5431918 :   *pd = d = xgcduu(N, x, 0, &v, &v1, &s);
    2881     5432647 :   if (s > 0) v = N - v;
    2882     5432647 :   if (d == 1) return v;
    2883             :   /* vx = gcd(x,N) (mod N), v coprime to N/d but need not be coprime to N */
    2884     2531437 :   e = N / d;
    2885     2531437 :   d0 = u_ppo(d, e); /* d = d0 d1, d0 coprime to N/d, rad(d1) | N/d */
    2886     2531600 :   if (d0 == 1) return v;
    2887     1463341 :   e = lcmuu(e, d / d0);
    2888     1463391 :   return u_chinese_coprime(v, 1, e, d0, e*d0);
    2889             : }
    2890             : 
    2891             : /* x t_INT, f ideal. Write x = x1 x2, sqf(x1) | f, (x2,f) = 1. Return x2 */
    2892             : static GEN
    2893         126 : nf_coprime_part(GEN nf, GEN x, GEN listpr)
    2894             : {
    2895         126 :   long v, j, lp = lg(listpr), N = nf_get_degree(nf);
    2896             :   GEN x1, x2, ex;
    2897             : 
    2898             : #if 0 /*1) via many gcds. Expensive ! */
    2899             :   GEN f = idealprodprime(nf, listpr);
    2900             :   f = ZM_hnfmodid(f, x); /* first gcd is less expensive since x in Z */
    2901             :   x = scalarmat(x, N);
    2902             :   for (;;)
    2903             :   {
    2904             :     if (gequal1(gcoeff(f,1,1))) break;
    2905             :     x = idealdivexact(nf, x, f);
    2906             :     f = ZM_hnfmodid(shallowconcat(f,x), gcoeff(x,1,1)); /* gcd(f,x) */
    2907             :   }
    2908             :   x2 = x;
    2909             : #else /*2) from prime decomposition */
    2910         126 :   x1 = NULL;
    2911         350 :   for (j=1; j<lp; j++)
    2912             :   {
    2913         224 :     GEN pr = gel(listpr,j);
    2914         224 :     v = Z_pval(x, pr_get_p(pr)); if (!v) continue;
    2915             : 
    2916         126 :     ex = muluu(v, pr_get_e(pr)); /* = v_pr(x) > 0 */
    2917         126 :     x1 = x1? idealmulpowprime(nf, x1, pr, ex)
    2918         126 :            : idealpow(nf, pr, ex);
    2919             :   }
    2920         126 :   x = scalarmat(x, N);
    2921         126 :   x2 = x1? idealdivexact(nf, x, x1): x;
    2922             : #endif
    2923         126 :   return x2;
    2924             : }
    2925             : 
    2926             : /* L0 in K^*, assume (L0,f) = 1. Return L integral, L0 = L mod f  */
    2927             : GEN
    2928       10920 : make_integral(GEN nf, GEN L0, GEN f, GEN listpr)
    2929             : {
    2930             :   GEN fZ, t, L, D2, d1, d2, d;
    2931             : 
    2932       10920 :   L = Q_remove_denom(L0, &d);
    2933       10920 :   if (!d) return L0;
    2934             : 
    2935             :   /* L0 = L / d, L integral */
    2936         518 :   fZ = gcoeff(f,1,1);
    2937         518 :   if (typ(L) == t_INT) return Fp_mul(L, Fp_inv(d, fZ), fZ);
    2938             :   /* Kill denom part coprime to fZ */
    2939         126 :   d2 = Z_ppo(d, fZ);
    2940         126 :   t = Fp_inv(d2, fZ); if (!is_pm1(t)) L = ZC_Z_mul(L,t);
    2941         126 :   if (equalii(d, d2)) return L;
    2942             : 
    2943         126 :   d1 = diviiexact(d, d2);
    2944             :   /* L0 = (L / d1) mod f. d1 not coprime to f
    2945             :    * write (d1) = D1 D2, D2 minimal, (D2,f) = 1. */
    2946         126 :   D2 = nf_coprime_part(nf, d1, listpr);
    2947         126 :   t = idealaddtoone_i(nf, D2, f); /* in D2, 1 mod f */
    2948         126 :   L = nfmuli(nf,t,L);
    2949             : 
    2950             :   /* if (L0, f) = 1, then L in D1 ==> in D1 D2 = (d1) */
    2951         126 :   return Q_div_to_int(L, d1); /* exact division */
    2952             : }
    2953             : 
    2954             : /* assume L is a list of prime ideals. Return the product */
    2955             : GEN
    2956         336 : idealprodprime(GEN nf, GEN L)
    2957             : {
    2958         336 :   long l = lg(L), i;
    2959             :   GEN z;
    2960         336 :   if (l == 1) return matid(nf_get_degree(nf));
    2961         336 :   z = pr_hnf(nf, gel(L,1));
    2962         364 :   for (i=2; i<l; i++) z = idealHNF_mul_two(nf,z, gel(L,i));
    2963         336 :   return z;
    2964             : }
    2965             : 
    2966             : /* optimize for the frequent case I = nfhnf()[2]: lots of them are 1 */
    2967             : GEN
    2968         462 : idealprod(GEN nf, GEN I)
    2969             : {
    2970         462 :   long i, l = lg(I);
    2971             :   GEN z;
    2972        1134 :   for (i = 1; i < l; i++)
    2973        1127 :     if (!equali1(gel(I,i))) break;
    2974         462 :   if (i == l) return gen_1;
    2975         455 :   z = gel(I,i);
    2976         763 :   for (i++; i<l; i++) z = idealmul(nf, z, gel(I,i));
    2977         455 :   return z;
    2978             : }
    2979             : 
    2980             : /* v_pr(idealprod(nf,I)) */
    2981             : long
    2982        2806 : idealprodval(GEN nf, GEN I, GEN pr)
    2983             : {
    2984        2806 :   long i, l = lg(I), v = 0;
    2985       16006 :   for (i = 1; i < l; i++)
    2986       13200 :     if (!equali1(gel(I,i))) v += idealval(nf, gel(I,i), pr);
    2987        2806 :   return v;
    2988             : }
    2989             : 
    2990             : /* assume L is a list of prime ideals. Return prod L[i]^e[i] */
    2991             : GEN
    2992       57566 : factorbackprime(GEN nf, GEN L, GEN e)
    2993             : {
    2994       57566 :   long l = lg(L), i;
    2995             :   GEN z;
    2996             : 
    2997       57566 :   if (l == 1) return matid(nf_get_degree(nf));
    2998       44154 :   z = idealpow(nf, gel(L,1), gel(e,1));
    2999       73770 :   for (i=2; i<l; i++)
    3000       29616 :     if (signe(gel(e,i))) z = idealmulpowprime(nf,z, gel(L,i),gel(e,i));
    3001       44154 :   return z;
    3002             : }
    3003             : 
    3004             : /* F in Z, divisible exactly by pr.p. Return F-uniformizer for pr, i.e.
    3005             :  * a t in Z_K such that v_pr(t) = 1 and (t, F/pr) = 1 */
    3006             : GEN
    3007       57932 : pr_uniformizer(GEN pr, GEN F)
    3008             : {
    3009       57932 :   GEN p = pr_get_p(pr), t = pr_get_gen(pr);
    3010       57932 :   if (!equalii(F, p))
    3011             :   {
    3012       36616 :     long e = pr_get_e(pr);
    3013       36616 :     GEN u, v, q = (e == 1)? sqri(p): p;
    3014       36616 :     u = mulii(q, Fp_inv(q, diviiexact(F,p))); /* 1 mod F/p, 0 mod q */
    3015       36616 :     v = subui(1UL, u); /* 0 mod F/p, 1 mod q */
    3016       36616 :     if (pr_is_inert(pr))
    3017          28 :       t = addii(mulii(p, v), u);
    3018             :     else
    3019             :     {
    3020       36588 :       t = ZC_Z_mul(t, v);
    3021       36588 :       gel(t,1) = addii(gel(t,1), u); /* return u + vt */
    3022             :     }
    3023             :   }
    3024       57932 :   return t;
    3025             : }
    3026             : /* L = list of prime ideals, return lcm_i (L[i] \cap \ZM) */
    3027             : GEN
    3028       81834 : prV_lcm_capZ(GEN L)
    3029             : {
    3030       81834 :   long i, r = lg(L);
    3031             :   GEN F;
    3032       81834 :   if (r == 1) return gen_1;
    3033       69283 :   F = pr_get_p(gel(L,1));
    3034      122724 :   for (i = 2; i < r; i++)
    3035             :   {
    3036       53441 :     GEN pr = gel(L,i), p = pr_get_p(pr);
    3037       53441 :     if (!dvdii(F, p)) F = mulii(F,p);
    3038             :   }
    3039       69283 :   return F;
    3040             : }
    3041             : /* v vector of prid. Return underlying list of rational primes */
    3042             : GEN
    3043       60501 : prV_primes(GEN v)
    3044             : {
    3045       60501 :   long i, l = lg(v);
    3046       60501 :   GEN w = cgetg(l,t_VEC);
    3047      196000 :   for (i=1; i<l; i++) gel(w,i) = pr_get_p(gel(v,i));
    3048       60501 :   return ZV_sort_uniq(w);
    3049             : }
    3050             : 
    3051             : /* Given a prime ideal factorization with possibly zero or negative
    3052             :  * exponents, gives b such that v_p(b) = v_p(x) for all prime ideals pr | x
    3053             :  * and v_pr(b) >= 0 for all other pr.
    3054             :  * For optimal performance, all [anti-]uniformizers should be precomputed,
    3055             :  * but no support for this yet. If nored, do not reduce result. */
    3056             : static GEN
    3057       53876 : idealapprfact_i(GEN nf, GEN x, int nored)
    3058             : {
    3059       53876 :   GEN d = NULL, z, L, e, e2, F;
    3060             :   long i, r;
    3061       53876 :   int hasden = 0;
    3062             : 
    3063       53876 :   nf = checknf(nf);
    3064       53876 :   L = gel(x,1);
    3065       53876 :   e = gel(x,2);
    3066       53876 :   F = prV_lcm_capZ(L);
    3067       53876 :   z = NULL; r = lg(e);
    3068      136197 :   for (i = 1; i < r; i++)
    3069             :   {
    3070       82321 :     long s = signe(gel(e,i));
    3071             :     GEN pi, q;
    3072       82321 :     if (!s) continue;
    3073       53564 :     if (s < 0) hasden = 1;
    3074       53564 :     pi = pr_uniformizer(gel(L,i), F);
    3075       53564 :     q = nfpow(nf, pi, gel(e,i));
    3076       53564 :     z = z? nfmul(nf, z, q): q;
    3077             :   }
    3078       53876 :   if (!z) return gen_1;
    3079       26771 :   if (hasden) /* denominator */
    3080             :   {
    3081       10107 :     z = Q_remove_denom(z, &d);
    3082       10107 :     d = diviiexact(d, Z_ppo(d, F));
    3083             :   }
    3084       26771 :   if (nored || typ(z) != t_COL) return d? gdiv(z, d): z;
    3085       10107 :   e2 = cgetg(r, t_VEC);
    3086       28680 :   for (i = 1; i < r; i++) gel(e2,i) = addiu(gel(e,i), 1);
    3087       10107 :   x = factorbackprime(nf, L, e2);
    3088       10107 :   if (d) x = RgM_Rg_mul(x, d);
    3089       10107 :   z = ZC_reducemodlll(z, x);
    3090       10107 :   return d? RgC_Rg_div(z,d): z;
    3091             : }
    3092             : 
    3093             : GEN
    3094           0 : idealapprfact(GEN nf, GEN x) {
    3095           0 :   pari_sp av = avma;
    3096           0 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    3097             : }
    3098             : GEN
    3099          14 : idealappr(GEN nf, GEN x) {
    3100          14 :   pari_sp av = avma;
    3101          14 :   if (!is_nf_extfactor(x)) x = idealfactor(nf, x);
    3102          14 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    3103             : }
    3104             : 
    3105             : /* OBSOLETE */
    3106             : GEN
    3107          14 : idealappr0(GEN nf, GEN x, long fl) { (void)fl; return idealappr(nf, x); }
    3108             : 
    3109             : static GEN
    3110          21 : mat_ideal_two_elt2(GEN nf, GEN x, GEN a)
    3111             : {
    3112          21 :   GEN F = idealfactor(nf,a), P = gel(F,1), E = gel(F,2);
    3113          21 :   long i, r = lg(E);
    3114          84 :   for (i=1; i<r; i++) gel(E,i) = stoi( idealval(nf,x,gel(P,i)) );
    3115          21 :   return idealapprfact_i(nf,F,1);
    3116             : }
    3117             : 
    3118             : static void
    3119          14 : not_in_ideal(GEN a) {
    3120          14 :   pari_err_DOMAIN("idealtwoelt2","element mod ideal", "!=", gen_0, a);
    3121           0 : }
    3122             : /* x integral in HNF, a an 'nf' */
    3123             : static int
    3124          28 : in_ideal(GEN x, GEN a)
    3125             : {
    3126          28 :   switch(typ(a))
    3127             :   {
    3128          14 :     case t_INT: return dvdii(a, gcoeff(x,1,1));
    3129           7 :     case t_COL: return RgV_is_ZV(a) && !!hnf_invimage(x, a);
    3130           7 :     default: return 0;
    3131             :   }
    3132             : }
    3133             : 
    3134             : /* Given an integral ideal x and a in x, gives a b such that
    3135             :  * x = aZ_K + bZ_K using the approximation theorem */
    3136             : GEN
    3137          42 : idealtwoelt2(GEN nf, GEN x, GEN a)
    3138             : {
    3139          42 :   pari_sp av = avma;
    3140             :   GEN cx, b;
    3141             : 
    3142          42 :   nf = checknf(nf);
    3143          42 :   a = nf_to_scalar_or_basis(nf, a);
    3144          42 :   x = idealhnf_shallow(nf,x);
    3145          42 :   if (lg(x) == 1)
    3146             :   {
    3147          14 :     if (!isintzero(a)) not_in_ideal(a);
    3148           7 :     set_avma(av); return gen_0;
    3149             :   }
    3150          28 :   x = Q_primitive_part(x, &cx);
    3151          28 :   if (cx) a = gdiv(a, cx);
    3152          28 :   if (!in_ideal(x, a)) not_in_ideal(a);
    3153          21 :   b = mat_ideal_two_elt2(nf, x, a);
    3154          21 :   if (typ(b) == t_COL)
    3155             :   {
    3156          14 :     GEN mod = idealhnf_principal(nf,a);
    3157          14 :     b = ZC_hnfrem(b,mod);
    3158          14 :     if (ZV_isscalar(b)) b = gel(b,1);
    3159             :   }
    3160             :   else
    3161             :   {
    3162           7 :     GEN aZ = typ(a) == t_COL? Q_denom(zk_inv(nf,a)): a; /* (a) \cap Z */
    3163           7 :     b = centermodii(b, aZ, shifti(aZ,-1));
    3164             :   }
    3165          21 :   b = cx? gmul(b,cx): gcopy(b);
    3166          21 :   return gerepileupto(av, b);
    3167             : }
    3168             : 
    3169             : /* Given 2 integral ideals x and y in nf, returns a beta in nf such that
    3170             :  * beta * x is an integral ideal coprime to y */
    3171             : GEN
    3172       37204 : idealcoprimefact(GEN nf, GEN x, GEN fy)
    3173             : {
    3174       37204 :   GEN L = gel(fy,1), e;
    3175       37204 :   long i, r = lg(L);
    3176             : 
    3177       37204 :   e = cgetg(r, t_COL);
    3178       76067 :   for (i=1; i<r; i++) gel(e,i) = stoi( -idealval(nf,x,gel(L,i)) );
    3179       37205 :   return idealapprfact_i(nf, mkmat2(L,e), 0);
    3180             : }
    3181             : GEN
    3182          84 : idealcoprime(GEN nf, GEN x, GEN y)
    3183             : {
    3184          84 :   pari_sp av = avma;
    3185          84 :   return gerepileupto(av, idealcoprimefact(nf, x, idealfactor(nf,y)));
    3186             : }
    3187             : 
    3188             : GEN
    3189           7 : nfmulmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    3190             : {
    3191           7 :   pari_sp av = avma;
    3192           7 :   GEN z, p, pr = modpr, T;
    3193             : 
    3194           7 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    3195           0 :   x = nf_to_Fq(nf,x,modpr);
    3196           0 :   y = nf_to_Fq(nf,y,modpr);
    3197           0 :   z = Fq_mul(x,y,T,p);
    3198           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    3199             : }
    3200             : 
    3201             : GEN
    3202           0 : nfdivmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    3203             : {
    3204           0 :   pari_sp av = avma;
    3205           0 :   nf = checknf(nf);
    3206           0 :   return gerepileupto(av, nfreducemodpr(nf, nfdiv(nf,x,y), modpr));
    3207             : }
    3208             : 
    3209             : GEN
    3210           0 : nfpowmodpr(GEN nf, GEN x, GEN k, GEN modpr)
    3211             : {
    3212           0 :   pari_sp av=avma;
    3213           0 :   GEN z, T, p, pr = modpr;
    3214             : 
    3215           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    3216           0 :   z = nf_to_Fq(nf,x,modpr);
    3217           0 :   z = Fq_pow(z,k,T,p);
    3218           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    3219             : }
    3220             : 
    3221             : GEN
    3222           0 : nfkermodpr(GEN nf, GEN x, GEN modpr)
    3223             : {
    3224           0 :   pari_sp av = avma;
    3225           0 :   GEN T, p, pr = modpr;
    3226             : 
    3227           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    3228           0 :   if (typ(x)!=t_MAT) pari_err_TYPE("nfkermodpr",x);
    3229           0 :   x = nfM_to_FqM(x, nf, modpr);
    3230           0 :   return gerepilecopy(av, FqM_to_nfM(FqM_ker(x,T,p), modpr));
    3231             : }
    3232             : 
    3233             : GEN
    3234           0 : nfsolvemodpr(GEN nf, GEN a, GEN b, GEN pr)
    3235             : {
    3236           0 :   const char *f = "nfsolvemodpr";
    3237           0 :   pari_sp av = avma;
    3238             :   GEN T, p, modpr;
    3239             : 
    3240           0 :   nf = checknf(nf);
    3241           0 :   modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    3242           0 :   if (typ(a)!=t_MAT) pari_err_TYPE(f,a);
    3243           0 :   a = nfM_to_FqM(a, nf, modpr);
    3244           0 :   switch(typ(b))
    3245             :   {
    3246           0 :     case t_MAT:
    3247           0 :       b = nfM_to_FqM(b, nf, modpr);
    3248           0 :       b = FqM_gauss(a,b,T,p);
    3249           0 :       if (!b) pari_err_INV(f,a);
    3250           0 :       a = FqM_to_nfM(b, modpr);
    3251           0 :       break;
    3252           0 :     case t_COL:
    3253           0 :       b = nfV_to_FqV(b, nf, modpr);
    3254           0 :       b = FqM_FqC_gauss(a,b,T,p);
    3255           0 :       if (!b) pari_err_INV(f,a);
    3256           0 :       a = FqV_to_nfV(b, modpr);
    3257           0 :       break;
    3258           0 :     default: pari_err_TYPE(f,b);
    3259             :   }
    3260           0 :   return gerepilecopy(av, a);
    3261             : }

Generated by: LCOV version 1.14