Code coverage tests

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

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

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

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

Generated by: LCOV version 1.13