Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - base4.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.11.0 lcov report (development 22860-5579deb0b) Lines: 1415 1563 90.5 %
Date: 2018-07-18 05:36:42 Functions: 141 155 91.0 %
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     4453340 : idealtyp(GEN *ideal, GEN *arch)
      44             : {
      45     4453340 :   GEN x = *ideal;
      46     4453340 :   long t,lx,tx = typ(x);
      47             : 
      48     4453340 :   if (tx!=t_VEC || lg(x)!=3) *arch = NULL;
      49             :   else
      50             :   {
      51      227508 :     GEN a = gel(x,2);
      52      227508 :     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      227501 :     *arch = a;
      58      227501 :     x = gel(x,1); tx = typ(x);
      59             :   }
      60     4453333 :   switch(tx)
      61             :   {
      62     1578756 :     case t_MAT: lx = lg(x);
      63     1578756 :       if (lx == 1) { t = id_PRINCIPAL; x = gen_0; break; }
      64     1578679 :       if (lx != lgcols(x)) pari_err_TYPE("idealtyp [non-square t_MAT]",x);
      65     1578672 :       t = id_MAT;
      66     1578672 :       break;
      67             : 
      68     2463165 :     case t_VEC: if (lg(x)!=6) pari_err_TYPE("idealtyp",x);
      69     2463151 :       t = id_PRIME; break;
      70             : 
      71             :     case t_POL: case t_POLMOD: case t_COL:
      72             :     case t_INT: case t_FRAC:
      73      411412 :       t = id_PRINCIPAL; break;
      74             :     default:
      75           0 :       pari_err_TYPE("idealtyp",x);
      76             :       return 0; /*LCOV_EXCL_LINE*/
      77             :   }
      78     4453312 :   *ideal = x; return t;
      79             : }
      80             : 
      81             : /* true nf; v = [a,x,...], a in Z. Return (a,x) */
      82             : GEN
      83      120961 : idealhnf_two(GEN nf, GEN v)
      84             : {
      85      120961 :   GEN p = gel(v,1), pi = gel(v,2), m = zk_scalar_or_multable(nf, pi);
      86      120961 :   if (typ(m) == t_INT) return scalarmat(gcdii(m,p), nf_get_degree(nf));
      87      106213 :   return ZM_hnfmodid(m, p);
      88             : }
      89             : /* true nf */
      90             : GEN
      91     1840189 : pr_hnf(GEN nf, GEN pr)
      92             : {
      93     1840189 :   GEN p = pr_get_p(pr), m;
      94     1840189 :   if (pr_is_inert(pr)) return scalarmat(p, nf_get_degree(nf));
      95     1582029 :   m = zk_scalar_or_multable(nf, pr_get_gen(pr));
      96     1582029 :   return ZM_hnfmodprime(m, p);
      97             : }
      98             : 
      99             : GEN
     100      271827 : idealhnf_principal(GEN nf, GEN x)
     101             : {
     102             :   GEN cx;
     103      271827 :   x = nf_to_scalar_or_basis(nf, x);
     104      271827 :   switch(typ(x))
     105             :   {
     106      155430 :     case t_COL: break;
     107       92514 :     case t_INT:  if (!signe(x)) return cgetg(1,t_MAT);
     108       92101 :       return scalarmat(absi_shallow(x), nf_get_degree(nf));
     109             :     case t_FRAC:
     110       23883 :       return scalarmat(Q_abs_shallow(x), nf_get_degree(nf));
     111           0 :     default: pari_err_TYPE("idealhnf",x);
     112             :   }
     113      155430 :   x = Q_primitive_part(x, &cx);
     114      155430 :   RgV_check_ZV(x, "idealhnf");
     115      155430 :   x = zk_multable(nf, x);
     116      155430 :   x = ZM_hnfmodid(x, zkmultable_capZ(x));
     117      155430 :   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          14 :     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      325264 : idealhnf_shallow(GEN nf, GEN x)
     133             : {
     134      325264 :   long tx = typ(x), lx = lg(x), N;
     135             : 
     136             :   /* cannot use idealtyp because here we allow non-square matrices */
     137      325264 :   if (tx == t_VEC && lx == 3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
     138      325264 :   if (tx == t_VEC && lx == 6) return pr_hnf(nf,x); /* PRIME */
     139      225608 :   switch(tx)
     140             :   {
     141             :     case t_MAT:
     142             :     {
     143             :       GEN cx;
     144       48244 :       long nx = lx-1;
     145       48244 :       N = nf_get_degree(nf);
     146       48244 :       if (nx == 0) return cgetg(1, t_MAT);
     147       48223 :       if (nbrows(x) != N) pari_err_TYPE("idealhnf [wrong dimension]",x);
     148       48216 :       if (nx == 1) return idealhnf_principal(nf, gel(x,1));
     149             : 
     150       46865 :       if (nx == N && RgM_is_ZM(x) && ZM_ishnf(x)) return x;
     151       22442 :       x = Q_primitive_part(x, &cx);
     152       22442 :       if (nx < N) x = vec_mulid(nf, x, nx, N);
     153       22442 :       x = ZM_hnfmod(x, ZM_detmult(x));
     154       22442 :       return cx? ZM_Q_mul(x,cx): x;
     155             :     }
     156             :     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      177350 :     default: return idealhnf_principal(nf, x); /* PRINCIPAL */
     178             :   }
     179             : }
     180             : GEN
     181        4228 : idealhnf(GEN nf, GEN x)
     182             : {
     183        4228 :   pari_sp av = avma;
     184        4228 :   GEN y = idealhnf_shallow(checknf(nf), x);
     185        4214 :   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          42 : idealpow0(GEN nf, GEN x, GEN n, long flag)
     199             : {
     200          42 :   if (flag) return idealpowred(nf,x,n);
     201          35 :   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         119 : idealhnf0(GEN nf, GEN a, GEN b)
     284             : {
     285             :   long ta, tb;
     286             :   pari_sp av;
     287             :   GEN x;
     288         119 :   if (!b) return idealhnf(nf,a);
     289             : 
     290             :   /* HNF of aZ_K+bZ_K */
     291          56 :   av = avma; nf = checknf(nf);
     292          56 :   a = nf_to_scalar_or_basis(nf,a); ta = typ(a);
     293          56 :   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      138680 : ok_elt(GEN x, GEN xZ, GEN y)
     310             : {
     311      138680 :   pari_sp av = avma;
     312      138680 :   int r = ZM_equal(x, ZM_hnfmodid(y, xZ));
     313      138680 :   avma = av; return r;
     314             : }
     315             : 
     316             : static GEN
     317       52827 : addmul_col(GEN a, long s, GEN b)
     318             : {
     319             :   long i,l;
     320       52827 :   if (!s) return a? leafcopy(a): a;
     321       52666 :   if (!a) return gmulsg(s,b);
     322       49678 :   l = lg(a);
     323      260586 :   for (i=1; i<l; i++)
     324      210908 :     if (signe(gel(b,i))) gel(a,i) = addii(gel(a,i), mulsi(s, gel(b,i)));
     325       49678 :   return a;
     326             : }
     327             : 
     328             : /* a <-- a + s * b, all coeffs integers */
     329             : static GEN
     330       23362 : addmul_mat(GEN a, long s, GEN b)
     331             : {
     332             :   long j,l;
     333             :   /* copy otherwise next call corrupts a */
     334       23362 :   if (!s) return a? RgM_shallowcopy(a): a;
     335       21894 :   if (!a) return gmulsg(s,b);
     336       11910 :   l = lg(a);
     337       57117 :   for (j=1; j<l; j++)
     338       45207 :     (void)addmul_col(gel(a,j), s, gel(b,j));
     339       11910 :   return a;
     340             : }
     341             : 
     342             : static GEN
     343       72722 : get_random_a(GEN nf, GEN x, GEN xZ)
     344             : {
     345             :   pari_sp av;
     346       72722 :   long i, lm, l = lg(x);
     347             :   GEN a, z, beta, mul;
     348             : 
     349       72722 :   beta= cgetg(l, t_VEC);
     350       72722 :   mul = cgetg(l, t_VEC); lm = 1; /* = lg(mul) */
     351             :   /* look for a in x such that a O/xZ = x O/xZ */
     352      142508 :   for (i = 2; i < l; i++)
     353             :   {
     354      139520 :     GEN xi = gel(x,i);
     355      139520 :     GEN t = FpM_red(zk_multable(nf,xi), xZ); /* ZM, cannot be a scalar */
     356      139520 :     if (gequal0(t)) continue;
     357      128696 :     if (ok_elt(x,xZ, t)) return xi;
     358       58962 :     gel(beta,lm) = xi;
     359             :     /* mul[i] = { canonical generators for x[i] O/xZ as Z-module } */
     360       58962 :     gel(mul,lm) = t; lm++;
     361             :   }
     362        2988 :   setlg(mul, lm);
     363        2988 :   setlg(beta,lm);
     364        2988 :   z = cgetg(lm, t_VECSMALL);
     365        9998 :   for(av = avma;; avma = av)
     366             :   {
     367       40370 :     for (a=NULL,i=1; i<lm; i++)
     368             :     {
     369       23362 :       long t = random_bits(4) - 7; /* in [-7,8] */
     370       23362 :       z[i] = t;
     371       23362 :       a = addmul_mat(a, t, gel(mul,i));
     372             :     }
     373             :     /* a = matrix (NOT HNF) of ideal generated by beta.z in O/xZ */
     374        9998 :     if (a && ok_elt(x,xZ, a)) break;
     375             :   }
     376       10608 :   for (a=NULL,i=1; i<lm; i++)
     377        7620 :     a = addmul_col(a, z[i], gel(beta,i));
     378        2988 :   return a;
     379             : }
     380             : 
     381             : /* x square matrix, assume it is HNF */
     382             : static GEN
     383      173094 : mat_ideal_two_elt(GEN nf, GEN x)
     384             : {
     385             :   GEN y, a, cx, xZ;
     386      173094 :   long N = nf_get_degree(nf);
     387             :   pari_sp av, tetpil;
     388             : 
     389      173094 :   if (lg(x)-1 != N) pari_err_DIM("idealtwoelt");
     390      173080 :   if (N == 2) return mkvec2copy(gcoeff(x,1,1), gel(x,2));
     391             : 
     392       82911 :   y = cgetg(3,t_VEC); av = avma;
     393       82911 :   cx = Q_content(x);
     394       82911 :   xZ = gcoeff(x,1,1);
     395       82911 :   if (gequal(xZ, cx)) /* x = (cx) */
     396             :   {
     397        3339 :     gel(y,1) = cx;
     398        3339 :     gel(y,2) = gen_0; return y;
     399             :   }
     400       79572 :   if (equali1(cx)) cx = NULL;
     401             :   else
     402             :   {
     403        1701 :     x = Q_div_to_int(x, cx);
     404        1701 :     xZ = gcoeff(x,1,1);
     405             :   }
     406       79572 :   if (N < 6)
     407       67529 :     a = get_random_a(nf, x, xZ);
     408             :   else
     409             :   {
     410       12043 :     const long FB[] = { _evallg(15+1) | evaltyp(t_VECSMALL),
     411             :       2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47
     412             :     };
     413       12043 :     GEN P, E, a1 = Z_smoothen(xZ, (GEN)FB, &P, &E);
     414       12043 :     if (!a1) /* factors completely */
     415        6850 :       a = idealapprfact_i(nf, idealfactor(nf,x), 1);
     416        5193 :     else if (lg(P) == 1) /* no small factors */
     417        3834 :       a = get_random_a(nf, x, xZ);
     418             :     else /* general case */
     419             :     {
     420             :       GEN A0, A1, a0, u0, u1, v0, v1, pi0, pi1, t, u;
     421        1359 :       a0 = diviiexact(xZ, a1);
     422        1359 :       A0 = ZM_hnfmodid(x, a0); /* smooth part of x */
     423        1359 :       A1 = ZM_hnfmodid(x, a1); /* cofactor */
     424        1359 :       pi0 = idealapprfact_i(nf, idealfactor(nf,A0), 1);
     425        1359 :       pi1 = get_random_a(nf, A1, a1);
     426        1359 :       (void)bezout(a0, a1, &v0,&v1);
     427        1359 :       u0 = mulii(a0, v0);
     428        1359 :       u1 = mulii(a1, v1);
     429        1359 :       if (typ(pi0) != t_COL) t = addmulii(u0, pi0, u1);
     430             :       else
     431        1359 :       { t = ZC_Z_mul(pi0, u1); gel(t,1) = addii(gel(t,1), u0); }
     432        1359 :       u = ZC_Z_mul(pi1, u0); gel(u,1) = addii(gel(u,1), u1);
     433        1359 :       a = nfmuli(nf, centermod(u, xZ), centermod(t, xZ));
     434             :     }
     435             :   }
     436       79572 :   if (cx)
     437             :   {
     438        1701 :     a = centermod(a, xZ);
     439        1701 :     tetpil = avma;
     440        1701 :     if (typ(cx) == t_INT)
     441             :     {
     442         469 :       gel(y,1) = mulii(xZ, cx);
     443         469 :       gel(y,2) = ZC_Z_mul(a, cx);
     444             :     }
     445             :     else
     446             :     {
     447        1232 :       gel(y,1) = gmul(xZ, cx);
     448        1232 :       gel(y,2) = RgC_Rg_mul(a, cx);
     449             :     }
     450             :   }
     451             :   else
     452             :   {
     453       77871 :     tetpil = avma;
     454       77871 :     gel(y,1) = icopy(xZ);
     455       77871 :     gel(y,2) = centermod(a, xZ);
     456             :   }
     457       79572 :   gerepilecoeffssp(av,tetpil,y+1,2); return y;
     458             : }
     459             : 
     460             : /* Given an ideal x, returns [a,alpha] such that a is in Q,
     461             :  * x = a Z_K + alpha Z_K, alpha in K^*
     462             :  * a = 0 or alpha = 0 are possible, but do not try to determine whether
     463             :  * x is principal. */
     464             : GEN
     465       52490 : idealtwoelt(GEN nf, GEN x)
     466             : {
     467             :   pari_sp av;
     468             :   GEN z;
     469       52490 :   long tx = idealtyp(&x,&z);
     470       52483 :   nf = checknf(nf);
     471       52483 :   if (tx == id_MAT) return mat_ideal_two_elt(nf,x);
     472        1918 :   if (tx == id_PRIME) return mkvec2copy(gel(x,1), gel(x,2));
     473             :   /* id_PRINCIPAL */
     474         847 :   av = avma; x = nf_to_scalar_or_basis(nf, x);
     475        1498 :   return gerepilecopy(av, typ(x)==t_COL? mkvec2(gen_0,x):
     476         742 :                                          mkvec2(Q_abs_shallow(x),gen_0));
     477             : }
     478             : 
     479             : /*******************************************************************/
     480             : /*                                                                 */
     481             : /*                         FACTORIZATION                           */
     482             : /*                                                                 */
     483             : /*******************************************************************/
     484             : /* x integral ideal in HNF, Zval = v_p(x \cap Z) > 0; return v_p(Nx) */
     485             : static long
     486      208411 : idealHNF_norm_pval(GEN x, GEN p, long Zval)
     487             : {
     488      208411 :   long i, v = Zval, l = lg(x);
     489      208411 :   for (i = 2; i < l; i++) v += Z_pval(gcoeff(x,i,i), p);
     490      208411 :   return v;
     491             : }
     492             : 
     493             : /* x integral in HNF, f0 = partial factorization of a multiple of
     494             :  * x[1,1] = x\cap Z */
     495             : GEN
     496       38813 : idealHNF_Z_factor_i(GEN x, GEN f0, GEN *pvN, GEN *pvZ)
     497             : {
     498       38813 :   GEN P, E, vN, vZ, xZ = gcoeff(x,1,1), f = f0? f0: Z_factor(xZ);
     499             :   long i, l;
     500       38813 :   P = gel(f,1); l = lg(P);
     501       38813 :   E = gel(f,2);
     502       38813 :   *pvN = vN = cgetg(l, t_VECSMALL);
     503       38813 :   *pvZ = vZ = cgetg(l, t_VECSMALL);
     504       74952 :   for (i = 1; i < l; i++)
     505             :   {
     506       36139 :     GEN p = gel(P,i);
     507       36139 :     vZ[i] = f0? Z_pval(xZ, p): itou(gel(E,i));
     508       36139 :     vN[i] = idealHNF_norm_pval(x,p, vZ[i]);
     509             :   }
     510       38813 :   return P;
     511             : }
     512             : /* return P, primes dividing Nx and xZ = x\cap Z, set v_p(Nx), v_p(xZ);
     513             :  * x integral in HNF */
     514             : GEN
     515           0 : idealHNF_Z_factor(GEN x, GEN *pvN, GEN *pvZ)
     516           0 : { return idealHNF_Z_factor_i(x, NULL, pvN, pvZ); }
     517             : 
     518             : /* v_P(A)*f(P) <= Nval [e.g. Nval = v_p(Norm A)], Zval = v_p(A \cap Z).
     519             :  * Return v_P(A) */
     520             : static long
     521      226803 : idealHNF_val(GEN A, GEN P, long Nval, long Zval)
     522             : {
     523      226803 :   long f = pr_get_f(P), vmax, v, e, i, j, k, l;
     524             :   GEN mul, B, a, y, r, p, pk, cx, vals;
     525             :   pari_sp av;
     526             : 
     527      226803 :   if (Nval < f) return 0;
     528      226726 :   p = pr_get_p(P);
     529      226726 :   e = pr_get_e(P);
     530             :   /* v_P(A) <= max [ e * v_p(A \cap Z), floor[v_p(Nix) / f ] */
     531      226726 :   vmax = minss(Zval * e, Nval / f);
     532      226726 :   mul = pr_get_tau(P);
     533      226726 :   l = lg(mul);
     534      226726 :   B = cgetg(l,t_MAT);
     535             :   /* B[1] not needed: v_pr(A[1]) = v_pr(A \cap Z) is known already */
     536      226726 :   gel(B,1) = gen_0; /* dummy */
     537      672505 :   for (j = 2; j < l; j++)
     538             :   {
     539      521122 :     GEN x = gel(A,j);
     540      521122 :     gel(B,j) = y = cgetg(l, t_COL);
     541     4144944 :     for (i = 1; i < l; i++)
     542             :     { /* compute a = (x.t0)_i, A in HNF ==> x[j+1..l-1] = 0 */
     543     3699165 :       a = mulii(gel(x,1), gcoeff(mul,i,1));
     544     3699165 :       for (k = 2; k <= j; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     545             :       /* p | a ? */
     546     3699165 :       gel(y,i) = dvmdii(a,p,&r); if (signe(r)) return 0;
     547             :     }
     548             :   }
     549      151383 :   vals = cgetg(l, t_VECSMALL);
     550             :   /* vals[1] not needed */
     551      538083 :   for (j = 2; j < l; j++)
     552             :   {
     553      386700 :     gel(B,j) = Q_primitive_part(gel(B,j), &cx);
     554      386700 :     vals[j] = cx? 1 + e * Q_pval(cx, p): 1;
     555             :   }
     556      151383 :   pk = powiu(p, ceildivuu(vmax, e));
     557      151383 :   av = avma; y = cgetg(l,t_COL);
     558             :   /* can compute mod p^ceil((vmax-v)/e) */
     559      216037 :   for (v = 1; v < vmax; v++)
     560             :   { /* we know v_pr(Bj) >= v for all j */
     561       67942 :     if (e == 1 || (vmax - v) % e == 0) pk = diviiexact(pk, p);
     562      509357 :     for (j = 2; j < l; j++)
     563             :     {
     564      444703 :       GEN x = gel(B,j); if (v < vals[j]) continue;
     565     4451351 :       for (i = 1; i < l; i++)
     566             :       {
     567     4127493 :         pari_sp av2 = avma;
     568     4127493 :         a = mulii(gel(x,1), gcoeff(mul,i,1));
     569     4127493 :         for (k = 2; k < l; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
     570             :         /* a = (x.t_0)_i; p | a ? */
     571     4127493 :         a = dvmdii(a,p,&r); if (signe(r)) return v;
     572     4124205 :         if (lgefint(a) > lgefint(pk)) a = remii(a, pk);
     573     4124205 :         gel(y,i) = gerepileuptoint(av2, a);
     574             :       }
     575      323858 :       gel(B,j) = y; y = x;
     576      323858 :       if (gc_needed(av,3))
     577             :       {
     578           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"idealval");
     579           0 :         gerepileall(av,3, &y,&B,&pk);
     580             :       }
     581             :     }
     582             :   }
     583      148095 :   return v;
     584             : }
     585             : /* true nf, x != 0 integral ideal in HNF, cx t_INT or NULL,
     586             :  * FA integer factorization matrix or NULL. Return partial factorization of
     587             :  * cx * x above primes in FA (complete factorization if !FA)*/
     588             : static GEN
     589       38813 : idealHNF_factor_i(GEN nf, GEN x, GEN cx, GEN FA)
     590             : {
     591       38813 :   const long N = lg(x)-1;
     592             :   long i, j, k, l, v;
     593       38813 :   GEN vN, vZ, vP, vE, vp = idealHNF_Z_factor_i(x, FA, &vN,&vZ);
     594             : 
     595       38813 :   l = lg(vp);
     596       38813 :   i = cx? expi(cx)+1: 1;
     597       38813 :   vP = cgetg((l+i-2)*N+1, t_COL);
     598       38813 :   vE = cgetg((l+i-2)*N+1, t_COL);
     599       74952 :   for (i = k = 1; i < l; i++)
     600             :   {
     601       36139 :     GEN L, p = gel(vp,i);
     602       36139 :     long Nval = vN[i], Zval = vZ[i], vc = cx? Z_pvalrem(cx,p,&cx): 0;
     603       36139 :     if (vc)
     604             :     {
     605        3038 :       L = idealprimedec(nf,p);
     606        3038 :       if (is_pm1(cx)) cx = NULL;
     607             :     }
     608             :     else
     609       33101 :       L = idealprimedec_limit_f(nf,p,Nval);
     610       90670 :     for (j = 1; Nval && j < lg(L); j++) /* !Nval => only cx contributes */
     611             :     {
     612       54531 :       GEN P = gel(L,j);
     613       54531 :       pari_sp av = avma;
     614       54531 :       v = idealHNF_val(x, P, Nval, Zval);
     615       54531 :       avma = av;
     616       54531 :       Nval -= v*pr_get_f(P);
     617       54531 :       v += vc * pr_get_e(P); if (!v) continue;
     618       39863 :       gel(vP,k) = P;
     619       39863 :       gel(vE,k) = utoipos(v); k++;
     620             :     }
     621       37807 :     if (vc) for (; j<lg(L); j++)
     622             :     {
     623        1668 :       GEN P = gel(L,j);
     624        1668 :       gel(vP,k) = P;
     625        1668 :       gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     626             :     }
     627             :   }
     628       38813 :   if (cx && !FA)
     629             :   { /* complete factorization */
     630        7525 :     GEN f = Z_factor(cx), cP = gel(f,1), cE = gel(f,2);
     631        7525 :     long lc = lg(cP);
     632       15743 :     for (i=1; i<lc; i++)
     633             :     {
     634        8218 :       GEN p = gel(cP,i), L = idealprimedec(nf,p);
     635        8218 :       long vc = itos(gel(cE,i));
     636       18242 :       for (j=1; j<lg(L); j++)
     637             :       {
     638       10024 :         GEN P = gel(L,j);
     639       10024 :         gel(vP,k) = P;
     640       10024 :         gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
     641             :       }
     642             :     }
     643             :   }
     644       38813 :   setlg(vP, k);
     645       38813 :   setlg(vE, k); return mkmat2(vP, vE);
     646             : }
     647             : /* true nf, x integral ideal */
     648             : static GEN
     649       38036 : idealHNF_factor(GEN nf, GEN x, ulong lim)
     650             : {
     651       38036 :   GEN cx, F = NULL;
     652       38036 :   if (lim)
     653             :   {
     654             :     GEN P, E;
     655             :     long l;
     656          42 :     F = Z_factor_limit(gcoeff(x,1,1), lim);
     657          42 :     P = gel(F,1); l = lg(P);
     658          42 :     E = gel(F,2);
     659          42 :     if (l > 1 && abscmpiu(gel(P,l-1), lim) >= 0) { setlg(P,l-1); setlg(E,l-1); }
     660             :   }
     661       38036 :   x = Q_primitive_part(x, &cx);
     662       38036 :   return idealHNF_factor_i(nf, x, cx, F);
     663             : }
     664             : /* c * vector(#L,i,L[i].e), assume results fit in ulong */
     665             : static GEN
     666        3339 : prV_e_muls(GEN L, long c)
     667             : {
     668        3339 :   long j, l = lg(L);
     669        3339 :   GEN z = cgetg(l, t_COL);
     670        3339 :   for (j = 1; j < l; j++) gel(z,j) = stoi(c * pr_get_e(gel(L,j)));
     671        3339 :   return z;
     672             : }
     673             : /* true nf, y in Q */
     674             : static GEN
     675        3472 : Q_nffactor(GEN nf, GEN y, ulong lim)
     676             : {
     677             :   GEN f, P, E;
     678             :   long l, i;
     679        3472 :   if (typ(y) == t_INT)
     680             :   {
     681        3444 :     if (!signe(y)) pari_err_DOMAIN("idealfactor", "ideal", "=",gen_0,y);
     682        3430 :     if (is_pm1(y)) return trivial_fact();
     683             :   }
     684        2506 :   y = Q_abs_shallow(y);
     685        2506 :   if (!lim) f = Q_factor(y);
     686             :   else
     687             :   {
     688          35 :     f = Q_factor_limit(y, lim);
     689          35 :     P = gel(f,1); l = lg(P);
     690          35 :     E = gel(f,2);
     691          77 :     for (i = l-1; i > 0; i--)
     692             :     {
     693          63 :       if (abscmpiu(gel(P,i), lim) < 0) break;
     694          42 :       setlg(P,i); setlg(E,i);
     695             :     }
     696             :   }
     697        2506 :   P = gel(f,1); l = lg(P); if (l == 1) return f;
     698        2492 :   E = gel(f,2);
     699        5831 :   for (i = 1; i < l; i++)
     700             :   {
     701        3339 :     gel(P,i) = idealprimedec(nf, gel(P,i));
     702        3339 :     gel(E,i) = prV_e_muls(gel(P,i), itos(gel(E,i)));
     703             :   }
     704        2492 :   settyp(P,t_VEC); P = shallowconcat1(P);
     705        2492 :   settyp(E,t_VEC); E = shallowconcat1(E);
     706        2492 :   gel(f,1) = P; settyp(P, t_COL);
     707        2492 :   gel(f,2) = E; return f;
     708             : }
     709             : 
     710             : GEN
     711       41550 : idealfactor_limit(GEN nf, GEN x, ulong lim)
     712             : {
     713       41550 :   pari_sp av = avma;
     714             :   GEN fa, y;
     715       41550 :   long tx = idealtyp(&x,&y);
     716             : 
     717       41550 :   nf = checknf(nf);
     718       41550 :   if (tx == id_PRIME)
     719             :   {
     720          49 :     if (lim && abscmpiu(pr_get_p(x), lim) >= 0) return trivial_fact();
     721          42 :     retmkmat2(mkcolcopy(x), mkcol(gen_1));
     722             :   }
     723       41501 :   if (tx == id_PRINCIPAL)
     724             :   {
     725        5649 :     y = nf_to_scalar_or_basis(nf, x);
     726        5649 :     if (typ(y) != t_COL) return gerepilecopy(av, Q_nffactor(nf, y, lim));
     727             :   }
     728       38029 :   y = idealnumden(nf, x);
     729       38029 :   fa = idealHNF_factor(nf, gel(y,1), lim);
     730       38029 :   if (!isint1(gel(y,2)))
     731           7 :     fa = famat_div_shallow(fa, idealHNF_factor(nf, gel(y,2), lim));
     732       38029 :   fa = gerepilecopy(av, fa);
     733       38029 :   return sort_factor(fa, (void*)&cmp_prime_ideal, &cmp_nodata);
     734             : }
     735             : GEN
     736       39534 : idealfactor(GEN nf, GEN x) { return idealfactor_limit(nf, x, 0); }
     737             : GEN
     738         140 : gpidealfactor(GEN nf, GEN x, GEN lim)
     739             : {
     740         140 :   ulong L = 0;
     741         140 :   if (lim)
     742             :   {
     743          70 :     if (typ(lim) != t_INT || signe(lim) < 0) pari_err_FLAG("idealfactor");
     744          70 :     L = itou(lim);
     745             :   }
     746         140 :   return idealfactor_limit(nf, x, L);
     747             : }
     748             : 
     749             : /* true nf; A is assumed to be the n-th power of an integral ideal,
     750             :  * return its n-th root; n > 1 */
     751             : static long
     752         182 : idealsqrtn_int(GEN nf, GEN A, long n, GEN *pB)
     753             : {
     754             :   GEN C, ram, vram, root;
     755             :   long i, l;
     756             : 
     757         182 :   if (typ(A) == t_INT) return Z_ispowerall(A, n, pB);
     758             :   /* compute valuations at ramified primes */
     759          91 :   ram = gel(idealfactor(nf, idealadd(nf, nf_get_diff(nf),A)), 1);
     760          91 :   l = lg(ram); vram = cgetg(l, t_VECSMALL);
     761         105 :   for (i = 1; i < l; i++)
     762             :   {
     763          14 :     long v = idealval(nf,A,gel(ram,i));
     764          14 :     if (v % n) return 0;
     765          14 :     vram[i] = v / n;
     766             :   }
     767          91 :   root = idealfactorback(nf, ram, vram, 0);
     768             :   /* remove ramified primes */
     769          91 :   if (isint1(root))
     770          77 :     root = matid(nf_get_degree(nf));
     771             :   else
     772          14 :     A = idealdivexact(nf, A, idealpows(nf,root,n));
     773          91 :   A = Q_primitive_part(A, &C);
     774          91 :   if (C)
     775             :   {
     776           0 :     if (!Z_ispowerall(C,n,&C)) return 0;
     777           0 :     if (pB) root = ZM_Z_mul(root, C);
     778             :   }
     779             : 
     780             :   /* compute final n-th root, at most degree(nf)-1 iterations */
     781         168 :   for (i = 0;; i++)
     782          77 :   {
     783         168 :     GEN J, b, a = gcoeff(A,1,1); /* A \cap Z */
     784         168 :     if (is_pm1(a)) break;
     785          91 :     if (!Z_ispowerall(a,n,&b)) return 0;
     786          77 :     J = idealadd(nf, b, A);
     787          77 :     A = idealdivexact(nf, idealpows(nf,J,n), A);
     788          77 :     if (pB) root = odd(i)? idealdivexact(nf, root, J): idealmul(nf, root, J);
     789             :   }
     790         154 :   if (pB) *pB = root;
     791          77 :   return 1;
     792             : }
     793             : 
     794             : /* A is assumed to be the n-th power of an ideal in nf
     795             :  returns its n-th root. */
     796             : long
     797         105 : idealispower(GEN nf, GEN A, long n, GEN *pB)
     798             : {
     799         105 :   pari_sp av = avma;
     800             :   GEN v, N, D;
     801         105 :   nf = checknf(nf);
     802         105 :   if (n <= 0) pari_err_DOMAIN("idealispower", "n", "<=", gen_0, stoi(n));
     803         105 :   if (n == 1) { if (pB) *pB = idealhnf(nf,A); return 1; }
     804          98 :   v = idealnumden(nf,A);
     805          98 :   if (gequal0(gel(v,1))) { avma = av; if (pB) *pB = cgetg(1,t_MAT); return 1; }
     806          98 :   if (!idealsqrtn_int(nf, gel(v,1), n, pB? &N: NULL)) return 0;
     807          84 :   if (!idealsqrtn_int(nf, gel(v,2), n, pB? &D: NULL)) return 0;
     808          84 :   if (pB) *pB = gerepileupto(av, idealdiv(nf,N,D)); else avma = av;
     809          84 :   return 1;
     810             : }
     811             : 
     812             : /* x t_INT or integral non-0 ideal in HNF */
     813             : static GEN
     814        3192 : idealredmodpower_i(GEN nf, GEN x, ulong k, ulong B)
     815             : {
     816             :   GEN cx, y, U, N, F, Q;
     817             :   long nF;
     818        3192 :   if (typ(x) == t_INT)
     819             :   {
     820        2408 :     if (!signe(x) || is_pm1(x)) return gen_1;
     821         693 :     F = Z_factor_limit(x, B);
     822         693 :     gel(F,2) = gdiventgs(gel(F,2), k);
     823         693 :     return ginv(factorback(F));
     824             :   }
     825         784 :   N = gcoeff(x,1,1); if (is_pm1(N)) return gen_1;
     826         777 :   F = Z_factor_limit(N, B); nF=lg(gel(F,1))-1;
     827         777 :   if (BPSW_psp(gcoeff(F,nF,1))) U = NULL;
     828             :   else
     829             :   {
     830          77 :     GEN M = powii(gcoeff(F,nF,1), gcoeff(F,nF,2));
     831          77 :     y = hnfmodid(x, M); /* coprime part to B! */
     832          77 :     if (!idealispower(nf, y, k, &U)) U = NULL;
     833          77 :     x = hnfmodid(x, diviiexact(N, M));
     834          77 :     setlg(gel(F,1), nF); /* remove last entry (unfactored part) */
     835          77 :     setlg(gel(F,2), nF);
     836             :   }
     837             :   /* x = B-smooth part of initial x */
     838         777 :   x = Q_primitive_part(x, &cx);
     839         777 :   F = idealHNF_factor_i(nf, x, cx, F);
     840         777 :   gel(F,2) = gdiventgs(gel(F,2), k);
     841         777 :   Q = idealfactorback(nf, gel(F,1), gel(F,2), 0);
     842         777 :   if (U) Q = idealmul(nf,Q,U);
     843         777 :   if (typ(Q) == t_INT) return Q;
     844         756 :   y = idealred_elt(nf, idealHNF_inv_Z(nf, Q));
     845         756 :   return gdiv(y, gcoeff(Q,1,1));
     846             : }
     847             : GEN
     848        1603 : idealredmodpower(GEN nf, GEN x, ulong n, ulong B)
     849             : {
     850        1603 :   pari_sp av = avma;
     851             :   GEN a, b;
     852        1603 :   nf = checknf(nf);
     853        1603 :   if (!n) pari_err_DOMAIN("idealredmodpower","n", "=", gen_0, gen_0);
     854        1603 :   x = idealnumden(nf, x);
     855        1603 :   a = gel(x,1);
     856        1603 :   if (isintzero(a)) { avma = av; return gen_1; }
     857        1596 :   a = idealredmodpower_i(nf, gel(x,1), n, B);
     858        1596 :   b = idealredmodpower_i(nf, gel(x,2), n, B);
     859        1596 :   if (!isint1(b)) a = nf_to_scalar_or_basis(nf, nfdiv(nf, a, b));
     860        1596 :   return gerepilecopy(av, a);
     861             : }
     862             : 
     863             : /* P prime ideal in idealprimedec format. Return valuation(A) at P */
     864             : long
     865      516108 : idealval(GEN nf, GEN A, GEN P)
     866             : {
     867      516108 :   pari_sp av = avma;
     868             :   GEN a, p, cA;
     869      516108 :   long vcA, v, Zval, tx = idealtyp(&A,&a);
     870             : 
     871      516108 :   if (tx == id_PRINCIPAL) return nfval(nf,A,P);
     872      511236 :   checkprid(P);
     873      511236 :   if (tx == id_PRIME) return pr_equal(P, A)? 1: 0;
     874             :   /* id_MAT */
     875      511208 :   nf = checknf(nf);
     876      511208 :   A = Q_primitive_part(A, &cA);
     877      511208 :   p = pr_get_p(P);
     878      511208 :   vcA = cA? Q_pval(cA,p): 0;
     879      511208 :   if (pr_is_inert(P)) { avma = av; return vcA; }
     880      502682 :   Zval = Z_pval(gcoeff(A,1,1), p);
     881      502682 :   if (!Zval) v = 0;
     882             :   else
     883             :   {
     884      172272 :     long Nval = idealHNF_norm_pval(A, p, Zval);
     885      172272 :     v = idealHNF_val(A, P, Nval, Zval);
     886             :   }
     887      502682 :   avma = av; return vcA? v + vcA*pr_get_e(P): v;
     888             : }
     889             : GEN
     890        6573 : gpidealval(GEN nf, GEN ix, GEN P)
     891             : {
     892        6573 :   long v = idealval(nf,ix,P);
     893        6573 :   return v == LONG_MAX? mkoo(): stoi(v);
     894             : }
     895             : 
     896             : /* gcd and generalized Bezout */
     897             : 
     898             : GEN
     899       61285 : idealadd(GEN nf, GEN x, GEN y)
     900             : {
     901       61285 :   pari_sp av = avma;
     902             :   long tx, ty;
     903             :   GEN z, a, dx, dy, dz;
     904             : 
     905       61285 :   tx = idealtyp(&x,&z);
     906       61285 :   ty = idealtyp(&y,&z); nf = checknf(nf);
     907       61285 :   if (tx != id_MAT) x = idealhnf_shallow(nf,x);
     908       61285 :   if (ty != id_MAT) y = idealhnf_shallow(nf,y);
     909       61285 :   if (lg(x) == 1) return gerepilecopy(av,y);
     910       61271 :   if (lg(y) == 1) return gerepilecopy(av,x); /* check for 0 ideal */
     911       60970 :   dx = Q_denom(x);
     912       60970 :   dy = Q_denom(y); dz = lcmii(dx,dy);
     913       60970 :   if (is_pm1(dz)) dz = NULL; else {
     914       12579 :     x = Q_muli_to_int(x, dz);
     915       12579 :     y = Q_muli_to_int(y, dz);
     916             :   }
     917       60970 :   a = gcdii(gcoeff(x,1,1), gcoeff(y,1,1));
     918       60970 :   if (is_pm1(a))
     919             :   {
     920       28321 :     long N = lg(x)-1;
     921       28321 :     if (!dz) { avma = av; return matid(N); }
     922        3555 :     return gerepileupto(av, scalarmat(ginv(dz), N));
     923             :   }
     924       32649 :   z = ZM_hnfmodid(shallowconcat(x,y), a);
     925       32649 :   if (dz) z = RgM_Rg_div(z,dz);
     926       32649 :   return gerepileupto(av,z);
     927             : }
     928             : 
     929             : static GEN
     930          28 : trivial_merge(GEN x)
     931          28 : { return (lg(x) == 1 || !is_pm1(gcoeff(x,1,1)))? NULL: gen_1; }
     932             : /* true nf */
     933             : static GEN
     934      127883 : _idealaddtoone(GEN nf, GEN x, GEN y, long red)
     935             : {
     936             :   GEN a;
     937      127883 :   long tx = idealtyp(&x, &a/*junk*/);
     938      127883 :   long ty = idealtyp(&y, &a/*junk*/);
     939             :   long ea;
     940      127883 :   if (tx != id_MAT) x = idealhnf_shallow(nf, x);
     941      127883 :   if (ty != id_MAT) y = idealhnf_shallow(nf, y);
     942      127883 :   if (lg(x) == 1)
     943          14 :     a = trivial_merge(y);
     944      127869 :   else if (lg(y) == 1)
     945          14 :     a = trivial_merge(x);
     946             :   else
     947      127855 :     a = hnfmerge_get_1(x, y);
     948      127883 :   if (!a) pari_err_COPRIME("idealaddtoone",x,y);
     949      127869 :   if (red && (ea = gexpo(a)) > 10)
     950             :   {
     951        6498 :     GEN b = (typ(a) == t_COL)? a: scalarcol_shallow(a, nf_get_degree(nf));
     952        6498 :     b = ZC_reducemodlll(b, idealHNF_mul(nf,x,y));
     953        6498 :     if (gexpo(b) < ea) a = b;
     954             :   }
     955      127869 :   return a;
     956             : }
     957             : /* true nf */
     958             : GEN
     959       14350 : idealaddtoone_i(GEN nf, GEN x, GEN y)
     960       14350 : { return _idealaddtoone(nf, x, y, 1); }
     961             : /* true nf */
     962             : GEN
     963      113533 : idealaddtoone_raw(GEN nf, GEN x, GEN y)
     964      113533 : { return _idealaddtoone(nf, x, y, 0); }
     965             : 
     966             : GEN
     967          98 : idealaddtoone(GEN nf, GEN x, GEN y)
     968             : {
     969          98 :   GEN z = cgetg(3,t_VEC), a;
     970          98 :   pari_sp av = avma;
     971          98 :   nf = checknf(nf);
     972          98 :   a = gerepileupto(av, idealaddtoone_i(nf,x,y));
     973          84 :   gel(z,1) = a;
     974          84 :   gel(z,2) = typ(a) == t_COL? Z_ZC_sub(gen_1,a): subui(1,a);
     975          84 :   return z;
     976             : }
     977             : 
     978             : /* assume elements of list are integral ideals */
     979             : GEN
     980          35 : idealaddmultoone(GEN nf, GEN list)
     981             : {
     982          35 :   pari_sp av = avma;
     983          35 :   long N, i, l, nz, tx = typ(list);
     984             :   GEN H, U, perm, L;
     985             : 
     986          35 :   nf = checknf(nf); N = nf_get_degree(nf);
     987          35 :   if (!is_vec_t(tx)) pari_err_TYPE("idealaddmultoone",list);
     988          35 :   l = lg(list);
     989          35 :   L = cgetg(l, t_VEC);
     990          35 :   if (l == 1)
     991           0 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
     992          35 :   nz = 0; /* number of non-zero ideals in L */
     993          98 :   for (i=1; i<l; i++)
     994             :   {
     995          70 :     GEN I = gel(list,i);
     996          70 :     if (typ(I) != t_MAT) I = idealhnf_shallow(nf,I);
     997          70 :     if (lg(I) != 1)
     998             :     {
     999          42 :       nz++; RgM_check_ZM(I,"idealaddmultoone");
    1000          35 :       if (lgcols(I) != N+1) pari_err_TYPE("idealaddmultoone [not an ideal]", I);
    1001             :     }
    1002          63 :     gel(L,i) = I;
    1003             :   }
    1004          28 :   H = ZM_hnfperm(shallowconcat1(L), &U, &perm);
    1005          28 :   if (lg(H) == 1 || !equali1(gcoeff(H,1,1)))
    1006           7 :     pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
    1007          49 :   for (i=1; i<=N; i++)
    1008          49 :     if (perm[i] == 1) break;
    1009          21 :   U = gel(U,(nz-1)*N + i); /* (L[1]|...|L[nz]) U = 1 */
    1010          21 :   nz = 0;
    1011          63 :   for (i=1; i<l; i++)
    1012             :   {
    1013          42 :     GEN c = gel(L,i);
    1014          42 :     if (lg(c) == 1)
    1015          14 :       c = gen_0;
    1016             :     else {
    1017          28 :       c = ZM_ZC_mul(c, vecslice(U, nz*N + 1, (nz+1)*N));
    1018          28 :       nz++;
    1019             :     }
    1020          42 :     gel(L,i) = c;
    1021             :   }
    1022          21 :   return gerepilecopy(av, L);
    1023             : }
    1024             : 
    1025             : /* multiplication */
    1026             : 
    1027             : /* x integral ideal (without archimedean component) in HNF form
    1028             :  * y = [a,alpha] corresponds to the integral ideal aZ_K+alpha Z_K, a in Z,
    1029             :  * alpha a ZV or a ZM (multiplication table). Multiply them */
    1030             : static GEN
    1031     1931953 : idealHNF_mul_two(GEN nf, GEN x, GEN y)
    1032             : {
    1033     1931953 :   GEN m, a = gel(y,1), alpha = gel(y,2);
    1034             :   long i, N;
    1035             : 
    1036     1931953 :   if (typ(alpha) != t_MAT)
    1037             :   {
    1038     1645266 :     alpha = zk_scalar_or_multable(nf, alpha);
    1039     1645266 :     if (typ(alpha) == t_INT) /* e.g. y inert ? 0 should not (but may) occur */
    1040        3312 :       return signe(a)? ZM_Z_mul(x, gcdii(a, alpha)): cgetg(1,t_MAT);
    1041             :   }
    1042     1928641 :   N = lg(x)-1; m = cgetg((N<<1)+1,t_MAT);
    1043     1928641 :   for (i=1; i<=N; i++) gel(m,i)   = ZM_ZC_mul(alpha,gel(x,i));
    1044     1928641 :   for (i=1; i<=N; i++) gel(m,i+N) = ZC_Z_mul(gel(x,i), a);
    1045     1928641 :   return ZM_hnfmodid(m, mulii(a, gcoeff(x,1,1)));
    1046             : }
    1047             : 
    1048             : /* Assume ix and iy are integral in HNF form [NOT extended]. Not memory clean.
    1049             :  * HACK: ideal in iy can be of the form [a,b], a in Z, b in Z_K */
    1050             : GEN
    1051      942126 : idealHNF_mul(GEN nf, GEN x, GEN y)
    1052             : {
    1053             :   GEN z;
    1054      942126 :   if (typ(y) == t_VEC)
    1055      857379 :     z = idealHNF_mul_two(nf,x,y);
    1056             :   else
    1057             :   { /* reduce one ideal to two-elt form. The smallest */
    1058       84747 :     GEN xZ = gcoeff(x,1,1), yZ = gcoeff(y,1,1);
    1059       84747 :     if (cmpii(xZ, yZ) < 0)
    1060             :     {
    1061       31061 :       if (is_pm1(xZ)) return gcopy(y);
    1062       20416 :       z = idealHNF_mul_two(nf, y, mat_ideal_two_elt(nf,x));
    1063             :     }
    1064             :     else
    1065             :     {
    1066       53686 :       if (is_pm1(yZ)) return gcopy(x);
    1067       28780 :       z = idealHNF_mul_two(nf, x, mat_ideal_two_elt(nf,y));
    1068             :     }
    1069             :   }
    1070      906575 :   return z;
    1071             : }
    1072             : 
    1073             : /* operations on elements in factored form */
    1074             : 
    1075             : GEN
    1076       78251 : famat_mul_shallow(GEN f, GEN g)
    1077             : {
    1078       78251 :   if (typ(f) != t_MAT) f = to_famat_shallow(f,gen_1);
    1079       78251 :   if (typ(g) != t_MAT) g = to_famat_shallow(g,gen_1);
    1080       78251 :   if (lgcols(f) == 1) return g;
    1081       66225 :   if (lgcols(g) == 1) return f;
    1082      132324 :   return mkmat2(shallowconcat(gel(f,1), gel(g,1)),
    1083      132324 :                 shallowconcat(gel(f,2), gel(g,2)));
    1084             : }
    1085             : GEN
    1086       59241 : famat_mulpow_shallow(GEN f, GEN g, GEN e)
    1087             : {
    1088       59241 :   if (!signe(e)) return f;
    1089       59115 :   return famat_mul_shallow(f, famat_pow_shallow(g, e));
    1090             : }
    1091             : 
    1092             : GEN
    1093        4606 : famat_mulpows_shallow(GEN f, GEN g, long e)
    1094             : {
    1095        4606 :   if (e==0) return f;
    1096        2960 :   return famat_mul_shallow(f, famat_pows_shallow(g, e));
    1097             : }
    1098             : 
    1099             : GEN
    1100           7 : famat_div_shallow(GEN f, GEN g)
    1101           7 : { return famat_mul_shallow(f, famat_inv_shallow(g)); }
    1102             : 
    1103             : GEN
    1104           0 : to_famat(GEN x, GEN y) { retmkmat2(mkcolcopy(x), mkcolcopy(y)); }
    1105             : GEN
    1106      879807 : to_famat_shallow(GEN x, GEN y) { return mkmat2(mkcol(x), mkcol(y)); }
    1107             : 
    1108             : /* concat the single elt x; not gconcat since x may be a t_COL */
    1109             : static GEN
    1110       28968 : append(GEN v, GEN x)
    1111             : {
    1112       28968 :   long i, l = lg(v);
    1113       28968 :   GEN w = cgetg(l+1, typ(v));
    1114       28968 :   for (i=1; i<l; i++) gel(w,i) = gcopy(gel(v,i));
    1115       28968 :   gel(w,i) = gcopy(x); return w;
    1116             : }
    1117             : /* add x^1 to famat f */
    1118             : static GEN
    1119       72640 : famat_add(GEN f, GEN x)
    1120             : {
    1121       72640 :   GEN h = cgetg(3,t_MAT);
    1122       72640 :   if (lgcols(f) == 1)
    1123             :   {
    1124       43672 :     gel(h,1) = mkcolcopy(x);
    1125       43672 :     gel(h,2) = mkcol(gen_1);
    1126             :   }
    1127             :   else
    1128             :   {
    1129       28968 :     gel(h,1) = append(gel(f,1), x);
    1130       28968 :     gel(h,2) = gconcat(gel(f,2), gen_1);
    1131             :   }
    1132       72640 :   return h;
    1133             : }
    1134             : 
    1135             : GEN
    1136       79133 : famat_mul(GEN f, GEN g)
    1137             : {
    1138             :   GEN h;
    1139       79133 :   if (typ(g) != t_MAT) {
    1140       72640 :     if (typ(f) == t_MAT) return famat_add(f, g);
    1141           0 :     h = cgetg(3, t_MAT);
    1142           0 :     gel(h,1) = mkcol2(gcopy(f), gcopy(g));
    1143           0 :     gel(h,2) = mkcol2(gen_1, gen_1);
    1144             :   }
    1145        6493 :   if (typ(f) != t_MAT) return famat_add(g, f);
    1146        6493 :   if (lgcols(f) == 1) return gcopy(g);
    1147        4447 :   if (lgcols(g) == 1) return gcopy(f);
    1148        1955 :   h = cgetg(3,t_MAT);
    1149        1955 :   gel(h,1) = gconcat(gel(f,1), gel(g,1));
    1150        1955 :   gel(h,2) = gconcat(gel(f,2), gel(g,2));
    1151        1955 :   return h;
    1152             : }
    1153             : 
    1154             : GEN
    1155       15737 : famat_sqr(GEN f)
    1156             : {
    1157             :   GEN h;
    1158       15737 :   if (typ(f) != t_MAT) return to_famat(f,gen_2);
    1159       15737 :   if (lgcols(f) == 1) return gcopy(f);
    1160       11526 :   h = cgetg(3,t_MAT);
    1161       11526 :   gel(h,1) = gcopy(gel(f,1));
    1162       11526 :   gel(h,2) = gmul2n(gel(f,2),1);
    1163       11526 :   return h;
    1164             : }
    1165             : 
    1166             : GEN
    1167       27055 : famat_inv_shallow(GEN f)
    1168             : {
    1169       27055 :   if (typ(f) != t_MAT) return to_famat_shallow(f,gen_m1);
    1170          42 :   if (lgcols(f) == 1) return f;
    1171          42 :   return mkmat2(gel(f,1), ZC_neg(gel(f,2)));
    1172             : }
    1173             : GEN
    1174       11095 : famat_inv(GEN f)
    1175             : {
    1176       11095 :   if (typ(f) != t_MAT) return to_famat(f,gen_m1);
    1177       11095 :   if (lgcols(f) == 1) return gcopy(f);
    1178        4173 :   retmkmat2(gcopy(gel(f,1)), ZC_neg(gel(f,2)));
    1179             : }
    1180             : GEN
    1181        1174 : famat_pow(GEN f, GEN n)
    1182             : {
    1183        1174 :   if (typ(f) != t_MAT) return to_famat(f,n);
    1184        1174 :   if (lgcols(f) == 1) return gcopy(f);
    1185           0 :   retmkmat2(gcopy(gel(f,1)), ZC_Z_mul(gel(f,2),n));
    1186             : }
    1187             : GEN
    1188       59115 : famat_pow_shallow(GEN f, GEN n)
    1189             : {
    1190       59115 :   if (is_pm1(n)) return signe(n) > 0? f: famat_inv_shallow(f);
    1191       30310 :   if (typ(f) != t_MAT) return to_famat_shallow(f,n);
    1192         168 :   if (lgcols(f) == 1) return f;
    1193         168 :   return mkmat2(gel(f,1), ZC_Z_mul(gel(f,2),n));
    1194             : }
    1195             : 
    1196             : GEN
    1197        2960 : famat_pows_shallow(GEN f, long n)
    1198             : {
    1199        2960 :   if (n==1) return f;
    1200        1330 :   if (n==-1) return famat_inv_shallow(f);
    1201        1197 :   if (typ(f) != t_MAT) return to_famat_shallow(f, stoi(n));
    1202        1112 :   if (lgcols(f) == 1) return f;
    1203        1112 :   return mkmat2(gel(f,1), ZC_z_mul(gel(f,2),n));
    1204             : }
    1205             : 
    1206             : GEN
    1207           0 : famat_Z_gcd(GEN M, GEN n)
    1208             : {
    1209           0 :   pari_sp av=avma;
    1210           0 :   long i, j, l=lgcols(M);
    1211           0 :   GEN F=cgetg(3,t_MAT);
    1212           0 :   gel(F,1)=cgetg(l,t_COL);
    1213           0 :   gel(F,2)=cgetg(l,t_COL);
    1214           0 :   for (i=1, j=1; i<l; i++)
    1215             :   {
    1216           0 :     GEN p = gcoeff(M,i,1);
    1217           0 :     GEN e = gminsg(Z_pval(n,p),gcoeff(M,i,2));
    1218           0 :     if (signe(e))
    1219             :     {
    1220           0 :       gcoeff(F,j,1)=p;
    1221           0 :       gcoeff(F,j,2)=e;
    1222           0 :       j++;
    1223             :     }
    1224             :   }
    1225           0 :   setlg(gel(F,1),j); setlg(gel(F,2),j);
    1226           0 :   return gerepilecopy(av,F);
    1227             : }
    1228             : 
    1229             : /* x assumed to be a t_MATs (factorization matrix), or compatible with
    1230             :  * the element_* functions. */
    1231             : static GEN
    1232       26314 : ext_sqr(GEN nf, GEN x)
    1233       26314 : { return (typ(x)==t_MAT)? famat_sqr(x): nfsqr(nf, x); }
    1234             : static GEN
    1235      113839 : ext_mul(GEN nf, GEN x, GEN y)
    1236      113839 : { return (typ(x)==t_MAT)? famat_mul(x,y): nfmul(nf, x, y); }
    1237             : static GEN
    1238       10955 : ext_inv(GEN nf, GEN x)
    1239       10955 : { return (typ(x)==t_MAT)? famat_inv(x): nfinv(nf, x); }
    1240             : static GEN
    1241        1174 : ext_pow(GEN nf, GEN x, GEN n)
    1242        1174 : { return (typ(x)==t_MAT)? famat_pow(x,n): nfpow(nf, x, n); }
    1243             : 
    1244             : GEN
    1245           0 : famat_to_nf(GEN nf, GEN f)
    1246             : {
    1247             :   GEN t, x, e;
    1248             :   long i;
    1249           0 :   if (lgcols(f) == 1) return gen_1;
    1250           0 :   x = gel(f,1);
    1251           0 :   e = gel(f,2);
    1252           0 :   t = nfpow(nf, gel(x,1), gel(e,1));
    1253           0 :   for (i=lg(x)-1; i>1; i--)
    1254           0 :     t = nfmul(nf, t, nfpow(nf, gel(x,i), gel(e,i)));
    1255           0 :   return t;
    1256             : }
    1257             : 
    1258             : GEN
    1259       18515 : famat_reduce(GEN fa)
    1260             : {
    1261             :   GEN E, G, L, g, e;
    1262             :   long i, k, l;
    1263             : 
    1264       18515 :   if (lgcols(fa) == 1) return fa;
    1265       15953 :   g = gel(fa,1); l = lg(g);
    1266       15953 :   e = gel(fa,2);
    1267       15953 :   L = gen_indexsort(g, (void*)&cmp_universal, &cmp_nodata);
    1268       15953 :   G = cgetg(l, t_COL);
    1269       15953 :   E = cgetg(l, t_COL);
    1270             :   /* merge */
    1271       39283 :   for (k=i=1; i<l; i++,k++)
    1272             :   {
    1273       23330 :     gel(G,k) = gel(g,L[i]);
    1274       23330 :     gel(E,k) = gel(e,L[i]);
    1275       23330 :     if (k > 1 && gidentical(gel(G,k), gel(G,k-1)))
    1276             :     {
    1277         735 :       gel(E,k-1) = addii(gel(E,k), gel(E,k-1));
    1278         735 :       k--;
    1279             :     }
    1280             :   }
    1281             :   /* kill 0 exponents */
    1282       15953 :   l = k;
    1283       38548 :   for (k=i=1; i<l; i++)
    1284       22595 :     if (!gequal0(gel(E,i)))
    1285             :     {
    1286       21559 :       gel(G,k) = gel(G,i);
    1287       21559 :       gel(E,k) = gel(E,i); k++;
    1288             :     }
    1289       15953 :   setlg(G, k);
    1290       15953 :   setlg(E, k); return mkmat2(G,E);
    1291             : }
    1292             : 
    1293             : GEN
    1294       14679 : famatsmall_reduce(GEN fa)
    1295             : {
    1296             :   GEN E, G, L, g, e;
    1297             :   long i, k, l;
    1298       14679 :   if (lgcols(fa) == 1) return fa;
    1299       14679 :   g = gel(fa,1); l = lg(g);
    1300       14679 :   e = gel(fa,2);
    1301       14679 :   L = vecsmall_indexsort(g);
    1302       14680 :   G = cgetg(l, t_VECSMALL);
    1303       14680 :   E = cgetg(l, t_VECSMALL);
    1304             :   /* merge */
    1305      131238 :   for (k=i=1; i<l; i++,k++)
    1306             :   {
    1307      116558 :     G[k] = g[L[i]];
    1308      116558 :     E[k] = e[L[i]];
    1309      116558 :     if (k > 1 && G[k] == G[k-1])
    1310             :     {
    1311        7071 :       E[k-1] += E[k];
    1312        7071 :       k--;
    1313             :     }
    1314             :   }
    1315             :   /* kill 0 exponents */
    1316       14680 :   l = k;
    1317      124167 :   for (k=i=1; i<l; i++)
    1318      109487 :     if (E[i])
    1319             :     {
    1320      105749 :       G[k] = G[i];
    1321      105749 :       E[k] = E[i]; k++;
    1322             :     }
    1323       14680 :   setlg(G, k);
    1324       14680 :   setlg(E, k); return mkmat2(G,E);
    1325             : }
    1326             : 
    1327             : GEN
    1328       54663 : ZM_famat_limit(GEN fa, GEN limit)
    1329             : {
    1330             :   pari_sp av;
    1331             :   GEN E, G, g, e, r;
    1332             :   long i, k, l, n, lG;
    1333             : 
    1334       54663 :   if (lgcols(fa) == 1) return fa;
    1335       54656 :   g = gel(fa,1); l = lg(g);
    1336       54656 :   e = gel(fa,2);
    1337      121639 :   for(n=0, i=1; i<l; i++)
    1338       66983 :     if (cmpii(gel(g,i),limit)<=0) n++;
    1339       54656 :   lG = n<l-1 ? n+2 : n+1;
    1340       54656 :   G = cgetg(lG, t_COL);
    1341       54656 :   E = cgetg(lG, t_COL);
    1342       54656 :   av = avma;
    1343      121639 :   for (i=1, k=1, r = gen_1; i<l; i++)
    1344             :   {
    1345       66983 :     if (cmpii(gel(g,i),limit)<=0)
    1346             :     {
    1347       66892 :       gel(G,k) = gel(g,i);
    1348       66892 :       gel(E,k) = gel(e,i);
    1349       66892 :       k++;
    1350          91 :     } else r = mulii(r, powii(gel(g,i), gel(e,i)));
    1351             :   }
    1352       54656 :   if (k<i)
    1353             :   {
    1354          91 :     gel(G, k) = gerepileuptoint(av, r);
    1355          91 :     gel(E, k) = gen_1;
    1356             :   }
    1357       54656 :   return mkmat2(G,E);
    1358             : }
    1359             : 
    1360             : /* assume pr has degree 1 and coprime to Q_denom(x) */
    1361             : static GEN
    1362        4754 : to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1363             : {
    1364        4754 :   GEN d, r, p = modpr_get_p(modpr);
    1365        4754 :   x = nf_to_scalar_or_basis(nf,x);
    1366        4754 :   if (typ(x) != t_COL) return Rg_to_Fp(x,p);
    1367        4446 :   x = Q_remove_denom(x, &d);
    1368        4446 :   r = zk_to_Fq(x, modpr);
    1369        4446 :   if (d) r = Fp_div(r, d, p);
    1370        4446 :   return r;
    1371             : }
    1372             : 
    1373             : /* pr coprime to all denominators occurring in x */
    1374             : static GEN
    1375         623 : famat_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1376             : {
    1377         623 :   GEN p = modpr_get_p(modpr);
    1378         623 :   GEN t = NULL, g = gel(x,1), e = gel(x,2), q = subiu(p,1);
    1379         623 :   long i, l = lg(g);
    1380        1966 :   for (i = 1; i < l; i++)
    1381             :   {
    1382        1343 :     GEN n = modii(gel(e,i), q);
    1383        1343 :     if (signe(n))
    1384             :     {
    1385        1343 :       GEN h = to_Fp_coprime(nf, gel(g,i), modpr);
    1386        1343 :       h = Fp_pow(h, n, p);
    1387        1343 :       t = t? Fp_mul(t, h, p): h;
    1388             :     }
    1389             :   }
    1390         623 :   return t? modii(t, p): gen_1;
    1391             : }
    1392             : 
    1393             : /* cf famat_to_nf_modideal_coprime, modpr attached to prime of degree 1 */
    1394             : GEN
    1395        4034 : nf_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
    1396             : {
    1397        4034 :   return typ(x)==t_MAT? famat_to_Fp_coprime(nf, x, modpr)
    1398        4034 :                       : to_Fp_coprime(nf, x, modpr);
    1399             : }
    1400             : 
    1401             : static long
    1402      135815 : zk_pvalrem(GEN x, GEN p, GEN *py)
    1403      135815 : { return (typ(x) == t_INT)? Z_pvalrem(x, p, py): ZV_pvalrem(x, p, py); }
    1404             : /* x a QC or Q. Return a ZC or Z, whose content is coprime to Z. Set v, dx
    1405             :  * such that x = p^v (newx / dx); dx = NULL if 1 */
    1406             : static GEN
    1407      263418 : nf_remove_denom_p(GEN nf, GEN x, GEN p, GEN *pdx, long *pv)
    1408             : {
    1409             :   long vcx;
    1410             :   GEN dx;
    1411      263418 :   x = nf_to_scalar_or_basis(nf, x);
    1412      263418 :   x = Q_remove_denom(x, &dx);
    1413      263418 :   if (dx)
    1414             :   {
    1415      170940 :     vcx = - Z_pvalrem(dx, p, &dx);
    1416      170940 :     if (!vcx) vcx = zk_pvalrem(x, p, &x);
    1417      170940 :     if (isint1(dx)) dx = NULL;
    1418             :   }
    1419             :   else
    1420             :   {
    1421       92478 :     vcx = zk_pvalrem(x, p, &x);
    1422       92478 :     dx = NULL;
    1423             :   }
    1424      263418 :   *pv = vcx;
    1425      263418 :   *pdx = dx; return x;
    1426             : }
    1427             : /* x = b^e/p^(e-1) in Z_K; x = 0 mod p/pr^e, (x,pr) = 1. Return NULL
    1428             :  * if p inert (instead of 1) */
    1429             : static GEN
    1430       62069 : p_makecoprime(GEN pr)
    1431             : {
    1432       62069 :   GEN B = pr_get_tau(pr), b;
    1433             :   long i, e;
    1434             : 
    1435       62069 :   if (typ(B) == t_INT) return NULL;
    1436       61929 :   b = gel(B,1); /* B = multiplication table by b */
    1437       61929 :   e = pr_get_e(pr);
    1438       61929 :   if (e == 1) return b;
    1439             :   /* one could also divide (exactly) by p in each iteration */
    1440       17150 :   for (i = 1; i < e; i++) b = ZM_ZC_mul(B, b);
    1441       17150 :   return ZC_Z_divexact(b, powiu(pr_get_p(pr), e-1));
    1442             : }
    1443             : 
    1444             : /* Compute A = prod g[i]^e[i] mod pr^k, assuming (A, pr) = 1.
    1445             :  * Method: modify each g[i] so that it becomes coprime to pr,
    1446             :  * g[i] *= (b/p)^v_pr(g[i]), where b/p = pr^(-1) times something integral
    1447             :  * and prime to p; globally, we multiply by (b/p)^v_pr(A) = 1.
    1448             :  * Optimizations:
    1449             :  * 1) remove all powers of p from contents, and consider extra generator p^vp;
    1450             :  * modified as p * (b/p)^e = b^e / p^(e-1)
    1451             :  * 2) remove denominators, coprime to p, by multiplying by inverse mod prk\cap Z
    1452             :  *
    1453             :  * EX = multiple of exponent of (O_K / pr^k)^* used to reduce the product in
    1454             :  * case the e[i] are large */
    1455             : GEN
    1456      111640 : famat_makecoprime(GEN nf, GEN g, GEN e, GEN pr, GEN prk, GEN EX)
    1457             : {
    1458      111640 :   GEN G, E, t, vp = NULL, p = pr_get_p(pr), prkZ = gcoeff(prk, 1,1);
    1459      111640 :   long i, l = lg(g);
    1460             : 
    1461      111640 :   G = cgetg(l+1, t_VEC);
    1462      111640 :   E = cgetg(l+1, t_VEC); /* l+1: room for "modified p" */
    1463      375058 :   for (i=1; i < l; i++)
    1464             :   {
    1465             :     long vcx;
    1466      263418 :     GEN dx, x = nf_remove_denom_p(nf, gel(g,i), p, &dx, &vcx);
    1467      263418 :     if (vcx) /* = v_p(content(g[i])) */
    1468             :     {
    1469      129262 :       GEN a = mulsi(vcx, gel(e,i));
    1470      129262 :       vp = vp? addii(vp, a): a;
    1471             :     }
    1472             :     /* x integral, content coprime to p; dx coprime to p */
    1473      263418 :     if (typ(x) == t_INT)
    1474             :     { /* x coprime to p, hence to pr */
    1475       38595 :       x = modii(x, prkZ);
    1476       38595 :       if (dx) x = Fp_div(x, dx, prkZ);
    1477             :     }
    1478             :     else
    1479             :     {
    1480      224823 :       (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
    1481      224823 :       x = ZC_hnfrem(FpC_red(x,prkZ), prk);
    1482      224823 :       if (dx) x = FpC_Fp_mul(x, Fp_inv(dx,prkZ), prkZ);
    1483             :     }
    1484      263418 :     gel(G,i) = x;
    1485      263418 :     gel(E,i) = gel(e,i);
    1486             :   }
    1487             : 
    1488      111640 :   t = vp? p_makecoprime(pr): NULL;
    1489      111640 :   if (!t)
    1490             :   { /* no need for extra generator */
    1491       49711 :     setlg(G,l);
    1492       49711 :     setlg(E,l);
    1493             :   }
    1494             :   else
    1495             :   {
    1496       61929 :     gel(G,i) = FpC_red(t, prkZ);
    1497       61929 :     gel(E,i) = vp;
    1498             :   }
    1499      111640 :   return famat_to_nf_modideal_coprime(nf, G, E, prk, EX);
    1500             : }
    1501             : 
    1502             : /* prod g[i]^e[i] mod bid, assume (g[i], id) = 1 and 1 < lg(g) <= lg(e) */
    1503             : GEN
    1504       11039 : famat_to_nf_moddivisor(GEN nf, GEN g, GEN e, GEN bid)
    1505             : {
    1506       11039 :   GEN t, cyc = bid_get_cyc(bid);
    1507       11039 :   if (lg(cyc) == 1)
    1508           0 :     t = gen_1;
    1509             :   else
    1510       11039 :     t = famat_to_nf_modideal_coprime(nf, g, e, bid_get_ideal(bid), gel(cyc,1));
    1511       11039 :   return set_sign_mod_divisor(nf, mkmat2(g,e), t, bid_get_sarch(bid));
    1512             : }
    1513             : 
    1514             : GEN
    1515      197274 : vecmul(GEN x, GEN y)
    1516             : {
    1517      197274 :   if (is_scalar_t(typ(x))) return gmul(x,y);
    1518       17822 :   pari_APPLY_same(vecmul(gel(x,i), gel(y,i)))
    1519             : }
    1520             : 
    1521             : GEN
    1522           0 : vecinv(GEN x)
    1523             : {
    1524           0 :   if (is_scalar_t(typ(x))) return ginv(x);
    1525           0 :   pari_APPLY_same(vecinv(gel(x,i)))
    1526             : }
    1527             : 
    1528             : GEN
    1529       15729 : vecpow(GEN x, GEN n)
    1530             : {
    1531       15729 :   if (is_scalar_t(typ(x))) return powgi(x,n);
    1532        4270 :   pari_APPLY_same(vecpow(gel(x,i), n))
    1533             : }
    1534             : 
    1535             : GEN
    1536         903 : vecdiv(GEN x, GEN y)
    1537             : {
    1538         903 :   if (is_scalar_t(typ(x))) return gdiv(x,y);
    1539         301 :   pari_APPLY_same(vecdiv(gel(x,i), gel(y,i)))
    1540             : }
    1541             : 
    1542             : /* A ideal as a square t_MAT */
    1543             : static GEN
    1544      204187 : idealmulelt(GEN nf, GEN x, GEN A)
    1545             : {
    1546             :   long i, lx;
    1547             :   GEN dx, dA, D;
    1548      204187 :   if (lg(A) == 1) return cgetg(1, t_MAT);
    1549      204187 :   x = nf_to_scalar_or_basis(nf,x);
    1550      204187 :   if (typ(x) != t_COL)
    1551       74437 :     return isintzero(x)? cgetg(1,t_MAT): RgM_Rg_mul(A, Q_abs_shallow(x));
    1552      129750 :   x = Q_remove_denom(x, &dx);
    1553      129750 :   A = Q_remove_denom(A, &dA);
    1554      129750 :   x = zk_multable(nf, x);
    1555      129750 :   D = mulii(zkmultable_capZ(x), gcoeff(A,1,1));
    1556      129750 :   x = zkC_multable_mul(A, x);
    1557      129750 :   settyp(x, t_MAT); lx = lg(x);
    1558             :   /* x may contain scalars (at most 1 since the ideal is non-0)*/
    1559      448590 :   for (i=1; i<lx; i++)
    1560      327379 :     if (typ(gel(x,i)) == t_INT)
    1561             :     {
    1562        8539 :       if (i > 1) swap(gel(x,1), gel(x,i)); /* help HNF */
    1563        8539 :       gel(x,1) = scalarcol_shallow(gel(x,1), lx-1);
    1564        8539 :       break;
    1565             :     }
    1566      129750 :   x = ZM_hnfmodid(x, D);
    1567      129750 :   dx = mul_denom(dx,dA);
    1568      129750 :   return dx? gdiv(x,dx): x;
    1569             : }
    1570             : 
    1571             : /* nf a true nf, tx <= ty */
    1572             : static GEN
    1573     1302057 : idealmul_aux(GEN nf, GEN x, GEN y, long tx, long ty)
    1574             : {
    1575             :   GEN z, cx, cy;
    1576     1302057 :   switch(tx)
    1577             :   {
    1578             :     case id_PRINCIPAL:
    1579      254243 :       switch(ty)
    1580             :       {
    1581             :         case id_PRINCIPAL:
    1582       49860 :           return idealhnf_principal(nf, nfmul(nf,x,y));
    1583             :         case id_PRIME:
    1584             :         {
    1585         196 :           GEN p = pr_get_p(y), pi = pr_get_gen(y), cx;
    1586         196 :           if (pr_is_inert(y)) return RgM_Rg_mul(idealhnf_principal(nf,x),p);
    1587             : 
    1588          42 :           x = nf_to_scalar_or_basis(nf, x);
    1589          42 :           switch(typ(x))
    1590             :           {
    1591             :             case t_INT:
    1592          28 :               if (!signe(x)) return cgetg(1,t_MAT);
    1593          28 :               return ZM_Z_mul(pr_hnf(nf,y), absi_shallow(x));
    1594             :             case t_FRAC:
    1595           7 :               return RgM_Rg_mul(pr_hnf(nf,y), Q_abs_shallow(x));
    1596             :           }
    1597             :           /* t_COL */
    1598           7 :           x = Q_primitive_part(x, &cx);
    1599           7 :           x = zk_multable(nf, x);
    1600           7 :           z = shallowconcat(ZM_Z_mul(x,p), ZM_ZC_mul(x,pi));
    1601           7 :           z = ZM_hnfmodid(z, mulii(p, zkmultable_capZ(x)));
    1602           7 :           return cx? ZM_Q_mul(z, cx): z;
    1603             :         }
    1604             :         default: /* id_MAT */
    1605      204187 :           return idealmulelt(nf, x,y);
    1606             :       }
    1607             :     case id_PRIME:
    1608      971295 :       if (ty==id_PRIME)
    1609      966905 :       { y = pr_hnf(nf,y); cy = NULL; }
    1610             :       else
    1611        4390 :         y = Q_primitive_part(y, &cy);
    1612      971295 :       y = idealHNF_mul_two(nf,y,x);
    1613      971295 :       return cy? ZM_Q_mul(y,cy): y;
    1614             : 
    1615             :     default: /* id_MAT */
    1616             :     {
    1617       76519 :       long N = nf_get_degree(nf);
    1618       76519 :       if (lg(x)-1 != N || lg(y)-1 != N) pari_err_DIM("idealmul");
    1619       76505 :       x = Q_primitive_part(x, &cx);
    1620       76505 :       y = Q_primitive_part(y, &cy); cx = mul_content(cx,cy);
    1621       76505 :       y = idealHNF_mul(nf,x,y);
    1622       76505 :       return cx? ZM_Q_mul(y,cx): y;
    1623             :     }
    1624             :   }
    1625             : }
    1626             : 
    1627             : /* output the ideal product ix.iy */
    1628             : GEN
    1629     1302057 : idealmul(GEN nf, GEN x, GEN y)
    1630             : {
    1631             :   pari_sp av;
    1632             :   GEN res, ax, ay, z;
    1633     1302057 :   long tx = idealtyp(&x,&ax);
    1634     1302057 :   long ty = idealtyp(&y,&ay), f;
    1635     1302057 :   if (tx>ty) { swap(ax,ay); swap(x,y); lswap(tx,ty); }
    1636     1302057 :   f = (ax||ay); res = f? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1637     1302057 :   av = avma;
    1638     1302057 :   z = gerepileupto(av, idealmul_aux(checknf(nf), x,y, tx,ty));
    1639     1302043 :   if (!f) return z;
    1640       25140 :   if (ax && ay)
    1641       23391 :     ax = ext_mul(nf, ax, ay);
    1642             :   else
    1643        1749 :     ax = gcopy(ax? ax: ay);
    1644       25140 :   gel(res,1) = z; gel(res,2) = ax; return res;
    1645             : }
    1646             : 
    1647             : /* Return x, integral in 2-elt form, such that pr^2 = c * x. cf idealpowprime
    1648             :  * nf = true nf */
    1649             : static GEN
    1650       31511 : idealsqrprime(GEN nf, GEN pr, GEN *pc)
    1651             : {
    1652       31511 :   GEN p = pr_get_p(pr), q, gen;
    1653       31511 :   long e = pr_get_e(pr), f = pr_get_f(pr);
    1654             : 
    1655       31511 :   q = (e == 1)? sqri(p): p;
    1656       31511 :   if (e <= 2 && e * f == nf_get_degree(nf))
    1657             :   { /* pr^e = (p) */
    1658       10087 :     *pc = q;
    1659       10087 :     return mkvec2(gen_1,gen_0);
    1660             :   }
    1661       21424 :   gen = nfsqr(nf, pr_get_gen(pr));
    1662       21424 :   gen = FpC_red(gen, q);
    1663       21424 :   *pc = NULL;
    1664       21424 :   return mkvec2(q, gen);
    1665             : }
    1666             : /* cf idealpow_aux */
    1667             : static GEN
    1668       26349 : idealsqr_aux(GEN nf, GEN x, long tx)
    1669             : {
    1670       26349 :   GEN T = nf_get_pol(nf), m, cx, a, alpha;
    1671       26349 :   long N = degpol(T);
    1672       26349 :   switch(tx)
    1673             :   {
    1674             :     case id_PRINCIPAL:
    1675           0 :       return idealhnf_principal(nf, nfsqr(nf,x));
    1676             :     case id_PRIME:
    1677        8815 :       if (pr_is_inert(x)) return scalarmat(sqri(gel(x,1)), N);
    1678        8647 :       x = idealsqrprime(nf, x, &cx);
    1679        8647 :       x = idealhnf_two(nf,x);
    1680        8647 :       return cx? ZM_Z_mul(x, cx): x;
    1681             :     default:
    1682       17534 :       x = Q_primitive_part(x, &cx);
    1683       17534 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    1684       17534 :       alpha = nfsqr(nf,alpha);
    1685       17534 :       m = zk_scalar_or_multable(nf, alpha);
    1686       17534 :       if (typ(m) == t_INT) {
    1687        1197 :         x = gcdii(sqri(a), m);
    1688        1197 :         if (cx) x = gmul(x, gsqr(cx));
    1689        1197 :         x = scalarmat(x, N);
    1690             :       }
    1691             :       else
    1692             :       {
    1693       16337 :         x = ZM_hnfmodid(m, gcdii(sqri(a), zkmultable_capZ(m)));
    1694       16337 :         if (cx) cx = gsqr(cx);
    1695       16337 :         if (cx) x = ZM_Q_mul(x, cx);
    1696             :       }
    1697       17534 :       return x;
    1698             :   }
    1699             : }
    1700             : GEN
    1701       26349 : idealsqr(GEN nf, GEN x)
    1702             : {
    1703             :   pari_sp av;
    1704             :   GEN res, ax, z;
    1705       26349 :   long tx = idealtyp(&x,&ax);
    1706       26349 :   res = ax? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
    1707       26349 :   av = avma;
    1708       26349 :   z = gerepileupto(av, idealsqr_aux(checknf(nf), x, tx));
    1709       26349 :   if (!ax) return z;
    1710       26314 :   gel(res,1) = z;
    1711       26314 :   gel(res,2) = ext_sqr(nf, ax); return res;
    1712             : }
    1713             : 
    1714             : /* norm of an ideal */
    1715             : GEN
    1716        7105 : idealnorm(GEN nf, GEN x)
    1717             : {
    1718             :   pari_sp av;
    1719             :   GEN y, T;
    1720             :   long tx;
    1721             : 
    1722        7105 :   switch(idealtyp(&x,&y))
    1723             :   {
    1724         245 :     case id_PRIME: return pr_norm(x);
    1725        4802 :     case id_MAT: return RgM_det_triangular(x);
    1726             :   }
    1727             :   /* id_PRINCIPAL */
    1728        2058 :   nf = checknf(nf); T = nf_get_pol(nf); av = avma;
    1729        2058 :   x = nf_to_scalar_or_alg(nf, x);
    1730        2058 :   x = (typ(x) == t_POL)? RgXQ_norm(x, T): gpowgs(x, degpol(T));
    1731        2058 :   tx = typ(x);
    1732        2058 :   if (tx == t_INT) return gerepileuptoint(av, absi(x));
    1733         532 :   if (tx != t_FRAC) pari_err_TYPE("idealnorm",x);
    1734         532 :   return gerepileupto(av, Q_abs(x));
    1735             : }
    1736             : 
    1737             : /* I^(-1) = { x \in K, Tr(x D^(-1) I) \in Z }, D different of K/Q
    1738             :  *
    1739             :  * nf[5][6] = pp( D^(-1) ) = pp( HNF( T^(-1) ) ), T = (Tr(wi wj))
    1740             :  * nf[5][7] = same in 2-elt form.
    1741             :  * Assume I integral. Return the integral ideal (I\cap Z) I^(-1) */
    1742             : GEN
    1743      167945 : idealHNF_inv_Z(GEN nf, GEN I)
    1744             : {
    1745      167945 :   GEN J, dual, IZ = gcoeff(I,1,1); /* I \cap Z */
    1746      167945 :   if (isint1(IZ)) return matid(lg(I)-1);
    1747      156850 :   J = idealHNF_mul(nf,I, gmael(nf,5,7));
    1748             :  /* I in HNF, hence easily inverted; multiply by IZ to get integer coeffs
    1749             :   * missing content cancels while solving the linear equation */
    1750      156850 :   dual = shallowtrans( hnf_divscale(J, gmael(nf,5,6), IZ) );
    1751      156850 :   return ZM_hnfmodid(dual, IZ);
    1752             : }
    1753             : /* I HNF with rational coefficients (denominator d). */
    1754             : GEN
    1755       58799 : idealHNF_inv(GEN nf, GEN I)
    1756             : {
    1757       58799 :   GEN J, IQ = gcoeff(I,1,1); /* I \cap Q; d IQ = dI \cap Z */
    1758       58799 :   J = idealHNF_inv_Z(nf, Q_remove_denom(I, NULL)); /* = (dI)^(-1) * (d IQ) */
    1759       58799 :   return equali1(IQ)? J: RgM_Rg_div(J, IQ);
    1760             : }
    1761             : 
    1762             : /* return p * P^(-1)  [integral] */
    1763             : GEN
    1764       23702 : pr_inv_p(GEN pr)
    1765             : {
    1766       23702 :   if (pr_is_inert(pr)) return matid(pr_get_f(pr));
    1767       23135 :   return ZM_hnfmodid(pr_get_tau(pr), pr_get_p(pr));
    1768             : }
    1769             : GEN
    1770        3579 : pr_inv(GEN pr)
    1771             : {
    1772        3579 :   GEN p = pr_get_p(pr);
    1773        3579 :   if (pr_is_inert(pr)) return scalarmat(ginv(p), pr_get_f(pr));
    1774        3257 :   return RgM_Rg_div(ZM_hnfmodid(pr_get_tau(pr),p), p);
    1775             : }
    1776             : 
    1777             : GEN
    1778       98931 : idealinv(GEN nf, GEN x)
    1779             : {
    1780             :   GEN res, ax;
    1781             :   pari_sp av;
    1782       98931 :   long tx = idealtyp(&x,&ax), N;
    1783             : 
    1784       98931 :   res = ax? cgetg(3,t_VEC): NULL;
    1785       98931 :   nf = checknf(nf); av = avma;
    1786       98931 :   N = nf_get_degree(nf);
    1787       98931 :   switch (tx)
    1788             :   {
    1789             :     case id_MAT:
    1790       53157 :       if (lg(x)-1 != N) pari_err_DIM("idealinv");
    1791       53157 :       x = idealHNF_inv(nf,x); break;
    1792             :     case id_PRINCIPAL:
    1793       43056 :       x = nf_to_scalar_or_basis(nf, x);
    1794       43056 :       if (typ(x) != t_COL)
    1795       43014 :         x = idealhnf_principal(nf,ginv(x));
    1796             :       else
    1797             :       { /* nfinv + idealhnf where we already know (x) \cap Z */
    1798             :         GEN c, d;
    1799          42 :         x = Q_remove_denom(x, &c);
    1800          42 :         x = zk_inv(nf, x);
    1801          42 :         x = Q_remove_denom(x, &d); /* true inverse is c/d * x */
    1802          42 :         if (!d) /* x and x^(-1) integral => x a unit */
    1803           7 :           x = scalarmat_shallow(c? c: gen_1, N);
    1804             :         else
    1805             :         {
    1806          35 :           c = c? gdiv(c,d): ginv(d);
    1807          35 :           x = zk_multable(nf, x);
    1808          35 :           x = ZM_Q_mul(ZM_hnfmodid(x,d), c);
    1809             :         }
    1810             :       }
    1811       43056 :       break;
    1812             :     case id_PRIME:
    1813        2718 :       x = pr_inv(x); break;
    1814             :   }
    1815       98931 :   x = gerepileupto(av,x); if (!ax) return x;
    1816       10955 :   gel(res,1) = x;
    1817       10955 :   gel(res,2) = ext_inv(nf, ax); return res;
    1818             : }
    1819             : 
    1820             : /* write x = A/B, A,B coprime integral ideals */
    1821             : GEN
    1822       39954 : idealnumden(GEN nf, GEN x)
    1823             : {
    1824       39954 :   pari_sp av = avma;
    1825             :   GEN x0, ax, c, d, A, B, J;
    1826       39954 :   long tx = idealtyp(&x,&ax);
    1827       39954 :   nf = checknf(nf);
    1828       39954 :   switch (tx)
    1829             :   {
    1830             :     case id_PRIME:
    1831           7 :       retmkvec2(idealhnf(nf, x), gen_1);
    1832             :     case id_PRINCIPAL:
    1833             :     {
    1834             :       GEN xZ, mx;
    1835        3997 :       x = nf_to_scalar_or_basis(nf, x);
    1836        3997 :       switch(typ(x))
    1837             :       {
    1838         917 :         case t_INT: return gerepilecopy(av, mkvec2(absi_shallow(x),gen_1));
    1839          14 :         case t_FRAC:return gerepilecopy(av, mkvec2(absi_shallow(gel(x,1)), gel(x,2)));
    1840             :       }
    1841             :       /* t_COL */
    1842        3066 :       x = Q_remove_denom(x, &d);
    1843        3066 :       if (!d) return gerepilecopy(av, mkvec2(idealhnf(nf, x), gen_1));
    1844          35 :       mx = zk_multable(nf, x);
    1845          35 :       xZ = zkmultable_capZ(mx);
    1846          35 :       x = ZM_hnfmodid(mx, xZ); /* principal ideal (x) */
    1847          35 :       x0 = mkvec2(xZ, mx); /* same, for fast multiplication */
    1848          35 :       break;
    1849             :     }
    1850             :     default: /* id_MAT */
    1851             :     {
    1852       35950 :       long n = lg(x)-1;
    1853       35950 :       if (n == 0) return mkvec2(gen_0, gen_1);
    1854       35950 :       if (n != nf_get_degree(nf)) pari_err_DIM("idealnumden");
    1855       35950 :       x0 = x = Q_remove_denom(x, &d);
    1856       35950 :       if (!d) return gerepilecopy(av, mkvec2(x, gen_1));
    1857          14 :       break;
    1858             :     }
    1859             :   }
    1860          49 :   J = hnfmodid(x, d); /* = d/B */
    1861          49 :   c = gcoeff(J,1,1); /* (d/B) \cap Z, divides d */
    1862          49 :   B = idealHNF_inv_Z(nf, J); /* (d/B \cap Z) B/d */
    1863          49 :   if (!equalii(c,d)) B = ZM_Z_mul(B, diviiexact(d,c)); /* = B ! */
    1864          49 :   A = idealHNF_mul(nf, B, x0); /* d * (original x) * B = d A */
    1865          49 :   A = ZM_Z_divexact(A, d); /* = A ! */
    1866          49 :   return gerepilecopy(av, mkvec2(A, B));
    1867             : }
    1868             : 
    1869             : /* Return x, integral in 2-elt form, such that pr^n = c * x. Assume n != 0.
    1870             :  * nf = true nf */
    1871             : static GEN
    1872      113547 : idealpowprime(GEN nf, GEN pr, GEN n, GEN *pc)
    1873             : {
    1874      113547 :   GEN p = pr_get_p(pr), q, gen;
    1875             : 
    1876      113547 :   *pc = NULL;
    1877      113547 :   if (is_pm1(n)) /* n = 1 special cased for efficiency */
    1878             :   {
    1879       57624 :     q = p;
    1880       57624 :     if (typ(pr_get_tau(pr)) == t_INT) /* inert */
    1881             :     {
    1882           0 :       *pc = (signe(n) >= 0)? p: ginv(p);
    1883           0 :       return mkvec2(gen_1,gen_0);
    1884             :     }
    1885       57624 :     if (signe(n) >= 0) gen = pr_get_gen(pr);
    1886             :     else
    1887             :     {
    1888       10535 :       gen = pr_get_tau(pr); /* possibly t_MAT */
    1889       10535 :       *pc = ginv(p);
    1890             :     }
    1891             :   }
    1892       55923 :   else if (equalis(n,2)) return idealsqrprime(nf, pr, pc);
    1893             :   else
    1894             :   {
    1895       33059 :     long e = pr_get_e(pr), f = pr_get_f(pr);
    1896       33059 :     GEN r, m = truedvmdis(n, e, &r);
    1897       33059 :     if (e * f == nf_get_degree(nf))
    1898             :     { /* pr^e = (p) */
    1899        8869 :       if (signe(m)) *pc = powii(p,m);
    1900        8869 :       if (!signe(r)) return mkvec2(gen_1,gen_0);
    1901        3801 :       q = p;
    1902        3801 :       gen = nfpow(nf, pr_get_gen(pr), r);
    1903             :     }
    1904             :     else
    1905             :     {
    1906       24190 :       m = absi_shallow(m);
    1907       24190 :       if (signe(r)) m = addiu(m,1);
    1908       24190 :       q = powii(p,m); /* m = ceil(|n|/e) */
    1909       24190 :       if (signe(n) >= 0) gen = nfpow(nf, pr_get_gen(pr), n);
    1910             :       else
    1911             :       {
    1912        2779 :         gen = pr_get_tau(pr);
    1913        2779 :         if (typ(gen) == t_MAT) gen = gel(gen,1);
    1914        2779 :         n = negi(n);
    1915        2779 :         gen = ZC_Z_divexact(nfpow(nf, gen, n), powii(p, subii(n,m)));
    1916        2779 :         *pc = ginv(q);
    1917             :       }
    1918             :     }
    1919       27991 :     gen = FpC_red(gen, q);
    1920             :   }
    1921       85615 :   return mkvec2(q, gen);
    1922             : }
    1923             : 
    1924             : /* x * pr^n. Assume x in HNF or scalar (possibly non-integral) */
    1925             : GEN
    1926       91610 : idealmulpowprime(GEN nf, GEN x, GEN pr, GEN n)
    1927             : {
    1928             :   GEN c, cx, y;
    1929             :   long N;
    1930             : 
    1931       91610 :   nf = checknf(nf);
    1932       91610 :   N = nf_get_degree(nf);
    1933       91610 :   if (!signe(n)) return typ(x) == t_MAT? x: scalarmat_shallow(x, N);
    1934             : 
    1935             :   /* inert, special cased for efficiency */
    1936       91498 :   if (pr_is_inert(pr))
    1937             :   {
    1938        7686 :     GEN q = powii(pr_get_p(pr), n);
    1939        7686 :     return typ(x) == t_MAT? RgM_Rg_mul(x,q)
    1940        7686 :                           : scalarmat_shallow(gmul(Q_abs(x),q), N);
    1941             :   }
    1942             : 
    1943       83812 :   y = idealpowprime(nf, pr, n, &c);
    1944       83812 :   if (typ(x) == t_MAT)
    1945       81586 :   { x = Q_primitive_part(x, &cx); if (is_pm1(gcoeff(x,1,1))) x = NULL; }
    1946             :   else
    1947        2226 :   { cx = x; x = NULL; }
    1948       83812 :   cx = mul_content(c,cx);
    1949       83812 :   if (x)
    1950       54055 :     x = idealHNF_mul_two(nf,x,y);
    1951             :   else
    1952       29757 :     x = idealhnf_two(nf,y);
    1953       83812 :   if (cx) x = ZM_Q_mul(x,cx);
    1954       83812 :   return x;
    1955             : }
    1956             : GEN
    1957       17199 : idealdivpowprime(GEN nf, GEN x, GEN pr, GEN n)
    1958             : {
    1959       17199 :   return idealmulpowprime(nf,x,pr, negi(n));
    1960             : }
    1961             : 
    1962             : /* nf = true nf */
    1963             : static GEN
    1964      183534 : idealpow_aux(GEN nf, GEN x, long tx, GEN n)
    1965             : {
    1966      183534 :   GEN T = nf_get_pol(nf), m, cx, n1, a, alpha;
    1967      183534 :   long N = degpol(T), s = signe(n);
    1968      183534 :   if (!s) return matid(N);
    1969      177691 :   switch(tx)
    1970             :   {
    1971             :     case id_PRINCIPAL:
    1972           0 :       return idealhnf_principal(nf, nfpow(nf,x,n));
    1973             :     case id_PRIME:
    1974       75725 :       if (pr_is_inert(x)) return scalarmat(powii(gel(x,1), n), N);
    1975       29735 :       x = idealpowprime(nf, x, n, &cx);
    1976       29735 :       x = idealhnf_two(nf,x);
    1977       29735 :       return cx? ZM_Q_mul(x, cx): x;
    1978             :     default:
    1979      101966 :       if (is_pm1(n)) return (s < 0)? idealinv(nf, x): gcopy(x);
    1980       55799 :       n1 = (s < 0)? negi(n): n;
    1981             : 
    1982       55799 :       x = Q_primitive_part(x, &cx);
    1983       55799 :       a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
    1984       55799 :       alpha = nfpow(nf,alpha,n1);
    1985       55799 :       m = zk_scalar_or_multable(nf, alpha);
    1986       55799 :       if (typ(m) == t_INT) {
    1987         189 :         x = gcdii(powii(a,n1), m);
    1988         189 :         if (s<0) x = ginv(x);
    1989         189 :         if (cx) x = gmul(x, powgi(cx,n));
    1990         189 :         x = scalarmat(x, N);
    1991             :       }
    1992             :       else
    1993             :       {
    1994       55610 :         x = ZM_hnfmodid(m, gcdii(powii(a,n1), zkmultable_capZ(m)));
    1995       55610 :         if (cx) cx = powgi(cx,n);
    1996       55610 :         if (s<0) {
    1997           7 :           GEN xZ = gcoeff(x,1,1);
    1998           7 :           cx = cx ? gdiv(cx, xZ): ginv(xZ);
    1999           7 :           x = idealHNF_inv_Z(nf,x);
    2000             :         }
    2001       55610 :         if (cx) x = ZM_Q_mul(x, cx);
    2002             :       }
    2003       55799 :       return x;
    2004             :   }
    2005             : }
    2006             : 
    2007             : /* raise the ideal x to the power n (in Z) */
    2008             : GEN
    2009      183534 : idealpow(GEN nf, GEN x, GEN n)
    2010             : {
    2011             :   pari_sp av;
    2012             :   long tx;
    2013             :   GEN res, ax;
    2014             : 
    2015      183534 :   if (typ(n) != t_INT) pari_err_TYPE("idealpow",n);
    2016      183534 :   tx = idealtyp(&x,&ax);
    2017      183534 :   res = ax? cgetg(3,t_VEC): NULL;
    2018      183534 :   av = avma;
    2019      183534 :   x = gerepileupto(av, idealpow_aux(checknf(nf), x, tx, n));
    2020      183534 :   if (!ax) return x;
    2021        1174 :   ax = ext_pow(nf, ax, n);
    2022        1174 :   gel(res,1) = x;
    2023        1174 :   gel(res,2) = ax;
    2024        1174 :   return res;
    2025             : }
    2026             : 
    2027             : /* Return ideal^e in number field nf. e is a C integer. */
    2028             : GEN
    2029       21336 : idealpows(GEN nf, GEN ideal, long e)
    2030             : {
    2031       21336 :   long court[] = {evaltyp(t_INT) | _evallg(3),0,0};
    2032       21336 :   affsi(e,court); return idealpow(nf,ideal,court);
    2033             : }
    2034             : 
    2035             : static GEN
    2036       25161 : _idealmulred(GEN nf, GEN x, GEN y)
    2037       25161 : { return idealred(nf,idealmul(nf,x,y)); }
    2038             : static GEN
    2039       26328 : _idealsqrred(GEN nf, GEN x)
    2040       26328 : { return idealred(nf,idealsqr(nf,x)); }
    2041             : static GEN
    2042        8601 : _mul(void *data, GEN x, GEN y) { return _idealmulred((GEN)data,x,y); }
    2043             : static GEN
    2044       26328 : _sqr(void *data, GEN x) { return _idealsqrred((GEN)data, x); }
    2045             : 
    2046             : /* compute x^n (x ideal, n integer), reducing along the way */
    2047             : GEN
    2048       43500 : idealpowred(GEN nf, GEN x, GEN n)
    2049             : {
    2050       43500 :   pari_sp av = avma;
    2051             :   long s;
    2052             :   GEN y;
    2053             : 
    2054       43500 :   if (typ(n) != t_INT) pari_err_TYPE("idealpowred",n);
    2055       43500 :   s = signe(n); if (s == 0) return idealpow(nf,x,n);
    2056       42326 :   y = gen_pow(x, n, (void*)nf, &_sqr, &_mul);
    2057             : 
    2058       42326 :   if (s < 0) y = idealinv(nf,y);
    2059       42326 :   if (s < 0 || is_pm1(n)) y = idealred(nf,y);
    2060       42326 :   return gerepileupto(av,y);
    2061             : }
    2062             : 
    2063             : GEN
    2064       16560 : idealmulred(GEN nf, GEN x, GEN y)
    2065             : {
    2066       16560 :   pari_sp av = avma;
    2067       16560 :   return gerepileupto(av, _idealmulred(nf,x,y));
    2068             : }
    2069             : 
    2070             : long
    2071          91 : isideal(GEN nf,GEN x)
    2072             : {
    2073          91 :   long N, i, j, lx, tx = typ(x);
    2074             :   pari_sp av;
    2075             :   GEN T, xZ;
    2076             : 
    2077          91 :   nf = checknf(nf); T = nf_get_pol(nf); lx = lg(x);
    2078          91 :   if (tx==t_VEC && lx==3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
    2079          91 :   switch(tx)
    2080             :   {
    2081          14 :     case t_INT: case t_FRAC: return 1;
    2082           7 :     case t_POL: return varn(x) == varn(T);
    2083           7 :     case t_POLMOD: return RgX_equal_var(T, gel(x,1));
    2084          14 :     case t_VEC: return get_prid(x)? 1 : 0;
    2085          42 :     case t_MAT: break;
    2086           7 :     default: return 0;
    2087             :   }
    2088          42 :   N = degpol(T);
    2089          42 :   if (lx-1 != N) return (lx == 1);
    2090          28 :   if (nbrows(x) != N) return 0;
    2091             : 
    2092          28 :   av = avma; x = Q_primpart(x);
    2093          28 :   if (!ZM_ishnf(x)) return 0;
    2094          14 :   xZ = gcoeff(x,1,1);
    2095          21 :   for (j=2; j<=N; j++)
    2096          14 :     if (!dvdii(xZ, gcoeff(x,j,j))) { avma = av; return 0; }
    2097          14 :   for (i=2; i<=N; i++)
    2098          14 :     for (j=2; j<=N; j++)
    2099           7 :       if (! hnf_invimage(x, zk_ei_mul(nf,gel(x,i),j))) { avma = av; return 0; }
    2100           7 :   avma=av; return 1;
    2101             : }
    2102             : 
    2103             : GEN
    2104       23779 : idealdiv(GEN nf, GEN x, GEN y)
    2105             : {
    2106       23779 :   pari_sp av = avma, tetpil;
    2107       23779 :   GEN z = idealinv(nf,y);
    2108       23779 :   tetpil = avma; return gerepile(av,tetpil, idealmul(nf,x,z));
    2109             : }
    2110             : 
    2111             : /* This routine computes the quotient x/y of two ideals in the number field nf.
    2112             :  * It assumes that the quotient is an integral ideal.  The idea is to find an
    2113             :  * ideal z dividing y such that gcd(Nx/Nz, Nz) = 1.  Then
    2114             :  *
    2115             :  *   x + (Nx/Nz)    x
    2116             :  *   ----------- = ---
    2117             :  *   y + (Ny/Nz)    y
    2118             :  *
    2119             :  * Proof: we can assume x and y are integral. Let p be any prime ideal
    2120             :  *
    2121             :  * If p | Nz, then it divides neither Nx/Nz nor Ny/Nz (since Nx/Nz is the
    2122             :  * product of the integers N(x/y) and N(y/z)).  Both the numerator and the
    2123             :  * denominator on the left will be coprime to p.  So will x/y, since x/y is
    2124             :  * assumed integral and its norm N(x/y) is coprime to p.
    2125             :  *
    2126             :  * If instead p does not divide Nz, then v_p (Nx/Nz) = v_p (Nx) >= v_p(x).
    2127             :  * Hence v_p (x + Nx/Nz) = v_p(x).  Likewise for the denominators.  QED.
    2128             :  *
    2129             :  *                Peter Montgomery.  July, 1994. */
    2130             : static void
    2131           7 : err_divexact(GEN x, GEN y)
    2132           7 : { pari_err_DOMAIN("idealdivexact","denominator(x/y)", "!=",
    2133           0 :                   gen_1,mkvec2(x,y)); }
    2134             : GEN
    2135        1197 : idealdivexact(GEN nf, GEN x0, GEN y0)
    2136             : {
    2137        1197 :   pari_sp av = avma;
    2138             :   GEN x, y, xZ, yZ, Nx, Ny, Nz, cy, q, r;
    2139             : 
    2140        1197 :   nf = checknf(nf);
    2141        1197 :   x = idealhnf_shallow(nf, x0);
    2142        1197 :   y = idealhnf_shallow(nf, y0);
    2143        1197 :   if (lg(y) == 1) pari_err_INV("idealdivexact", y0);
    2144        1190 :   if (lg(x) == 1) { avma = av; return cgetg(1, t_MAT); } /* numerator is zero */
    2145        1190 :   y = Q_primitive_part(y, &cy);
    2146        1190 :   if (cy) x = RgM_Rg_div(x,cy);
    2147        1190 :   xZ = gcoeff(x,1,1); if (typ(xZ) != t_INT) err_divexact(x,y);
    2148        1183 :   yZ = gcoeff(y,1,1); if (isint1(yZ)) return gerepilecopy(av, x);
    2149         343 :   Nx = idealnorm(nf,x);
    2150         343 :   Ny = idealnorm(nf,y);
    2151         343 :   if (typ(Nx) != t_INT) err_divexact(x,y);
    2152         343 :   q = dvmdii(Nx,Ny, &r);
    2153         343 :   if (signe(r)) err_divexact(x,y);
    2154         343 :   if (is_pm1(q)) { avma = av; return matid(nf_get_degree(nf)); }
    2155             :   /* Find a norm Nz | Ny such that gcd(Nx/Nz, Nz) = 1 */
    2156         252 :   for (Nz = Ny;;) /* q = Nx/Nz */
    2157         168 :   {
    2158         420 :     GEN p1 = gcdii(Nz, q);
    2159         420 :     if (is_pm1(p1)) break;
    2160         168 :     Nz = diviiexact(Nz,p1);
    2161         168 :     q = mulii(q,p1);
    2162             :   }
    2163         252 :   xZ = gcoeff(x,1,1); q = gcdii(q, xZ);
    2164         252 :   if (!equalii(xZ,q))
    2165             :   { /* Replace x/y  by  x+(Nx/Nz) / y+(Ny/Nz) */
    2166          91 :     x = ZM_hnfmodid(x, q);
    2167             :     /* y reduced to unit ideal ? */
    2168          91 :     if (Nz == Ny) return gerepileupto(av, x);
    2169             : 
    2170           7 :     yZ = gcoeff(y,1,1); q = gcdii(diviiexact(Ny,Nz), yZ);
    2171           7 :     y = ZM_hnfmodid(y, q);
    2172             :   }
    2173         168 :   yZ = gcoeff(y,1,1);
    2174         168 :   y = idealHNF_mul(nf,x, idealHNF_inv_Z(nf,y));
    2175         168 :   return gerepileupto(av, ZM_Z_divexact(y, yZ));
    2176             : }
    2177             : 
    2178             : GEN
    2179          21 : idealintersect(GEN nf, GEN x, GEN y)
    2180             : {
    2181          21 :   pari_sp av = avma;
    2182             :   long lz, lx, i;
    2183             :   GEN z, dx, dy, xZ, yZ;;
    2184             : 
    2185          21 :   nf = checknf(nf);
    2186          21 :   x = idealhnf_shallow(nf,x);
    2187          21 :   y = idealhnf_shallow(nf,y);
    2188          21 :   if (lg(x) == 1 || lg(y) == 1) { avma = av; return cgetg(1,t_MAT); }
    2189          14 :   x = Q_remove_denom(x, &dx);
    2190          14 :   y = Q_remove_denom(y, &dy);
    2191          14 :   if (dx) y = ZM_Z_mul(y, dx);
    2192          14 :   if (dy) x = ZM_Z_mul(x, dy);
    2193          14 :   xZ = gcoeff(x,1,1);
    2194          14 :   yZ = gcoeff(y,1,1);
    2195          14 :   dx = mul_denom(dx,dy);
    2196          14 :   z = ZM_lll(shallowconcat(x,y), 0.99, LLL_KER); lz = lg(z);
    2197          14 :   lx = lg(x);
    2198          14 :   for (i=1; i<lz; i++) setlg(z[i], lx);
    2199          14 :   z = ZM_hnfmodid(ZM_mul(x,z), lcmii(xZ, yZ));
    2200          14 :   if (dx) z = RgM_Rg_div(z,dx);
    2201          14 :   return gerepileupto(av,z);
    2202             : }
    2203             : 
    2204             : /*******************************************************************/
    2205             : /*                                                                 */
    2206             : /*                      T2-IDEAL REDUCTION                         */
    2207             : /*                                                                 */
    2208             : /*******************************************************************/
    2209             : 
    2210             : static GEN
    2211          21 : chk_vdir(GEN nf, GEN vdir)
    2212             : {
    2213          21 :   long i, l = lg(vdir);
    2214             :   GEN v;
    2215          21 :   if (l != lg(nf_get_roots(nf))) pari_err_DIM("idealred");
    2216          14 :   switch(typ(vdir))
    2217             :   {
    2218           0 :     case t_VECSMALL: return vdir;
    2219          14 :     case t_VEC: break;
    2220           0 :     default: pari_err_TYPE("idealred",vdir);
    2221             :   }
    2222          14 :   v = cgetg(l, t_VECSMALL);
    2223          14 :   for (i = 1; i < l; i++) v[i] = itos(gceil(gel(vdir,i)));
    2224          14 :   return v;
    2225             : }
    2226             : 
    2227             : static void
    2228       26987 : twistG(GEN G, long r1, long i, long v)
    2229             : {
    2230       26987 :   long j, lG = lg(G);
    2231       26987 :   if (i <= r1) {
    2232       23560 :     for (j=1; j<lG; j++) gcoeff(G,i,j) = gmul2n(gcoeff(G,i,j), v);
    2233             :   } else {
    2234        3427 :     long k = (i<<1) - r1;
    2235       18285 :     for (j=1; j<lG; j++)
    2236             :     {
    2237       14858 :       gcoeff(G,k-1,j) = gmul2n(gcoeff(G,k-1,j), v);
    2238       14858 :       gcoeff(G,k  ,j) = gmul2n(gcoeff(G,k  ,j), v);
    2239             :     }
    2240             :   }
    2241       26987 : }
    2242             : 
    2243             : GEN
    2244      135480 : nf_get_Gtwist(GEN nf, GEN vdir)
    2245             : {
    2246             :   long i, l, v, r1;
    2247             :   GEN G;
    2248             : 
    2249      135480 :   if (!vdir) return nf_get_roundG(nf);
    2250       26008 :   if (typ(vdir) == t_MAT)
    2251             :   {
    2252       25987 :     long N = nf_get_degree(nf);
    2253       25987 :     if (lg(vdir) != N+1 || lgcols(vdir) != N+1) pari_err_DIM("idealred");
    2254       25987 :     return vdir;
    2255             :   }
    2256          21 :   vdir = chk_vdir(nf, vdir);
    2257          14 :   G = RgM_shallowcopy(nf_get_G(nf));
    2258          14 :   r1 = nf_get_r1(nf);
    2259          14 :   l = lg(vdir);
    2260          56 :   for (i=1; i<l; i++)
    2261             :   {
    2262          42 :     v = vdir[i]; if (!v) continue;
    2263          42 :     twistG(G, r1, i, v);
    2264             :   }
    2265          14 :   return RM_round_maxrank(G);
    2266             : }
    2267             : GEN
    2268       26945 : nf_get_Gtwist1(GEN nf, long i)
    2269             : {
    2270       26945 :   GEN G = RgM_shallowcopy( nf_get_G(nf) );
    2271       26945 :   long r1 = nf_get_r1(nf);
    2272       26945 :   twistG(G, r1, i, 10);
    2273       26945 :   return RM_round_maxrank(G);
    2274             : }
    2275             : 
    2276             : GEN
    2277       40784 : RM_round_maxrank(GEN G0)
    2278             : {
    2279       40784 :   long e, r = lg(G0)-1;
    2280       40784 :   pari_sp av = avma;
    2281       40784 :   GEN G = G0;
    2282       40784 :   for (e = 4; ; e <<= 1)
    2283           0 :   {
    2284       40784 :     GEN H = ground(G);
    2285       81568 :     if (ZM_rank(H) == r) return H; /* maximal rank ? */
    2286           0 :     avma = av;
    2287           0 :     G = gmul2n(G0, e);
    2288             :   }
    2289             : }
    2290             : 
    2291             : GEN
    2292      135473 : idealred0(GEN nf, GEN I, GEN vdir)
    2293             : {
    2294      135473 :   pari_sp av = avma;
    2295      135473 :   GEN G, aI, IZ, J, y, yZ, my, c1 = NULL;
    2296             :   long N;
    2297             : 
    2298      135473 :   nf = checknf(nf);
    2299      135473 :   N = nf_get_degree(nf);
    2300             :   /* put first for sanity checks, unused when I obviously principal */
    2301      135473 :   G = nf_get_Gtwist(nf, vdir);
    2302      135466 :   switch (idealtyp(&I,&aI))
    2303             :   {
    2304             :     case id_PRIME:
    2305       22323 :       if (pr_is_inert(I)) {
    2306         581 :         if (!aI) { avma = av; return matid(N); }
    2307         581 :         c1 = gel(I,1); I = matid(N);
    2308         581 :         goto END;
    2309             :       }
    2310       21742 :       IZ = pr_get_p(I);
    2311       21742 :       J = pr_inv_p(I);
    2312       21742 :       I = idealhnf_two(nf,I);
    2313       21742 :       break;
    2314             :     case id_MAT:
    2315      113115 :       I = Q_primitive_part(I, &c1);
    2316      113115 :       IZ = gcoeff(I,1,1);
    2317      113115 :       if (is_pm1(IZ))
    2318             :       {
    2319        7903 :         if (!aI) { avma = av; return matid(N); }
    2320        7847 :         goto END;
    2321             :       }
    2322      105212 :       J = idealHNF_inv_Z(nf, I);
    2323      105212 :       break;
    2324             :     default: /* id_PRINCIPAL, silly case */
    2325          21 :       if (gequal0(I)) I = cgetg(1,t_MAT); else { c1 = I; I = matid(N); }
    2326          21 :       if (!aI) return I;
    2327          14 :       goto END;
    2328             :   }
    2329             :   /* now I integral, HNF; and J = (I\cap Z) I^(-1), integral */
    2330      126954 :   y = idealpseudomin(J, G); /* small elt in (I\cap Z)I^(-1), integral */
    2331      126954 :   if (ZV_isscalar(y))
    2332             :   { /* already reduced */
    2333       43603 :     if (!aI) return gerepilecopy(av, I);
    2334       43204 :     goto END;
    2335             :   }
    2336             : 
    2337       83351 :   my = zk_multable(nf, y);
    2338       83351 :   I = ZM_Z_divexact(ZM_mul(my, I), IZ); /* y I / (I\cap Z), integral */
    2339       83351 :   c1 = mul_content(c1, IZ);
    2340       83351 :   my = ZM_gauss(my, col_ei(N,1)); /* y^-1 */
    2341       83351 :   yZ = Q_denom(my); /* (y) \cap Z */
    2342       83351 :   I = hnfmodid(I, yZ);
    2343       83351 :   if (!aI) return gerepileupto(av, I);
    2344       82077 :   c1 = RgC_Rg_mul(my, c1);
    2345             : END:
    2346      133723 :   if (c1) aI = ext_mul(nf, aI,c1);
    2347      133723 :   return gerepilecopy(av, mkvec2(I, aI));
    2348             : }
    2349             : 
    2350             : GEN
    2351           7 : idealmin(GEN nf, GEN x, GEN vdir)
    2352             : {
    2353           7 :   pari_sp av = avma;
    2354             :   GEN y, dx;
    2355           7 :   nf = checknf(nf);
    2356           7 :   switch( idealtyp(&x,&y) )
    2357             :   {
    2358           0 :     case id_PRINCIPAL: return gcopy(x);
    2359           0 :     case id_PRIME: x = pr_hnf(nf,x); break;
    2360           7 :     case id_MAT: if (lg(x) == 1) return gen_0;
    2361             :   }
    2362           7 :   x = Q_remove_denom(x, &dx);
    2363           7 :   y = idealpseudomin(x, nf_get_Gtwist(nf,vdir));
    2364           7 :   if (dx) y = RgC_Rg_div(y, dx);
    2365           7 :   return gerepileupto(av, y);
    2366             : }
    2367             : 
    2368             : /*******************************************************************/
    2369             : /*                                                                 */
    2370             : /*                   APPROXIMATION THEOREM                         */
    2371             : /*                                                                 */
    2372             : /*******************************************************************/
    2373             : /* a = ppi(a,b) ppo(a,b), where ppi regroups primes common to a and b
    2374             :  * and ppo(a,b) = Z_ppo(a,b) */
    2375             : /* return gcd(a,b),ppi(a,b),ppo(a,b) */
    2376             : GEN
    2377      454020 : Z_ppio(GEN a, GEN b)
    2378             : {
    2379      454020 :   GEN x, y, d = gcdii(a,b);
    2380      454020 :   if (is_pm1(d)) return mkvec3(gen_1, gen_1, a);
    2381      345079 :   x = d; y = diviiexact(a,d);
    2382             :   for(;;)
    2383       62713 :   {
    2384      407792 :     GEN g = gcdii(x,y);
    2385      407792 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2386       62713 :     x = mulii(x,g); y = diviiexact(y,g);
    2387             :   }
    2388             : }
    2389             : /* a = ppg(a,b)pple(a,b), where ppg regroups primes such that v(a) > v(b)
    2390             :  * and pple all others */
    2391             : /* return gcd(a,b),ppg(a,b),pple(a,b) */
    2392             : GEN
    2393           0 : Z_ppgle(GEN a, GEN b)
    2394             : {
    2395           0 :   GEN x, y, g, d = gcdii(a,b);
    2396           0 :   if (equalii(a, d)) return mkvec3(a, gen_1, a);
    2397           0 :   x = diviiexact(a,d); y = d;
    2398             :   for(;;)
    2399             :   {
    2400           0 :     g = gcdii(x,y);
    2401           0 :     if (is_pm1(g)) return mkvec3(d, x, y);
    2402           0 :     x = mulii(x,g); y = diviiexact(y,g);
    2403             :   }
    2404             : }
    2405             : static void
    2406           0 : Z_dcba_rec(GEN L, GEN a, GEN b)
    2407             : {
    2408             :   GEN x, r, v, g, h, c, c0;
    2409             :   long n;
    2410           0 :   if (is_pm1(b)) {
    2411           0 :     if (!is_pm1(a)) vectrunc_append(L, a);
    2412           0 :     return;
    2413             :   }
    2414           0 :   v = Z_ppio(a,b);
    2415           0 :   a = gel(v,2);
    2416           0 :   r = gel(v,3);
    2417           0 :   if (!is_pm1(r)) vectrunc_append(L, r);
    2418           0 :   v = Z_ppgle(a,b);
    2419           0 :   g = gel(v,1);
    2420           0 :   h = gel(v,2);
    2421           0 :   x = c0 = gel(v,3);
    2422           0 :   for (n = 1; !is_pm1(h); n++)
    2423             :   {
    2424             :     GEN d, y;
    2425             :     long i;
    2426           0 :     v = Z_ppgle(h,sqri(g));
    2427           0 :     g = gel(v,1);
    2428           0 :     h = gel(v,2);
    2429           0 :     c = gel(v,3); if (is_pm1(c)) continue;
    2430           0 :     d = gcdii(c,b);
    2431           0 :     x = mulii(x,d);
    2432           0 :     y = d; for (i=1; i < n; i++) y = sqri(y);
    2433           0 :     Z_dcba_rec(L, diviiexact(c,y), d);
    2434             :   }
    2435           0 :   Z_dcba_rec(L,diviiexact(b,x), c0);
    2436             : }
    2437             : static GEN
    2438     3069759 : Z_cba_rec(GEN L, GEN a, GEN b)
    2439             : {
    2440             :   GEN g;
    2441     3069759 :   if (lg(L) > 10)
    2442             :   { /* a few naive steps before switching to dcba */
    2443           0 :     Z_dcba_rec(L, a, b);
    2444           0 :     return gel(L, lg(L)-1);
    2445             :   }
    2446     3069759 :   if (is_pm1(a)) return b;
    2447     1823948 :   g = gcdii(a,b);
    2448     1823948 :   if (is_pm1(g)) { vectrunc_append(L, a); return b; }
    2449     1362494 :   a = diviiexact(a,g);
    2450     1362494 :   b = diviiexact(b,g);
    2451     1362494 :   return Z_cba_rec(L, Z_cba_rec(L, a, g), b);
    2452             : }
    2453             : GEN
    2454      344771 : Z_cba(GEN a, GEN b)
    2455             : {
    2456      344771 :   GEN L = vectrunc_init(expi(a) + expi(b) + 2);
    2457      344771 :   GEN t = Z_cba_rec(L, a, b);
    2458      344771 :   if (!is_pm1(t)) vectrunc_append(L, t);
    2459      344771 :   return L;
    2460             : }
    2461             : /* P = coprime base, extend it by b; TODO: quadratic for now */
    2462             : GEN
    2463           0 : ZV_cba_extend(GEN P, GEN b)
    2464             : {
    2465           0 :   long i, l = lg(P);
    2466           0 :   GEN w = cgetg(l+1, t_VEC);
    2467           0 :   for (i = 1; i < l; i++)
    2468             :   {
    2469           0 :     GEN v = Z_cba(gel(P,i), b);
    2470           0 :     long nv = lg(v)-1;
    2471           0 :     gel(w,i) = vecslice(v, 1, nv-1); /* those divide P[i] but not b */
    2472           0 :     b = gel(v,nv);
    2473             :   }
    2474           0 :   gel(w,l) = b; return shallowconcat1(w);
    2475             : }
    2476             : GEN
    2477           0 : ZV_cba(GEN v)
    2478             : {
    2479           0 :   long i, l = lg(v);
    2480             :   GEN P;
    2481           0 :   if (l <= 2) return v;
    2482           0 :   P = Z_cba(gel(v,1), gel(v,2));
    2483           0 :   for (i = 3; i < l; i++) P = ZV_cba_extend(P, gel(v,i));
    2484           0 :   return P;
    2485             : }
    2486             : 
    2487             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2488             : GEN
    2489     1777195 : Z_ppo(GEN x, GEN f)
    2490             : {
    2491             :   for (;;)
    2492             :   {
    2493     2754672 :     f = gcdii(x, f); if (is_pm1(f)) break;
    2494      977477 :     x = diviiexact(x, f);
    2495             :   }
    2496      799718 :   return x;
    2497             : }
    2498             : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
    2499             : ulong
    2500    41355407 : u_ppo(ulong x, ulong f)
    2501             : {
    2502             :   for (;;)
    2503             :   {
    2504    49441024 :     f = ugcd(x, f); if (f == 1) break;
    2505     8085617 :     x /= f;
    2506             :   }
    2507    33269790 :   return x;
    2508             : }
    2509             : 
    2510             : /* x t_INT, f ideal. Write x = x1 x2, sqf(x1) | f, (x2,f) = 1. Return x2 */
    2511             : static GEN
    2512         140 : nf_coprime_part(GEN nf, GEN x, GEN listpr)
    2513             : {
    2514         140 :   long v, j, lp = lg(listpr), N = nf_get_degree(nf);
    2515             :   GEN x1, x2, ex;
    2516             : 
    2517             : #if 0 /*1) via many gcds. Expensive ! */
    2518             :   GEN f = idealprodprime(nf, listpr);
    2519             :   f = ZM_hnfmodid(f, x); /* first gcd is less expensive since x in Z */
    2520             :   x = scalarmat(x, N);
    2521             :   for (;;)
    2522             :   {
    2523             :     if (gequal1(gcoeff(f,1,1))) break;
    2524             :     x = idealdivexact(nf, x, f);
    2525             :     f = ZM_hnfmodid(shallowconcat(f,x), gcoeff(x,1,1)); /* gcd(f,x) */
    2526             :   }
    2527             :   x2 = x;
    2528             : #else /*2) from prime decomposition */
    2529         140 :   x1 = NULL;
    2530         399 :   for (j=1; j<lp; j++)
    2531             :   {
    2532         259 :     GEN pr = gel(listpr,j);
    2533         259 :     v = Z_pval(x, pr_get_p(pr)); if (!v) continue;
    2534             : 
    2535         140 :     ex = muluu(v, pr_get_e(pr)); /* = v_pr(x) > 0 */
    2536         140 :     x1 = x1? idealmulpowprime(nf, x1, pr, ex)
    2537         140 :            : idealpow(nf, pr, ex);
    2538             :   }
    2539         140 :   x = scalarmat(x, N);
    2540         140 :   x2 = x1? idealdivexact(nf, x, x1): x;
    2541             : #endif
    2542         140 :   return x2;
    2543             : }
    2544             : 
    2545             : /* L0 in K^*, assume (L0,f) = 1. Return L integral, L0 = L mod f  */
    2546             : GEN
    2547        5600 : make_integral(GEN nf, GEN L0, GEN f, GEN listpr)
    2548             : {
    2549             :   GEN fZ, t, L, D2, d1, d2, d;
    2550             : 
    2551        5600 :   L = Q_remove_denom(L0, &d);
    2552        5600 :   if (!d) return L0;
    2553             : 
    2554             :   /* L0 = L / d, L integral */
    2555        2149 :   fZ = gcoeff(f,1,1);
    2556        2149 :   if (typ(L) == t_INT) return Fp_mul(L, Fp_inv(d, fZ), fZ);
    2557             :   /* Kill denom part coprime to fZ */
    2558        1918 :   d2 = Z_ppo(d, fZ);
    2559        1918 :   t = Fp_inv(d2, fZ); if (!is_pm1(t)) L = ZC_Z_mul(L,t);
    2560        1918 :   if (equalii(d, d2)) return L;
    2561             : 
    2562         140 :   d1 = diviiexact(d, d2);
    2563             :   /* L0 = (L / d1) mod f. d1 not coprime to f
    2564             :    * write (d1) = D1 D2, D2 minimal, (D2,f) = 1. */
    2565         140 :   D2 = nf_coprime_part(nf, d1, listpr);
    2566         140 :   t = idealaddtoone_i(nf, D2, f); /* in D2, 1 mod f */
    2567         140 :   L = nfmuli(nf,t,L);
    2568             : 
    2569             :   /* if (L0, f) = 1, then L in D1 ==> in D1 D2 = (d1) */
    2570         140 :   return Q_div_to_int(L, d1); /* exact division */
    2571             : }
    2572             : 
    2573             : /* assume L is a list of prime ideals. Return the product */
    2574             : GEN
    2575         329 : idealprodprime(GEN nf, GEN L)
    2576             : {
    2577         329 :   long l = lg(L), i;
    2578             :   GEN z;
    2579         329 :   if (l == 1) return matid(nf_get_degree(nf));
    2580         329 :   z = pr_hnf(nf, gel(L,1));
    2581         329 :   for (i=2; i<l; i++) z = idealHNF_mul_two(nf,z, gel(L,i));
    2582         329 :   return z;
    2583             : }
    2584             : 
    2585             : /* optimize for the frequent case I = nfhnf()[2]: lots of them are 1 */
    2586             : GEN
    2587         784 : idealprod(GEN nf, GEN I)
    2588             : {
    2589         784 :   long i, l = lg(I);
    2590             :   GEN z;
    2591         889 :   for (i = 1; i < l; i++)
    2592         868 :     if (!equali1(gel(I,i))) break;
    2593         784 :   if (i == l) return gen_1;
    2594         763 :   z = gel(I,i);
    2595         763 :   for (i++; i<l; i++) z = idealmul(nf, z, gel(I,i));
    2596         763 :   return z;
    2597             : }
    2598             : 
    2599             : /* v_pr(idealprod(nf,I)) */
    2600             : long
    2601        1946 : idealprodval(GEN nf, GEN I, GEN pr)
    2602             : {
    2603        1946 :   long i, l = lg(I), v = 0;
    2604       11067 :   for (i = 1; i < l; i++)
    2605        9121 :     if (!equali1(gel(I,i))) v += idealval(nf, gel(I,i), pr);
    2606        1946 :   return v;
    2607             : }
    2608             : 
    2609             : /* assume L is a list of prime ideals. Return prod L[i]^e[i] */
    2610             : GEN
    2611        8337 : factorbackprime(GEN nf, GEN L, GEN e)
    2612             : {
    2613        8337 :   long l = lg(L), i;
    2614             :   GEN z;
    2615             : 
    2616        8337 :   if (l == 1) return matid(nf_get_degree(nf));
    2617        8323 :   z = idealpow(nf, gel(L,1), gel(e,1));
    2618       12663 :   for (i=2; i<l; i++)
    2619        4340 :     if (signe(gel(e,i))) z = idealmulpowprime(nf,z, gel(L,i),gel(e,i));
    2620        8323 :   return z;
    2621             : }
    2622             : 
    2623             : /* F in Z, divisible exactly by pr.p. Return F-uniformizer for pr, i.e.
    2624             :  * a t in Z_K such that v_pr(t) = 1 and (t, F/pr) = 1 */
    2625             : GEN
    2626       18067 : pr_uniformizer(GEN pr, GEN F)
    2627             : {
    2628       18067 :   GEN p = pr_get_p(pr), t = pr_get_gen(pr);
    2629       18067 :   if (!equalii(F, p))
    2630             :   {
    2631        7707 :     long e = pr_get_e(pr);
    2632        7707 :     GEN u, v, q = (e == 1)? sqri(p): p;
    2633        7707 :     u = mulii(q, Fp_inv(q, diviiexact(F,p))); /* 1 mod F/p, 0 mod q */
    2634        7707 :     v = subui(1UL, u); /* 0 mod F/p, 1 mod q */
    2635        7707 :     if (pr_is_inert(pr))
    2636           0 :       t = addii(mulii(p, v), u);
    2637             :     else
    2638             :     {
    2639        7707 :       t = ZC_Z_mul(t, v);
    2640        7707 :       gel(t,1) = addii(gel(t,1), u); /* return u + vt */
    2641             :     }
    2642             :   }
    2643       18067 :   return t;
    2644             : }
    2645             : /* L = list of prime ideals, return lcm_i (L[i] \cap \ZM) */
    2646             : GEN
    2647       35243 : prV_lcm_capZ(GEN L)
    2648             : {
    2649       35243 :   long i, r = lg(L);
    2650             :   GEN F;
    2651       35243 :   if (r == 1) return gen_1;
    2652       29811 :   F = pr_get_p(gel(L,1));
    2653       44492 :   for (i = 2; i < r; i++)
    2654             :   {
    2655       14681 :     GEN pr = gel(L,i), p = pr_get_p(pr);
    2656       14681 :     if (!dvdii(F, p)) F = mulii(F,p);
    2657             :   }
    2658       29811 :   return F;
    2659             : }
    2660             : 
    2661             : /* Given a prime ideal factorization with possibly zero or negative
    2662             :  * exponents, gives b such that v_p(b) = v_p(x) for all prime ideals pr | x
    2663             :  * and v_pr(b) >= 0 for all other pr.
    2664             :  * For optimal performance, all [anti-]uniformizers should be precomputed,
    2665             :  * but no support for this yet.
    2666             :  *
    2667             :  * If nored, do not reduce result.
    2668             :  * No garbage collecting */
    2669             : static GEN
    2670       20816 : idealapprfact_i(GEN nf, GEN x, int nored)
    2671             : {
    2672             :   GEN z, d, L, e, e2, F;
    2673             :   long i, r;
    2674             :   int flagden;
    2675             : 
    2676       20816 :   nf = checknf(nf);
    2677       20816 :   L = gel(x,1);
    2678       20816 :   e = gel(x,2);
    2679       20816 :   F = prV_lcm_capZ(L);
    2680       20816 :   flagden = 0;
    2681       20816 :   z = NULL; r = lg(e);
    2682       44238 :   for (i = 1; i < r; i++)
    2683             :   {
    2684       23422 :     long s = signe(gel(e,i));
    2685             :     GEN pi, q;
    2686       23422 :     if (!s) continue;
    2687       15946 :     if (s < 0) flagden = 1;
    2688       15946 :     pi = pr_uniformizer(gel(L,i), F);
    2689       15946 :     q = nfpow(nf, pi, gel(e,i));
    2690       15946 :     z = z? nfmul(nf, z, q): q;
    2691             :   }
    2692       20816 :   if (!z) return gen_1;
    2693       10939 :   if (nored || typ(z) != t_COL) return z;
    2694        2716 :   e2 = cgetg(r, t_VEC);
    2695        2716 :   for (i=1; i<r; i++) gel(e2,i) = addiu(gel(e,i), 1);
    2696        2716 :   x = factorbackprime(nf, L,e2);
    2697        2716 :   if (flagden) /* denominator */
    2698             :   {
    2699        2702 :     z = Q_remove_denom(z, &d);
    2700        2702 :     d = diviiexact(d, Z_ppo(d, F));
    2701        2702 :     x = RgM_Rg_mul(x, d);
    2702             :   }
    2703             :   else
    2704          14 :     d = NULL;
    2705        2716 :   z = ZC_reducemodlll(z, x);
    2706        2716 :   return d? RgC_Rg_div(z,d): z;
    2707             : }
    2708             : 
    2709             : GEN
    2710           0 : idealapprfact(GEN nf, GEN x) {
    2711           0 :   pari_sp av = avma;
    2712           0 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    2713             : }
    2714             : GEN
    2715          14 : idealappr(GEN nf, GEN x) {
    2716          14 :   pari_sp av = avma;
    2717          14 :   if (!is_nf_extfactor(x)) x = idealfactor(nf, x);
    2718          14 :   return gerepileupto(av, idealapprfact_i(nf, x, 0));
    2719             : }
    2720             : 
    2721             : /* OBSOLETE */
    2722             : GEN
    2723          14 : idealappr0(GEN nf, GEN x, long fl) { (void)fl; return idealappr(nf, x); }
    2724             : 
    2725             : static GEN
    2726          21 : mat_ideal_two_elt2(GEN nf, GEN x, GEN a)
    2727             : {
    2728          21 :   GEN F = idealfactor(nf,a), P = gel(F,1), E = gel(F,2);
    2729          21 :   long i, r = lg(E);
    2730          21 :   for (i=1; i<r; i++) gel(E,i) = stoi( idealval(nf,x,gel(P,i)) );
    2731          21 :   return idealapprfact_i(nf,F,1);
    2732             : }
    2733             : 
    2734             : static void
    2735          14 : not_in_ideal(GEN a) {
    2736          14 :   pari_err_DOMAIN("idealtwoelt2","element mod ideal", "!=", gen_0, a);
    2737           0 : }
    2738             : /* x integral in HNF, a an 'nf' */
    2739             : static int
    2740          28 : in_ideal(GEN x, GEN a)
    2741             : {
    2742          28 :   switch(typ(a))
    2743             :   {
    2744          14 :     case t_INT: return dvdii(a, gcoeff(x,1,1));
    2745           7 :     case t_COL: return RgV_is_ZV(a) && !!hnf_invimage(x, a);
    2746           7 :     default: return 0;
    2747             :   }
    2748             : }
    2749             : 
    2750             : /* Given an integral ideal x and a in x, gives a b such that
    2751             :  * x = aZ_K + bZ_K using the approximation theorem */
    2752             : GEN
    2753          42 : idealtwoelt2(GEN nf, GEN x, GEN a)
    2754             : {
    2755          42 :   pari_sp av = avma;
    2756             :   GEN cx, b;
    2757             : 
    2758          42 :   nf = checknf(nf);
    2759          42 :   a = nf_to_scalar_or_basis(nf, a);
    2760          42 :   x = idealhnf_shallow(nf,x);
    2761          42 :   if (lg(x) == 1)
    2762             :   {
    2763          14 :     if (!isintzero(a)) not_in_ideal(a);
    2764           7 :     avma = av; return gen_0;
    2765             :   }
    2766          28 :   x = Q_primitive_part(x, &cx);
    2767          28 :   if (cx) a = gdiv(a, cx);
    2768          28 :   if (!in_ideal(x, a)) not_in_ideal(a);
    2769          21 :   b = mat_ideal_two_elt2(nf, x, a);
    2770          21 :   if (typ(b) == t_COL)
    2771             :   {
    2772          14 :     GEN mod = idealhnf_principal(nf,a);
    2773          14 :     b = ZC_hnfrem(b,mod);
    2774          14 :     if (ZV_isscalar(b)) b = gel(b,1);
    2775             :   }
    2776             :   else
    2777             :   {
    2778           7 :     GEN aZ = typ(a) == t_COL? Q_denom(zk_inv(nf,a)): a; /* (a) \cap Z */
    2779           7 :     b = centermodii(b, aZ, shifti(aZ,-1));
    2780             :   }
    2781          21 :   b = cx? gmul(b,cx): gcopy(b);
    2782          21 :   return gerepileupto(av, b);
    2783             : }
    2784             : 
    2785             : /* Given 2 integral ideals x and y in nf, returns a beta in nf such that
    2786             :  * beta * x is an integral ideal coprime to y */
    2787             : GEN
    2788       12572 : idealcoprimefact(GEN nf, GEN x, GEN fy)
    2789             : {
    2790       12572 :   GEN L = gel(fy,1), e;
    2791       12572 :   long i, r = lg(L);
    2792             : 
    2793       12572 :   e = cgetg(r, t_COL);
    2794       12572 :   for (i=1; i<r; i++) gel(e,i) = stoi( -idealval(nf,x,gel(L,i)) );
    2795       12572 :   return idealapprfact_i(nf, mkmat2(L,e), 0);
    2796             : }
    2797             : GEN
    2798          70 : idealcoprime(GEN nf, GEN x, GEN y)
    2799             : {
    2800          70 :   pari_sp av = avma;
    2801          70 :   return gerepileupto(av, idealcoprimefact(nf, x, idealfactor(nf,y)));
    2802             : }
    2803             : 
    2804             : GEN
    2805           7 : nfmulmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    2806             : {
    2807           7 :   pari_sp av = avma;
    2808           7 :   GEN z, p, pr = modpr, T;
    2809             : 
    2810           7 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    2811           0 :   x = nf_to_Fq(nf,x,modpr);
    2812           0 :   y = nf_to_Fq(nf,y,modpr);
    2813           0 :   z = Fq_mul(x,y,T,p);
    2814           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    2815             : }
    2816             : 
    2817             : GEN
    2818           0 : nfdivmodpr(GEN nf, GEN x, GEN y, GEN modpr)
    2819             : {
    2820           0 :   pari_sp av = avma;
    2821           0 :   nf = checknf(nf);
    2822           0 :   return gerepileupto(av, nfreducemodpr(nf, nfdiv(nf,x,y), modpr));
    2823             : }
    2824             : 
    2825             : GEN
    2826           0 : nfpowmodpr(GEN nf, GEN x, GEN k, GEN modpr)
    2827             : {
    2828           0 :   pari_sp av=avma;
    2829           0 :   GEN z, T, p, pr = modpr;
    2830             : 
    2831           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    2832           0 :   z = nf_to_Fq(nf,x,modpr);
    2833           0 :   z = Fq_pow(z,k,T,p);
    2834           0 :   return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
    2835             : }
    2836             : 
    2837             : GEN
    2838           0 : nfkermodpr(GEN nf, GEN x, GEN modpr)
    2839             : {
    2840           0 :   pari_sp av = avma;
    2841           0 :   GEN T, p, pr = modpr;
    2842             : 
    2843           0 :   nf = checknf(nf); modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    2844           0 :   if (typ(x)!=t_MAT) pari_err_TYPE("nfkermodpr",x);
    2845           0 :   x = nfM_to_FqM(x, nf, modpr);
    2846           0 :   return gerepilecopy(av, FqM_to_nfM(FqM_ker(x,T,p), modpr));
    2847             : }
    2848             : 
    2849             : GEN
    2850           0 : nfsolvemodpr(GEN nf, GEN a, GEN b, GEN pr)
    2851             : {
    2852           0 :   const char *f = "nfsolvemodpr";
    2853           0 :   pari_sp av = avma;
    2854             :   GEN T, p, modpr;
    2855             : 
    2856           0 :   nf = checknf(nf);
    2857           0 :   modpr = nf_to_Fq_init(nf, &pr,&T,&p);
    2858           0 :   if (typ(a)!=t_MAT) pari_err_TYPE(f,a);
    2859           0 :   a = nfM_to_FqM(a, nf, modpr);
    2860           0 :   switch(typ(b))
    2861             :   {
    2862             :     case t_MAT:
    2863           0 :       b = nfM_to_FqM(b, nf, modpr);
    2864           0 :       b = FqM_gauss(a,b,T,p);
    2865           0 :       if (!b) pari_err_INV(f,a);
    2866           0 :       a = FqM_to_nfM(b, modpr);
    2867           0 :       break;
    2868             :     case t_COL:
    2869           0 :       b = nfV_to_FqV(b, nf, modpr);
    2870           0 :       b = FqM_FqC_gauss(a,b,T,p);
    2871           0 :       if (!b) pari_err_INV(f,a);
    2872           0 :       a = FqV_to_nfV(b, modpr);
    2873           0 :       break;
    2874           0 :     default: pari_err_TYPE(f,b);
    2875             :   }
    2876           0 :   return gerepilecopy(av, a);
    2877             : }

Generated by: LCOV version 1.13