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.13.0 lcov report (development 25825-edc109b529) Lines: 1578 1771 89.1 %
Date: 2020-09-21 06:08:33 Functions: 155 173 89.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13