Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - base4.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.14.0 lcov report (development 27775-aca467eab2) Lines: 1592 1776 89.6 %
Date: 2022-07-03 07:33:15 Functions: 156 174 89.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : /*******************************************************************/
      16             : /*                                                                 */
      17             : /*                       BASIC NF OPERATIONS                       */
      18             : /*                           (continued)                           */
      19             : /*                                                                 */
      20             : /*******************************************************************/
      21             : #include "pari.h"
      22             : #include "paripriv.h"
      23             : 
      24             : #define DEBUGLEVEL DEBUGLEVEL_nf
      25             : 
      26             : /*******************************************************************/
      27             : /*                                                                 */
      28             : /*                     IDEAL OPERATIONS                            */
      29             : /*                                                                 */
      30             : /*******************************************************************/
      31             : 
      32             : /* A valid ideal is either principal (valid nf_element), or prime, or a matrix
      33             :  * on the integer basis in HNF.
      34             :  * A prime ideal is of the form [p,a,e,f,b], where the ideal is p.Z_K+a.Z_K,
      35             :  * p is a rational prime, a belongs to Z_K, e=e(P/p), f=f(P/p), and b
      36             :  * is Lenstra's constant, such that p.P^(-1)= p Z_K + b Z_K.
      37             :  *
      38             :  * An extended ideal is a couple [I,F] where I is an ideal and F is either an
      39             :  * algebraic number, or a factorization matrix attached to an algebraic number.
      40             :  * All routines work with either extended ideals or ideals (an omitted F is
      41             :  * assumed to be factor(1)). All ideals are output in HNF form. */
      42             : 
      43             : /* types and conversions */
      44             : 
      45             : long
      46     6736304 : idealtyp(GEN *ideal, GEN *arch)
      47             : {
      48     6736304 :   GEN x = *ideal;
      49     6736304 :   long t,lx,tx = typ(x);
      50             : 
      51     6736304 :   if (tx!=t_VEC || lg(x)!=3) { if (arch) *arch = NULL; }
      52             :   else
      53             :   {
      54      245768 :     GEN a = gel(x,2);
      55      245768 :     if (typ(a) == t_MAT && lg(a) != 3)
      56             :     { /* allow [;] */
      57          14 :       if (lg(a) != 1) pari_err_TYPE("idealtyp [extended ideal]",x);
      58           7 :       if (arch) *arch = trivial_fact();
      59             :     }
      60             :     else
      61      245754 :       if (arch) *arch = a;
      62      245761 :     x = gel(x,1); tx = typ(x);
      63             :   }
      64     6736297 :   switch(tx)
      65             :   {
      66     3815811 :     case t_MAT: lx = lg(x);
      67     3815811 :       if (lx == 1) { t = id_PRINCIPAL; x = gen_0; break; }
      68     3815650 :       if (lx != lgcols(x)) pari_err_TYPE("idealtyp [nonsquare t_MAT]",x);
      69     3815634 :       t = id_MAT;
      70     3815634 :       break;
      71             : 
      72     2154855 :     case t_VEC: if (lg(x)!=6) pari_err_TYPE("idealtyp",x);
      73     2154820 :       t = id_PRIME; break;
      74             : 
      75      765779 :     case t_POL: case t_POLMOD: case t_COL:
      76             :     case t_INT: case t_FRAC:
      77      765779 :       t = id_PRINCIPAL; break;
      78           0 :     default:
      79           0 :       pari_err_TYPE("idealtyp",x);
      80             :       return 0; /*LCOV_EXCL_LINE*/
      81             :   }
      82     6736394 :   *ideal = x; return t;
      83             : }
      84             : 
      85             : /* true nf; v = [a,x,...], a in Z. Return (a,x) */
      86             : GEN
      87      642774 : idealhnf_two(GEN nf, GEN v)
      88             : {
      89      642774 :   GEN p = gel(v,1), pi = gel(v,2), m = zk_scalar_or_multable(nf, pi);
      90      642779 :   if (typ(m) == t_INT) return scalarmat(gcdii(m,p), nf_get_degree(nf));
      91      573427 :   return ZM_hnfmodid(m, p);
      92             : }
      93             : /* true nf */
      94             : GEN
      95     2906462 : pr_hnf(GEN nf, GEN pr)
      96             : {
      97     2906462 :   GEN p = pr_get_p(pr), m;
      98     2906422 :   if (pr_is_inert(pr)) return scalarmat(p, nf_get_degree(nf));
      99     2479856 :   m = zk_scalar_or_multable(nf, pr_get_gen(pr));
     100     2479572 :   return ZM_hnfmodprime(m, p);
     101             : }
     102             : 
     103             : GEN
     104      682126 : idealhnf_principal(GEN nf, GEN x)
     105             : {
     106             :   GEN cx;
     107      682126 :   x = nf_to_scalar_or_basis(nf, x);
     108      682127 :   switch(typ(x))
     109             :   {
     110      434424 :     case t_COL: break;
     111      216777 :     case t_INT:  if (!signe(x)) return cgetg(1,t_MAT);
     112      216014 :       return scalarmat(absi_shallow(x), nf_get_degree(nf));
     113       30926 :     case t_FRAC:
     114       30926 :       return scalarmat(Q_abs_shallow(x), nf_get_degree(nf));
     115           0 :     default: pari_err_TYPE("idealhnf",x);
     116             :   }
     117      434424 :   x = Q_primitive_part(x, &cx);
     118      434417 :   RgV_check_ZV(x, "idealhnf");
     119      434417 :   x = zk_multable(nf, x);
     120      434418 :   x = ZM_hnfmodid(x, zkmultable_capZ(x));
     121      434420 :   return cx? ZM_Q_mul(x,cx): x;
     122             : }
     123             : 
     124             : /* x integral ideal in t_MAT form, nx columns */
     125             : static GEN
     126           7 : vec_mulid(GEN nf, GEN x, long nx, long N)
     127             : {
     128           7 :   GEN m = cgetg(nx*N + 1, t_MAT);
     129             :   long i, j, k;
     130          21 :   for (i=k=1; i<=nx; i++)
     131          56 :     for (j=1; j<=N; j++) gel(m, k++) = zk_ei_mul(nf, gel(x,i),j);
     132           7 :   return m;
     133             : }
     134             : /* true nf */
     135             : GEN
     136      953675 : idealhnf_shallow(GEN nf, GEN x)
     137             : {
     138      953675 :   long tx = typ(x), lx = lg(x), N;
     139             : 
     140             :   /* cannot use idealtyp because here we allow nonsquare matrices */
     141      953675 :   if (tx == t_VEC && lx == 3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
     142      953675 :   if (tx == t_VEC && lx == 6) return pr_hnf(nf,x); /* PRIME */
     143      577785 :   switch(tx)
     144             :   {
     145       97536 :     case t_MAT:
     146             :     {
     147             :       GEN cx;
     148       97536 :       long nx = lx-1;
     149       97536 :       N = nf_get_degree(nf);
     150       97536 :       if (nx == 0) return cgetg(1, t_MAT);
     151       97515 :       if (nbrows(x) != N) pari_err_TYPE("idealhnf [wrong dimension]",x);
     152       97508 :       if (nx == 1) return idealhnf_principal(nf, gel(x,1));
     153             : 
     154       81555 :       if (nx == N && RgM_is_ZM(x) && ZM_ishnf(x)) return x;
     155       47426 :       x = Q_primitive_part(x, &cx);
     156       47426 :       if (nx < N) x = vec_mulid(nf, x, nx, N);
     157       47426 :       x = ZM_hnfmod(x, ZM_detmult(x));
     158       47426 :       return cx? ZM_Q_mul(x,cx): x;
     159             :     }
     160          14 :     case t_QFB:
     161             :     {
     162          14 :       pari_sp av = avma;
     163          14 :       GEN u, D = nf_get_disc(nf), T = nf_get_pol(nf), f = nf_get_index(nf);
     164          14 :       GEN A = gel(x,1), B = gel(x,2);
     165          14 :       N = nf_get_degree(nf);
     166          14 :       if (N != 2)
     167           0 :         pari_err_TYPE("idealhnf [Qfb for nonquadratic fields]", x);
     168          14 :       if (!equalii(qfb_disc(x), D))
     169           7 :         pari_err_DOMAIN("idealhnf [Qfb]", "disc(q)", "!=", D, x);
     170             :       /* x -> A Z + (-B + sqrt(D)) / 2 Z
     171             :          K = Q[t]/T(t), t^2 + ut + v = 0,  u^2 - 4v = Df^2
     172             :          => t = (-u + sqrt(D) f)/2
     173             :          => sqrt(D)/2 = (t + u/2)/f */
     174           7 :       u = gel(T,3);
     175           7 :       B = deg1pol_shallow(ginv(f),
     176             :                           gsub(gdiv(u, shifti(f,1)), gdiv(B,gen_2)),
     177           7 :                           varn(T));
     178           7 :       return gerepileupto(av, idealhnf_two(nf, mkvec2(A,B)));
     179             :     }
     180      480235 :     default: return idealhnf_principal(nf, x); /* PRINCIPAL */
     181             :   }
     182             : }
     183             : /* true nf */
     184             : GEN
     185         252 : idealhnf(GEN nf, GEN x)
     186             : {
     187         252 :   pari_sp av = avma;
     188         252 :   GEN y = idealhnf_shallow(nf, x);
     189         245 :   return (avma == av)? gcopy(y): gerepileupto(av, y);
     190             : }
     191             : 
     192             : /* GP functions */
     193             : 
     194             : GEN
     195          63 : idealtwoelt0(GEN nf, GEN x, GEN a)
     196             : {
     197          63 :   if (!a) return idealtwoelt(nf,x);
     198          42 :   return idealtwoelt2(nf,x,a);
     199             : }
     200             : 
     201             : GEN
     202          84 : idealpow0(GEN nf, GEN x, GEN n, long flag)
     203             : {
     204          84 :   if (flag) return idealpowred(nf,x,n);
     205          77 :   return idealpow(nf,x,n);
     206             : }
     207             : 
     208             : GEN
     209          70 : idealmul0(GEN nf, GEN x, GEN y, long flag)
     210             : {
     211          70 :   if (flag) return idealmulred(nf,x,y);
     212          63 :   return idealmul(nf,x,y);
     213             : }
     214             : 
     215             : GEN
     216          56 : idealdiv0(GEN nf, GEN x, GEN y, long flag)
     217             : {
     218          56 :   switch(flag)
     219             :   {
     220          28 :     case 0: return idealdiv(nf,x,y);
     221          28 :     case 1: return idealdivexact(nf,x,y);
     222           0 :     default: pari_err_FLAG("idealdiv");
     223             :   }
     224             :   return NULL; /* LCOV_EXCL_LINE */
     225             : }
     226             : 
     227             : GEN
     228          70 : idealaddtoone0(GEN nf, GEN arg1, GEN arg2)
     229             : {
     230          70 :   if (!arg2) return idealaddmultoone(nf,arg1);
     231          35 :   return idealaddtoone(nf,arg1,arg2);
     232             : }
     233             : 
     234             : /* b not a scalar */
     235             : static GEN
     236          35 : hnf_Z_ZC(GEN nf, GEN a, GEN b) { return hnfmodid(zk_multable(nf,b), a); }
     237             : /* b not a scalar */
     238             : static GEN
     239          28 : hnf_Z_QC(GEN nf, GEN a, GEN b)
     240             : {
     241             :   GEN db;
     242          28 :   b = Q_remove_denom(b, &db);
     243          28 :   if (db) a = mulii(a, db);
     244          28 :   b = hnf_Z_ZC(nf,a,b);
     245          28 :   return db? RgM_Rg_div(b, db): b;
     246             : }
     247             : /* b not a scalar (not point in trying to optimize for this case) */
     248             : static GEN
     249          35 : hnf_Q_QC(GEN nf, GEN a, GEN b)
     250             : {
     251             :   GEN da, db;
     252          35 :   if (typ(a) == t_INT) return hnf_Z_QC(nf, a, b);
     253           7 :   da = gel(a,2);
     254           7 :   a = gel(a,1);
     255           7 :   b = Q_remove_denom(b, &db);
     256             :   /* write da = d*A, db = d*B, gcd(A,B) = 1
     257             :    * gcd(a/(d A), b/(d B)) = gcd(a B, A b) / A B d = gcd(a B, b) / A B d */
     258           7 :   if (db)
     259             :   {
     260           7 :     GEN d = gcdii(da,db);
     261           7 :     if (!is_pm1(d)) db = diviiexact(db,d); /* B */
     262           7 :     if (!is_pm1(db))
     263             :     {
     264           7 :       a = mulii(a, db); /* a B */
     265           7 :       da = mulii(da, db); /* A B d = lcm(denom(a),denom(b)) */
     266             :     }
     267             :   }
     268           7 :   return RgM_Rg_div(hnf_Z_ZC(nf,a,b), da);
     269             : }
     270             : static GEN
     271           7 : hnf_QC_QC(GEN nf, GEN a, GEN b)
     272             : {
     273             :   GEN da, db, d, x;
     274           7 :   a = Q_remove_denom(a, &da);
     275           7 :   b = Q_remove_denom(b, &db);
     276           7 :   if (da) b = ZC_Z_mul(b, da);
     277           7 :   if (db) a = ZC_Z_mul(a, db);
     278           7 :   d = mul_denom(da, db);
     279           7 :   a = zk_multable(nf,a); da = zkmultable_capZ(a);
     280           7 :   b = zk_multable(nf,b); db = zkmultable_capZ(b);
     281           7 :   x = ZM_hnfmodid(shallowconcat(a,b), gcdii(da,db));
     282           7 :   return d? RgM_Rg_div(x, d): x;
     283             : }
     284             : static GEN
     285          21 : hnf_Q_Q(GEN nf, GEN a, GEN b) {return scalarmat(Q_gcd(a,b), nf_get_degree(nf));}
     286             : GEN
     287         168 : idealhnf0(GEN nf, GEN a, GEN b)
     288             : {
     289             :   long ta, tb;
     290             :   pari_sp av;
     291             :   GEN x;
     292         168 :   nf = checknf(nf);
     293         168 :   if (!b) return idealhnf(nf,a);
     294             : 
     295             :   /* HNF of aZ_K+bZ_K */
     296          70 :   av = avma;
     297          70 :   a = nf_to_scalar_or_basis(nf,a); ta = typ(a);
     298          70 :   b = nf_to_scalar_or_basis(nf,b); tb = typ(b);
     299          63 :   if (ta == t_COL)
     300          14 :     x = (tb==t_COL)? hnf_QC_QC(nf, a,b): hnf_Q_QC(nf, b,a);
     301             :   else
     302          49 :     x = (tb==t_COL)? hnf_Q_QC(nf, a,b): hnf_Q_Q(nf, a,b);
     303          63 :   return gerepileupto(av, x);
     304             : }
     305             : 
     306             : /*******************************************************************/
     307             : /*                                                                 */
     308             : /*                       TWO-ELEMENT FORM                          */
     309             : /*                                                                 */
     310             : /*******************************************************************/
     311             : static GEN idealapprfact_i(GEN nf, GEN x, int nored);
     312             : 
     313             : static int
     314      440609 : ok_elt(GEN x, GEN xZ, GEN y)
     315             : {
     316      440609 :   pari_sp av = avma;
     317      440609 :   return gc_bool(av, ZM_equal(x, ZM_hnfmodid(y, xZ)));
     318             : }
     319             : 
     320             : static GEN
     321      141349 : addmul_col(GEN a, long s, GEN b)
     322             : {
     323             :   long i,l;
     324      141349 :   if (!s) return a? leafcopy(a): a;
     325      141096 :   if (!a) return gmulsg(s,b);
     326      130719 :   l = lg(a);
     327      605921 :   for (i=1; i<l; i++)
     328      475213 :     if (signe(gel(b,i))) gel(a,i) = addii(gel(a,i), mulsi(s, gel(b,i)));
     329      130708 :   return a;
     330             : }
     331             : 
     332             : /* a <-- a + s * b, all coeffs integers */
     333             : static GEN
     334       75665 : addmul_mat(GEN a, long s, GEN b)
     335             : {
     336             :   long j,l;
     337             :   /* copy otherwise next call corrupts a */
     338       75665 :   if (!s) return a? RgM_shallowcopy(a): a;
     339       70554 :   if (!a) return gmulsg(s,b);
     340       35122 :   l = lg(a);
     341      153798 :   for (j=1; j<l; j++)
     342      118678 :     (void)addmul_col(gel(a,j), s, gel(b,j));
     343       35120 :   return a;
     344             : }
     345             : 
     346             : static GEN
     347      256907 : get_random_a(GEN nf, GEN x, GEN xZ)
     348             : {
     349             :   pari_sp av;
     350      256907 :   long i, lm, l = lg(x);
     351             :   GEN a, z, beta, mul;
     352             : 
     353      256907 :   beta= cgetg(l, t_VEC);
     354      256907 :   mul = cgetg(l, t_VEC); lm = 1; /* = lg(mul) */
     355             :   /* look for a in x such that a O/xZ = x O/xZ */
     356      475081 :   for (i = 2; i < l; i++)
     357             :   {
     358      464704 :     GEN xi = gel(x,i);
     359      464704 :     GEN t = FpM_red(zk_multable(nf,xi), xZ); /* ZM, cannot be a scalar */
     360      464695 :     if (gequal0(t)) continue;
     361      405178 :     if (ok_elt(x,xZ, t)) return xi;
     362      158651 :     gel(beta,lm) = xi;
     363             :     /* mul[i] = { canonical generators for x[i] O/xZ as Z-module } */
     364      158651 :     gel(mul,lm) = t; lm++;
     365             :   }
     366       10377 :   setlg(mul, lm);
     367       10377 :   setlg(beta,lm);
     368       10377 :   z = cgetg(lm, t_VECSMALL);
     369       35635 :   for(av = avma;; set_avma(av))
     370             :   {
     371      111300 :     for (a=NULL,i=1; i<lm; i++)
     372             :     {
     373       75665 :       long t = random_bits(4) - 7; /* in [-7,8] */
     374       75665 :       z[i] = t;
     375       75665 :       a = addmul_mat(a, t, gel(mul,i));
     376             :     }
     377             :     /* a = matrix (NOT HNF) of ideal generated by beta.z in O/xZ */
     378       35635 :     if (a && ok_elt(x,xZ, a)) break;
     379             :   }
     380       33048 :   for (a=NULL,i=1; i<lm; i++)
     381       22671 :     a = addmul_col(a, z[i], gel(beta,i));
     382       10377 :   return a;
     383             : }
     384             : 
     385             : /* x square matrix, assume it is HNF */
     386             : static GEN
     387      405410 : mat_ideal_two_elt(GEN nf, GEN x)
     388             : {
     389             :   GEN y, a, cx, xZ;
     390      405410 :   long N = nf_get_degree(nf);
     391             :   pari_sp av, tetpil;
     392             : 
     393      405410 :   if (lg(x)-1 != N) pari_err_DIM("idealtwoelt");
     394      405396 :   if (N == 2) return mkvec2copy(gcoeff(x,1,1), gel(x,2));
     395             : 
     396      280647 :   y = cgetg(3,t_VEC); av = avma;
     397      280647 :   cx = Q_content(x);
     398      280648 :   xZ = gcoeff(x,1,1);
     399      280648 :   if (gequal(xZ, cx)) /* x = (cx) */
     400             :   {
     401        4966 :     gel(y,1) = cx;
     402        4966 :     gel(y,2) = gen_0; return y;
     403             :   }
     404      275682 :   if (equali1(cx)) cx = NULL;
     405             :   else
     406             :   {
     407        1533 :     x = Q_div_to_int(x, cx);
     408        1533 :     xZ = gcoeff(x,1,1);
     409             :   }
     410      275682 :   if (N < 6)
     411      238578 :     a = get_random_a(nf, x, xZ);
     412             :   else
     413             :   {
     414       37104 :     const long FB[] = { _evallg(15+1) | evaltyp(t_VECSMALL),
     415             :       2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47
     416             :     };
     417       37104 :     GEN P, E, a1 = Z_lsmoothen(xZ, (GEN)FB, &P, &E);
     418       37104 :     if (!a1) /* factors completely */
     419       18775 :       a = idealapprfact_i(nf, idealfactor(nf,x), 1);
     420       18329 :     else if (lg(P) == 1) /* no small factors */
     421       11029 :       a = get_random_a(nf, x, xZ);
     422             :     else /* general case */
     423             :     {
     424             :       GEN A0, A1, a0, u0, u1, v0, v1, pi0, pi1, t, u;
     425        7300 :       a0 = diviiexact(xZ, a1);
     426        7300 :       A0 = ZM_hnfmodid(x, a0); /* smooth part of x */
     427        7300 :       A1 = ZM_hnfmodid(x, a1); /* cofactor */
     428        7300 :       pi0 = idealapprfact_i(nf, idealfactor(nf,A0), 1);
     429        7300 :       pi1 = get_random_a(nf, A1, a1);
     430        7300 :       (void)bezout(a0, a1, &v0,&v1);
     431        7300 :       u0 = mulii(a0, v0);
     432        7300 :       u1 = mulii(a1, v1);
     433        7300 :       if (typ(pi0) != t_COL) t = addmulii(u0, pi0, u1);
     434             :       else
     435        7300 :       { t = ZC_Z_mul(pi0, u1); gel(t,1) = addii(gel(t,1), u0); }
     436        7300 :       u = ZC_Z_mul(pi1, u0); gel(u,1) = addii(gel(u,1), u1);
     437        7300 :       a = nfmuli(nf, centermod(u, xZ), centermod(t, xZ));
     438             :     }
     439             :   }
     440      275681 :   if (cx)
     441             :   {
     442        1533 :     a = centermod(a, xZ);
     443        1533 :     tetpil = avma;
     444        1533 :     if (typ(cx) == t_INT)
     445             :     {
     446          70 :       gel(y,1) = mulii(xZ, cx);
     447          70 :       gel(y,2) = ZC_Z_mul(a, cx);
     448             :     }
     449             :     else
     450             :     {
     451        1463 :       gel(y,1) = gmul(xZ, cx);
     452        1463 :       gel(y,2) = RgC_Rg_mul(a, cx);
     453             :     }
     454             :   }
     455             :   else
     456             :   {
     457      274148 :     tetpil = avma;
     458      274148 :     gel(y,1) = icopy(xZ);
     459      274149 :     gel(y,2) = centermod(a, xZ);
     460             :   }
     461      275681 :   gerepilecoeffssp(av,tetpil,y+1,2); return y;
     462             : }
     463             : 
     464             : /* Given an ideal x, returns [a,alpha] such that a is in Q,
     465             :  * x = a Z_K + alpha Z_K, alpha in K^*
     466             :  * a = 0 or alpha = 0 are possible, but do not try to determine whether
     467             :  * x is principal. */
     468             : GEN
     469       99224 : idealtwoelt(GEN nf, GEN x)
     470             : {
     471             :   pari_sp av;
     472       99224 :   long tx = idealtyp(&x, NULL);
     473       99218 :   nf = checknf(nf);
     474       99218 :   if (tx == id_MAT) return mat_ideal_two_elt(nf,x);
     475        1050 :   if (tx == id_PRIME) return mkvec2copy(gel(x,1), gel(x,2));
     476             :   /* id_PRINCIPAL */
     477        1043 :   av = avma; x = nf_to_scalar_or_basis(nf, x);
     478        1890 :   return gerepilecopy(av, typ(x)==t_COL? mkvec2(gen_0,x):
     479         938 :                                          mkvec2(Q_abs_shallow(x),gen_0));
     480             : }
     481             : 
     482             : /*******************************************************************/
     483             : /*                                                                 */
     484             : /*                         FACTORIZATION                           */
     485             : /*                                                                 */
     486             : /*******************************************************************/
     487             : /* x integral ideal in HNF, Zval = v_p(x \cap Z) > 0; return v_p(Nx) */
     488             : static long
     489      734383 : idealHNF_norm_pval(GEN x, GEN p, long Zval)
     490             : {
     491      734383 :   long i, v = Zval, l = lg(x);
     492     2387319 :   for (i = 2; i < l; i++) v += Z_pval(gcoeff(x,i,i), p);
     493      734381 :   return v;
     494             : }
     495             : 
     496             : /* x integral in HNF, f0 = partial factorization of a multiple of
     497             :  * x[1,1] = x\cap Z */
     498             : GEN
     499      278860 : idealHNF_Z_factor_i(GEN x, GEN f0, GEN *pvN, GEN *pvZ)
     500             : {
     501      278860 :   GEN P, E, vN, vZ, xZ = gcoeff(x,1,1), f = f0? f0: Z_factor(xZ);
     502             :   long i, l;
     503      278868 :   P = gel(f,1); l = lg(P);
     504      278868 :   E = gel(f,2);
     505      278868 :   *pvN = vN = cgetg(l, t_VECSMALL);
     506      278873 :   *pvZ = vZ = cgetg(l, t_VECSMALL);
     507      712471 :   for (i = 1; i < l; i++)
     508             :   {
     509      433602 :     GEN p = gel(P,i);
     510      433602 :     vZ[i] = f0? Z_pval(xZ, p): (long) itou(gel(E,i));
     511      433602 :     vN[i] = idealHNF_norm_pval(x,p, vZ[i]);
     512             :   }
     513      278869 :   return P;
     514             : }
     515             : /* return P, primes dividing Nx and xZ = x\cap Z, set v_p(Nx), v_p(xZ);
     516             :  * x integral in HNF */
     517             : GEN
     518           0 : idealHNF_Z_factor(GEN x, GEN *pvN, GEN *pvZ)
     519           0 : { return idealHNF_Z_factor_i(x, NULL, pvN, pvZ); }
     520             : 
     521             : /* v_P(A)*f(P) <= Nval [e.g. Nval = v_p(Norm A)], Zval = v_p(A \cap Z).
     522             :  * Return v_P(A) */
     523             : static long
     524      905054 : idealHNF_val(GEN A, GEN P, long Nval, long Zval)
     525             : {
     526      905054 :   long f = pr_get_f(P), vmax, v, e, i, j, k, l;
     527             :   GEN mul, B, a, y, r, p, pk, cx, vals;
     528             :   pari_sp av;
     529             : 
     530      905054 :   if (Nval < f) return 0;
     531      904753 :   p = pr_get_p(P);
     532      904755 :   e = pr_get_e(P);
     533             :   /* v_P(A) <= max [ e * v_p(A \cap Z), floor[v_p(Nix) / f ] */
     534      904757 :   vmax = minss(Zval * e, Nval / f);
     535      904754 :   mul = pr_get_tau(P);
     536      904751 :   l = lg(mul);
     537      904751 :   B = cgetg(l,t_MAT);
     538             :   /* B[1] not needed: v_pr(A[1]) = v_pr(A \cap Z) is known already */
     539      905116 :   gel(B,1) = gen_0; /* dummy */
     540     2816116 :   for (j = 2; j < l; j++)
     541             :   {
     542     2149105 :     GEN x = gel(A,j);
     543     2149105 :     gel(B,j) = y = cgetg(l, t_COL);
     544    18119298 :     for (i = 1; i < l; i++)
     545             :     { /* compute a = (x.t0)_i, A in HNF ==> x[j+1..l-1] = 0 */
     546    16208298 :       a = mulii(gel(x,1), gcoeff(mul,i,1));
     547   143982379 :       for (k = 2; k <= j; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     548             :       /* p | a ? */
     549    16207845 :       gel(y,i) = dvmdii(a,p,&r); if (signe(r)) return 0;
     550             :     }
     551             :   }
     552      667011 :   vals = cgetg(l, t_VECSMALL);
     553             :   /* vals[1] not needed */
     554     2378580 :   for (j = 2; j < l; j++)
     555             :   {
     556     1711490 :     gel(B,j) = Q_primitive_part(gel(B,j), &cx);
     557     1711507 :     vals[j] = cx? 1 + e * Q_pval(cx, p): 1;
     558             :   }
     559      667090 :   pk = powiu(p, ceildivuu(vmax, e));
     560      667072 :   av = avma; y = cgetg(l,t_COL);
     561             :   /* can compute mod p^ceil((vmax-v)/e) */
     562     1262857 :   for (v = 1; v < vmax; v++)
     563             :   { /* we know v_pr(Bj) >= v for all j */
     564      622429 :     if (e == 1 || (vmax - v) % e == 0) pk = diviiexact(pk, p);
     565     3609366 :     for (j = 2; j < l; j++)
     566             :     {
     567     3013612 :       GEN x = gel(B,j); if (v < vals[j]) continue;
     568    18288305 :       for (i = 1; i < l; i++)
     569             :       {
     570    16552586 :         pari_sp av2 = avma;
     571    16552586 :         a = mulii(gel(x,1), gcoeff(mul,i,1));
     572   262829377 :         for (k = 2; k < l; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     573             :         /* a = (x.t_0)_i; p | a ? */
     574    16551056 :         a = dvmdii(a,p,&r); if (signe(r)) return v;
     575    16524368 :         if (lgefint(a) > lgefint(pk)) a = remii(a, pk);
     576    16524393 :         gel(y,i) = gerepileuptoint(av2, a);
     577             :       }
     578     1735719 :       gel(B,j) = y; y = x;
     579     1735719 :       if (gc_needed(av,3))
     580             :       {
     581           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"idealval");
     582           0 :         gerepileall(av,3, &y,&B,&pk);
     583             :       }
     584             :     }
     585             :   }
     586      640428 :   return v;
     587             : }
     588             : /* true nf, x != 0 integral ideal in HNF, cx t_INT or NULL,
     589             :  * FA integer factorization matrix or NULL. Return partial factorization of
     590             :  * cx * x above primes in FA (complete factorization if !FA)*/
     591             : static GEN
     592      278856 : idealHNF_factor_i(GEN nf, GEN x, GEN cx, GEN FA)
     593             : {
     594      278856 :   const long N = lg(x)-1;
     595             :   long i, j, k, l, v;
     596      278856 :   GEN vN, vZ, vP, vE, vp = idealHNF_Z_factor_i(x, FA, &vN,&vZ);
     597             : 
     598      278868 :   l = lg(vp);
     599      278868 :   i = cx? expi(cx)+1: 1;
     600      278876 :   vP = cgetg((l+i-2)*N+1, t_COL);
     601      278869 :   vE = cgetg((l+i-2)*N+1, t_COL);
     602      712447 :   for (i = k = 1; i < l; i++)
     603             :   {
     604      433594 :     GEN L, p = gel(vp,i);
     605      433594 :     long Nval = vN[i], Zval = vZ[i], vc = cx? Z_pvalrem(cx,p,&cx): 0;
     606      433585 :     if (vc)
     607             :     {
     608       45616 :       L = idealprimedec(nf,p);
     609       45618 :       if (is_pm1(cx)) cx = NULL;
     610             :     }
     611             :     else
     612      387969 :       L = idealprimedec_limit_f(nf,p,Nval);
     613     1037834 :     for (j = 1; Nval && j < lg(L); j++) /* !Nval => only cx contributes */
     614             :     {
     615      604264 :       GEN P = gel(L,j);
     616      604264 :       pari_sp av = avma;
     617      604264 :       v = idealHNF_val(x, P, Nval, Zval);
     618      604231 :       set_avma(av);
     619      604233 :       Nval -= v*pr_get_f(P);
     620      604233 :       v += vc * pr_get_e(P); if (!v) continue;
     621      483940 :       gel(vP,k) = P;
     622      483940 :       gel(vE,k) = utoipos(v); k++;
     623             :     }
     624      477867 :     if (vc) for (; j<lg(L); j++)
     625             :     {
     626       44299 :       GEN P = gel(L,j);
     627       44299 :       gel(vP,k) = P;
     628       44299 :       gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     629             :     }
     630             :   }
     631      278853 :   if (cx && !FA)
     632             :   { /* complete factorization */
     633       60086 :     GEN f = Z_factor(cx), cP = gel(f,1), cE = gel(f,2);
     634       60086 :     long lc = lg(cP);
     635      128070 :     for (i=1; i<lc; i++)
     636             :     {
     637       67982 :       GEN p = gel(cP,i), L = idealprimedec(nf,p);
     638       67984 :       long vc = itos(gel(cE,i));
     639      146454 :       for (j=1; j<lg(L); j++)
     640             :       {
     641       78470 :         GEN P = gel(L,j);
     642       78470 :         gel(vP,k) = P;
     643       78470 :         gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     644             :       }
     645             :     }
     646             :   }
     647      278855 :   setlg(vP, k);
     648      278867 :   setlg(vE, k); return mkmat2(vP, vE);
     649             : }
     650             : /* true nf, x integral ideal */
     651             : static GEN
     652      233558 : idealHNF_factor(GEN nf, GEN x, ulong lim)
     653             : {
     654      233558 :   GEN cx, F = NULL;
     655      233558 :   if (lim)
     656             :   {
     657             :     GEN P, E;
     658             :     long i;
     659             :     /* strict useless because of prime table */
     660          42 :     F = absZ_factor_limit(gcoeff(x,1,1), lim);
     661          42 :     P = gel(F,1);
     662          42 :     E = gel(F,2);
     663             :     /* filter out entries > lim */
     664          77 :     for (i = lg(P)-1; i; i--)
     665          77 :       if (cmpiu(gel(P,i), lim) <= 0) break;
     666          42 :     setlg(P, i+1);
     667          42 :     setlg(E, i+1);
     668             :   }
     669      233558 :   x = Q_primitive_part(x, &cx);
     670      233549 :   return idealHNF_factor_i(nf, x, cx, F);
     671             : }
     672             : /* c * vector(#L,i,L[i].e), assume results fit in ulong */
     673             : static GEN
     674       33025 : prV_e_muls(GEN L, long c)
     675             : {
     676       33025 :   long j, l = lg(L);
     677       33025 :   GEN z = cgetg(l, t_COL);
     678       67574 :   for (j = 1; j < l; j++) gel(z,j) = stoi(c * pr_get_e(gel(L,j)));
     679       33034 :   return z;
     680             : }
     681             : /* true nf, y in Q */
     682             : static GEN
     683       26317 : Q_nffactor(GEN nf, GEN y, ulong lim)
     684             : {
     685             :   GEN f, P, E;
     686             :   long l, i;
     687       26317 :   if (typ(y) == t_INT)
     688             :   {
     689       26294 :     if (!signe(y)) pari_err_DOMAIN("idealfactor", "ideal", "=",gen_0,y);
     690       26280 :     if (is_pm1(y)) return trivial_fact();
     691             :   }
     692       17249 :   y = Q_abs_shallow(y);
     693       17256 :   if (!lim) f = Q_factor(y);
     694             :   else
     695             :   {
     696          35 :     f = Q_factor_limit(y, lim);
     697          35 :     P = gel(f,1);
     698          35 :     E = gel(f,2);
     699          77 :     for (i = lg(P)-1; i > 0; i--)
     700          63 :       if (abscmpiu(gel(P,i), lim) < 0) break;
     701          35 :     setlg(P,i+1); setlg(E,i+1);
     702             :   }
     703       17247 :   P = gel(f,1); l = lg(P); if (l == 1) return f;
     704       17233 :   E = gel(f,2);
     705       50310 :   for (i = 1; i < l; i++)
     706             :   {
     707       33056 :     gel(P,i) = idealprimedec(nf, gel(P,i));
     708       33009 :     gel(E,i) = prV_e_muls(gel(P,i), itos(gel(E,i)));
     709             :   }
     710       17254 :   P = shallowconcat1(P); gel(f,1) = P; settyp(P, t_COL);
     711       17275 :   E = shallowconcat1(E); gel(f,2) = E; return f;
     712             : }
     713             : 
     714             : GEN
     715        3696 : idealfactor_partial(GEN nf, GEN x, GEN L)
     716             : {
     717        3696 :   pari_sp av = avma;
     718             :   long i, j, l;
     719             :   GEN P, E;
     720        3696 :   if (!L) return idealfactor(nf, x);
     721        2975 :   if (typ(L) == t_INT) return idealfactor_limit(nf, x, itou(L));
     722        2954 :   l = lg(L); if (l == 1) return trivial_fact();
     723        2170 :   P = cgetg(l, t_VEC);
     724        4480 :   for (i = 1; i < l; i++)
     725             :   {
     726        2310 :     GEN p = gel(L,i);
     727        2310 :     gel(P,i) = typ(p) == t_INT? idealprimedec(nf, p): mkvec(p);
     728             :   }
     729        2170 :   P = shallowconcat1(P); settyp(P, t_COL);
     730        2170 :   P = gen_sort_uniq(P, (void*)&cmp_prime_ideal, &cmp_nodata);
     731        2170 :   E = cgetg_copy(P, &l);
     732       12215 :   for (i = j = 1; i < l; i++)
     733             :   {
     734       10045 :     long v = idealval(nf, x, gel(P,i));
     735       10045 :     if (v) { gel(P,j) = gel(P,i); gel(E,j) = stoi(v); j++; }
     736             :   }
     737        2170 :   setlg(P,j);
     738        2170 :   setlg(E,j); return gerepilecopy(av, mkmat2(P, E));
     739             : }
     740             : GEN
     741      259961 : idealfactor_limit(GEN nf, GEN x, ulong lim)
     742             : {
     743      259961 :   pari_sp av = avma;
     744             :   GEN fa, y;
     745      259961 :   long tx = idealtyp(&x, NULL);
     746             : 
     747      259956 :   if (tx == id_PRIME)
     748             :   {
     749          98 :     if (lim && abscmpiu(pr_get_p(x), lim) >= 0) return trivial_fact();
     750          91 :     retmkmat2(mkcolcopy(x), mkcol(gen_1));
     751             :   }
     752      259858 :   nf = checknf(nf);
     753      259858 :   if (tx == id_PRINCIPAL)
     754             :   {
     755       27806 :     y = nf_to_scalar_or_basis(nf, x);
     756       27807 :     if (typ(y) != t_COL) return gerepilecopy(av, Q_nffactor(nf, y, lim));
     757             :   }
     758      233534 :   y = idealnumden(nf, x);
     759      233547 :   fa = idealHNF_factor(nf, gel(y,1), lim);
     760      233495 :   if (!isint1(gel(y,2)))
     761          14 :     fa = famat_div_shallow(fa, idealHNF_factor(nf, gel(y,2), lim));
     762      233547 :   fa = gerepilecopy(av, fa);
     763      233555 :   return sort_factor(fa, (void*)&cmp_prime_ideal, &cmp_nodata);
     764             : }
     765             : GEN
     766      259761 : idealfactor(GEN nf, GEN x) { return idealfactor_limit(nf, x, 0); }
     767             : GEN
     768         182 : gpidealfactor(GEN nf, GEN x, GEN lim)
     769             : {
     770         182 :   ulong L = 0;
     771         182 :   if (lim)
     772             :   {
     773          70 :     if (typ(lim) != t_INT || signe(lim) < 0) pari_err_FLAG("idealfactor");
     774          70 :     L = itou(lim);
     775             :   }
     776         182 :   return idealfactor_limit(nf, x, L);
     777             : }
     778             : 
     779             : static GEN
     780        7532 : ramified_root(GEN nf, GEN R, GEN A, long n)
     781             : {
     782        7532 :   GEN v, P = gel(idealfactor(nf, R), 1);
     783        7532 :   long i, l = lg(P);
     784        7532 :   v = cgetg(l, t_VECSMALL);
     785        7994 :   for (i = 1; i < l; i++)
     786             :   {
     787         469 :     long w = idealval(nf, A, gel(P,i));
     788         469 :     if (w % n) return NULL;
     789         462 :     v[i] = w / n;
     790             :   }
     791        7525 :   return idealfactorback(nf, P, v, 0);
     792             : }
     793             : static int
     794           0 : ramified_root_simple(GEN nf, long n, GEN P, GEN v)
     795             : {
     796           0 :   long i, l = lg(v);
     797           0 :   for (i = 1; i < l; i++) if (v[i])
     798             :   {
     799           0 :     GEN vpr = idealprimedec(nf, gel(P,i));
     800           0 :     long lpr = lg(vpr), j;
     801           0 :     for (j = 1; j < lpr; j++)
     802             :     {
     803           0 :       long e = pr_get_e(gel(vpr,j));
     804           0 :       if ((e * v[i]) % n) return 0;
     805             :     }
     806             :   }
     807           0 :   return 1;
     808             : }
     809             : /* true nf; A is assumed to be the n-th power of an integral ideal,
     810             :  * return its n-th root; n > 1 */
     811             : static long
     812        7532 : idealsqrtn_int(GEN nf, GEN A, long n, GEN *pB)
     813             : {
     814             :   GEN C, root;
     815             :   long i, l;
     816             : 
     817        7532 :   if (typ(A) == t_INT) /* > 0 */
     818             :   {
     819        3766 :     GEN P = nf_get_ramified_primes(nf), v, q;
     820        3766 :     l = lg(P); v = cgetg(l, t_VECSMALL);
     821       17031 :     for (i = 1; i < l; i++) v[i] = Z_pvalrem(A, gel(P,i), &A);
     822        3766 :     C = gen_1;
     823        3766 :     if (!isint1(A) && !Z_ispowerall(A, n, pB? &C: NULL)) return 0;
     824        3766 :     if (!pB) return ramified_root_simple(nf, n, P, v);
     825        3766 :     q = factorback2(P, v);
     826        3766 :     root = ramified_root(nf, q, q, n);
     827        3766 :     if (!root) return 0;
     828        3766 :     if (!equali1(C)) root = isint1(root)? C: ZM_Z_mul(root, C);
     829        3766 :     *pB = root; return 1;
     830             :   }
     831             :   /* compute valuations at ramified primes */
     832        3766 :   root = ramified_root(nf, idealadd(nf, nf_get_diff(nf), A), A, n);
     833        3766 :   if (!root) return 0;
     834             :   /* remove ramified primes */
     835        3759 :   if (isint1(root))
     836        3325 :     root = matid(nf_get_degree(nf));
     837             :   else
     838         434 :     A = idealdivexact(nf, A, idealpows(nf,root,n));
     839        3759 :   A = Q_primitive_part(A, &C);
     840        3759 :   if (C)
     841             :   {
     842         245 :     if (!Z_ispowerall(C,n,&C)) return 0;
     843         245 :     if (pB) root = ZM_Z_mul(root, C);
     844             :   }
     845             : 
     846             :   /* compute final n-th root, at most degree(nf)-1 iterations */
     847        3759 :   for (i = 0;; i++)
     848        2387 :   {
     849        6146 :     GEN J, b, a = gcoeff(A,1,1); /* A \cap Z */
     850        6146 :     if (is_pm1(a)) break;
     851        2408 :     if (!Z_ispowerall(a,n,&b)) return 0;
     852        2387 :     J = idealadd(nf, b, A);
     853        2387 :     A = idealdivexact(nf, idealpows(nf,J,n), A);
     854             :     /* div and not divexact here */
     855        2387 :     if (pB) root = odd(i)? idealdiv(nf, root, J): idealmul(nf, root, J);
     856             :   }
     857        3738 :   if (pB) *pB = root;
     858        3738 :   return 1;
     859             : }
     860             : 
     861             : /* A is assumed to be the n-th power of an ideal in nf
     862             :  returns its n-th root. */
     863             : long
     864        3787 : idealispower(GEN nf, GEN A, long n, GEN *pB)
     865             : {
     866        3787 :   pari_sp av = avma;
     867             :   GEN v, N, D;
     868        3787 :   nf = checknf(nf);
     869        3787 :   if (n <= 0) pari_err_DOMAIN("idealispower", "n", "<=", gen_0, stoi(n));
     870        3787 :   if (n == 1) { if (pB) *pB = idealhnf(nf,A); return 1; }
     871        3780 :   v = idealnumden(nf,A);
     872        3780 :   if (gequal0(gel(v,1))) { set_avma(av); if (pB) *pB = cgetg(1,t_MAT); return 1; }
     873        3780 :   if (!idealsqrtn_int(nf, gel(v,1), n, pB? &N: NULL)) return 0;
     874        3752 :   if (!idealsqrtn_int(nf, gel(v,2), n, pB? &D: NULL)) return 0;
     875        3752 :   if (pB) *pB = gerepileupto(av, idealdiv(nf,N,D)); else set_avma(av);
     876        3752 :   return 1;
     877             : }
     878             : 
     879             : /* x t_INT or integral nonzero ideal in HNF */
     880             : static GEN
     881       96572 : idealredmodpower_i(GEN nf, GEN x, ulong k, ulong B)
     882             : {
     883             :   GEN cx, y, U, N, F, Q;
     884       96572 :   if (typ(x) == t_INT)
     885             :   {
     886       50687 :     if (!signe(x) || is_pm1(x)) return gen_1;
     887        1785 :     F = Z_factor_limit(x, B);
     888        1785 :     gel(F,2) = gdiventgs(gel(F,2), k);
     889        1785 :     return ginv(factorback(F));
     890             :   }
     891       45885 :   N = gcoeff(x,1,1); if (is_pm1(N)) return gen_1;
     892       45311 :   F = absZ_factor_limit_strict(N, B, &U);
     893       45310 :   if (U)
     894             :   {
     895         336 :     GEN M = powii(gel(U,1), gel(U,2));
     896         336 :     y = hnfmodid(x, M); /* coprime part to B! */
     897         336 :     if (!idealispower(nf, y, k, &U)) U = NULL;
     898         336 :     x = hnfmodid(x, diviiexact(N, M));
     899             :   }
     900             :   /* x = B-smooth part of initial x */
     901       45310 :   x = Q_primitive_part(x, &cx);
     902       45310 :   F = idealHNF_factor_i(nf, x, cx, F);
     903       45310 :   gel(F,2) = gdiventgs(gel(F,2), k);
     904       45311 :   Q = idealfactorback(nf, gel(F,1), gel(F,2), 0);
     905       45310 :   if (U) Q = idealmul(nf,Q,U);
     906       45311 :   if (typ(Q) == t_INT) return Q;
     907       13733 :   y = idealred_elt(nf, idealHNF_inv_Z(nf, Q));
     908       13733 :   return gdiv(y, gcoeff(Q,1,1));
     909             : }
     910             : GEN
     911       48293 : idealredmodpower(GEN nf, GEN x, ulong n, ulong B)
     912             : {
     913       48293 :   pari_sp av = avma;
     914             :   GEN a, b;
     915       48293 :   nf = checknf(nf);
     916       48293 :   if (!n) pari_err_DOMAIN("idealredmodpower","n", "=", gen_0, gen_0);
     917       48293 :   x = idealnumden(nf, x);
     918       48293 :   a = gel(x,1);
     919       48293 :   if (isintzero(a)) { set_avma(av); return gen_1; }
     920       48286 :   a = idealredmodpower_i(nf, gel(x,1), n, B);
     921       48286 :   b = idealredmodpower_i(nf, gel(x,2), n, B);
     922       48286 :   if (!isint1(b)) a = nf_to_scalar_or_basis(nf, nfdiv(nf, a, b));
     923       48286 :   return gerepilecopy(av, a);
     924             : }
     925             : 
     926             : /* P prime ideal in idealprimedec format. Return valuation(A) at P */
     927             : long
     928      931474 : idealval(GEN nf, GEN A, GEN P)
     929             : {
     930      931474 :   pari_sp av = avma;
     931             :   GEN p, cA;
     932      931474 :   long vcA, v, Zval, tx = idealtyp(&A, NULL);
     933             : 
     934      931475 :   if (tx == id_PRINCIPAL) return nfval(nf,A,P);
     935      847704 :   checkprid(P);
     936      847705 :   if (tx == id_PRIME) return pr_equal(P, A)? 1: 0;
     937             :   /* id_MAT */
     938      847677 :   nf = checknf(nf);
     939      847677 :   A = Q_primitive_part(A, &cA);
     940      847676 :   p = pr_get_p(P);
     941      847677 :   vcA = cA? Q_pval(cA,p): 0;
     942      847677 :   if (pr_is_inert(P)) return gc_long(av,vcA);
     943      834798 :   Zval = Z_pval(gcoeff(A,1,1), p);
     944      834799 :   if (!Zval) v = 0;
     945             :   else
     946             :   {
     947      300796 :     long Nval = idealHNF_norm_pval(A, p, Zval);
     948      300795 :     v = idealHNF_val(A, P, Nval, Zval);
     949             :   }
     950      834795 :   return gc_long(av, vcA? v + vcA*pr_get_e(P): v);
     951             : }
     952             : GEN
     953        7105 : gpidealval(GEN nf, GEN ix, GEN P)
     954             : {
     955        7105 :   long v = idealval(nf,ix,P);
     956        7105 :   return v == LONG_MAX? mkoo(): stoi(v);
     957             : }
     958             : 
     959             : /* gcd and generalized Bezout */
     960             : 
     961             : GEN
     962       73630 : idealadd(GEN nf, GEN x, GEN y)
     963             : {
     964       73630 :   pari_sp av = avma;
     965             :   long tx, ty;
     966             :   GEN z, a, dx, dy, dz;
     967             : 
     968       73630 :   tx = idealtyp(&x, NULL);
     969       73630 :   ty = idealtyp(&y, NULL); nf = checknf(nf);
     970       73630 :   if (tx != id_MAT) x = idealhnf_shallow(nf,x);
     971       73630 :   if (ty != id_MAT) y = idealhnf_shallow(nf,y);
     972       73630 :   if (lg(x) == 1) return gerepilecopy(av,y);
     973       73504 :   if (lg(y) == 1) return gerepilecopy(av,x); /* check for 0 ideal */
     974       73049 :   dx = Q_denom(x);
     975       73049 :   dy = Q_denom(y); dz = lcmii(dx,dy);
     976       73049 :   if (is_pm1(dz)) dz = NULL; else {
     977       13083 :     x = Q_muli_to_int(x, dz);
     978       13083 :     y = Q_muli_to_int(y, dz);
     979             :   }
     980       73049 :   a = gcdii(gcoeff(x,1,1), gcoeff(y,1,1));
     981       73049 :   if (is_pm1(a))
     982             :   {
     983       34601 :     long N = lg(x)-1;
     984       34601 :     if (!dz) { set_avma(av); return matid(N); }
     985        3794 :     return gerepileupto(av, scalarmat(ginv(dz), N));
     986             :   }
     987       38448 :   z = ZM_hnfmodid(shallowconcat(x,y), a);
     988       38448 :   if (dz) z = RgM_Rg_div(z,dz);
     989       38448 :   return gerepileupto(av,z);
     990             : }
     991             : 
     992             : static GEN
     993          28 : trivial_merge(GEN x)
     994          28 : { return (lg(x) == 1 || !is_pm1(gcoeff(x,1,1)))? NULL: gen_1; }
     995             : /* true nf */
     996             : static GEN
     997      621833 : _idealaddtoone(GEN nf, GEN x, GEN y, long red)
     998             : {
     999             :   GEN a;
    1000      621833 :   long tx = idealtyp(&x, NULL);
    1001      621819 :   long ty = idealtyp(&y, NULL);
    1002             :   long ea;
    1003      621823 :   if (tx != id_MAT) x = idealhnf_shallow(nf, x);
    1004      621852 :   if (ty != id_MAT) y = idealhnf_shallow(nf, y);
    1005      621852 :   if (lg(x) == 1)
    1006          14 :     a = trivial_merge(y);
    1007      621838 :   else if (lg(y) == 1)
    1008          14 :     a = trivial_merge(x);
    1009             :   else
    1010      621824 :     a = hnfmerge_get_1(x, y);
    1011      621787 :   if (!a) pari_err_COPRIME("idealaddtoone",x,y);
    1012      621768 :   if (red && (ea = gexpo(a)) > 10)
    1013             :   {
    1014        5425 :     GEN b = (typ(a) == t_COL)? a: scalarcol_shallow(a, nf_get_degree(nf));
    1015        5425 :     b = ZC_reducemodlll(b, idealHNF_mul(nf,x,y));
    1016        5425 :     if (gexpo(b) < ea) a = b;
    1017             :   }
    1018      621768 :   return a;
    1019             : }
    1020             : /* true nf */
    1021             : GEN
    1022       18137 : idealaddtoone_i(GEN nf, GEN x, GEN y)
    1023       18137 : { return _idealaddtoone(nf, x, y, 1); }
    1024             : /* true nf */
    1025             : GEN
    1026      603698 : idealaddtoone_raw(GEN nf, GEN x, GEN y)
    1027      603698 : { return _idealaddtoone(nf, x, y, 0); }
    1028             : 
    1029             : GEN
    1030          98 : idealaddtoone(GEN nf, GEN x, GEN y)
    1031             : {
    1032          98 :   GEN z = cgetg(3,t_VEC), a;
    1033          98 :   pari_sp av = avma;
    1034          98 :   nf = checknf(nf);
    1035          98 :   a = gerepileupto(av, idealaddtoone_i(nf,x,y));
    1036          84 :   gel(z,1) = a;
    1037          84 :   gel(z,2) = typ(a) == t_COL? Z_ZC_sub(gen_1,a): subui(1,a);
    1038          84 :   return z;
    1039             : }
    1040             : 
    1041             : /* assume elements of list are integral ideals */
    1042             : GEN
    1043          35 : idealaddmultoone(GEN nf, GEN list)
    1044             : {
    1045          35 :   pari_sp av = avma;
    1046          35 :   long N, i, l, nz, tx = typ(list);
    1047             :   GEN H, U, perm, L;
    1048             : 
    1049          35 :   nf = checknf(nf); N = nf_get_degree(nf);
    1050          35 :   if (!is_vec_t(tx)) pari_err_TYPE("idealaddmultoone",list);
    1051          35 :   l = lg(list);
    1052          35 :   L = cgetg(l, t_VEC);
    1053          35 :   if (l == 1)
    1054           0 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
    1055          35 :   nz = 0; /* number of nonzero ideals in L */
    1056          98 :   for (i=1; i<l; i++)
    1057             :   {
    1058          70 :     GEN I = gel(list,i);
    1059          70 :     if (typ(I) != t_MAT) I = idealhnf_shallow(nf,I);
    1060          70 :     if (lg(I) != 1)
    1061             :     {
    1062          42 :       nz++; RgM_check_ZM(I,"idealaddmultoone");
    1063          35 :       if (lgcols(I) != N+1) pari_err_TYPE("idealaddmultoone [not an ideal]", I);
    1064             :     }
    1065          63 :     gel(L,i) = I;
    1066             :   }
    1067          28 :   H = ZM_hnfperm(shallowconcat1(L), &U, &perm);
    1068          28 :   if (lg(H) == 1 || !equali1(gcoeff(H,1,1)))
    1069           7 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
    1070          49 :   for (i=1; i<=N; i++)
    1071          49 :     if (perm[i] == 1) break;
    1072          21 :   U = gel(U,(nz-1)*N + i); /* (L[1]|...|L[nz]) U = 1 */
    1073          21 :   nz = 0;
    1074          63 :   for (i=1; i<l; i++)
    1075             :   {
    1076          42 :     GEN c = gel(L,i);
    1077          42 :     if (lg(c) == 1)
    1078          14 :       c = gen_0;
    1079             :     else {
    1080          28 :       c = ZM_ZC_mul(c, vecslice(U, nz*N + 1, (nz+1)*N));
    1081          28 :       nz++;
    1082             :     }
    1083          42 :     gel(L,i) = c;
    1084             :   }
    1085          21 :   return gerepilecopy(av, L);
    1086             : }
    1087             : 
    1088             : /* multiplication */
    1089             : 
    1090             : /* x integral ideal (without archimedean component) in HNF form
    1091             :  * y = [a,alpha] corresponds to the integral ideal aZ_K+alpha Z_K, a in Z,
    1092             :  * alpha a ZV or a ZM (multiplication table). Multiply them */
    1093             : static GEN
    1094     1036511 : idealHNF_mul_two(GEN nf, GEN x, GEN y)
    1095             : {
    1096     1036511 :   GEN m, a = gel(y,1), alpha = gel(y,2);
    1097             :   long i, N;
    1098             : 
    1099     1036511 :   if (typ(alpha) != t_MAT)
    1100             :   {
    1101      702983 :     alpha = zk_scalar_or_multable(nf, alpha);
    1102      702987 :     if (typ(alpha) == t_INT) /* e.g. y inert ? 0 should not (but may) occur */
    1103       18073 :       return signe(a)? ZM_Z_mul(x, gcdii(a, alpha)): cgetg(1,t_MAT);
    1104             :   }
    1105     1018442 :   N = lg(x)-1; m = cgetg((N<<1)+1,t_MAT);
    1106     4939776 :   for (i=1; i<=N; i++) gel(m,i)   = ZM_ZC_mul(alpha,gel(x,i));
    1107     4938592 :   for (i=1; i<=N; i++) gel(m,i+N) = ZC_Z_mul(gel(x,i), a);
    1108     1017471 :   return ZM_hnfmodid(m, mulii(a, gcoeff(x,1,1)));
    1109             : }
    1110             : 
    1111             : /* Assume x and y are integral in HNF form [NOT extended]. Not memory clean.
    1112             :  * HACK: ideal in y can be of the form [a,b], a in Z, b in Z_K */
    1113             : GEN
    1114      541841 : idealHNF_mul(GEN nf, GEN x, GEN y)
    1115             : {
    1116             :   GEN z;
    1117      541841 :   if (typ(y) == t_VEC)
    1118      211855 :     z = idealHNF_mul_two(nf,x,y);
    1119             :   else
    1120             :   { /* reduce one ideal to two-elt form. The smallest */
    1121      329986 :     GEN xZ = gcoeff(x,1,1), yZ = gcoeff(y,1,1);
    1122      329986 :     if (cmpii(xZ, yZ) < 0)
    1123             :     {
    1124      116815 :       if (is_pm1(xZ)) return gcopy(y);
    1125       99616 :       z = idealHNF_mul_two(nf, y, mat_ideal_two_elt(nf,x));
    1126             :     }
    1127             :     else
    1128             :     {
    1129      213172 :       if (is_pm1(yZ)) return gcopy(x);
    1130      107832 :       z = idealHNF_mul_two(nf, x, mat_ideal_two_elt(nf,y));
    1131             :     }
    1132             :   }
    1133      419304 :   return z;
    1134             : }
    1135             : 
    1136             : /* operations on elements in factored form */
    1137             : 
    1138             : GEN
    1139      233193 : famat_mul_shallow(GEN f, GEN g)
    1140             : {
    1141      233193 :   if (typ(f) != t_MAT) f = to_famat_shallow(f,gen_1);
    1142      233193 :   if (typ(g) != t_MAT) g = to_famat_shallow(g,gen_1);
    1143      233193 :   if (lgcols(f) == 1) return g;
    1144      180950 :   if (lgcols(g) == 1) return f;
    1145      179233 :   return mkmat2(shallowconcat(gel(f,1), gel(g,1)),
    1146      179228 :                 shallowconcat(gel(f,2), gel(g,2)));
    1147             : }
    1148             : GEN
    1149       88979 : famat_mulpow_shallow(GEN f, GEN g, GEN e)
    1150             : {
    1151       88979 :   if (!signe(e)) return f;
    1152       58063 :   return famat_mul_shallow(f, famat_pow_shallow(g, e));
    1153             : }
    1154             : 
    1155             : GEN
    1156      146269 : famat_mulpows_shallow(GEN f, GEN g, long e)
    1157             : {
    1158      146269 :   if (e==0) return f;
    1159      123007 :   return famat_mul_shallow(f, famat_pows_shallow(g, e));
    1160             : }
    1161             : 
    1162             : GEN
    1163       10143 : famat_div_shallow(GEN f, GEN g)
    1164       10143 : { return famat_mul_shallow(f, famat_inv_shallow(g)); }
    1165             : 
    1166             : GEN
    1167           0 : to_famat(GEN x, GEN y) { retmkmat2(mkcolcopy(x), mkcolcopy(y)); }
    1168             : GEN
    1169     1492605 : to_famat_shallow(GEN x, GEN y) { return mkmat2(mkcol(x), mkcol(y)); }
    1170             : 
    1171             : /* concat the single elt x; not gconcat since x may be a t_COL */
    1172             : static GEN
    1173      185786 : append(GEN v, GEN x)
    1174             : {
    1175      185786 :   long i, l = lg(v);
    1176      185786 :   GEN w = cgetg(l+1, typ(v));
    1177     1134825 :   for (i=1; i<l; i++) gel(w,i) = gcopy(gel(v,i));
    1178      185786 :   gel(w,i) = gcopy(x); return w;
    1179             : }
    1180             : /* add x^1 to famat f */
    1181             : static GEN
    1182      171498 : famat_add(GEN f, GEN x)
    1183             : {
    1184      171498 :   GEN h = cgetg(3,t_MAT);
    1185      171498 :   if (lgcols(f) == 1)
    1186             :   {
    1187       10116 :     gel(h,1) = mkcolcopy(x);
    1188       10116 :     gel(h,2) = mkcol(gen_1);
    1189             :   }
    1190             :   else
    1191             :   {
    1192      161382 :     gel(h,1) = append(gel(f,1), x);
    1193      161382 :     gel(h,2) = gconcat(gel(f,2), gen_1);
    1194             :   }
    1195      171498 :   return h;
    1196             : }
    1197             : /* add x^-1 to famat f */
    1198             : static GEN
    1199       41520 : famat_sub(GEN f, GEN x)
    1200             : {
    1201       41520 :   GEN h = cgetg(3,t_MAT);
    1202       41520 :   if (lgcols(f) == 1)
    1203             :   {
    1204       17116 :     gel(h,1) = mkcolcopy(x);
    1205       17116 :     gel(h,2) = mkcol(gen_m1);
    1206             :   }
    1207             :   else
    1208             :   {
    1209       24404 :     gel(h,1) = append(gel(f,1), x);
    1210       24404 :     gel(h,2) = gconcat(gel(f,2), gen_m1);
    1211             :   }
    1212       41520 :   return h;
    1213             : }
    1214             : 
    1215             : GEN
    1216      266568 : famat_mul(GEN f, GEN g)
    1217             : {
    1218             :   GEN h;
    1219      266568 :   if (typ(g) != t_MAT) {
    1220       43713 :     if (typ(f) == t_MAT) return famat_add(f, g);
    1221           0 :     h = cgetg(3, t_MAT);
    1222           0 :     gel(h,1) = mkcol2(gcopy(f), gcopy(g));
    1223           0 :     gel(h,2) = mkcol2(gen_1, gen_1);
    1224             :   }
    1225      222855 :   if (typ(f) != t_MAT) return famat_add(g, f);
    1226       95070 :   if (lgcols(f) == 1) return gcopy(g);
    1227       72532 :   if (lgcols(g) == 1) return gcopy(f);
    1228       68419 :   h = cgetg(3,t_MAT);
    1229       68419 :   gel(h,1) = gconcat(gel(f,1), gel(g,1));
    1230       68419 :   gel(h,2) = gconcat(gel(f,2), gel(g,2));
    1231       68419 :   return h;
    1232             : }
    1233             : 
    1234             : GEN
    1235       41527 : famat_div(GEN f, GEN g)
    1236             : {
    1237             :   GEN h;
    1238       41527 :   if (typ(g) != t_MAT) {
    1239       41478 :     if (typ(f) == t_MAT) return famat_sub(f, g);
    1240           0 :     h = cgetg(3, t_MAT);
    1241           0 :     gel(h,1) = mkcol2(gcopy(f), gcopy(g));
    1242           0 :     gel(h,2) = mkcol2(gen_1, gen_m1);
    1243             :   }
    1244          49 :   if (typ(f) != t_MAT) return famat_sub(g, f);
    1245           7 :   if (lgcols(f) == 1) return famat_inv(g);
    1246           7 :   if (lgcols(g) == 1) return gcopy(f);
    1247           7 :   h = cgetg(3,t_MAT);
    1248           7 :   gel(h,1) = gconcat(gel(f,1), gel(g,1));
    1249           7 :   gel(h,2) = gconcat(gel(f,2), gneg(gel(g,2)));
    1250           7 :   return h;
    1251             : }
    1252             : 
    1253             : GEN
    1254       23137 : famat_sqr(GEN f)
    1255             : {
    1256             :   GEN h;
    1257       23137 :   if (typ(f) != t_MAT) return to_famat(f,gen_2);
    1258       23137 :   if (lgcols(f) == 1) return gcopy(f);
    1259       14307 :   h = cgetg(3,t_MAT);
    1260       14307 :   gel(h,1) = gcopy(gel(f,1));
    1261       14307 :   gel(h,2) = gmul2n(gel(f,2),1);
    1262       14307 :   return h;
    1263             : }
    1264             : 
    1265             : GEN
    1266       26697 : famat_inv_shallow(GEN f)
    1267             : {
    1268       26697 :   if (typ(f) != t_MAT) return to_famat_shallow(f,gen_m1);
    1269       10290 :   if (lgcols(f) == 1) return f;
    1270       10290 :   return mkmat2(gel(f,1), ZC_neg(gel(f,2)));
    1271             : }
    1272             : GEN
    1273       20358 : famat_inv(GEN f)
    1274             : {
    1275       20358 :   if (typ(f) != t_MAT) return to_famat(f,gen_m1);
    1276       20358 :   if (lgcols(f) == 1) return gcopy(f);
    1277        2150 :   retmkmat2(gcopy(gel(f,1)), ZC_neg(gel(f,2)));
    1278             : }
    1279             : GEN
    1280       60298 : famat_pow(GEN f, GEN n)
    1281             : {
    1282       60298 :   if (typ(f) != t_MAT) return to_famat(f,n);
    1283       60298 :   if (lgcols(f) == 1) return gcopy(f);
    1284       60298 :   retmkmat2(gcopy(gel(f,1)), ZC_Z_mul(gel(f,2),n));
    1285             : }
    1286             : GEN
    1287       67069 : famat_pow_shallow(GEN f, GEN n)
    1288             : {
    1289       67069 :   if (is_pm1(n)) return signe(n) > 0? f: famat_inv_shallow(f);
    1290       38421 :   if (typ(f) != t_MAT) return to_famat_shallow(f,n);
    1291        9948 :   if (lgcols(f) == 1) return f;
    1292        7393 :   return mkmat2(gel(f,1), ZC_Z_mul(gel(f,2),n));
    1293             : }
    1294             : 
    1295             : GEN
    1296      150327 : famat_pows_shallow(GEN f, long n)
    1297             : {
    1298      150327 :   if (n==1) return f;
    1299       63191 :   if (n==-1) return famat_inv_shallow(f);
    1300       63177 :   if (typ(f) != t_MAT) return to_famat_shallow(f, stoi(n));
    1301       26574 :   if (lgcols(f) == 1) return f;
    1302       26574 :   return mkmat2(gel(f,1), ZC_z_mul(gel(f,2),n));
    1303             : }
    1304             : 
    1305             : GEN
    1306           0 : famat_Z_gcd(GEN M, GEN n)
    1307             : {
    1308           0 :   pari_sp av=avma;
    1309           0 :   long i, j, l=lgcols(M);
    1310           0 :   GEN F=cgetg(3,t_MAT);
    1311           0 :   gel(F,1)=cgetg(l,t_COL);
    1312           0 :   gel(F,2)=cgetg(l,t_COL);
    1313           0 :   for (i=1, j=1; i<l; i++)
    1314             :   {
    1315           0 :     GEN p = gcoeff(M,i,1);
    1316           0 :     GEN e = gminsg(Z_pval(n,p),gcoeff(M,i,2));
    1317           0 :     if (signe(e))
    1318             :     {
    1319           0 :       gcoeff(F,j,1)=p;
    1320           0 :       gcoeff(F,j,2)=e;
    1321           0 :       j++;
    1322             :     }
    1323             :   }
    1324           0 :   setlg(gel(F,1),j); setlg(gel(F,2),j);
    1325           0 :   return gerepilecopy(av,F);
    1326             : }
    1327             : 
    1328             : /* x assumed to be a t_MATs (factorization matrix), or compatible with
    1329             :  * the element_* functions. */
    1330             : static GEN
    1331       34071 : ext_sqr(GEN nf, GEN x)
    1332       34071 : { return (typ(x)==t_MAT)? famat_sqr(x): nfsqr(nf, x); }
    1333             : static GEN
    1334       89822 : ext_mul(GEN nf, GEN x, GEN y)
    1335       89822 : { return (typ(x)==t_MAT)? famat_mul(x,y): nfmul(nf, x, y); }
    1336             : static GEN
    1337       20358 : ext_inv(GEN nf, GEN x)
    1338       20358 : { return (typ(x)==t_MAT)? famat_inv(x): nfinv(nf, x); }
    1339             : static GEN
    1340           0 : ext_pow(GEN nf, GEN x, GEN n)
    1341           0 : { return (typ(x)==t_MAT)? famat_pow(x,n): nfpow(nf, x, n); }
    1342             : 
    1343             : GEN
    1344           0 : famat_to_nf(GEN nf, GEN f)
    1345             : {
    1346             :   GEN t, x, e;
    1347             :   long i;
    1348           0 :   if (lgcols(f) == 1) return gen_1;
    1349           0 :   x = gel(f,1);
    1350           0 :   e = gel(f,2);
    1351           0 :   t = nfpow(nf, gel(x,1), gel(e,1));
    1352           0 :   for (i=lg(x)-1; i>1; i--)
    1353           0 :     t = nfmul(nf, t, nfpow(nf, gel(x,i), gel(e,i)));
    1354           0 :   return t;
    1355             : }
    1356             : 
    1357             : GEN
    1358           0 : famat_idealfactor(GEN nf, GEN x)
    1359             : {
    1360             :   long i, l;
    1361           0 :   GEN g = gel(x,1), e = gel(x,2), h = cgetg_copy(g, &l);
    1362           0 :   for (i = 1; i < l; i++) gel(h,i) = idealfactor(nf, gel(g,i));
    1363           0 :   h = famat_reduce(famatV_factorback(h,e));
    1364           0 :   return sort_factor(h, (void*)&cmp_prime_ideal, &cmp_nodata);
    1365             : }
    1366             : 
    1367             : GEN
    1368      324165 : famat_reduce(GEN fa)
    1369             : {
    1370             :   GEN E, G, L, g, e;
    1371             :   long i, k, l;
    1372             : 
    1373      324165 :   if (typ(fa) != t_MAT || lgcols(fa) == 1) return fa;
    1374      311893 :   g = gel(fa,1); l = lg(g);
    1375      311893 :   e = gel(fa,2);
    1376      311893 :   L = gen_indexsort(g, (void*)&cmp_universal, &cmp_nodata);
    1377      311897 :   G = cgetg(l, t_COL);
    1378      311895 :   E = cgetg(l, t_COL);
    1379             :   /* merge */
    1380     2652243 :   for (k=i=1; i<l; i++,k++)
    1381             :   {
    1382     2340347 :     gel(G,k) = gel(g,L[i]);
    1383     2340347 :     gel(E,k) = gel(e,L[i]);
    1384     2340347 :     if (k > 1 && gidentical(gel(G,k), gel(G,k-1)))
    1385             :     {
    1386     1075934 :       gel(E,k-1) = addii(gel(E,k), gel(E,k-1));
    1387     1075931 :       k--;
    1388             :     }
    1389             :   }
    1390             :   /* kill 0 exponents */
    1391      311896 :   l = k;
    1392     1576316 :   for (k=i=1; i<l; i++)
    1393     1264419 :     if (!gequal0(gel(E,i)))
    1394             :     {
    1395     1252414 :       gel(G,k) = gel(G,i);
    1396     1252414 :       gel(E,k) = gel(E,i); k++;
    1397             :     }
    1398      311897 :   setlg(G, k);
    1399      311896 :   setlg(E, k); return mkmat2(G,E);
    1400             : }
    1401             : GEN
    1402          63 : matreduce(GEN f)
    1403          63 : { pari_sp av = avma;
    1404          63 :   switch(typ(f))
    1405             :   {
    1406          21 :     case t_VEC: case t_COL:
    1407             :     {
    1408          21 :       GEN e; f = vec_reduce(f, &e); settyp(f, t_COL);
    1409          21 :       return gerepilecopy(av, mkmat2(f, zc_to_ZC(e)));
    1410             :     }
    1411          35 :     case t_MAT:
    1412          35 :       if (lg(f) == 3) break;
    1413             :     default:
    1414          14 :       pari_err_TYPE("matreduce", f);
    1415             :   }
    1416          28 :   if (typ(gel(f,1)) == t_VECSMALL)
    1417           0 :     f = famatsmall_reduce(f);
    1418             :   else
    1419             :   {
    1420          28 :     if (!RgV_is_ZV(gel(f,2))) pari_err_TYPE("matreduce",f);
    1421          21 :     f = famat_reduce(f);
    1422             :   }
    1423          21 :   return gerepilecopy(av, f);
    1424             : }
    1425             : 
    1426             : GEN
    1427       14679 : famatsmall_reduce(GEN fa)
    1428             : {
    1429             :   GEN E, G, L, g, e;
    1430             :   long i, k, l;
    1431       14679 :   if (lgcols(fa) == 1) return fa;
    1432       14679 :   g = gel(fa,1); l = lg(g);
    1433       14679 :   e = gel(fa,2);
    1434       14679 :   L = vecsmall_indexsort(g);
    1435       14679 :   G = cgetg(l, t_VECSMALL);
    1436       14679 :   E = cgetg(l, t_VECSMALL);
    1437             :   /* merge */
    1438      131238 :   for (k=i=1; i<l; i++,k++)
    1439             :   {
    1440      116559 :     G[k] = g[L[i]];
    1441      116559 :     E[k] = e[L[i]];
    1442      116559 :     if (k > 1 && G[k] == G[k-1])
    1443             :     {
    1444        7075 :       E[k-1] += E[k];
    1445        7075 :       k--;
    1446             :     }
    1447             :   }
    1448             :   /* kill 0 exponents */
    1449       14679 :   l = k;
    1450      124163 :   for (k=i=1; i<l; i++)
    1451      109484 :     if (E[i])
    1452             :     {
    1453      105733 :       G[k] = G[i];
    1454      105733 :       E[k] = E[i]; k++;
    1455             :     }
    1456       14679 :   setlg(G, k);
    1457       14679 :   setlg(E, k); return mkmat2(G,E);
    1458             : }
    1459             : 
    1460             : GEN
    1461       55057 : famat_remove_trivial(GEN fa)
    1462             : {
    1463       55057 :   GEN P, E, p = gel(fa,1), e = gel(fa,2);
    1464       55057 :   long j, k, l = lg(p);
    1465       55057 :   P = cgetg(l, t_COL);
    1466       55057 :   E = cgetg(l, t_COL);
    1467     1801725 :   for (j = k = 1; j < l; j++)
    1468     1746668 :     if (signe(gel(e,j))) { gel(P,k) = gel(p,j); gel(E,k++) = gel(e,j); }
    1469       55057 :   setlg(P, k); setlg(E, k); return mkmat2(P,E);
    1470             : }
    1471             : 
    1472             : GEN
    1473       15605 : famatV_factorback(GEN v, GEN e)
    1474             : {
    1475       15605 :   long i, l = lg(e);
    1476             :   GEN V;
    1477       15605 :   if (l == 1) return trivial_fact();
    1478       15220 :   V = signe(gel(e,1))? famat_pow_shallow(gel(v,1), gel(e,1)): trivial_fact();
    1479       58500 :   for (i = 2; i < l; i++) V = famat_mulpow_shallow(V, gel(v,i), gel(e,i));
    1480       15220 :   return V;
    1481             : }
    1482             : 
    1483             : GEN
    1484       46529 : famatV_zv_factorback(GEN v, GEN e)
    1485             : {
    1486       46529 :   long i, l = lg(e);
    1487             :   GEN V;
    1488       46529 :   if (l == 1) return trivial_fact();
    1489       44142 :   V = uel(e,1)? famat_pows_shallow(gel(v,1), uel(e,1)): trivial_fact();
    1490      138698 :   for (i = 2; i < l; i++) V = famat_mulpows_shallow(V, gel(v,i), uel(e,i));
    1491       44142 :   return V;
    1492             : }
    1493             : 
    1494             : GEN
    1495      159821 : ZM_famat_limit(GEN fa, GEN limit)
    1496             : {
    1497             :   pari_sp av;
    1498             :   GEN E, G, g, e, r;
    1499             :   long i, k, l, n, lG;
    1500             : 
    1501      159821 :   if (lgcols(fa) == 1) return fa;
    1502      159814 :   g = gel(fa,1); l = lg(g);
    1503      159814 :   e = gel(fa,2);
    1504      320778 :   for(n=0, i=1; i<l; i++)
    1505      160963 :     if (cmpii(gel(g,i),limit)<=0) n++;
    1506      159815 :   lG = n<l-1 ? n+2 : n+1;
    1507      159815 :   G = cgetg(lG, t_COL);
    1508      159816 :   E = cgetg(lG, t_COL);
    1509      159816 :   av = avma;
    1510      320780 :   for (i=1, k=1, r = gen_1; i<l; i++)
    1511             :   {
    1512      160964 :     if (cmpii(gel(g,i),limit)<=0)
    1513             :     {
    1514      160831 :       gel(G,k) = gel(g,i);
    1515      160831 :       gel(E,k) = gel(e,i);
    1516      160831 :       k++;
    1517         133 :     } else r = mulii(r, powii(gel(g,i), gel(e,i)));
    1518             :   }
    1519      159816 :   if (k<i)
    1520             :   {
    1521         133 :     gel(G, k) = gerepileuptoint(av, r);
    1522         133 :     gel(E, k) = gen_1;
    1523             :   }
    1524      159816 :   return mkmat2(G,E);
    1525             : }
    1526             : 
    1527             : /* assume pr has degree 1 and coprime to Q_denom(x) */
    1528             : static GEN
    1529      119299 : to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1530             : {
    1531      119299 :   GEN d, r, p = modpr_get_p(modpr);
    1532      119299 :   x = nf_to_scalar_or_basis(nf,x);
    1533      119299 :   if (typ(x) != t_COL) return Rg_to_Fp(x,p);
    1534      118151 :   x = Q_remove_denom(x, &d);
    1535      118151 :   r = zk_to_Fq(x, modpr);
    1536      118148 :   if (d) r = Fp_div(r, d, p);
    1537      118148 :   return r;
    1538             : }
    1539             : 
    1540             : /* pr coprime to all denominators occurring in x */
    1541             : static GEN
    1542         602 : famat_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1543             : {
    1544         602 :   GEN p = modpr_get_p(modpr);
    1545         602 :   GEN t = NULL, g = gel(x,1), e = gel(x,2), q = subiu(p,1);
    1546         602 :   long i, l = lg(g);
    1547        3094 :   for (i = 1; i < l; i++)
    1548             :   {
    1549        2492 :     GEN n = modii(gel(e,i), q);
    1550        2492 :     if (signe(n))
    1551             :     {
    1552        2492 :       GEN h = to_Fp_coprime(nf, gel(g,i), modpr);
    1553        2492 :       h = Fp_pow(h, n, p);
    1554        2492 :       t = t? Fp_mul(t, h, p): h;
    1555             :     }
    1556             :   }
    1557         602 :   return t? modii(t, p): gen_1;
    1558             : }
    1559             : 
    1560             : /* cf famat_to_nf_modideal_coprime, modpr attached to prime of degree 1 */
    1561             : GEN
    1562      117409 : nf_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1563             : {
    1564         602 :   return typ(x)==t_MAT? famat_to_Fp_coprime(nf, x, modpr)
    1565      118011 :                       : to_Fp_coprime(nf, x, modpr);
    1566             : }
    1567             : 
    1568             : static long
    1569     2810391 : zk_pvalrem(GEN x, GEN p, GEN *py)
    1570     2810391 : { return (typ(x) == t_INT)? Z_pvalrem(x, p, py): ZV_pvalrem(x, p, py); }
    1571             : /* x a QC or Q. Return a ZC or Z, whose content is coprime to Z. Set v, dx
    1572             :  * such that x = p^v (newx / dx); dx = NULL if 1 */
    1573             : static GEN
    1574     2907427 : nf_remove_denom_p(GEN nf, GEN x, GEN p, GEN *pdx, long *pv)
    1575             : {
    1576             :   long vcx;
    1577             :   GEN dx;
    1578     2907427 :   x = nf_to_scalar_or_basis(nf, x);
    1579     2907426 :   x = Q_remove_denom(x, &dx);
    1580     2907433 :   if (dx)
    1581             :   {
    1582      351738 :     vcx = - Z_pvalrem(dx, p, &dx);
    1583      351738 :     if (!vcx) vcx = zk_pvalrem(x, p, &x);
    1584      351738 :     if (isint1(dx)) dx = NULL;
    1585             :   }
    1586             :   else
    1587             :   {
    1588     2555695 :     vcx = zk_pvalrem(x, p, &x);
    1589     2555698 :     dx = NULL;
    1590             :   }
    1591     2907436 :   *pv = vcx;
    1592     2907436 :   *pdx = dx; return x;
    1593             : }
    1594             : /* x = b^e/p^(e-1) in Z_K; x = 0 mod p/pr^e, (x,pr) = 1. Return NULL
    1595             :  * if p inert (instead of 1) */
    1596             : static GEN
    1597       89519 : p_makecoprime(GEN pr)
    1598             : {
    1599       89519 :   GEN B = pr_get_tau(pr), b;
    1600             :   long i, e;
    1601             : 
    1602       89519 :   if (typ(B) == t_INT) return NULL;
    1603       66902 :   b = gel(B,1); /* B = multiplication table by b */
    1604       66902 :   e = pr_get_e(pr);
    1605       66902 :   if (e == 1) return b;
    1606             :   /* one could also divide (exactly) by p in each iteration */
    1607       40331 :   for (i = 1; i < e; i++) b = ZM_ZC_mul(B, b);
    1608       19754 :   return ZC_Z_divexact(b, powiu(pr_get_p(pr), e-1));
    1609             : }
    1610             : 
    1611             : /* Compute A = prod g[i]^e[i] mod pr^k, assuming (A, pr) = 1.
    1612             :  * Method: modify each g[i] so that it becomes coprime to pr,
    1613             :  * g[i] *= (b/p)^v_pr(g[i]), where b/p = pr^(-1) times something integral
    1614             :  * and prime to p; globally, we multiply by (b/p)^v_pr(A) = 1.
    1615             :  * Optimizations:
    1616             :  * 1) remove all powers of p from contents, and consider extra generator p^vp;
    1617             :  * modified as p * (b/p)^e = b^e / p^(e-1)
    1618             :  * 2) remove denominators, coprime to p, by multiplying by inverse mod prk\cap Z
    1619             :  *
    1620             :  * EX = multiple of exponent of (O_K / pr^k)^* used to reduce the product in
    1621             :  * case the e[i] are large */
    1622             : GEN
    1623     1000181 : famat_makecoprime(GEN nf, GEN g, GEN e, GEN pr, GEN prk, GEN EX)
    1624             : {
    1625     1000181 :   GEN G, E, t, vp = NULL, p = pr_get_p(pr), prkZ = gcoeff(prk, 1,1);
    1626     1000181 :   long i, l = lg(g);
    1627             : 
    1628     1000181 :   G = cgetg(l+1, t_VEC);
    1629     1000192 :   E = cgetg(l+1, t_VEC); /* l+1: room for "modified p" */
    1630     3907603 :   for (i=1; i < l; i++)
    1631             :   {
    1632             :     long vcx;
    1633     2907424 :     GEN dx, x = nf_remove_denom_p(nf, gel(g,i), p, &dx, &vcx);
    1634     2907435 :     if (vcx) /* = v_p(content(g[i])) */
    1635             :     {
    1636      135485 :       GEN a = mulsi(vcx, gel(e,i));
    1637      135486 :       vp = vp? addii(vp, a): a;
    1638             :     }
    1639             :     /* x integral, content coprime to p; dx coprime to p */
    1640     2907436 :     if (typ(x) == t_INT)
    1641             :     { /* x coprime to p, hence to pr */
    1642      616861 :       x = modii(x, prkZ);
    1643      616860 :       if (dx) x = Fp_div(x, dx, prkZ);
    1644             :     }
    1645             :     else
    1646             :     {
    1647     2290575 :       (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
    1648     2290530 :       x = ZC_hnfrem(FpC_red(x,prkZ), prk);
    1649     2290557 :       if (dx) x = FpC_Fp_mul(x, Fp_inv(dx,prkZ), prkZ);
    1650             :     }
    1651     2907410 :     gel(G,i) = x;
    1652     2907410 :     gel(E,i) = gel(e,i);
    1653             :   }
    1654             : 
    1655     1000179 :   t = vp? p_makecoprime(pr): NULL;
    1656     1000183 :   if (!t)
    1657             :   { /* no need for extra generator */
    1658      933302 :     setlg(G,l);
    1659      933307 :     setlg(E,l);
    1660             :   }
    1661             :   else
    1662             :   {
    1663       66881 :     gel(G,i) = FpC_red(t, prkZ);
    1664       66881 :     gel(E,i) = vp;
    1665             :   }
    1666     1000189 :   return famat_to_nf_modideal_coprime(nf, G, E, prk, EX);
    1667             : }
    1668             : 
    1669             : /* simplified version of famat_makecoprime for X = SUnits[1] */
    1670             : GEN
    1671          91 : sunits_makecoprime(GEN X, GEN pr, GEN prk)
    1672             : {
    1673          91 :   GEN G, p = pr_get_p(pr), prkZ = gcoeff(prk,1,1);
    1674          91 :   long i, l = lg(X);
    1675             : 
    1676          91 :   G = cgetg(l, t_VEC);
    1677        5992 :   for (i = 1; i < l; i++)
    1678             :   {
    1679        5901 :     GEN x = gel(X,i);
    1680        5901 :     if (typ(x) == t_INT) /* a prime */
    1681        1281 :       x = equalii(x,p)? p_makecoprime(pr): modii(x, prkZ);
    1682             :     else
    1683             :     {
    1684        4620 :       (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
    1685        4620 :       x = ZC_hnfrem(FpC_red(x,prkZ), prk);
    1686             :     }
    1687        5901 :     gel(G,i) = x;
    1688             :   }
    1689          91 :   return G;
    1690             : }
    1691             : 
    1692             : /* prod g[i]^e[i] mod bid, assume (g[i], id) = 1 and 1 < lg(g) <= lg(e) */
    1693             : GEN
    1694       19845 : famat_to_nf_moddivisor(GEN nf, GEN g, GEN e, GEN bid)
    1695             : {
    1696       19845 :   GEN t, cyc = bid_get_cyc(bid);
    1697       19845 :   if (lg(cyc) == 1)
    1698           0 :     t = gen_1;
    1699             :   else
    1700       19845 :     t = famat_to_nf_modideal_coprime(nf, g, e, bid_get_ideal(bid),
    1701             :                                      cyc_get_expo(cyc));
    1702       19845 :   return set_sign_mod_divisor(nf, mkmat2(g,e), t, bid_get_sarch(bid));
    1703             : }
    1704             : 
    1705             : GEN
    1706      765697 : vecmul(GEN x, GEN y)
    1707             : {
    1708      765697 :   if (is_scalar_t(typ(x))) return gmul(x,y);
    1709      765694 :   pari_APPLY_same(vecmul(gel(x,i), gel(y,i)))
    1710             : }
    1711             : 
    1712             : GEN
    1713           0 : vecinv(GEN x)
    1714             : {
    1715           0 :   if (is_scalar_t(typ(x))) return ginv(x);
    1716           0 :   pari_APPLY_same(vecinv(gel(x,i)))
    1717             : }
    1718             : 
    1719             : GEN
    1720           0 : vecpow(GEN x, GEN n)
    1721             : {
    1722           0 :   if (is_scalar_t(typ(x))) return powgi(x,n);
    1723           0 :   pari_APPLY_same(vecpow(gel(x,i), n))
    1724             : }
    1725             : 
    1726             : GEN
    1727         903 : vecdiv(GEN x, GEN y)
    1728             : {
    1729         903 :   if (is_scalar_t(typ(x))) return gdiv(x,y);
    1730         903 :   pari_APPLY_same(vecdiv(gel(x,i), gel(y,i)))
    1731             : }
    1732             : 
    1733             : /* A ideal as a square t_MAT */
    1734             : static GEN
    1735      295269 : idealmulelt(GEN nf, GEN x, GEN A)
    1736             : {
    1737             :   long i, lx;
    1738             :   GEN dx, dA, D;
    1739      295269 :   if (lg(A) == 1) return cgetg(1, t_MAT);
    1740      295269 :   x = nf_to_scalar_or_basis(nf,x);
    1741      295269 :   if (typ(x) != t_COL)
    1742       92094 :     return isintzero(x)? cgetg(1,t_MAT): RgM_Rg_mul(A, Q_abs_shallow(x));
    1743      203175 :   x = Q_remove_denom(x, &dx);
    1744      203175 :   A = Q_remove_denom(A, &dA);
    1745      203175 :   x = zk_multable(nf, x);
    1746      203175 :   D = mulii(zkmultable_capZ(x), gcoeff(A,1,1));
    1747      203175 :   x = zkC_multable_mul(A, x);
    1748      203175 :   settyp(x, t_MAT); lx = lg(x);
    1749             :   /* x may contain scalars (at most 1 since the ideal is nonzero)*/
    1750      773487 :   for (i=1; i<lx; i++)
    1751      582597 :     if (typ(gel(x,i)) == t_INT)
    1752             :     {
    1753       12285 :       if (i > 1) swap(gel(x,1), gel(x,i)); /* help HNF */
    1754       12285 :       gel(x,1) = scalarcol_shallow(gel(x,1), lx-1);
    1755       12285 :       break;
    1756             :     }
    1757      203175 :   x = ZM_hnfmodid(x, D);
    1758      203175 :   dx = mul_denom(dx,dA);
    1759      203175 :   return dx? gdiv(x,dx): x;
    1760             : }
    1761             : 
    1762             : /* nf a true nf, tx <= ty */
    1763             : static GEN
    1764      936952 : idealmul_aux(GEN nf, GEN x, GEN y, long tx, long ty)
    1765             : {
    1766             :   GEN z, cx, cy;
    1767      936952 :   switch(tx)
    1768             :   {
    1769      348952 :     case id_PRINCIPAL:
    1770      348952 :       switch(ty)
    1771             :       {
    1772       53291 :         case id_PRINCIPAL:
    1773       53291 :           return idealhnf_principal(nf, nfmul(nf,x,y));
    1774         392 :         case id_PRIME:
    1775             :         {
    1776         392 :           GEN p = pr_get_p(y), pi = pr_get_gen(y), cx;
    1777         392 :           if (pr_is_inert(y)) return RgM_Rg_mul(idealhnf_principal(nf,x),p);
    1778             : 
    1779         217 :           x = nf_to_scalar_or_basis(nf, x);
    1780         217 :           switch(typ(x))
    1781             :           {
    1782         203 :             case t_INT:
    1783         203 :               if (!signe(x)) return cgetg(1,t_MAT);
    1784         203 :               return ZM_Z_mul(pr_hnf(nf,y), absi_shallow(x));
    1785           7 :             case t_FRAC:
    1786           7 :               return RgM_Rg_mul(pr_hnf(nf,y), Q_abs_shallow(x));
    1787             :           }
    1788             :           /* t_COL */
    1789           7 :           x = Q_primitive_part(x, &cx);
    1790           7 :           x = zk_multable(nf, x);
    1791           7 :           z = shallowconcat(ZM_Z_mul(x,p), ZM_ZC_mul(x,pi));
    1792           7 :           z = ZM_hnfmodid(z, mulii(p, zkmultable_capZ(x)));
    1793           7 :           return cx? ZM_Q_mul(z, cx): z;
    1794             :         }
    1795      295269 :         default: /* id_MAT */
    1796      295269 :           return idealmulelt(nf, x,y);
    1797             :       }
    1798      267296 :     case id_PRIME:
    1799      267296 :       if (ty==id_PRIME)
    1800      260901 :       { y = pr_hnf(nf,y); cy = NULL; }
    1801             :       else
    1802        6395 :         y = Q_primitive_part(y, &cy);
    1803      267296 :       y = idealHNF_mul_two(nf,y,x);
    1804      267296 :       return cy? ZM_Q_mul(y,cy): y;
    1805             : 
    1806      320704 :     default: /* id_MAT */
    1807             :     {
    1808      320704 :       long N = nf_get_degree(nf);
    1809      320704 :       if (lg(x)-1 != N || lg(y)-1 != N) pari_err_DIM("idealmul");
    1810      320690 :       x = Q_primitive_part(x, &cx);
    1811      320690 :       y = Q_primitive_part(y, &cy); cx = mul_content(cx,cy);
    1812      320690 :       y = idealHNF_mul(nf,x,y);
    1813      320690 :       return cx? ZM_Q_mul(y,cx): y;
    1814             :     }
    1815             :   }
    1816             : }
    1817             : 
    1818             : /* output the ideal product x.y */
    1819             : GEN
    1820      936951 : idealmul(GEN nf, GEN x, GEN y)
    1821             : {
    1822             :   pari_sp av;
    1823             :   GEN res, ax, ay, z;
    1824      936951 :   long tx = idealtyp(&x,&ax);
    1825      936951 :   long ty = idealtyp(&y,&ay), f;
    1826      936951 :   if (tx>ty) { swap(ax,ay); swap(x,y); lswap(tx,ty); }
    1827      936951 :   f = (ax||ay); res = f? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1828      936951 :   av = avma;
    1829      936951 :   z = gerepileupto(av, idealmul_aux(checknf(nf), x,y, tx,ty));
    1830      936938 :   if (!f) return z;
    1831       27660 :   if (ax && ay)
    1832       26148 :     ax = ext_mul(nf, ax, ay);
    1833             :   else
    1834        1512 :     ax = gcopy(ax? ax: ay);
    1835       27660 :   gel(res,1) = z; gel(res,2) = ax; return res;
    1836             : }
    1837             : 
    1838             : /* Return x, integral in 2-elt form, such that pr^2 = c * x. cf idealpowprime
    1839             :  * nf = true nf */
    1840             : static GEN
    1841      209069 : idealsqrprime(GEN nf, GEN pr, GEN *pc)
    1842             : {
    1843      209069 :   GEN p = pr_get_p(pr), q, gen;
    1844      209069 :   long e = pr_get_e(pr), f = pr_get_f(pr);
    1845             : 
    1846      209070 :   q = (e == 1)? sqri(p): p;
    1847      209063 :   if (e <= 2 && e * f == nf_get_degree(nf))
    1848             :   { /* pr^e = (p) */
    1849       44100 :     *pc = q;
    1850       44100 :     return mkvec2(gen_1,gen_0);
    1851             :   }
    1852      164969 :   gen = nfsqr(nf, pr_get_gen(pr));
    1853      164970 :   gen = FpC_red(gen, q);
    1854      164951 :   *pc = NULL;
    1855      164951 :   return mkvec2(q, gen);
    1856             : }
    1857             : /* cf idealpow_aux */
    1858             : static GEN
    1859       38670 : idealsqr_aux(GEN nf, GEN x, long tx)
    1860             : {
    1861       38670 :   GEN T = nf_get_pol(nf), m, cx, a, alpha;
    1862       38670 :   long N = degpol(T);
    1863       38670 :   switch(tx)
    1864             :   {
    1865          70 :     case id_PRINCIPAL:
    1866          70 :       return idealhnf_principal(nf, nfsqr(nf,x));
    1867       10761 :     case id_PRIME:
    1868       10761 :       if (pr_is_inert(x)) return scalarmat(sqri(gel(x,1)), N);
    1869       10593 :       x = idealsqrprime(nf, x, &cx);
    1870       10593 :       x = idealhnf_two(nf,x);
    1871       10593 :       return cx? ZM_Z_mul(x, cx): x;
    1872       27839 :     default:
    1873       27839 :       x = Q_primitive_part(x, &cx);
    1874       27839 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    1875       27839 :       alpha = nfsqr(nf,alpha);
    1876       27839 :       m = zk_scalar_or_multable(nf, alpha);
    1877       27839 :       if (typ(m) == t_INT) {
    1878        1568 :         x = gcdii(sqri(a), m);
    1879        1568 :         if (cx) x = gmul(x, gsqr(cx));
    1880        1568 :         x = scalarmat(x, N);
    1881             :       }
    1882             :       else
    1883             :       { /* could use gcdii(sqri(a), zkmultable_capZ(m)), but costly */
    1884       26271 :         x = ZM_hnfmodid(m, sqri(a));
    1885       26271 :         if (cx) cx = gsqr(cx);
    1886       26271 :         if (cx) x = ZM_Q_mul(x, cx);
    1887             :       }
    1888       27839 :       return x;
    1889             :   }
    1890             : }
    1891             : GEN
    1892       38670 : idealsqr(GEN nf, GEN x)
    1893             : {
    1894             :   pari_sp av;
    1895             :   GEN res, ax, z;
    1896       38670 :   long tx = idealtyp(&x,&ax);
    1897       38670 :   res = ax? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1898       38670 :   av = avma;
    1899       38670 :   z = gerepileupto(av, idealsqr_aux(checknf(nf), x, tx));
    1900       38670 :   if (!ax) return z;
    1901       34071 :   gel(res,1) = z;
    1902       34071 :   gel(res,2) = ext_sqr(nf, ax); return res;
    1903             : }
    1904             : 
    1905             : /* norm of an ideal */
    1906             : GEN
    1907       12825 : idealnorm(GEN nf, GEN x)
    1908             : {
    1909             :   pari_sp av;
    1910             :   long tx;
    1911             : 
    1912       12825 :   switch(idealtyp(&x, NULL))
    1913             :   {
    1914         630 :     case id_PRIME: return pr_norm(x);
    1915        9430 :     case id_MAT: return RgM_det_triangular(x);
    1916             :   }
    1917             :   /* id_PRINCIPAL */
    1918        2765 :   nf = checknf(nf); av = avma;
    1919        2765 :   x = nfnorm(nf, x);
    1920        2765 :   tx = typ(x);
    1921        2765 :   if (tx == t_INT) return gerepileuptoint(av, absi(x));
    1922         406 :   if (tx != t_FRAC) pari_err_TYPE("idealnorm",x);
    1923         406 :   return gerepileupto(av, Q_abs(x));
    1924             : }
    1925             : 
    1926             : /* x \cap Z */
    1927             : GEN
    1928        2982 : idealdown(GEN nf, GEN x)
    1929             : {
    1930        2982 :   pari_sp av = avma;
    1931             :   GEN y, c;
    1932        2982 :   switch(idealtyp(&x, NULL))
    1933             :   {
    1934           7 :     case id_PRIME: return icopy(pr_get_p(x));
    1935        2107 :     case id_MAT: return gcopy(gcoeff(x,1,1));
    1936             :   }
    1937             :   /* id_PRINCIPAL */
    1938         868 :   nf = checknf(nf); av = avma;
    1939         868 :   x = nf_to_scalar_or_basis(nf, x);
    1940         868 :   if (is_rational_t(typ(x))) return Q_abs(x);
    1941          14 :   x = Q_primitive_part(x, &c);
    1942          14 :   y = zkmultable_capZ(zk_multable(nf, x));
    1943          14 :   return gerepilecopy(av, mul_content(c, y));
    1944             : }
    1945             : 
    1946             : /* true nf */
    1947             : static GEN
    1948          35 : idealismaximal_int(GEN nf, GEN p)
    1949             : {
    1950             :   GEN L;
    1951          35 :   if (!BPSW_psp(p)) return NULL;
    1952          70 :   if (!dvdii(nf_get_index(nf), p) &&
    1953          49 :       !FpX_is_irred(FpX_red(nf_get_pol(nf),p), p)) return NULL;
    1954          21 :   L = idealprimedec(nf, p);
    1955          21 :   return lg(L) == 2? gel(L,1): NULL;
    1956             : }
    1957             : /* true nf */
    1958             : static GEN
    1959          21 : idealismaximal_mat(GEN nf, GEN x)
    1960             : {
    1961             :   GEN p, c, L;
    1962             :   long i, l, f;
    1963          21 :   x = Q_primitive_part(x, &c);
    1964          21 :   p = gcoeff(x,1,1);
    1965          21 :   if (c)
    1966             :   {
    1967           7 :     if (typ(c) == t_FRAC || !equali1(p)) return NULL;
    1968           7 :     return idealismaximal_int(nf, c);
    1969             :   }
    1970          14 :   if (!BPSW_psp(p)) return NULL;
    1971          14 :   l = lg(x); f = 1;
    1972          35 :   for (i = 2; i < l; i++)
    1973             :   {
    1974          21 :     c = gcoeff(x,i,i);
    1975          21 :     if (equalii(c, p)) f++; else if (!equali1(c)) return NULL;
    1976             :   }
    1977          14 :   L = idealprimedec_limit_f(nf, p, f);
    1978          28 :   for (i = lg(L)-1; i; i--)
    1979             :   {
    1980          28 :     GEN pr = gel(L,i);
    1981          28 :     if (pr_get_f(pr) != f) break;
    1982          28 :     if (idealval(nf, x, pr) == 1) return pr;
    1983             :   }
    1984           0 :   return NULL;
    1985             : }
    1986             : /* true nf */
    1987             : static GEN
    1988          56 : idealismaximal_i(GEN nf, GEN x)
    1989             : {
    1990             :   GEN L, p, pr, c;
    1991             :   long i, l;
    1992          56 :   switch(idealtyp(&x, NULL))
    1993             :   {
    1994           7 :     case id_PRIME: return x;
    1995          21 :     case id_MAT: return idealismaximal_mat(nf, x);
    1996             :   }
    1997             :   /* id_PRINCIPAL */
    1998          28 :   x = nf_to_scalar_or_basis(nf, x);
    1999          28 :   switch(typ(x))
    2000             :   {
    2001          28 :     case t_INT: return idealismaximal_int(nf, absi_shallow(x));
    2002           0 :     case t_FRAC: return NULL;
    2003             :   }
    2004           0 :   x = Q_primitive_part(x, &c);
    2005           0 :   if (c) return NULL;
    2006           0 :   p = zkmultable_capZ(zk_multable(nf, x));
    2007           0 :   L = idealprimedec(nf, p); l = lg(L); pr = NULL;
    2008           0 :   for (i = 1; i < l; i++)
    2009             :   {
    2010           0 :     long v = ZC_nfval(x, gel(L,i));
    2011           0 :     if (v > 1 || (v && pr)) return NULL;
    2012           0 :     pr = gel(L,i);
    2013             :   }
    2014           0 :   return pr;
    2015             : }
    2016             : GEN
    2017          56 : idealismaximal(GEN nf, GEN x)
    2018             : {
    2019          56 :   pari_sp av = avma;
    2020          56 :   x = idealismaximal_i(checknf(nf), x);
    2021          56 :   if (!x) { set_avma(av); return gen_0; }
    2022          42 :   return gerepilecopy(av, x);
    2023             : }
    2024             : 
    2025             : /* I^(-1) = { x \in K, Tr(x D^(-1) I) \in Z }, D different of K/Q
    2026             :  *
    2027             :  * nf[5][6] = pp( D^(-1) ) = pp( HNF( T^(-1) ) ), T = (Tr(wi wj))
    2028             :  * nf[5][7] = same in 2-elt form.
    2029             :  * Assume I integral. Return the integral ideal (I\cap Z) I^(-1) */
    2030             : GEN
    2031      214792 : idealHNF_inv_Z(GEN nf, GEN I)
    2032             : {
    2033      214792 :   GEN J, dual, IZ = gcoeff(I,1,1); /* I \cap Z */
    2034      214792 :   if (isint1(IZ)) return matid(lg(I)-1);
    2035      195165 :   J = idealHNF_mul(nf,I, gmael(nf,5,7));
    2036             :  /* I in HNF, hence easily inverted; multiply by IZ to get integer coeffs
    2037             :   * missing content cancels while solving the linear equation */
    2038      195168 :   dual = shallowtrans( hnf_divscale(J, gmael(nf,5,6), IZ) );
    2039      195167 :   return ZM_hnfmodid(dual, IZ);
    2040             : }
    2041             : /* I HNF with rational coefficients (denominator d). */
    2042             : GEN
    2043       73613 : idealHNF_inv(GEN nf, GEN I)
    2044             : {
    2045       73613 :   GEN J, IQ = gcoeff(I,1,1); /* I \cap Q; d IQ = dI \cap Z */
    2046       73613 :   J = idealHNF_inv_Z(nf, Q_remove_denom(I, NULL)); /* = (dI)^(-1) * (d IQ) */
    2047       73613 :   return equali1(IQ)? J: RgM_Rg_div(J, IQ);
    2048             : }
    2049             : 
    2050             : /* return p * P^(-1)  [integral] */
    2051             : GEN
    2052       36342 : pr_inv_p(GEN pr)
    2053             : {
    2054       36342 :   if (pr_is_inert(pr)) return matid(pr_get_f(pr));
    2055       35566 :   return ZM_hnfmodid(pr_get_tau(pr), pr_get_p(pr));
    2056             : }
    2057             : GEN
    2058       17823 : pr_inv(GEN pr)
    2059             : {
    2060       17823 :   GEN p = pr_get_p(pr);
    2061       17823 :   if (pr_is_inert(pr)) return scalarmat(ginv(p), pr_get_f(pr));
    2062       17431 :   return RgM_Rg_div(ZM_hnfmodid(pr_get_tau(pr),p), p);
    2063             : }
    2064             : 
    2065             : GEN
    2066      141626 : idealinv(GEN nf, GEN x)
    2067             : {
    2068             :   GEN res, ax;
    2069             :   pari_sp av;
    2070      141626 :   long tx = idealtyp(&x,&ax), N;
    2071             : 
    2072      141626 :   res = ax? cgetg(3,t_VEC): NULL;
    2073      141625 :   nf = checknf(nf); av = avma;
    2074      141625 :   N = nf_get_degree(nf);
    2075      141626 :   switch (tx)
    2076             :   {
    2077       68279 :     case id_MAT:
    2078       68279 :       if (lg(x)-1 != N) pari_err_DIM("idealinv");
    2079       68279 :       x = idealHNF_inv(nf,x); break;
    2080       56560 :     case id_PRINCIPAL:
    2081       56560 :       x = nf_to_scalar_or_basis(nf, x);
    2082       56560 :       if (typ(x) != t_COL)
    2083       56511 :         x = idealhnf_principal(nf,ginv(x));
    2084             :       else
    2085             :       { /* nfinv + idealhnf where we already know (x) \cap Z */
    2086             :         GEN c, d;
    2087          49 :         x = Q_remove_denom(x, &c);
    2088          49 :         x = zk_inv(nf, x);
    2089          49 :         x = Q_remove_denom(x, &d); /* true inverse is c/d * x */
    2090          49 :         if (!d) /* x and x^(-1) integral => x a unit */
    2091          14 :           x = c? scalarmat(c, N): matid(N);
    2092             :         else
    2093             :         {
    2094          35 :           c = c? gdiv(c,d): ginv(d);
    2095          35 :           x = zk_multable(nf, x);
    2096          35 :           x = ZM_Q_mul(ZM_hnfmodid(x,d), c);
    2097             :         }
    2098             :       }
    2099       56560 :       break;
    2100       16787 :     case id_PRIME:
    2101       16787 :       x = pr_inv(x); break;
    2102             :   }
    2103      141626 :   x = gerepileupto(av,x); if (!ax) return x;
    2104       20358 :   gel(res,1) = x;
    2105       20358 :   gel(res,2) = ext_inv(nf, ax); return res;
    2106             : }
    2107             : 
    2108             : /* write x = A/B, A,B coprime integral ideals */
    2109             : GEN
    2110      285831 : idealnumden(GEN nf, GEN x)
    2111             : {
    2112      285831 :   pari_sp av = avma;
    2113             :   GEN x0, c, d, A, B, J;
    2114      285831 :   long tx = idealtyp(&x, NULL);
    2115      285830 :   nf = checknf(nf);
    2116      285835 :   switch (tx)
    2117             :   {
    2118           7 :     case id_PRIME:
    2119           7 :       retmkvec2(idealhnf(nf, x), gen_1);
    2120       51079 :     case id_PRINCIPAL:
    2121             :     {
    2122             :       GEN xZ, mx;
    2123       51079 :       x = nf_to_scalar_or_basis(nf, x);
    2124       51079 :       switch(typ(x))
    2125             :       {
    2126        2562 :         case t_INT: return gerepilecopy(av, mkvec2(absi_shallow(x),gen_1));
    2127          14 :         case t_FRAC:return gerepilecopy(av, mkvec2(absi_shallow(gel(x,1)), gel(x,2)));
    2128             :       }
    2129             :       /* t_COL */
    2130       48503 :       x = Q_remove_denom(x, &d);
    2131       48503 :       if (!d) return gerepilecopy(av, mkvec2(idealhnf_shallow(nf, x), gen_1));
    2132          91 :       mx = zk_multable(nf, x);
    2133          91 :       xZ = zkmultable_capZ(mx);
    2134          91 :       x = ZM_hnfmodid(mx, xZ); /* principal ideal (x) */
    2135          91 :       x0 = mkvec2(xZ, mx); /* same, for fast multiplication */
    2136          91 :       break;
    2137             :     }
    2138      234749 :     default: /* id_MAT */
    2139             :     {
    2140      234749 :       long n = lg(x)-1;
    2141      234749 :       if (n == 0) return mkvec2(gen_0, gen_1);
    2142      234749 :       if (n != nf_get_degree(nf)) pari_err_DIM("idealnumden");
    2143      234749 :       x0 = x = Q_remove_denom(x, &d);
    2144      234747 :       if (!d) return gerepilecopy(av, mkvec2(x, gen_1));
    2145          14 :       break;
    2146             :     }
    2147             :   }
    2148         105 :   J = hnfmodid(x, d); /* = d/B */
    2149         105 :   c = gcoeff(J,1,1); /* (d/B) \cap Z, divides d */
    2150         105 :   B = idealHNF_inv_Z(nf, J); /* (d/B \cap Z) B/d */
    2151         105 :   if (!equalii(c,d)) B = ZM_Z_mul(B, diviiexact(d,c)); /* = B ! */
    2152         105 :   A = idealHNF_mul(nf, B, x0); /* d * (original x) * B = d A */
    2153         105 :   A = ZM_Z_divexact(A, d); /* = A ! */
    2154         105 :   return gerepilecopy(av, mkvec2(A, B));
    2155             : }
    2156             : 
    2157             : /* Return x, integral in 2-elt form, such that pr^n = c * x. Assume n != 0.
    2158             :  * nf = true nf */
    2159             : static GEN
    2160      930191 : idealpowprime(GEN nf, GEN pr, GEN n, GEN *pc)
    2161             : {
    2162      930191 :   GEN p = pr_get_p(pr), q, gen;
    2163             : 
    2164      930175 :   *pc = NULL;
    2165      930175 :   if (is_pm1(n)) /* n = 1 special cased for efficiency */
    2166             :   {
    2167      535664 :     q = p;
    2168      535664 :     if (typ(pr_get_tau(pr)) == t_INT) /* inert */
    2169             :     {
    2170           0 :       *pc = (signe(n) >= 0)? p: ginv(p);
    2171           0 :       return mkvec2(gen_1,gen_0);
    2172             :     }
    2173      535660 :     if (signe(n) >= 0) gen = pr_get_gen(pr);
    2174             :     else
    2175             :     {
    2176      155176 :       gen = pr_get_tau(pr); /* possibly t_MAT */
    2177      155185 :       *pc = ginv(p);
    2178             :     }
    2179             :   }
    2180      394569 :   else if (equalis(n,2)) return idealsqrprime(nf, pr, pc);
    2181             :   else
    2182             :   {
    2183      196093 :     long e = pr_get_e(pr), f = pr_get_f(pr);
    2184      196102 :     GEN r, m = truedvmdis(n, e, &r);
    2185      196100 :     if (e * f == nf_get_degree(nf))
    2186             :     { /* pr^e = (p) */
    2187       81717 :       if (signe(m)) *pc = powii(p,m);
    2188       81716 :       if (!signe(r)) return mkvec2(gen_1,gen_0);
    2189       40141 :       q = p;
    2190       40141 :       gen = nfpow(nf, pr_get_gen(pr), r);
    2191             :     }
    2192             :     else
    2193             :     {
    2194      114391 :       m = absi_shallow(m);
    2195      114391 :       if (signe(r)) m = addiu(m,1);
    2196      114391 :       q = powii(p,m); /* m = ceil(|n|/e) */
    2197      114390 :       if (signe(n) >= 0) gen = nfpow(nf, pr_get_gen(pr), n);
    2198             :       else
    2199             :       {
    2200       24029 :         gen = pr_get_tau(pr);
    2201       24029 :         if (typ(gen) == t_MAT) gen = gel(gen,1);
    2202       24029 :         n = negi(n);
    2203       24029 :         gen = ZC_Z_divexact(nfpow(nf, gen, n), powii(p, subii(n,m)));
    2204       24029 :         *pc = ginv(q);
    2205             :       }
    2206             :     }
    2207      154534 :     gen = FpC_red(gen, q);
    2208             :   }
    2209      690180 :   return mkvec2(q, gen);
    2210             : }
    2211             : 
    2212             : /* True nf. x * pr^n. Assume x in HNF or scalar (possibly nonintegral) */
    2213             : GEN
    2214      575257 : idealmulpowprime(GEN nf, GEN x, GEN pr, GEN n)
    2215             : {
    2216             :   GEN c, cx, y;
    2217      575257 :   long N = nf_get_degree(nf);
    2218             : 
    2219      575302 :   if (!signe(n)) return typ(x) == t_MAT? x: scalarmat_shallow(x, N);
    2220             : 
    2221             :   /* inert, special cased for efficiency */
    2222      575295 :   if (pr_is_inert(pr))
    2223             :   {
    2224       64666 :     GEN q = powii(pr_get_p(pr), n);
    2225       62657 :     return typ(x) == t_MAT? RgM_Rg_mul(x,q)
    2226      127305 :                           : scalarmat_shallow(gmul(Q_abs(x),q), N);
    2227             :   }
    2228             : 
    2229      510616 :   y = idealpowprime(nf, pr, n, &c);
    2230      510595 :   if (typ(x) == t_MAT)
    2231      508026 :   { x = Q_primitive_part(x, &cx); if (is_pm1(gcoeff(x,1,1))) x = NULL; }
    2232             :   else
    2233        2569 :   { cx = x; x = NULL; }
    2234      510518 :   cx = mul_content(c,cx);
    2235      510520 :   if (x)
    2236      349870 :     x = idealHNF_mul_two(nf,x,y);
    2237             :   else
    2238      160650 :     x = idealhnf_two(nf,y);
    2239      510663 :   if (cx) x = ZM_Q_mul(x,cx);
    2240      510358 :   return x;
    2241             : }
    2242             : GEN
    2243        5999 : idealdivpowprime(GEN nf, GEN x, GEN pr, GEN n)
    2244             : {
    2245        5999 :   return idealmulpowprime(nf,x,pr, negi(n));
    2246             : }
    2247             : 
    2248             : /* nf = true nf */
    2249             : static GEN
    2250      788777 : idealpow_aux(GEN nf, GEN x, long tx, GEN n)
    2251             : {
    2252      788777 :   GEN T = nf_get_pol(nf), m, cx, n1, a, alpha;
    2253      788776 :   long N = degpol(T), s = signe(n);
    2254      788778 :   if (!s) return matid(N);
    2255      777179 :   switch(tx)
    2256             :   {
    2257       75747 :     case id_PRINCIPAL:
    2258       75747 :       return idealhnf_principal(nf, nfpow(nf,x,n));
    2259      505587 :     case id_PRIME:
    2260      505587 :       if (pr_is_inert(x)) return scalarmat(powii(gel(x,1), n), N);
    2261      419574 :       x = idealpowprime(nf, x, n, &cx);
    2262      419568 :       x = idealhnf_two(nf,x);
    2263      419581 :       return cx? ZM_Q_mul(x, cx): x;
    2264      195845 :     default:
    2265      195845 :       if (is_pm1(n)) return (s < 0)? idealinv(nf, x): gcopy(x);
    2266       71954 :       n1 = (s < 0)? negi(n): n;
    2267             : 
    2268       71954 :       x = Q_primitive_part(x, &cx);
    2269       71955 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    2270       71955 :       alpha = nfpow(nf,alpha,n1);
    2271       71955 :       m = zk_scalar_or_multable(nf, alpha);
    2272       71955 :       if (typ(m) == t_INT) {
    2273         651 :         x = gcdii(powii(a,n1), m);
    2274         651 :         if (s<0) x = ginv(x);
    2275         651 :         if (cx) x = gmul(x, powgi(cx,n));
    2276         651 :         x = scalarmat(x, N);
    2277             :       }
    2278             :       else
    2279             :       { /* could use gcdii(powii(a,n1), zkmultable_capZ(m)), but costly */
    2280       71304 :         x = ZM_hnfmodid(m, powii(a,n1));
    2281       71304 :         if (cx) cx = powgi(cx,n);
    2282       71304 :         if (s<0) {
    2283           7 :           GEN xZ = gcoeff(x,1,1);
    2284           7 :           cx = cx ? gdiv(cx, xZ): ginv(xZ);
    2285           7 :           x = idealHNF_inv_Z(nf,x);
    2286             :         }
    2287       71304 :         if (cx) x = ZM_Q_mul(x, cx);
    2288             :       }
    2289       71955 :       return x;
    2290             :   }
    2291             : }
    2292             : 
    2293             : /* raise the ideal x to the power n (in Z) */
    2294             : GEN
    2295      788778 : idealpow(GEN nf, GEN x, GEN n)
    2296             : {
    2297             :   pari_sp av;
    2298             :   long tx;
    2299             :   GEN res, ax;
    2300             : 
    2301      788778 :   if (typ(n) != t_INT) pari_err_TYPE("idealpow",n);
    2302      788778 :   tx = idealtyp(&x,&ax);
    2303      788780 :   res = ax? cgetg(3,t_VEC): NULL;
    2304      788778 :   av = avma;
    2305      788778 :   x = gerepileupto(av, idealpow_aux(checknf(nf), x, tx, n));
    2306      788772 :   if (!ax) return x;
    2307           0 :   gel(res,1) = x;
    2308           0 :   gel(res,2) = ext_pow(nf, ax, n);
    2309           0 :   return res;
    2310             : }
    2311             : 
    2312             : /* Return ideal^e in number field nf. e is a C integer. */
    2313             : GEN
    2314      247908 : idealpows(GEN nf, GEN ideal, long e)
    2315             : {
    2316      247908 :   long court[] = {evaltyp(t_INT) | _evallg(3),0,0};
    2317      247908 :   affsi(e,court); return idealpow(nf,ideal,court);
    2318             : }
    2319             : 
    2320             : static GEN
    2321       28220 : _idealmulred(GEN nf, GEN x, GEN y)
    2322       28220 : { return idealred(nf,idealmul(nf,x,y)); }
    2323             : static GEN
    2324       35821 : _idealsqrred(GEN nf, GEN x)
    2325       35821 : { return idealred(nf,idealsqr(nf,x)); }
    2326             : static GEN
    2327       11350 : _mul(void *data, GEN x, GEN y) { return _idealmulred((GEN)data,x,y); }
    2328             : static GEN
    2329       35821 : _sqr(void *data, GEN x) { return _idealsqrred((GEN)data, x); }
    2330             : 
    2331             : /* compute x^n (x ideal, n integer), reducing along the way */
    2332             : GEN
    2333       77215 : idealpowred(GEN nf, GEN x, GEN n)
    2334             : {
    2335       77215 :   pari_sp av = avma, av2;
    2336             :   long s;
    2337             :   GEN y;
    2338             : 
    2339       77215 :   if (typ(n) != t_INT) pari_err_TYPE("idealpowred",n);
    2340       77219 :   s = signe(n); if (s == 0) return idealpow(nf,x,n);
    2341       77219 :   y = gen_pow_i(x, n, (void*)nf, &_sqr, &_mul);
    2342       77219 :   av2 = avma;
    2343       77219 :   if (s < 0) y = idealinv(nf,y);
    2344       77219 :   if (s < 0 || is_pm1(n)) y = idealred(nf,y);
    2345       77220 :   return avma == av2? gerepilecopy(av,y): gerepileupto(av,y);
    2346             : }
    2347             : 
    2348             : GEN
    2349       16870 : idealmulred(GEN nf, GEN x, GEN y)
    2350             : {
    2351       16870 :   pari_sp av = avma;
    2352       16870 :   return gerepileupto(av, _idealmulred(nf,x,y));
    2353             : }
    2354             : 
    2355             : long
    2356          91 : isideal(GEN nf,GEN x)
    2357             : {
    2358          91 :   long N, i, j, lx, tx = typ(x);
    2359             :   pari_sp av;
    2360             :   GEN T, xZ;
    2361             : 
    2362          91 :   nf = checknf(nf); T = nf_get_pol(nf); lx = lg(x);
    2363          91 :   if (tx==t_VEC && lx==3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
    2364          91 :   switch(tx)
    2365             :   {
    2366          14 :     case t_INT: case t_FRAC: return 1;
    2367           7 :     case t_POL: return varn(x) == varn(T);
    2368           7 :     case t_POLMOD: return RgX_equal_var(T, gel(x,1));
    2369          14 :     case t_VEC: return get_prid(x)? 1 : 0;
    2370          42 :     case t_MAT: break;
    2371           7 :     default: return 0;
    2372             :   }
    2373          42 :   N = degpol(T);
    2374          42 :   if (lx-1 != N) return (lx == 1);
    2375          28 :   if (nbrows(x) != N) return 0;
    2376             : 
    2377          28 :   av = avma; x = Q_primpart(x);
    2378          28 :   if (!ZM_ishnf(x)) return 0;
    2379          14 :   xZ = gcoeff(x,1,1);
    2380          21 :   for (j=2; j<=N; j++)
    2381          14 :     if (!dvdii(xZ, gcoeff(x,j,j))) return gc_long(av,0);
    2382          14 :   for (i=2; i<=N; i++)
    2383          14 :     for (j=2; j<=N; j++)
    2384           7 :        if (! hnf_invimage(x, zk_ei_mul(nf,gel(x,i),j))) return gc_long(av,0);
    2385           7 :   return gc_long(av,1);
    2386             : }
    2387             : 
    2388             : GEN
    2389       38717 : idealdiv(GEN nf, GEN x, GEN y)
    2390             : {
    2391       38717 :   pari_sp av = avma, tetpil;
    2392       38717 :   GEN z = idealinv(nf,y);
    2393       38717 :   tetpil = avma; return gerepile(av,tetpil, idealmul(nf,x,z));
    2394             : }
    2395             : 
    2396             : /* This routine computes the quotient x/y of two ideals in the number field nf.
    2397             :  * It assumes that the quotient is an integral ideal.  The idea is to find an
    2398             :  * ideal z dividing y such that gcd(Nx/Nz, Nz) = 1.  Then
    2399             :  *
    2400             :  *   x + (Nx/Nz)    x
    2401             :  *   ----------- = ---
    2402             :  *   y + (Ny/Nz)    y
    2403             :  *
    2404             :  * Proof: we can assume x and y are integral. Let p be any prime ideal
    2405             :  *
    2406             :  * If p | Nz, then it divides neither Nx/Nz nor Ny/Nz (since Nx/Nz is the
    2407             :  * product of the integers N(x/y) and N(y/z)).  Both the numerator and the
    2408             :  * denominator on the left will be coprime to p.  So will x/y, since x/y is
    2409             :  * assumed integral and its norm N(x/y) is coprime to p.
    2410             :  *
    2411             :  * If instead p does not divide Nz, then v_p (Nx/Nz) = v_p (Nx) >= v_p(x).
    2412             :  * Hence v_p (x + Nx/Nz) = v_p(x).  Likewise for the denominators.  QED.
    2413             :  *
    2414             :  *                Peter Montgomery.  July, 1994. */
    2415             : static void
    2416           7 : err_divexact(GEN x, GEN y)
    2417           7 : { pari_err_DOMAIN("idealdivexact","denominator(x/y)", "!=",
    2418           0 :                   gen_1,mkvec2(x,y)); }
    2419             : GEN
    2420        4050 : idealdivexact(GEN nf, GEN x0, GEN y0)
    2421             : {
    2422        4050 :   pari_sp av = avma;
    2423             :   GEN x, y, xZ, yZ, Nx, Ny, Nz, cy, q, r;
    2424             : 
    2425        4050 :   nf = checknf(nf);
    2426        4050 :   x = idealhnf_shallow(nf, x0);
    2427        4050 :   y = idealhnf_shallow(nf, y0);
    2428        4050 :   if (lg(y) == 1) pari_err_INV("idealdivexact", y0);
    2429        4043 :   if (lg(x) == 1) { set_avma(av); return cgetg(1, t_MAT); } /* numerator is zero */
    2430        4043 :   y = Q_primitive_part(y, &cy);
    2431        4043 :   if (cy) x = RgM_Rg_div(x,cy);
    2432        4043 :   xZ = gcoeff(x,1,1); if (typ(xZ) != t_INT) err_divexact(x,y);
    2433        4036 :   yZ = gcoeff(y,1,1); if (isint1(yZ)) return gerepilecopy(av, x);
    2434        2937 :   Nx = idealnorm(nf,x);
    2435        2937 :   Ny = idealnorm(nf,y);
    2436        2937 :   if (typ(Nx) != t_INT) err_divexact(x,y);
    2437        2937 :   q = dvmdii(Nx,Ny, &r);
    2438        2937 :   if (signe(r)) err_divexact(x,y);
    2439        2937 :   if (is_pm1(q)) { set_avma(av); return matid(nf_get_degree(nf)); }
    2440             :   /* Find a norm Nz | Ny such that gcd(Nx/Nz, Nz) = 1 */
    2441         599 :   for (Nz = Ny;;) /* q = Nx/Nz */
    2442         623 :   {
    2443        1222 :     GEN p1 = gcdii(Nz, q);
    2444        1222 :     if (is_pm1(p1)) break;
    2445         623 :     Nz = diviiexact(Nz,p1);
    2446         623 :     q = mulii(q,p1);
    2447             :   }
    2448         599 :   xZ = gcoeff(x,1,1); q = gcdii(q, xZ);
    2449         599 :   if (!equalii(xZ,q))
    2450             :   { /* Replace x/y  by  x+(Nx/Nz) / y+(Ny/Nz) */
    2451         354 :     x = ZM_hnfmodid(x, q);
    2452             :     /* y reduced to unit ideal ? */
    2453         354 :     if (Nz == Ny) return gerepileupto(av, x);
    2454             : 
    2455         140 :     yZ = gcoeff(y,1,1); q = gcdii(diviiexact(Ny,Nz), yZ);
    2456         140 :     y = ZM_hnfmodid(y, q);
    2457             :   }
    2458         385 :   yZ = gcoeff(y,1,1);
    2459         385 :   y = idealHNF_mul(nf,x, idealHNF_inv_Z(nf,y));
    2460         385 :   return gerepileupto(av, ZM_Z_divexact(y, yZ));
    2461             : }
    2462             : 
    2463             : GEN
    2464          21 : idealintersect(GEN nf, GEN x, GEN y)
    2465             : {
    2466          21 :   pari_sp av = avma;
    2467             :   long lz, lx, i;
    2468             :   GEN z, dx, dy, xZ, yZ;;
    2469             : 
    2470          21 :   nf = checknf(nf);
    2471          21 :   x = idealhnf_shallow(nf,x);
    2472          21 :   y = idealhnf_shallow(nf,y);
    2473          21 :   if (lg(x) == 1 || lg(y) == 1) { set_avma(av); return cgetg(1,t_MAT); }
    2474          14 :   x = Q_remove_denom(x, &dx);
    2475          14 :   y = Q_remove_denom(y, &dy);
    2476          14 :   if (dx) y = ZM_Z_mul(y, dx);
    2477          14 :   if (dy) x = ZM_Z_mul(x, dy);
    2478          14 :   xZ = gcoeff(x,1,1);
    2479          14 :   yZ = gcoeff(y,1,1);
    2480          14 :   dx = mul_denom(dx,dy);
    2481          14 :   z = ZM_lll(shallowconcat(x,y), 0.99, LLL_KER); lz = lg(z);
    2482          14 :   lx = lg(x);
    2483          63 :   for (i=1; i<lz; i++) setlg(z[i], lx);
    2484          14 :   z = ZM_hnfmodid(ZM_mul(x,z), lcmii(xZ, yZ));
    2485          14 :   if (dx) z = RgM_Rg_div(z,dx);
    2486          14 :   return gerepileupto(av,z);
    2487             : }
    2488             : 
    2489             : /*******************************************************************/
    2490             : /*                                                                 */
    2491             : /*                      T2-IDEAL REDUCTION                         */
    2492             : /*                                                                 */
    2493             : /*******************************************************************/
    2494             : 
    2495             : static GEN
    2496          21 : chk_vdir(GEN nf, GEN vdir)
    2497             : {
    2498          21 :   long i, l = lg(vdir);
    2499             :   GEN v;
    2500          21 :   if (l != lg(nf_get_roots(nf))) pari_err_DIM("idealred");
    2501          14 :   switch(typ(vdir))
    2502             :   {
    2503           0 :     case t_VECSMALL: return vdir;
    2504          14 :     case t_VEC: break;
    2505           0 :     default: pari_err_TYPE("idealred",vdir);
    2506             :   }
    2507          14 :   v = cgetg(l, t_VECSMALL);
    2508          56 :   for (i = 1; i < l; i++) v[i] = itos(gceil(gel(vdir,i)));
    2509          14 :   return v;
    2510             : }
    2511             : 
    2512             : static void
    2513       12727 : twistG(GEN G, long r1, long i, long v)
    2514             : {
    2515       12727 :   long j, lG = lg(G);
    2516       12727 :   if (i <= r1) {
    2517       37321 :     for (j=1; j<lG; j++) gcoeff(G,i,j) = gmul2n(gcoeff(G,i,j), v);
    2518             :   } else {
    2519         651 :     long k = (i<<1) - r1;
    2520        4319 :     for (j=1; j<lG; j++)
    2521             :     {
    2522        3668 :       gcoeff(G,k-1,j) = gmul2n(gcoeff(G,k-1,j), v);
    2523        3668 :       gcoeff(G,k  ,j) = gmul2n(gcoeff(G,k  ,j), v);
    2524             :     }
    2525             :   }
    2526       12727 : }
    2527             : 
    2528             : GEN
    2529      135628 : nf_get_Gtwist(GEN nf, GEN vdir)
    2530             : {
    2531             :   long i, l, v, r1;
    2532             :   GEN G;
    2533             : 
    2534      135628 :   if (!vdir) return nf_get_roundG(nf);
    2535          21 :   if (typ(vdir) == t_MAT)
    2536             :   {
    2537           0 :     long N = nf_get_degree(nf);
    2538           0 :     if (lg(vdir) != N+1 || lgcols(vdir) != N+1) pari_err_DIM("idealred");
    2539           0 :     return vdir;
    2540             :   }
    2541          21 :   vdir = chk_vdir(nf, vdir);
    2542          14 :   G = RgM_shallowcopy(nf_get_G(nf));
    2543          14 :   r1 = nf_get_r1(nf);
    2544          14 :   l = lg(vdir);
    2545          56 :   for (i=1; i<l; i++)
    2546             :   {
    2547          42 :     v = vdir[i]; if (!v) continue;
    2548          42 :     twistG(G, r1, i, v);
    2549             :   }
    2550          14 :   return RM_round_maxrank(G);
    2551             : }
    2552             : GEN
    2553       12685 : nf_get_Gtwist1(GEN nf, long i)
    2554             : {
    2555       12685 :   GEN G = RgM_shallowcopy( nf_get_G(nf) );
    2556       12685 :   long r1 = nf_get_r1(nf);
    2557       12685 :   twistG(G, r1, i, 10);
    2558       12685 :   return RM_round_maxrank(G);
    2559             : }
    2560             : 
    2561             : GEN
    2562       95524 : RM_round_maxrank(GEN G0)
    2563             : {
    2564       95524 :   long e, r = lg(G0)-1;
    2565       95524 :   pari_sp av = avma;
    2566       95524 :   for (e = 4; ; e <<= 1, set_avma(av))
    2567           0 :   {
    2568       95524 :     GEN G = gmul2n(G0, e), H = ground(G);
    2569       95523 :     if (ZM_rank(H) == r) return H; /* maximal rank ? */
    2570             :   }
    2571             : }
    2572             : 
    2573             : GEN
    2574      135621 : idealred0(GEN nf, GEN I, GEN vdir)
    2575             : {
    2576      135621 :   pari_sp av = avma;
    2577      135621 :   GEN G, aI, IZ, J, y, yZ, my, yi, c1 = NULL;
    2578             :   long N;
    2579             : 
    2580      135621 :   nf = checknf(nf);
    2581      135621 :   N = nf_get_degree(nf);
    2582             :   /* put first for sanity checks, unused when I obviously principal */
    2583      135621 :   G = nf_get_Gtwist(nf, vdir);
    2584      135614 :   switch (idealtyp(&I,&aI))
    2585             :   {
    2586       34491 :     case id_PRIME:
    2587       34491 :       if (pr_is_inert(I)) {
    2588         585 :         if (!aI) { set_avma(av); return matid(N); }
    2589         585 :         c1 = gel(I,1); I = matid(N);
    2590         585 :         goto END;
    2591             :       }
    2592       33906 :       IZ = pr_get_p(I);
    2593       33906 :       J = pr_inv_p(I);
    2594       33907 :       I = idealhnf_two(nf,I);
    2595       33905 :       break;
    2596      101095 :     case id_MAT:
    2597      101095 :       I = Q_primitive_part(I, &c1);
    2598      101094 :       IZ = gcoeff(I,1,1);
    2599      101094 :       if (is_pm1(IZ))
    2600             :       {
    2601        8666 :         if (!aI) { set_avma(av); return matid(N); }
    2602        8631 :         goto END;
    2603             :       }
    2604       92428 :       J = idealHNF_inv_Z(nf, I);
    2605       92429 :       break;
    2606          21 :     default: /* id_PRINCIPAL, silly case */
    2607          21 :       if (gequal0(I)) I = cgetg(1,t_MAT); else { c1 = I; I = matid(N); }
    2608          21 :       if (!aI) return I;
    2609          14 :       goto END;
    2610             :   }
    2611             :   /* now I integral, HNF; and J = (I\cap Z) I^(-1), integral */
    2612      126334 :   y = idealpseudomin(J, G); /* small elt in (I\cap Z)I^(-1), integral */
    2613      126336 :   if (equalii(ZV_content(y), IZ))
    2614             :   { /* already reduced */
    2615       68688 :     if (!aI) return gerepilecopy(av, I);
    2616       65195 :     goto END;
    2617             :   }
    2618             : 
    2619       57648 :   my = zk_multable(nf, y);
    2620       57648 :   I = ZM_Z_divexact(ZM_mul(my, I), IZ); /* y I / (I\cap Z), integral */
    2621       57644 :   c1 = mul_content(c1, IZ);
    2622       57644 :   if (equali1(c1)) c1 = NULL; /* can be simplified with IZ */
    2623       57644 :   yi = ZM_gauss(my, col_ei(N,1)); /* y^-1 */
    2624       57648 :   yZ = Q_denom(yi); /* (y) \cap Z */
    2625       57647 :   I = hnfmodid(I, yZ);
    2626       57648 :   if (!aI) return gerepileupto(av, I);
    2627       55989 :   if (typ(aI) == t_MAT) /* yi is not integral and usually larger than y */
    2628       41436 :     aI = famat_div(aI, y);
    2629       14553 :   else if (c1)
    2630       14553 :     c1 = RgC_Rg_mul(yi, c1);
    2631           0 : END:
    2632      130414 :   if (c1) aI = ext_mul(nf, aI,c1);
    2633      130413 :   return gerepilecopy(av, mkvec2(I, aI));
    2634             : }
    2635             : 
    2636             : GEN
    2637           7 : idealmin(GEN nf, GEN x, GEN vdir)
    2638             : {
    2639           7 :   pari_sp av = avma;
    2640             :   GEN y, dx;
    2641           7 :   nf = checknf(nf);
    2642           7 :   switch( idealtyp(&x, NULL) )
    2643             :   {
    2644           0 :     case id_PRINCIPAL: return gcopy(x);
    2645           0 :     case id_PRIME: x = pr_hnf(nf,x); break;
    2646           7 :     case id_MAT: if (lg(x) == 1) return gen_0;
    2647             :   }
    2648           7 :   x = Q_remove_denom(x, &dx);
    2649           7 :   y = idealpseudomin(x, nf_get_Gtwist(nf,vdir));
    2650           7 :   if (dx) y = RgC_Rg_div(y, dx);
    2651           7 :   return gerepileupto(av, y);
    2652             : }
    2653             : 
    2654             : /*******************************************************************/
    2655             : /*                                                                 */
    2656             : /*                   APPROXIMATION THEOREM                         */
    2657             : /*                                                                 */
    2658             : /*******************************************************************/
    2659             : /* a = ppi(a,b) ppo(a,b), where ppi regroups primes common to a and b
    2660             :  * and ppo(a,b) = Z_ppo(a,b) */
    2661             : /* return gcd(a,b),ppi(a,b),ppo(a,b) */
    2662             : GEN
    2663      456155 : Z_ppio(GEN a, GEN b)
    2664             : {
    2665      456155 :   GEN x, y, d = gcdii(a,b);
    2666      456155 :   if (is_pm1(d)) return mkvec3(gen_1, gen_1, a);
    2667      346871 :   x = d; y = diviiexact(a,d);
    2668             :   for(;;)
    2669       63077 :   {
    2670      409948 :     GEN g = gcdii(x,y);
    2671      409948 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2672       63077 :     x = mulii(x,g); y = diviiexact(y,g);
    2673             :   }
    2674             : }
    2675             : /* a = ppg(a,b)pple(a,b), where ppg regroups primes such that v(a) > v(b)
    2676             :  * and pple all others */
    2677             : /* return gcd(a,b),ppg(a,b),pple(a,b) */
    2678             : GEN
    2679           0 : Z_ppgle(GEN a, GEN b)
    2680             : {
    2681           0 :   GEN x, y, g, d = gcdii(a,b);
    2682           0 :   if (equalii(a, d)) return mkvec3(a, gen_1, a);
    2683           0 :   x = diviiexact(a,d); y = d;
    2684             :   for(;;)
    2685             :   {
    2686           0 :     g = gcdii(x,y);
    2687           0 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2688           0 :     x = mulii(x,g); y = diviiexact(y,g);
    2689             :   }
    2690             : }
    2691             : static void
    2692           0 : Z_dcba_rec(GEN L, GEN a, GEN b)
    2693             : {
    2694             :   GEN x, r, v, g, h, c, c0;
    2695             :   long n;
    2696           0 :   if (is_pm1(b)) {
    2697           0 :     if (!is_pm1(a)) vectrunc_append(L, a);
    2698           0 :     return;
    2699             :   }
    2700           0 :   v = Z_ppio(a,b);
    2701           0 :   a = gel(v,2);
    2702           0 :   r = gel(v,3);
    2703           0 :   if (!is_pm1(r)) vectrunc_append(L, r);
    2704           0 :   v = Z_ppgle(a,b);
    2705           0 :   g = gel(v,1);
    2706           0 :   h = gel(v,2);
    2707           0 :   x = c0 = gel(v,3);
    2708           0 :   for (n = 1; !is_pm1(h); n++)
    2709             :   {
    2710             :     GEN d, y;
    2711             :     long i;
    2712           0 :     v = Z_ppgle(h,sqri(g));
    2713           0 :     g = gel(v,1);
    2714           0 :     h = gel(v,2);
    2715           0 :     c = gel(v,3); if (is_pm1(c)) continue;
    2716           0 :     d = gcdii(c,b);
    2717           0 :     x = mulii(x,d);
    2718           0 :     y = d; for (i=1; i < n; i++) y = sqri(y);
    2719           0 :     Z_dcba_rec(L, diviiexact(c,y), d);
    2720             :   }
    2721           0 :   Z_dcba_rec(L,diviiexact(b,x), c0);
    2722             : }
    2723             : static GEN
    2724     3088351 : Z_cba_rec(GEN L, GEN a, GEN b)
    2725             : {
    2726             :   GEN g;
    2727     3088351 :   if (lg(L) > 10)
    2728             :   { /* a few naive steps before switching to dcba */
    2729           0 :     Z_dcba_rec(L, a, b);
    2730           0 :     return gel(L, lg(L)-1);
    2731             :   }
    2732     3088351 :   if (is_pm1(a)) return b;
    2733     1835050 :   g = gcdii(a,b);
    2734     1835050 :   if (is_pm1(g)) { vectrunc_append(L, a); return b; }
    2735     1370894 :   a = diviiexact(a,g);
    2736     1370894 :   b = diviiexact(b,g);
    2737     1370894 :   return Z_cba_rec(L, Z_cba_rec(L, a, g), b);
    2738             : }
    2739             : GEN
    2740      346563 : Z_cba(GEN a, GEN b)
    2741             : {
    2742      346563 :   GEN L = vectrunc_init(expi(a) + expi(b) + 2);
    2743      346563 :   GEN t = Z_cba_rec(L, a, b);
    2744      346563 :   if (!is_pm1(t)) vectrunc_append(L, t);
    2745      346563 :   return L;
    2746             : }
    2747             : /* P = coprime base, extend it by b; TODO: quadratic for now */
    2748             : GEN
    2749           0 : ZV_cba_extend(GEN P, GEN b)
    2750             : {
    2751           0 :   long i, l = lg(P);
    2752           0 :   GEN w = cgetg(l+1, t_VEC);
    2753           0 :   for (i = 1; i < l; i++)
    2754             :   {
    2755           0 :     GEN v = Z_cba(gel(P,i), b);
    2756           0 :     long nv = lg(v)-1;
    2757           0 :     gel(w,i) = vecslice(v, 1, nv-1); /* those divide P[i] but not b */
    2758           0 :     b = gel(v,nv);
    2759             :   }
    2760           0 :   gel(w,l) = b; return shallowconcat1(w);
    2761             : }
    2762             : GEN
    2763           0 : ZV_cba(GEN v)
    2764             : {
    2765           0 :   long i, l = lg(v);
    2766             :   GEN P;
    2767           0 :   if (l <= 2) return v;
    2768           0 :   P = Z_cba(gel(v,1), gel(v,2));
    2769           0 :   for (i = 3; i < l; i++) P = ZV_cba_extend(P, gel(v,i));
    2770           0 :   return P;
    2771             : }
    2772             : 
    2773             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2774             : GEN
    2775     2232389 : Z_ppo(GEN x, GEN f)
    2776             : {
    2777             :   for (;;)
    2778             :   {
    2779     2232389 :     f = gcdii(x, f); if (is_pm1(f)) break;
    2780     1406715 :     x = diviiexact(x, f);
    2781             :   }
    2782      825671 :   return x;
    2783             : }
    2784             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2785             : ulong
    2786    66767935 : u_ppo(ulong x, ulong f)
    2787             : {
    2788             :   for (;;)
    2789             :   {
    2790    66767935 :     f = ugcd(x, f); if (f == 1) break;
    2791    14899249 :     x /= f;
    2792             :   }
    2793    51868664 :   return x;
    2794             : }
    2795             : 
    2796             : /* result known to be representable as an ulong */
    2797             : static ulong
    2798     2114552 : lcmuu(ulong a, ulong b) { ulong d = ugcd(a,b); return (a/d) * b; }
    2799             : 
    2800             : /* assume 0 < x < N; return u in (Z/NZ)^* such that u x = gcd(x,N) (mod N);
    2801             :  * set *pd = gcd(x,N) */
    2802             : ulong
    2803     6495395 : Fl_invgen(ulong x, ulong N, ulong *pd)
    2804             : {
    2805             :   ulong d, d0, e, v, v1;
    2806             :   long s;
    2807     6495395 :   *pd = d = xgcduu(N, x, 0, &v, &v1, &s);
    2808     6495743 :   if (s > 0) v = N - v;
    2809     6495743 :   if (d == 1) return v;
    2810             :   /* vx = gcd(x,N) (mod N), v coprime to N/d but need not be coprime to N */
    2811     3348238 :   e = N / d;
    2812     3348238 :   d0 = u_ppo(d, e); /* d = d0 d1, d0 coprime to N/d, rad(d1) | N/d */
    2813     3348374 :   if (d0 == 1) return v;
    2814     2114504 :   e = lcmuu(e, d / d0);
    2815     2114542 :   return u_chinese_coprime(v, 1, e, d0, e*d0);
    2816             : }
    2817             : 
    2818             : /* x t_INT, f ideal. Write x = x1 x2, sqf(x1) | f, (x2,f) = 1. Return x2 */
    2819             : static GEN
    2820         196 : nf_coprime_part(GEN nf, GEN x, GEN listpr)
    2821             : {
    2822         196 :   long v, j, lp = lg(listpr), N = nf_get_degree(nf);
    2823             :   GEN x1, x2, ex;
    2824             : 
    2825             : #if 0 /*1) via many gcds. Expensive ! */
    2826             :   GEN f = idealprodprime(nf, listpr);
    2827             :   f = ZM_hnfmodid(f, x); /* first gcd is less expensive since x in Z */
    2828             :   x = scalarmat(x, N);
    2829             :   for (;;)
    2830             :   {
    2831             :     if (gequal1(gcoeff(f,1,1))) break;
    2832             :     x = idealdivexact(nf, x, f);
    2833             :     f = ZM_hnfmodid(shallowconcat(f,x), gcoeff(x,1,1)); /* gcd(f,x) */
    2834             :   }
    2835             :   x2 = x;
    2836             : #else /*2) from prime decomposition */
    2837         196 :   x1 = NULL;
    2838         532 :   for (j=1; j<lp; j++)
    2839             :   {
    2840         336 :     GEN pr = gel(listpr,j);
    2841         336 :     v = Z_pval(x, pr_get_p(pr)); if (!v) continue;
    2842             : 
    2843         196 :     ex = muluu(v, pr_get_e(pr)); /* = v_pr(x) > 0 */
    2844         196 :     x1 = x1? idealmulpowprime(nf, x1, pr, ex)
    2845         196 :            : idealpow(nf, pr, ex);
    2846             :   }
    2847         196 :   x = scalarmat(x, N);
    2848         196 :   x2 = x1? idealdivexact(nf, x, x1): x;
    2849             : #endif
    2850         196 :   return x2;
    2851             : }
    2852             : 
    2853             : /* L0 in K^*, assume (L0,f) = 1. Return L integral, L0 = L mod f  */
    2854             : GEN
    2855       10654 : make_integral(GEN nf, GEN L0, GEN f, GEN listpr)
    2856             : {
    2857             :   GEN fZ, t, L, D2, d1, d2, d;
    2858             : 
    2859       10654 :   L = Q_remove_denom(L0, &d);
    2860       10654 :   if (!d) return L0;
    2861             : 
    2862             :   /* L0 = L / d, L integral */
    2863         672 :   fZ = gcoeff(f,1,1);
    2864         672 :   if (typ(L) == t_INT) return Fp_mul(L, Fp_inv(d, fZ), fZ);
    2865             :   /* Kill denom part coprime to fZ */
    2866         196 :   d2 = Z_ppo(d, fZ);
    2867         196 :   t = Fp_inv(d2, fZ); if (!is_pm1(t)) L = ZC_Z_mul(L,t);
    2868         196 :   if (equalii(d, d2)) return L;
    2869             : 
    2870         196 :   d1 = diviiexact(d, d2);
    2871             :   /* L0 = (L / d1) mod f. d1 not coprime to f
    2872             :    * write (d1) = D1 D2, D2 minimal, (D2,f) = 1. */
    2873         196 :   D2 = nf_coprime_part(nf, d1, listpr);
    2874         196 :   t = idealaddtoone_i(nf, D2, f); /* in D2, 1 mod f */
    2875         196 :   L = nfmuli(nf,t,L);
    2876             : 
    2877             :   /* if (L0, f) = 1, then L in D1 ==> in D1 D2 = (d1) */
    2878         196 :   return Q_div_to_int(L, d1); /* exact division */
    2879             : }
    2880             : 
    2881             : /* assume L is a list of prime ideals. Return the product */
    2882             : GEN
    2883         336 : idealprodprime(GEN nf, GEN L)
    2884             : {
    2885         336 :   long l = lg(L), i;
    2886             :   GEN z;
    2887         336 :   if (l == 1) return matid(nf_get_degree(nf));
    2888         336 :   z = pr_hnf(nf, gel(L,1));
    2889         371 :   for (i=2; i<l; i++) z = idealHNF_mul_two(nf,z, gel(L,i));
    2890         336 :   return z;
    2891             : }
    2892             : 
    2893             : /* optimize for the frequent case I = nfhnf()[2]: lots of them are 1 */
    2894             : GEN
    2895         427 : idealprod(GEN nf, GEN I)
    2896             : {
    2897         427 :   long i, l = lg(I);
    2898             :   GEN z;
    2899        1043 :   for (i = 1; i < l; i++)
    2900        1036 :     if (!equali1(gel(I,i))) break;
    2901         427 :   if (i == l) return gen_1;
    2902         420 :   z = gel(I,i);
    2903         693 :   for (i++; i<l; i++) z = idealmul(nf, z, gel(I,i));
    2904         420 :   return z;
    2905             : }
    2906             : 
    2907             : /* v_pr(idealprod(nf,I)) */
    2908             : long
    2909        2422 : idealprodval(GEN nf, GEN I, GEN pr)
    2910             : {
    2911        2422 :   long i, l = lg(I), v = 0;
    2912       14420 :   for (i = 1; i < l; i++)
    2913       11998 :     if (!equali1(gel(I,i))) v += idealval(nf, gel(I,i), pr);
    2914        2422 :   return v;
    2915             : }
    2916             : 
    2917             : /* assume L is a list of prime ideals. Return prod L[i]^e[i] */
    2918             : GEN
    2919       55705 : factorbackprime(GEN nf, GEN L, GEN e)
    2920             : {
    2921       55705 :   long l = lg(L), i;
    2922             :   GEN z;
    2923             : 
    2924       55705 :   if (l == 1) return matid(nf_get_degree(nf));
    2925       42433 :   z = idealpow(nf, gel(L,1), gel(e,1));
    2926       65863 :   for (i=2; i<l; i++)
    2927       23429 :     if (signe(gel(e,i))) z = idealmulpowprime(nf,z, gel(L,i),gel(e,i));
    2928       42434 :   return z;
    2929             : }
    2930             : 
    2931             : /* F in Z, divisible exactly by pr.p. Return F-uniformizer for pr, i.e.
    2932             :  * a t in Z_K such that v_pr(t) = 1 and (t, F/pr) = 1 */
    2933             : GEN
    2934       77234 : pr_uniformizer(GEN pr, GEN F)
    2935             : {
    2936       77234 :   GEN p = pr_get_p(pr), t = pr_get_gen(pr);
    2937       77234 :   if (!equalii(F, p))
    2938             :   {
    2939       43468 :     long e = pr_get_e(pr);
    2940       43468 :     GEN u, v, q = (e == 1)? sqri(p): p;
    2941       43468 :     u = mulii(q, Fp_inv(q, diviiexact(F,p))); /* 1 mod F/p, 0 mod q */
    2942       43467 :     v = subui(1UL, u); /* 0 mod F/p, 1 mod q */
    2943       43467 :     if (pr_is_inert(pr))
    2944          28 :       t = addii(mulii(p, v), u);
    2945             :     else
    2946             :     {
    2947       43439 :       t = ZC_Z_mul(t, v);
    2948       43440 :       gel(t,1) = addii(gel(t,1), u); /* return u + vt */
    2949             :     }
    2950             :   }
    2951       77234 :   return t;
    2952             : }
    2953             : /* L = list of prime ideals, return lcm_i (L[i] \cap \ZM) */
    2954             : GEN
    2955       90923 : prV_lcm_capZ(GEN L)
    2956             : {
    2957       90923 :   long i, r = lg(L);
    2958             :   GEN F;
    2959       90923 :   if (r == 1) return gen_1;
    2960       78442 :   F = pr_get_p(gel(L,1));
    2961      141822 :   for (i = 2; i < r; i++)
    2962             :   {
    2963       63381 :     GEN pr = gel(L,i), p = pr_get_p(pr);
    2964       63381 :     if (!dvdii(F, p)) F = mulii(F,p);
    2965             :   }
    2966       78441 :   return F;
    2967             : }
    2968             : /* v vector of prid. Return underlying list of rational primes */
    2969             : GEN
    2970       40005 : prV_primes(GEN v)
    2971             : {
    2972       40005 :   long i, l = lg(v);
    2973       40005 :   GEN w = cgetg(l,t_VEC);
    2974      110208 :   for (i=1; i<l; i++) gel(w,i) = pr_get_p(gel(v,i));
    2975       40005 :   return ZV_sort_uniq(w);
    2976             : }
    2977             : 
    2978             : /* Given a prime ideal factorization with possibly zero or negative
    2979             :  * exponents, gives b such that v_p(b) = v_p(x) for all prime ideals pr | x
    2980             :  * and v_pr(b) >= 0 for all other pr.
    2981             :  * For optimal performance, all [anti-]uniformizers should be precomputed,
    2982             :  * but no support for this yet. If nored, do not reduce result. */
    2983             : static GEN
    2984       63308 : idealapprfact_i(GEN nf, GEN x, int nored)
    2985             : {
    2986       63308 :   GEN d = NULL, z, L, e, e2, F;
    2987             :   long i, r;
    2988       63308 :   int hasden = 0;
    2989             : 
    2990       63308 :   nf = checknf(nf);
    2991       63308 :   L = gel(x,1);
    2992       63308 :   e = gel(x,2);
    2993       63308 :   F = prV_lcm_capZ(L);
    2994       63306 :   z = NULL; r = lg(e);
    2995      165032 :   for (i = 1; i < r; i++)
    2996             :   {
    2997      101725 :     long s = signe(gel(e,i));
    2998             :     GEN pi, q;
    2999      101725 :     if (!s) continue;
    3000       72879 :     if (s < 0) hasden = 1;
    3001       72879 :     pi = pr_uniformizer(gel(L,i), F);
    3002       72880 :     q = nfpow(nf, pi, gel(e,i));
    3003       72880 :     z = z? nfmul(nf, z, q): q;
    3004             :   }
    3005       63307 :   if (!z) return gen_1;
    3006       36126 :   if (hasden) /* denominator */
    3007             :   {
    3008       10024 :     z = Q_remove_denom(z, &d);
    3009       10024 :     d = diviiexact(d, Z_ppo(d, F));
    3010             :   }
    3011       36126 :   if (nored || typ(z) != t_COL) return d? gdiv(z, d): z;
    3012       10023 :   e2 = cgetg(r, t_VEC);
    3013       28439 :   for (i = 1; i < r; i++) gel(e2,i) = addiu(gel(e,i), 1);
    3014       10022 :   x = factorbackprime(nf, L, e2);
    3015       10024 :   if (d) x = RgM_Rg_mul(x, d);
    3016       10023 :   z = ZC_reducemodlll(z, x);
    3017       10024 :   return d? RgC_Rg_div(z,d): z;
    3018             : }
    3019             : 
    3020             : GEN
    3021           0 : idealapprfact(GEN nf, GEN x) {
    3022           0 :   pari_sp av = avma;
    3023           0 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    3024             : }
    3025             : GEN
    3026          14 : idealappr(GEN nf, GEN x) {
    3027          14 :   pari_sp av = avma;
    3028          14 :   if (!is_nf_extfactor(x)) x = idealfactor(nf, x);
    3029          14 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    3030             : }
    3031             : 
    3032             : /* OBSOLETE */
    3033             : GEN
    3034          14 : idealappr0(GEN nf, GEN x, long fl) { (void)fl; return idealappr(nf, x); }
    3035             : 
    3036             : static GEN
    3037          21 : mat_ideal_two_elt2(GEN nf, GEN x, GEN a)
    3038             : {
    3039          21 :   GEN F = idealfactor(nf,a), P = gel(F,1), E = gel(F,2);
    3040          21 :   long i, r = lg(E);
    3041          84 :   for (i=1; i<r; i++) gel(E,i) = stoi( idealval(nf,x,gel(P,i)) );
    3042          21 :   return idealapprfact_i(nf,F,1);
    3043             : }
    3044             : 
    3045             : static void
    3046          14 : not_in_ideal(GEN a) {
    3047          14 :   pari_err_DOMAIN("idealtwoelt2","element mod ideal", "!=", gen_0, a);
    3048           0 : }
    3049             : /* x integral in HNF, a an 'nf' */
    3050             : static int
    3051          28 : in_ideal(GEN x, GEN a)
    3052             : {
    3053          28 :   switch(typ(a))
    3054             :   {
    3055          14 :     case t_INT: return dvdii(a, gcoeff(x,1,1));
    3056           7 :     case t_COL: return RgV_is_ZV(a) && !!hnf_invimage(x, a);
    3057           7 :     default: return 0;
    3058             :   }
    3059             : }
    3060             : 
    3061             : /* Given an integral ideal x and a in x, gives a b such that
    3062             :  * x = aZ_K + bZ_K using the approximation theorem */
    3063             : GEN
    3064          42 : idealtwoelt2(GEN nf, GEN x, GEN a)
    3065             : {
    3066          42 :   pari_sp av = avma;
    3067             :   GEN cx, b;
    3068             : 
    3069          42 :   nf = checknf(nf);
    3070          42 :   a = nf_to_scalar_or_basis(nf, a);
    3071          42 :   x = idealhnf_shallow(nf,x);
    3072          42 :   if (lg(x) == 1)
    3073             :   {
    3074          14 :     if (!isintzero(a)) not_in_ideal(a);
    3075           7 :     set_avma(av); return gen_0;
    3076             :   }
    3077          28 :   x = Q_primitive_part(x, &cx);
    3078          28 :   if (cx) a = gdiv(a, cx);
    3079          28 :   if (!in_ideal(x, a)) not_in_ideal(a);
    3080          21 :   b = mat_ideal_two_elt2(nf, x, a);
    3081          21 :   if (typ(b) == t_COL)
    3082             :   {
    3083          14 :     GEN mod = idealhnf_principal(nf,a);
    3084          14 :     b = ZC_hnfrem(b,mod);
    3085          14 :     if (ZV_isscalar(b)) b = gel(b,1);
    3086             :   }
    3087             :   else
    3088             :   {
    3089           7 :     GEN aZ = typ(a) == t_COL? Q_denom(zk_inv(nf,a)): a; /* (a) \cap Z */
    3090           7 :     b = centermodii(b, aZ, shifti(aZ,-1));
    3091             :   }
    3092          21 :   b = cx? gmul(b,cx): gcopy(b);
    3093          21 :   return gerepileupto(av, b);
    3094             : }
    3095             : 
    3096             : /* Given 2 integral ideals x and y in nf, returns a beta in nf such that
    3097             :  * beta * x is an integral ideal coprime to y */
    3098             : GEN
    3099       37198 : idealcoprimefact(GEN nf, GEN x, GEN fy)
    3100             : {
    3101       37198 :   GEN L = gel(fy,1), e;
    3102       37198 :   long i, r = lg(L);
    3103             : 
    3104       37198 :   e = cgetg(r, t_COL);
    3105       76068 :   for (i=1; i<r; i++) gel(e,i) = stoi( -idealval(nf,x,gel(L,i)) );
    3106       37198 :   return idealapprfact_i(nf, mkmat2(L,e), 0);
    3107             : }
    3108             : GEN
    3109          84 : idealcoprime(GEN nf, GEN x, GEN y)
    3110             : {
    3111          84 :   pari_sp av = avma;
    3112          84 :   return gerepileupto(av, idealcoprimefact(nf, x, idealfactor(nf,y)));
    3113             : }
    3114             : 
    3115             : GEN
    3116           7 : nfmulmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    3117             : {
    3118           7 :   pari_sp av = avma;
    3119           7 :   GEN z, p, pr = modpr, T;
    3120             : 
    3121           7 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    3122           0 :   x = nf_to_Fq(nf,x,modpr);
    3123           0 :   y = nf_to_Fq(nf,y,modpr);
    3124           0 :   z = Fq_mul(x,y,T,p);
    3125           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    3126             : }
    3127             : 
    3128             : GEN
    3129           0 : nfdivmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    3130             : {
    3131           0 :   pari_sp av = avma;
    3132           0 :   nf = checknf(nf);
    3133           0 :   return gerepileupto(av, nfreducemodpr(nf, nfdiv(nf,x,y), modpr));
    3134             : }
    3135             : 
    3136             : GEN
    3137           0 : nfpowmodpr(GEN nf, GEN x, GEN k, GEN modpr)
    3138             : {
    3139           0 :   pari_sp av=avma;
    3140           0 :   GEN z, T, p, pr = modpr;
    3141             : 
    3142           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    3143           0 :   z = nf_to_Fq(nf,x,modpr);
    3144           0 :   z = Fq_pow(z,k,T,p);
    3145           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    3146             : }
    3147             : 
    3148             : GEN
    3149           0 : nfkermodpr(GEN nf, GEN x, GEN modpr)
    3150             : {
    3151           0 :   pari_sp av = avma;
    3152           0 :   GEN T, p, pr = modpr;
    3153             : 
    3154           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    3155           0 :   if (typ(x)!=t_MAT) pari_err_TYPE("nfkermodpr",x);
    3156           0 :   x = nfM_to_FqM(x, nf, modpr);
    3157           0 :   return gerepilecopy(av, FqM_to_nfM(FqM_ker(x,T,p), modpr));
    3158             : }
    3159             : 
    3160             : GEN
    3161           0 : nfsolvemodpr(GEN nf, GEN a, GEN b, GEN pr)
    3162             : {
    3163           0 :   const char *f = "nfsolvemodpr";
    3164           0 :   pari_sp av = avma;
    3165             :   GEN T, p, modpr;
    3166             : 
    3167           0 :   nf = checknf(nf);
    3168           0 :   modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    3169           0 :   if (typ(a)!=t_MAT) pari_err_TYPE(f,a);
    3170           0 :   a = nfM_to_FqM(a, nf, modpr);
    3171           0 :   switch(typ(b))
    3172             :   {
    3173           0 :     case t_MAT:
    3174           0 :       b = nfM_to_FqM(b, nf, modpr);
    3175           0 :       b = FqM_gauss(a,b,T,p);
    3176           0 :       if (!b) pari_err_INV(f,a);
    3177           0 :       a = FqM_to_nfM(b, modpr);
    3178           0 :       break;
    3179           0 :     case t_COL:
    3180           0 :       b = nfV_to_FqV(b, nf, modpr);
    3181           0 :       b = FqM_FqC_gauss(a,b,T,p);
    3182           0 :       if (!b) pari_err_INV(f,a);
    3183           0 :       a = FqV_to_nfV(b, modpr);
    3184           0 :       break;
    3185           0 :     default: pari_err_TYPE(f,b);
    3186             :   }
    3187           0 :   return gerepilecopy(av, a);
    3188             : }

Generated by: LCOV version 1.13