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 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.10.0 lcov report (development 21730-50db3b0) Lines: 1357 1503 90.3 %
Date: 2018-01-19 06:18:47 Functions: 135 148 91.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /*******************************************************************/
      15             : /*                                                                 */
      16             : /*                       BASIC NF OPERATIONS                       */
      17             : /*                           (continued)                           */
      18             : /*                                                                 */
      19             : /*******************************************************************/
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : 
      23             : /*******************************************************************/
      24             : /*                                                                 */
      25             : /*                     IDEAL OPERATIONS                            */
      26             : /*                                                                 */
      27             : /*******************************************************************/
      28             : 
      29             : /* A valid ideal is either principal (valid nf_element), or prime, or a matrix
      30             :  * on the integer basis in HNF.
      31             :  * A prime ideal is of the form [p,a,e,f,b], where the ideal is p.Z_K+a.Z_K,
      32             :  * p is a rational prime, a belongs to Z_K, e=e(P/p), f=f(P/p), and b
      33             :  * is Lenstra's constant, such that p.P^(-1)= p Z_K + b Z_K.
      34             :  *
      35             :  * An extended ideal is a couple [I,F] where I is a valid ideal and F is
      36             :  * either an algebraic number, or a factorization matrix attached to an
      37             :  * algebraic number. All routines work with either extended ideals or ideals
      38             :  * (an omitted F is assumed to be [;] <-> 1).
      39             :  * All ideals are output in HNF form. */
      40             : 
      41             : /* types and conversions */
      42             : 
      43             : long
      44     3116596 : idealtyp(GEN *ideal, GEN *arch)
      45             : {
      46     3116596 :   GEN x = *ideal;
      47     3116596 :   long t,lx,tx = typ(x);
      48             : 
      49     3116596 :   if (tx==t_VEC && lg(x)==3)
      50      328262 :   { *arch = gel(x,2); x = gel(x,1); tx = typ(x); }
      51             :   else
      52     2788334 :     *arch = NULL;
      53     3116596 :   switch(tx)
      54             :   {
      55     1567713 :     case t_MAT: lx = lg(x);
      56     1567713 :       if (lx == 1) { t = id_PRINCIPAL; x = gen_0; break; }
      57     1567636 :       if (lx != lgcols(x)) pari_err_TYPE("idealtyp [non-square t_MAT]",x);
      58     1567629 :       t = id_MAT;
      59     1567629 :       break;
      60             : 
      61     1150477 :     case t_VEC: if (lg(x)!=6) pari_err_TYPE("idealtyp",x);
      62     1150463 :       t = id_PRIME; break;
      63             : 
      64             :     case t_POL: case t_POLMOD: case t_COL:
      65             :     case t_INT: case t_FRAC:
      66      398406 :       t = id_PRINCIPAL; break;
      67             :     default:
      68           0 :       pari_err_TYPE("idealtyp",x);
      69             :       return 0; /*LCOV_EXCL_LINE*/
      70             :   }
      71     3116575 :   *ideal = x; return t;
      72             : }
      73             : 
      74             : /* true nf; v = [a,x,...], a in Z. Return (a,x) */
      75             : GEN
      76      107871 : idealhnf_two(GEN nf, GEN v)
      77             : {
      78      107871 :   GEN p = gel(v,1), pi = gel(v,2), m = zk_scalar_or_multable(nf, pi);
      79      107871 :   if (typ(m) == t_INT) return scalarmat(gcdii(m,p), nf_get_degree(nf));
      80       93661 :   return ZM_hnfmodid(m, p);
      81             : }
      82             : /* true nf */
      83             : GEN
      84     1161471 : pr_hnf(GEN nf, GEN pr)
      85             : {
      86     1161471 :   GEN p = pr_get_p(pr), m;
      87     1161471 :   if (pr_is_inert(pr)) return scalarmat(p, nf_get_degree(nf));
      88      904053 :   m = zk_scalar_or_multable(nf, pr_get_gen(pr));
      89      904053 :   return ZM_hnfmodprime(m, p);
      90             : }
      91             : 
      92             : GEN
      93      267538 : idealhnf_principal(GEN nf, GEN x)
      94             : {
      95             :   GEN cx;
      96      267538 :   x = nf_to_scalar_or_basis(nf, x);
      97      267538 :   switch(typ(x))
      98             :   {
      99      152909 :     case t_COL: break;
     100       90977 :     case t_INT:  if (!signe(x)) return cgetg(1,t_MAT);
     101       90592 :       return scalarmat(absi(x), nf_get_degree(nf));
     102             :     case t_FRAC:
     103       23652 :       return scalarmat(Q_abs_shallow(x), nf_get_degree(nf));
     104           0 :     default: pari_err_TYPE("idealhnf",x);
     105             :   }
     106      152909 :   x = Q_primitive_part(x, &cx);
     107      152909 :   RgV_check_ZV(x, "idealhnf");
     108      152909 :   x = zk_multable(nf, x);
     109      152909 :   x = ZM_hnfmodid(x, zkmultable_capZ(x));
     110      152909 :   return cx? ZM_Q_mul(x,cx): x;
     111             : }
     112             : 
     113             : /* x integral ideal in t_MAT form, nx columns */
     114             : static GEN
     115           7 : vec_mulid(GEN nf, GEN x, long nx, long N)
     116             : {
     117           7 :   GEN m = cgetg(nx*N + 1, t_MAT);
     118             :   long i, j, k;
     119          21 :   for (i=k=1; i<=nx; i++)
     120          14 :     for (j=1; j<=N; j++) gel(m, k++) = zk_ei_mul(nf, gel(x,i),j);
     121           7 :   return m;
     122             : }
     123             : /* true nf */
     124             : GEN
     125      320829 : idealhnf_shallow(GEN nf, GEN x)
     126             : {
     127      320829 :   long tx = typ(x), lx = lg(x), N;
     128             : 
     129             :   /* cannot use idealtyp because here we allow non-square matrices */
     130      320829 :   if (tx == t_VEC && lx == 3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
     131      320829 :   if (tx == t_VEC && lx == 6) return pr_hnf(nf,x); /* PRIME */
     132      221494 :   switch(tx)
     133             :   {
     134             :     case t_MAT:
     135             :     {
     136             :       GEN cx;
     137       46914 :       long nx = lx-1;
     138       46914 :       N = nf_get_degree(nf);
     139       46914 :       if (nx == 0) return cgetg(1, t_MAT);
     140       46893 :       if (nbrows(x) != N) pari_err_TYPE("idealhnf [wrong dimension]",x);
     141       46886 :       if (nx == 1) return idealhnf_principal(nf, gel(x,1));
     142             : 
     143       45696 :       if (nx == N && RgM_is_ZM(x) && ZM_ishnf(x)) return x;
     144       22330 :       x = Q_primitive_part(x, &cx);
     145       22330 :       if (nx < N) x = vec_mulid(nf, x, nx, N);
     146       22330 :       x = ZM_hnfmod(x, ZM_detmult(x));
     147       22330 :       return cx? ZM_Q_mul(x,cx): x;
     148             :     }
     149             :     case t_QFI:
     150             :     case t_QFR:
     151             :     {
     152          14 :       pari_sp av = avma;
     153          14 :       GEN u, D = nf_get_disc(nf), T = nf_get_pol(nf), f = nf_get_index(nf);
     154          14 :       GEN A = gel(x,1), B = gel(x,2);
     155          14 :       N = nf_get_degree(nf);
     156          14 :       if (N != 2)
     157           0 :         pari_err_TYPE("idealhnf [Qfb for non-quadratic fields]", x);
     158          14 :       if (!equalii(qfb_disc(x), D))
     159           7 :         pari_err_DOMAIN("idealhnf [Qfb]", "disc(q)", "!=", D, x);
     160             :       /* x -> A Z + (-B + sqrt(D)) / 2 Z
     161             :          K = Q[t]/T(t), t^2 + ut + v = 0,  u^2 - 4v = Df^2
     162             :          => t = (-u + sqrt(D) f)/2
     163             :          => sqrt(D)/2 = (t + u/2)/f */
     164           7 :       u = gel(T,3);
     165           7 :       B = deg1pol_shallow(ginv(f),
     166             :                           gsub(gdiv(u, shifti(f,1)), gdiv(B,gen_2)),
     167           7 :                           varn(T));
     168           7 :       return gerepileupto(av, idealhnf_two(nf, mkvec2(A,B)));
     169             :     }
     170      174566 :     default: return idealhnf_principal(nf, x); /* PRINCIPAL */
     171             :   }
     172             : }
     173             : GEN
     174        3094 : idealhnf(GEN nf, GEN x)
     175             : {
     176        3094 :   pari_sp av = avma;
     177        3094 :   GEN y = idealhnf_shallow(checknf(nf), x);
     178        3080 :   return (avma == av)? gcopy(y): gerepileupto(av, y);
     179             : }
     180             : 
     181             : /* GP functions */
     182             : 
     183             : GEN
     184          63 : idealtwoelt0(GEN nf, GEN x, GEN a)
     185             : {
     186          63 :   if (!a) return idealtwoelt(nf,x);
     187          42 :   return idealtwoelt2(nf,x,a);
     188             : }
     189             : 
     190             : GEN
     191          42 : idealpow0(GEN nf, GEN x, GEN n, long flag)
     192             : {
     193          42 :   if (flag) return idealpowred(nf,x,n);
     194          35 :   return idealpow(nf,x,n);
     195             : }
     196             : 
     197             : GEN
     198          56 : idealmul0(GEN nf, GEN x, GEN y, long flag)
     199             : {
     200          56 :   if (flag) return idealmulred(nf,x,y);
     201          49 :   return idealmul(nf,x,y);
     202             : }
     203             : 
     204             : GEN
     205          42 : idealdiv0(GEN nf, GEN x, GEN y, long flag)
     206             : {
     207          42 :   switch(flag)
     208             :   {
     209          21 :     case 0: return idealdiv(nf,x,y);
     210          21 :     case 1: return idealdivexact(nf,x,y);
     211           0 :     default: pari_err_FLAG("idealdiv");
     212             :   }
     213             :   return NULL; /* LCOV_EXCL_LINE */
     214             : }
     215             : 
     216             : GEN
     217          70 : idealaddtoone0(GEN nf, GEN arg1, GEN arg2)
     218             : {
     219          70 :   if (!arg2) return idealaddmultoone(nf,arg1);
     220          35 :   return idealaddtoone(nf,arg1,arg2);
     221             : }
     222             : 
     223             : /* b not a scalar */
     224             : static GEN
     225          28 : hnf_Z_ZC(GEN nf, GEN a, GEN b) { return hnfmodid(zk_multable(nf,b), a); }
     226             : /* b not a scalar */
     227             : static GEN
     228          21 : hnf_Z_QC(GEN nf, GEN a, GEN b)
     229             : {
     230             :   GEN db;
     231          21 :   b = Q_remove_denom(b, &db);
     232          21 :   if (db) a = mulii(a, db);
     233          21 :   b = hnf_Z_ZC(nf,a,b);
     234          21 :   return db? RgM_Rg_div(b, db): b;
     235             : }
     236             : /* b not a scalar (not point in trying to optimize for this case) */
     237             : static GEN
     238          28 : hnf_Q_QC(GEN nf, GEN a, GEN b)
     239             : {
     240             :   GEN da, db;
     241          28 :   if (typ(a) == t_INT) return hnf_Z_QC(nf, a, b);
     242           7 :   da = gel(a,2);
     243           7 :   a = gel(a,1);
     244           7 :   b = Q_remove_denom(b, &db);
     245             :   /* write da = d*A, db = d*B, gcd(A,B) = 1
     246             :    * gcd(a/(d A), b/(d B)) = gcd(a B, A b) / A B d = gcd(a B, b) / A B d */
     247           7 :   if (db)
     248             :   {
     249           7 :     GEN d = gcdii(da,db);
     250           7 :     if (!is_pm1(d)) db = diviiexact(db,d); /* B */
     251           7 :     if (!is_pm1(db))
     252             :     {
     253           7 :       a = mulii(a, db); /* a B */
     254           7 :       da = mulii(da, db); /* A B d = lcm(denom(a),denom(b)) */
     255             :     }
     256             :   }
     257           7 :   return RgM_Rg_div(hnf_Z_ZC(nf,a,b), da);
     258             : }
     259             : static GEN
     260           7 : hnf_QC_QC(GEN nf, GEN a, GEN b)
     261             : {
     262             :   GEN da, db, d, x;
     263           7 :   a = Q_remove_denom(a, &da);
     264           7 :   b = Q_remove_denom(b, &db);
     265           7 :   if (da) b = ZC_Z_mul(b, da);
     266           7 :   if (db) a = ZC_Z_mul(a, db);
     267           7 :   d = mul_denom(da, db);
     268           7 :   a = zk_multable(nf,a); da = zkmultable_capZ(a);
     269           7 :   b = zk_multable(nf,b); db = zkmultable_capZ(b);
     270           7 :   x = ZM_hnfmodid(shallowconcat(a,b), gcdii(da,db));
     271           7 :   return d? RgM_Rg_div(x, d): x;
     272             : }
     273             : static GEN
     274          21 : hnf_Q_Q(GEN nf, GEN a, GEN b) {return scalarmat(Q_gcd(a,b), nf_get_degree(nf));}
     275             : GEN
     276         119 : idealhnf0(GEN nf, GEN a, GEN b)
     277             : {
     278             :   long ta, tb;
     279             :   pari_sp av;
     280             :   GEN x;
     281         119 :   if (!b) return idealhnf(nf,a);
     282             : 
     283             :   /* HNF of aZ_K+bZ_K */
     284          56 :   av = avma; nf = checknf(nf);
     285          56 :   a = nf_to_scalar_or_basis(nf,a); ta = typ(a);
     286          56 :   b = nf_to_scalar_or_basis(nf,b); tb = typ(b);
     287          56 :   if (ta == t_COL)
     288          14 :     x = (tb==t_COL)? hnf_QC_QC(nf, a,b): hnf_Q_QC(nf, b,a);
     289             :   else
     290          42 :     x = (tb==t_COL)? hnf_Q_QC(nf, a,b): hnf_Q_Q(nf, a,b);
     291          56 :   return gerepileupto(av, x);
     292             : }
     293             : 
     294             : /*******************************************************************/
     295             : /*                                                                 */
     296             : /*                       TWO-ELEMENT FORM                          */
     297             : /*                                                                 */
     298             : /*******************************************************************/
     299             : static GEN idealapprfact_i(GEN nf, GEN x, int nored);
     300             : 
     301             : static int
     302      148072 : ok_elt(GEN x, GEN xZ, GEN y)
     303             : {
     304      148072 :   pari_sp av = avma;
     305      148072 :   int r = ZM_equal(x, ZM_hnfmodid(y, xZ));
     306      148072 :   avma = av; return r;
     307             : }
     308             : 
     309             : static GEN
     310       51962 : addmul_col(GEN a, long s, GEN b)
     311             : {
     312             :   long i,l;
     313       51962 :   if (!s) return a? leafcopy(a): a;
     314       51795 :   if (!a) return gmulsg(s,b);
     315       48723 :   l = lg(a);
     316      260551 :   for (i=1; i<l; i++)
     317      211828 :     if (signe(gel(b,i))) gel(a,i) = addii(gel(a,i), mulsi(s, gel(b,i)));
     318       48723 :   return a;
     319             : }
     320             : 
     321             : /* a <-- a + s * b, all coeffs integers */
     322             : static GEN
     323       22967 : addmul_mat(GEN a, long s, GEN b)
     324             : {
     325             :   long j,l;
     326             :   /* copy otherwise next call corrupts a */
     327       22967 :   if (!s) return a? RgM_shallowcopy(a): a;
     328       21384 :   if (!a) return gmulsg(s,b);
     329       11529 :   l = lg(a);
     330       55709 :   for (j=1; j<l; j++)
     331       44180 :     (void)addmul_col(gel(a,j), s, gel(b,j));
     332       11529 :   return a;
     333             : }
     334             : 
     335             : static GEN
     336       77365 : get_random_a(GEN nf, GEN x, GEN xZ)
     337             : {
     338             :   pari_sp av;
     339       77365 :   long i, lm, l = lg(x);
     340             :   GEN a, z, beta, mul;
     341             : 
     342       77365 :   beta= cgetg(l, t_VEC);
     343       77365 :   mul = cgetg(l, t_VEC); lm = 1; /* = lg(mul) */
     344             :   /* look for a in x such that a O/xZ = x O/xZ */
     345      151745 :   for (i = 2; i < l; i++)
     346             :   {
     347      148673 :     GEN xi = gel(x,i);
     348      148673 :     GEN t = FpM_red(zk_multable(nf,xi), xZ); /* ZM, cannot be a scalar */
     349      148673 :     if (gequal0(t)) continue;
     350      138217 :     if (ok_elt(x,xZ, t)) return xi;
     351       63924 :     gel(beta,lm) = xi;
     352             :     /* mul[i] = { canonical generators for x[i] O/xZ as Z-module } */
     353       63924 :     gel(mul,lm) = t; lm++;
     354             :   }
     355        3072 :   setlg(mul, lm);
     356        3072 :   setlg(beta,lm);
     357        3072 :   z = cgetg(lm, t_VECSMALL);
     358        9904 :   for(av = avma;; avma = av)
     359             :   {
     360       32871 :     for (a=NULL,i=1; i<lm; i++)
     361             :     {
     362       22967 :       long t = random_bits(4) - 7; /* in [-7,8] */
     363       22967 :       z[i] = t;
     364       22967 :       a = addmul_mat(a, t, gel(mul,i));
     365             :     }
     366             :     /* a = matrix (NOT HNF) of ideal generated by beta.z in O/xZ */
     367        9904 :     if (a && ok_elt(x,xZ, a)) break;
     368        6832 :   }
     369       10854 :   for (a=NULL,i=1; i<lm; i++)
     370        7782 :     a = addmul_col(a, z[i], gel(beta,i));
     371        3072 :   return a;
     372             : }
     373             : 
     374             : /* x square matrix, assume it is HNF */
     375             : static GEN
     376      194626 : mat_ideal_two_elt(GEN nf, GEN x)
     377             : {
     378             :   GEN y, a, cx, xZ;
     379      194626 :   long N = nf_get_degree(nf);
     380             :   pari_sp av, tetpil;
     381             : 
     382      194626 :   if (lg(x)-1 != N) pari_err_DIM("idealtwoelt");
     383      194612 :   if (N == 2) return mkvec2copy(gcoeff(x,1,1), gel(x,2));
     384             : 
     385       87176 :   y = cgetg(3,t_VEC); av = avma;
     386       87176 :   cx = Q_content(x);
     387       87176 :   xZ = gcoeff(x,1,1);
     388       87176 :   if (gequal(xZ, cx)) /* x = (cx) */
     389             :   {
     390        3171 :     gel(y,1) = cx;
     391        3171 :     gel(y,2) = gen_0; return y;
     392             :   }
     393       84005 :   if (equali1(cx)) cx = NULL;
     394             :   else
     395             :   {
     396        1393 :     x = Q_div_to_int(x, cx);
     397        1393 :     xZ = gcoeff(x,1,1);
     398             :   }
     399       84005 :   if (N < 6)
     400       73197 :     a = get_random_a(nf, x, xZ);
     401             :   else
     402             :   {
     403       10808 :     const long FB[] = { _evallg(15+1) | evaltyp(t_VECSMALL),
     404             :       2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47
     405             :     };
     406       10808 :     GEN P, E, a1 = Z_smoothen(xZ, (GEN)FB, &P, &E);
     407       10808 :     if (!a1) /* factors completely */
     408        6640 :       a = idealapprfact_i(nf, idealfactor(nf,x), 1);
     409        4168 :     else if (lg(P) == 1) /* no small factors */
     410        2961 :       a = get_random_a(nf, x, xZ);
     411             :     else /* general case */
     412             :     {
     413             :       GEN A0, A1, a0, u0, u1, v0, v1, pi0, pi1, t, u;
     414        1207 :       a0 = diviiexact(xZ, a1);
     415        1207 :       A0 = ZM_hnfmodid(x, a0); /* smooth part of x */
     416        1207 :       A1 = ZM_hnfmodid(x, a1); /* cofactor */
     417        1207 :       pi0 = idealapprfact_i(nf, idealfactor(nf,A0), 1);
     418        1207 :       pi1 = get_random_a(nf, A1, a1);
     419        1207 :       (void)bezout(a0, a1, &v0,&v1);
     420        1207 :       u0 = mulii(a0, v0);
     421        1207 :       u1 = mulii(a1, v1);
     422        1207 :       if (typ(pi0) != t_COL) t = addmulii(u0, pi0, u1);
     423             :       else
     424        1207 :       { t = ZC_Z_mul(pi0, u1); gel(t,1) = addii(gel(t,1), u0); }
     425        1207 :       u = ZC_Z_mul(pi1, u0); gel(u,1) = addii(gel(u,1), u1);
     426        1207 :       a = nfmuli(nf, centermod(u, xZ), centermod(t, xZ));
     427             :     }
     428             :   }
     429       84005 :   if (cx)
     430             :   {
     431        1393 :     a = centermod(a, xZ);
     432        1393 :     tetpil = avma;
     433        1393 :     if (typ(cx) == t_INT)
     434             :     {
     435         273 :       gel(y,1) = mulii(xZ, cx);
     436         273 :       gel(y,2) = ZC_Z_mul(a, cx);
     437             :     }
     438             :     else
     439             :     {
     440        1120 :       gel(y,1) = gmul(xZ, cx);
     441        1120 :       gel(y,2) = RgC_Rg_mul(a, cx);
     442             :     }
     443             :   }
     444             :   else
     445             :   {
     446       82612 :     tetpil = avma;
     447       82612 :     gel(y,1) = icopy(xZ);
     448       82612 :     gel(y,2) = centermod(a, xZ);
     449             :   }
     450       84005 :   gerepilecoeffssp(av,tetpil,y+1,2); return y;
     451             : }
     452             : 
     453             : /* Given an ideal x, returns [a,alpha] such that a is in Q,
     454             :  * x = a Z_K + alpha Z_K, alpha in K^*
     455             :  * a = 0 or alpha = 0 are possible, but do not try to determine whether
     456             :  * x is principal. */
     457             : GEN
     458       38186 : idealtwoelt(GEN nf, GEN x)
     459             : {
     460             :   pari_sp av;
     461             :   GEN z;
     462       38186 :   long tx = idealtyp(&x,&z);
     463       38179 :   nf = checknf(nf);
     464       38179 :   if (tx == id_MAT) return mat_ideal_two_elt(nf,x);
     465        1694 :   if (tx == id_PRIME) return mkvec2copy(gel(x,1), gel(x,2));
     466             :   /* id_PRINCIPAL */
     467         875 :   av = avma; x = nf_to_scalar_or_basis(nf, x);
     468        1554 :   return gerepilecopy(av, typ(x)==t_COL? mkvec2(gen_0,x):
     469         770 :                                          mkvec2(Q_abs_shallow(x),gen_0));
     470             : }
     471             : 
     472             : /*******************************************************************/
     473             : /*                                                                 */
     474             : /*                         FACTORIZATION                           */
     475             : /*                                                                 */
     476             : /*******************************************************************/
     477             : /* x integral ideal in HNF, Zval = v_p(x \cap Z) > 0; return v_p(Nx) */
     478             : static long
     479      201801 : idealHNF_norm_pval(GEN x, GEN p, long Zval)
     480             : {
     481      201801 :   long i, v = Zval, l = lg(x);
     482      201801 :   for (i = 2; i < l; i++) v += Z_pval(gcoeff(x,i,i), p);
     483      201801 :   return v;
     484             : }
     485             : 
     486             : /* return P, primes dividing Nx and xZ = x\cap Z, set v_p(Nx), v_p(xZ);
     487             :  * x integral in HNF */
     488             : GEN
     489       36848 : idealHNF_Z_factor(GEN x, GEN *pvN, GEN *pvZ)
     490             : {
     491       36848 :   GEN xZ = gcoeff(x,1,1), f, P, E, vN, vZ;
     492             :   long i, l;
     493       36848 :   if (typ(xZ) != t_INT) pari_err_TYPE("idealfactor",x);
     494       36848 :   f = Z_factor(xZ);
     495       36848 :   P = gel(f,1); l = lg(P);
     496       36848 :   E = gel(f,2);
     497       36848 :   *pvN = vN = cgetg(l, t_VECSMALL);
     498       36848 :   *pvZ = vZ = cgetg(l, t_VECSMALL);
     499       69403 :   for (i = 1; i < l; i++)
     500             :   {
     501       32555 :     vZ[i] = itou(gel(E,i));
     502       32555 :     vN[i] = idealHNF_norm_pval(x,gel(P,i), vZ[i]);
     503             :   }
     504       36848 :   return P;
     505             : }
     506             : 
     507             : /* v_P(A)*f(P) <= Nval [e.g. Nval = v_p(Norm A)], Zval = v_p(A \cap Z).
     508             :  * Return v_P(A) */
     509             : static long
     510      218550 : idealHNF_val(GEN A, GEN P, long Nval, long Zval)
     511             : {
     512      218550 :   long f = pr_get_f(P), vmax, v, e, i, j, k, l;
     513             :   GEN mul, B, a, y, r, p, pk, cx, vals;
     514             :   pari_sp av;
     515             : 
     516      218550 :   if (Nval < f) return 0;
     517      218487 :   p = pr_get_p(P);
     518      218487 :   e = pr_get_e(P);
     519             :   /* v_P(A) <= max [ e * v_p(A \cap Z), floor[v_p(Nix) / f ] */
     520      218487 :   vmax = minss(Zval * e, Nval / f);
     521      218487 :   mul = pr_get_tau(P);
     522      218487 :   l = lg(mul);
     523      218487 :   B = cgetg(l,t_MAT);
     524             :   /* B[1] not needed: v_pr(A[1]) = v_pr(A \cap Z) is known already */
     525      218487 :   gel(B,1) = gen_0; /* dummy */
     526      669836 :   for (j = 2; j < l; j++)
     527             :   {
     528      522988 :     GEN x = gel(A,j);
     529      522988 :     gel(B,j) = y = cgetg(l, t_COL);
     530     4219241 :     for (i = 1; i < l; i++)
     531             :     { /* compute a = (x.t0)_i, A in HNF ==> x[j+1..l-1] = 0 */
     532     3767892 :       a = mulii(gel(x,1), gcoeff(mul,i,1));
     533     3767892 :       for (k = 2; k <= j; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     534             :       /* p | a ? */
     535     3767892 :       gel(y,i) = dvmdii(a,p,&r); if (signe(r)) return 0;
     536             :     }
     537             :   }
     538      146848 :   vals = cgetg(l, t_VECSMALL);
     539             :   /* vals[1] not needed */
     540      533988 :   for (j = 2; j < l; j++)
     541             :   {
     542      387140 :     gel(B,j) = Q_primitive_part(gel(B,j), &cx);
     543      387140 :     vals[j] = cx? 1 + e * Q_pval(cx, p): 1;
     544             :   }
     545      146848 :   pk = powiu(p, ceildivuu(vmax, e));
     546      146848 :   av = avma; y = cgetg(l,t_COL);
     547             :   /* can compute mod p^ceil((vmax-v)/e) */
     548      207338 :   for (v = 1; v < vmax; v++)
     549             :   { /* we know v_pr(Bj) >= v for all j */
     550       63723 :     if (e == 1 || (vmax - v) % e == 0) pk = diviiexact(pk, p);
     551      494564 :     for (j = 2; j < l; j++)
     552             :     {
     553      434074 :       GEN x = gel(B,j); if (v < vals[j]) continue;
     554     4381263 :       for (i = 1; i < l; i++)
     555             :       {
     556     4067584 :         pari_sp av2 = avma;
     557     4067584 :         a = mulii(gel(x,1), gcoeff(mul,i,1));
     558     4067584 :         for (k = 2; k < l; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     559             :         /* a = (x.t_0)_i; p | a ? */
     560     4067584 :         a = dvmdii(a,p,&r); if (signe(r)) return v;
     561     4064351 :         if (lgefint(a) > lgefint(pk)) a = remii(a, pk);
     562     4064351 :         gel(y,i) = gerepileuptoint(av2, a);
     563             :       }
     564      313679 :       gel(B,j) = y; y = x;
     565      313679 :       if (gc_needed(av,3))
     566             :       {
     567           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"idealval");
     568           0 :         gerepileall(av,3, &y,&B,&pk);
     569             :       }
     570             :     }
     571             :   }
     572      143615 :   return v;
     573             : }
     574             : /* true nf, x integral ideal */
     575             : static GEN
     576       36848 : idealHNF_factor(GEN nf, GEN x)
     577             : {
     578       36848 :   const long N = lg(x)-1;
     579             :   long i, j, k, l, v;
     580             :   GEN vp, vN, vZ, vP, vE, cx;
     581             : 
     582       36848 :   x = Q_primitive_part(x, &cx);
     583       36848 :   vp = idealHNF_Z_factor(x, &vN,&vZ);
     584       36848 :   l = lg(vp);
     585       36848 :   i = cx? expi(cx)+1: 1;
     586       36848 :   vP = cgetg((l+i-2)*N+1, t_COL);
     587       36848 :   vE = cgetg((l+i-2)*N+1, t_COL);
     588       69403 :   for (i = k = 1; i < l; i++)
     589             :   {
     590       32555 :     GEN L, p = gel(vp,i);
     591       32555 :     long Nval = vN[i], Zval = vZ[i], vc = cx? Z_pvalrem(cx,p,&cx): 0;
     592       32555 :     if (vc)
     593             :     {
     594        1757 :       L = idealprimedec(nf,p);
     595        1757 :       if (is_pm1(cx)) cx = NULL;
     596             :     }
     597             :     else
     598       30798 :       L = idealprimedec_limit_f(nf,p,Nval);
     599       49311 :     for (j = 1; j < lg(L); j++)
     600             :     {
     601       49304 :       GEN P = gel(L,j);
     602       49304 :       pari_sp av = avma;
     603       49304 :       v = idealHNF_val(x, P, Nval, Zval);
     604       49304 :       avma = av;
     605       49304 :       Nval -= v*pr_get_f(P);
     606       49304 :       v += vc * pr_get_e(P); if (!v) continue;
     607       36848 :       gel(vP,k) = P;
     608       36848 :       gel(vE,k) = utoipos(v); k++;
     609       36848 :       if (!Nval) break; /* now only the content contributes */
     610             :     }
     611       33270 :     if (vc) for (j++; j<lg(L); j++)
     612             :     {
     613         715 :       GEN P = gel(L,j);
     614         715 :       gel(vP,k) = P;
     615         715 :       gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     616             :     }
     617             :   }
     618       36848 :   if (cx)
     619             :   {
     620        7490 :     GEN f = Z_factor(cx), cP = gel(f,1), cE = gel(f,2);
     621        7490 :     long lc = lg(cP);
     622       15729 :     for (i=1; i<lc; i++)
     623             :     {
     624        8239 :       GEN p = gel(cP,i), L = idealprimedec(nf,p);
     625        8239 :       long vc = itos(gel(cE,i));
     626       18256 :       for (j=1; j<lg(L); j++)
     627             :       {
     628       10017 :         GEN P = gel(L,j);
     629       10017 :         gel(vP,k) = P;
     630       10017 :         gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     631             :       }
     632             :     }
     633             :   }
     634       36848 :   setlg(vP, k);
     635       36848 :   setlg(vE, k); return mkmat2(vP, vE);
     636             : }
     637             : /* c * vector(#L,i,L[i].e), assume results fit in ulong */
     638             : static GEN
     639        3080 : prV_e_muls(GEN L, long c)
     640             : {
     641        3080 :   long j, l = lg(L);
     642        3080 :   GEN z = cgetg(l, t_COL);
     643        3080 :   for (j = 1; j < l; j++) gel(z,j) = stoi(c * pr_get_e(gel(L,j)));
     644        3080 :   return z;
     645             : }
     646             : /* true nf, y in Q */
     647             : static GEN
     648        3108 : Q_nffactor(GEN nf, GEN y)
     649             : {
     650             :   GEN f, P, E;
     651             :   long lfa, i;
     652        3108 :   if (typ(y) == t_INT)
     653             :   {
     654        3094 :     if (!signe(y)) pari_err_DOMAIN("idealfactor", "ideal", "=",gen_0,y);
     655        3073 :     if (is_pm1(y)) return trivial_fact();
     656             :   }
     657        2324 :   f = factor(Q_abs_shallow(y));
     658        2324 :   P = gel(f,1); lfa = lg(P);
     659        2324 :   E = gel(f,2);
     660        5404 :   for (i = 1; i < lfa; i++)
     661             :   {
     662        3080 :     gel(P,i) = idealprimedec(nf, gel(P,i));
     663        3080 :     gel(E,i) = prV_e_muls(gel(P,i), itos(gel(E,i)));
     664             :   }
     665        2324 :   settyp(P,t_VEC); P = shallowconcat1(P);
     666        2324 :   settyp(E,t_VEC); E = shallowconcat1(E);
     667        2324 :   gel(f,1) = P; settyp(P, t_COL);
     668        2324 :   gel(f,2) = E; return f;
     669             : }
     670             : 
     671             : GEN
     672       39984 : idealfactor(GEN nf, GEN x)
     673             : {
     674       39984 :   pari_sp av = avma;
     675             :   GEN fa, y;
     676       39984 :   long tx = idealtyp(&x,&y);
     677             : 
     678       39984 :   nf = checknf(nf);
     679       39984 :   if (tx == id_PRIME) retmkmat2(mkcolcopy(x), mkcol(gen_1));
     680       39949 :   if (tx == id_PRINCIPAL)
     681             :   {
     682        5033 :     y = nf_to_scalar_or_basis(nf, x);
     683        5033 :     if (typ(y) != t_COL) return gerepilecopy(av, Q_nffactor(nf, y));
     684             :   }
     685       36841 :   y = idealnumden(nf, x);
     686       36841 :   fa = idealHNF_factor(nf, gel(y,1));
     687       36841 :   if (!isint1(gel(y,2)))
     688             :   {
     689           7 :     GEN F = idealHNF_factor(nf, gel(y,2));
     690           7 :     fa = famat_mul_shallow(fa, famat_inv_shallow(F));
     691             :   }
     692       36841 :   fa = gerepilecopy(av, fa);
     693       36841 :   return sort_factor(fa, (void*)&cmp_prime_ideal, &cmp_nodata);
     694             : }
     695             : 
     696             : /* true nf; A is assumed to be the n-th power of an integral ideal,
     697             :  * return its n-th root; n > 1 */
     698             : static long
     699          35 : idealsqrtn_int(GEN nf, GEN A, long n, GEN *pB)
     700             : {
     701             :   GEN C, ram, vram, root;
     702             :   long i, l;
     703             :   /* compute valuations at ramified primes */
     704          35 :   ram = gel(idealfactor(nf, idealadd(nf, nf_get_diff(nf),A)), 1);
     705          35 :   l = lg(ram); vram = cgetg(l, t_VECSMALL);
     706          49 :   for (i = 1; i < l; i++)
     707             :   {
     708          14 :     long v = idealval(nf,A,gel(ram,i));
     709          14 :     if (v % n) return 0;
     710          14 :     vram[i] = v / n;
     711             :   }
     712          35 :   root = idealfactorback(nf, ram, vram, 0);
     713             :   /* remove ramified primes */
     714          35 :   A = idealdivexact(nf, A, idealpows(nf,root,n));
     715          35 :   A = Q_primitive_part(A, &C);
     716          35 :   if (C)
     717             :   {
     718           0 :     if (!Z_ispowerall(C,n,&C)) return 0;
     719           0 :     if (pB) root = ZM_Z_mul(root, C);
     720             :   }
     721             : 
     722             :   /* compute final n-th root, at most degree(nf)-1 iterations */
     723          49 :   for (i = 0;; i++)
     724             :   {
     725          49 :     GEN J, b, a = gcoeff(A,1,1); /* A \cap Z */
     726          49 :     if (is_pm1(a)) break;
     727          21 :     if (!Z_ispowerall(a,n,&b)) return 0;
     728          14 :     J = idealadd(nf, b, A);
     729          14 :     A = idealdivexact(nf, idealpows(nf,J,n), A);
     730          14 :     if (pB) root = odd(i)? idealdivexact(nf, root, J): idealmul(nf, root, J);
     731          14 :   }
     732          56 :   if (pB) *pB = root;
     733          28 :   return 1;
     734             : }
     735             : 
     736             : /* A is assumed to be the n-th power of an ideal in nf
     737             :  returns its n-th root. */
     738             : long
     739          28 : idealispower(GEN nf, GEN A, long n, GEN *pB)
     740             : {
     741          28 :   pari_sp av = avma;
     742             :   GEN v, N, D;
     743          28 :   nf = checknf(nf);
     744          28 :   if (n <= 0) pari_err_DOMAIN("idealispower", "n", "<=", gen_0, stoi(n));
     745          28 :   if (n == 1) { if (pB) *pB = idealhnf(nf,A); return 1; }
     746          21 :   v = idealnumden(nf,A);
     747          21 :   if (gequal0(gel(v,1))) { avma = av; if (pB) *pB = cgetg(1,t_MAT); return 1; }
     748          21 :   if (!idealsqrtn_int(nf, gel(v,1), n, pB? &N: NULL)) return 0;
     749          14 :   if (!idealsqrtn_int(nf, gel(v,2), n, pB? &D: NULL)) return 0;
     750          14 :   if (pB) *pB = gerepileupto(av, idealdiv(nf,N,D)); else avma = av;
     751          14 :   return 1;
     752             : }
     753             : 
     754             : GEN
     755           7 : nfeltredmodpower(GEN nf, GEN x, ulong k, ulong B)
     756             : {
     757           7 :   pari_sp av = avma;
     758             :   GEN ix, F, N, iD, iQ, y, z;
     759           7 :   nf = checknf(nf);
     760           7 :   ix = gmael(idealhnf_principal(nf,x),1,1);
     761           7 :   F = Z_factor_limit(ix, B);
     762           7 :   N = factorback2(gel(F,1), FpV_red(gel(F,2), utoi(k)));
     763           7 :   iD = idealdivexact(nf, x, idealadd(nf, x, N)); /* integral */
     764           7 :   if (!idealispower(nf, iD, k, &iQ)) { avma = av; return cgetg(1,t_VEC); }
     765           7 :   y = idealred_elt(nf, idealHNF_inv_Z(nf, iQ));
     766           7 :   ix = gcoeff(iQ,1,1);
     767           7 :   z = gdiv(nfmul(nf, x, nfpow_u(nf, y, k)), powiu(ix,k));
     768           7 :   return gerepilecopy(av, mkvec2(z, gdiv(y,ix)));
     769             : }
     770             : 
     771             : /* P prime ideal in idealprimedec format. Return valuation(A) at P */
     772             : long
     773      488143 : idealval(GEN nf, GEN A, GEN P)
     774             : {
     775      488143 :   pari_sp av = avma;
     776             :   GEN a, p, cA;
     777      488143 :   long vcA, v, Zval, tx = idealtyp(&A,&a);
     778             : 
     779      488143 :   if (tx == id_PRINCIPAL) return nfval(nf,A,P);
     780      483712 :   checkprid(P);
     781      483712 :   if (tx == id_PRIME) return pr_equal(P, A)? 1: 0;
     782             :   /* id_MAT */
     783      483684 :   nf = checknf(nf);
     784      483684 :   A = Q_primitive_part(A, &cA);
     785      483684 :   p = pr_get_p(P);
     786      483684 :   vcA = cA? Q_pval(cA,p): 0;
     787      483684 :   if (pr_is_inert(P)) { avma = av; return vcA; }
     788      476229 :   Zval = Z_pval(gcoeff(A,1,1), p);
     789      476229 :   if (!Zval) v = 0;
     790             :   else
     791             :   {
     792      169246 :     long Nval = idealHNF_norm_pval(A, p, Zval);
     793      169246 :     v = idealHNF_val(A, P, Nval, Zval);
     794             :   }
     795      476229 :   avma = av; return vcA? v + vcA*pr_get_e(P): v;
     796             : }
     797             : GEN
     798        6573 : gpidealval(GEN nf, GEN ix, GEN P)
     799             : {
     800        6573 :   long v = idealval(nf,ix,P);
     801        6573 :   return v == LONG_MAX? mkoo(): stoi(v);
     802             : }
     803             : 
     804             : /* gcd and generalized Bezout */
     805             : 
     806             : GEN
     807       59472 : idealadd(GEN nf, GEN x, GEN y)
     808             : {
     809       59472 :   pari_sp av = avma;
     810             :   long tx, ty;
     811             :   GEN z, a, dx, dy, dz;
     812             : 
     813       59472 :   tx = idealtyp(&x,&z);
     814       59472 :   ty = idealtyp(&y,&z); nf = checknf(nf);
     815       59472 :   if (tx != id_MAT) x = idealhnf_shallow(nf,x);
     816       59472 :   if (ty != id_MAT) y = idealhnf_shallow(nf,y);
     817       59472 :   if (lg(x) == 1) return gerepilecopy(av,y);
     818       59465 :   if (lg(y) == 1) return gerepilecopy(av,x); /* check for 0 ideal */
     819       59185 :   dx = Q_denom(x);
     820       59185 :   dy = Q_denom(y); dz = lcmii(dx,dy);
     821       59185 :   if (is_pm1(dz)) dz = NULL; else {
     822       12453 :     x = Q_muli_to_int(x, dz);
     823       12453 :     y = Q_muli_to_int(y, dz);
     824             :   }
     825       59185 :   a = gcdii(gcoeff(x,1,1), gcoeff(y,1,1));
     826       59185 :   if (is_pm1(a))
     827             :   {
     828       27684 :     long N = lg(x)-1;
     829       27684 :     if (!dz) { avma = av; return matid(N); }
     830        3611 :     return gerepileupto(av, scalarmat(ginv(dz), N));
     831             :   }
     832       31501 :   z = ZM_hnfmodid(shallowconcat(x,y), a);
     833       31501 :   if (dz) z = RgM_Rg_div(z,dz);
     834       31501 :   return gerepileupto(av,z);
     835             : }
     836             : 
     837             : static GEN
     838          28 : trivial_merge(GEN x)
     839          28 : { return (lg(x) == 1 || !is_pm1(gcoeff(x,1,1)))? NULL: gen_1; }
     840             : /* true nf */
     841             : static GEN
     842      120421 : _idealaddtoone(GEN nf, GEN x, GEN y, long red)
     843             : {
     844             :   GEN a;
     845      120421 :   long tx = idealtyp(&x, &a/*junk*/);
     846      120421 :   long ty = idealtyp(&y, &a/*junk*/);
     847             :   long ea;
     848      120421 :   if (tx != id_MAT) x = idealhnf_shallow(nf, x);
     849      120421 :   if (ty != id_MAT) y = idealhnf_shallow(nf, y);
     850      120421 :   if (lg(x) == 1)
     851          14 :     a = trivial_merge(y);
     852      120407 :   else if (lg(y) == 1)
     853          14 :     a = trivial_merge(x);
     854             :   else
     855      120393 :     a = hnfmerge_get_1(x, y);
     856      120421 :   if (!a) pari_err_COPRIME("idealaddtoone",x,y);
     857      120407 :   if (red && (ea = gexpo(a)) > 10)
     858             :   {
     859        6736 :     GEN b = (typ(a) == t_COL)? a: scalarcol_shallow(a, nf_get_degree(nf));
     860        6736 :     b = ZC_reducemodlll(b, idealHNF_mul(nf,x,y));
     861        6736 :     if (gexpo(b) < ea) a = b;
     862             :   }
     863      120407 :   return a;
     864             : }
     865             : /* true nf */
     866             : GEN
     867       12201 : idealaddtoone_i(GEN nf, GEN x, GEN y)
     868       12201 : { return _idealaddtoone(nf, x, y, 1); }
     869             : /* true nf */
     870             : GEN
     871      108220 : idealaddtoone_raw(GEN nf, GEN x, GEN y)
     872      108220 : { return _idealaddtoone(nf, x, y, 0); }
     873             : 
     874             : GEN
     875          98 : idealaddtoone(GEN nf, GEN x, GEN y)
     876             : {
     877          98 :   GEN z = cgetg(3,t_VEC), a;
     878          98 :   pari_sp av = avma;
     879          98 :   nf = checknf(nf);
     880          98 :   a = gerepileupto(av, idealaddtoone_i(nf,x,y));
     881          84 :   gel(z,1) = a;
     882          84 :   gel(z,2) = typ(a) == t_COL? Z_ZC_sub(gen_1,a): subui(1,a);
     883          84 :   return z;
     884             : }
     885             : 
     886             : /* assume elements of list are integral ideals */
     887             : GEN
     888          35 : idealaddmultoone(GEN nf, GEN list)
     889             : {
     890          35 :   pari_sp av = avma;
     891          35 :   long N, i, l, nz, tx = typ(list);
     892             :   GEN H, U, perm, L;
     893             : 
     894          35 :   nf = checknf(nf); N = nf_get_degree(nf);
     895          35 :   if (!is_vec_t(tx)) pari_err_TYPE("idealaddmultoone",list);
     896          35 :   l = lg(list);
     897          35 :   L = cgetg(l, t_VEC);
     898          35 :   if (l == 1)
     899           0 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
     900          35 :   nz = 0; /* number of non-zero ideals in L */
     901          98 :   for (i=1; i<l; i++)
     902             :   {
     903          70 :     GEN I = gel(list,i);
     904          70 :     if (typ(I) != t_MAT) I = idealhnf_shallow(nf,I);
     905          70 :     if (lg(I) != 1)
     906             :     {
     907          42 :       nz++; RgM_check_ZM(I,"idealaddmultoone");
     908          35 :       if (lgcols(I) != N+1) pari_err_TYPE("idealaddmultoone [not an ideal]", I);
     909             :     }
     910          63 :     gel(L,i) = I;
     911             :   }
     912          28 :   H = ZM_hnfperm(shallowconcat1(L), &U, &perm);
     913          28 :   if (lg(H) == 1 || !equali1(gcoeff(H,1,1)))
     914           7 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
     915          49 :   for (i=1; i<=N; i++)
     916          49 :     if (perm[i] == 1) break;
     917          21 :   U = gel(U,(nz-1)*N + i); /* (L[1]|...|L[nz]) U = 1 */
     918          21 :   nz = 0;
     919          63 :   for (i=1; i<l; i++)
     920             :   {
     921          42 :     GEN c = gel(L,i);
     922          42 :     if (lg(c) == 1)
     923          14 :       c = gen_0;
     924             :     else {
     925          28 :       c = ZM_ZC_mul(c, vecslice(U, nz*N + 1, (nz+1)*N));
     926          28 :       nz++;
     927             :     }
     928          42 :     gel(L,i) = c;
     929             :   }
     930          21 :   return gerepilecopy(av, L);
     931             : }
     932             : 
     933             : /* multiplication */
     934             : 
     935             : /* x integral ideal (without archimedean component) in HNF form
     936             :  * y = [a,alpha] corresponds to the integral ideal aZ_K+alpha Z_K, a in Z,
     937             :  * alpha a ZV or a ZM (multiplication table). Multiply them */
     938             : static GEN
     939      630183 : idealHNF_mul_two(GEN nf, GEN x, GEN y)
     940             : {
     941      630183 :   GEN m, a = gel(y,1), alpha = gel(y,2);
     942             :   long i, N;
     943             : 
     944      630183 :   if (typ(alpha) != t_MAT)
     945             :   {
     946      427187 :     alpha = zk_scalar_or_multable(nf, alpha);
     947      427187 :     if (typ(alpha) == t_INT) /* e.g. y inert ? 0 should not (but may) occur */
     948        2912 :       return signe(a)? ZM_Z_mul(x, gcdii(a, alpha)): cgetg(1,t_MAT);
     949             :   }
     950      627271 :   N = lg(x)-1; m = cgetg((N<<1)+1,t_MAT);
     951      627271 :   for (i=1; i<=N; i++) gel(m,i)   = ZM_ZC_mul(alpha,gel(x,i));
     952      627271 :   for (i=1; i<=N; i++) gel(m,i+N) = ZC_Z_mul(gel(x,i), a);
     953      627271 :   return ZM_hnfmodid(m, mulii(a, gcoeff(x,1,1)));
     954             : }
     955             : 
     956             : /* Assume ix and iy are integral in HNF form [NOT extended]. Not memory clean.
     957             :  * HACK: ideal in iy can be of the form [a,b], a in Z, b in Z_K */
     958             : GEN
     959      309051 : idealHNF_mul(GEN nf, GEN x, GEN y)
     960             : {
     961             :   GEN z;
     962      309051 :   if (typ(y) == t_VEC)
     963      211126 :     z = idealHNF_mul_two(nf,x,y);
     964             :   else
     965             :   { /* reduce one ideal to two-elt form. The smallest */
     966       97925 :     GEN xZ = gcoeff(x,1,1), yZ = gcoeff(y,1,1);
     967       97925 :     if (cmpii(xZ, yZ) < 0)
     968             :     {
     969       34872 :       if (is_pm1(xZ)) return gcopy(y);
     970       24553 :       z = idealHNF_mul_two(nf, y, mat_ideal_two_elt(nf,x));
     971             :     }
     972             :     else
     973             :     {
     974       63053 :       if (is_pm1(yZ)) return gcopy(x);
     975       39050 :       z = idealHNF_mul_two(nf, x, mat_ideal_two_elt(nf,y));
     976             :     }
     977             :   }
     978      274729 :   return z;
     979             : }
     980             : 
     981             : /* operations on elements in factored form */
     982             : 
     983             : GEN
     984       93859 : famat_mul_shallow(GEN f, GEN g)
     985             : {
     986       93859 :   if (typ(f) != t_MAT) f = to_famat_shallow(f,gen_1);
     987       93859 :   if (typ(g) != t_MAT) g = to_famat_shallow(g,gen_1);
     988       93859 :   if (lg(f) == 1) return g;
     989       74369 :   if (lg(g) == 1) return f;
     990      146010 :   return mkmat2(shallowconcat(gel(f,1), gel(g,1)),
     991      146010 :                 shallowconcat(gel(f,2), gel(g,2)));
     992             : }
     993             : GEN
     994       59234 : famat_mulpow_shallow(GEN f, GEN g, GEN e)
     995             : {
     996       59234 :   if (!signe(e)) return f;
     997       59108 :   return famat_mul_shallow(f, famat_pow_shallow(g, e));
     998             : }
     999             : 
    1000             : GEN
    1001        4466 : famat_mulpows_shallow(GEN f, GEN g, long e)
    1002             : {
    1003        4466 :   if (e==0) return f;
    1004        2807 :   return famat_mul_shallow(f, famat_pows_shallow(g, e));
    1005             : }
    1006             : 
    1007             : GEN
    1008           0 : to_famat(GEN x, GEN y) { retmkmat2(mkcolcopy(x), mkcolcopy(y)); }
    1009             : GEN
    1010      935529 : to_famat_shallow(GEN x, GEN y) { return mkmat2(mkcol(x), mkcol(y)); }
    1011             : 
    1012             : /* concat the single elt x; not gconcat since x may be a t_COL */
    1013             : static GEN
    1014       59742 : append(GEN v, GEN x)
    1015             : {
    1016       59742 :   long i, l = lg(v);
    1017       59742 :   GEN w = cgetg(l+1, typ(v));
    1018       59742 :   for (i=1; i<l; i++) gel(w,i) = gcopy(gel(v,i));
    1019       59742 :   gel(w,i) = gcopy(x); return w;
    1020             : }
    1021             : /* add x^1 to famat f */
    1022             : static GEN
    1023       86402 : famat_add(GEN f, GEN x)
    1024             : {
    1025       86402 :   GEN h = cgetg(3,t_MAT);
    1026       86402 :   if (lg(f) == 1)
    1027             :   {
    1028       26660 :     gel(h,1) = mkcolcopy(x);
    1029       26660 :     gel(h,2) = mkcol(gen_1);
    1030             :   }
    1031             :   else
    1032             :   {
    1033       59742 :     gel(h,1) = append(gel(f,1), x);
    1034       59742 :     gel(h,2) = gconcat(gel(f,2), gen_1);
    1035             :   }
    1036       86402 :   return h;
    1037             : }
    1038             : 
    1039             : GEN
    1040      110525 : famat_mul(GEN f, GEN g)
    1041             : {
    1042             :   GEN h;
    1043      110525 :   if (typ(g) != t_MAT) {
    1044       86402 :     if (typ(f) == t_MAT) return famat_add(f, g);
    1045           0 :     h = cgetg(3, t_MAT);
    1046           0 :     gel(h,1) = mkcol2(gcopy(f), gcopy(g));
    1047           0 :     gel(h,2) = mkcol2(gen_1, gen_1);
    1048             :   }
    1049       24123 :   if (typ(f) != t_MAT) return famat_add(g, f);
    1050       24123 :   if (lg(f) == 1) return gcopy(g);
    1051        4382 :   if (lg(g) == 1) return gcopy(f);
    1052        1897 :   h = cgetg(3,t_MAT);
    1053        1897 :   gel(h,1) = gconcat(gel(f,1), gel(g,1));
    1054        1897 :   gel(h,2) = gconcat(gel(f,2), gel(g,2));
    1055        1897 :   return h;
    1056             : }
    1057             : 
    1058             : GEN
    1059       51020 : famat_sqr(GEN f)
    1060             : {
    1061             :   GEN h;
    1062       51020 :   if (lg(f) == 1) return cgetg(1,t_MAT);
    1063       25409 :   if (typ(f) != t_MAT) return to_famat(f,gen_2);
    1064       25409 :   h = cgetg(3,t_MAT);
    1065       25409 :   gel(h,1) = gcopy(gel(f,1));
    1066       25409 :   gel(h,2) = gmul2n(gel(f,2),1);
    1067       25409 :   return h;
    1068             : }
    1069             : 
    1070             : GEN
    1071       27020 : famat_inv_shallow(GEN f)
    1072             : {
    1073       27020 :   if (lg(f) == 1) return f;
    1074       27020 :   if (typ(f) != t_MAT) return to_famat_shallow(f,gen_m1);
    1075          14 :   return mkmat2(gel(f,1), ZC_neg(gel(f,2)));
    1076             : }
    1077             : GEN
    1078       11084 : famat_inv(GEN f)
    1079             : {
    1080       11084 :   if (lg(f) == 1) return cgetg(1,t_MAT);
    1081        4156 :   if (typ(f) != t_MAT) return to_famat(f,gen_m1);
    1082        4156 :   retmkmat2(gcopy(gel(f,1)), ZC_neg(gel(f,2)));
    1083             : }
    1084             : GEN
    1085        1177 : famat_pow(GEN f, GEN n)
    1086             : {
    1087        1177 :   if (lg(f) == 1) return cgetg(1,t_MAT);
    1088           0 :   if (typ(f) != t_MAT) return to_famat(f,n);
    1089           0 :   retmkmat2(gcopy(gel(f,1)), ZC_Z_mul(gel(f,2),n));
    1090             : }
    1091             : GEN
    1092       59108 : famat_pow_shallow(GEN f, GEN n)
    1093             : {
    1094       59108 :   if (is_pm1(n)) return signe(n) > 0? f: famat_inv_shallow(f);
    1095       30296 :   if (lg(f) == 1) return f;
    1096       30296 :   if (typ(f) != t_MAT) return to_famat_shallow(f,n);
    1097         168 :   return mkmat2(gel(f,1), ZC_Z_mul(gel(f,2),n));
    1098             : }
    1099             : 
    1100             : GEN
    1101        2807 : famat_pows_shallow(GEN f, long n)
    1102             : {
    1103        2807 :   if (n==1) return f;
    1104        1344 :   if (n==-1) return famat_inv_shallow(f);
    1105        1211 :   if (lg(f) == 1) return f;
    1106        1211 :   if (typ(f) != t_MAT) return to_famat_shallow(f, stoi(n));
    1107        1106 :   return mkmat2(gel(f,1), ZC_z_mul(gel(f,2),n));
    1108             : }
    1109             : 
    1110             : GEN
    1111           0 : famat_Z_gcd(GEN M, GEN n)
    1112             : {
    1113           0 :   pari_sp av=avma;
    1114           0 :   long i, j, l=lgcols(M);
    1115           0 :   GEN F=cgetg(3,t_MAT);
    1116           0 :   gel(F,1)=cgetg(l,t_COL);
    1117           0 :   gel(F,2)=cgetg(l,t_COL);
    1118           0 :   for (i=1, j=1; i<l; i++)
    1119             :   {
    1120           0 :     GEN p = gcoeff(M,i,1);
    1121           0 :     GEN e = gminsg(Z_pval(n,p),gcoeff(M,i,2));
    1122           0 :     if (signe(e))
    1123             :     {
    1124           0 :       gcoeff(F,j,1)=p;
    1125           0 :       gcoeff(F,j,2)=e;
    1126           0 :       j++;
    1127             :     }
    1128             :   }
    1129           0 :   setlg(gel(F,1),j); setlg(gel(F,2),j);
    1130           0 :   return gerepilecopy(av,F);
    1131             : }
    1132             : 
    1133             : /* x assumed to be a t_MATs (factorization matrix), or compatible with
    1134             :  * the element_* functions. */
    1135             : static GEN
    1136       61590 : ext_sqr(GEN nf, GEN x)
    1137       61590 : { return (typ(x)==t_MAT)? famat_sqr(x): nfsqr(nf, x); }
    1138             : static GEN
    1139      145231 : ext_mul(GEN nf, GEN x, GEN y)
    1140      145231 : { return (typ(x)==t_MAT)? famat_mul(x,y): nfmul(nf, x, y); }
    1141             : static GEN
    1142       10944 : ext_inv(GEN nf, GEN x)
    1143       10944 : { return (typ(x)==t_MAT)? famat_inv(x): nfinv(nf, x); }
    1144             : static GEN
    1145        1177 : ext_pow(GEN nf, GEN x, GEN n)
    1146        1177 : { return (typ(x)==t_MAT)? famat_pow(x,n): nfpow(nf, x, n); }
    1147             : 
    1148             : GEN
    1149           0 : famat_to_nf(GEN nf, GEN f)
    1150             : {
    1151             :   GEN t, x, e;
    1152             :   long i;
    1153           0 :   if (lg(f) == 1) return gen_1;
    1154             : 
    1155           0 :   x = gel(f,1);
    1156           0 :   e = gel(f,2);
    1157           0 :   t = nfpow(nf, gel(x,1), gel(e,1));
    1158           0 :   for (i=lg(x)-1; i>1; i--)
    1159           0 :     t = nfmul(nf, t, nfpow(nf, gel(x,i), gel(e,i)));
    1160           0 :   return t;
    1161             : }
    1162             : 
    1163             : GEN
    1164       17983 : famat_reduce(GEN fa)
    1165             : {
    1166             :   GEN E, G, L, g, e;
    1167             :   long i, k, l;
    1168             : 
    1169       17983 :   if (lg(fa) == 1) return fa;
    1170       15428 :   g = gel(fa,1); l = lg(g);
    1171       15428 :   e = gel(fa,2);
    1172       15428 :   L = gen_indexsort(g, (void*)&cmp_universal, &cmp_nodata);
    1173       15428 :   G = cgetg(l, t_COL);
    1174       15428 :   E = cgetg(l, t_COL);
    1175             :   /* merge */
    1176       37842 :   for (k=i=1; i<l; i++,k++)
    1177             :   {
    1178       22414 :     gel(G,k) = gel(g,L[i]);
    1179       22414 :     gel(E,k) = gel(e,L[i]);
    1180       22414 :     if (k > 1 && gidentical(gel(G,k), gel(G,k-1)))
    1181             :     {
    1182         749 :       gel(E,k-1) = addii(gel(E,k), gel(E,k-1));
    1183         749 :       k--;
    1184             :     }
    1185             :   }
    1186             :   /* kill 0 exponents */
    1187       15428 :   l = k;
    1188       37093 :   for (k=i=1; i<l; i++)
    1189       21665 :     if (!gequal0(gel(E,i)))
    1190             :     {
    1191       20965 :       gel(G,k) = gel(G,i);
    1192       20965 :       gel(E,k) = gel(E,i); k++;
    1193             :     }
    1194       15428 :   setlg(G, k);
    1195       15428 :   setlg(E, k); return mkmat2(G,E);
    1196             : }
    1197             : 
    1198             : GEN
    1199       12718 : famatsmall_reduce(GEN fa)
    1200             : {
    1201             :   GEN E, G, L, g, e;
    1202             :   long i, k, l;
    1203       12718 :   if (lg(fa) == 1) return fa;
    1204       12718 :   g = gel(fa,1); l = lg(g);
    1205       12718 :   e = gel(fa,2);
    1206       12718 :   L = vecsmall_indexsort(g);
    1207       12718 :   G = cgetg(l, t_VECSMALL);
    1208       12718 :   E = cgetg(l, t_VECSMALL);
    1209             :   /* merge */
    1210      113827 :   for (k=i=1; i<l; i++,k++)
    1211             :   {
    1212      101109 :     G[k] = g[L[i]];
    1213      101109 :     E[k] = e[L[i]];
    1214      101109 :     if (k > 1 && G[k] == G[k-1])
    1215             :     {
    1216        5915 :       E[k-1] += E[k];
    1217        5915 :       k--;
    1218             :     }
    1219             :   }
    1220             :   /* kill 0 exponents */
    1221       12718 :   l = k;
    1222      107912 :   for (k=i=1; i<l; i++)
    1223       95194 :     if (E[i])
    1224             :     {
    1225       92254 :       G[k] = G[i];
    1226       92254 :       E[k] = E[i]; k++;
    1227             :     }
    1228       12718 :   setlg(G, k);
    1229       12718 :   setlg(E, k); return mkmat2(G,E);
    1230             : }
    1231             : 
    1232             : GEN
    1233       55930 : ZM_famat_limit(GEN fa, GEN limit)
    1234             : {
    1235             :   pari_sp av;
    1236             :   GEN E, G, g, e, r;
    1237             :   long i, k, l, n, lG;
    1238             : 
    1239       55930 :   if (lg(fa) == 1) return fa;
    1240       55930 :   g = gel(fa,1); l = lg(g);
    1241       55930 :   e = gel(fa,2);
    1242      124152 :   for(n=0, i=1; i<l; i++)
    1243       68222 :     if (cmpii(gel(g,i),limit)<=0) n++;
    1244       55930 :   lG = n<l-1 ? n+2 : n+1;
    1245       55930 :   G = cgetg(lG, t_COL);
    1246       55930 :   E = cgetg(lG, t_COL);
    1247       55930 :   av = avma;
    1248      124152 :   for (i=1, k=1, r = gen_1; i<l; i++)
    1249             :   {
    1250       68222 :     if (cmpii(gel(g,i),limit)<=0)
    1251             :     {
    1252       68138 :       gel(G,k) = gel(g,i);
    1253       68138 :       gel(E,k) = gel(e,i);
    1254       68138 :       k++;
    1255          84 :     } else r = mulii(r, powii(gel(g,i), gel(e,i)));
    1256             :   }
    1257       55930 :   if (k<i)
    1258             :   {
    1259          84 :     gel(G, k) = gerepileuptoint(av, r);
    1260          84 :     gel(E, k) = gen_1;
    1261             :   }
    1262       55930 :   return mkmat2(G,E);
    1263             : }
    1264             : 
    1265             : /* assume pr has degree 1 and coprime to Q_denom(x) */
    1266             : static GEN
    1267        5117 : to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1268             : {
    1269        5117 :   GEN d, r, p = modpr_get_p(modpr);
    1270        5117 :   x = nf_to_scalar_or_basis(nf,x);
    1271        5117 :   if (typ(x) != t_COL) return Rg_to_Fp(x,p);
    1272        4753 :   x = Q_remove_denom(x, &d);
    1273        4753 :   r = zk_to_Fq(x, modpr);
    1274        4753 :   if (d) r = Fp_div(r, d, p);
    1275        4753 :   return r;
    1276             : }
    1277             : 
    1278             : /* pr coprime to all denominators occurring in x */
    1279             : static GEN
    1280         789 : famat_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1281             : {
    1282         789 :   GEN p = modpr_get_p(modpr);
    1283         789 :   GEN t = NULL, g = gel(x,1), e = gel(x,2), q = subiu(p,1);
    1284         789 :   long i, l = lg(g);
    1285        2433 :   for (i = 1; i < l; i++)
    1286             :   {
    1287        1644 :     GEN n = modii(gel(e,i), q);
    1288        1644 :     if (signe(n))
    1289             :     {
    1290        1644 :       GEN h = to_Fp_coprime(nf, gel(g,i), modpr);
    1291        1644 :       h = Fp_pow(h, n, p);
    1292        1644 :       t = t? Fp_mul(t, h, p): h;
    1293             :     }
    1294             :   }
    1295         789 :   return t? modii(t, p): gen_1;
    1296             : }
    1297             : 
    1298             : /* cf famat_to_nf_modideal_coprime, modpr attached to prime of degree 1 */
    1299             : GEN
    1300        4262 : nf_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1301             : {
    1302        8524 :   return typ(x)==t_MAT? famat_to_Fp_coprime(nf, x, modpr)
    1303        4262 :                       : to_Fp_coprime(nf, x, modpr);
    1304             : }
    1305             : 
    1306             : static long
    1307      135997 : zk_pvalrem(GEN x, GEN p, GEN *py)
    1308      135997 : { return (typ(x) == t_INT)? Z_pvalrem(x, p, py): ZV_pvalrem(x, p, py); }
    1309             : /* x a QC or Q. Return a ZC or Z, whose content is coprime to Z. Set v, dx
    1310             :  * such that x = p^v (newx / dx); dx = NULL if 1 */
    1311             : static GEN
    1312      263600 : nf_remove_denom_p(GEN nf, GEN x, GEN p, GEN *pdx, long *pv)
    1313             : {
    1314             :   long vcx;
    1315             :   GEN dx;
    1316      263600 :   x = nf_to_scalar_or_basis(nf, x);
    1317      263600 :   x = Q_remove_denom(x, &dx);
    1318      263600 :   if (dx)
    1319             :   {
    1320      170940 :     vcx = - Z_pvalrem(dx, p, &dx);
    1321      170940 :     if (!vcx) vcx = zk_pvalrem(x, p, &x);
    1322      170940 :     if (isint1(dx)) dx = NULL;
    1323             :   }
    1324             :   else
    1325             :   {
    1326       92660 :     vcx = zk_pvalrem(x, p, &x);
    1327       92660 :     dx = NULL;
    1328             :   }
    1329      263600 :   *pv = vcx;
    1330      263600 :   *pdx = dx; return x;
    1331             : }
    1332             : /* x = b^e/p^(e-1) in Z_K; x = 0 mod p/pr^e, (x,pr) = 1. Return NULL
    1333             :  * if p inert (instead of 1) */
    1334             : static GEN
    1335       62069 : p_makecoprime(GEN pr)
    1336             : {
    1337       62069 :   GEN B = pr_get_tau(pr), b;
    1338             :   long i, e;
    1339             : 
    1340       62069 :   if (typ(B) == t_INT) return NULL;
    1341       61929 :   b = gel(B,1); /* B = multiplication table by b */
    1342       61929 :   e = pr_get_e(pr);
    1343       61929 :   if (e == 1) return b;
    1344             :   /* one could also divide (exactly) by p in each iteration */
    1345       17150 :   for (i = 1; i < e; i++) b = ZM_ZC_mul(B, b);
    1346       17150 :   return ZC_Z_divexact(b, powiu(pr_get_p(pr), e-1));
    1347             : }
    1348             : 
    1349             : /* Compute A = prod g[i]^e[i] mod pr^k, assuming (A, pr) = 1.
    1350             :  * Method: modify each g[i] so that it becomes coprime to pr,
    1351             :  * g[i] *= (b/p)^v_pr(g[i]), where b/p = pr^(-1) times something integral
    1352             :  * and prime to p; globally, we multiply by (b/p)^v_pr(A) = 1.
    1353             :  * Optimizations:
    1354             :  * 1) remove all powers of p from contents, and consider extra generator p^vp;
    1355             :  * modified as p * (b/p)^e = b^e / p^(e-1)
    1356             :  * 2) remove denominators, coprime to p, by multiplying by inverse mod prk\cap Z
    1357             :  *
    1358             :  * EX = multiple of exponent of (O_K / pr^k)^* used to reduce the product in
    1359             :  * case the e[i] are large */
    1360             : GEN
    1361      111640 : famat_makecoprime(GEN nf, GEN g, GEN e, GEN pr, GEN prk, GEN EX)
    1362             : {
    1363      111640 :   GEN G, E, t, vp = NULL, p = pr_get_p(pr), prkZ = gcoeff(prk, 1,1);
    1364      111640 :   long i, l = lg(g);
    1365             : 
    1366      111640 :   G = cgetg(l+1, t_VEC);
    1367      111640 :   E = cgetg(l+1, t_VEC); /* l+1: room for "modified p" */
    1368      375240 :   for (i=1; i < l; i++)
    1369             :   {
    1370             :     long vcx;
    1371      263600 :     GEN dx, x = nf_remove_denom_p(nf, gel(g,i), p, &dx, &vcx);
    1372      263600 :     if (vcx) /* = v_p(content(g[i])) */
    1373             :     {
    1374      129262 :       GEN a = mulsi(vcx, gel(e,i));
    1375      129262 :       vp = vp? addii(vp, a): a;
    1376             :     }
    1377             :     /* x integral, content coprime to p; dx coprime to p */
    1378      263600 :     if (typ(x) == t_INT)
    1379             :     { /* x coprime to p, hence to pr */
    1380       38602 :       x = modii(x, prkZ);
    1381       38602 :       if (dx) x = Fp_div(x, dx, prkZ);
    1382             :     }
    1383             :     else
    1384             :     {
    1385      224998 :       (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
    1386      224998 :       x = ZC_hnfrem(FpC_red(x,prkZ), prk);
    1387      224998 :       if (dx) x = FpC_Fp_mul(x, Fp_inv(dx,prkZ), prkZ);
    1388             :     }
    1389      263600 :     gel(G,i) = x;
    1390      263600 :     gel(E,i) = gel(e,i);
    1391             :   }
    1392             : 
    1393      111640 :   t = vp? p_makecoprime(pr): NULL;
    1394      111640 :   if (!t)
    1395             :   { /* no need for extra generator */
    1396       49711 :     setlg(G,l);
    1397       49711 :     setlg(E,l);
    1398             :   }
    1399             :   else
    1400             :   {
    1401       61929 :     gel(G,i) = FpC_red(t, prkZ);
    1402       61929 :     gel(E,i) = vp;
    1403             :   }
    1404      111640 :   return famat_to_nf_modideal_coprime(nf, G, E, prk, EX);
    1405             : }
    1406             : 
    1407             : /* prod g[i]^e[i] mod bid, assume (g[i], id) = 1 */
    1408             : GEN
    1409       10983 : famat_to_nf_moddivisor(GEN nf, GEN g, GEN e, GEN bid)
    1410             : {
    1411             :   GEN t, cyc;
    1412       10983 :   if (lg(g) == 1) return gen_1;
    1413       10983 :   cyc = bid_get_cyc(bid);
    1414       10983 :   if (lg(cyc) == 1)
    1415           0 :     t = gen_1;
    1416             :   else
    1417       10983 :     t = famat_to_nf_modideal_coprime(nf, g, e, bid_get_ideal(bid), gel(cyc,1));
    1418       10983 :   return set_sign_mod_divisor(nf, mkmat2(g,e), t, bid_get_sarch(bid));
    1419             : }
    1420             : 
    1421             : GEN
    1422      185794 : vecmul(GEN x, GEN y)
    1423             : {
    1424      185794 :   if (is_scalar_t(typ(x))) return gmul(x,y);
    1425       16373 :   pari_APPLY_same(vecmul(gel(x,i), gel(y,i)))
    1426             : }
    1427             : 
    1428             : GEN
    1429           0 : vecinv(GEN x)
    1430             : {
    1431           0 :   if (is_scalar_t(typ(x))) return ginv(x);
    1432           0 :   pari_APPLY_same(vecinv(gel(x,i)))
    1433             : }
    1434             : 
    1435             : GEN
    1436       15729 : vecpow(GEN x, GEN n)
    1437             : {
    1438       15729 :   if (is_scalar_t(typ(x))) return powgi(x,n);
    1439        4270 :   pari_APPLY_same(vecpow(gel(x,i), n))
    1440             : }
    1441             : 
    1442             : GEN
    1443         903 : vecdiv(GEN x, GEN y)
    1444             : {
    1445         903 :   if (is_scalar_t(typ(x))) return gdiv(x,y);
    1446         301 :   pari_APPLY_same(vecdiv(gel(x,i), gel(y,i)))
    1447             : }
    1448             : 
    1449             : /* A ideal as a square t_MAT */
    1450             : static GEN
    1451      196592 : idealmulelt(GEN nf, GEN x, GEN A)
    1452             : {
    1453             :   long i, lx;
    1454             :   GEN dx, dA, D;
    1455      196592 :   if (lg(A) == 1) return cgetg(1, t_MAT);
    1456      196592 :   x = nf_to_scalar_or_basis(nf,x);
    1457      196592 :   if (typ(x) != t_COL)
    1458       67437 :     return isintzero(x)? cgetg(1,t_MAT): RgM_Rg_mul(A, Q_abs_shallow(x));
    1459      129155 :   x = Q_remove_denom(x, &dx);
    1460      129155 :   A = Q_remove_denom(A, &dA);
    1461      129155 :   x = zk_multable(nf, x);
    1462      129155 :   D = mulii(zkmultable_capZ(x), gcoeff(A,1,1));
    1463      129155 :   x = zkC_multable_mul(A, x);
    1464      129155 :   settyp(x, t_MAT); lx = lg(x);
    1465             :   /* x may contain scalars (at most 1 since the ideal is non-0)*/
    1466      446469 :   for (i=1; i<lx; i++)
    1467      325797 :     if (typ(gel(x,i)) == t_INT)
    1468             :     {
    1469        8483 :       if (i > 1) swap(gel(x,1), gel(x,i)); /* help HNF */
    1470        8483 :       gel(x,1) = scalarcol_shallow(gel(x,1), lx-1);
    1471        8483 :       break;
    1472             :     }
    1473      129155 :   x = ZM_hnfmodid(x, D);
    1474      129155 :   dx = mul_denom(dx,dA);
    1475      129155 :   return dx? gdiv(x,dx): x;
    1476             : }
    1477             : 
    1478             : /* nf a true nf, tx <= ty */
    1479             : static GEN
    1480      640900 : idealmul_aux(GEN nf, GEN x, GEN y, long tx, long ty)
    1481             : {
    1482             :   GEN z, cx, cy;
    1483      640900 :   switch(tx)
    1484             :   {
    1485             :     case id_PRINCIPAL:
    1486      245808 :       switch(ty)
    1487             :       {
    1488             :         case id_PRINCIPAL:
    1489       49090 :           return idealhnf_principal(nf, nfmul(nf,x,y));
    1490             :         case id_PRIME:
    1491             :         {
    1492         126 :           GEN p = pr_get_p(y), pi = pr_get_gen(y), cx;
    1493         126 :           if (pr_is_inert(y)) return RgM_Rg_mul(idealhnf_principal(nf,x),p);
    1494             : 
    1495          42 :           x = nf_to_scalar_or_basis(nf, x);
    1496          42 :           switch(typ(x))
    1497             :           {
    1498             :             case t_INT:
    1499          28 :               if (!signe(x)) return cgetg(1,t_MAT);
    1500          28 :               return ZM_Z_mul(pr_hnf(nf,y), absi_shallow(x));
    1501             :             case t_FRAC:
    1502           7 :               return RgM_Rg_mul(pr_hnf(nf,y), Q_abs_shallow(x));
    1503             :           }
    1504             :           /* t_COL */
    1505           7 :           x = Q_primitive_part(x, &cx);
    1506           7 :           x = zk_multable(nf, x);
    1507           7 :           z = shallowconcat(ZM_Z_mul(x,p), ZM_ZC_mul(x,pi));
    1508           7 :           z = ZM_hnfmodid(z, mulii(p, zkmultable_capZ(x)));
    1509           7 :           return cx? ZM_Q_mul(z, cx): z;
    1510             :         }
    1511             :         default: /* id_MAT */
    1512      196592 :           return idealmulelt(nf, x,y);
    1513             :       }
    1514             :     case id_PRIME:
    1515      321385 :       if (ty==id_PRIME)
    1516      299409 :       { y = pr_hnf(nf,y); cy = NULL; }
    1517             :       else
    1518       21976 :         y = Q_primitive_part(y, &cy);
    1519      321385 :       y = idealHNF_mul_two(nf,y,x);
    1520      321385 :       return cy? ZM_Q_mul(y,cy): y;
    1521             : 
    1522             :     default: /* id_MAT */
    1523             :     {
    1524       73707 :       long N = nf_get_degree(nf);
    1525       73707 :       if (lg(x)-1 != N || lg(y)-1 != N) pari_err_DIM("idealmul");
    1526       73693 :       x = Q_primitive_part(x, &cx);
    1527       73693 :       y = Q_primitive_part(y, &cy); cx = mul_content(cx,cy);
    1528       73693 :       y = idealHNF_mul(nf,x,y);
    1529       73693 :       return cx? ZM_Q_mul(y,cx): y;
    1530             :     }
    1531             :   }
    1532             : }
    1533             : 
    1534             : /* output the ideal product ix.iy */
    1535             : GEN
    1536      640900 : idealmul(GEN nf, GEN x, GEN y)
    1537             : {
    1538             :   pari_sp av;
    1539             :   GEN res, ax, ay, z;
    1540      640900 :   long tx = idealtyp(&x,&ax);
    1541      640900 :   long ty = idealtyp(&y,&ay), f;
    1542      640900 :   if (tx>ty) { swap(ax,ay); swap(x,y); lswap(tx,ty); }
    1543      640900 :   f = (ax||ay); res = f? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1544      640900 :   av = avma;
    1545      640900 :   z = gerepileupto(av, idealmul_aux(checknf(nf), x,y, tx,ty));
    1546      640886 :   if (!f) return z;
    1547       42696 :   if (ax && ay)
    1548       41021 :     ax = ext_mul(nf, ax, ay);
    1549             :   else
    1550        1675 :     ax = gcopy(ax? ax: ay);
    1551       42696 :   gel(res,1) = z; gel(res,2) = ax; return res;
    1552             : }
    1553             : 
    1554             : /* Return x, integral in 2-elt form, such that pr^2 = c * x. cf idealpowprime
    1555             :  * nf = true nf */
    1556             : static GEN
    1557       44641 : idealsqrprime(GEN nf, GEN pr, GEN *pc)
    1558             : {
    1559       44641 :   GEN p = pr_get_p(pr), q, gen;
    1560       44641 :   long e = pr_get_e(pr), f = pr_get_f(pr);
    1561             : 
    1562       44641 :   q = (e == 1)? sqri(p): p;
    1563       44641 :   if (e <= 2 && e * f == nf_get_degree(nf))
    1564             :   { /* pr^e = (p) */
    1565        9947 :     *pc = q;
    1566        9947 :     return mkvec2(gen_1,gen_0);
    1567             :   }
    1568       34694 :   gen = nfsqr(nf, pr_get_gen(pr));
    1569       34694 :   gen = FpC_red(gen, q);
    1570       34694 :   *pc = NULL;
    1571       34694 :   return mkvec2(q, gen);
    1572             : }
    1573             : /* cf idealpow_aux */
    1574             : static GEN
    1575       62353 : idealsqr_aux(GEN nf, GEN x, long tx)
    1576             : {
    1577       62353 :   GEN T = nf_get_pol(nf), m, cx, a, alpha;
    1578       62353 :   long N = degpol(T);
    1579       62353 :   switch(tx)
    1580             :   {
    1581             :     case id_PRINCIPAL:
    1582          77 :       return idealhnf_principal(nf, nfsqr(nf,x));
    1583             :     case id_PRIME:
    1584       23474 :       if (pr_is_inert(x)) return scalarmat(sqri(gel(x,1)), N);
    1585       23306 :       x = idealsqrprime(nf, x, &cx);
    1586       23306 :       x = idealhnf_two(nf,x);
    1587       23306 :       return cx? ZM_Z_mul(x, cx): x;
    1588             :     default:
    1589       38802 :       x = Q_primitive_part(x, &cx);
    1590       38802 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    1591       38802 :       alpha = nfsqr(nf,alpha);
    1592       38802 :       m = zk_scalar_or_multable(nf, alpha);
    1593       38802 :       if (typ(m) == t_INT) {
    1594        1456 :         x = gcdii(sqri(a), m);
    1595        1456 :         if (cx) x = gmul(x, gsqr(cx));
    1596        1456 :         x = scalarmat(x, N);
    1597             :       }
    1598             :       else
    1599             :       {
    1600       37346 :         x = ZM_hnfmodid(m, gcdii(sqri(a), zkmultable_capZ(m)));
    1601       37346 :         if (cx) cx = gsqr(cx);
    1602       37346 :         if (cx) x = ZM_Q_mul(x, cx);
    1603             :       }
    1604       38802 :       return x;
    1605             :   }
    1606             : }
    1607             : GEN
    1608       62353 : idealsqr(GEN nf, GEN x)
    1609             : {
    1610             :   pari_sp av;
    1611             :   GEN res, ax, z;
    1612       62353 :   long tx = idealtyp(&x,&ax);
    1613       62353 :   res = ax? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1614       62353 :   av = avma;
    1615       62353 :   z = gerepileupto(av, idealsqr_aux(checknf(nf), x, tx));
    1616       62353 :   if (!ax) return z;
    1617       61590 :   gel(res,1) = z;
    1618       61590 :   gel(res,2) = ext_sqr(nf, ax); return res;
    1619             : }
    1620             : 
    1621             : /* norm of an ideal */
    1622             : GEN
    1623        6440 : idealnorm(GEN nf, GEN x)
    1624             : {
    1625             :   pari_sp av;
    1626             :   GEN y, T;
    1627             :   long tx;
    1628             : 
    1629        6440 :   switch(idealtyp(&x,&y))
    1630             :   {
    1631         175 :     case id_PRIME: return pr_norm(x);
    1632        4207 :     case id_MAT: return RgM_det_triangular(x);
    1633             :   }
    1634             :   /* id_PRINCIPAL */
    1635        2058 :   nf = checknf(nf); T = nf_get_pol(nf); av = avma;
    1636        2058 :   x = nf_to_scalar_or_alg(nf, x);
    1637        2058 :   x = (typ(x) == t_POL)? RgXQ_norm(x, T): gpowgs(x, degpol(T));
    1638        2058 :   tx = typ(x);
    1639        2058 :   if (tx == t_INT) return gerepileuptoint(av, absi(x));
    1640         532 :   if (tx != t_FRAC) pari_err_TYPE("idealnorm",x);
    1641         532 :   return gerepileupto(av, Q_abs(x));
    1642             : }
    1643             : 
    1644             : /* I^(-1) = { x \in K, Tr(x D^(-1) I) \in Z }, D different of K/Q
    1645             :  *
    1646             :  * nf[5][6] = pp( D^(-1) ) = pp( HNF( T^(-1) ) ), T = (Tr(wi wj))
    1647             :  * nf[5][7] = same in 2-elt form.
    1648             :  * Assume I integral. Return the integral ideal (I\cap Z) I^(-1) */
    1649             : GEN
    1650      191606 : idealHNF_inv_Z(GEN nf, GEN I)
    1651             : {
    1652      191606 :   GEN J, dual, IZ = gcoeff(I,1,1); /* I \cap Z */
    1653      191606 :   if (isint1(IZ)) return matid(lg(I)-1);
    1654      180532 :   J = idealHNF_mul(nf,I, gmael(nf,5,7));
    1655             :  /* I in HNF, hence easily inverted; multiply by IZ to get integer coeffs
    1656             :   * missing content cancels while solving the linear equation */
    1657      180532 :   dual = shallowtrans( hnf_divscale(J, gmael(nf,5,6), IZ) );
    1658      180532 :   return ZM_hnfmodid(dual, IZ);
    1659             : }
    1660             : /* I HNF with rational coefficients (denominator d). */
    1661             : GEN
    1662       55633 : idealHNF_inv(GEN nf, GEN I)
    1663             : {
    1664       55633 :   GEN J, IQ = gcoeff(I,1,1); /* I \cap Q; d IQ = dI \cap Z */
    1665       55633 :   J = idealHNF_inv_Z(nf, Q_remove_denom(I, NULL)); /* = (dI)^(-1) * (d IQ) */
    1666       55633 :   return equali1(IQ)? J: RgM_Rg_div(J, IQ);
    1667             : }
    1668             : 
    1669             : /* return p * P^(-1)  [integral] */
    1670             : GEN
    1671       24473 : pr_inv_p(GEN pr)
    1672             : {
    1673       24473 :   if (pr_is_inert(pr)) return matid(pr_get_f(pr));
    1674       23983 :   return ZM_hnfmodid(pr_get_tau(pr), pr_get_p(pr));
    1675             : }
    1676             : GEN
    1677        3584 : pr_inv(GEN pr)
    1678             : {
    1679        3584 :   GEN p = pr_get_p(pr);
    1680        3584 :   if (pr_is_inert(pr)) return scalarmat(ginv(p), pr_get_f(pr));
    1681        3234 :   return RgM_Rg_div(ZM_hnfmodid(pr_get_tau(pr),p), p);
    1682             : }
    1683             : 
    1684             : GEN
    1685       95385 : idealinv(GEN nf, GEN x)
    1686             : {
    1687             :   GEN res, ax;
    1688             :   pari_sp av;
    1689       95385 :   long tx = idealtyp(&x,&ax), N;
    1690             : 
    1691       95385 :   res = ax? cgetg(3,t_VEC): NULL;
    1692       95385 :   nf = checknf(nf); av = avma;
    1693       95385 :   N = nf_get_degree(nf);
    1694       95385 :   switch (tx)
    1695             :   {
    1696             :     case id_MAT:
    1697       50201 :       if (lg(x)-1 != N) pari_err_DIM("idealinv");
    1698       50201 :       x = idealHNF_inv(nf,x); break;
    1699             :     case id_PRINCIPAL:
    1700       42454 :       x = nf_to_scalar_or_basis(nf, x);
    1701       42454 :       if (typ(x) != t_COL)
    1702       42412 :         x = idealhnf_principal(nf,ginv(x));
    1703             :       else
    1704             :       { /* nfinv + idealhnf where we already know (x) \cap Z */
    1705             :         GEN c, d;
    1706          42 :         x = Q_remove_denom(x, &c);
    1707          42 :         x = zk_inv(nf, x);
    1708          42 :         x = Q_remove_denom(x, &d); /* true inverse is c/d * x */
    1709          42 :         if (!d) /* x and x^(-1) integral => x a unit */
    1710           7 :           x = scalarmat_shallow(c? c: gen_1, N);
    1711             :         else
    1712             :         {
    1713          35 :           c = c? gdiv(c,d): ginv(d);
    1714          35 :           x = zk_multable(nf, x);
    1715          35 :           x = ZM_Q_mul(ZM_hnfmodid(x,d), c);
    1716             :         }
    1717             :       }
    1718       42454 :       break;
    1719             :     case id_PRIME:
    1720        2730 :       x = pr_inv(x); break;
    1721             :   }
    1722       95385 :   x = gerepileupto(av,x); if (!ax) return x;
    1723       10944 :   gel(res,1) = x;
    1724       10944 :   gel(res,2) = ext_inv(nf, ax); return res;
    1725             : }
    1726             : 
    1727             : /* write x = A/B, A,B coprime integral ideals */
    1728             : GEN
    1729       37086 : idealnumden(GEN nf, GEN x)
    1730             : {
    1731       37086 :   pari_sp av = avma;
    1732             :   GEN x0, ax, c, d, A, B, J;
    1733       37086 :   long tx = idealtyp(&x,&ax);
    1734       37086 :   nf = checknf(nf);
    1735       37086 :   switch (tx)
    1736             :   {
    1737             :     case id_PRIME:
    1738           7 :       retmkvec2(idealhnf(nf, x), gen_1);
    1739             :     case id_PRINCIPAL:
    1740             :     {
    1741             :       GEN xZ, mx;
    1742        2135 :       x = nf_to_scalar_or_basis(nf, x);
    1743        2135 :       switch(typ(x))
    1744             :       {
    1745          77 :         case t_INT: return gerepilecopy(av, mkvec2(absi(x),gen_1));
    1746          14 :         case t_FRAC:return gerepilecopy(av, mkvec2(absi(gel(x,1)), gel(x,2)));
    1747             :       }
    1748             :       /* t_COL */
    1749        2044 :       x = Q_remove_denom(x, &d);
    1750        2044 :       if (!d) return gerepilecopy(av, mkvec2(idealhnf(nf, x), gen_1));
    1751          21 :       mx = zk_multable(nf, x);
    1752          21 :       xZ = zkmultable_capZ(mx);
    1753          21 :       x = ZM_hnfmodid(mx, xZ); /* principal ideal (x) */
    1754          21 :       x0 = mkvec2(xZ, mx); /* same, for fast multiplication */
    1755          21 :       break;
    1756             :     }
    1757             :     default: /* id_MAT */
    1758             :     {
    1759       34944 :       long n = lg(x)-1;
    1760       34944 :       if (n == 0) return mkvec2(gen_0, gen_1);
    1761       34944 :       if (n != nf_get_degree(nf)) pari_err_DIM("idealnumden");
    1762       34944 :       x0 = x = Q_remove_denom(x, &d);
    1763       34944 :       if (!d) return gerepilecopy(av, mkvec2(x, gen_1));
    1764          14 :       break;
    1765             :     }
    1766             :   }
    1767          35 :   J = hnfmodid(x, d); /* = d/B */
    1768          35 :   c = gcoeff(J,1,1); /* (d/B) \cap Z, divides d */
    1769          35 :   B = idealHNF_inv_Z(nf, J); /* (d/B \cap Z) B/d */
    1770          35 :   if (!equalii(c,d)) B = ZM_Z_mul(B, diviiexact(d,c)); /* = B ! */
    1771          35 :   A = idealHNF_mul(nf, B, x0); /* d * (original x) * B = d A */
    1772          35 :   A = ZM_Z_divexact(A, d); /* = A ! */
    1773          35 :   return gerepilecopy(av, mkvec2(A, B));
    1774             : }
    1775             : 
    1776             : /* Return x, integral in 2-elt form, such that pr^n = c * x. Assume n != 0.
    1777             :  * nf = true nf */
    1778             : static GEN
    1779       88917 : idealpowprime(GEN nf, GEN pr, GEN n, GEN *pc)
    1780             : {
    1781       88917 :   GEN p = pr_get_p(pr), q, gen;
    1782             : 
    1783       88917 :   *pc = NULL;
    1784       88917 :   if (is_pm1(n)) /* n = 1 special cased for efficiency */
    1785             :   {
    1786       50738 :     q = p;
    1787       50738 :     if (typ(pr_get_tau(pr)) == t_INT) /* inert */
    1788             :     {
    1789           0 :       *pc = (signe(n) >= 0)? p: ginv(p);
    1790           0 :       return mkvec2(gen_1,gen_0);
    1791             :     }
    1792       50738 :     if (signe(n) >= 0) gen = pr_get_gen(pr);
    1793             :     else
    1794             :     {
    1795        8127 :       gen = pr_get_tau(pr); /* possibly t_MAT */
    1796        8127 :       *pc = ginv(p);
    1797             :     }
    1798             :   }
    1799       38179 :   else if (equalis(n,2)) return idealsqrprime(nf, pr, pc);
    1800             :   else
    1801             :   {
    1802       16844 :     long e = pr_get_e(pr), f = pr_get_f(pr);
    1803       16844 :     GEN r, m = truedvmdis(n, e, &r);
    1804       16844 :     if (e * f == nf_get_degree(nf))
    1805             :     { /* pr^e = (p) */
    1806        7770 :       if (signe(m)) *pc = powii(p,m);
    1807        7770 :       if (!signe(r)) return mkvec2(gen_1,gen_0);
    1808        3171 :       q = p;
    1809        3171 :       gen = nfpow(nf, pr_get_gen(pr), r);
    1810             :     }
    1811             :     else
    1812             :     {
    1813        9074 :       m = absi(m);
    1814        9074 :       if (signe(r)) m = addiu(m,1);
    1815        9074 :       q = powii(p,m); /* m = ceil(|n|/e) */
    1816        9074 :       if (signe(n) >= 0) gen = nfpow(nf, pr_get_gen(pr), n);
    1817             :       else
    1818             :       {
    1819        2219 :         gen = pr_get_tau(pr);
    1820        2219 :         if (typ(gen) == t_MAT) gen = gel(gen,1);
    1821        2219 :         n = negi(n);
    1822        2219 :         gen = ZC_Z_divexact(nfpow(nf, gen, n), powii(p, subii(n,m)));
    1823        2219 :         *pc = ginv(q);
    1824             :       }
    1825             :     }
    1826       12245 :     gen = FpC_red(gen, q);
    1827             :   }
    1828       62983 :   return mkvec2(q, gen);
    1829             : }
    1830             : 
    1831             : /* x * pr^n. Assume x in HNF or scalar (possibly non-integral) */
    1832             : GEN
    1833       67620 : idealmulpowprime(GEN nf, GEN x, GEN pr, GEN n)
    1834             : {
    1835             :   GEN c, cx, y;
    1836             :   long N;
    1837             : 
    1838       67620 :   nf = checknf(nf);
    1839       67620 :   N = nf_get_degree(nf);
    1840       67620 :   if (!signe(n)) return typ(x) == t_MAT? x: scalarmat_shallow(x, N);
    1841             : 
    1842             :   /* inert, special cased for efficiency */
    1843       67508 :   if (pr_is_inert(pr))
    1844             :   {
    1845        5551 :     GEN q = powii(pr_get_p(pr), n);
    1846        5551 :     return typ(x) == t_MAT? RgM_Rg_mul(x,q): scalarmat_shallow(gmul(x,q), N);
    1847             :   }
    1848             : 
    1849       61957 :   y = idealpowprime(nf, pr, n, &c);
    1850       61957 :   if (typ(x) == t_MAT)
    1851       60634 :   { x = Q_primitive_part(x, &cx); if (is_pm1(gcoeff(x,1,1))) x = NULL; }
    1852             :   else
    1853        1323 :   { cx = x; x = NULL; }
    1854       61957 :   cx = mul_content(c,cx);
    1855       61957 :   if (x)
    1856       34048 :     x = idealHNF_mul_two(nf,x,y);
    1857             :   else
    1858       27909 :     x = idealhnf_two(nf,y);
    1859       61957 :   if (cx) x = ZM_Q_mul(x,cx);
    1860       61957 :   return x;
    1861             : }
    1862             : GEN
    1863       13482 : idealdivpowprime(GEN nf, GEN x, GEN pr, GEN n)
    1864             : {
    1865       13482 :   return idealmulpowprime(nf,x,pr, negi(n));
    1866             : }
    1867             : 
    1868             : /* nf = true nf */
    1869             : static GEN
    1870      178459 : idealpow_aux(GEN nf, GEN x, long tx, GEN n)
    1871             : {
    1872      178459 :   GEN T = nf_get_pol(nf), m, cx, n1, a, alpha;
    1873      178459 :   long N = degpol(T), s = signe(n);
    1874      178459 :   if (!s) return matid(N);
    1875      172900 :   switch(tx)
    1876             :   {
    1877             :     case id_PRINCIPAL:
    1878          21 :       return idealhnf_principal(nf, nfpow(nf,x,n));
    1879             :     case id_PRIME:
    1880       70969 :       if (pr_is_inert(x)) return scalarmat(powii(gel(x,1), n), N);
    1881       26960 :       x = idealpowprime(nf, x, n, &cx);
    1882       26960 :       x = idealhnf_two(nf,x);
    1883       26960 :       return cx? ZM_Q_mul(x, cx): x;
    1884             :     default:
    1885      101910 :       if (is_pm1(n)) return (s < 0)? idealinv(nf, x): gcopy(x);
    1886       55736 :       n1 = (s < 0)? negi(n): n;
    1887             : 
    1888       55736 :       x = Q_primitive_part(x, &cx);
    1889       55736 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    1890       55736 :       alpha = nfpow(nf,alpha,n1);
    1891       55736 :       m = zk_scalar_or_multable(nf, alpha);
    1892       55736 :       if (typ(m) == t_INT) {
    1893         189 :         x = gcdii(powii(a,n1), m);
    1894         189 :         if (s<0) x = ginv(x);
    1895         189 :         if (cx) x = gmul(x, powgi(cx,n));
    1896         189 :         x = scalarmat(x, N);
    1897             :       }
    1898             :       else
    1899             :       {
    1900       55547 :         x = ZM_hnfmodid(m, gcdii(powii(a,n1), zkmultable_capZ(m)));
    1901       55547 :         if (cx) cx = powgi(cx,n);
    1902       55547 :         if (s<0) {
    1903           7 :           GEN xZ = gcoeff(x,1,1);
    1904           7 :           cx = cx ? gdiv(cx, xZ): ginv(xZ);
    1905           7 :           x = idealHNF_inv_Z(nf,x);
    1906             :         }
    1907       55547 :         if (cx) x = ZM_Q_mul(x, cx);
    1908             :       }
    1909       55736 :       return x;
    1910             :   }
    1911             : }
    1912             : 
    1913             : /* raise the ideal x to the power n (in Z) */
    1914             : GEN
    1915      178459 : idealpow(GEN nf, GEN x, GEN n)
    1916             : {
    1917             :   pari_sp av;
    1918             :   long tx;
    1919             :   GEN res, ax;
    1920             : 
    1921      178459 :   if (typ(n) != t_INT) pari_err_TYPE("idealpow",n);
    1922      178459 :   tx = idealtyp(&x,&ax);
    1923      178459 :   res = ax? cgetg(3,t_VEC): NULL;
    1924      178459 :   av = avma;
    1925      178459 :   x = gerepileupto(av, idealpow_aux(checknf(nf), x, tx, n));
    1926      178459 :   if (!ax) return x;
    1927        1177 :   ax = ext_pow(nf, ax, n);
    1928        1177 :   gel(res,1) = x;
    1929        1177 :   gel(res,2) = ax;
    1930        1177 :   return res;
    1931             : }
    1932             : 
    1933             : /* Return ideal^e in number field nf. e is a C integer. */
    1934             : GEN
    1935       21245 : idealpows(GEN nf, GEN ideal, long e)
    1936             : {
    1937       21245 :   long court[] = {evaltyp(t_INT) | _evallg(3),0,0};
    1938       21245 :   affsi(e,court); return idealpow(nf,ideal,court);
    1939             : }
    1940             : 
    1941             : static GEN
    1942       42745 : _idealmulred(GEN nf, GEN x, GEN y)
    1943       42745 : { return idealred(nf,idealmul(nf,x,y)); }
    1944             : static GEN
    1945       61646 : _idealsqrred(GEN nf, GEN x)
    1946       61646 : { return idealred(nf,idealsqr(nf,x)); }
    1947             : static GEN
    1948       26305 : _mul(void *data, GEN x, GEN y) { return _idealmulred((GEN)data,x,y); }
    1949             : static GEN
    1950       61646 : _sqr(void *data, GEN x) { return _idealsqrred((GEN)data, x); }
    1951             : 
    1952             : /* compute x^n (x ideal, n integer), reducing along the way */
    1953             : GEN
    1954       59106 : idealpowred(GEN nf, GEN x, GEN n)
    1955             : {
    1956       59106 :   pari_sp av = avma;
    1957             :   long s;
    1958             :   GEN y;
    1959             : 
    1960       59106 :   if (typ(n) != t_INT) pari_err_TYPE("idealpowred",n);
    1961       59106 :   s = signe(n); if (s == 0) return idealpow(nf,x,n);
    1962       57929 :   y = gen_pow(x, n, (void*)nf, &_sqr, &_mul);
    1963             : 
    1964       57929 :   if (s < 0) y = idealinv(nf,y);
    1965       57929 :   if (s < 0 || is_pm1(n)) y = idealred(nf,y);
    1966       57929 :   return gerepileupto(av,y);
    1967             : }
    1968             : 
    1969             : GEN
    1970       16440 : idealmulred(GEN nf, GEN x, GEN y)
    1971             : {
    1972       16440 :   pari_sp av = avma;
    1973       16440 :   return gerepileupto(av, _idealmulred(nf,x,y));
    1974             : }
    1975             : 
    1976             : long
    1977          91 : isideal(GEN nf,GEN x)
    1978             : {
    1979          91 :   long N, i, j, lx, tx = typ(x);
    1980             :   pari_sp av;
    1981             :   GEN T, xZ;
    1982             : 
    1983          91 :   nf = checknf(nf); T = nf_get_pol(nf); lx = lg(x);
    1984          91 :   if (tx==t_VEC && lx==3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
    1985          91 :   switch(tx)
    1986             :   {
    1987          14 :     case t_INT: case t_FRAC: return 1;
    1988           7 :     case t_POL: return varn(x) == varn(T);
    1989           7 :     case t_POLMOD: return RgX_equal_var(T, gel(x,1));
    1990          14 :     case t_VEC: return get_prid(x)? 1 : 0;
    1991          42 :     case t_MAT: break;
    1992           7 :     default: return 0;
    1993             :   }
    1994          42 :   N = degpol(T);
    1995          42 :   if (lx-1 != N) return (lx == 1);
    1996          28 :   if (nbrows(x) != N) return 0;
    1997             : 
    1998          28 :   av = avma; x = Q_primpart(x);
    1999          28 :   if (!ZM_ishnf(x)) return 0;
    2000          14 :   xZ = gcoeff(x,1,1);
    2001          21 :   for (j=2; j<=N; j++)
    2002          14 :     if (!dvdii(xZ, gcoeff(x,j,j))) { avma = av; return 0; }
    2003          14 :   for (i=2; i<=N; i++)
    2004          14 :     for (j=2; j<=N; j++)
    2005           7 :       if (! hnf_invimage(x, zk_ei_mul(nf,gel(x,i),j))) { avma = av; return 0; }
    2006           7 :   avma=av; return 1;
    2007             : }
    2008             : 
    2009             : GEN
    2010       20818 : idealdiv(GEN nf, GEN x, GEN y)
    2011             : {
    2012       20818 :   pari_sp av = avma, tetpil;
    2013       20818 :   GEN z = idealinv(nf,y);
    2014       20818 :   tetpil = avma; return gerepile(av,tetpil, idealmul(nf,x,z));
    2015             : }
    2016             : 
    2017             : /* This routine computes the quotient x/y of two ideals in the number field nf.
    2018             :  * It assumes that the quotient is an integral ideal.  The idea is to find an
    2019             :  * ideal z dividing y such that gcd(Nx/Nz, Nz) = 1.  Then
    2020             :  *
    2021             :  *   x + (Nx/Nz)    x
    2022             :  *   ----------- = ---
    2023             :  *   y + (Ny/Nz)    y
    2024             :  *
    2025             :  * Proof: we can assume x and y are integral. Let p be any prime ideal
    2026             :  *
    2027             :  * If p | Nz, then it divides neither Nx/Nz nor Ny/Nz (since Nx/Nz is the
    2028             :  * product of the integers N(x/y) and N(y/z)).  Both the numerator and the
    2029             :  * denominator on the left will be coprime to p.  So will x/y, since x/y is
    2030             :  * assumed integral and its norm N(x/y) is coprime to p.
    2031             :  *
    2032             :  * If instead p does not divide Nz, then v_p (Nx/Nz) = v_p (Nx) >= v_p(x).
    2033             :  * Hence v_p (x + Nx/Nz) = v_p(x).  Likewise for the denominators.  QED.
    2034             :  *
    2035             :  *                Peter Montgomery.  July, 1994. */
    2036             : static void
    2037           7 : err_divexact(GEN x, GEN y)
    2038           7 : { pari_err_DOMAIN("idealdivexact","denominator(x/y)", "!=",
    2039           0 :                   gen_1,mkvec2(x,y)); }
    2040             : GEN
    2041         833 : idealdivexact(GEN nf, GEN x0, GEN y0)
    2042             : {
    2043         833 :   pari_sp av = avma;
    2044             :   GEN x, y, yZ, Nx, Ny, Nz, cy, q, r;
    2045             : 
    2046         833 :   nf = checknf(nf);
    2047         833 :   x = idealhnf_shallow(nf, x0);
    2048         833 :   y = idealhnf_shallow(nf, y0);
    2049         833 :   if (lg(y) == 1) pari_err_INV("idealdivexact", y0);
    2050         826 :   if (lg(x) == 1) { avma = av; return cgetg(1, t_MAT); } /* numerator is zero */
    2051         826 :   y = Q_primitive_part(y, &cy);
    2052         826 :   if (cy) x = RgM_Rg_div(x,cy);
    2053         826 :   Nx = idealnorm(nf,x);
    2054         826 :   Ny = idealnorm(nf,y);
    2055         826 :   if (typ(Nx) != t_INT) err_divexact(x,y);
    2056         819 :   q = dvmdii(Nx,Ny, &r);
    2057         819 :   if (signe(r)) err_divexact(x,y);
    2058         819 :   if (is_pm1(q)) { avma = av; return matid(nf_get_degree(nf)); }
    2059             :   /* Find a norm Nz | Ny such that gcd(Nx/Nz, Nz) = 1 */
    2060         567 :   for (Nz = Ny;;) /* q = Nx/Nz */
    2061             :   {
    2062         721 :     GEN p1 = gcdii(Nz, q);
    2063         721 :     if (is_pm1(p1)) break;
    2064         154 :     Nz = diviiexact(Nz,p1);
    2065         154 :     q = mulii(q,p1);
    2066         154 :   }
    2067             :   /* Replace x/y  by  x+(Nx/Nz) / y+(Ny/Nz) */
    2068         567 :   x = ZM_hnfmodid(x, q);
    2069             :   /* y reduced to unit ideal ? */
    2070         567 :   if (Nz == Ny) return gerepileupto(av, x);
    2071             : 
    2072         154 :   y = ZM_hnfmodid(y, diviiexact(Ny,Nz));
    2073         154 :   yZ = gcoeff(y,1,1);
    2074         154 :   y = idealHNF_mul(nf,x, idealHNF_inv_Z(nf,y));
    2075         154 :   return gerepileupto(av, RgM_Rg_div(y, yZ));
    2076             : }
    2077             : 
    2078             : GEN
    2079          21 : idealintersect(GEN nf, GEN x, GEN y)
    2080             : {
    2081          21 :   pari_sp av = avma;
    2082             :   long lz, lx, i;
    2083             :   GEN z, dx, dy, xZ, yZ;;
    2084             : 
    2085          21 :   nf = checknf(nf);
    2086          21 :   x = idealhnf_shallow(nf,x);
    2087          21 :   y = idealhnf_shallow(nf,y);
    2088          21 :   if (lg(x) == 1 || lg(y) == 1) { avma = av; return cgetg(1,t_MAT); }
    2089          14 :   x = Q_remove_denom(x, &dx);
    2090          14 :   y = Q_remove_denom(y, &dy);
    2091          14 :   if (dx) y = ZM_Z_mul(y, dx);
    2092          14 :   if (dy) x = ZM_Z_mul(x, dy);
    2093          14 :   xZ = gcoeff(x,1,1);
    2094          14 :   yZ = gcoeff(y,1,1);
    2095          14 :   dx = mul_denom(dx,dy);
    2096          14 :   z = ZM_lll(shallowconcat(x,y), 0.99, LLL_KER); lz = lg(z);
    2097          14 :   lx = lg(x);
    2098          14 :   for (i=1; i<lz; i++) setlg(z[i], lx);
    2099          14 :   z = ZM_hnfmodid(ZM_mul(x,z), lcmii(xZ, yZ));
    2100          14 :   if (dx) z = RgM_Rg_div(z,dx);
    2101          14 :   return gerepileupto(av,z);
    2102             : }
    2103             : 
    2104             : /*******************************************************************/
    2105             : /*                                                                 */
    2106             : /*                      T2-IDEAL REDUCTION                         */
    2107             : /*                                                                 */
    2108             : /*******************************************************************/
    2109             : 
    2110             : static GEN
    2111          21 : chk_vdir(GEN nf, GEN vdir)
    2112             : {
    2113          21 :   long i, l = lg(vdir);
    2114             :   GEN v;
    2115          21 :   if (l != lg(nf_get_roots(nf))) pari_err_DIM("idealred");
    2116          14 :   switch(typ(vdir))
    2117             :   {
    2118           0 :     case t_VECSMALL: return vdir;
    2119          14 :     case t_VEC: break;
    2120           0 :     default: pari_err_TYPE("idealred",vdir);
    2121             :   }
    2122          14 :   v = cgetg(l, t_VECSMALL);
    2123          14 :   for (i = 1; i < l; i++) v[i] = itos(gceil(gel(vdir,i)));
    2124          14 :   return v;
    2125             : }
    2126             : 
    2127             : static void
    2128       26913 : twistG(GEN G, long r1, long i, long v)
    2129             : {
    2130       26913 :   long j, lG = lg(G);
    2131       26913 :   if (i <= r1) {
    2132       23560 :     for (j=1; j<lG; j++) gcoeff(G,i,j) = gmul2n(gcoeff(G,i,j), v);
    2133             :   } else {
    2134        3353 :     long k = (i<<1) - r1;
    2135       17871 :     for (j=1; j<lG; j++)
    2136             :     {
    2137       14518 :       gcoeff(G,k-1,j) = gmul2n(gcoeff(G,k-1,j), v);
    2138       14518 :       gcoeff(G,k  ,j) = gmul2n(gcoeff(G,k  ,j), v);
    2139             :     }
    2140             :   }
    2141       26913 : }
    2142             : 
    2143             : GEN
    2144      164772 : nf_get_Gtwist(GEN nf, GEN vdir)
    2145             : {
    2146             :   long i, l, v, r1;
    2147             :   GEN G;
    2148             : 
    2149      164772 :   if (!vdir) return nf_get_roundG(nf);
    2150        2820 :   if (typ(vdir) == t_MAT)
    2151             :   {
    2152        2799 :     long N = nf_get_degree(nf);
    2153        2799 :     if (lg(vdir) != N+1 || lgcols(vdir) != N+1) pari_err_DIM("idealred");
    2154        2799 :     return vdir;
    2155             :   }
    2156          21 :   vdir = chk_vdir(nf, vdir);
    2157          14 :   G = RgM_shallowcopy(nf_get_G(nf));
    2158          14 :   r1 = nf_get_r1(nf);
    2159          14 :   l = lg(vdir);
    2160          56 :   for (i=1; i<l; i++)
    2161             :   {
    2162          42 :     v = vdir[i]; if (!v) continue;
    2163          42 :     twistG(G, r1, i, v);
    2164             :   }
    2165          14 :   return RM_round_maxrank(G);
    2166             : }
    2167             : GEN
    2168       26871 : nf_get_Gtwist1(GEN nf, long i)
    2169             : {
    2170       26871 :   GEN G = RgM_shallowcopy( nf_get_G(nf) );
    2171       26871 :   long r1 = nf_get_r1(nf);
    2172       26871 :   twistG(G, r1, i, 10);
    2173       26871 :   return RM_round_maxrank(G);
    2174             : }
    2175             : 
    2176             : GEN
    2177       39562 : RM_round_maxrank(GEN G0)
    2178             : {
    2179       39562 :   long e, r = lg(G0)-1;
    2180       39562 :   pari_sp av = avma;
    2181       39562 :   GEN G = G0;
    2182       39562 :   for (e = 4; ; e <<= 1)
    2183             :   {
    2184       39562 :     GEN H = ground(G);
    2185       79124 :     if (ZM_rank(H) == r) return H; /* maximal rank ? */
    2186           0 :     avma = av;
    2187           0 :     G = gmul2n(G0, e);
    2188           0 :   }
    2189             : }
    2190             : 
    2191             : GEN
    2192      164765 : idealred0(GEN nf, GEN I, GEN vdir)
    2193             : {
    2194      164765 :   pari_sp av = avma;
    2195      164765 :   GEN G, aI, IZ, J, y, yZ, my, c1 = NULL;
    2196             :   long N;
    2197             : 
    2198      164765 :   nf = checknf(nf);
    2199      164765 :   N = nf_get_degree(nf);
    2200             :   /* put first for sanity checks, unused when I obviously principal */
    2201      164765 :   G = nf_get_Gtwist(nf, vdir);
    2202      164758 :   switch (idealtyp(&I,&aI))
    2203             :   {
    2204             :     case id_PRIME:
    2205       23241 :       if (pr_is_inert(I)) {
    2206         581 :         if (!aI) { avma = av; return matid(N); }
    2207         581 :         c1 = gel(I,1); I = matid(N);
    2208         581 :         goto END;
    2209             :       }
    2210       22660 :       IZ = pr_get_p(I);
    2211       22660 :       J = pr_inv_p(I);
    2212       22660 :       I = idealhnf_two(nf,I);
    2213       22660 :       break;
    2214             :     case id_MAT:
    2215      141503 :       I = Q_primitive_part(I, &c1);
    2216      141503 :       IZ = gcoeff(I,1,1);
    2217      141503 :       if (is_pm1(IZ))
    2218             :       {
    2219        7994 :         if (!aI) { avma = av; return matid(N); }
    2220        7938 :         goto END;
    2221             :       }
    2222      133509 :       J = idealHNF_inv_Z(nf, I);
    2223      133509 :       break;
    2224             :     default: /* id_PRINCIPAL, silly case */
    2225          14 :       if (gequal0(I)) I = cgetg(1,t_MAT); else { c1 = I; I = matid(N); }
    2226          14 :       if (!aI) return I;
    2227           7 :       goto END;
    2228             :   }
    2229             :   /* now I integral, HNF; and J = (I\cap Z) I^(-1), integral */
    2230      156169 :   y = idealpseudomin(J, G); /* small elt in (I\cap Z)I^(-1), integral */
    2231      156169 :   if (ZV_isscalar(y))
    2232             :   { /* already reduced */
    2233       65879 :     if (!aI) return gerepilecopy(av, I);
    2234       65480 :     goto END;
    2235             :   }
    2236             : 
    2237       90290 :   my = zk_multable(nf, y);
    2238       90290 :   I = ZM_Z_divexact(ZM_mul(my, I), IZ); /* y I / (I\cap Z), integral */
    2239       90290 :   c1 = mul_content(c1, IZ);
    2240       90290 :   my = ZM_gauss(my, col_ei(N,1)); /* y^-1 */
    2241       90290 :   yZ = Q_denom(my); /* (y) \cap Z */
    2242       90290 :   I = hnfmodid(I, yZ);
    2243       90290 :   if (!aI) return gerepileupto(av, I);
    2244       90024 :   c1 = RgC_Rg_mul(my, c1);
    2245             : END:
    2246      164030 :   if (c1) aI = ext_mul(nf, aI,c1);
    2247      164030 :   return gerepilecopy(av, mkvec2(I, aI));
    2248             : }
    2249             : 
    2250             : GEN
    2251           7 : idealmin(GEN nf, GEN x, GEN vdir)
    2252             : {
    2253           7 :   pari_sp av = avma;
    2254             :   GEN y, dx;
    2255           7 :   nf = checknf(nf);
    2256           7 :   switch( idealtyp(&x,&y) )
    2257             :   {
    2258           0 :     case id_PRINCIPAL: return gcopy(x);
    2259           0 :     case id_PRIME: x = pr_hnf(nf,x); break;
    2260           7 :     case id_MAT: if (lg(x) == 1) return gen_0;
    2261             :   }
    2262           7 :   x = Q_remove_denom(x, &dx);
    2263           7 :   y = idealpseudomin(x, nf_get_Gtwist(nf,vdir));
    2264           7 :   if (dx) y = RgC_Rg_div(y, dx);
    2265           7 :   return gerepileupto(av, y);
    2266             : }
    2267             : 
    2268             : /*******************************************************************/
    2269             : /*                                                                 */
    2270             : /*                   APPROXIMATION THEOREM                         */
    2271             : /*                                                                 */
    2272             : /*******************************************************************/
    2273             : /* a = ppi(a,b) ppo(a,b), where ppi regroups primes common to a and b
    2274             :  * and ppo(a,b) = Z_ppo(a,b) */
    2275             : /* return gcd(a,b),ppi(a,b),ppo(a,b) */
    2276             : GEN
    2277      452893 : Z_ppio(GEN a, GEN b)
    2278             : {
    2279      452893 :   GEN x, y, d = gcdii(a,b);
    2280      452893 :   if (is_pm1(d)) return mkvec3(gen_1, gen_1, a);
    2281      344589 :   x = d; y = diviiexact(a,d);
    2282             :   for(;;)
    2283             :   {
    2284      407281 :     GEN g = gcdii(x,y);
    2285      407281 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2286       62692 :     x = mulii(x,g); y = diviiexact(y,g);
    2287       62692 :   }
    2288             : }
    2289             : /* a = ppg(a,b)pple(a,b), where ppg regroups primes such that v(a) > v(b)
    2290             :  * and pple all others */
    2291             : /* return gcd(a,b),ppg(a,b),pple(a,b) */
    2292             : GEN
    2293           0 : Z_ppgle(GEN a, GEN b)
    2294             : {
    2295           0 :   GEN x, y, g, d = gcdii(a,b);
    2296           0 :   if (equalii(a, d)) return mkvec3(a, gen_1, a);
    2297           0 :   x = diviiexact(a,d); y = d;
    2298             :   for(;;)
    2299             :   {
    2300           0 :     g = gcdii(x,y);
    2301           0 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2302           0 :     x = mulii(x,g); y = diviiexact(y,g);
    2303           0 :   }
    2304             : }
    2305             : static void
    2306           0 : Z_dcba_rec(GEN L, GEN a, GEN b)
    2307             : {
    2308             :   GEN x, r, v, g, h, c, c0;
    2309             :   long n;
    2310           0 :   if (is_pm1(b)) {
    2311           0 :     if (!is_pm1(a)) vectrunc_append(L, a);
    2312           0 :     return;
    2313             :   }
    2314           0 :   v = Z_ppio(a,b);
    2315           0 :   a = gel(v,2);
    2316           0 :   r = gel(v,3);
    2317           0 :   if (!is_pm1(r)) vectrunc_append(L, r);
    2318           0 :   v = Z_ppgle(a,b);
    2319           0 :   g = gel(v,1);
    2320           0 :   h = gel(v,2);
    2321           0 :   x = c0 = gel(v,3);
    2322           0 :   for (n = 1; !is_pm1(h); n++)
    2323             :   {
    2324             :     GEN d, y;
    2325             :     long i;
    2326           0 :     v = Z_ppgle(h,sqri(g));
    2327           0 :     g = gel(v,1);
    2328           0 :     h = gel(v,2);
    2329           0 :     c = gel(v,3); if (is_pm1(c)) continue;
    2330           0 :     d = gcdii(c,b);
    2331           0 :     x = mulii(x,d);
    2332           0 :     y = d; for (i=1; i < n; i++) y = sqri(y);
    2333           0 :     Z_dcba_rec(L, diviiexact(c,y), d);
    2334             :   }
    2335           0 :   Z_dcba_rec(L,diviiexact(b,x), c0);
    2336             : }
    2337             : static GEN
    2338     3068744 : Z_cba_rec(GEN L, GEN a, GEN b)
    2339             : {
    2340             :   GEN g;
    2341     3068744 :   if (lg(L) > 10)
    2342             :   { /* a few naive steps before switching to dcba */
    2343           0 :     Z_dcba_rec(L, a, b);
    2344           0 :     return gel(L, lg(L)-1);
    2345             :   }
    2346     3068744 :   if (is_pm1(a)) return b;
    2347     1823360 :   g = gcdii(a,b);
    2348     1823360 :   if (is_pm1(g)) { vectrunc_append(L, a); return b; }
    2349     1362053 :   a = diviiexact(a,g);
    2350     1362053 :   b = diviiexact(b,g);
    2351     1362053 :   return Z_cba_rec(L, Z_cba_rec(L, a, g), b);
    2352             : }
    2353             : GEN
    2354      344638 : Z_cba(GEN a, GEN b)
    2355             : {
    2356      344638 :   GEN L = vectrunc_init(expi(a) + expi(b) + 2);
    2357      344638 :   GEN t = Z_cba_rec(L, a, b);
    2358      344638 :   if (!is_pm1(t)) vectrunc_append(L, t);
    2359      344638 :   return L;
    2360             : }
    2361             : /* P = coprime base, extend it by b; TODO: quadratic for now */
    2362             : GEN
    2363           0 : ZV_cba_extend(GEN P, GEN b)
    2364             : {
    2365           0 :   long i, l = lg(P);
    2366           0 :   GEN w = cgetg(l+1, t_VEC);
    2367           0 :   for (i = 1; i < l; i++)
    2368             :   {
    2369           0 :     GEN v = Z_cba(gel(P,i), b);
    2370           0 :     long nv = lg(v)-1;
    2371           0 :     gel(w,i) = vecslice(v, 1, nv-1); /* those divide P[i] but not b */
    2372           0 :     b = gel(v,nv);
    2373             :   }
    2374           0 :   gel(w,l) = b; return shallowconcat1(w);
    2375             : }
    2376             : GEN
    2377           0 : ZV_cba(GEN v)
    2378             : {
    2379           0 :   long i, l = lg(v);
    2380             :   GEN P;
    2381           0 :   if (l <= 2) return v;
    2382           0 :   P = Z_cba(gel(v,1), gel(v,2));
    2383           0 :   for (i = 3; i < l; i++) P = ZV_cba_extend(P, gel(v,i));
    2384           0 :   return P;
    2385             : }
    2386             : 
    2387             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2388             : GEN
    2389     1113222 : Z_ppo(GEN x, GEN f)
    2390             : {
    2391             :   for (;;)
    2392             :   {
    2393     1113222 :     f = gcdii(x, f); if (is_pm1(f)) break;
    2394      756699 :     x = diviiexact(x, f);
    2395      756699 :   }
    2396      356523 :   return x;
    2397             : }
    2398             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2399             : ulong
    2400    38015600 : u_ppo(ulong x, ulong f)
    2401             : {
    2402             :   for (;;)
    2403             :   {
    2404    38015600 :     f = ugcd(x, f); if (f == 1) break;
    2405     7317093 :     x /= f;
    2406     7317093 :   }
    2407    30698507 :   return x;
    2408             : }
    2409             : 
    2410             : /* x t_INT, f ideal. Write x = x1 x2, sqf(x1) | f, (x2,f) = 1. Return x2 */
    2411             : static GEN
    2412         140 : nf_coprime_part(GEN nf, GEN x, GEN listpr)
    2413             : {
    2414         140 :   long v, j, lp = lg(listpr), N = nf_get_degree(nf);
    2415             :   GEN x1, x2, ex;
    2416             : 
    2417             : #if 0 /*1) via many gcds. Expensive ! */
    2418             :   GEN f = idealprodprime(nf, listpr);
    2419             :   f = ZM_hnfmodid(f, x); /* first gcd is less expensive since x in Z */
    2420             :   x = scalarmat(x, N);
    2421             :   for (;;)
    2422             :   {
    2423             :     if (gequal1(gcoeff(f,1,1))) break;
    2424             :     x = idealdivexact(nf, x, f);
    2425             :     f = ZM_hnfmodid(shallowconcat(f,x), gcoeff(x,1,1)); /* gcd(f,x) */
    2426             :   }
    2427             :   x2 = x;
    2428             : #else /*2) from prime decomposition */
    2429         140 :   x1 = NULL;
    2430         399 :   for (j=1; j<lp; j++)
    2431             :   {
    2432         259 :     GEN pr = gel(listpr,j);
    2433         259 :     v = Z_pval(x, pr_get_p(pr)); if (!v) continue;
    2434             : 
    2435         140 :     ex = muluu(v, pr_get_e(pr)); /* = v_pr(x) > 0 */
    2436         140 :     x1 = x1? idealmulpowprime(nf, x1, pr, ex)
    2437         140 :            : idealpow(nf, pr, ex);
    2438             :   }
    2439         140 :   x = scalarmat(x, N);
    2440         140 :   x2 = x1? idealdivexact(nf, x, x1): x;
    2441             : #endif
    2442         140 :   return x2;
    2443             : }
    2444             : 
    2445             : /* L0 in K^*, assume (L0,f) = 1. Return L integral, L0 = L mod f  */
    2446             : GEN
    2447        5621 : make_integral(GEN nf, GEN L0, GEN f, GEN listpr)
    2448             : {
    2449             :   GEN fZ, t, L, D2, d1, d2, d;
    2450             : 
    2451        5621 :   L = Q_remove_denom(L0, &d);
    2452        5621 :   if (!d) return L0;
    2453             : 
    2454             :   /* L0 = L / d, L integral */
    2455        2170 :   fZ = gcoeff(f,1,1);
    2456        2170 :   if (typ(L) == t_INT) return Fp_mul(L, Fp_inv(d, fZ), fZ);
    2457             :   /* Kill denom part coprime to fZ */
    2458        1939 :   d2 = Z_ppo(d, fZ);
    2459        1939 :   t = Fp_inv(d2, fZ); if (!is_pm1(t)) L = ZC_Z_mul(L,t);
    2460        1939 :   if (equalii(d, d2)) return L;
    2461             : 
    2462         140 :   d1 = diviiexact(d, d2);
    2463             :   /* L0 = (L / d1) mod f. d1 not coprime to f
    2464             :    * write (d1) = D1 D2, D2 minimal, (D2,f) = 1. */
    2465         140 :   D2 = nf_coprime_part(nf, d1, listpr);
    2466         140 :   t = idealaddtoone_i(nf, D2, f); /* in D2, 1 mod f */
    2467         140 :   L = nfmuli(nf,t,L);
    2468             : 
    2469             :   /* if (L0, f) = 1, then L in D1 ==> in D1 D2 = (d1) */
    2470         140 :   return Q_div_to_int(L, d1); /* exact division */
    2471             : }
    2472             : 
    2473             : /* assume L is a list of prime ideals. Return the product */
    2474             : GEN
    2475         126 : idealprodprime(GEN nf, GEN L)
    2476             : {
    2477         126 :   long l = lg(L), i;
    2478             :   GEN z;
    2479         126 :   if (l == 1) return matid(nf_get_degree(nf));
    2480         126 :   z = pr_hnf(nf, gel(L,1));
    2481         126 :   for (i=2; i<l; i++) z = idealHNF_mul_two(nf,z, gel(L,i));
    2482         126 :   return z;
    2483             : }
    2484             : 
    2485             : /* optimize for the frequent case I = nfhnf()[2]: lots of them are 1 */
    2486             : GEN
    2487        1470 : idealprod(GEN nf, GEN I)
    2488             : {
    2489        1470 :   long i, l = lg(I);
    2490             :   GEN z;
    2491        2541 :   for (i = 1; i < l; i++)
    2492        2520 :     if (!equali1(gel(I,i))) break;
    2493        1470 :   if (i == l) return gen_1;
    2494        1449 :   z = gel(I,i);
    2495        1449 :   for (i++; i<l; i++) z = idealmul(nf, z, gel(I,i));
    2496        1449 :   return z;
    2497             : }
    2498             : 
    2499             : /* assume L is a list of prime ideals. Return prod L[i]^e[i] */
    2500             : GEN
    2501        7203 : factorbackprime(GEN nf, GEN L, GEN e)
    2502             : {
    2503        7203 :   long l = lg(L), i;
    2504             :   GEN z;
    2505             : 
    2506        7203 :   if (l == 1) return matid(nf_get_degree(nf));
    2507        7189 :   z = idealpow(nf, gel(L,1), gel(e,1));
    2508       11032 :   for (i=2; i<l; i++)
    2509        3843 :     if (signe(gel(e,i))) z = idealmulpowprime(nf,z, gel(L,i),gel(e,i));
    2510        7189 :   return z;
    2511             : }
    2512             : 
    2513             : /* F in Z, divisible exactly by pr.p. Return F-uniformizer for pr, i.e.
    2514             :  * a t in Z_K such that v_pr(t) = 1 and (t, F/pr) = 1 */
    2515             : GEN
    2516       17515 : pr_uniformizer(GEN pr, GEN F)
    2517             : {
    2518       17515 :   GEN p = pr_get_p(pr), t = pr_get_gen(pr);
    2519       17515 :   if (!equalii(F, p))
    2520             :   {
    2521        7424 :     long e = pr_get_e(pr);
    2522        7424 :     GEN u, v, q = (e == 1)? sqri(p): p;
    2523        7424 :     u = mulii(q, Fp_inv(q, diviiexact(F,p))); /* 1 mod F/p, 0 mod q */
    2524        7424 :     v = subui(1UL, u); /* 0 mod F/p, 1 mod q */
    2525        7424 :     if (pr_is_inert(pr))
    2526           0 :       t = addii(mulii(p, v), u);
    2527             :     else
    2528             :     {
    2529        7424 :       t = ZC_Z_mul(t, v);
    2530        7424 :       gel(t,1) = addii(gel(t,1), u); /* return u + vt */
    2531             :     }
    2532             :   }
    2533       17515 :   return t;
    2534             : }
    2535             : /* L = list of prime ideals, return lcm_i (L[i] \cap \ZM) */
    2536             : GEN
    2537       34832 : prV_lcm_capZ(GEN L)
    2538             : {
    2539       34832 :   long i, r = lg(L);
    2540             :   GEN F;
    2541       34832 :   if (r == 1) return gen_1;
    2542       29421 :   F = pr_get_p(gel(L,1));
    2543       43849 :   for (i = 2; i < r; i++)
    2544             :   {
    2545       14428 :     GEN pr = gel(L,i), p = pr_get_p(pr);
    2546       14428 :     if (!dvdii(F, p)) F = mulii(F,p);
    2547             :   }
    2548       29421 :   return F;
    2549             : }
    2550             : 
    2551             : /* Given a prime ideal factorization with possibly zero or negative
    2552             :  * exponents, gives b such that v_p(b) = v_p(x) for all prime ideals pr | x
    2553             :  * and v_pr(b) >= 0 for all other pr.
    2554             :  * For optimal performance, all [anti-]uniformizers should be precomputed,
    2555             :  * but no support for this yet.
    2556             :  *
    2557             :  * If nored, do not reduce result.
    2558             :  * No garbage collecting */
    2559             : static GEN
    2560       20433 : idealapprfact_i(GEN nf, GEN x, int nored)
    2561             : {
    2562             :   GEN z, d, L, e, e2, F;
    2563             :   long i, r;
    2564             :   int flagden;
    2565             : 
    2566       20433 :   nf = checknf(nf);
    2567       20433 :   L = gel(x,1);
    2568       20433 :   e = gel(x,2);
    2569       20433 :   F = prV_lcm_capZ(L);
    2570       20433 :   flagden = 0;
    2571       20433 :   z = NULL; r = lg(e);
    2572       43261 :   for (i = 1; i < r; i++)
    2573             :   {
    2574       22828 :     long s = signe(gel(e,i));
    2575             :     GEN pi, q;
    2576       22828 :     if (!s) continue;
    2577       15352 :     if (s < 0) flagden = 1;
    2578       15352 :     pi = pr_uniformizer(gel(L,i), F);
    2579       15352 :     q = nfpow(nf, pi, gel(e,i));
    2580       15352 :     z = z? nfmul(nf, z, q): q;
    2581             :   }
    2582       20433 :   if (!z) return gen_1;
    2583       10577 :   if (nored || typ(z) != t_COL) return z;
    2584        2716 :   e2 = cgetg(r, t_VEC);
    2585        2716 :   for (i=1; i<r; i++) gel(e2,i) = addiu(gel(e,i), 1);
    2586        2716 :   x = factorbackprime(nf, L,e2);
    2587        2716 :   if (flagden) /* denominator */
    2588             :   {
    2589        2702 :     z = Q_remove_denom(z, &d);
    2590        2702 :     d = diviiexact(d, Z_ppo(d, F));
    2591        2702 :     x = RgM_Rg_mul(x, d);
    2592             :   }
    2593             :   else
    2594          14 :     d = NULL;
    2595        2716 :   z = ZC_reducemodlll(z, x);
    2596        2716 :   return d? RgC_Rg_div(z,d): z;
    2597             : }
    2598             : 
    2599             : GEN
    2600           0 : idealapprfact(GEN nf, GEN x) {
    2601           0 :   pari_sp av = avma;
    2602           0 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    2603             : }
    2604             : GEN
    2605          14 : idealappr(GEN nf, GEN x) {
    2606          14 :   pari_sp av = avma;
    2607          14 :   if (!is_nf_extfactor(x)) x = idealfactor(nf, x);
    2608          14 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    2609             : }
    2610             : 
    2611             : /* OBSOLETE */
    2612             : GEN
    2613          14 : idealappr0(GEN nf, GEN x, long fl) { (void)fl; return idealappr(nf, x); }
    2614             : 
    2615             : static GEN
    2616          21 : mat_ideal_two_elt2(GEN nf, GEN x, GEN a)
    2617             : {
    2618          21 :   GEN F = idealfactor(nf,a), P = gel(F,1), E = gel(F,2);
    2619          21 :   long i, r = lg(E);
    2620          21 :   for (i=1; i<r; i++) gel(E,i) = stoi( idealval(nf,x,gel(P,i)) );
    2621          21 :   return idealapprfact_i(nf,F,1);
    2622             : }
    2623             : 
    2624             : static void
    2625          14 : not_in_ideal(GEN a) {
    2626          14 :   pari_err_DOMAIN("idealtwoelt2","element mod ideal", "!=", gen_0, a);
    2627           0 : }
    2628             : /* x integral in HNF, a an 'nf' */
    2629             : static int
    2630          28 : in_ideal(GEN x, GEN a)
    2631             : {
    2632          28 :   switch(typ(a))
    2633             :   {
    2634          14 :     case t_INT: return dvdii(a, gcoeff(x,1,1));
    2635           7 :     case t_COL: return RgV_is_ZV(a) && !!hnf_invimage(x, a);
    2636           7 :     default: return 0;
    2637             :   }
    2638             : }
    2639             : 
    2640             : /* Given an integral ideal x and a in x, gives a b such that
    2641             :  * x = aZ_K + bZ_K using the approximation theorem */
    2642             : GEN
    2643          42 : idealtwoelt2(GEN nf, GEN x, GEN a)
    2644             : {
    2645          42 :   pari_sp av = avma;
    2646             :   GEN cx, b;
    2647             : 
    2648          42 :   nf = checknf(nf);
    2649          42 :   a = nf_to_scalar_or_basis(nf, a);
    2650          42 :   x = idealhnf_shallow(nf,x);
    2651          42 :   if (lg(x) == 1)
    2652             :   {
    2653          14 :     if (!isintzero(a)) not_in_ideal(a);
    2654           7 :     avma = av; return gen_0;
    2655             :   }
    2656          28 :   x = Q_primitive_part(x, &cx);
    2657          28 :   if (cx) a = gdiv(a, cx);
    2658          28 :   if (!in_ideal(x, a)) not_in_ideal(a);
    2659          21 :   b = mat_ideal_two_elt2(nf, x, a);
    2660          21 :   if (typ(b) == t_COL)
    2661             :   {
    2662          14 :     GEN mod = idealhnf_principal(nf,a);
    2663          14 :     b = ZC_hnfrem(b,mod);
    2664          14 :     if (ZV_isscalar(b)) b = gel(b,1);
    2665             :   }
    2666             :   else
    2667             :   {
    2668           7 :     GEN aZ = typ(a) == t_COL? Q_denom(zk_inv(nf,a)): a; /* (a) \cap Z */
    2669           7 :     b = centermodii(b, aZ, shifti(aZ,-1));
    2670             :   }
    2671          21 :   b = cx? gmul(b,cx): gcopy(b);
    2672          21 :   return gerepileupto(av, b);
    2673             : }
    2674             : 
    2675             : /* Given 2 integral ideals x and y in nf, returns a beta in nf such that
    2676             :  * beta * x is an integral ideal coprime to y */
    2677             : GEN
    2678       12551 : idealcoprimefact(GEN nf, GEN x, GEN fy)
    2679             : {
    2680       12551 :   GEN L = gel(fy,1), e;
    2681       12551 :   long i, r = lg(L);
    2682             : 
    2683       12551 :   e = cgetg(r, t_COL);
    2684       12551 :   for (i=1; i<r; i++) gel(e,i) = stoi( -idealval(nf,x,gel(L,i)) );
    2685       12551 :   return idealapprfact_i(nf, mkmat2(L,e), 0);
    2686             : }
    2687             : GEN
    2688          70 : idealcoprime(GEN nf, GEN x, GEN y)
    2689             : {
    2690          70 :   pari_sp av = avma;
    2691          70 :   return gerepileupto(av, idealcoprimefact(nf, x, idealfactor(nf,y)));
    2692             : }
    2693             : 
    2694             : GEN
    2695           7 : nfmulmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    2696             : {
    2697           7 :   pari_sp av = avma;
    2698           7 :   GEN z, p, pr = modpr, T;
    2699             : 
    2700           7 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    2701           0 :   x = nf_to_Fq(nf,x,modpr);
    2702           0 :   y = nf_to_Fq(nf,y,modpr);
    2703           0 :   z = Fq_mul(x,y,T,p);
    2704           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    2705             : }
    2706             : 
    2707             : GEN
    2708           0 : nfdivmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    2709             : {
    2710           0 :   pari_sp av = avma;
    2711           0 :   nf = checknf(nf);
    2712           0 :   return gerepileupto(av, nfreducemodpr(nf, nfdiv(nf,x,y), modpr));
    2713             : }
    2714             : 
    2715             : GEN
    2716           0 : nfpowmodpr(GEN nf, GEN x, GEN k, GEN modpr)
    2717             : {
    2718           0 :   pari_sp av=avma;
    2719           0 :   GEN z, T, p, pr = modpr;
    2720             : 
    2721           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    2722           0 :   z = nf_to_Fq(nf,x,modpr);
    2723           0 :   z = Fq_pow(z,k,T,p);
    2724           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    2725             : }
    2726             : 
    2727             : GEN
    2728           0 : nfkermodpr(GEN nf, GEN x, GEN modpr)
    2729             : {
    2730           0 :   pari_sp av = avma;
    2731           0 :   GEN T, p, pr = modpr;
    2732             : 
    2733           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    2734           0 :   if (typ(x)!=t_MAT) pari_err_TYPE("nfkermodpr",x);
    2735           0 :   x = nfM_to_FqM(x, nf, modpr);
    2736           0 :   return gerepilecopy(av, FqM_to_nfM(FqM_ker(x,T,p), modpr));
    2737             : }
    2738             : 
    2739             : GEN
    2740           0 : nfsolvemodpr(GEN nf, GEN a, GEN b, GEN pr)
    2741             : {
    2742           0 :   const char *f = "nfsolvemodpr";
    2743           0 :   pari_sp av = avma;
    2744             :   GEN T, p, modpr;
    2745             : 
    2746           0 :   nf = checknf(nf);
    2747           0 :   modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    2748           0 :   if (typ(a)!=t_MAT) pari_err_TYPE(f,a);
    2749           0 :   a = nfM_to_FqM(a, nf, modpr);
    2750           0 :   switch(typ(b))
    2751             :   {
    2752             :     case t_MAT:
    2753           0 :       b = nfM_to_FqM(b, nf, modpr);
    2754           0 :       b = FqM_gauss(a,b,T,p);
    2755           0 :       if (!b) pari_err_INV(f,a);
    2756           0 :       a = FqM_to_nfM(b, modpr);
    2757           0 :       break;
    2758             :     case t_COL:
    2759           0 :       b = nfV_to_FqV(b, nf, modpr);
    2760           0 :       b = FqM_FqC_gauss(a,b,T,p);
    2761           0 :       if (!b) pari_err_INV(f,a);
    2762           0 :       a = FqV_to_nfV(b, modpr);
    2763           0 :       break;
    2764           0 :     default: pari_err_TYPE(f,b);
    2765             :   }
    2766           0 :   return gerepilecopy(av, a);
    2767             : }

Generated by: LCOV version 1.11