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 26712-590d837a1c) Lines: 1583 1773 89.3 %
Date: 2021-06-22 07:13:04 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     8705355 : idealtyp(GEN *ideal, GEN *arch)
      47             : {
      48     8705355 :   GEN x = *ideal;
      49     8705355 :   long t,lx,tx = typ(x);
      50             : 
      51     8705355 :   if (tx!=t_VEC || lg(x)!=3) *arch = NULL;
      52             :   else
      53             :   {
      54      212524 :     GEN a = gel(x,2);
      55      212524 :     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 :       a = trivial_fact();
      59             :     }
      60      212517 :     *arch = a;
      61      212517 :     x = gel(x,1); tx = typ(x);
      62             :   }
      63     8705348 :   switch(tx)
      64             :   {
      65     3456439 :     case t_MAT: lx = lg(x);
      66     3456439 :       if (lx == 1) { t = id_PRINCIPAL; x = gen_0; break; }
      67     3456278 :       if (lx != lgcols(x)) pari_err_TYPE("idealtyp [nonsquare t_MAT]",x);
      68     3456268 :       t = id_MAT;
      69     3456268 :       break;
      70             : 
      71     4616239 :     case t_VEC: if (lg(x)!=6) pari_err_TYPE("idealtyp",x);
      72     4616224 :       t = id_PRIME; break;
      73             : 
      74      632818 :     case t_POL: case t_POLMOD: case t_COL:
      75             :     case t_INT: case t_FRAC:
      76      632818 :       t = id_PRINCIPAL; break;
      77           0 :     default:
      78           0 :       pari_err_TYPE("idealtyp",x);
      79             :       return 0; /*LCOV_EXCL_LINE*/
      80             :   }
      81     8705471 :   *ideal = x; return t;
      82             : }
      83             : 
      84             : /* true nf; v = [a,x,...], a in Z. Return (a,x) */
      85             : GEN
      86      668536 : idealhnf_two(GEN nf, GEN v)
      87             : {
      88      668536 :   GEN p = gel(v,1), pi = gel(v,2), m = zk_scalar_or_multable(nf, pi);
      89      668599 :   if (typ(m) == t_INT) return scalarmat(gcdii(m,p), nf_get_degree(nf));
      90      599451 :   return ZM_hnfmodid(m, p);
      91             : }
      92             : /* true nf */
      93             : GEN
      94     4004790 : pr_hnf(GEN nf, GEN pr)
      95             : {
      96     4004790 :   GEN p = pr_get_p(pr), m;
      97     4004753 :   if (pr_is_inert(pr)) return scalarmat(p, nf_get_degree(nf));
      98     3579724 :   m = zk_scalar_or_multable(nf, pr_get_gen(pr));
      99     3579449 :   return ZM_hnfmodprime(m, p);
     100             : }
     101             : 
     102             : GEN
     103      479577 : idealhnf_principal(GEN nf, GEN x)
     104             : {
     105             :   GEN cx;
     106      479577 :   x = nf_to_scalar_or_basis(nf, x);
     107      479577 :   switch(typ(x))
     108             :   {
     109      297849 :     case t_COL: break;
     110      152986 :     case t_INT:  if (!signe(x)) return cgetg(1,t_MAT);
     111      152223 :       return scalarmat(absi_shallow(x), nf_get_degree(nf));
     112       28741 :     case t_FRAC:
     113       28741 :       return scalarmat(Q_abs_shallow(x), nf_get_degree(nf));
     114           1 :     default: pari_err_TYPE("idealhnf",x);
     115             :   }
     116      297849 :   x = Q_primitive_part(x, &cx);
     117      297847 :   RgV_check_ZV(x, "idealhnf");
     118      297847 :   x = zk_multable(nf, x);
     119      297849 :   x = ZM_hnfmodid(x, zkmultable_capZ(x));
     120      297846 :   return cx? ZM_Q_mul(x,cx): x;
     121             : }
     122             : 
     123             : /* x integral ideal in t_MAT form, nx columns */
     124             : static GEN
     125           7 : vec_mulid(GEN nf, GEN x, long nx, long N)
     126             : {
     127           7 :   GEN m = cgetg(nx*N + 1, t_MAT);
     128             :   long i, j, k;
     129          21 :   for (i=k=1; i<=nx; i++)
     130          56 :     for (j=1; j<=N; j++) gel(m, k++) = zk_ei_mul(nf, gel(x,i),j);
     131           7 :   return m;
     132             : }
     133             : /* true nf */
     134             : GEN
     135      792626 : idealhnf_shallow(GEN nf, GEN x)
     136             : {
     137      792626 :   long tx = typ(x), lx = lg(x), N;
     138             : 
     139             :   /* cannot use idealtyp because here we allow nonsquare matrices */
     140      792626 :   if (tx == t_VEC && lx == 3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
     141      792626 :   if (tx == t_VEC && lx == 6) return pr_hnf(nf,x); /* PRIME */
     142      429575 :   switch(tx)
     143             :   {
     144       87723 :     case t_MAT:
     145             :     {
     146             :       GEN cx;
     147       87723 :       long nx = lx-1;
     148       87723 :       N = nf_get_degree(nf);
     149       87724 :       if (nx == 0) return cgetg(1, t_MAT);
     150       87703 :       if (nbrows(x) != N) pari_err_TYPE("idealhnf [wrong dimension]",x);
     151       87696 :       if (nx == 1) return idealhnf_principal(nf, gel(x,1));
     152             : 
     153       80717 :       if (nx == N && RgM_is_ZM(x) && ZM_ishnf(x)) return x;
     154       47208 :       x = Q_primitive_part(x, &cx);
     155       47208 :       if (nx < N) x = vec_mulid(nf, x, nx, N);
     156       47208 :       x = ZM_hnfmod(x, ZM_detmult(x));
     157       47208 :       return cx? ZM_Q_mul(x,cx): x;
     158             :     }
     159          14 :     case t_QFB:
     160             :     {
     161          14 :       pari_sp av = avma;
     162          14 :       GEN u, D = nf_get_disc(nf), T = nf_get_pol(nf), f = nf_get_index(nf);
     163          14 :       GEN A = gel(x,1), B = gel(x,2);
     164          14 :       N = nf_get_degree(nf);
     165          14 :       if (N != 2)
     166           0 :         pari_err_TYPE("idealhnf [Qfb for nonquadratic fields]", x);
     167          14 :       if (!equalii(qfb_disc(x), D))
     168           7 :         pari_err_DOMAIN("idealhnf [Qfb]", "disc(q)", "!=", D, x);
     169             :       /* x -> A Z + (-B + sqrt(D)) / 2 Z
     170             :          K = Q[t]/T(t), t^2 + ut + v = 0,  u^2 - 4v = Df^2
     171             :          => t = (-u + sqrt(D) f)/2
     172             :          => sqrt(D)/2 = (t + u/2)/f */
     173           7 :       u = gel(T,3);
     174           7 :       B = deg1pol_shallow(ginv(f),
     175             :                           gsub(gdiv(u, shifti(f,1)), gdiv(B,gen_2)),
     176           7 :                           varn(T));
     177           7 :       return gerepileupto(av, idealhnf_two(nf, mkvec2(A,B)));
     178             :     }
     179      341838 :     default: return idealhnf_principal(nf, x); /* PRINCIPAL */
     180             :   }
     181             : }
     182             : /* true nf */
     183             : GEN
     184         238 : idealhnf(GEN nf, GEN x)
     185             : {
     186         238 :   pari_sp av = avma;
     187         238 :   GEN y = idealhnf_shallow(nf, x);
     188         231 :   return (avma == av)? gcopy(y): gerepileupto(av, y);
     189             : }
     190             : 
     191             : /* GP functions */
     192             : 
     193             : GEN
     194          63 : idealtwoelt0(GEN nf, GEN x, GEN a)
     195             : {
     196          63 :   if (!a) return idealtwoelt(nf,x);
     197          42 :   return idealtwoelt2(nf,x,a);
     198             : }
     199             : 
     200             : GEN
     201          56 : idealpow0(GEN nf, GEN x, GEN n, long flag)
     202             : {
     203          56 :   if (flag) return idealpowred(nf,x,n);
     204          49 :   return idealpow(nf,x,n);
     205             : }
     206             : 
     207             : GEN
     208          56 : idealmul0(GEN nf, GEN x, GEN y, long flag)
     209             : {
     210          56 :   if (flag) return idealmulred(nf,x,y);
     211          49 :   return idealmul(nf,x,y);
     212             : }
     213             : 
     214             : GEN
     215          56 : idealdiv0(GEN nf, GEN x, GEN y, long flag)
     216             : {
     217          56 :   switch(flag)
     218             :   {
     219          28 :     case 0: return idealdiv(nf,x,y);
     220          28 :     case 1: return idealdivexact(nf,x,y);
     221           0 :     default: pari_err_FLAG("idealdiv");
     222             :   }
     223             :   return NULL; /* LCOV_EXCL_LINE */
     224             : }
     225             : 
     226             : GEN
     227          70 : idealaddtoone0(GEN nf, GEN arg1, GEN arg2)
     228             : {
     229          70 :   if (!arg2) return idealaddmultoone(nf,arg1);
     230          35 :   return idealaddtoone(nf,arg1,arg2);
     231             : }
     232             : 
     233             : /* b not a scalar */
     234             : static GEN
     235          28 : hnf_Z_ZC(GEN nf, GEN a, GEN b) { return hnfmodid(zk_multable(nf,b), a); }
     236             : /* b not a scalar */
     237             : static GEN
     238          21 : hnf_Z_QC(GEN nf, GEN a, GEN b)
     239             : {
     240             :   GEN db;
     241          21 :   b = Q_remove_denom(b, &db);
     242          21 :   if (db) a = mulii(a, db);
     243          21 :   b = hnf_Z_ZC(nf,a,b);
     244          21 :   return db? RgM_Rg_div(b, db): b;
     245             : }
     246             : /* b not a scalar (not point in trying to optimize for this case) */
     247             : static GEN
     248          28 : hnf_Q_QC(GEN nf, GEN a, GEN b)
     249             : {
     250             :   GEN da, db;
     251          28 :   if (typ(a) == t_INT) return hnf_Z_QC(nf, a, b);
     252           7 :   da = gel(a,2);
     253           7 :   a = gel(a,1);
     254           7 :   b = Q_remove_denom(b, &db);
     255             :   /* write da = d*A, db = d*B, gcd(A,B) = 1
     256             :    * gcd(a/(d A), b/(d B)) = gcd(a B, A b) / A B d = gcd(a B, b) / A B d */
     257           7 :   if (db)
     258             :   {
     259           7 :     GEN d = gcdii(da,db);
     260           7 :     if (!is_pm1(d)) db = diviiexact(db,d); /* B */
     261           7 :     if (!is_pm1(db))
     262             :     {
     263           7 :       a = mulii(a, db); /* a B */
     264           7 :       da = mulii(da, db); /* A B d = lcm(denom(a),denom(b)) */
     265             :     }
     266             :   }
     267           7 :   return RgM_Rg_div(hnf_Z_ZC(nf,a,b), da);
     268             : }
     269             : static GEN
     270           7 : hnf_QC_QC(GEN nf, GEN a, GEN b)
     271             : {
     272             :   GEN da, db, d, x;
     273           7 :   a = Q_remove_denom(a, &da);
     274           7 :   b = Q_remove_denom(b, &db);
     275           7 :   if (da) b = ZC_Z_mul(b, da);
     276           7 :   if (db) a = ZC_Z_mul(a, db);
     277           7 :   d = mul_denom(da, db);
     278           7 :   a = zk_multable(nf,a); da = zkmultable_capZ(a);
     279           7 :   b = zk_multable(nf,b); db = zkmultable_capZ(b);
     280           7 :   x = ZM_hnfmodid(shallowconcat(a,b), gcdii(da,db));
     281           7 :   return d? RgM_Rg_div(x, d): x;
     282             : }
     283             : static GEN
     284          21 : hnf_Q_Q(GEN nf, GEN a, GEN b) {return scalarmat(Q_gcd(a,b), nf_get_degree(nf));}
     285             : GEN
     286         147 : idealhnf0(GEN nf, GEN a, GEN b)
     287             : {
     288             :   long ta, tb;
     289             :   pari_sp av;
     290             :   GEN x;
     291         147 :   nf = checknf(nf);
     292         147 :   if (!b) return idealhnf(nf,a);
     293             : 
     294             :   /* HNF of aZ_K+bZ_K */
     295          63 :   av = avma;
     296          63 :   a = nf_to_scalar_or_basis(nf,a); ta = typ(a);
     297          63 :   b = nf_to_scalar_or_basis(nf,b); tb = typ(b);
     298          56 :   if (ta == t_COL)
     299          14 :     x = (tb==t_COL)? hnf_QC_QC(nf, a,b): hnf_Q_QC(nf, b,a);
     300             :   else
     301          42 :     x = (tb==t_COL)? hnf_Q_QC(nf, a,b): hnf_Q_Q(nf, a,b);
     302          56 :   return gerepileupto(av, x);
     303             : }
     304             : 
     305             : /*******************************************************************/
     306             : /*                                                                 */
     307             : /*                       TWO-ELEMENT FORM                          */
     308             : /*                                                                 */
     309             : /*******************************************************************/
     310             : static GEN idealapprfact_i(GEN nf, GEN x, int nored);
     311             : 
     312             : static int
     313      431027 : ok_elt(GEN x, GEN xZ, GEN y)
     314             : {
     315      431027 :   pari_sp av = avma;
     316      431027 :   return gc_bool(av, ZM_equal(x, ZM_hnfmodid(y, xZ)));
     317             : }
     318             : 
     319             : static GEN
     320      135704 : addmul_col(GEN a, long s, GEN b)
     321             : {
     322             :   long i,l;
     323      135704 :   if (!s) return a? leafcopy(a): a;
     324      135409 :   if (!a) return gmulsg(s,b);
     325      125019 :   l = lg(a);
     326      602924 :   for (i=1; i<l; i++)
     327      477907 :     if (signe(gel(b,i))) gel(a,i) = addii(gel(a,i), mulsi(s, gel(b,i)));
     328      125017 :   return a;
     329             : }
     330             : 
     331             : /* a <-- a + s * b, all coeffs integers */
     332             : static GEN
     333       68433 : addmul_mat(GEN a, long s, GEN b)
     334             : {
     335             :   long j,l;
     336             :   /* copy otherwise next call corrupts a */
     337       68433 :   if (!s) return a? RgM_shallowcopy(a): a;
     338       63995 :   if (!a) return gmulsg(s,b);
     339       32431 :   l = lg(a);
     340      144889 :   for (j=1; j<l; j++)
     341      112458 :     (void)addmul_col(gel(a,j), s, gel(b,j));
     342       32431 :   return a;
     343             : }
     344             : 
     345             : static GEN
     346      260249 : get_random_a(GEN nf, GEN x, GEN xZ)
     347             : {
     348             :   pari_sp av;
     349      260249 :   long i, lm, l = lg(x);
     350             :   GEN a, z, beta, mul;
     351             : 
     352      260249 :   beta= cgetg(l, t_VEC);
     353      260249 :   mul = cgetg(l, t_VEC); lm = 1; /* = lg(mul) */
     354             :   /* look for a in x such that a O/xZ = x O/xZ */
     355      468055 :   for (i = 2; i < l; i++)
     356             :   {
     357      457665 :     GEN xi = gel(x,i);
     358      457665 :     GEN t = FpM_red(zk_multable(nf,xi), xZ); /* ZM, cannot be a scalar */
     359      457662 :     if (gequal0(t)) continue;
     360      399463 :     if (ok_elt(x,xZ, t)) return xi;
     361      149604 :     gel(beta,lm) = xi;
     362             :     /* mul[i] = { canonical generators for x[i] O/xZ as Z-module } */
     363      149604 :     gel(mul,lm) = t; lm++;
     364             :   }
     365       10390 :   setlg(mul, lm);
     366       10390 :   setlg(beta,lm);
     367       10390 :   z = cgetg(lm, t_VECSMALL);
     368       31668 :   for(av = avma;; set_avma(av))
     369             :   {
     370      100101 :     for (a=NULL,i=1; i<lm; i++)
     371             :     {
     372       68433 :       long t = random_bits(4) - 7; /* in [-7,8] */
     373       68433 :       z[i] = t;
     374       68433 :       a = addmul_mat(a, t, gel(mul,i));
     375             :     }
     376             :     /* a = matrix (NOT HNF) of ideal generated by beta.z in O/xZ */
     377       31668 :     if (a && ok_elt(x,xZ, a)) break;
     378             :   }
     379       33636 :   for (a=NULL,i=1; i<lm; i++)
     380       23246 :     a = addmul_col(a, z[i], gel(beta,i));
     381       10390 :   return a;
     382             : }
     383             : 
     384             : /* x square matrix, assume it is HNF */
     385             : static GEN
     386      393920 : mat_ideal_two_elt(GEN nf, GEN x)
     387             : {
     388             :   GEN y, a, cx, xZ;
     389      393920 :   long N = nf_get_degree(nf);
     390             :   pari_sp av, tetpil;
     391             : 
     392      393920 :   if (lg(x)-1 != N) pari_err_DIM("idealtwoelt");
     393      393906 :   if (N == 2) return mkvec2copy(gcoeff(x,1,1), gel(x,2));
     394             : 
     395      279944 :   y = cgetg(3,t_VEC); av = avma;
     396      279944 :   cx = Q_content(x);
     397      279945 :   xZ = gcoeff(x,1,1);
     398      279945 :   if (gequal(xZ, cx)) /* x = (cx) */
     399             :   {
     400        4819 :     gel(y,1) = cx;
     401        4819 :     gel(y,2) = gen_0; return y;
     402             :   }
     403      275124 :   if (equali1(cx)) cx = NULL;
     404             :   else
     405             :   {
     406        1526 :     x = Q_div_to_int(x, cx);
     407        1526 :     xZ = gcoeff(x,1,1);
     408             :   }
     409      275124 :   if (N < 6)
     410      245403 :     a = get_random_a(nf, x, xZ);
     411             :   else
     412             :   {
     413       29721 :     const long FB[] = { _evallg(15+1) | evaltyp(t_VECSMALL),
     414             :       2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47
     415             :     };
     416       29721 :     GEN P, E, a1 = Z_lsmoothen(xZ, (GEN)FB, &P, &E);
     417       29721 :     if (!a1) /* factors completely */
     418       14876 :       a = idealapprfact_i(nf, idealfactor(nf,x), 1);
     419       14845 :     else if (lg(P) == 1) /* no small factors */
     420       10859 :       a = get_random_a(nf, x, xZ);
     421             :     else /* general case */
     422             :     {
     423             :       GEN A0, A1, a0, u0, u1, v0, v1, pi0, pi1, t, u;
     424        3986 :       a0 = diviiexact(xZ, a1);
     425        3986 :       A0 = ZM_hnfmodid(x, a0); /* smooth part of x */
     426        3986 :       A1 = ZM_hnfmodid(x, a1); /* cofactor */
     427        3986 :       pi0 = idealapprfact_i(nf, idealfactor(nf,A0), 1);
     428        3986 :       pi1 = get_random_a(nf, A1, a1);
     429        3986 :       (void)bezout(a0, a1, &v0,&v1);
     430        3986 :       u0 = mulii(a0, v0);
     431        3986 :       u1 = mulii(a1, v1);
     432        3986 :       if (typ(pi0) != t_COL) t = addmulii(u0, pi0, u1);
     433             :       else
     434        3986 :       { t = ZC_Z_mul(pi0, u1); gel(t,1) = addii(gel(t,1), u0); }
     435        3986 :       u = ZC_Z_mul(pi1, u0); gel(u,1) = addii(gel(u,1), u1);
     436        3986 :       a = nfmuli(nf, centermod(u, xZ), centermod(t, xZ));
     437             :     }
     438             :   }
     439      275126 :   if (cx)
     440             :   {
     441        1526 :     a = centermod(a, xZ);
     442        1526 :     tetpil = avma;
     443        1526 :     if (typ(cx) == t_INT)
     444             :     {
     445          70 :       gel(y,1) = mulii(xZ, cx);
     446          70 :       gel(y,2) = ZC_Z_mul(a, cx);
     447             :     }
     448             :     else
     449             :     {
     450        1456 :       gel(y,1) = gmul(xZ, cx);
     451        1456 :       gel(y,2) = RgC_Rg_mul(a, cx);
     452             :     }
     453             :   }
     454             :   else
     455             :   {
     456      273600 :     tetpil = avma;
     457      273600 :     gel(y,1) = icopy(xZ);
     458      273600 :     gel(y,2) = centermod(a, xZ);
     459             :   }
     460      275123 :   gerepilecoeffssp(av,tetpil,y+1,2); return y;
     461             : }
     462             : 
     463             : /* Given an ideal x, returns [a,alpha] such that a is in Q,
     464             :  * x = a Z_K + alpha Z_K, alpha in K^*
     465             :  * a = 0 or alpha = 0 are possible, but do not try to determine whether
     466             :  * x is principal. */
     467             : GEN
     468       97874 : idealtwoelt(GEN nf, GEN x)
     469             : {
     470             :   pari_sp av;
     471             :   GEN z;
     472       97874 :   long tx = idealtyp(&x,&z);
     473       97868 :   nf = checknf(nf);
     474       97868 :   if (tx == id_MAT) return mat_ideal_two_elt(nf,x);
     475        1029 :   if (tx == id_PRIME) return mkvec2copy(gel(x,1), gel(x,2));
     476             :   /* id_PRINCIPAL */
     477        1022 :   av = avma; x = nf_to_scalar_or_basis(nf, x);
     478        1848 :   return gerepilecopy(av, typ(x)==t_COL? mkvec2(gen_0,x):
     479         917 :                                          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      737310 : idealHNF_norm_pval(GEN x, GEN p, long Zval)
     490             : {
     491      737310 :   long i, v = Zval, l = lg(x);
     492     2350618 :   for (i = 2; i < l; i++) v += Z_pval(gcoeff(x,i,i), p);
     493      737310 :   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      269997 : idealHNF_Z_factor_i(GEN x, GEN f0, GEN *pvN, GEN *pvZ)
     500             : {
     501      269997 :   GEN P, E, vN, vZ, xZ = gcoeff(x,1,1), f = f0? f0: Z_factor(xZ);
     502             :   long i, l;
     503      270013 :   P = gel(f,1); l = lg(P);
     504      270013 :   E = gel(f,2);
     505      270013 :   *pvN = vN = cgetg(l, t_VECSMALL);
     506      270019 :   *pvZ = vZ = cgetg(l, t_VECSMALL);
     507      703031 :   for (i = 1; i < l; i++)
     508             :   {
     509      433008 :     GEN p = gel(P,i);
     510      433008 :     vZ[i] = f0? Z_pval(xZ, p): (long) itou(gel(E,i));
     511      433009 :     vN[i] = idealHNF_norm_pval(x,p, vZ[i]);
     512             :   }
     513      270023 :   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      889859 : idealHNF_val(GEN A, GEN P, long Nval, long Zval)
     525             : {
     526      889859 :   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      889857 :   if (Nval < f) return 0;
     531      889318 :   p = pr_get_p(P);
     532      889321 :   e = pr_get_e(P);
     533             :   /* v_P(A) <= max [ e * v_p(A \cap Z), floor[v_p(Nix) / f ] */
     534      889321 :   vmax = minss(Zval * e, Nval / f);
     535      889323 :   mul = pr_get_tau(P);
     536      889320 :   l = lg(mul);
     537      889320 :   B = cgetg(l,t_MAT);
     538             :   /* B[1] not needed: v_pr(A[1]) = v_pr(A \cap Z) is known already */
     539      889695 :   gel(B,1) = gen_0; /* dummy */
     540     2694464 :   for (j = 2; j < l; j++)
     541             :   {
     542     2031230 :     GEN x = gel(A,j);
     543     2031230 :     gel(B,j) = y = cgetg(l, t_COL);
     544    17116153 :     for (i = 1; i < l; i++)
     545             :     { /* compute a = (x.t0)_i, A in HNF ==> x[j+1..l-1] = 0 */
     546    15311384 :       a = mulii(gel(x,1), gcoeff(mul,i,1));
     547   141302750 :       for (k = 2; k <= j; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     548             :       /* p | a ? */
     549    15311036 :       gel(y,i) = dvmdii(a,p,&r); if (signe(r)) return 0;
     550             :     }
     551             :   }
     552      663234 :   vals = cgetg(l, t_VECSMALL);
     553             :   /* vals[1] not needed */
     554     2292031 :   for (j = 2; j < l; j++)
     555             :   {
     556     1628743 :     gel(B,j) = Q_primitive_part(gel(B,j), &cx);
     557     1628752 :     vals[j] = cx? 1 + e * Q_pval(cx, p): 1;
     558             :   }
     559      663288 :   pk = powiu(p, ceildivuu(vmax, e));
     560      663275 :   av = avma; y = cgetg(l,t_COL);
     561             :   /* can compute mod p^ceil((vmax-v)/e) */
     562     1342817 :   for (v = 1; v < vmax; v++)
     563             :   { /* we know v_pr(Bj) >= v for all j */
     564      704910 :     if (e == 1 || (vmax - v) % e == 0) pk = diviiexact(pk, p);
     565     3990785 :     for (j = 2; j < l; j++)
     566             :     {
     567     3311267 :       GEN x = gel(B,j); if (v < vals[j]) continue;
     568    20767913 :       for (i = 1; i < l; i++)
     569             :       {
     570    18817624 :         pari_sp av2 = avma;
     571    18817624 :         a = mulii(gel(x,1), gcoeff(mul,i,1));
     572   299764634 :         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    18816666 :         a = dvmdii(a,p,&r); if (signe(r)) return v;
     575    18791214 :         if (lgefint(a) > lgefint(pk)) a = remii(a, pk);
     576    18791211 :         gel(y,i) = gerepileuptoint(av2, a);
     577             :       }
     578     1950289 :       gel(B,j) = y; y = x;
     579     1950289 :       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      637907 :   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      269994 : idealHNF_factor_i(GEN nf, GEN x, GEN cx, GEN FA)
     593             : {
     594      269994 :   const long N = lg(x)-1;
     595             :   long i, j, k, l, v;
     596      269994 :   GEN vN, vZ, vP, vE, vp = idealHNF_Z_factor_i(x, FA, &vN,&vZ);
     597             : 
     598      270022 :   l = lg(vp);
     599      270022 :   i = cx? expi(cx)+1: 1;
     600      270023 :   vP = cgetg((l+i-2)*N+1, t_COL);
     601      270021 :   vE = cgetg((l+i-2)*N+1, t_COL);
     602      703020 :   for (i = k = 1; i < l; i++)
     603             :   {
     604      433008 :     GEN L, p = gel(vp,i);
     605      433008 :     long Nval = vN[i], Zval = vZ[i], vc = cx? Z_pvalrem(cx,p,&cx): 0;
     606      433007 :     if (vc)
     607             :     {
     608       43009 :       L = idealprimedec(nf,p);
     609       43008 :       if (is_pm1(cx)) cx = NULL;
     610             :     }
     611             :     else
     612      389998 :       L = idealprimedec_limit_f(nf,p,Nval);
     613     1018550 :     for (j = 1; Nval && j < lg(L); j++) /* !Nval => only cx contributes */
     614             :     {
     615      585553 :       GEN P = gel(L,j);
     616      585553 :       pari_sp av = avma;
     617      585553 :       v = idealHNF_val(x, P, Nval, Zval);
     618      585528 :       set_avma(av);
     619      585529 :       Nval -= v*pr_get_f(P);
     620      585532 :       v += vc * pr_get_e(P); if (!v) continue;
     621      477923 :       gel(vP,k) = P;
     622      477923 :       gel(vE,k) = utoipos(v); k++;
     623             :     }
     624      476154 :     if (vc) for (; j<lg(L); j++)
     625             :     {
     626       43164 :       GEN P = gel(L,j);
     627       43164 :       gel(vP,k) = P;
     628       43164 :       gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     629             :     }
     630             :   }
     631      270012 :   if (cx && !FA)
     632             :   { /* complete factorization */
     633       59717 :     GEN f = Z_factor(cx), cP = gel(f,1), cE = gel(f,2);
     634       59717 :     long lc = lg(cP);
     635      126761 :     for (i=1; i<lc; i++)
     636             :     {
     637       67046 :       GEN p = gel(cP,i), L = idealprimedec(nf,p);
     638       67044 :       long vc = itos(gel(cE,i));
     639      144000 :       for (j=1; j<lg(L); j++)
     640             :       {
     641       76956 :         GEN P = gel(L,j);
     642       76956 :         gel(vP,k) = P;
     643       76956 :         gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     644             :       }
     645             :     }
     646             :   }
     647      270010 :   setlg(vP, k);
     648      270011 :   setlg(vE, k); return mkmat2(vP, vE);
     649             : }
     650             : /* true nf, x integral ideal */
     651             : static GEN
     652      225091 : idealHNF_factor(GEN nf, GEN x, ulong lim)
     653             : {
     654      225091 :   GEN cx, F = NULL;
     655      225091 :   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      225091 :   x = Q_primitive_part(x, &cx);
     670      225072 :   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       32556 : prV_e_muls(GEN L, long c)
     675             : {
     676       32556 :   long j, l = lg(L);
     677       32556 :   GEN z = cgetg(l, t_COL);
     678       66438 :   for (j = 1; j < l; j++) gel(z,j) = stoi(c * pr_get_e(gel(L,j)));
     679       32575 :   return z;
     680             : }
     681             : /* true nf, y in Q */
     682             : static GEN
     683       25257 : Q_nffactor(GEN nf, GEN y, ulong lim)
     684             : {
     685             :   GEN f, P, E;
     686             :   long l, i;
     687       25257 :   if (typ(y) == t_INT)
     688             :   {
     689       25233 :     if (!signe(y)) pari_err_DOMAIN("idealfactor", "ideal", "=",gen_0,y);
     690       25219 :     if (is_pm1(y)) return trivial_fact();
     691             :   }
     692       17054 :   y = Q_abs_shallow(y);
     693       17065 :   if (!lim) f = Q_factor(y);
     694             :   else
     695             :   {
     696          36 :     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       17051 :   P = gel(f,1); l = lg(P); if (l == 1) return f;
     704       17037 :   E = gel(f,2);
     705       49639 :   for (i = 1; i < l; i++)
     706             :   {
     707       32578 :     gel(P,i) = idealprimedec(nf, gel(P,i));
     708       32543 :     gel(E,i) = prV_e_muls(gel(P,i), itos(gel(E,i)));
     709             :   }
     710       17061 :   P = shallowconcat1(P); gel(f,1) = P; settyp(P, t_COL);
     711       17077 :   E = shallowconcat1(E); gel(f,2) = E; return f;
     712             : }
     713             : 
     714             : GEN
     715        3738 : idealfactor_partial(GEN nf, GEN x, GEN L)
     716             : {
     717        3738 :   pari_sp av = avma;
     718             :   long i, j, l;
     719             :   GEN P, E;
     720        3738 :   if (!L) return idealfactor(nf, x);
     721        2968 :   if (typ(L) == t_INT) return idealfactor_limit(nf, x, itou(L));
     722        2947 :   l = lg(L); if (l == 1) return trivial_fact();
     723        2163 :   P = cgetg(l, t_VEC);
     724        4466 :   for (i = 1; i < l; i++)
     725             :   {
     726        2303 :     GEN p = gel(L,i);
     727        2303 :     gel(P,i) = typ(p) == t_INT? idealprimedec(nf, p): mkvec(p);
     728             :   }
     729        2163 :   P = shallowconcat1(P); settyp(P, t_COL);
     730        2163 :   P = gen_sort_uniq(P, (void*)&cmp_prime_ideal, &cmp_nodata);
     731        2163 :   E = cgetg_copy(P, &l);
     732       12194 :   for (i = j = 1; i < l; i++)
     733             :   {
     734       10031 :     long v = idealval(nf, x, gel(P,i));
     735       10031 :     if (v) { gel(P,j) = gel(P,i); gel(E,j) = stoi(v); j++; }
     736             :   }
     737        2163 :   setlg(P,j);
     738        2163 :   setlg(E,j); return gerepilecopy(av, mkmat2(P, E));
     739             : }
     740             : GEN
     741      250392 : idealfactor_limit(GEN nf, GEN x, ulong lim)
     742             : {
     743      250392 :   pari_sp av = avma;
     744             :   GEN fa, y;
     745      250392 :   long tx = idealtyp(&x,&y);
     746             : 
     747      250398 :   if (tx == id_PRIME)
     748             :   {
     749          63 :     if (lim && abscmpiu(pr_get_p(x), lim) >= 0) return trivial_fact();
     750          56 :     retmkmat2(mkcolcopy(x), mkcol(gen_1));
     751             :   }
     752      250335 :   nf = checknf(nf);
     753      250331 :   if (tx == id_PRINCIPAL)
     754             :   {
     755       26633 :     y = nf_to_scalar_or_basis(nf, x);
     756       26637 :     if (typ(y) != t_COL) return gerepilecopy(av, Q_nffactor(nf, y, lim));
     757             :   }
     758      225068 :   y = idealnumden(nf, x);
     759      225080 :   fa = idealHNF_factor(nf, gel(y,1), lim);
     760      225077 :   if (!isint1(gel(y,2)))
     761          14 :     fa = famat_div_shallow(fa, idealHNF_factor(nf, gel(y,2), lim));
     762      225077 :   fa = gerepilecopy(av, fa);
     763      225085 :   return sort_factor(fa, (void*)&cmp_prime_ideal, &cmp_nodata);
     764             : }
     765             : GEN
     766      250212 : idealfactor(GEN nf, GEN x) { return idealfactor_limit(nf, x, 0); }
     767             : GEN
     768         154 : gpidealfactor(GEN nf, GEN x, GEN lim)
     769             : {
     770         154 :   ulong L = 0;
     771         154 :   if (lim)
     772             :   {
     773          70 :     if (typ(lim) != t_INT || signe(lim) < 0) pari_err_FLAG("idealfactor");
     774          70 :     L = itou(lim);
     775             :   }
     776         154 :   return idealfactor_limit(nf, x, L);
     777             : }
     778             : 
     779             : static GEN
     780        6664 : ramified_root(GEN nf, GEN R, GEN A, long n)
     781             : {
     782        6664 :   GEN v, P = gel(idealfactor(nf, R), 1);
     783        6664 :   long i, l = lg(P);
     784        6664 :   v = cgetg(l, t_VECSMALL);
     785        7140 :   for (i = 1; i < l; i++)
     786             :   {
     787         483 :     long w = idealval(nf, A, gel(P,i));
     788         483 :     if (w % n) return NULL;
     789         476 :     v[i] = w / n;
     790             :   }
     791        6657 :   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        6664 : idealsqrtn_int(GEN nf, GEN A, long n, GEN *pB)
     813             : {
     814             :   GEN C, root;
     815             :   long i, l;
     816             : 
     817        6664 :   if (typ(A) == t_INT) /* > 0 */
     818             :   {
     819        3332 :     GEN P = nf_get_ramified_primes(nf), v, q;
     820        3332 :     l = lg(P); v = cgetg(l, t_VECSMALL);
     821       15638 :     for (i = 1; i < l; i++) v[i] = Z_pvalrem(A, gel(P,i), &A);
     822        3332 :     C = gen_1;
     823        3332 :     if (!isint1(A) && !Z_ispowerall(A, n, pB? &C: NULL)) return 0;
     824        3332 :     if (!pB) return ramified_root_simple(nf, n, P, v);
     825        3332 :     q = factorback2(P, v);
     826        3332 :     root = ramified_root(nf, q, q, n);
     827        3332 :     if (!root) return 0;
     828        3332 :     if (!equali1(C)) root = isint1(root)? C: ZM_Z_mul(root, C);
     829        3332 :     *pB = root; return 1;
     830             :   }
     831             :   /* compute valuations at ramified primes */
     832        3332 :   root = ramified_root(nf, idealadd(nf, nf_get_diff(nf), A), A, n);
     833        3332 :   if (!root) return 0;
     834             :   /* remove ramified primes */
     835        3325 :   if (isint1(root))
     836        2891 :     root = matid(nf_get_degree(nf));
     837             :   else
     838         434 :     A = idealdivexact(nf, A, idealpows(nf,root,n));
     839        3325 :   A = Q_primitive_part(A, &C);
     840        3325 :   if (C)
     841             :   {
     842           0 :     if (!Z_ispowerall(C,n,&C)) return 0;
     843           0 :     if (pB) root = ZM_Z_mul(root, C);
     844             :   }
     845             : 
     846             :   /* compute final n-th root, at most degree(nf)-1 iterations */
     847        3325 :   for (i = 0;; i++)
     848        2247 :   {
     849        5572 :     GEN J, b, a = gcoeff(A,1,1); /* A \cap Z */
     850        5572 :     if (is_pm1(a)) break;
     851        2268 :     if (!Z_ispowerall(a,n,&b)) return 0;
     852        2247 :     J = idealadd(nf, b, A);
     853        2247 :     A = idealdivexact(nf, idealpows(nf,J,n), A);
     854             :     /* div and not divexact here */
     855        2247 :     if (pB) root = odd(i)? idealdiv(nf, root, J): idealmul(nf, root, J);
     856             :   }
     857        3304 :   if (pB) *pB = root;
     858        3304 :   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        3353 : idealispower(GEN nf, GEN A, long n, GEN *pB)
     865             : {
     866        3353 :   pari_sp av = avma;
     867             :   GEN v, N, D;
     868        3353 :   nf = checknf(nf);
     869        3353 :   if (n <= 0) pari_err_DOMAIN("idealispower", "n", "<=", gen_0, stoi(n));
     870        3353 :   if (n == 1) { if (pB) *pB = idealhnf(nf,A); return 1; }
     871        3346 :   v = idealnumden(nf,A);
     872        3346 :   if (gequal0(gel(v,1))) { set_avma(av); if (pB) *pB = cgetg(1,t_MAT); return 1; }
     873        3346 :   if (!idealsqrtn_int(nf, gel(v,1), n, pB? &N: NULL)) return 0;
     874        3318 :   if (!idealsqrtn_int(nf, gel(v,2), n, pB? &D: NULL)) return 0;
     875        3318 :   if (pB) *pB = gerepileupto(av, idealdiv(nf,N,D)); else set_avma(av);
     876        3318 :   return 1;
     877             : }
     878             : 
     879             : /* x t_INT or integral nonzero ideal in HNF */
     880             : static GEN
     881       95576 : idealredmodpower_i(GEN nf, GEN x, ulong k, ulong B)
     882             : {
     883             :   GEN cx, y, U, N, F, Q;
     884       95576 :   if (typ(x) == t_INT)
     885             :   {
     886       50044 :     if (!signe(x) || is_pm1(x)) return gen_1;
     887        1792 :     F = Z_factor_limit(x, B);
     888        1792 :     gel(F,2) = gdiventgs(gel(F,2), k);
     889        1792 :     return ginv(factorback(F));
     890             :   }
     891       45532 :   N = gcoeff(x,1,1); if (is_pm1(N)) return gen_1;
     892       44923 :   F = absZ_factor_limit_strict(N, B, &U);
     893       44924 :   if (U)
     894             :   {
     895         182 :     GEN M = powii(gel(U,1), gel(U,2));
     896         182 :     y = hnfmodid(x, M); /* coprime part to B! */
     897         182 :     if (!idealispower(nf, y, k, &U)) U = NULL;
     898         182 :     x = hnfmodid(x, diviiexact(N, M));
     899             :   }
     900             :   /* x = B-smooth part of initial x */
     901       44924 :   x = Q_primitive_part(x, &cx);
     902       44923 :   F = idealHNF_factor_i(nf, x, cx, F);
     903       44923 :   gel(F,2) = gdiventgs(gel(F,2), k);
     904       44923 :   Q = idealfactorback(nf, gel(F,1), gel(F,2), 0);
     905       44923 :   if (U) Q = idealmul(nf,Q,U);
     906       44923 :   if (typ(Q) == t_INT) return Q;
     907       13844 :   y = idealred_elt(nf, idealHNF_inv_Z(nf, Q));
     908       13844 :   return gdiv(y, gcoeff(Q,1,1));
     909             : }
     910             : GEN
     911       47794 : idealredmodpower(GEN nf, GEN x, ulong n, ulong B)
     912             : {
     913       47794 :   pari_sp av = avma;
     914             :   GEN a, b;
     915       47794 :   nf = checknf(nf);
     916       47794 :   if (!n) pari_err_DOMAIN("idealredmodpower","n", "=", gen_0, gen_0);
     917       47794 :   x = idealnumden(nf, x);
     918       47796 :   a = gel(x,1);
     919       47796 :   if (isintzero(a)) { set_avma(av); return gen_1; }
     920       47788 :   a = idealredmodpower_i(nf, gel(x,1), n, B);
     921       47788 :   b = idealredmodpower_i(nf, gel(x,2), n, B);
     922       47788 :   if (!isint1(b)) a = nf_to_scalar_or_basis(nf, nfdiv(nf, a, b));
     923       47788 :   return gerepilecopy(av, a);
     924             : }
     925             : 
     926             : /* P prime ideal in idealprimedec format. Return valuation(A) at P */
     927             : long
     928      872593 : idealval(GEN nf, GEN A, GEN P)
     929             : {
     930      872593 :   pari_sp av = avma;
     931             :   GEN a, p, cA;
     932      872593 :   long vcA, v, Zval, tx = idealtyp(&A,&a);
     933             : 
     934      872588 :   if (tx == id_PRINCIPAL) return nfval(nf,A,P);
     935      790524 :   checkprid(P);
     936      790526 :   if (tx == id_PRIME) return pr_equal(P, A)? 1: 0;
     937             :   /* id_MAT */
     938      790498 :   nf = checknf(nf);
     939      790498 :   A = Q_primitive_part(A, &cA);
     940      790495 :   p = pr_get_p(P);
     941      790496 :   vcA = cA? Q_pval(cA,p): 0;
     942      790496 :   if (pr_is_inert(P)) return gc_long(av,vcA);
     943      778995 :   Zval = Z_pval(gcoeff(A,1,1), p);
     944      778995 :   if (!Zval) v = 0;
     945             :   else
     946             :   {
     947      304306 :     long Nval = idealHNF_norm_pval(A, p, Zval);
     948      304307 :     v = idealHNF_val(A, P, Nval, Zval);
     949             :   }
     950      778999 :   return gc_long(av, vcA? v + vcA*pr_get_e(P): v);
     951             : }
     952             : GEN
     953        6615 : gpidealval(GEN nf, GEN ix, GEN P)
     954             : {
     955        6615 :   long v = idealval(nf,ix,P);
     956        6615 :   return v == LONG_MAX? mkoo(): stoi(v);
     957             : }
     958             : 
     959             : /* gcd and generalized Bezout */
     960             : 
     961             : GEN
     962       70630 : idealadd(GEN nf, GEN x, GEN y)
     963             : {
     964       70630 :   pari_sp av = avma;
     965             :   long tx, ty;
     966             :   GEN z, a, dx, dy, dz;
     967             : 
     968       70630 :   tx = idealtyp(&x,&z);
     969       70630 :   ty = idealtyp(&y,&z); nf = checknf(nf);
     970       70630 :   if (tx != id_MAT) x = idealhnf_shallow(nf,x);
     971       70630 :   if (ty != id_MAT) y = idealhnf_shallow(nf,y);
     972       70630 :   if (lg(x) == 1) return gerepilecopy(av,y);
     973       70504 :   if (lg(y) == 1) return gerepilecopy(av,x); /* check for 0 ideal */
     974       70049 :   dx = Q_denom(x);
     975       70049 :   dy = Q_denom(y); dz = lcmii(dx,dy);
     976       70049 :   if (is_pm1(dz)) dz = NULL; else {
     977       13048 :     x = Q_muli_to_int(x, dz);
     978       13048 :     y = Q_muli_to_int(y, dz);
     979             :   }
     980       70049 :   a = gcdii(gcoeff(x,1,1), gcoeff(y,1,1));
     981       70049 :   if (is_pm1(a))
     982             :   {
     983       33521 :     long N = lg(x)-1;
     984       33521 :     if (!dz) { set_avma(av); return matid(N); }
     985        3773 :     return gerepileupto(av, scalarmat(ginv(dz), N));
     986             :   }
     987       36528 :   z = ZM_hnfmodid(shallowconcat(x,y), a);
     988       36528 :   if (dz) z = RgM_Rg_div(z,dz);
     989       36528 :   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      607972 : _idealaddtoone(GEN nf, GEN x, GEN y, long red)
     998             : {
     999             :   GEN a;
    1000      607972 :   long tx = idealtyp(&x, &a/*junk*/);
    1001      607958 :   long ty = idealtyp(&y, &a/*junk*/);
    1002             :   long ea;
    1003      607954 :   if (tx != id_MAT) x = idealhnf_shallow(nf, x);
    1004      607989 :   if (ty != id_MAT) y = idealhnf_shallow(nf, y);
    1005      607989 :   if (lg(x) == 1)
    1006          14 :     a = trivial_merge(y);
    1007      607975 :   else if (lg(y) == 1)
    1008          14 :     a = trivial_merge(x);
    1009             :   else
    1010      607961 :     a = hnfmerge_get_1(x, y);
    1011      607979 :   if (!a) pari_err_COPRIME("idealaddtoone",x,y);
    1012      607959 :   if (red && (ea = gexpo(a)) > 10)
    1013             :   {
    1014        5572 :     GEN b = (typ(a) == t_COL)? a: scalarcol_shallow(a, nf_get_degree(nf));
    1015        5572 :     b = ZC_reducemodlll(b, idealHNF_mul(nf,x,y));
    1016        5572 :     if (gexpo(b) < ea) a = b;
    1017             :   }
    1018      607959 :   return a;
    1019             : }
    1020             : /* true nf */
    1021             : GEN
    1022       18144 : idealaddtoone_i(GEN nf, GEN x, GEN y)
    1023       18144 : { return _idealaddtoone(nf, x, y, 1); }
    1024             : /* true nf */
    1025             : GEN
    1026      589826 : idealaddtoone_raw(GEN nf, GEN x, GEN y)
    1027      589826 : { 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     2909837 : idealHNF_mul_two(GEN nf, GEN x, GEN y)
    1095             : {
    1096     2909837 :   GEN m, a = gel(y,1), alpha = gel(y,2);
    1097             :   long i, N;
    1098             : 
    1099     2909837 :   if (typ(alpha) != t_MAT)
    1100             :   {
    1101     2595093 :     alpha = zk_scalar_or_multable(nf, alpha);
    1102     2595097 :     if (typ(alpha) == t_INT) /* e.g. y inert ? 0 should not (but may) occur */
    1103       18309 :       return signe(a)? ZM_Z_mul(x, gcdii(a, alpha)): cgetg(1,t_MAT);
    1104             :   }
    1105     2891532 :   N = lg(x)-1; m = cgetg((N<<1)+1,t_MAT);
    1106    10813820 :   for (i=1; i<=N; i++) gel(m,i)   = ZM_ZC_mul(alpha,gel(x,i));
    1107    10812536 :   for (i=1; i<=N; i++) gel(m,i+N) = ZC_Z_mul(gel(x,i), a);
    1108     2890493 :   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      963930 : idealHNF_mul(GEN nf, GEN x, GEN y)
    1115             : {
    1116             :   GEN z;
    1117      963930 :   if (typ(y) == t_VEC)
    1118      677641 :     z = idealHNF_mul_two(nf,x,y);
    1119             :   else
    1120             :   { /* reduce one ideal to two-elt form. The smallest */
    1121      286289 :     GEN xZ = gcoeff(x,1,1), yZ = gcoeff(y,1,1);
    1122      286289 :     if (cmpii(xZ, yZ) < 0)
    1123             :     {
    1124      116576 :       if (is_pm1(xZ)) return gcopy(y);
    1125      101012 :       z = idealHNF_mul_two(nf, y, mat_ideal_two_elt(nf,x));
    1126             :     }
    1127             :     else
    1128             :     {
    1129      169714 :       if (is_pm1(yZ)) return gcopy(x);
    1130      110877 :       z = idealHNF_mul_two(nf, x, mat_ideal_two_elt(nf,y));
    1131             :     }
    1132             :   }
    1133      889529 :   return z;
    1134             : }
    1135             : 
    1136             : /* operations on elements in factored form */
    1137             : 
    1138             : GEN
    1139      229866 : famat_mul_shallow(GEN f, GEN g)
    1140             : {
    1141      229866 :   if (typ(f) != t_MAT) f = to_famat_shallow(f,gen_1);
    1142      229866 :   if (typ(g) != t_MAT) g = to_famat_shallow(g,gen_1);
    1143      229866 :   if (lgcols(f) == 1) return g;
    1144      178473 :   if (lgcols(g) == 1) return f;
    1145      176845 :   return mkmat2(shallowconcat(gel(f,1), gel(g,1)),
    1146      176842 :                 shallowconcat(gel(f,2), gel(g,2)));
    1147             : }
    1148             : GEN
    1149       84105 : famat_mulpow_shallow(GEN f, GEN g, GEN e)
    1150             : {
    1151       84105 :   if (!signe(e)) return f;
    1152       56555 :   return famat_mul_shallow(f, famat_pow_shallow(g, e));
    1153             : }
    1154             : 
    1155             : GEN
    1156      145948 : famat_mulpows_shallow(GEN f, GEN g, long e)
    1157             : {
    1158      145948 :   if (e==0) return f;
    1159      122536 :   return famat_mul_shallow(f, famat_pows_shallow(g, e));
    1160             : }
    1161             : 
    1162             : GEN
    1163        9772 : famat_div_shallow(GEN f, GEN g)
    1164        9772 : { 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     1281781 : 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       16315 : append(GEN v, GEN x)
    1174             : {
    1175       16315 :   long i, l = lg(v);
    1176       16315 :   GEN w = cgetg(l+1, typ(v));
    1177      119174 :   for (i=1; i<l; i++) gel(w,i) = gcopy(gel(v,i));
    1178       16315 :   gel(w,i) = gcopy(x); return w;
    1179             : }
    1180             : /* add x^1 to famat f */
    1181             : static GEN
    1182       42178 : famat_add(GEN f, GEN x)
    1183             : {
    1184       42178 :   GEN h = cgetg(3,t_MAT);
    1185       42178 :   if (lgcols(f) == 1)
    1186             :   {
    1187       25947 :     gel(h,1) = mkcolcopy(x);
    1188       25947 :     gel(h,2) = mkcol(gen_1);
    1189             :   }
    1190             :   else
    1191             :   {
    1192       16231 :     gel(h,1) = append(gel(f,1), x);
    1193       16231 :     gel(h,2) = gconcat(gel(f,2), gen_1);
    1194             :   }
    1195       42178 :   return h;
    1196             : }
    1197             : /* add x^-1 to famat f */
    1198             : static GEN
    1199          84 : famat_sub(GEN f, GEN x)
    1200             : {
    1201          84 :   GEN h = cgetg(3,t_MAT);
    1202          84 :   if (lgcols(f) == 1)
    1203             :   {
    1204           0 :     gel(h,1) = mkcolcopy(x);
    1205           0 :     gel(h,2) = mkcol(gen_m1);
    1206             :   }
    1207             :   else
    1208             :   {
    1209          84 :     gel(h,1) = append(gel(f,1), x);
    1210          84 :     gel(h,2) = gconcat(gel(f,2), gen_m1);
    1211             :   }
    1212          84 :   return h;
    1213             : }
    1214             : 
    1215             : GEN
    1216       72636 : famat_mul(GEN f, GEN g)
    1217             : {
    1218             :   GEN h;
    1219       72636 :   if (typ(g) != t_MAT) {
    1220       42136 :     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       30500 :   if (typ(f) != t_MAT) return famat_add(g, f);
    1226       30458 :   if (lgcols(f) == 1) return gcopy(g);
    1227       10035 :   if (lgcols(g) == 1) return gcopy(f);
    1228        7129 :   h = cgetg(3,t_MAT);
    1229        7129 :   gel(h,1) = gconcat(gel(f,1), gel(g,1));
    1230        7129 :   gel(h,2) = gconcat(gel(f,2), gel(g,2));
    1231        7129 :   return h;
    1232             : }
    1233             : 
    1234             : GEN
    1235          91 : famat_div(GEN f, GEN g)
    1236             : {
    1237             :   GEN h;
    1238          91 :   if (typ(g) != t_MAT) {
    1239          42 :     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       14695 : famat_sqr(GEN f)
    1255             : {
    1256             :   GEN h;
    1257       14695 :   if (typ(f) != t_MAT) return to_famat(f,gen_2);
    1258       14695 :   if (lgcols(f) == 1) return gcopy(f);
    1259        8212 :   h = cgetg(3,t_MAT);
    1260        8212 :   gel(h,1) = gcopy(gel(f,1));
    1261        8212 :   gel(h,2) = gmul2n(gel(f,2),1);
    1262        8212 :   return h;
    1263             : }
    1264             : 
    1265             : GEN
    1266       26306 : famat_inv_shallow(GEN f)
    1267             : {
    1268       26306 :   if (typ(f) != t_MAT) return to_famat_shallow(f,gen_m1);
    1269        9919 :   if (lgcols(f) == 1) return f;
    1270        9919 :   return mkmat2(gel(f,1), ZC_neg(gel(f,2)));
    1271             : }
    1272             : GEN
    1273       18835 : famat_inv(GEN f)
    1274             : {
    1275       18835 :   if (typ(f) != t_MAT) return to_famat(f,gen_m1);
    1276       18835 :   if (lgcols(f) == 1) return gcopy(f);
    1277        1487 :   retmkmat2(gcopy(gel(f,1)), ZC_neg(gel(f,2)));
    1278             : }
    1279             : GEN
    1280          14 : famat_pow(GEN f, GEN n)
    1281             : {
    1282          14 :   if (typ(f) != t_MAT) return to_famat(f,n);
    1283          14 :   if (lgcols(f) == 1) return gcopy(f);
    1284          14 :   retmkmat2(gcopy(gel(f,1)), ZC_Z_mul(gel(f,2),n));
    1285             : }
    1286             : GEN
    1287       65597 : famat_pow_shallow(GEN f, GEN n)
    1288             : {
    1289       65597 :   if (is_pm1(n)) return signe(n) > 0? f: famat_inv_shallow(f);
    1290       35936 :   if (typ(f) != t_MAT) return to_famat_shallow(f,n);
    1291        9938 :   if (lgcols(f) == 1) return f;
    1292        6195 :   return mkmat2(gel(f,1), ZC_Z_mul(gel(f,2),n));
    1293             : }
    1294             : 
    1295             : GEN
    1296      149752 : famat_pows_shallow(GEN f, long n)
    1297             : {
    1298      149752 :   if (n==1) return f;
    1299       63266 :   if (n==-1) return famat_inv_shallow(f);
    1300       63252 :   if (typ(f) != t_MAT) return to_famat_shallow(f, stoi(n));
    1301       26659 :   if (lgcols(f) == 1) return f;
    1302       26661 :   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       25622 : ext_sqr(GEN nf, GEN x)
    1332       25622 : { return (typ(x)==t_MAT)? famat_sqr(x): nfsqr(nf, x); }
    1333             : static GEN
    1334       84409 : ext_mul(GEN nf, GEN x, GEN y)
    1335       84409 : { return (typ(x)==t_MAT)? famat_mul(x,y): nfmul(nf, x, y); }
    1336             : static GEN
    1337       18835 : ext_inv(GEN nf, GEN x)
    1338       18835 : { 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      135639 : famat_reduce(GEN fa)
    1369             : {
    1370             :   GEN E, G, L, g, e;
    1371             :   long i, k, l;
    1372             : 
    1373      135639 :   if (typ(fa) != t_MAT || lgcols(fa) == 1) return fa;
    1374      122721 :   g = gel(fa,1); l = lg(g);
    1375      122721 :   e = gel(fa,2);
    1376      122721 :   L = gen_indexsort(g, (void*)&cmp_universal, &cmp_nodata);
    1377      122719 :   G = cgetg(l, t_COL);
    1378      122717 :   E = cgetg(l, t_COL);
    1379             :   /* merge */
    1380     1764454 :   for (k=i=1; i<l; i++,k++)
    1381             :   {
    1382     1641730 :     gel(G,k) = gel(g,L[i]);
    1383     1641730 :     gel(E,k) = gel(e,L[i]);
    1384     1641730 :     if (k > 1 && gidentical(gel(G,k), gel(G,k-1)))
    1385             :     {
    1386     1002390 :       gel(E,k-1) = addii(gel(E,k), gel(E,k-1));
    1387     1002387 :       k--;
    1388             :     }
    1389             :   }
    1390             :   /* kill 0 exponents */
    1391      122724 :   l = k;
    1392      762079 :   for (k=i=1; i<l; i++)
    1393      639351 :     if (!gequal0(gel(E,i)))
    1394             :     {
    1395      629907 :       gel(G,k) = gel(G,i);
    1396      629907 :       gel(E,k) = gel(E,i); k++;
    1397             :     }
    1398      122728 :   setlg(G, k);
    1399      122722 :   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       14673 : famatsmall_reduce(GEN fa)
    1428             : {
    1429             :   GEN E, G, L, g, e;
    1430             :   long i, k, l;
    1431       14673 :   if (lgcols(fa) == 1) return fa;
    1432       14673 :   g = gel(fa,1); l = lg(g);
    1433       14673 :   e = gel(fa,2);
    1434       14673 :   L = vecsmall_indexsort(g);
    1435       14673 :   G = cgetg(l, t_VECSMALL);
    1436       14673 :   E = cgetg(l, t_VECSMALL);
    1437             :   /* merge */
    1438      131197 :   for (k=i=1; i<l; i++,k++)
    1439             :   {
    1440      116524 :     G[k] = g[L[i]];
    1441      116524 :     E[k] = e[L[i]];
    1442      116524 :     if (k > 1 && G[k] == G[k-1])
    1443             :     {
    1444        7073 :       E[k-1] += E[k];
    1445        7073 :       k--;
    1446             :     }
    1447             :   }
    1448             :   /* kill 0 exponents */
    1449       14673 :   l = k;
    1450      124124 :   for (k=i=1; i<l; i++)
    1451      109451 :     if (E[i])
    1452             :     {
    1453      105703 :       G[k] = G[i];
    1454      105703 :       E[k] = E[i]; k++;
    1455             :     }
    1456       14673 :   setlg(G, k);
    1457       14673 :   setlg(E, k); return mkmat2(G,E);
    1458             : }
    1459             : 
    1460             : GEN
    1461       53985 : famat_remove_trivial(GEN fa)
    1462             : {
    1463       53985 :   GEN P, E, p = gel(fa,1), e = gel(fa,2);
    1464       53985 :   long j, k, l = lg(p);
    1465       53985 :   P = cgetg(l, t_COL);
    1466       53984 :   E = cgetg(l, t_COL);
    1467     1632984 :   for (j = k = 1; j < l; j++)
    1468     1579000 :     if (signe(gel(e,j))) { gel(P,k) = gel(p,j); gel(E,k++) = gel(e,j); }
    1469       53984 :   setlg(P, k); setlg(E, k); return mkmat2(P,E);
    1470             : }
    1471             : 
    1472             : GEN
    1473       15048 : famatV_factorback(GEN v, GEN e)
    1474             : {
    1475       15048 :   long i, l = lg(e);
    1476             :   GEN V;
    1477       15048 :   if (l == 1) return trivial_fact();
    1478       14740 :   V = signe(gel(e,1))? famat_pow_shallow(gel(v,1), gel(e,1)): trivial_fact();
    1479       55634 :   for (i = 2; i < l; i++) V = famat_mulpow_shallow(V, gel(v,i), gel(e,i));
    1480       14740 :   return V;
    1481             : }
    1482             : 
    1483             : GEN
    1484       46438 : famatV_zv_factorback(GEN v, GEN e)
    1485             : {
    1486       46438 :   long i, l = lg(e);
    1487             :   GEN V;
    1488       46438 :   if (l == 1) return trivial_fact();
    1489       44058 :   V = uel(e,1)? famat_pows_shallow(gel(v,1), uel(e,1)): trivial_fact();
    1490      138306 :   for (i = 2; i < l; i++) V = famat_mulpows_shallow(V, gel(v,i), uel(e,i));
    1491       44058 :   return V;
    1492             : }
    1493             : 
    1494             : GEN
    1495       45199 : 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       45199 :   if (lgcols(fa) == 1) return fa;
    1502       45192 :   g = gel(fa,1); l = lg(g);
    1503       45192 :   e = gel(fa,2);
    1504       90776 :   for(n=0, i=1; i<l; i++)
    1505       45584 :     if (cmpii(gel(g,i),limit)<=0) n++;
    1506       45192 :   lG = n<l-1 ? n+2 : n+1;
    1507       45192 :   G = cgetg(lG, t_COL);
    1508       45192 :   E = cgetg(lG, t_COL);
    1509       45192 :   av = avma;
    1510       90776 :   for (i=1, k=1, r = gen_1; i<l; i++)
    1511             :   {
    1512       45584 :     if (cmpii(gel(g,i),limit)<=0)
    1513             :     {
    1514       45451 :       gel(G,k) = gel(g,i);
    1515       45451 :       gel(E,k) = gel(e,i);
    1516       45451 :       k++;
    1517         133 :     } else r = mulii(r, powii(gel(g,i), gel(e,i)));
    1518             :   }
    1519       45192 :   if (k<i)
    1520             :   {
    1521         133 :     gel(G, k) = gerepileuptoint(av, r);
    1522         133 :     gel(E, k) = gen_1;
    1523             :   }
    1524       45192 :   return mkmat2(G,E);
    1525             : }
    1526             : 
    1527             : /* assume pr has degree 1 and coprime to Q_denom(x) */
    1528             : static GEN
    1529      119453 : to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1530             : {
    1531      119453 :   GEN d, r, p = modpr_get_p(modpr);
    1532      119453 :   x = nf_to_scalar_or_basis(nf,x);
    1533      119453 :   if (typ(x) != t_COL) return Rg_to_Fp(x,p);
    1534      118886 :   x = Q_remove_denom(x, &d);
    1535      118886 :   r = zk_to_Fq(x, modpr);
    1536      118886 :   if (d) r = Fp_div(r, d, p);
    1537      118886 :   return r;
    1538             : }
    1539             : 
    1540             : /* pr coprime to all denominators occurring in x */
    1541             : static GEN
    1542         784 : famat_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1543             : {
    1544         784 :   GEN p = modpr_get_p(modpr);
    1545         784 :   GEN t = NULL, g = gel(x,1), e = gel(x,2), q = subiu(p,1);
    1546         784 :   long i, l = lg(g);
    1547        3423 :   for (i = 1; i < l; i++)
    1548             :   {
    1549        2639 :     GEN n = modii(gel(e,i), q);
    1550        2639 :     if (signe(n))
    1551             :     {
    1552        2625 :       GEN h = to_Fp_coprime(nf, gel(g,i), modpr);
    1553        2625 :       h = Fp_pow(h, n, p);
    1554        2625 :       t = t? Fp_mul(t, h, p): h;
    1555             :     }
    1556             :   }
    1557         784 :   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      117612 : nf_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1563             : {
    1564         784 :   return typ(x)==t_MAT? famat_to_Fp_coprime(nf, x, modpr)
    1565      118396 :                       : to_Fp_coprime(nf, x, modpr);
    1566             : }
    1567             : 
    1568             : static long
    1569     1185488 : zk_pvalrem(GEN x, GEN p, GEN *py)
    1570     1185488 : { 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     1281675 : nf_remove_denom_p(GEN nf, GEN x, GEN p, GEN *pdx, long *pv)
    1575             : {
    1576             :   long vcx;
    1577             :   GEN dx;
    1578     1281675 :   x = nf_to_scalar_or_basis(nf, x);
    1579     1281670 :   x = Q_remove_denom(x, &dx);
    1580     1281677 :   if (dx)
    1581             :   {
    1582      178014 :     vcx = - Z_pvalrem(dx, p, &dx);
    1583      178013 :     if (!vcx) vcx = zk_pvalrem(x, p, &x);
    1584      178013 :     if (isint1(dx)) dx = NULL;
    1585             :   }
    1586             :   else
    1587             :   {
    1588     1103663 :     vcx = zk_pvalrem(x, p, &x);
    1589     1103673 :     dx = NULL;
    1590             :   }
    1591     1281686 :   *pv = vcx;
    1592     1281686 :   *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       68832 : p_makecoprime(GEN pr)
    1598             : {
    1599       68832 :   GEN B = pr_get_tau(pr), b;
    1600             :   long i, e;
    1601             : 
    1602       68832 :   if (typ(B) == t_INT) return NULL;
    1603       68692 :   b = gel(B,1); /* B = multiplication table by b */
    1604       68692 :   e = pr_get_e(pr);
    1605       68692 :   if (e == 1) return b;
    1606             :   /* one could also divide (exactly) by p in each iteration */
    1607       39943 :   for (i = 1; i < e; i++) b = ZM_ZC_mul(B, b);
    1608       19509 :   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      560894 : famat_makecoprime(GEN nf, GEN g, GEN e, GEN pr, GEN prk, GEN EX)
    1624             : {
    1625      560894 :   GEN G, E, t, vp = NULL, p = pr_get_p(pr), prkZ = gcoeff(prk, 1,1);
    1626      560894 :   long i, l = lg(g);
    1627             : 
    1628      560894 :   G = cgetg(l+1, t_VEC);
    1629      560900 :   E = cgetg(l+1, t_VEC); /* l+1: room for "modified p" */
    1630     1842563 :   for (i=1; i < l; i++)
    1631             :   {
    1632             :     long vcx;
    1633     1281671 :     GEN dx, x = nf_remove_denom_p(nf, gel(g,i), p, &dx, &vcx);
    1634     1281684 :     if (vcx) /* = v_p(content(g[i])) */
    1635             :     {
    1636      113421 :       GEN a = mulsi(vcx, gel(e,i));
    1637      113416 :       vp = vp? addii(vp, a): a;
    1638             :     }
    1639             :     /* x integral, content coprime to p; dx coprime to p */
    1640     1281681 :     if (typ(x) == t_INT)
    1641             :     { /* x coprime to p, hence to pr */
    1642      165090 :       x = modii(x, prkZ);
    1643      165090 :       if (dx) x = Fp_div(x, dx, prkZ);
    1644             :     }
    1645             :     else
    1646             :     {
    1647     1116591 :       (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
    1648     1116565 :       x = ZC_hnfrem(FpC_red(x,prkZ), prk);
    1649     1116574 :       if (dx) x = FpC_Fp_mul(x, Fp_inv(dx,prkZ), prkZ);
    1650             :     }
    1651     1281658 :     gel(G,i) = x;
    1652     1281658 :     gel(E,i) = gel(e,i);
    1653             :   }
    1654             : 
    1655      560892 :   t = vp? p_makecoprime(pr): NULL;
    1656      560896 :   if (!t)
    1657             :   { /* no need for extra generator */
    1658      492225 :     setlg(G,l);
    1659      492225 :     setlg(E,l);
    1660             :   }
    1661             :   else
    1662             :   {
    1663       68671 :     gel(G,i) = FpC_red(t, prkZ);
    1664       68671 :     gel(E,i) = vp;
    1665             :   }
    1666      560898 :   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          42 : sunits_makecoprime(GEN X, GEN pr, GEN prk)
    1672             : {
    1673          42 :   GEN G, p = pr_get_p(pr), prkZ = gcoeff(prk,1,1);
    1674          42 :   long i, l = lg(X);
    1675             : 
    1676          42 :   G = cgetg(l, t_VEC);
    1677        3710 :   for (i = 1; i < l; i++)
    1678             :   {
    1679        3668 :     GEN x = gel(X,i);
    1680        3668 :     if (typ(x) == t_INT) /* a prime */
    1681         938 :       x = equalii(x,p)? p_makecoprime(pr): modii(x, prkZ);
    1682             :     else
    1683             :     {
    1684        2730 :       (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
    1685        2730 :       x = ZC_hnfrem(FpC_red(x,prkZ), prk);
    1686             :     }
    1687        3668 :     gel(G,i) = x;
    1688             :   }
    1689          42 :   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       19880 : famat_to_nf_moddivisor(GEN nf, GEN g, GEN e, GEN bid)
    1695             : {
    1696       19880 :   GEN t, cyc = bid_get_cyc(bid);
    1697       19880 :   if (lg(cyc) == 1)
    1698           0 :     t = gen_1;
    1699             :   else
    1700       19880 :     t = famat_to_nf_modideal_coprime(nf, g, e, bid_get_ideal(bid),
    1701             :                                      cyc_get_expo(cyc));
    1702       19880 :   return set_sign_mod_divisor(nf, mkmat2(g,e), t, bid_get_sarch(bid));
    1703             : }
    1704             : 
    1705             : GEN
    1706      766998 : vecmul(GEN x, GEN y)
    1707             : {
    1708      766998 :   if (is_scalar_t(typ(x))) return gmul(x,y);
    1709      766997 :   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      225042 : idealmulelt(GEN nf, GEN x, GEN A)
    1736             : {
    1737             :   long i, lx;
    1738             :   GEN dx, dA, D;
    1739      225042 :   if (lg(A) == 1) return cgetg(1, t_MAT);
    1740      225042 :   x = nf_to_scalar_or_basis(nf,x);
    1741      225042 :   if (typ(x) != t_COL)
    1742       89263 :     return isintzero(x)? cgetg(1,t_MAT): RgM_Rg_mul(A, Q_abs_shallow(x));
    1743      135779 :   x = Q_remove_denom(x, &dx);
    1744      135779 :   A = Q_remove_denom(A, &dA);
    1745      135779 :   x = zk_multable(nf, x);
    1746      135779 :   D = mulii(zkmultable_capZ(x), gcoeff(A,1,1));
    1747      135779 :   x = zkC_multable_mul(A, x);
    1748      135779 :   settyp(x, t_MAT); lx = lg(x);
    1749             :   /* x may contain scalars (at most 1 since the ideal is nonzero)*/
    1750      466628 :   for (i=1; i<lx; i++)
    1751      341475 :     if (typ(gel(x,i)) == t_INT)
    1752             :     {
    1753       10626 :       if (i > 1) swap(gel(x,1), gel(x,i)); /* help HNF */
    1754       10626 :       gel(x,1) = scalarcol_shallow(gel(x,1), lx-1);
    1755       10626 :       break;
    1756             :     }
    1757      135779 :   x = ZM_hnfmodid(x, D);
    1758      135779 :   dx = mul_denom(dx,dA);
    1759      135779 :   return dx? gdiv(x,dx): x;
    1760             : }
    1761             : 
    1762             : /* nf a true nf, tx <= ty */
    1763             : static GEN
    1764     2150049 : idealmul_aux(GEN nf, GEN x, GEN y, long tx, long ty)
    1765             : {
    1766             :   GEN z, cx, cy;
    1767     2150049 :   switch(tx)
    1768             :   {
    1769      276281 :     case id_PRINCIPAL:
    1770      276281 :       switch(ty)
    1771             :       {
    1772       50875 :         case id_PRINCIPAL:
    1773       50875 :           return idealhnf_principal(nf, nfmul(nf,x,y));
    1774         364 :         case id_PRIME:
    1775             :         {
    1776         364 :           GEN p = pr_get_p(y), pi = pr_get_gen(y), cx;
    1777         364 :           if (pr_is_inert(y)) return RgM_Rg_mul(idealhnf_principal(nf,x),p);
    1778             : 
    1779         210 :           x = nf_to_scalar_or_basis(nf, x);
    1780         210 :           switch(typ(x))
    1781             :           {
    1782         196 :             case t_INT:
    1783         196 :               if (!signe(x)) return cgetg(1,t_MAT);
    1784         196 :               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      225042 :         default: /* id_MAT */
    1796      225042 :           return idealmulelt(nf, x,y);
    1797             :       }
    1798     1596539 :     case id_PRIME:
    1799     1596539 :       if (ty==id_PRIME)
    1800     1592313 :       { y = pr_hnf(nf,y); cy = NULL; }
    1801             :       else
    1802        4226 :         y = Q_primitive_part(y, &cy);
    1803     1596539 :       y = idealHNF_mul_two(nf,y,x);
    1804     1596539 :       return cy? ZM_Q_mul(y,cy): y;
    1805             : 
    1806      277229 :     default: /* id_MAT */
    1807             :     {
    1808      277229 :       long N = nf_get_degree(nf);
    1809      277229 :       if (lg(x)-1 != N || lg(y)-1 != N) pari_err_DIM("idealmul");
    1810      277215 :       x = Q_primitive_part(x, &cx);
    1811      277215 :       y = Q_primitive_part(y, &cy); cx = mul_content(cx,cy);
    1812      277215 :       y = idealHNF_mul(nf,x,y);
    1813      277215 :       return cx? ZM_Q_mul(y,cx): y;
    1814             :     }
    1815             :   }
    1816             : }
    1817             : 
    1818             : /* output the ideal product x.y */
    1819             : GEN
    1820     2150048 : idealmul(GEN nf, GEN x, GEN y)
    1821             : {
    1822             :   pari_sp av;
    1823             :   GEN res, ax, ay, z;
    1824     2150048 :   long tx = idealtyp(&x,&ax);
    1825     2150049 :   long ty = idealtyp(&y,&ay), f;
    1826     2150049 :   if (tx>ty) { swap(ax,ay); swap(x,y); lswap(tx,ty); }
    1827     2150049 :   f = (ax||ay); res = f? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1828     2150049 :   av = avma;
    1829     2150049 :   z = gerepileupto(av, idealmul_aux(checknf(nf), x,y, tx,ty));
    1830     2150035 :   if (!f) return z;
    1831       23523 :   if (ax && ay)
    1832       22312 :     ax = ext_mul(nf, ax, ay);
    1833             :   else
    1834        1211 :     ax = gcopy(ax? ax: ay);
    1835       23523 :   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      213096 : idealsqrprime(GEN nf, GEN pr, GEN *pc)
    1842             : {
    1843      213096 :   GEN p = pr_get_p(pr), q, gen;
    1844      213096 :   long e = pr_get_e(pr), f = pr_get_f(pr);
    1845             : 
    1846      213097 :   q = (e == 1)? sqri(p): p;
    1847      213085 :   if (e <= 2 && e * f == nf_get_degree(nf))
    1848             :   { /* pr^e = (p) */
    1849       44026 :     *pc = q;
    1850       44026 :     return mkvec2(gen_1,gen_0);
    1851             :   }
    1852      169069 :   gen = nfsqr(nf, pr_get_gen(pr));
    1853      169076 :   gen = FpC_red(gen, q);
    1854      169061 :   *pc = NULL;
    1855      169061 :   return mkvec2(q, gen);
    1856             : }
    1857             : /* cf idealpow_aux */
    1858             : static GEN
    1859       30193 : idealsqr_aux(GEN nf, GEN x, long tx)
    1860             : {
    1861       30193 :   GEN T = nf_get_pol(nf), m, cx, a, alpha;
    1862       30193 :   long N = degpol(T);
    1863       30193 :   switch(tx)
    1864             :   {
    1865          70 :     case id_PRINCIPAL:
    1866          70 :       return idealhnf_principal(nf, nfsqr(nf,x));
    1867        9691 :     case id_PRIME:
    1868        9691 :       if (pr_is_inert(x)) return scalarmat(sqri(gel(x,1)), N);
    1869        9523 :       x = idealsqrprime(nf, x, &cx);
    1870        9523 :       x = idealhnf_two(nf,x);
    1871        9523 :       return cx? ZM_Z_mul(x, cx): x;
    1872       20432 :     default:
    1873       20432 :       x = Q_primitive_part(x, &cx);
    1874       20432 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    1875       20432 :       alpha = nfsqr(nf,alpha);
    1876       20432 :       m = zk_scalar_or_multable(nf, alpha);
    1877       20432 :       if (typ(m) == t_INT) {
    1878        1547 :         x = gcdii(sqri(a), m);
    1879        1547 :         if (cx) x = gmul(x, gsqr(cx));
    1880        1547 :         x = scalarmat(x, N);
    1881             :       }
    1882             :       else
    1883             :       { /* could use gcdii(sqri(a), zkmultable_capZ(m)), but costly */
    1884       18885 :         x = ZM_hnfmodid(m, sqri(a));
    1885       18885 :         if (cx) cx = gsqr(cx);
    1886       18885 :         if (cx) x = ZM_Q_mul(x, cx);
    1887             :       }
    1888       20432 :       return x;
    1889             :   }
    1890             : }
    1891             : GEN
    1892       30193 : idealsqr(GEN nf, GEN x)
    1893             : {
    1894             :   pari_sp av;
    1895             :   GEN res, ax, z;
    1896       30193 :   long tx = idealtyp(&x,&ax);
    1897       30193 :   res = ax? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1898       30193 :   av = avma;
    1899       30193 :   z = gerepileupto(av, idealsqr_aux(checknf(nf), x, tx));
    1900       30193 :   if (!ax) return z;
    1901       25622 :   gel(res,1) = z;
    1902       25622 :   gel(res,2) = ext_sqr(nf, ax); return res;
    1903             : }
    1904             : 
    1905             : /* norm of an ideal */
    1906             : GEN
    1907       11137 : idealnorm(GEN nf, GEN x)
    1908             : {
    1909             :   pari_sp av;
    1910             :   GEN y;
    1911             :   long tx;
    1912             : 
    1913       11137 :   switch(idealtyp(&x,&y))
    1914             :   {
    1915         245 :     case id_PRIME: return pr_norm(x);
    1916        8764 :     case id_MAT: return RgM_det_triangular(x);
    1917             :   }
    1918             :   /* id_PRINCIPAL */
    1919        2128 :   nf = checknf(nf); av = avma;
    1920        2128 :   x = nfnorm(nf, x);
    1921        2128 :   tx = typ(x);
    1922        2128 :   if (tx == t_INT) return gerepileuptoint(av, absi(x));
    1923         406 :   if (tx != t_FRAC) pari_err_TYPE("idealnorm",x);
    1924         406 :   return gerepileupto(av, Q_abs(x));
    1925             : }
    1926             : 
    1927             : /* x \cap Z */
    1928             : GEN
    1929        2975 : idealdown(GEN nf, GEN x)
    1930             : {
    1931        2975 :   pari_sp av = avma;
    1932             :   GEN y, c;
    1933        2975 :   switch(idealtyp(&x,&y))
    1934             :   {
    1935           7 :     case id_PRIME: return icopy(pr_get_p(x));
    1936        2100 :     case id_MAT: return gcopy(gcoeff(x,1,1));
    1937             :   }
    1938             :   /* id_PRINCIPAL */
    1939         868 :   nf = checknf(nf); av = avma;
    1940         868 :   x = nf_to_scalar_or_basis(nf, x);
    1941         868 :   if (is_rational_t(typ(x))) return Q_abs(x);
    1942          14 :   x = Q_primitive_part(x, &c);
    1943          14 :   y = zkmultable_capZ(zk_multable(nf, x));
    1944          14 :   return gerepilecopy(av, mul_content(c, y));
    1945             : }
    1946             : 
    1947             : /* true nf */
    1948             : static GEN
    1949          28 : idealismaximal_int(GEN nf, GEN p)
    1950             : {
    1951             :   GEN L;
    1952          28 :   if (!BPSW_psp(p)) return NULL;
    1953          56 :   if (!dvdii(nf_get_index(nf), p) &&
    1954          42 :       !FpX_is_irred(FpX_red(nf_get_pol(nf),p), p)) return NULL;
    1955          14 :   L = idealprimedec(nf, p);
    1956          14 :   return lg(L) == 2? gel(L,1): NULL;
    1957             : }
    1958             : /* true nf */
    1959             : static GEN
    1960           7 : idealismaximal_mat(GEN nf, GEN x)
    1961             : {
    1962             :   GEN p, c, L;
    1963             :   long i, l, f;
    1964           7 :   x = Q_primitive_part(x, &c);
    1965           7 :   p = gcoeff(x,1,1);
    1966           7 :   if (c)
    1967             :   {
    1968           0 :     if (typ(c) == t_FRAC || !equali1(p)) return NULL;
    1969           0 :     return idealismaximal_int(nf, p);
    1970             :   }
    1971           7 :   if (!BPSW_psp(p)) return NULL;
    1972           7 :   l = lg(x); f = 1;
    1973          21 :   for (i = 2; i < l; i++)
    1974             :   {
    1975          14 :     c = gcoeff(x,i,i);
    1976          14 :     if (equalii(c, p)) f++; else if (!equali1(c)) return NULL;
    1977             :   }
    1978           7 :   L = idealprimedec_limit_f(nf, p, f);
    1979          14 :   for (i = lg(L)-1; i; i--)
    1980             :   {
    1981          14 :     GEN pr = gel(L,i);
    1982          14 :     if (pr_get_f(pr) != f) break;
    1983          14 :     if (idealval(nf, x, pr) == 1) return pr;
    1984             :   }
    1985           0 :   return NULL;
    1986             : }
    1987             : /* true nf */
    1988             : static GEN
    1989          42 : idealismaximal_i(GEN nf, GEN x)
    1990             : {
    1991             :   GEN L, p, pr, c;
    1992             :   long i, l;
    1993          42 :   switch(idealtyp(&x,&c))
    1994             :   {
    1995           7 :     case id_PRIME: return x;
    1996           7 :     case id_MAT: return idealismaximal_mat(nf, x);
    1997             :   }
    1998             :   /* id_PRINCIPAL */
    1999          28 :   x = nf_to_scalar_or_basis(nf, x);
    2000          28 :   switch(typ(x))
    2001             :   {
    2002          28 :     case t_INT: return idealismaximal_int(nf, absi_shallow(x));
    2003           0 :     case t_FRAC: return NULL;
    2004             :   }
    2005           0 :   x = Q_primitive_part(x, &c);
    2006           0 :   if (c) return NULL;
    2007           0 :   p = zkmultable_capZ(zk_multable(nf, x));
    2008           0 :   L = idealprimedec(nf, p); l = lg(L); pr = NULL;
    2009           0 :   for (i = 1; i < l; i++)
    2010             :   {
    2011           0 :     long v = ZC_nfval(x, gel(L,i));
    2012           0 :     if (v > 1 || (v && pr)) return NULL;
    2013           0 :     pr = gel(L,i);
    2014             :   }
    2015           0 :   return pr;
    2016             : }
    2017             : GEN
    2018          42 : idealismaximal(GEN nf, GEN x)
    2019             : {
    2020          42 :   pari_sp av = avma;
    2021          42 :   x = idealismaximal_i(checknf(nf), x);
    2022          42 :   if (!x) { set_avma(av); return gen_0; }
    2023          28 :   return gerepilecopy(av, x);
    2024             : }
    2025             : 
    2026             : /* I^(-1) = { x \in K, Tr(x D^(-1) I) \in Z }, D different of K/Q
    2027             :  *
    2028             :  * nf[5][6] = pp( D^(-1) ) = pp( HNF( T^(-1) ) ), T = (Tr(wi wj))
    2029             :  * nf[5][7] = same in 2-elt form.
    2030             :  * Assume I integral. Return the integral ideal (I\cap Z) I^(-1) */
    2031             : GEN
    2032      193297 : idealHNF_inv_Z(GEN nf, GEN I)
    2033             : {
    2034      193297 :   GEN J, dual, IZ = gcoeff(I,1,1); /* I \cap Z */
    2035      193297 :   if (isint1(IZ)) return matid(lg(I)-1);
    2036      177750 :   J = idealHNF_mul(nf,I, gmael(nf,5,7));
    2037             :  /* I in HNF, hence easily inverted; multiply by IZ to get integer coeffs
    2038             :   * missing content cancels while solving the linear equation */
    2039      177749 :   dual = shallowtrans( hnf_divscale(J, gmael(nf,5,6), IZ) );
    2040      177750 :   return ZM_hnfmodid(dual, IZ);
    2041             : }
    2042             : /* I HNF with rational coefficients (denominator d). */
    2043             : GEN
    2044       66131 : idealHNF_inv(GEN nf, GEN I)
    2045             : {
    2046       66131 :   GEN J, IQ = gcoeff(I,1,1); /* I \cap Q; d IQ = dI \cap Z */
    2047       66131 :   J = idealHNF_inv_Z(nf, Q_remove_denom(I, NULL)); /* = (dI)^(-1) * (d IQ) */
    2048       66131 :   return equali1(IQ)? J: RgM_Rg_div(J, IQ);
    2049             : }
    2050             : 
    2051             : /* return p * P^(-1)  [integral] */
    2052             : GEN
    2053       36109 : pr_inv_p(GEN pr)
    2054             : {
    2055       36109 :   if (pr_is_inert(pr)) return matid(pr_get_f(pr));
    2056       35451 :   return ZM_hnfmodid(pr_get_tau(pr), pr_get_p(pr));
    2057             : }
    2058             : GEN
    2059       17069 : pr_inv(GEN pr)
    2060             : {
    2061       17069 :   GEN p = pr_get_p(pr);
    2062       17069 :   if (pr_is_inert(pr)) return scalarmat(ginv(p), pr_get_f(pr));
    2063       16691 :   return RgM_Rg_div(ZM_hnfmodid(pr_get_tau(pr),p), p);
    2064             : }
    2065             : 
    2066             : GEN
    2067      130576 : idealinv(GEN nf, GEN x)
    2068             : {
    2069             :   GEN res, ax;
    2070             :   pari_sp av;
    2071      130576 :   long tx = idealtyp(&x,&ax), N;
    2072             : 
    2073      130576 :   res = ax? cgetg(3,t_VEC): NULL;
    2074      130576 :   nf = checknf(nf); av = avma;
    2075      130576 :   N = nf_get_degree(nf);
    2076      130576 :   switch (tx)
    2077             :   {
    2078       60846 :     case id_MAT:
    2079       60846 :       if (lg(x)-1 != N) pari_err_DIM("idealinv");
    2080       60846 :       x = idealHNF_inv(nf,x); break;
    2081       53676 :     case id_PRINCIPAL:
    2082       53676 :       x = nf_to_scalar_or_basis(nf, x);
    2083       53676 :       if (typ(x) != t_COL)
    2084       53627 :         x = idealhnf_principal(nf,ginv(x));
    2085             :       else
    2086             :       { /* nfinv + idealhnf where we already know (x) \cap Z */
    2087             :         GEN c, d;
    2088          49 :         x = Q_remove_denom(x, &c);
    2089          49 :         x = zk_inv(nf, x);
    2090          49 :         x = Q_remove_denom(x, &d); /* true inverse is c/d * x */
    2091          49 :         if (!d) /* x and x^(-1) integral => x a unit */
    2092          14 :           x = c? scalarmat(c, N): matid(N);
    2093             :         else
    2094             :         {
    2095          35 :           c = c? gdiv(c,d): ginv(d);
    2096          35 :           x = zk_multable(nf, x);
    2097          35 :           x = ZM_Q_mul(ZM_hnfmodid(x,d), c);
    2098             :         }
    2099             :       }
    2100       53676 :       break;
    2101       16054 :     case id_PRIME:
    2102       16054 :       x = pr_inv(x); break;
    2103             :   }
    2104      130574 :   x = gerepileupto(av,x); if (!ax) return x;
    2105       18835 :   gel(res,1) = x;
    2106       18835 :   gel(res,2) = ext_inv(nf, ax); return res;
    2107             : }
    2108             : 
    2109             : /* write x = A/B, A,B coprime integral ideals */
    2110             : GEN
    2111      276432 : idealnumden(GEN nf, GEN x)
    2112             : {
    2113      276432 :   pari_sp av = avma;
    2114             :   GEN x0, ax, c, d, A, B, J;
    2115      276432 :   long tx = idealtyp(&x,&ax);
    2116      276430 :   nf = checknf(nf);
    2117      276433 :   switch (tx)
    2118             :   {
    2119           7 :     case id_PRIME:
    2120           7 :       retmkvec2(idealhnf(nf, x), gen_1);
    2121       50468 :     case id_PRINCIPAL:
    2122             :     {
    2123             :       GEN xZ, mx;
    2124       50468 :       x = nf_to_scalar_or_basis(nf, x);
    2125       50468 :       switch(typ(x))
    2126             :       {
    2127        2506 :         case t_INT: return gerepilecopy(av, mkvec2(absi_shallow(x),gen_1));
    2128          14 :         case t_FRAC:return gerepilecopy(av, mkvec2(absi_shallow(gel(x,1)), gel(x,2)));
    2129             :       }
    2130             :       /* t_COL */
    2131       47948 :       x = Q_remove_denom(x, &d);
    2132       47948 :       if (!d) return gerepilecopy(av, mkvec2(idealhnf_shallow(nf, x), gen_1));
    2133         180 :       mx = zk_multable(nf, x);
    2134         180 :       xZ = zkmultable_capZ(mx);
    2135         180 :       x = ZM_hnfmodid(mx, xZ); /* principal ideal (x) */
    2136         180 :       x0 = mkvec2(xZ, mx); /* same, for fast multiplication */
    2137         180 :       break;
    2138             :     }
    2139      225958 :     default: /* id_MAT */
    2140             :     {
    2141      225958 :       long n = lg(x)-1;
    2142      225958 :       if (n == 0) return mkvec2(gen_0, gen_1);
    2143      225958 :       if (n != nf_get_degree(nf)) pari_err_DIM("idealnumden");
    2144      225958 :       x0 = x = Q_remove_denom(x, &d);
    2145      225960 :       if (!d) return gerepilecopy(av, mkvec2(x, gen_1));
    2146          14 :       break;
    2147             :     }
    2148             :   }
    2149         194 :   J = hnfmodid(x, d); /* = d/B */
    2150         194 :   c = gcoeff(J,1,1); /* (d/B) \cap Z, divides d */
    2151         194 :   B = idealHNF_inv_Z(nf, J); /* (d/B \cap Z) B/d */
    2152         194 :   if (!equalii(c,d)) B = ZM_Z_mul(B, diviiexact(d,c)); /* = B ! */
    2153         194 :   A = idealHNF_mul(nf, B, x0); /* d * (original x) * B = d A */
    2154         194 :   A = ZM_Z_divexact(A, d); /* = A ! */
    2155         194 :   return gerepilecopy(av, mkvec2(A, B));
    2156             : }
    2157             : 
    2158             : /* Return x, integral in 2-elt form, such that pr^n = c * x. Assume n != 0.
    2159             :  * nf = true nf */
    2160             : static GEN
    2161     1031111 : idealpowprime(GEN nf, GEN pr, GEN n, GEN *pc)
    2162             : {
    2163     1031111 :   GEN p = pr_get_p(pr), q, gen;
    2164             : 
    2165     1031096 :   *pc = NULL;
    2166     1031096 :   if (is_pm1(n)) /* n = 1 special cased for efficiency */
    2167             :   {
    2168      546077 :     q = p;
    2169      546077 :     if (typ(pr_get_tau(pr)) == t_INT) /* inert */
    2170             :     {
    2171           0 :       *pc = (signe(n) >= 0)? p: ginv(p);
    2172           0 :       return mkvec2(gen_1,gen_0);
    2173             :     }
    2174      546075 :     if (signe(n) >= 0) gen = pr_get_gen(pr);
    2175             :     else
    2176             :     {
    2177      150843 :       gen = pr_get_tau(pr); /* possibly t_MAT */
    2178      150859 :       *pc = ginv(p);
    2179             :     }
    2180             :   }
    2181      485065 :   else if (equalis(n,2)) return idealsqrprime(nf, pr, pc);
    2182             :   else
    2183             :   {
    2184      281490 :     long e = pr_get_e(pr), f = pr_get_f(pr);
    2185      281501 :     GEN r, m = truedvmdis(n, e, &r);
    2186      281491 :     if (e * f == nf_get_degree(nf))
    2187             :     { /* pr^e = (p) */
    2188       82328 :       if (signe(m)) *pc = powii(p,m);
    2189       82328 :       if (!signe(r)) return mkvec2(gen_1,gen_0);
    2190       40312 :       q = p;
    2191       40312 :       gen = nfpow(nf, pr_get_gen(pr), r);
    2192             :     }
    2193             :     else
    2194             :     {
    2195      199176 :       m = absi_shallow(m);
    2196      199176 :       if (signe(r)) m = addiu(m,1);
    2197      199176 :       q = powii(p,m); /* m = ceil(|n|/e) */
    2198      199176 :       if (signe(n) >= 0) gen = nfpow(nf, pr_get_gen(pr), n);
    2199             :       else
    2200             :       {
    2201       23757 :         gen = pr_get_tau(pr);
    2202       23758 :         if (typ(gen) == t_MAT) gen = gel(gen,1);
    2203       23758 :         n = negi(n);
    2204       23758 :         gen = ZC_Z_divexact(nfpow(nf, gen, n), powii(p, subii(n,m)));
    2205       23758 :         *pc = ginv(q);
    2206             :       }
    2207             :     }
    2208      239489 :     gen = FpC_red(gen, q);
    2209             :   }
    2210      785561 :   return mkvec2(q, gen);
    2211             : }
    2212             : 
    2213             : /* True nf. x * pr^n. Assume x in HNF or scalar (possibly nonintegral) */
    2214             : GEN
    2215      647223 : idealmulpowprime(GEN nf, GEN x, GEN pr, GEN n)
    2216             : {
    2217             :   GEN c, cx, y;
    2218      647223 :   long N = nf_get_degree(nf);
    2219             : 
    2220      647252 :   if (!signe(n)) return typ(x) == t_MAT? x: scalarmat_shallow(x, N);
    2221             : 
    2222             :   /* inert, special cased for efficiency */
    2223      647245 :   if (pr_is_inert(pr))
    2224             :   {
    2225       64206 :     GEN q = powii(pr_get_p(pr), n);
    2226       62326 :     return typ(x) == t_MAT? RgM_Rg_mul(x,q)
    2227      126524 :                           : scalarmat_shallow(gmul(Q_abs(x),q), N);
    2228             :   }
    2229             : 
    2230      583018 :   y = idealpowprime(nf, pr, n, &c);
    2231      582987 :   if (typ(x) == t_MAT)
    2232      580446 :   { x = Q_primitive_part(x, &cx); if (is_pm1(gcoeff(x,1,1))) x = NULL; }
    2233             :   else
    2234        2541 :   { cx = x; x = NULL; }
    2235      582950 :   cx = mul_content(c,cx);
    2236      582948 :   if (x)
    2237      423726 :     x = idealHNF_mul_two(nf,x,y);
    2238             :   else
    2239      159222 :     x = idealhnf_two(nf,y);
    2240      583125 :   if (cx) x = ZM_Q_mul(x,cx);
    2241      582792 :   return x;
    2242             : }
    2243             : GEN
    2244        4564 : idealdivpowprime(GEN nf, GEN x, GEN pr, GEN n)
    2245             : {
    2246        4564 :   return idealmulpowprime(nf,x,pr, negi(n));
    2247             : }
    2248             : 
    2249             : /* nf = true nf */
    2250             : static GEN
    2251      692677 : idealpow_aux(GEN nf, GEN x, long tx, GEN n)
    2252             : {
    2253      692677 :   GEN T = nf_get_pol(nf), m, cx, n1, a, alpha;
    2254      692676 :   long N = degpol(T), s = signe(n);
    2255      692676 :   if (!s) return matid(N);
    2256      680839 :   switch(tx)
    2257             :   {
    2258       25963 :     case id_PRINCIPAL:
    2259       25963 :       return idealhnf_principal(nf, nfpow(nf,x,n));
    2260      533451 :     case id_PRIME:
    2261      533451 :       if (pr_is_inert(x)) return scalarmat(powii(gel(x,1), n), N);
    2262      448084 :       x = idealpowprime(nf, x, n, &cx);
    2263      448081 :       x = idealhnf_two(nf,x);
    2264      448093 :       return cx? ZM_Q_mul(x, cx): x;
    2265      121425 :     default:
    2266      121425 :       if (is_pm1(n)) return (s < 0)? idealinv(nf, x): gcopy(x);
    2267       64760 :       n1 = (s < 0)? negi(n): n;
    2268             : 
    2269       64760 :       x = Q_primitive_part(x, &cx);
    2270       64760 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    2271       64760 :       alpha = nfpow(nf,alpha,n1);
    2272       64760 :       m = zk_scalar_or_multable(nf, alpha);
    2273       64760 :       if (typ(m) == t_INT) {
    2274         637 :         x = gcdii(powii(a,n1), m);
    2275         637 :         if (s<0) x = ginv(x);
    2276         637 :         if (cx) x = gmul(x, powgi(cx,n));
    2277         637 :         x = scalarmat(x, N);
    2278             :       }
    2279             :       else
    2280             :       { /* could use gcdii(powii(a,n1), zkmultable_capZ(m)), but costly */
    2281       64123 :         x = ZM_hnfmodid(m, powii(a,n1));
    2282       64123 :         if (cx) cx = powgi(cx,n);
    2283       64123 :         if (s<0) {
    2284           7 :           GEN xZ = gcoeff(x,1,1);
    2285           7 :           cx = cx ? gdiv(cx, xZ): ginv(xZ);
    2286           7 :           x = idealHNF_inv_Z(nf,x);
    2287             :         }
    2288       64123 :         if (cx) x = ZM_Q_mul(x, cx);
    2289             :       }
    2290       64760 :       return x;
    2291             :   }
    2292             : }
    2293             : 
    2294             : /* raise the ideal x to the power n (in Z) */
    2295             : GEN
    2296      692681 : idealpow(GEN nf, GEN x, GEN n)
    2297             : {
    2298             :   pari_sp av;
    2299             :   long tx;
    2300             :   GEN res, ax;
    2301             : 
    2302      692681 :   if (typ(n) != t_INT) pari_err_TYPE("idealpow",n);
    2303      692681 :   tx = idealtyp(&x,&ax);
    2304      692681 :   res = ax? cgetg(3,t_VEC): NULL;
    2305      692681 :   av = avma;
    2306      692681 :   x = gerepileupto(av, idealpow_aux(checknf(nf), x, tx, n));
    2307      692670 :   if (!ax) return x;
    2308           0 :   ax = ext_pow(nf, ax, n);
    2309           0 :   gel(res,1) = x;
    2310           0 :   gel(res,2) = ax;
    2311           0 :   return res;
    2312             : }
    2313             : 
    2314             : /* Return ideal^e in number field nf. e is a C integer. */
    2315             : GEN
    2316      246786 : idealpows(GEN nf, GEN ideal, long e)
    2317             : {
    2318      246786 :   long court[] = {evaltyp(t_INT) | _evallg(3),0,0};
    2319      246786 :   affsi(e,court); return idealpow(nf,ideal,court);
    2320             : }
    2321             : 
    2322             : static GEN
    2323       24041 : _idealmulred(GEN nf, GEN x, GEN y)
    2324       24041 : { return idealred(nf,idealmul(nf,x,y)); }
    2325             : static GEN
    2326       27344 : _idealsqrred(GEN nf, GEN x)
    2327       27344 : { return idealred(nf,idealsqr(nf,x)); }
    2328             : static GEN
    2329        7927 : _mul(void *data, GEN x, GEN y) { return _idealmulred((GEN)data,x,y); }
    2330             : static GEN
    2331       27344 : _sqr(void *data, GEN x) { return _idealsqrred((GEN)data, x); }
    2332             : 
    2333             : /* compute x^n (x ideal, n integer), reducing along the way */
    2334             : GEN
    2335       75354 : idealpowred(GEN nf, GEN x, GEN n)
    2336             : {
    2337       75354 :   pari_sp av = avma, av2;
    2338             :   long s;
    2339             :   GEN y;
    2340             : 
    2341       75354 :   if (typ(n) != t_INT) pari_err_TYPE("idealpowred",n);
    2342       75354 :   s = signe(n); if (s == 0) return idealpow(nf,x,n);
    2343       75354 :   y = gen_pow_i(x, n, (void*)nf, &_sqr, &_mul);
    2344       75354 :   av2 = avma;
    2345       75354 :   if (s < 0) y = idealinv(nf,y);
    2346       75353 :   if (s < 0 || is_pm1(n)) y = idealred(nf,y);
    2347       75356 :   return avma == av2? gerepilecopy(av,y): gerepileupto(av,y);
    2348             : }
    2349             : 
    2350             : GEN
    2351       16114 : idealmulred(GEN nf, GEN x, GEN y)
    2352             : {
    2353       16114 :   pari_sp av = avma;
    2354       16114 :   return gerepileupto(av, _idealmulred(nf,x,y));
    2355             : }
    2356             : 
    2357             : long
    2358          91 : isideal(GEN nf,GEN x)
    2359             : {
    2360          91 :   long N, i, j, lx, tx = typ(x);
    2361             :   pari_sp av;
    2362             :   GEN T, xZ;
    2363             : 
    2364          91 :   nf = checknf(nf); T = nf_get_pol(nf); lx = lg(x);
    2365          91 :   if (tx==t_VEC && lx==3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
    2366          91 :   switch(tx)
    2367             :   {
    2368          14 :     case t_INT: case t_FRAC: return 1;
    2369           7 :     case t_POL: return varn(x) == varn(T);
    2370           7 :     case t_POLMOD: return RgX_equal_var(T, gel(x,1));
    2371          14 :     case t_VEC: return get_prid(x)? 1 : 0;
    2372          42 :     case t_MAT: break;
    2373           7 :     default: return 0;
    2374             :   }
    2375          42 :   N = degpol(T);
    2376          42 :   if (lx-1 != N) return (lx == 1);
    2377          28 :   if (nbrows(x) != N) return 0;
    2378             : 
    2379          28 :   av = avma; x = Q_primpart(x);
    2380          28 :   if (!ZM_ishnf(x)) return 0;
    2381          14 :   xZ = gcoeff(x,1,1);
    2382          21 :   for (j=2; j<=N; j++)
    2383          14 :     if (!dvdii(xZ, gcoeff(x,j,j))) return gc_long(av,0);
    2384          14 :   for (i=2; i<=N; i++)
    2385          14 :     for (j=2; j<=N; j++)
    2386           7 :        if (! hnf_invimage(x, zk_ei_mul(nf,gel(x,i),j))) return gc_long(av,0);
    2387           7 :   return gc_long(av,1);
    2388             : }
    2389             : 
    2390             : GEN
    2391       36897 : idealdiv(GEN nf, GEN x, GEN y)
    2392             : {
    2393       36897 :   pari_sp av = avma, tetpil;
    2394       36897 :   GEN z = idealinv(nf,y);
    2395       36897 :   tetpil = avma; return gerepile(av,tetpil, idealmul(nf,x,z));
    2396             : }
    2397             : 
    2398             : /* This routine computes the quotient x/y of two ideals in the number field nf.
    2399             :  * It assumes that the quotient is an integral ideal.  The idea is to find an
    2400             :  * ideal z dividing y such that gcd(Nx/Nz, Nz) = 1.  Then
    2401             :  *
    2402             :  *   x + (Nx/Nz)    x
    2403             :  *   ----------- = ---
    2404             :  *   y + (Ny/Nz)    y
    2405             :  *
    2406             :  * Proof: we can assume x and y are integral. Let p be any prime ideal
    2407             :  *
    2408             :  * If p | Nz, then it divides neither Nx/Nz nor Ny/Nz (since Nx/Nz is the
    2409             :  * product of the integers N(x/y) and N(y/z)).  Both the numerator and the
    2410             :  * denominator on the left will be coprime to p.  So will x/y, since x/y is
    2411             :  * assumed integral and its norm N(x/y) is coprime to p.
    2412             :  *
    2413             :  * If instead p does not divide Nz, then v_p (Nx/Nz) = v_p (Nx) >= v_p(x).
    2414             :  * Hence v_p (x + Nx/Nz) = v_p(x).  Likewise for the denominators.  QED.
    2415             :  *
    2416             :  *                Peter Montgomery.  July, 1994. */
    2417             : static void
    2418           7 : err_divexact(GEN x, GEN y)
    2419           7 : { pari_err_DOMAIN("idealdivexact","denominator(x/y)", "!=",
    2420           0 :                   gen_1,mkvec2(x,y)); }
    2421             : GEN
    2422        3955 : idealdivexact(GEN nf, GEN x0, GEN y0)
    2423             : {
    2424        3955 :   pari_sp av = avma;
    2425             :   GEN x, y, xZ, yZ, Nx, Ny, Nz, cy, q, r;
    2426             : 
    2427        3955 :   nf = checknf(nf);
    2428        3955 :   x = idealhnf_shallow(nf, x0);
    2429        3955 :   y = idealhnf_shallow(nf, y0);
    2430        3955 :   if (lg(y) == 1) pari_err_INV("idealdivexact", y0);
    2431        3948 :   if (lg(x) == 1) { set_avma(av); return cgetg(1, t_MAT); } /* numerator is zero */
    2432        3948 :   y = Q_primitive_part(y, &cy);
    2433        3948 :   if (cy) x = RgM_Rg_div(x,cy);
    2434        3948 :   xZ = gcoeff(x,1,1); if (typ(xZ) != t_INT) err_divexact(x,y);
    2435        3941 :   yZ = gcoeff(y,1,1); if (isint1(yZ)) return gerepilecopy(av, x);
    2436        2877 :   Nx = idealnorm(nf,x);
    2437        2877 :   Ny = idealnorm(nf,y);
    2438        2877 :   if (typ(Nx) != t_INT) err_divexact(x,y);
    2439        2877 :   q = dvmdii(Nx,Ny, &r);
    2440        2877 :   if (signe(r)) err_divexact(x,y);
    2441        2877 :   if (is_pm1(q)) { set_avma(av); return matid(nf_get_degree(nf)); }
    2442             :   /* Find a norm Nz | Ny such that gcd(Nx/Nz, Nz) = 1 */
    2443         714 :   for (Nz = Ny;;) /* q = Nx/Nz */
    2444         714 :   {
    2445        1428 :     GEN p1 = gcdii(Nz, q);
    2446        1428 :     if (is_pm1(p1)) break;
    2447         714 :     Nz = diviiexact(Nz,p1);
    2448         714 :     q = mulii(q,p1);
    2449             :   }
    2450         714 :   xZ = gcoeff(x,1,1); q = gcdii(q, xZ);
    2451         714 :   if (!equalii(xZ,q))
    2452             :   { /* Replace x/y  by  x+(Nx/Nz) / y+(Ny/Nz) */
    2453         378 :     x = ZM_hnfmodid(x, q);
    2454             :     /* y reduced to unit ideal ? */
    2455         378 :     if (Nz == Ny) return gerepileupto(av, x);
    2456             : 
    2457         140 :     yZ = gcoeff(y,1,1); q = gcdii(diviiexact(Ny,Nz), yZ);
    2458         140 :     y = ZM_hnfmodid(y, q);
    2459             :   }
    2460         476 :   yZ = gcoeff(y,1,1);
    2461         476 :   y = idealHNF_mul(nf,x, idealHNF_inv_Z(nf,y));
    2462         476 :   return gerepileupto(av, ZM_Z_divexact(y, yZ));
    2463             : }
    2464             : 
    2465             : GEN
    2466          21 : idealintersect(GEN nf, GEN x, GEN y)
    2467             : {
    2468          21 :   pari_sp av = avma;
    2469             :   long lz, lx, i;
    2470             :   GEN z, dx, dy, xZ, yZ;;
    2471             : 
    2472          21 :   nf = checknf(nf);
    2473          21 :   x = idealhnf_shallow(nf,x);
    2474          21 :   y = idealhnf_shallow(nf,y);
    2475          21 :   if (lg(x) == 1 || lg(y) == 1) { set_avma(av); return cgetg(1,t_MAT); }
    2476          14 :   x = Q_remove_denom(x, &dx);
    2477          14 :   y = Q_remove_denom(y, &dy);
    2478          14 :   if (dx) y = ZM_Z_mul(y, dx);
    2479          14 :   if (dy) x = ZM_Z_mul(x, dy);
    2480          14 :   xZ = gcoeff(x,1,1);
    2481          14 :   yZ = gcoeff(y,1,1);
    2482          14 :   dx = mul_denom(dx,dy);
    2483          14 :   z = ZM_lll(shallowconcat(x,y), 0.99, LLL_KER); lz = lg(z);
    2484          14 :   lx = lg(x);
    2485          63 :   for (i=1; i<lz; i++) setlg(z[i], lx);
    2486          14 :   z = ZM_hnfmodid(ZM_mul(x,z), lcmii(xZ, yZ));
    2487          14 :   if (dx) z = RgM_Rg_div(z,dx);
    2488          14 :   return gerepileupto(av,z);
    2489             : }
    2490             : 
    2491             : /*******************************************************************/
    2492             : /*                                                                 */
    2493             : /*                      T2-IDEAL REDUCTION                         */
    2494             : /*                                                                 */
    2495             : /*******************************************************************/
    2496             : 
    2497             : static GEN
    2498          21 : chk_vdir(GEN nf, GEN vdir)
    2499             : {
    2500          21 :   long i, l = lg(vdir);
    2501             :   GEN v;
    2502          21 :   if (l != lg(nf_get_roots(nf))) pari_err_DIM("idealred");
    2503          14 :   switch(typ(vdir))
    2504             :   {
    2505           0 :     case t_VECSMALL: return vdir;
    2506          14 :     case t_VEC: break;
    2507           0 :     default: pari_err_TYPE("idealred",vdir);
    2508             :   }
    2509          14 :   v = cgetg(l, t_VECSMALL);
    2510          56 :   for (i = 1; i < l; i++) v[i] = itos(gceil(gel(vdir,i)));
    2511          14 :   return v;
    2512             : }
    2513             : 
    2514             : static void
    2515       12471 : twistG(GEN G, long r1, long i, long v)
    2516             : {
    2517       12471 :   long j, lG = lg(G);
    2518       12471 :   if (i <= r1) {
    2519       37340 :     for (j=1; j<lG; j++) gcoeff(G,i,j) = gmul2n(gcoeff(G,i,j), v);
    2520             :   } else {
    2521         392 :     long k = (i<<1) - r1;
    2522        2142 :     for (j=1; j<lG; j++)
    2523             :     {
    2524        1750 :       gcoeff(G,k-1,j) = gmul2n(gcoeff(G,k-1,j), v);
    2525        1750 :       gcoeff(G,k  ,j) = gmul2n(gcoeff(G,k  ,j), v);
    2526             :     }
    2527             :   }
    2528       12471 : }
    2529             : 
    2530             : GEN
    2531      121850 : nf_get_Gtwist(GEN nf, GEN vdir)
    2532             : {
    2533             :   long i, l, v, r1;
    2534             :   GEN G;
    2535             : 
    2536      121850 :   if (!vdir) return nf_get_roundG(nf);
    2537          21 :   if (typ(vdir) == t_MAT)
    2538             :   {
    2539           0 :     long N = nf_get_degree(nf);
    2540           0 :     if (lg(vdir) != N+1 || lgcols(vdir) != N+1) pari_err_DIM("idealred");
    2541           0 :     return vdir;
    2542             :   }
    2543          21 :   vdir = chk_vdir(nf, vdir);
    2544          14 :   G = RgM_shallowcopy(nf_get_G(nf));
    2545          14 :   r1 = nf_get_r1(nf);
    2546          14 :   l = lg(vdir);
    2547          56 :   for (i=1; i<l; i++)
    2548             :   {
    2549          42 :     v = vdir[i]; if (!v) continue;
    2550          42 :     twistG(G, r1, i, v);
    2551             :   }
    2552          14 :   return RM_round_maxrank(G);
    2553             : }
    2554             : GEN
    2555       12429 : nf_get_Gtwist1(GEN nf, long i)
    2556             : {
    2557       12429 :   GEN G = RgM_shallowcopy( nf_get_G(nf) );
    2558       12429 :   long r1 = nf_get_r1(nf);
    2559       12429 :   twistG(G, r1, i, 10);
    2560       12429 :   return RM_round_maxrank(G);
    2561             : }
    2562             : 
    2563             : GEN
    2564       93958 : RM_round_maxrank(GEN G0)
    2565             : {
    2566       93958 :   long e, r = lg(G0)-1;
    2567       93958 :   pari_sp av = avma;
    2568       93958 :   for (e = 4; ; e <<= 1, set_avma(av))
    2569           0 :   {
    2570       93958 :     GEN G = gmul2n(G0, e), H = ground(G);
    2571       93960 :     if (ZM_rank(H) == r) return H; /* maximal rank ? */
    2572             :   }
    2573             : }
    2574             : 
    2575             : GEN
    2576      121843 : idealred0(GEN nf, GEN I, GEN vdir)
    2577             : {
    2578      121843 :   pari_sp av = avma;
    2579      121843 :   GEN G, aI, IZ, J, y, yZ, my, c1 = NULL;
    2580             :   long N;
    2581             : 
    2582      121843 :   nf = checknf(nf);
    2583      121843 :   N = nf_get_degree(nf);
    2584             :   /* put first for sanity checks, unused when I obviously principal */
    2585      121843 :   G = nf_get_Gtwist(nf, vdir);
    2586      121836 :   switch (idealtyp(&I,&aI))
    2587             :   {
    2588       34391 :     case id_PRIME:
    2589       34391 :       if (pr_is_inert(I)) {
    2590         585 :         if (!aI) { set_avma(av); return matid(N); }
    2591         585 :         c1 = gel(I,1); I = matid(N);
    2592         585 :         goto END;
    2593             :       }
    2594       33806 :       IZ = pr_get_p(I);
    2595       33806 :       J = pr_inv_p(I);
    2596       33808 :       I = idealhnf_two(nf,I);
    2597       33807 :       break;
    2598       87417 :     case id_MAT:
    2599       87417 :       I = Q_primitive_part(I, &c1);
    2600       87418 :       IZ = gcoeff(I,1,1);
    2601       87418 :       if (is_pm1(IZ))
    2602             :       {
    2603        8561 :         if (!aI) { set_avma(av); return matid(N); }
    2604        8519 :         goto END;
    2605             :       }
    2606       78857 :       J = idealHNF_inv_Z(nf, I);
    2607       78857 :       break;
    2608          21 :     default: /* id_PRINCIPAL, silly case */
    2609          21 :       if (gequal0(I)) I = cgetg(1,t_MAT); else { c1 = I; I = matid(N); }
    2610          21 :       if (!aI) return I;
    2611          14 :       goto END;
    2612             :   }
    2613             :   /* now I integral, HNF; and J = (I\cap Z) I^(-1), integral */
    2614      112664 :   y = idealpseudomin(J, G); /* small elt in (I\cap Z)I^(-1), integral */
    2615      112665 :   if (equalii(ZV_content(y), IZ))
    2616             :   { /* already reduced */
    2617       64335 :     if (!aI) return gerepilecopy(av, I);
    2618       60968 :     goto END;
    2619             :   }
    2620             : 
    2621       48330 :   my = zk_multable(nf, y);
    2622       48330 :   I = ZM_Z_divexact(ZM_mul(my, I), IZ); /* y I / (I\cap Z), integral */
    2623       48328 :   c1 = mul_content(c1, IZ);
    2624       48328 :   my = ZM_gauss(my, col_ei(N,1)); /* y^-1 */
    2625       48330 :   yZ = Q_denom(my); /* (y) \cap Z */
    2626       48330 :   I = hnfmodid(I, yZ);
    2627       48330 :   if (!aI) return gerepileupto(av, I);
    2628       45597 :   c1 = RgC_Rg_mul(my, c1);
    2629      115683 : END:
    2630      115683 :   if (c1) aI = ext_mul(nf, aI,c1);
    2631      115681 :   return gerepilecopy(av, mkvec2(I, aI));
    2632             : }
    2633             : 
    2634             : GEN
    2635           7 : idealmin(GEN nf, GEN x, GEN vdir)
    2636             : {
    2637           7 :   pari_sp av = avma;
    2638             :   GEN y, dx;
    2639           7 :   nf = checknf(nf);
    2640           7 :   switch( idealtyp(&x,&y) )
    2641             :   {
    2642           0 :     case id_PRINCIPAL: return gcopy(x);
    2643           0 :     case id_PRIME: x = pr_hnf(nf,x); break;
    2644           7 :     case id_MAT: if (lg(x) == 1) return gen_0;
    2645             :   }
    2646           7 :   x = Q_remove_denom(x, &dx);
    2647           7 :   y = idealpseudomin(x, nf_get_Gtwist(nf,vdir));
    2648           7 :   if (dx) y = RgC_Rg_div(y, dx);
    2649           7 :   return gerepileupto(av, y);
    2650             : }
    2651             : 
    2652             : /*******************************************************************/
    2653             : /*                                                                 */
    2654             : /*                   APPROXIMATION THEOREM                         */
    2655             : /*                                                                 */
    2656             : /*******************************************************************/
    2657             : /* a = ppi(a,b) ppo(a,b), where ppi regroups primes common to a and b
    2658             :  * and ppo(a,b) = Z_ppo(a,b) */
    2659             : /* return gcd(a,b),ppi(a,b),ppo(a,b) */
    2660             : GEN
    2661      455357 : Z_ppio(GEN a, GEN b)
    2662             : {
    2663      455357 :   GEN x, y, d = gcdii(a,b);
    2664      455357 :   if (is_pm1(d)) return mkvec3(gen_1, gen_1, a);
    2665      346353 :   x = d; y = diviiexact(a,d);
    2666             :   for(;;)
    2667       62965 :   {
    2668      409318 :     GEN g = gcdii(x,y);
    2669      409318 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2670       62965 :     x = mulii(x,g); y = diviiexact(y,g);
    2671             :   }
    2672             : }
    2673             : /* a = ppg(a,b)pple(a,b), where ppg regroups primes such that v(a) > v(b)
    2674             :  * and pple all others */
    2675             : /* return gcd(a,b),ppg(a,b),pple(a,b) */
    2676             : GEN
    2677           0 : Z_ppgle(GEN a, GEN b)
    2678             : {
    2679           0 :   GEN x, y, g, d = gcdii(a,b);
    2680           0 :   if (equalii(a, d)) return mkvec3(a, gen_1, a);
    2681           0 :   x = diviiexact(a,d); y = d;
    2682             :   for(;;)
    2683             :   {
    2684           0 :     g = gcdii(x,y);
    2685           0 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2686           0 :     x = mulii(x,g); y = diviiexact(y,g);
    2687             :   }
    2688             : }
    2689             : static void
    2690           0 : Z_dcba_rec(GEN L, GEN a, GEN b)
    2691             : {
    2692             :   GEN x, r, v, g, h, c, c0;
    2693             :   long n;
    2694           0 :   if (is_pm1(b)) {
    2695           0 :     if (!is_pm1(a)) vectrunc_append(L, a);
    2696           0 :     return;
    2697             :   }
    2698           0 :   v = Z_ppio(a,b);
    2699           0 :   a = gel(v,2);
    2700           0 :   r = gel(v,3);
    2701           0 :   if (!is_pm1(r)) vectrunc_append(L, r);
    2702           0 :   v = Z_ppgle(a,b);
    2703           0 :   g = gel(v,1);
    2704           0 :   h = gel(v,2);
    2705           0 :   x = c0 = gel(v,3);
    2706           0 :   for (n = 1; !is_pm1(h); n++)
    2707             :   {
    2708             :     GEN d, y;
    2709             :     long i;
    2710           0 :     v = Z_ppgle(h,sqri(g));
    2711           0 :     g = gel(v,1);
    2712           0 :     h = gel(v,2);
    2713           0 :     c = gel(v,3); if (is_pm1(c)) continue;
    2714           0 :     d = gcdii(c,b);
    2715           0 :     x = mulii(x,d);
    2716           0 :     y = d; for (i=1; i < n; i++) y = sqri(y);
    2717           0 :     Z_dcba_rec(L, diviiexact(c,y), d);
    2718             :   }
    2719           0 :   Z_dcba_rec(L,diviiexact(b,x), c0);
    2720             : }
    2721             : static GEN
    2722     3082737 : Z_cba_rec(GEN L, GEN a, GEN b)
    2723             : {
    2724             :   GEN g;
    2725     3082737 :   if (lg(L) > 10)
    2726             :   { /* a few naive steps before switching to dcba */
    2727           0 :     Z_dcba_rec(L, a, b);
    2728           0 :     return gel(L, lg(L)-1);
    2729             :   }
    2730     3082737 :   if (is_pm1(a)) return b;
    2731     1831711 :   g = gcdii(a,b);
    2732     1831711 :   if (is_pm1(g)) { vectrunc_append(L, a); return b; }
    2733     1368297 :   a = diviiexact(a,g);
    2734     1368297 :   b = diviiexact(b,g);
    2735     1368297 :   return Z_cba_rec(L, Z_cba_rec(L, a, g), b);
    2736             : }
    2737             : GEN
    2738      346143 : Z_cba(GEN a, GEN b)
    2739             : {
    2740      346143 :   GEN L = vectrunc_init(expi(a) + expi(b) + 2);
    2741      346143 :   GEN t = Z_cba_rec(L, a, b);
    2742      346143 :   if (!is_pm1(t)) vectrunc_append(L, t);
    2743      346143 :   return L;
    2744             : }
    2745             : /* P = coprime base, extend it by b; TODO: quadratic for now */
    2746             : GEN
    2747           0 : ZV_cba_extend(GEN P, GEN b)
    2748             : {
    2749           0 :   long i, l = lg(P);
    2750           0 :   GEN w = cgetg(l+1, t_VEC);
    2751           0 :   for (i = 1; i < l; i++)
    2752             :   {
    2753           0 :     GEN v = Z_cba(gel(P,i), b);
    2754           0 :     long nv = lg(v)-1;
    2755           0 :     gel(w,i) = vecslice(v, 1, nv-1); /* those divide P[i] but not b */
    2756           0 :     b = gel(v,nv);
    2757             :   }
    2758           0 :   gel(w,l) = b; return shallowconcat1(w);
    2759             : }
    2760             : GEN
    2761           0 : ZV_cba(GEN v)
    2762             : {
    2763           0 :   long i, l = lg(v);
    2764             :   GEN P;
    2765           0 :   if (l <= 2) return v;
    2766           0 :   P = Z_cba(gel(v,1), gel(v,2));
    2767           0 :   for (i = 3; i < l; i++) P = ZV_cba_extend(P, gel(v,i));
    2768           0 :   return P;
    2769             : }
    2770             : 
    2771             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2772             : GEN
    2773     3172817 : Z_ppo(GEN x, GEN f)
    2774             : {
    2775             :   for (;;)
    2776             :   {
    2777     3172817 :     f = gcdii(x, f); if (is_pm1(f)) break;
    2778     1890917 :     x = diviiexact(x, f);
    2779             :   }
    2780     1281898 :   return x;
    2781             : }
    2782             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2783             : ulong
    2784    61697467 : u_ppo(ulong x, ulong f)
    2785             : {
    2786             :   for (;;)
    2787             :   {
    2788    61697467 :     f = ugcd(x, f); if (f == 1) break;
    2789    13327616 :     x /= f;
    2790             :   }
    2791    48369797 :   return x;
    2792             : }
    2793             : 
    2794             : /* result known to be representable as an ulong */
    2795             : static ulong
    2796     3481481 : lcmuu(ulong a, ulong b) { ulong d = ugcd(a,b); return (a/d) * b; }
    2797             : 
    2798             : /* assume 0 < x < N; return u in (Z/NZ)^* such that u x = gcd(x,N) (mod N);
    2799             :  * set *pd = gcd(x,N) */
    2800             : ulong
    2801     7539670 : Fl_invgen(ulong x, ulong N, ulong *pd)
    2802             : {
    2803             :   ulong d, d0, e, v, v1;
    2804             :   long s;
    2805     7539670 :   *pd = d = xgcduu(N, x, 0, &v, &v1, &s);
    2806     7539979 :   if (s > 0) v = N - v;
    2807     7539979 :   if (d == 1) return v;
    2808             :   /* vx = gcd(x,N) (mod N), v coprime to N/d but need not be coprime to N */
    2809     4569572 :   e = N / d;
    2810     4569572 :   d0 = u_ppo(d, e); /* d = d0 d1, d0 coprime to N/d, rad(d1) | N/d */
    2811     4569713 :   if (d0 == 1) return v;
    2812     3481419 :   e = lcmuu(e, d / d0);
    2813     3481475 :   return u_chinese_coprime(v, 1, e, d0, e*d0);
    2814             : }
    2815             : 
    2816             : /* x t_INT, f ideal. Write x = x1 x2, sqf(x1) | f, (x2,f) = 1. Return x2 */
    2817             : static GEN
    2818         280 : nf_coprime_part(GEN nf, GEN x, GEN listpr)
    2819             : {
    2820         280 :   long v, j, lp = lg(listpr), N = nf_get_degree(nf);
    2821             :   GEN x1, x2, ex;
    2822             : 
    2823             : #if 0 /*1) via many gcds. Expensive ! */
    2824             :   GEN f = idealprodprime(nf, listpr);
    2825             :   f = ZM_hnfmodid(f, x); /* first gcd is less expensive since x in Z */
    2826             :   x = scalarmat(x, N);
    2827             :   for (;;)
    2828             :   {
    2829             :     if (gequal1(gcoeff(f,1,1))) break;
    2830             :     x = idealdivexact(nf, x, f);
    2831             :     f = ZM_hnfmodid(shallowconcat(f,x), gcoeff(x,1,1)); /* gcd(f,x) */
    2832             :   }
    2833             :   x2 = x;
    2834             : #else /*2) from prime decomposition */
    2835         280 :   x1 = NULL;
    2836         784 :   for (j=1; j<lp; j++)
    2837             :   {
    2838         504 :     GEN pr = gel(listpr,j);
    2839         504 :     v = Z_pval(x, pr_get_p(pr)); if (!v) continue;
    2840             : 
    2841         294 :     ex = muluu(v, pr_get_e(pr)); /* = v_pr(x) > 0 */
    2842         294 :     x1 = x1? idealmulpowprime(nf, x1, pr, ex)
    2843         294 :            : idealpow(nf, pr, ex);
    2844             :   }
    2845         280 :   x = scalarmat(x, N);
    2846         280 :   x2 = x1? idealdivexact(nf, x, x1): x;
    2847             : #endif
    2848         280 :   return x2;
    2849             : }
    2850             : 
    2851             : /* L0 in K^*, assume (L0,f) = 1. Return L integral, L0 = L mod f  */
    2852             : GEN
    2853        6615 : make_integral(GEN nf, GEN L0, GEN f, GEN listpr)
    2854             : {
    2855             :   GEN fZ, t, L, D2, d1, d2, d;
    2856             : 
    2857        6615 :   L = Q_remove_denom(L0, &d);
    2858        6615 :   if (!d) return L0;
    2859             : 
    2860             :   /* L0 = L / d, L integral */
    2861        1106 :   fZ = gcoeff(f,1,1);
    2862        1106 :   if (typ(L) == t_INT) return Fp_mul(L, Fp_inv(d, fZ), fZ);
    2863             :   /* Kill denom part coprime to fZ */
    2864         630 :   d2 = Z_ppo(d, fZ);
    2865         630 :   t = Fp_inv(d2, fZ); if (!is_pm1(t)) L = ZC_Z_mul(L,t);
    2866         630 :   if (equalii(d, d2)) return L;
    2867             : 
    2868         280 :   d1 = diviiexact(d, d2);
    2869             :   /* L0 = (L / d1) mod f. d1 not coprime to f
    2870             :    * write (d1) = D1 D2, D2 minimal, (D2,f) = 1. */
    2871         280 :   D2 = nf_coprime_part(nf, d1, listpr);
    2872         280 :   t = idealaddtoone_i(nf, D2, f); /* in D2, 1 mod f */
    2873         280 :   L = nfmuli(nf,t,L);
    2874             : 
    2875             :   /* if (L0, f) = 1, then L in D1 ==> in D1 D2 = (d1) */
    2876         280 :   return Q_div_to_int(L, d1); /* exact division */
    2877             : }
    2878             : 
    2879             : /* assume L is a list of prime ideals. Return the product */
    2880             : GEN
    2881         336 : idealprodprime(GEN nf, GEN L)
    2882             : {
    2883         336 :   long l = lg(L), i;
    2884             :   GEN z;
    2885         336 :   if (l == 1) return matid(nf_get_degree(nf));
    2886         336 :   z = pr_hnf(nf, gel(L,1));
    2887         371 :   for (i=2; i<l; i++) z = idealHNF_mul_two(nf,z, gel(L,i));
    2888         336 :   return z;
    2889             : }
    2890             : 
    2891             : /* optimize for the frequent case I = nfhnf()[2]: lots of them are 1 */
    2892             : GEN
    2893         427 : idealprod(GEN nf, GEN I)
    2894             : {
    2895         427 :   long i, l = lg(I);
    2896             :   GEN z;
    2897        1043 :   for (i = 1; i < l; i++)
    2898        1036 :     if (!equali1(gel(I,i))) break;
    2899         427 :   if (i == l) return gen_1;
    2900         420 :   z = gel(I,i);
    2901         693 :   for (i++; i<l; i++) z = idealmul(nf, z, gel(I,i));
    2902         420 :   return z;
    2903             : }
    2904             : 
    2905             : /* v_pr(idealprod(nf,I)) */
    2906             : long
    2907        2289 : idealprodval(GEN nf, GEN I, GEN pr)
    2908             : {
    2909        2289 :   long i, l = lg(I), v = 0;
    2910       13391 :   for (i = 1; i < l; i++)
    2911       11102 :     if (!equali1(gel(I,i))) v += idealval(nf, gel(I,i), pr);
    2912        2289 :   return v;
    2913             : }
    2914             : 
    2915             : /* assume L is a list of prime ideals. Return prod L[i]^e[i] */
    2916             : GEN
    2917       55825 : factorbackprime(GEN nf, GEN L, GEN e)
    2918             : {
    2919       55825 :   long l = lg(L), i;
    2920             :   GEN z;
    2921             : 
    2922       55825 :   if (l == 1) return matid(nf_get_degree(nf));
    2923       42490 :   z = idealpow(nf, gel(L,1), gel(e,1));
    2924       64680 :   for (i=2; i<l; i++)
    2925       22190 :     if (signe(gel(e,i))) z = idealmulpowprime(nf,z, gel(L,i),gel(e,i));
    2926       42490 :   return z;
    2927             : }
    2928             : 
    2929             : /* F in Z, divisible exactly by pr.p. Return F-uniformizer for pr, i.e.
    2930             :  * a t in Z_K such that v_pr(t) = 1 and (t, F/pr) = 1 */
    2931             : GEN
    2932       60597 : pr_uniformizer(GEN pr, GEN F)
    2933             : {
    2934       60597 :   GEN p = pr_get_p(pr), t = pr_get_gen(pr);
    2935       60597 :   if (!equalii(F, p))
    2936             :   {
    2937       32925 :     long e = pr_get_e(pr);
    2938       32925 :     GEN u, v, q = (e == 1)? sqri(p): p;
    2939       32925 :     u = mulii(q, Fp_inv(q, diviiexact(F,p))); /* 1 mod F/p, 0 mod q */
    2940       32924 :     v = subui(1UL, u); /* 0 mod F/p, 1 mod q */
    2941       32923 :     if (pr_is_inert(pr))
    2942           0 :       t = addii(mulii(p, v), u);
    2943             :     else
    2944             :     {
    2945       32923 :       t = ZC_Z_mul(t, v);
    2946       32924 :       gel(t,1) = addii(gel(t,1), u); /* return u + vt */
    2947             :     }
    2948             :   }
    2949       60596 :   return t;
    2950             : }
    2951             : /* L = list of prime ideals, return lcm_i (L[i] \cap \ZM) */
    2952             : GEN
    2953       90204 : prV_lcm_capZ(GEN L)
    2954             : {
    2955       90204 :   long i, r = lg(L);
    2956             :   GEN F;
    2957       90204 :   if (r == 1) return gen_1;
    2958       70794 :   F = pr_get_p(gel(L,1));
    2959      125185 :   for (i = 2; i < r; i++)
    2960             :   {
    2961       54390 :     GEN pr = gel(L,i), p = pr_get_p(pr);
    2962       54390 :     if (!dvdii(F, p)) F = mulii(F,p);
    2963             :   }
    2964       70795 :   return F;
    2965             : }
    2966             : /* v vector of prid. Return underlying list of rational primes */
    2967             : GEN
    2968       39746 : prV_primes(GEN v)
    2969             : {
    2970       39746 :   long i, l = lg(v);
    2971       39746 :   GEN w = cgetg(l,t_VEC);
    2972      109529 :   for (i=1; i<l; i++) gel(w,i) = pr_get_p(gel(v,i));
    2973       39746 :   return ZV_sort_uniq(w);
    2974             : }
    2975             : 
    2976             : /* Given a prime ideal factorization with possibly zero or negative
    2977             :  * exponents, gives b such that v_p(b) = v_p(x) for all prime ideals pr | x
    2978             :  * and v_pr(b) >= 0 for all other pr.
    2979             :  * For optimal performance, all [anti-]uniformizers should be precomputed,
    2980             :  * but no support for this yet.
    2981             :  *
    2982             :  * If nored, do not reduce result.
    2983             :  * No garbage collecting */
    2984             : static GEN
    2985       63807 : idealapprfact_i(GEN nf, GEN x, int nored)
    2986             : {
    2987             :   GEN z, d, L, e, e2, F;
    2988             :   long i, r;
    2989             :   int flagden;
    2990             : 
    2991       63807 :   nf = checknf(nf);
    2992       63807 :   L = gel(x,1);
    2993       63807 :   e = gel(x,2);
    2994       63807 :   F = prV_lcm_capZ(L);
    2995       63808 :   flagden = 0;
    2996       63808 :   z = NULL; r = lg(e);
    2997      149654 :   for (i = 1; i < r; i++)
    2998             :   {
    2999       85846 :     long s = signe(gel(e,i));
    3000             :     GEN pi, q;
    3001       85846 :     if (!s) continue;
    3002       56880 :     if (s < 0) flagden = 1;
    3003       56880 :     pi = pr_uniformizer(gel(L,i), F);
    3004       56879 :     q = nfpow(nf, pi, gel(e,i));
    3005       56880 :     z = z? nfmul(nf, z, q): q;
    3006             :   }
    3007       63808 :   if (!z) return gen_1;
    3008       29145 :   if (nored || typ(z) != t_COL) return z;
    3009       10269 :   e2 = cgetg(r, t_VEC);
    3010       29077 :   for (i=1; i<r; i++) gel(e2,i) = addiu(gel(e,i), 1);
    3011       10268 :   x = factorbackprime(nf, L,e2);
    3012       10268 :   if (flagden) /* denominator */
    3013             :   {
    3014       10254 :     z = Q_remove_denom(z, &d);
    3015       10255 :     d = diviiexact(d, Z_ppo(d, F));
    3016       10253 :     x = RgM_Rg_mul(x, d);
    3017             :   }
    3018             :   else
    3019          14 :     d = NULL;
    3020       10267 :   z = ZC_reducemodlll(z, x);
    3021       10269 :   return d? RgC_Rg_div(z,d): z;
    3022             : }
    3023             : 
    3024             : GEN
    3025           0 : idealapprfact(GEN nf, GEN x) {
    3026           0 :   pari_sp av = avma;
    3027           0 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    3028             : }
    3029             : GEN
    3030          14 : idealappr(GEN nf, GEN x) {
    3031          14 :   pari_sp av = avma;
    3032          14 :   if (!is_nf_extfactor(x)) x = idealfactor(nf, x);
    3033          14 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    3034             : }
    3035             : 
    3036             : /* OBSOLETE */
    3037             : GEN
    3038          14 : idealappr0(GEN nf, GEN x, long fl) { (void)fl; return idealappr(nf, x); }
    3039             : 
    3040             : static GEN
    3041          21 : mat_ideal_two_elt2(GEN nf, GEN x, GEN a)
    3042             : {
    3043          21 :   GEN F = idealfactor(nf,a), P = gel(F,1), E = gel(F,2);
    3044          21 :   long i, r = lg(E);
    3045          84 :   for (i=1; i<r; i++) gel(E,i) = stoi( idealval(nf,x,gel(P,i)) );
    3046          21 :   return idealapprfact_i(nf,F,1);
    3047             : }
    3048             : 
    3049             : static void
    3050          14 : not_in_ideal(GEN a) {
    3051          14 :   pari_err_DOMAIN("idealtwoelt2","element mod ideal", "!=", gen_0, a);
    3052           0 : }
    3053             : /* x integral in HNF, a an 'nf' */
    3054             : static int
    3055          28 : in_ideal(GEN x, GEN a)
    3056             : {
    3057          28 :   switch(typ(a))
    3058             :   {
    3059          14 :     case t_INT: return dvdii(a, gcoeff(x,1,1));
    3060           7 :     case t_COL: return RgV_is_ZV(a) && !!hnf_invimage(x, a);
    3061           7 :     default: return 0;
    3062             :   }
    3063             : }
    3064             : 
    3065             : /* Given an integral ideal x and a in x, gives a b such that
    3066             :  * x = aZ_K + bZ_K using the approximation theorem */
    3067             : GEN
    3068          42 : idealtwoelt2(GEN nf, GEN x, GEN a)
    3069             : {
    3070          42 :   pari_sp av = avma;
    3071             :   GEN cx, b;
    3072             : 
    3073          42 :   nf = checknf(nf);
    3074          42 :   a = nf_to_scalar_or_basis(nf, a);
    3075          42 :   x = idealhnf_shallow(nf,x);
    3076          42 :   if (lg(x) == 1)
    3077             :   {
    3078          14 :     if (!isintzero(a)) not_in_ideal(a);
    3079           7 :     set_avma(av); return gen_0;
    3080             :   }
    3081          28 :   x = Q_primitive_part(x, &cx);
    3082          28 :   if (cx) a = gdiv(a, cx);
    3083          28 :   if (!in_ideal(x, a)) not_in_ideal(a);
    3084          21 :   b = mat_ideal_two_elt2(nf, x, a);
    3085          21 :   if (typ(b) == t_COL)
    3086             :   {
    3087          14 :     GEN mod = idealhnf_principal(nf,a);
    3088          14 :     b = ZC_hnfrem(b,mod);
    3089          14 :     if (ZV_isscalar(b)) b = gel(b,1);
    3090             :   }
    3091             :   else
    3092             :   {
    3093           7 :     GEN aZ = typ(a) == t_COL? Q_denom(zk_inv(nf,a)): a; /* (a) \cap Z */
    3094           7 :     b = centermodii(b, aZ, shifti(aZ,-1));
    3095             :   }
    3096          21 :   b = cx? gmul(b,cx): gcopy(b);
    3097          21 :   return gerepileupto(av, b);
    3098             : }
    3099             : 
    3100             : /* Given 2 integral ideals x and y in nf, returns a beta in nf such that
    3101             :  * beta * x is an integral ideal coprime to y */
    3102             : GEN
    3103       44911 : idealcoprimefact(GEN nf, GEN x, GEN fy)
    3104             : {
    3105       44911 :   GEN L = gel(fy,1), e;
    3106       44911 :   long i, r = lg(L);
    3107             : 
    3108       44911 :   e = cgetg(r, t_COL);
    3109       84129 :   for (i=1; i<r; i++) gel(e,i) = stoi( -idealval(nf,x,gel(L,i)) );
    3110       44911 :   return idealapprfact_i(nf, mkmat2(L,e), 0);
    3111             : }
    3112             : GEN
    3113          70 : idealcoprime(GEN nf, GEN x, GEN y)
    3114             : {
    3115          70 :   pari_sp av = avma;
    3116          70 :   return gerepileupto(av, idealcoprimefact(nf, x, idealfactor(nf,y)));
    3117             : }
    3118             : 
    3119             : GEN
    3120           7 : nfmulmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    3121             : {
    3122           7 :   pari_sp av = avma;
    3123           7 :   GEN z, p, pr = modpr, T;
    3124             : 
    3125           7 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    3126           0 :   x = nf_to_Fq(nf,x,modpr);
    3127           0 :   y = nf_to_Fq(nf,y,modpr);
    3128           0 :   z = Fq_mul(x,y,T,p);
    3129           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    3130             : }
    3131             : 
    3132             : GEN
    3133           0 : nfdivmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    3134             : {
    3135           0 :   pari_sp av = avma;
    3136           0 :   nf = checknf(nf);
    3137           0 :   return gerepileupto(av, nfreducemodpr(nf, nfdiv(nf,x,y), modpr));
    3138             : }
    3139             : 
    3140             : GEN
    3141           0 : nfpowmodpr(GEN nf, GEN x, GEN k, GEN modpr)
    3142             : {
    3143           0 :   pari_sp av=avma;
    3144           0 :   GEN z, T, p, pr = modpr;
    3145             : 
    3146           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    3147           0 :   z = nf_to_Fq(nf,x,modpr);
    3148           0 :   z = Fq_pow(z,k,T,p);
    3149           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    3150             : }
    3151             : 
    3152             : GEN
    3153           0 : nfkermodpr(GEN nf, GEN x, GEN modpr)
    3154             : {
    3155           0 :   pari_sp av = avma;
    3156           0 :   GEN T, p, pr = modpr;
    3157             : 
    3158           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    3159           0 :   if (typ(x)!=t_MAT) pari_err_TYPE("nfkermodpr",x);
    3160           0 :   x = nfM_to_FqM(x, nf, modpr);
    3161           0 :   return gerepilecopy(av, FqM_to_nfM(FqM_ker(x,T,p), modpr));
    3162             : }
    3163             : 
    3164             : GEN
    3165           0 : nfsolvemodpr(GEN nf, GEN a, GEN b, GEN pr)
    3166             : {
    3167           0 :   const char *f = "nfsolvemodpr";
    3168           0 :   pari_sp av = avma;
    3169             :   GEN T, p, modpr;
    3170             : 
    3171           0 :   nf = checknf(nf);
    3172           0 :   modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    3173           0 :   if (typ(a)!=t_MAT) pari_err_TYPE(f,a);
    3174           0 :   a = nfM_to_FqM(a, nf, modpr);
    3175           0 :   switch(typ(b))
    3176             :   {
    3177           0 :     case t_MAT:
    3178           0 :       b = nfM_to_FqM(b, nf, modpr);
    3179           0 :       b = FqM_gauss(a,b,T,p);
    3180           0 :       if (!b) pari_err_INV(f,a);
    3181           0 :       a = FqM_to_nfM(b, modpr);
    3182           0 :       break;
    3183           0 :     case t_COL:
    3184           0 :       b = nfV_to_FqV(b, nf, modpr);
    3185           0 :       b = FqM_FqC_gauss(a,b,T,p);
    3186           0 :       if (!b) pari_err_INV(f,a);
    3187           0 :       a = FqV_to_nfV(b, modpr);
    3188           0 :       break;
    3189           0 :     default: pari_err_TYPE(f,b);
    3190             :   }
    3191           0 :   return gerepilecopy(av, a);
    3192             : }

Generated by: LCOV version 1.13