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

Generated by: LCOV version 1.16